hdu 3037——Saving Beans
Saving Beans
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8258 Accepted Submission(s): 3302
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.
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.
#include <cstdio>
#include <iostream>
using namespace std;
long long n,m,p;
long long sum[]; long long quick_mod(long long a,long long b)
{
long long ans=;
while(b)
{
if(b&)ans=ans*a%p;
b>>=;
a=a*a%p;
}
return ans;
} long long C(long long n,long long m)
{
if(n<m)return ;
return (sum[n]*quick_mod((sum[m]*sum[n-m])%p,p-)%p)%p;
} long long Lucas(long long n,long long m)
{
if(m==)return ;
return C(n%p,m%p)*Lucas(n/p,m/p)%p;
} int main()
{
int cas=;scanf("%d",&cas);
while(cas--)
{
scanf("%I64d%I64d%I64d",&n,&m,&p);
sum[]=;for(int i=;i<=p;i++)sum[i]=sum[i-]*i%p;
printf("%I64d\n",Lucas(n+m,m));
}
return ;
}
点个赞吧↓
hdu 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(Lucus定理+乘法逆元)
Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...
- 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定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- HDU 3037 Saving Beans(Lucas定理的直接应用)
解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...
- HDU 3037 Saving Beans (数论,Lucas定理)
题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...
- HDU 3073 Saving Beans
Saving Beans Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...
随机推荐
- mybatis报错:Caused by: java.lang.IllegalArgumentException: Caches collection already contains value for com.crm.dao.PaperUserMapper
一.问题 eclipse启动时报下面的错误: Caused by: java.lang.IllegalArgumentException: Caches collection already cont ...
- ListView展示不同布局需要注意的地方
尊重劳动成果,转载请标明出处:http://www.cnblogs.com/tangZH/p/8419010.html 我们在项目中经常需在一个listview中展示不一样的布局,我们可以在adapt ...
- Android 简单登陆 涉及 Button CheckBox TextView EditText简单应用
GitHub地址:https://github.com/1165863642/LoginDemo 直接贴代码<?xml version="1.0" encoding=&quo ...
- ADB和Fastboot最新版的谷歌官方下载链接
ADB和Fastboot for Windows https://dl.google.com/android/repository/platform-tools-latest-windows.zip ...
- Dubbo开发,利用项目模拟提供者和消费者之间的调用--初学
开发工具:IDEA,虚拟机 VMware Workstation 预备工作:安装好zookeeper的虚拟机,电脑jdk更换为1.7,本地tomcat启动,能够访问以下页面即可进行开发 2.建立以下s ...
- Centos7VMware虚拟机最小化安装后,安装Tenda U12 USB无线网卡驱动
前几天买下了Tenda U12 USB 无线网卡 ,想连接上无线玩玩,可惜买下折腾了一周才解决他它驱动问题,前后在VMware上装了十多次,测试了好几个内核版本才搞定,好了废话不多说,分享下我安装过程 ...
- geth搭建私有网络
geth --rpc --unlock "3ae88fe370c39384fc16da2c9e768cf5d2495b48,81063419f13cab5ac090cd8329d8fff9f ...
- Undefined attribute name (placeholder)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Cube的高级设置
分享来源地址:http://bigdata.51cto.com/art/201705/538648.htm Cube的高级设置 随着维度数目的增加,Cuboid 的数量会爆炸式地增长.为了缓解 Cub ...
- System.map文件的作用
有关System.map文件的信息好象很缺乏.其实它一点也不神秘,并且在整个事情当中它并不象看上去那么得重要.但是由于缺乏必要的文档说明,使其显得比较神秘.它就象耳垂,我们每个人都有,但却不知道是干什 ...