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 ...
随机推荐
- Archive & Backup 概念
Archive & Backup 提起归档和备份两个词,给人感觉上是相同的概念,就是对指定文件的一个copy而已.archive和backup感觉是相似的,但是他们有着明显的不同de. arc ...
- 我从来不理解JavaScript闭包,直到有人这样向我解释它...
摘要: 理解JS闭包. 原文:我从来不理解JavaScript闭包,直到有人这样向我解释它... 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 正如标题所述,JavaScript闭包 ...
- 广州.NET微软技术俱乐部提技术问题的正确方式
这是 北京.NET微软技术俱乐部 里一个人问问题的方式, 很赞, 所以希望大家问问题也采用这种方式. 耗时少, 使用12月8日活动上董志强先生介绍的windows 10 Snip&Sketch ...
- Android为TV端助力(转载)
作者地址http://www.jianshu.com/u/63915ef020e2 针对Android Tv的自定义RecyclerView 作者 wenju_song 关注 2016.12.09 1 ...
- Eclipse插件开发教程-插件的导出和安装应用
Eclipse插件可以给开发人员提供不少便利,在很多场景下,插件甚至是必不可少的.那么怎么自己开发一个插件呢?前面两篇讲了怎么开发插件,那么开发完了Eclipse插件之后,怎么使用呢?下面就讲讲插件的 ...
- linux下编译protobuf
这里我介绍两种方法,一是直接ccmake配置,二是修改cmake文件下面的CMakeList.txt文件 第一种方法:配置ccmake 1.安装sudo apt-get install cmake-c ...
- Undefined attribute name (placeholder)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 程序员利器Tmux使用手册
转:https://blog.csdn.net/chenqiuge1984/article/details/80132042
- 高德地图 Service 创建服务 USERKEY_PLAT_NOMATCH
在使用高的地图 创建服务的时候 { "errmsg": "USERKEY_PLAT_NOMATCH", "errcode": 10009, ...
- UGUI合批原理笔记
可以通过Frame debugger查看每个drawcall绘制了哪些东西 UGUI源码下载地址:https://bitbucket.org/Unity-Technologies/ui/downloa ...