hdu 3037Saving Beans(卢卡斯定理)
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
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.
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.
1 2 5
2 1 5
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.
/*
题目相当于求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(卢卡斯定理)的更多相关文章
- hdu3037Saving Beans——卢卡斯定理
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037 卢卡斯定理模板——大组合数的取模 代码如下: #include<iostream> #i ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- hdu3037——卢卡斯定理
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037 卢卡斯定理模板——大组合数取模 #include<iostream> #include& ...
- 【BZOJ4403】序列统计(组合数学,卢卡斯定理)
[BZOJ4403]序列统计(组合数学,卢卡斯定理) 题面 Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取 ...
- 【Luogu3807】【模板】卢卡斯定理(数论)
题目描述 给定\(n,m,p(1≤n,m,p≤10^5)\) 求 \(C_{n+m}^m mod p\) 保证\(P\)为\(prime\) \(C\)表示组合数. 一个测试点内包含多组数据. 输入输 ...
- 【数论】卢卡斯定理模板 洛谷P3807
[数论]卢卡斯定理模板 洛谷P3807 >>>>题目 [题目] https://www.luogu.org/problemnew/show/P3807 [输入格式] 第一行一个 ...
- 【XSY2691】中关村 卢卡斯定理 数位DP
题目描述 在一个\(k\)维空间中,每个整点被黑白染色.对于一个坐标为\((x_1,x_2,\ldots,x_k)\)的点,他的颜色我们通过如下方式计算: 如果存在一维坐标是\(0\),则颜色是黑色. ...
- 【CTSC2017】【BZOJ4903】吉夫特 卢卡斯定理 DP
题目描述 给你一个长度为\(n\)的数列\(a\),求有多少个长度\(\geq 2\)的不上升子序列\(a_{b_1},a_{b_2},\ldots,a_{b_k}\)满足 \[ \prod_{i=2 ...
- 卢卡斯定理&扩展卢卡斯定理
卢卡斯定理 求\(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 ...
随机推荐
- (转)postgis常用函数介绍(二)
http://blog.csdn.net/gisshixisheng/article/details/47903151 概述: 书接上文,本文继续讲解Postgres中常用的空间函数的使用. 常用函数 ...
- discuz 微社区开通
检测api: http://wsq.discuz.com/?a=apitest
- BZOJ 1606 USACO 2008 Dec. 购买干草
[题意概述] 有n件物品,每件物品有体积Vi,背包容量为C,问最多可以装多少体积的物品 [题解] 显然是个无限背包嘛.. 直接做背包DP就好 注意无限背包的写法和01背包的区别 #include< ...
- Codeforces 939D - Love Rescue
传送门:http://codeforces.com/contest/939/problem/D 本题是一个数据结构问题——并查集(Disjoint Set). 给出两个长度相同,且仅由小写字母组成的字 ...
- Navicat premium连接Oracle报ORA-12545错误
1:ORA-12545 原因: 这里填localhost,127.0.0.1,或者远程ip.
- Java基础——基础数据类型与读入输出
首先我们写完了HelloWorld就学会了java的一种输出 System.out.println() 用起来就像是被强化过的C++的puts函数 或者就是自带endl的cout函数,中间的" ...
- Python 3 条件语句
条件语句: 用于判定,判定是否符合某条件,符合则执行,不符合则不执行该条件所定义的操作. 一步判定: 用于理解不会这样使用. if 1==1: if条件判定只能出现一次. print(&q ...
- libcloud代码研究(三)——bugs
Bug 1:对不可迭代类进行迭代(libcloud.storage.driver.cloudfile line. 141-142) 使用libcloud连接自搭建swift服务,自己在服务器 ...
- CODEVS——T 2969 角谷猜想
http://codevs.cn/problem/2969/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descri ...
- HDU 3681
也算难题,难在如何处理有些点可以无限次经过 问题. 这道题,其实很容易想到二分+TSP的状态压缩,但在处理上述问题时,确实没想到.题解是处理每一个Y或G或F点到其他YGF点的距离,BFS,这样就出现一 ...