Interesting Yang Yui Triangle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 332    Accepted Submission(s): 199

Problem Description
Harry
is a Junior middle student. He is very interested in the story told by
his mathematics teacher about the Yang Hui triangle in the class
yesterday. After class he wrote the following numbers to show the
triangle our ancestor studied.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
......

He
found many interesting things in the above triangle. It is symmetrical,
and the first and the last numbers on each line is 1; there are exactly
i numbers on the line i.

Then Harry studied the elements on every line deeply. Of course, his study is comprehensive.

Now
he wanted to count the number of elements which are the multiple of 3
on each line. He found that the numbers of elements which are the
multiple of 3 on line 2, 3, 4, 5, 6, 7, ... are 0, 0, 2, 1, 0, 4, ... So
the numbers of elements which are not divided by 3 are 2, 3, 2, 4, 6,
3, ... , respectively. But he also found that it was not an easy job to
do so with the number of lines increasing. Furthermore, he is not
satisfied with the research on the numbers divided only by 3. So he
asked you, an erudite expert, to offer him help. Your kind help would be
highly appreciated by him.

Since the result may be very large
and rather difficult to compute, you only need to tell Harry the last
four digits of the result.
 
Input
There
are multiple test cases in the input file. Each test case contains two
numbers P and N , (P < 1000, N<=10^9) , where P is a prime
number and N is a positive decimal integer.

P = 0, N = 0 indicates the end of input file and should not be processed by your program.
 
Output
For
each test case, output the last four digits of the number of elements
on the N + 1 line on Yang Hui Triangle which can not be divided by P
in the format as indicated in the sample output.
 
Sample Input
3 4
3 48
0 0
 Sample Output
Case 1: 0004
Case 2: 0012
思路:lucas定理;
要求是p的倍数,那么那个数模p为0。
lucas定理为C(n,m)=C(n%p,m%p)*C(n/p,m/p);所以在递归的过程中如果当前C(n%p,m%p)所取得值不能为0,也就是n%p的值要大于或等于m%p的值,那么可能取值的个数sum*(n%p+1);同时m%p<=n%p也保证了所取得数小于原来底数。
 1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <cmath>
5 #include <iostream>
6 #include <algorithm>
7 #include <map>
8 #include <queue>
9 #include <vector>
10 #include<set>
11 using namespace std;
12 typedef long long LL;
13 int main(void)
14 {
15 LL p,N;
16 int __ca=0;
17 while(scanf("%lld %lld",&p,&N),p!=0||N!=0)
18 {
19 LL sum=1;
20 while(N)
21 {
22 int mod=N%p;
23 sum*=mod+1;
24 sum%=10000;
25 N/=p;
26 }printf("Case %d: ",++__ca);
27 printf("%04d\n",sum);
28 }
29 return 0;
30 }
 

Interesting Yang Yui Triangle(hdu3304)的更多相关文章

  1. hdu 3304 Interesting Yang Yui Triangle

    hdu 3304 Interesting Yang Yui Triangle 题意: 给出P,N,问第N行的斐波那契数模P不等于0的有多少个? 限制: P < 1000,N <= 10^9 ...

  2. HDU 3304 Interesting Yang Yui Triangle lucas定理

    输入p n 求杨辉三角的第n+1行不能被p整除的数有多少个 Lucas定理: A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0] ...

  3. UVALive - 3700 Interesting Yang Hui Triangle

    题目大意就是求一下 杨辉三角的第N行中不能被P整除的有多少个. 直接卢卡斯定理一下就行啦. #include<bits/stdc++.h> #define ll long long usi ...

  4. UVAL3700

    Interesting Yang Hui Triangle 题目大意:杨辉三角第n + 1行不能整除p(p是质数)的数的个数 题解: lucas定理C(n,m) = πC(ni,mi) (mod p) ...

  5. 武大OJ 613. Count in Sama’s triangle

    Description Today, the math teacher taught Alice Hui Yang’s triangle. However, the teacher came up w ...

  6. [SinGuLaRiTy] 组合数学题目复习

    [SinGuLaRiTy] Copyright (c) SinGuLaRiTy 2017.  All Rights Reserved. [CQBZOJ 2011] 计算系数 题目描述 给定一个多项式( ...

  7. hdu 3944 dp?

    DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Subm ...

  8. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  9. 编程输出杨辉三角的前10行---多维数组的应用---java实现

    import java.util.Scanner;public class yanghui{ public static void main(String[] args){  Scanner sc=n ...

随机推荐

  1. gcc 引用math 库 编译的问题 解决方法

    1.gcc app.c -lm 其中lm表示的是连接 m forlibm.so / libm.a表示你想要的库 abc for libabc.so / libabc.a 其中.a表示的是静态链接库 . ...

  2. 什么是DDL,DML,DCL

    转载自  https://www.2cto.com/database/201610/555167.html DML.DDL.DCL区别 . 总体解释: DML(data manipulation la ...

  3. Java 数据类型转化

    目录 Java类型转化 基本数据类型自动类型转换 自动类型提升 强制类型转换 - 自动类型提升的逆运算 int与long int类型与String类型 int类型转换成String类型 方法1:+ 拼 ...

  4. python 从ubantu环境迁移到windows环境

    下载安装Anaconda3 Anaconda3-2021.05-Windows-x86_64.exe 默认安装目录 C:\ProgramData\Anaconda3 可以启动Anaconda查看不同的 ...

  5. GO瞬间并发数控制

    var wg2 sync.WaitGroup wg2.Add(nums) xc :=0 parallelNum := plt.MaxParallel var waitCount int32 = 0 f ...

  6. Spring DM 2.0 环境配置 解决Log4j问题

    搭建 spring dm 2.0 环境出的问题 log4j 的问题解决办法是 一.引入SpringDM2.0的Bundle,最后完成如下图所示:注意:要引入slf4j.api.slf4j.log4j. ...

  7. Windows下80端口被占用的解决方法(SQL Server)

    查找80端口被谁占用的方法 进入命令提示行(WIN+R 输入 CMD),输入命令 netstat -ano|findstr 80 (显示包含:80的网络连接) ,就可以看到本机所有端口的使用情况,一般 ...

  8. 【力扣】188. 买卖股票的最佳时机 IV

    给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 k 笔交易. 注意:你不能同时参 ...

  9. Jenkins配置代码化

    目录 一.简介 二.init.groovy 脚本命令行调试 一.简介 Jenkins用久了,会有一种莫名的紧张感.因为没人清楚Jenkins都配置了什么,以至于没人敢动它. 但凡使用界面进行配置的都会 ...

  10. Java(变量和常量)

    变量 可以变化的量.可以通过变量来操控内存中的数据:变量可以指代的是内存中的一块空间,而这块空间的位置是确定的但里边要放什么东西还不确定. Java是强类型语言,每个变量都要声明其类型. Java变量 ...