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
            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, N109) ,
where P is a prime number and N is a positive decimal integer.

P = 0N = 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

题意:杨辉三角第n行的n+1个数有多少个数可以被p整除

思路:将n,m转化成p进制,每一位分别为(a1,a2,a3,a4.....) ,(b1,b2,b3,b4.....)

C(n,m)%p  =  C(a1,b1)*C(a2,b2)*C(a3,b3)......%p

要使C(ai,bi) = 0 只有当bi>ai ,因此求不能整除的数的个数可以通过 (ai+1)累乘(计数原理)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
int p,n;
int ans;
vector<int> digit;
int main(){
    int T = 1;
    while(~scanf("%d%d",&p,&n) && p+n){
        digit.clear();
        ans = 1;
        while(n){
            digit.push_back(n%p);
            n /= p;
        }
        for(int i = 0; i < digit.size(); i++){
            ans = (ans*(digit[i]+1))%10000;
        }

        printf("Case %d: %04d\n",T++,ans);
    }
    return 0;
}
时间: 2024-10-14 13:23:11

LA3700 Interesting Yang Hui Triangle(Lucas定理)的相关文章

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

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 th

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

HDU3944-DP?(帕斯卡公式+Lucas定理)

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

BZOJ 4403 2982 Lucas定理模板

思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; const int mod=1000003; #define int long long int cases,N,L,R,fac[mod],inv[mod]; int C(int n,int m){ if(n<m)return 0; if(n<mod&&m<mod)return fac[n]*inv[n-m]

HDU 3037 Saving Beans (数论,Lucas定理)

题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后就求这个值,直接求肯定不好求,所以我们可以运用Lucas定理,来分解这个组合数,也就是Lucas(n,m,p)=C(n%p,m%p)* Lucas(n/p,m/p,p). 然后再根据费马小定理就能做了. 代码如下: 第一种: #pragma comment(linker, "/STACK:10240

HDU3037 Saving Beans(Lucas定理+乘法逆元)

题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个数是个经典问题,可以+1转化正整数解的个数用插板法解决:$C_{y+n-1}^{n-1}=C_{y+n-1}^y$. 而0<=y<=m,最后的结果就是—— $$\sum_{i=0}^m C_{i+n-1}^i$$ $$C_{n-1}^0+C_{n}^1+C_{n+1}^2+\dots+C_{n-1