Saving Beans

Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5761    Accepted Submission(s):
2310

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
 
/*
题目相当于求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的值
*/
#include<iostream>
#include<cstdio>
#include<cstring> #define N 100007 using namespace std;
long long f[N]; long long Mi(long long a,long long b,long long p)
{
long long res=;
while(b)
{
if(b&) res=res*a%p;
b>>=;a=a*a%p;
}return res;
} long long C(long long n,long long m,long long p)
{
if(m>n)return ;
return f[n]*Mi(f[m]*f[n-m]%p,p-,p)%p;
} long long Lcs(long long n,long long m,long long p)
{
if(m==)return ;
return (C(n%p,m%p,p)*Lcs(n/p,m/p,p))%p;
} int main()
{
long long n,m,p;long long t;
cin>>t;
while(t--)
{
cin>>n>>m>>p;
f[]=;
for(long long i=;i<=p;i++)
f[i]=f[i-]*i%p;
printf("%lld\n",Lcs(n+m,m,p));
}
return ;
}

hdu 3037Saving Beans(卢卡斯定理)的更多相关文章

  1. hdu3037Saving Beans——卢卡斯定理

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037 卢卡斯定理模板——大组合数的取模 代码如下: #include<iostream> #i ...

  2. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  3. hdu3037——卢卡斯定理

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037 卢卡斯定理模板——大组合数取模 #include<iostream> #include& ...

  4. 【BZOJ4403】序列统计(组合数学,卢卡斯定理)

    [BZOJ4403]序列统计(组合数学,卢卡斯定理) 题面 Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取 ...

  5. 【Luogu3807】【模板】卢卡斯定理(数论)

    题目描述 给定\(n,m,p(1≤n,m,p≤10^5)\) 求 \(C_{n+m}^m mod p\) 保证\(P\)为\(prime\) \(C\)表示组合数. 一个测试点内包含多组数据. 输入输 ...

  6. 【数论】卢卡斯定理模板 洛谷P3807

    [数论]卢卡斯定理模板 洛谷P3807 >>>>题目 [题目] https://www.luogu.org/problemnew/show/P3807 [输入格式] 第一行一个 ...

  7. 【XSY2691】中关村 卢卡斯定理 数位DP

    题目描述 在一个\(k\)维空间中,每个整点被黑白染色.对于一个坐标为\((x_1,x_2,\ldots,x_k)\)的点,他的颜色我们通过如下方式计算: 如果存在一维坐标是\(0\),则颜色是黑色. ...

  8. 【CTSC2017】【BZOJ4903】吉夫特 卢卡斯定理 DP

    题目描述 给你一个长度为\(n\)的数列\(a\),求有多少个长度\(\geq 2\)的不上升子序列\(a_{b_1},a_{b_2},\ldots,a_{b_k}\)满足 \[ \prod_{i=2 ...

  9. 卢卡斯定理&扩展卢卡斯定理

    卢卡斯定理 求\(C_m^n~mod~p\) 设\(m={a_0}^{p_0}+{a_1}^{p_1}+\cdots+{a_k}^{p_k},n={b_0}^{p_0}+{b_1}^{p_1}+\cd ...

随机推荐

  1. async await 同步方法调用异步方法死锁

    同步方法调用异步方法.GetAwaiter().GetResult()计算函数超时,异步方法所有的回调操作都会期望返回到主线程. 所以会导致各种线程死锁.异步方法中使用ConfigureAwait(f ...

  2. HDU_1068_Girls and Boys_二分图匹配

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. PHP 之ip查询接口

    /** * @param $ip 待查询的ip * @return mixed */ function getIpAddressInfo($ip) { $ipurl = 'http://api.ip1 ...

  4. Nginx +tomcat 实现负载均衡集群

    一.       工具   nginx-1.8.0 apache-tomcat-6.0.33 二.    目标   实现高性能负载均衡的Tomcat集群: 三.    步骤   1.首先下载Nginx ...

  5. C语言实现截屏

    实现手机控制电脑执行部分功能需要获取桌面状态,在网上找的一段截屏代码 /** * GDI 截屏函数 * 参数 hwnd 要截屏的窗口句柄 * 参数 dirPath 截图存放目录 * 参数 filena ...

  6. Unity jointmoto

    jointmoto是模拟电机的,他的参数包括了最高车速和最大扭矩 扭矩总是正数,而目标车速决定了方向

  7. Django Template(模板系统)

    一.Django模板 内置模板标签和过滤器 二.常用操作 两种特殊符号: {{  }}  和 {%  %} 变量相关的用: {{  }} 逻辑相关的用: {%  %} 2.1 变量 在Django的模 ...

  8. MySQL(端口3306)

    MySQL(二进制)安装: 下载地址:http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49-linux2.6-x86_64.tar.gz ...

  9. 8.mysql执行语句的顺序

    mysql执行语句的顺序     一.group by + where group by 字句和where条件语句结合在一起使用,where在前,group by 在后.即先对select xx fr ...

  10. String formatting in Python

    | \n | 换行   || \t | 制表符 || \  | 转义   || \\ | \      | the '%' operator is used to format a set of va ...