HDOJ 3037 Saving Beans
如果您有n+1树,文章n+1埋不足一棵树m种子,法国隔C【n+m】【m】
大量的组合,以取mod使用Lucas定理:
Lucas(n,m,p) = C[n%p][m%p] × Lucas(n/p,m/p,p) ;
Saving Beans
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2314 Accepted Submission(s): 845
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.
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.
2
1 2 5
2 1 5
3
3HintHint 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long int LL; LL n,m,p; LL fact[100100]; LL QuickPow(LL x,LL t,LL m)
{
if(t==0) return 1LL;
LL e=x,ret=1LL;
while(t)
{
if(t&1) ret=(ret*e)%m;
e=(e*e)%m;
t>>=1LL;
}
return ret%m;
} void get_fact(LL p)
{
fact[0]=1LL;
for(int i=1;i<=p+10;i++)
fact[i]=(fact[i-1]*i)%p;
} LL Lucas(LL n,LL m,LL p)
{
///lucas(n,m,p)=c[n%p][m%p]*lucas(n/p,m/p,p);
LL ret=1LL;
while(n&&m)
{
LL a=n%p,b=m%p;
if(a<b) return 0;
ret=(ret*fact[a]*QuickPow((fact[b]*fact[a-b])%p,p-2,p))%p;
n/=p; m/=p;
}
return ret%p;
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
LL n,m,p;
cin>>n>>m>>p;
get_fact(p);
cout<<Lucas(n+m,m,p)<<endl;
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
HDOJ 3037 Saving Beans的更多相关文章
- hdu 3037 Saving Beans(组合数学)
hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...
- hdu 3037 Saving Beans Lucas定理
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3037 Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3037——Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- poj—— 3037 Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Hdu 3037 Saving Beans(Lucus定理+乘法逆元)
Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...
- HDU 3037 Saving Beans(Lucas定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- HDU 3037 Saving Beans (Lucas法则)
主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...
- HDU 3037 Saving Beans(Lucas定理的直接应用)
解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...
随机推荐
- 简单的反射 把datatable 转换成list对象
/// <summary> /// 把datatable 转换成list对象 /// </summary> /// <typeparam name="T&quo ...
- 升级到cocos2d-x 2.0.2代码差异
来自:http://www.cnblogs.com/TopWin/archive/2012/09/12/2682042.html 近期看cocos2d-x 2.0.2公布后升级了一下.升级后发现又出现 ...
- iOS 真机调试(最具体的步骤来解决历史,hmt精心打造)
/*************************************************************1************************************* ...
- 使用ThinkPHP框架高速发展网站(多图)
使用ThinkPHP框架高速搭建站点 这一周一直忙于做实验室的站点,基本功能算是完毕了.比較有收获的是大概了解了ThinkPHP框架.写一些东西留作纪念吧.假设对于相同是Web方面新手的你有一丝丝帮助 ...
- 英特尔® 硬件加速执行管理器安装指南 — Microsoft Windows*
介绍 本文将指导您安装英特尔® 硬件加速执行管理器(英特尔® HAXM),这是一款可以使用英特尔® 虚拟化技术(VT)加快 Android* 开发速度的硬件辅助虚拟化引擎(管理程序). 前提条件 英特 ...
- Case learning
bad case: <?php foreach($user_detail AS $val) { if(!empty($val->portrait)) { //假设这个循环从来没有到达过 $ ...
- RESTful API Design With NodeJS & Restify
http://code.tutsplus.com/tutorials/restful-api-design-with-nodejs-restify--cms-22637 The RESTful API ...
- Paint获取Text的宽和高的数据
获取字符串的宽度: public static float GetTextWidth(String text, float Size) { // 第一个参数是要计算的字符串,第二个参数是字提大小 Te ...
- Java NIO 系列教程(转)
原文中说了最重要的3个概念,Channel 通道Buffer 缓冲区Selector 选择器其中Channel对应以前的流,Buffer不是什么新东西,Selector是因为nio可以使用异步的非堵塞 ...
- Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...