Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4153    Accepted Submission(s): 1607

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.

 思路:隔板法+lucas定理;
 题意:在n个树上放小于m个苹果有多少种方案;
 首先我们先考虑放m个苹果在n棵树上有多少种方案,问题转化为求x1+x2+...xn=m的方案数。
 这个就可以用隔板法来求,那么就是C(n+m-1,n-1)=C(n+m-1,m);
 那么答案就是C(n-1,0)+C(n,1)+C(n+1,2)+...C(n+m-1,m);
 根据杨辉三角上式等于C(n,0)+C(n,1)+C(n+1,2)+...C(n+m-1,m);逐项两两合并就可以得到C(n+m,m);
 那么由于primep比较小,并且n+m比较大,所以用lucas定理去求;

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<map>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 LL quick(LL n,LL m,LL mod);
12 LL lucas(LL n,LL m,LL mod);
13 LL a[100005];
14 int main(void)
15 {
16 int i,j,k;
17 LL x,y,z;
18 scanf("%d",&k);
19 while(k--)
20 {
21 scanf("%lld %lld %lld",&x,&y,&z);
22 a[0]=1;
23 a[1]=1;
24 for(i=2; i<=z; i++)
25 {
26 a[i]=a[i-1]*i;
27 a[i]%=z;
28 }
29 LL n=(x+y);
30 LL m=x;
31 LL ask=lucas(m,n,z);
32 printf("%lld\n",ask%z);
33 }
34 return 0;
35 }
36 LL quick(LL n,LL m,LL mod)
37 {
38 LL ans=1;
39 while(m)
40 {
41 if(m&1)
42 {
43 ans=ans*n%mod;
44 }
45 n=n*n%mod;
46 m/=2;
47 }
48 return ans;
49 }
50 LL lucas(LL n,LL m,LL mod)
51 {
52 if(n==0)
53 {
54 return 1;
55 }
56 else
57 {
58 LL x1=n/mod;
59 LL x2=m/mod;
60 LL t1=n%mod;
61 LL t2=m%mod;
62 LL t3=a[t2-t1]*a[t1]%mod;
63 if(t2<t1)return 0;
64 LL nit3=quick(t3,mod-2,mod);
65 return (nit3*a[t2]%mod*lucas(x1,x2,mod)%mod)%mod;
66 }
67 }

Saving Beans(hud3037)的更多相关文章

  1. hdu 3037 Saving Beans Lucas定理

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

  2. hdu 3037 Saving Beans

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

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

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

  4. 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/ ...

  5. hdu3037 Saving Beans

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

  6. hdu 3037——Saving Beans

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

  7. hdu3037 Saving Beans(Lucas定理)

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

  8. poj—— 3037 Saving Beans

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

  9. HDU 3073 Saving Beans

    Saving Beans Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

随机推荐

  1. mysql 实现某年单季度内的品牌TOPn销量在此年此单季度内销量占比

    数据表:       结果表: mysql语句:  

  2. kubernetes部署Docker私有仓库Registry

    在后面的部署过程中,有很多的docker镜像文件,由于kubernetes是使用国外的镜像,可能会出现下载很慢或者下载不下来的情况,我们先搭建一个简单的镜像服务器,我们将需要的镜像下载回来,放到我们自 ...

  3. TD课程通最终版本体验

    功能上,新版本增加了学校教室的上课情况,有无课程可以清楚查询,如下图: 在添加课程的设置上有改进,相比于之前编辑课程后不能保存,新版本在可保存的基础上又增加了登陆教务系统的功能,学生使用更加方便快捷, ...

  4. 『学了就忘』Linux文件系统管理 — 66、通过图形界面进行LVM分区

    目录 1.选择自定义分区 2.分配boot分区 3.创建LVM物理卷 4.生成卷组 5.创建逻辑卷 6.格式化安装 我们先用新安装Linux系统时的图形化界面,来演示一下LVM逻辑卷如何进行分区. 提 ...

  5. 日常Java 2021/11/16

    获得applet参数 下面的例子演示了如何使用一个Applet响应来设置文件中指定的参数.该Applet显示了-个黑色棋盘图案和第二种颜色.第二种颜色和每一列的大小通过文档中的Applet的参数指定. ...

  6. [web安全] 利用pearcmd.php从LFI到getshell

    有一段时间没写blog了,主要是事多,加上学的有些迷茫,所以内耗比较大.害,沉下心好好学吧. 漏洞利用背景: 允许文件包含,但session等各种文件包含都已经被过滤了.ctf题中可以关注regist ...

  7. restful接口文档

    1.先理清业务bai流程 2.定义前后端开发的接口规范.比如json的格dao式,url的格式 3.定内义接口文容档,这里的接口文档一般就是对应后台的实体reqVo(调用后台接口<控制器> ...

  8. spring注解-bean生命周期

    https://www.jianshu.com/p/70b935f2b3fe bean的生命周期 bean创建---初始化----销毁的过程 容器管理bean的生命周期 对象创建:容器启动后调用bea ...

  9. Spring Cloud服务离线

    服务离线,即某服务不能对外提供服务了.服务离线的原因有两种:服务下架与服务下线.这两种方案都是基于Actuator监控器实现的. 服务下架:将注册到Eureka Server中的Eureka Clie ...

  10. 【编程思想】【设计模式】【创建模式creational】建造者模式builder

    Python版 https://github.com/faif/python-patterns/blob/master/creational/builder.py #!/usr/bin/python ...