您的位置:首页 > 其它

hdu 3037 Saving Beans 求Comb(n,m)%p p是素数且p<=10000

2011-08-16 20:18 441 查看
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.

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.

Output

You should output the answer modulo p.

Sample Input

2
1 2 5
2 1 5


Sample Output

3
3


//

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

//计算C(a,b)%p p是素数且p<=100000

//求(a/b)%p,gcd(B,p)=1

__int64 exgcd(__int64 a,__int64 b,__int64& x,__int64& y)

{

if(b==0) return x=1,y=0,a;

__int64 r=exgcd(b,a%b,x,y);

__int64 t=x;

x=y;

y=t-(a/b)*y;

return r;

}

__int64 adivideb_mod_p(__int64 a,__int64 b,__int64 p)

{

__int64 x,y;

__int64 r=exgcd(b,p,x,y);

x*=a;

x=(x%p+p)%p;

return x;

}

//a<p,b<p p素数

__int64 fac[120000];

void init_mod_p(__int64 p)

{

fac[0]=1;

for(int i=1;i<p;i++) fac[i]=(fac[i-1]*i)%p;

}

__int64 comb_p(__int64 a,__int64 b,__int64 p)

{

if(b>a) return 0;

if(a==b) return 1;

__int64 tn,tm;

tn=fac[a];tm=(fac[a-b]*fac[b])%p;

return adivideb_mod_p(tn,tm,p);

}

//Lucas定理 answer

__int64 comb_mod_p(__int64 a,__int64 b,__int64 p)

{

if(b==0) return 1;

return (comb_p(a%p,b%p,p)*comb_mod_p(a/p,b/p,p))%p;

}

int main()

{

int ci;scanf("%d",&ci);

while(ci--)

{

__int64 n,m,p;

scanf("%I64d%I64d%I64d",&n,&m,&p);

init_mod_p(p);

printf("%I64d\n",comb_mod_p(n+m,n,p));

}

return 0;

}

//当p不固定且case很多的时候 可以改成下面这样

//计算C(a,b)%p p是素数且p<=100000

//求(a/b)%p,gcd(B,p)=1

bignum exgcd(bignum a,bignum b,bignum& x,bignum& y)

{

if(b==0) return x=1,y=0,a;

bignum r=exgcd(b,a%b,x,y);

bignum t=x;

x=y;

y=t-(a/b)*y;

return r;

}

bignum adivideb_mod_p(bignum a,bignum b,bignum p)

{

bignum x,y;

bignum r=exgcd(b,p,x,y);

x*=a;

x=(x%p+p)%p;

return x;

}

//a<p,b<p p素数

bignum comb_p(bignum a,bignum b,bignum p)

{

if(b>a) return 0;

if(a==b) return 1;

if(b>a-b) b=a-b;

bignum tn=1,tm=1;

//tn=fac[a];tm=(fac[a-b]*fac[b])%p;

for(int i=1;i<=b;i++)

{

tn=(tn*(a-i+1))%p;

tm=(tm*i)%p;

}

return adivideb_mod_p(tn,tm,p);

}

//Lucas定理 answer

bignum comb_mod_p(bignum a,bignum b,bignum p)

{

if(b==0) return 1;

return (comb_p(a%p,b%p,p)*comb_mod_p(a/p,b/p,p))%p;

}

//若b<=10000 b<p且p是素数 则用下面函数

//计算C(a,b)%p p是素数且b<=10000,b<p

bignum comb_mod_p(bignum a,bignum b,bignum p)

{

if(b>a) return 0;

if(a==b) return 1;

bignum tn=1,tm=1;

for(int i=1;i<=b;i++)

{

tn=(tn*(a-i+1))%p;

tm=(tm*i)%p;

}

return adivideb_mod_p(tn,tm,p);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: