hdu3037 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
 
 
21 2 52 1 5
 

Sample Output
 
 
33
Hint
HintFor 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.
 
题意:
有n个不同的盒子,在每个盒子中放一些求(可以不放),使得总球数小于等于m,求方案数(mod p).
1<=n,m<=10^9,1<p<10^5,保证p是素数

题解:
设最后放了k个球,根据"隔板法"由方案数C(k+n-1,n-1),:
ans=C(n-1,n-1)+C(n,n-1)+C(n+1,n-1)+……+C(n+m-2,n-1)+C(n+m-1,n-1)=C(n+m,n);(mod p)

由于n,m太大,于是Lucas

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MOD 2333
using namespace std;
inline int read(){
    int date=0,w=1;char c=0;
    while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
    while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
    return date*w;
}
long long mexp(long long a,long long b,long long c){
    long long s=1;
    while(b){
        if(b&1)s=s*a%c;
        a=a*a%c;
        b>>=1;
    }
    return s;
}
long long lucas(int n,int m,long long p){
    if(n<m)return 0;
    if(m>=p||n>=p)return lucas(n/p,m/p,p)%p*lucas(n%p,m%p,p)%p;
    if(m>n-m)m=n-m;
    long long s1=1,s2=1;
    for(int i=n-m+1;i<=n;i++)s1=s1*i%p;
    for(int i=1;i<=m;i++)s2=s2*i%p;
    return s1*mexp(s2,p-2,p)%p;
}
int main(){
    int t=read(),n,m,p;
    while(t--){
        n=read();m=read();p=read();
        printf("%lld\n",lucas(n+m,m,p));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值