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
Sample Output
//
#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);
}
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);
}
相关文章推荐
- hdu 3439 Sequence 求Comb(n,m)%p p<=10000且p任意,n,m很大 求h(n)%m(错排列)
- zoj 3557 How Many Sets II 求Comb(n,m)%p p很大素数 m<=1e4
- 【HDU 3037】Saving Beans(卢卡斯模板)
- HDU 2544 最短路 <裸地迪杰斯特拉算法>
- HDU 1596 find the safest road <迪杰斯特拉算法>
- HDU 2098 分拆素数和
- 求1到n的最小公倍数(要存储<M的素数)
- [HDU] 3037 Saving Beans
- HDU 3037 Saving Beans(组合数学+Lucas定理)
- Saving Beans-HDU - 3037
- hdu 1203 I NEED A OFFER! <背包的变形>
- hdu 3037 Saving Beans (大组合数取模--Lucas定理)
- HDU 3037 Saving Beans
- hdu 3037 Saving Beans
- hdu 1162 Eddy's picture<java><prime>
- SGU 231 Prime Sum 求<=n内有多少对素数(a,b)使得a+b也为素数 规律题
- hdu 1312 Red and Black <java>
- hdu 2527 Safe Or Unsafe<multiset>
- Hdu 3037 Saving Beans 卢卡斯定理 解题报告
- 【HDU 6069 Counting Divisors】 + 素数筛 & 思维