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 }

最新文章

  1. java生成压缩文件
  2. MySQL之aborted connections和aborted clients
  3. Redis中常用命令
  4. [Netbeans]为面板设置背景图片
  5. Class类中getMethods() 与getDeclaredMethods() 方法的区别
  6. 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简介
  7. Android Studio学习随笔-移动动画的实现
  8. linux shell学习记录
  9. Django 源码小剖: 初探 WSGI
  10. CentOS下Eclipse的安装教程
  11. CodeForces 707A Brain&#39;s Photos
  12. 初学者易上手的SSH-struts2 04值栈与ognl表达式
  13. 自动化之路 python psutil模块 收集硬件信息
  14. Nginx概述和安装(1)
  15. 【Python】SciKit-Learn包安装问题
  16. 【SQLServer】附加数据库失败
  17. default.conf
  18. html5-figure和figcaption元素
  19. css-过渡
  20. LeetCode--263--丑数

热门文章

  1. Flume消费内外网分流配置的Kafka时遇到的坑
  2. 8.深入TiDB:解析Hash Join实现原理
  3. 基于python win32setpixel api 实现计算机图形学相关操作
  4. API 管理在云原生场景下的机遇与挑战
  5. day13 iptables防火墙
  6. flink---实时项目----day03---1.练习讲解(全局参数,数据以parquet格式写入hdfs中) 2 异步查询 3 BroadcastState
  7. RB-Tree深度探索
  8. gen already exists but is not a source folder. Convert to a source folder or rename it 的解决办法
  9. Appium获取toast消息遇到的问题(一)
  10. arcgis api for js自定义引用方式