hdu3037 Saving Beans(个数可为0的特殊“插板法”+推公式+组合数取摸(需预处理素数阶乘,否则TLE))
来源:互联网 发布:读屏软件下载 编辑:程序博客网 时间:2024/04/28 16:18
Link:http://acm.hdu.edu.cn/showproblem.php?pid=3037
Saving Beans
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3264 Accepted Submission(s): 1256
Problem Description
Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that they will save beans in n different trees. However, since the food is not sufficient nowadays, they will get no more than m beans. They want to know that how many ways there are to save no more than m beans (they are the same) in n trees.
Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
Input
The first line contains one integer T, means the number of cases.
Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.
Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.
Output
You should output the answer modulo p.
Sample Input
21 2 52 1 5
Sample Output
33HintHintFor sample 1, squirrels will put no more than 2 beans in one tree. Since trees are different, we can label them as 1, 2 … and so on. The 3 ways are: put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For sample 2, the 3 ways are: put no beans, put 1 bean in tree 1 and put 1 bean in tree 2.
Source
2009 Multi-University Training Contest 13 - Host by HIT
编程思想:由于每棵树上都可摘0~m颗豆子(存在分组的元素个数为0的情况),则对于特定的m颗豆子,利用插板法分成m+1份,第m+1份表示原先要摘的m颗豆子减去已经在n颗树采摘豆子的总数后剩下的数目,所以有C(n+m-1,m)种方案,所以总数为
C(n-1,0)+C(n,1)+...+C(n+m-1,m)
= C(n,0)+C(n,1)+C(n+1,2)+...+C(n+m-1,m)
= C(n+m,m)
答案就是C(n+m,m) % p,其中p是素数。利用Lucas定理对组合数求模,同时观察到素数模较小(100000),需要先对其进行素数阶乘处理,否则会TLE!!!
AC code:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<cmath>#include<queue>#include<map>#include<stack>#include<vector>#define LL long long#define MAXN 1000010using namespace std;const int N=20;//模方程数 LL a[N],mod[N];int cnt;//素数个数int prim[2];//存储素数/*const int maxx=10005;//最大素数上限(适用于要模的素数最大不超过10000的情况下) int frc[3300][maxx];//frc[i][j]表示(j!)%(第i个素数prim[i])int id[maxx];//素数编号 bool vis[maxx];void init()//素数筛选及打表求:(j!)%(第i个素数prim[i]) {for(int i=2;i<maxx;i++) if(!vis[i]){prim[cnt]=i;id[i]=cnt;for(int j=i*2;j<maxx;j+=i)vis[j]=1;frc[cnt][0]=1;for(int j=1;j<prim[cnt];j++) frc[cnt][j]=frc[cnt][j-1]*j%prim[cnt];cnt++;}} */int frc[100011];void Single_init(int p)//只初始化一个素数的阶乘{frc[0]=1;for(int j=1;j<=p;j++) frc[j]=(LL)frc[j-1]*j%p;} /*LL mul(LL a,LL b,LL mod)//a*b%mod{LL ans=0;while(b){if(b&1)ans=(ans+a)%mod;b>>=1;a=(a+a)%mod;}return ans;}*/LL quick_mod(LL a,LL b,LL m)//a^b%m {LL ans=1;a%=m;while(b){if(b&1){ans=ans*a%m;}b>>=1;a=a*a%m;}return ans;}LL getC(LL n,LL m,int cur)//C(n,m)%mod[cur]{LL p=mod[cur];//p为要取的素数模mod[cur] if(m>n)return 0;if(m>n-m)m=n-m;LL ans=1;/*for(int i=1;i<=m;i++){LL a=(n+i-m)%p;LL b=i%p;//ans=mul(ans,mul(a,quick_mod(b,p-2,p),p),p);//p为素数,i对p的逆元可以不用扩张欧几里得进行求解 re=i^(P-2) ans = ans * (a * quick_mod(b, p-2,p) % p) % p; }*///注释掉这个,然后用以下方法求解提高速率 //直接用打表方式求解(适用于要模的素数最大不超过10000的情况下)//int num=id[p];//素数编号 ans=(LL)frc[n]*quick_mod((LL)frc[n-m]*frc[m]%p,p-2,p)%p;return ans;}LL Lucas(LL n,LL k,int cur)//求C(n,k)%mod[cur] {LL p=mod[cur];if(k==0)return 1%p;//return getC(n%p,k%p,cur)*Lucas(n/p,k/p,cur)%p;return getC(n % p, k % p,cur) * Lucas(n / p, k / p,cur) % p; }/*void extend_Euclid(LL a,LL b,LL &x,LL &y){if(b==0){x=1;y=0;return;}extend_Euclid(b,a%b,x,y);LL tmp=x;x=y;y=tmp-a/b*y;}LL CRT(LL a[],LL m[],int k)//求C(n,m)%M,其中M=(m0*m2*…*m(k-1)),mi为素数,则先用a[i]存储模方程C(n,m)%mi,{ //m[]存储所有素数因子mi,k表示总共有k个模方程,返回C(n,m)%M的值 LL M=1;LL ans=0;for(int i=0;i<k;i++)M*=mod[i];for(int i=0;i<k;i++){LL x,y,tmp;LL Mi=M/m[i];extend_Euclid(Mi,m[i],x,y);if(x<0){x=-x;tmp=mul(Mi,x,M);tmp=mul(tmp,a[i],M);tmp=-tmp;}else{tmp=mul(Mi,x,M);tmp=mul(tmp,a[i],M);}ans=(ans+tmp)%M;}while(ans<0)ans+=M;return ans;}*/LL ans;int main(){//freopen("D:\\in.txt","r",stdin);int T,n,m,p;T=0;//init();scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&p);//T++;Single_init(p);//先处理单个素数的阶乘,否则TLE!!! mod[0]=p;ans=Lucas(n+m,m,0);printf("%I64d\n",ans); } return 0;}
0 0
- hdu3037 Saving Beans(个数可为0的特殊“插板法”+推公式+组合数取摸(需预处理素数阶乘,否则TLE))
- hdu3037 Saving Beans (lucas定理+组合数公式)
- [HDU3037]Saving Beans(组合数学Lucas定理)
- Saving Beans HDU3037 ( 可重复的组合数计数原理+Lucas定理 )
- HDU3037——Saving Beans(数论,组合数取模,lucas定理模板)
- HDU3037 Saving Beans
- hdu3037 Saving Beans
- 【HDU3037】Saving Beans
- 【日常学习】【组合数取模Lucas定理】HDU3037 Saving Beans题解
- 【hdu3037】【Lucas定理】Saving Beans
- Hdu3037 Saving Beans Lucas定理
- hdu 3037 Saving Beans(组合数取模)
- 【HDU 3037】Saving Beans(组合数取模)
- hdu3037 Saving Beans(Lucas定理+费马小定理or扩展欧几里德算发)
- 组合数公式和“插板法”的使用
- n阶乘后面0的个数+组合数学结果0的个数举例+公式推导
- [ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)
- hdu 3037 Saving Beans (大组合数取模--Lucas定理)
- [论文阅读]词汇树 Scalable Recognition with a Vocabulary Tree
- 欢迎使用CSDN-markdown编辑器
- 转 e文 jvm 内存分配总结
- Spring获取XML
- Android 面试 基础
- hdu3037 Saving Beans(个数可为0的特殊“插板法”+推公式+组合数取摸(需预处理素数阶乘,否则TLE))
- JobTracker和TaskTracker概述
- Swift2.0新特性
- hoj2298Toxophily【二分+三分】
- 一只简单的网络爬虫(基于linux C/C++)————线程相关
- js修改title
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
- java 适配器模式
- java学习笔记IO输入与输出——IO流概述及字节流(2)