poj 3146 Interesting Yang Hui Triangle (Lucas定理)
2017-02-14 15:29
465 查看
Interesting Yang Hui Triangle
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.
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 ≤ 109), 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 Source Shanghai 2006 |
[Discuss]
题目大意:求c
[0..n]中有多少数不能被p整除,其中p为质数,c表示组合数。
题解:Lucas定理
对于c(n,m)我们可以将n,m都分解成p进制数
n=a[k]a[k-1]...a[0]
m=b[k]b[k-1]...b[0]
如果最后的结果不能被p整除,那么一定不存在c[ni][mi]==0
那么在什么情况下c[ni][mi]等于0呢?当且仅当mi>ni的时候。因为p是质数,ni<p,所以0..ni中的所以数与p互质,那么c[ni][mi]也一定与p互质。
因为n已知,那么a数组就是已知的,bi的取值只有ai+1种,最后用乘法原理乘起来即可。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #define N 1003 #define LL long long using namespace std; int n,p; int c ,a ,x ,y ,mi ,cnt,vis ,sum; int lucas(int n,int m) { if (m==0) return 1; return c[n%p][m%p]*lucas(n/p,m/p)%p; } int main() { freopen("a.in","r",stdin); //freopen("my.out","w",stdout); int T=0; while (true) { ++T; scanf("%d%d",&p,&n); if (!p&&!n) break; printf("Case %d: ",T); memset(c,0,sizeof(c)); c[1][1]=1; c[2][1]=1; c[2][2]=1; for (int i=3;i<=p;i++) for (int j=1;j<=i;j++) { c[i][j]=(c[i-1][j-1]+c[i-1][j])%p; if (c[i][j]==0) a[i][++a[i][0]]=j; } int k=n; cnt=-1; int sum=1; while (k) { x[++cnt]=k%p; sum*=(x[cnt]+1); sum%=10000; k/=p; } if (sum/1000==0) printf("0"); if (sum/100==0) printf("0"); if (sum/10==0) printf("0"); printf("%d\n",sum); } }
相关文章推荐
- queue模拟
- ueditor 后端配置项没有正常加载,上传插件不能正常使用!jsp版本
- 利用UEditor进行上传图片、视频
- PLAN_HASH_VALUE - How equal (and stable?) are your execution plans - part 1
- require.js
- Fiddler大师之路(七) | QuickExec
- U3D之GUI开发
- 我要开启vue2新征程。
- MyBatis循环获取Map中的key和value的方法
- View-MeasuerSepc
- iOS 监听UILabel点击
- Duilib vs调试程序加载资源失败,但是单独执行exe可以成功
- UINavgationController中覆写preferredStatusBarStyle方法不执行的问题
- __cpuidex读取CPU序列号
- Phoenix和SQuirrel安装详解
- Vue框架Element的事件传递broadcast和dispatch方法分析
- PHP中include()与require()的区别说明
- invalidate() vs postInvalidate() vs requestLayout()
- Java转换Json日期/Date(1487053489965+0800)/格式以及js时间格式 Tue Feb 14 2017 14:06:32 GMT+0800
- 放肆的使用UIBezierPath和CAShapeLayer画各种图形