ACM 数论 HDU 3037 Saving Beans (Lucas定理,大数取魔)

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
Hint
Hint For 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
 


题目相当于求n个数的和不超过m的方案数。

由提议可以得到C(n-1,0)+C(n,1)+...+C(n+m-1,m)

套路一:C(n,k) = C(n-1,k)+C(n-1,k-1)

通过不停的代换可以得到原式=C(n+m,m);

接下来就是求C(n+m,m)%p的值


套路二:

Lucas(n,m,p)=C(n%p,m%p)* Lucas(n/p,m/p,p)
Lucas(x,0,p)=1;

不停的递归就可以得到答案


此处用了http://blog.csdn.net/tju_virus/article/details/7843248的代码

  1. #include   
  2. #include   
  3.   
  4. //typedef __int64 lld;  
  5. typedef long long lld;  
  6. lld N,M,P;  
  7.   
  8. int Pow(lld a,lld n,lld p)  
  9. {  
  10.     lld x = a;  
  11.     lld res = 1;  
  12.     while(n)  
  13.     {  
  14.         if(n & 1)  
  15.         {  
  16.             res = ((lld)res * (lld)x) % p;  
  17.         }  
  18.         n >>= 1;  
  19.         x = ((lld)x*(lld)x) % p;  
  20.     }  
  21.     return res;  
  22. }  
  23.   
  24. int Cm(lld n,lld m,lld p)  
  25. {  
  26.     lld a = 1,b = 1;  
  27.     if(m > n) return 0;  
  28.     //实现(a!/(a-b)!) * (b!)^(p-2)) mod p,由于n比较大,所以,此处不知道有什么好的优化  
  29.     while(m)  
  30.     {  
  31.         a = (a * n) % p;  
  32.         b = (b * m) % p;  
  33.         m--;  
  34.         n--;  
  35.     }  
  36.     return ((lld)a * (lld)Pow(b,p-2,p))%p;  
  37. }  
  38.   
  39. int Lucas(lld n,lld m,lld p)  
  40. {  
  41.     if(m==0)  
  42.         return 1;  
  43.     return((lld)Cm(n%p,m%p,p)*(lld)Lucas(n/p,m/p,p))%p;  
  44. }  
  45.   
  46. int main()  
  47. {  
  48.     int t;  
  49.     //freopen("in.txt","r",stdin);  
  50.     scanf("%d",&t);  
  51.     while(t--)  
  52.     {  
  53.         scanf("%I64d%I64d%I64d",&N,&M,&P);  
  54.         printf("%d\n",Lucas(N+M,M,P));  
  55.     }  
  56.     return 0;  
  57. }  

你可能感兴趣的:(ACM 数论 HDU 3037 Saving Beans (Lucas定理,大数取魔))