1. 程式人生 > >hdu 3037 Saving Beans(組合數取模)

hdu 3037 Saving Beans(組合數取模)

Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3697    Accepted Submission(s): 1424


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
Recommend gaojie   |   We have carefully selected several similar problems for you:  
3033
 3038 3036 3035 3034 

題解:組合數取模(lucas定理)

題目大意:從n棵不同的樹上取不超過M顆豆子(豆子無差異),有多少種取法。

題目可以轉換成  x1+x2+……+xn=m 有多少組解,m在題中可以取0~m。

對於  x1+x2+……+xn=m,需要利用到插板法。

插板法:插板法就是在n個元素間的(n-1)個空中插入 若干個(b)個板,可以把n個元素分成(b+1)組的方法。 
應用插板法必須滿足三個條件: 
(1) 這n個元素必須互不相異 
(2) 所分成的每一組至少分得一個元素 
(3) 分成的組別彼此相異

那麼這相當於是m 個元素,插入n個板子,但是所分成的每組元素可以剩餘,

條件(2)不滿足,此時如果在3個箱子種各預先插入1個元素,則問題就等價於把n+m個元素插入n個板子,此時的答案就是C(n+m-1,n-1) =C(n+m-1,m) (C(N,M)=C(N,N-M))  。

則題目解的個數可以轉換成求   sum=C(n+m-1,0)+C(n+m-1,1)+C(n+m-1,2)……+C(n+m-1,m)利用公式C(n,r)=C(n-1,r)+C(n-1,r-1)  == >  sum=C(n+m,m)。

sum=C(n+m-1,0)(m=0)+C(n+m-1,1)(m=1)+C(n+m-1,2)(m=2)……+C(n+m-1,m)(m=m)

       =C(n-1,0)+C(n,1)+C(n+1,2)....+C(n-m-1,m)

因為C(n-1,0)=C(n,0)=1

所以式子可以利用C(n,r)=C(n-1,r)+C(n-1,r-1) ,進行化簡最終得到C(n+m,m)

現在就是要求C(n+m,m)%p。

lucas定理:

A、B是非負整數,p是質數。AB寫成p進位制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。
則組合數C(A,B)與C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  modp同餘

即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p) 

但是lucas定理只有在A,B<=10^18,P為質數,p<=10^5時才可以使用(如果p<=10^9,但是n,m較小的話,貌似也可以用lucas定理,但是沒法預處理階乘,只能單獨計算組合數,總之具體情況具體分析)

這道題的話可以先預處理出階乘,根據公式c(n,m)=n!/(m!(n-m)!),因為是模意義下所以可以轉化為n!*inv(m!(n-m)!)  mod p,因為p是質數,根據費馬小定理,可得  inv(m!(n-m)!)=(m!(n-m)!)^p-2,可用快速冪求解。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
ll k[500003],n,m,p;
int t;
void calc(ll p)
{
   k[0]=1;
   for (ll i=1;i<=p;i++)
    k[i]=(ll)k[i-1]*i%p;
}
ll quick(ll num,ll x)
{
	ll  ans=1; ll base=(ll)num%p; 
	while(x)
	{
		if (x&1)
		 ans=ans*base%p;
		x>>=1;
		base=base*base%p;
	}
	return ans%p;
}
ll c(ll x,ll y)
{
	if (y>x) return 0;
	return (ll)k[x]*quick(k[y]*k[x-y],p-2)%p;
}
ll lucas(ll n,ll m,ll p)
{
	if (m==0) return 1;
	return (ll)c(n%p,m%p)*lucas(n/p,m/p,p)%p;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d",&t);
	for (int i=1;i<=t;i++)
	 {
	 	scanf("%I64d%I64d%I64d",&n,&m,&p);
	 	calc(p);
	 	printf("%I64d\n",lucas(n+m,m,p)%p);
	 }
}