HDU 3037 Saving Beans(Lucas定理的直接应用)
解题思路:
直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题。
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <cstdio>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#define LL long long
#define FOR(i, x, y) for(int i=x;i<=y;i++)
using namespace std;
const int maxn = 100000 + 10;
LL Pow_mod(LL a, LL b, LL mod)
{
LL ret = 1;
while(b)
{
if(b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
LL fac[maxn];
void init(LL p)
{
fac[0] = 1;
FOR(i, 1, p) fac[i] = (fac[i-1] * i) % 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] * Pow_mod(fac[b] * fac[a-b] % p , p - 2 , p)) % p;
n /= p, m /= p;
}
return ret;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n, m, p;
scanf("%d%d%d", &n, &m, &p);
init(p);
printf("%d\n", (int)Lucas(n + m, m, p));
}
return 0;
}
HDU 3037 Saving Beans(Lucas定理的直接应用)的更多相关文章
- 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(Lucus定理+乘法逆元)
Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...
- hdu 3037 Saving Beans(组合数学)
hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- HDU 3037 Saving Beans (数论,Lucas定理)
题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...
- 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
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(Lucas定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
随机推荐
- 这些年,我收集的JavaScript代码(一)
一.取URL中的参数 function getParameterByName(name) { var match = RegExp('[?&]' + name + '=([^&]*)' ...
- UML-状态图,顺序图,活动图
一.编写用例文档 1.用例的内容: 用例编号 用例名 执行者 前置条件 后置条件 基本路径 扩展路径 字段列表 业务规则 ...
- 【git】error: Your local changes to the following files
今天在服务器上git pull是出现以下错误: error: Your local changes to the following files would be overwritten by mer ...
- block,inline,inline-block
block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div>, <p>, <h1>, ...
- Delphi下重载窗体CreateParams翻转关闭按钮
type TForm1 = class(TForm) private { Private declarations } public { Public declarations } ...
- Delphi实现全局鼠标钩子
其中涉及到的一些API,网上均能查到详细的解释,这里不再熬述.源码下载 因为是全局钩子,所以要用dll注入.用到的鼠标消息结构如下: PMouseHookStruct = ^TMouseHookStr ...
- 关于IE的兼容模式
前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外观,IE8引入了文件兼容性.在IE6中引入一个增设的兼容性模式,文件兼容性使你能够在IE呈现你的网页时选择特定编译模式. 新的IE为了确保网页 ...
- 栈和队列的Java实现
一. 栈 1.概念 栈是一种特殊的线性表,它只能在栈顶(top)进行插入(push)和删除(pop)操作. 栈的常用操作: 入栈(push):向栈顶插入元素 出栈(pop):从栈顶删除元素 访问栈顶 ...
- Spring、Bean的生命周期
1.默认情况下,在Bean容器被实例化的时候,bean对象将被创建: public class PersonServiceImpl implements PersonIService { public ...
- java.lang.OutOfMemoryError: PermGen space 解决方案
只需两步: 将值改为512或者1024,然后CTRL+S,重启tomcat 和eclipse即可.