Hdu 3037 Saving Beans(Lucus定理+乘法逆元)
Saving Beans
Time Limit: 3000 MS Memory Limit: 32768 K
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
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,求方案数模p后的值.
1<=n,m<=10^9,1< p < 10^5,保证p是素数.
题解(第一次用数学编辑器2333)
#include<iostream>
#include<cstdio>
#define MAXN 100001
#define LL long long
using namespace std;
LL M[MAXN];
LL mi(LL a,LL b,LL p)
{
LL tot=1;
while(b)
{
if(b&1) tot=tot*a%p;
a=a*a%p;
b>>=1;
}
return tot;
}
LL C(LL n,LL m,LL p)
{
if(m>n) return 0;
LL tot=1;
return M[n]*mi(M[n-m],p-2,p)%p*mi(M[m],p-2,p)%p;
}
LL lucus(LL n,LL m,LL p)
{
if(!m) return 1;
return lucus(n/p,m/p,p)*C(n%p,m%p,p)%p;
}
int main()
{
LL n,m,p,t;
cin>>t;
while(t--)
{
cin>>n>>m>>p;
M[0]=1;
for(int i=1;i<=p;i++) M[i]=M[i-1]*i%p;
printf("%lld\n",lucus(n+m,m,p));
}
return 0;
}
Hdu 3037 Saving Beans(Lucus定理+乘法逆元)的更多相关文章
- 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(Lucas定理的直接应用)
解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...
- hdu 3037 Saving Beans(组合数学)
hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...
- 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. ...
- 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定理 ...
- bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)
Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...
随机推荐
- 洛谷 P1411 树 (树形dp)
大意: 给定树, 求删除一些边, 使得连通块大小的乘积最大 设$dp_{i,j}$表示只考虑点$i$的子树, $i$所在连通块大小为$j$的最大值. 转移的时候不计算$i$所在连通块的贡献, 留到最后 ...
- SpringCloud整合sleuth,使用zipkin时不显示服务
转载于:https://www.cnblogs.com/Dandwj/p/11179141.html 原文地址:https://blog.csdn.net/weixin_30416497/articl ...
- Spring-Cloud之开篇
一.为什么会有spring-cloud.随着现代互联网的发展,以前很多传统的单体项目将不再满足于现在的互联网需求,而这个时候就诞生了另外一种说法,微服务.简单理解就是将软件应用程序独立部署的服务的一中 ...
- 【WEB基础】HTML & CSS 基础入门(1)初识
前面 我们每天都在浏览着网络上丰富多彩的页面,那么在网页中所呈现出的绚丽多彩的内容是怎么设计出来的呢?我们想要自己设计一个页面又该如何来做呢?对于刚刚接触网页设计的小伙伴来说,看到网页背后的一堆符号和 ...
- pandas-01 Series()的几种创建方法
pandas-01 Series()的几种创建方法 pandas.Series()的几种创建方法. import numpy as np import pandas as pd # 使用一个列表生成一 ...
- 原生JavaScript遮罩
/* 适用原生JS */ function showInfo(info) { var zzInfo = info; var mask_bg = document.createEleme ...
- css3 text-fill-color属性
text-fill-color是什么意思呢?单单从字面上来看就是“文本填充颜色”,不过它实际也是设置对象中文字的填充颜色,和color的效果很相似.如果同时设置text-fill-color和colo ...
- iOS多线程GCD简介(二)
在上一篇中,我们主要讲了Dispatch Queue相关的内容.这篇主要讲一下一些和实际相关的使用实例,Dispatch Groups和Dispatch Semaphore. dispatch_aft ...
- Synchronized可重入锁通俗易懂的简单分析
可重入锁概念: 当一个线程得到一个对象锁后,再次请求此对象时时可以再次得到该对象的锁的,这也证明synchronized方法/块的内部调用本类的其他synchronized方法/块时,时永远可以得到锁 ...
- mysql模糊查询1,11,111用逗号(其他符号)拼接的相似字符串
mysql进行模糊查询时,基本都是LIKE "%sss%",有时候这种查询时准确的,但是有种情况这种查询会出现很大问题. 看一下下面这张表 如果想查询字段test包含1的数据,一般 ...