首页 > 代码库 > [ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)

[ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)

Saving Beans



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
题意为:将不超过m个豆子放在n棵不同的树上,一共有多少种方法。

以下转载于:

http://blog.csdn.net/tju_virus/article/details/7843248

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

如果和恰好等于m,那么就等价于方程x1+x2+...+xn = m的解的个数,利用插板法可以得到方案数为:

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

现在就需要求不大于m的,相当于对i = 0,1...,m对C(n+i-1,i)求和,根据公式C(n,k) = C(n-1,k)+C(n-1,k-1)得

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

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

= C(n+m,m)

现在就是要求C(n+m,m) % p,其中p是素数。

然后利用Lucas定理的模板就可以轻松的求得C(n+m,m) % p的值


下面简单介绍一下Lucas定理:

Lucas定理是用来求 C(n,m) mod p的值,p是素数(从n取m组合,模上p)。
描述为:
Lucas(n,m,p)=C(n%p,m%p)* Lucas(n/p,m/p,p)
Lucas(x,0,p)=1;

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) 

简单的理解就是:

以求解n! % p 为例,把n分段,每p个一段,每一段求得结果是一样的。但是需要单独处理每一段的末尾p,2p,...,把p提取出来,会发现剩下的数正好又是(n/p)! ,相当于

划归了一个子问题,这样递归求解即可。

这个是单独处理n!的情况,当然C(n,m)就是n!/(m! *(n-m)!),每一个阶乘都用上面的方法处理的话,就是Lucas定理了

Lucas最大的数据处理能力是p在10^5左右。

而C(a,b) =a! / ( b! * (a-b)! ) mod p

其实就是求 ( a! / (a-b)!)  * ( b! )^(p-2) mod p

  (上面这一步变换是根据费马小定理:假如p是质数,且a,p互质,那么a的(p-1)次方除以p的余数恒为1,

那么a和a^(p-2)互为乘法逆元,则(b / a) = (b * a^(p-2) ) mod p)


代码:

#include <iostream>
#include <string.h>
#include <cmath>
#define ll long long
using namespace std;
const int maxn=100002;
ll n,m,p;
ll fac[maxn];

void getfac(ll p)//预处理阶层
{
    fac[0]=1;
    for(int i=1;i<=p;i++)
        fac[i]=fac[i-1]*i%p;
}

ll power(ll a,ll n,ll p)//快速幂运算
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=ans*a%p;
        a=a*a%p;
        n/=2;
    }
    return ans;
}

ll lucas(ll n,ll m,ll p)
{
    ll ans=1;
    while(n&&m)
    {
        ll a=n%p;
        ll b=m%p;
        if(a<b) return 0;
        ans=(ans*fac[a]*power(fac[b]*fac[a-b]%p,p-2,p))%p;//  fac[b]*fac[a-b]后面别忘了%p,否则WA
        n/=p;
        m/=p;
    }
    return ans;
}


int main()
{
    int t;cin>>t;
    while(t--)
    {
        cin>>n>>m>>p;
        getfac(p);
        cout<<lucas(n+m,m,p)<<endl;
    }
    return 0;
}

Lucas 定理两种写法:

ll lucas(ll n,ll m,ll p)
{
    ll ans=1;
    while(n&&m)
    {
        ll a=n%p;
        ll b=m%p;
        if(a<b) return 0;
        ans=(ans*fac[a]*power(fac[b]*fac[a-b]%p,p-2,p))%p;//  fac[b]*fac[a-b]后面别忘了%p,否则WA
        n/=p;
        m/=p;
    }
    return ans;
}

int Lucas(lld n,lld m,lld p)  
{  
    if(m==0)  
        return 1;  
    return((lld)Cm(n%p,m%p,p)*(lld)Lucas(n/p,m/p,p))%p;  
} 
int Cm(lld n,lld m,lld p)  
{  
    lld a = 1,b = 1;  
    if(m > n) return 0;  
    //实现(a!/(a-b)!) * (b!)^(p-2)) mod p,由于n比较大,所以,此处不知道有什么好的优化  
    while(m)  
    {  
        a = (a * n) % p;  
        b = (b * m) % p;  
        m--;  
        n--;  
    }  
    return ((lld)a * (lld)Pow(b,p-2,p))%p;  
}






[ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)