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)\times 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\n",lucas(n+m,m,p));
}
return 0;
}

【HDU 3037】Saving Beans(卢卡斯模板)的更多相关文章

  1. hdu 3037 Saving Beans(组合数学)

    hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...

  2. hdu 3037 Saving Beans Lucas定理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. hdu 3037 Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 3037——Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. Hdu 3037 Saving Beans(Lucus定理+乘法逆元)

    Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...

  6. HDU 3037 Saving Beans(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  7. HDU 3037 Saving Beans (Lucas法则)

    主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...

  8. HDU 3037 Saving Beans(Lucas定理的直接应用)

    解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...

  9. HDU 3037 Saving Beans (数论,Lucas定理)

    题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...

  10. HDU 3073 Saving Beans

    Saving Beans Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

随机推荐

  1. 3-zookeeper应用场景

    1 概述 zk的核心体系是一个由业务注册进来的文件系统+对文件系统变化进行监听通知的监听机制. 假如在一个分布式系统中,有5台服务器,上面跑业务进程.在进程启动时,会去zk注册临时节点,并注册监听器. ...

  2. python实现判断素数

    import math def is_prime_1(n): if n <= 1: return False for i in range(2, int(math.sqrt(n) + 1)): ...

  3. 洛谷 P2147 [SDOI2008]洞穴勘测

    以下这个做法应该是叫线段树分治... 根据修改操作预处理出每条边存在的时间区间[l,r](以操作序号为时间),然后把所有形式化后的修改挂到线段树节点上. 处理完修改后,dfs一遍线段树,进入某个节点时 ...

  4. (转)生产者/消费者问题的多种Java实现方式

    参考来源:http://blog.csdn.net/monkey_d_meng/article/details/6251879/ 生产者/消费者问题的多种Java实现方式 实质上,很多后台服务程序并发 ...

  5. Hibernate5 与 Spring Boot2 最佳性能实践

    参考 Hibernate5 与 Spring Boot2 最佳性能实践(1) Hibernate5 与 Spring Boot2 最佳性能实践(2) Best Performance Practice ...

  6. 洛谷P2774 方格取数问题(最小割)

    题意 $n \times m$的矩阵,不能取相邻的元素,问最大能取多少 Sol 首先补集转化一下:最大权值 = sum - 使图不连通的最小权值 进行黑白染色 从S向黑点连权值为点权的边 从白点向T连 ...

  7. 在vscode中显示空格和tab符号

    转自:https://blog.csdn.net/bmzk123/article/details/86501706 使用python时最烦人的就是代码对齐,而且tab和空格还不一样,为了便于对其,希望 ...

  8. .net4.5注册到iis

    开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windir%\Microsoft.NET\Framework\v4.0.30319\as ...

  9. Javaweb学习笔记9—过滤器

      今天来讲javaweb的第9阶段学习.   过滤器,我在本次的思维导图中将过滤器和监听器放在一起总结了,监听器比较简单就不单独写了.   老规矩,首先先用一张思维导图来展现今天的博客内容.     ...

  10. UVA 11346 Probability 概率 (连续概率)

    题意:给出a和b,表示在直角坐标系上的x=[-a,a] 和 y=[-b,b]的这样一块矩形区域.给出一个数s,问在矩形内随机选择一个点p=(x,y),则(0.0)和p点组成的矩形面积大于s的概率是多少 ...