zoukankan      html  css  js  c++  java
  • 【HDU 3037】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


    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

    题解

    求C(n+m),
    Lucas定理:
    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]) mod p)同余
    即:$Lucas(n,m,p)=c(n%p,m%p) imes Lucas(n/p,m/p,p) $

    参考代码

    #include<queue>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    #define inf 1000000000
    #define REP(i,x,n) for(int i=x;i<=n;i++)
    #define DEP(i,x,n) for(int i=n;i>=x;i--)
    #define mem(a,x) memset(a,x,sizeof(a))
    using namespace std;
    ll read(){
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    void Out(ll a){
        if(a<0) putchar('-'),a=-a;
        if(a>=10) Out(a/10);
        putchar(a%10+'0');
    }
    const int N=100005;
    ll fac[N];
    void Init(int p){
        fac[0]=1LL;
        for(int i=1;i<=p;i++) fac[i]=fac[i-1]*i%p;
    }
    ll qpow(ll a,ll b,ll p){
    	ll ans=1;
    	for(int i=b;i;i>>=1,a=(a*a)%p)
    		if(i&1) ans=(ans*a)%p;
    	return ans;
    }
    ll C(ll n,ll m,ll p){
    	if(n<m) return 0;
    	return fac[n]*qpow(fac[n-m],p-2,p)%p*qpow(fac[m],p-2,p)%p;
    }
    ll lucas(int n,int m,int p){
    	if(m==0) return 1;
    	return C(n%p,m%p,p)*lucas(n/p,m/p,p)%p;
    }
    ll Lucas(ll n,ll m,ll p) {
        ll ret=1;
        while(n&&m){
            ll a=n%p,b=m%p;
            if(a<b) return 0;
            ret = (ret*fac[a]*qpow(fac[b]*fac[a-b]%p, p-2, p)) % p;
            n/=p;
            m/=p;
        }
        return ret;
    }
    int main(){
    	for(int T=read();T;T--){
            int n=read(),m=read(),p=read();
            Init(p);
            printf("%lld
    ",lucas(n+m,m,p));
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    【转】我是一个线程
    前端之 JS 实现全选、反选、取消选中
    Python文件操作——逐行插入内容
    angularJs实现数据双向绑定的原理
    手机连接电脑调试页面
    工程化框架之feather
    网页上线后音频不能自动播放
    FormData对象
    地图热区自适应
    需求移交会
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/7191701.html
Copyright © 2011-2022 走看看