hdu 3037 Saving Beans( lucas定理+隔板法 )

题意:将不大于m颗种子存放在n颗不同的树中,可以为空,问有多少种存法。
解析: 有隔板法可知 : m个种子,用 n-1个板子隔开,但由于可以为空,所以加n个种子,这样得到的答案是c(n+m-1,n-1);即为恰好放m个种子的放法数,可以考虑加一个板子,这样最后一个板子左侧的就是不大于m的部分,其右边是不需要的,所以 答案是C(n+m,n);

这时就需要用lucas的模板了

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)

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#define PI acos(-1.0)
typedef __int64 LL;
//const
const int N=100005;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7,STA=8000010;
//const LL LNF=1LL<<60;
const double EPS=1e-8;
const double OO=1e15;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}

LL f[N],p;
void inint()
{
    f[0]=1;
    for(int i=1;i<=p;i++)
      f[i]=(f[i-1]*i)%p;
}

LL poww(LL a,LL n)
{
    LL res=1,q=a;
    while(n)
    {
        if(n&1) res=(res*q)%p;
        q=(q*q)%p;
        n>>=1;

    }
    return res;
}
LL C(LL n,LL m)
{
   if(m>n) return 0;
   return f[n]*poww(f[m]*f[n-m]%p,p-2)%p;
}
int main()
{

    int t;
    LL res=1,n,m,a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d%I64d",&n,&m,&p);
        inint();
        res=1;
        n+=m;
        while(n&&m)
        {
           a=n%p;
           b=m%p;
           if(b>a) { res=0; break;}
           res=(res*C(a,b))%p;
           n/=p;
           m/=p;
        }
        printf("%I64d\n",res%p);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值