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个 ...
随机推荐
- js提取135编辑器相同的css
135编辑器导入的内容,有重复的很多css,导致加载很慢,只能去掉 function remove135FormatContent(content){ if(!content) return ''; ...
- SQL server字符串分割成表-表分割为字符串
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ),)) )) as begin declare @i int set @SourceSql=rtri ...
- Net Core 自定义 Middleware 加密解密
前言:第一次写文章,有问题请轻喷 当前使用 Net Core 版本 2.1.3 我们经常在开发中需要把实体的主键 Id 传输到前端,但是在Get的时候又不想让前端能看到明文,我们通常会加密这些数据,所 ...
- 2019 房多多java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.房多多等公司offer,岗位是Java后端开发,因为发展原因最终选择去了房多多,入职一年时间了,也成为了面试官 ...
- 【转载】WPS通过设置密码的方式对Excel文件加密
有时候Excel文件中可能包含一些敏感数据,此时希望对Excel文件进行加入密码的形式进行加密保护,在WPS软件和Office Excel软件中都支持对Excel文件进行密码保护,设置了密码保护的Ex ...
- Oracle中nlssort()函数排序功能
转自:https://www.iteye.com/blog/libaxiaoyuan-2199851 Oracle9i之前,中文是按照二进制编码进行排序的.在oracle9i中新增了按照拼音.部首.笔 ...
- Navicat链接数据库报错1130解决方案
1.背景 使用localhost 可以正常连接mysql服务器,但是使用ip地址连接保存如下: 2.问题原因 默认情况下root用户只允许本机访问,即使用localhost访问,如下图: 解决方案:将 ...
- idea2018.3导入grails项目,无法正常使用问题解决
注:作者的grails版本为2.0.4,grails3版本以上的导入方式为gradle方式 一.导入grails项目 1.启动新项目向导.如果IntelliJ IDEA目前没有打开任何项目,请在欢迎屏 ...
- Oracle 数据块
以emp表为例 SYS@ prod>select * from scott.emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO --------- ...
- 基于gtk的imshow:用stb_image读取图像并用gtk显示
在前面一篇,已经能够基于gtk读取图像并显示.更前面的一篇:基于GDI的imshow:使用stb_image读取图像并修正绘制,通过stb_image读取图像并通过GDI显示图像,实现了一个imsho ...