Interesting Yang Yui Triangle(hdu3304)

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*(m%p+1);同时n%p<=m%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 }
时间: 2024-10-13 20:54:32

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

hdu 3304 Interesting Yang Yui Triangle

题意: 给出P,N,问第N行的斐波那契数模P不等于0的有多少个? 限制: P < 1000,N <= 10^9 思路: lucas定理, 如果: n = a[k]*p^k + a[k-1]*p^(k-1) + ... + a[1]*p + a[0] m = b[k]*p^k + b[k-1]*p^(k-1) + ... + b[1]*p + b[0] 则: C(n,m) = pe(i=0~k,C(a[i],b[i]))%p 其中pe表示连乘符号. 由于n已经确定,所以a[i] (0 <=

POJ 3146 &amp; HDU 3304 Interesting Yang Yui Triangle(杨辉三角)

题目链接: HDU 3304 :http://acm.hdu.edu.cn/showproblem.php?pid=3304 POJ 3146  :http://poj.org/problem?id=3146 Problem Description Harry is a Junior middle student. He is very interested in the story told by his mathematics teacher about the Yang Hui trian

HDU Interesting Yang Yui Triangle (Lucas定理)

题意:求杨辉三角中第 n+1行不能整除 p的数目. 析:运用Lucas定理,只要统计C(ni, mi)中全都不是0的数目即可,因为是第 n+1行,所以ni每次都不变,也就是mi <= ni,那么C(ni, mi),就不是0. 所以就有ni+1种答案,最后乘起来即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string>

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]. 则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  mod p同余 即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p),在存在i.b[i]>a[i]时,mod值为0,所以必然整除.当对于全部i,b

LA3700 Interesting Yang Hui Triangle(Lucas定理)

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

UVAL3700

Interesting Yang Hui Triangle 题目大意:杨辉三角第n + 1行不能整除p(p是质数)的数的个数 题解: lucas定理C(n,m) = πC(ni,mi) (mod p) 蓝书犯了两个错误 第一,题意弄错了,应该是“不能”,蓝书上写的能 第二,组合数国外常用记法弄错了.C(n,m)应该记为: ( n m)(凑活着看吧) 蓝书上记的是 ( m n) 根据lucas定理,发现要想为0,必须存在ni < mi,若不为零,则必有所有的ni >= mi,mi的取值有0~ni

HDOJ 4349 DP?

尽量沿着边走距离最短,化减后 C(n+1,k)+ n - k, 预处理阶乘,Lucas定理组合数取模 DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others) Total Submission(s): 1899    Accepted Submission(s): 633 Problem Description Figure 1 shows the Yang Hui Tri

hdu 3944 dp?

DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 1804    Accepted Submission(s): 595 Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,-a

compute Binomial Coefficient or combinations with dynamic programming

The ProblemWrite a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2. 1 def ComputeBinomialCoe