HDU3037 Saving Beans

 

题目链接:HDU3037

正解:$Lucas$定理

解题报告:

  Lucas定理裸题:

  求$C_{n+m}^m$ $mod$ $p$,$p<=10^5$.

  边界注意一下。

 

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
const int MAXN = 100011;
int n,m,p,lim;
LL jie[MAXN],nj[MAXN];
inline LL C(LL n,LL m){ if(n<m) return 0;/*!!!*/ return jie[n]*nj[m]%p*nj[n-m]%p; }
inline LL fast_pow(LL x,LL y){ LL r=1; while(y>0) { if(y&1) r*=x,r%=p; x*=x; x%=p; y>>=1; } return r; }
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void init(){
	lim=p-1; jie[0]=nj[0]=1;
	for(int i=1;i<=lim;i++) jie[i]=jie[i-1]*i,jie[i]%=p;
	nj[lim]=fast_pow(jie[lim],p-2);
	for(int i=lim-1;i>=1;i--) nj[i]=nj[i+1]*(i+1)%p;
}

inline LL Lucas(LL n,LL m){
	if(n<m) return 0; if(m==0) return 1;
	if(n<=lim && m<=lim) return C(n,m);
	return Lucas(n/p,m/p)*C(n%p,m%p)%p;
}

inline void work(){
	int T=getint();
	while(T--) {
		n=getint(); m=getint(); p=getint();
		init();	printf("%lld\n",Lucas(n+m,m));
	}
}

int main()
{
    work();
    return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

posted @ 2017-03-15 20:43  ljh_2000  阅读(170)  评论(0编辑  收藏  举报