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 ...
随机推荐
- Html test
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- Python 之糗事百科多线程爬虫案例
import requests from lxml import etree import json import threading import queue # 采集html类 class Get ...
- 怎么选择最适合自己的Python培训机构?
Python培训已经成为入门Python的一个重要途径,它的优势在于学习知识的系统性.快速性和实用性.Python培训毕业的学员大多数拥有较强的实战动手能力,能够较快上手,更符合企业需求. 不过,大部 ...
- 在vue中通过js动态控制图片按比列缩放
1.html 通过外层的div来给img对应的class,隐藏的img是得到img图片请求回来时的原始尺寸.外层div是固定大小,因此,图片有两种情况去适应外部div的尺寸.一种是宽度大于高度的情况, ...
- recreate dbcontrol on database 11.2.0.1 using emca
[oracle@osb ~]$ env | grep ORA ORACLE_SID=ACE ORACLE_BASE=/oracle ORACLE_TERM=xterm ORACLE_HOME=/ora ...
- Getmemory问题
题目一: [cpp] view plaincopy void GetMemory( char *p ) { p = ( ); } void Test( void ) { char *str = NUL ...
- 准备MPI编程环境——Visual Studio
准备 下载并安装Visual Studio 2017 下载并安装MPI (建议使用MSMPI,相对简单方便一点,可以从微软官网下载获得) 配置 新建空白项目 在该项目中新建源文件 右击项目-> ...
- 【codeforces 509C】Sums of Digits
[题目链接]:http://codeforces.com/contest/509/problem/C [题意] 给你一个数组b[i] 要求一个严格升序的数组a[i]; 使得a[i]是b[i]各个位上的 ...
- Wizard's Tour
F. Wizard's Tour time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Eleastisearch6.0.0 read_only_allow_delete: false
Eleastisearch6.0.0由单节点升级到多节点集群cluster时候出现的分片同步错误问题解决 原创 2018年01月18日 16:33:21 5 启动多个节点的ES后,ES开始推举mast ...