Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

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.

 
Source
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3033 3038 3036 3035 3034 
 
 
Tips:
  答案是求C(n+m,m)% P
  这里P不大,N过大,用lucas定理可以求出来;
 
Code:
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. long long t,n,m,ans,p,f[],x,y;
  8.  
  9. void exgcd(long long a,long long b){
  10. if(b==){
  11. x=; y=;
  12. return;
  13. }
  14. exgcd(b,a%b);
  15. long long t=x; x=y; y=t-(a/b)*y;
  16. return;
  17. }
  18.  
  19. long long lucas(long long a,long long b,long long MOD){
  20. long long res=;
  21. while(a&&b){
  22. long long aa=a%MOD,bb=b%MOD;
  23. if(aa<bb) return ;
  24. res=res*f[aa]%MOD;
  25. exgcd(f[aa-bb]*f[bb],MOD);
  26. x=(x%MOD+MOD)%MOD;
  27. res=(res*x)%MOD;
  28. a=a/MOD;
  29. b=b/MOD;
  30. }
  31. return res;
  32. }
  33.  
  34. void init(long long MOD){
  35. f[]=;
  36. for(int i=;i<=MOD;i++){
  37. f[i]=f[i-]*i%MOD;
  38. }
  39. }
  40.  
  41. int main(){
  42. scanf("%lld",&t);
  43. for(int i=;i<=t;i++){
  44. scanf("%lld%lld%lld",&n,&m,&p);
  45. n=n+m;
  46. init(p);
  47. ans=lucas(n,m,p);
  48. printf("%lld\n",ans);
  49. }
  50. }

hdu3037 Saving Beans的更多相关文章

  1. hdu3037 Saving Beans(Lucas定理)

    hdu3037 Saving Beans 题意:n个不同的盒子,每个盒子里放一些球(可不放),总球数<=m,求方案数. $1<=n,m<=1e9,1<p<1e5,p∈pr ...

  2. HDU3037 Saving Beans(Lucas定理+乘法逆元)

    题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...

  3. [HDU3037]Saving Beans,插板法+lucas定理

    [基本解题思路] 将n个相同的元素排成一行,n个元素之间出现了(n-1)个空档,现在我们用(m-1)个“档板”插入(n-1)个空档中,就把n个元素隔成有序的m份,每个组依次按组序号分到对应位置的几个元 ...

  4. hdu 3037 Saving Beans Lucas定理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. hdu 3037 Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. hdu 3037 Saving Beans(组合数学)

    hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...

  7. HDOJ 3037 Saving Beans

    如果您有n+1树,文章n+1埋不足一棵树m种子,法国隔C[n+m][m] 大量的组合,以取mod使用Lucas定理: Lucas(n,m,p) = C[n%p][m%p] × Lucas(n/p,m/ ...

  8. hdu 3037——Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. poj—— 3037 Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

随机推荐

  1. MySQL用户管理:添加用户、授权、删除用户

    文章首发于[博客园-陈树义],请尊重原创保留原文链接. 添加用户 以root用户登录数据库,运行以下命令: create user zhangsan identified by 'zhangsan'; ...

  2. 漫谈ELK在大数据运维中的应用

    漫谈ELK在大数据运维中的应用 圈子里关于大数据.云计算相关文章和讨论是越来越多,愈演愈烈.行业内企业也争前恐后,群雄逐鹿.而在大数据时代的运维挑站问题也就日渐突出,任重而道远了.众所周知,大数据平台 ...

  3. tensorflow笔记(三)之 tensorboard的使用

    tensorflow笔记(三)之 tensorboard的使用 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7429344.h ...

  4. Node.js之操作文件系统(一)

    Node.js之操作文件系统(一) 1. 同步方法与异步方法 在Node.js中,使用fs模块来实现所有有关文件及目录的创建.写入及删除操作.,在fs模块中,所有对文件及目录的操作都可以使用同步与异步 ...

  5. 关于操作系统中多个fork()会创建几个进程的理解

    最近在看操作系统的书,在讲到用fork()创建子进程时,有些地方一时迷惑,最终理解,特记录下来.如下: //创建一个子进程:#include "csapp.h" int main( ...

  6. java web轻量级开发面试教程摘录,java web面试技巧汇总,如何准备Spring MVC方面的面试

    本内容摘自 java web轻量级开发面试教程 https://baike.baidu.com/item/Java%20Web%E8%BD%BB%E9%87%8F%E7%BA%A7%E5%BC%80% ...

  7. oozie调用java实例------Java action

    Oozie支持Java action ,Java action 会自动执行workflow任务中制定的java类中的 public static void main(String[] args)方法, ...

  8. # hadoop入门第六篇:Hive实例

    前言   前面已经讲了如何部署在hadoop集群上部署hive,现在我们就做一个很小的实例去熟悉HIVE QL.使用的数据是视频播放数据包括视频编码,播放设备编码,用户账号编码等,我们在这个数据基础上 ...

  9. javaScript 设计模式系列之三:代理模式

    介绍 代理模式为其他对象提供一种代理以控制对这个对象的访问. 根据代理模式的使用目的不同,代理模式又可以分为多种类型: 远程代理(Remote Proxy) 虚拟代理(Virtual Proxy)如需 ...

  10. Java基础---泛型、集合框架工具类:collections和Arrays

    第一讲     泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...