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. DOM给表格添加新一行和删除整个行的内容

    DOM用appendChild()给表格添加新一行时,要注意,在HTML中没特别设置<thead>,<tbody>时,会自动添加上,所以要选择表格第一个元素在添加tr. // ...

  2. Shell 输出第五行的内容

    目录 Shell 输出第五行的内容 题目 题解-awk 题解-sed Shell 输出第五行的内容 题目 写一个 bash脚本以输出一个文本文件 nowcoder.txt 中第5行的内容. 示例: 假 ...

  3. 零基础学习java------25--------jdbc

    jdbc开发步骤图 以下要用到的products表 一. JDBC简介 补充    JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口,各个数据库厂商趋势线这个接口,提 ...

  4. android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token)

    android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token) 我的原因是因为string 里面有< ...

  5. rust常用技巧

    允许未使用的方法,写在文件开头,可过滤过掉该项提示 #![allow(unused)]

  6. spring注解-属性

    一.@Value 基本数值 可以写SpEL: #{} 可以写${}取出配置文件[properties]中的值(在运行环境变量里面的值) @Value("张三") private S ...

  7. golang vendor

    安装参考 https://blog.csdn.net/huwh_/article/details/77169858 Go 1.5引入了vendor文件夹,其对语言使用,go命令没有任何影响.若某个路径 ...

  8. Spring Boot中使用模板引擎Thymeleaf

    一.Thymeleaf简介 Thymeleaf[taɪm lif],百里香叶,是一个流行的模板引擎,该模板引擎采用Java语言开发.Java中常见的模板引擎有Velocity.Freemaker.Th ...

  9. window安装ab压力测试

    ab是Apache HTTP server benchmarking tool的缩写,可以用以测试HTTP请求的服务器性能,也是业界比较流行和简单易用的一种压力测试工具包 ## 下载 下载地址:(ht ...

  10. 智龙开发板搭建llsp环境

    智龙开发板搭建llsp(linux+lighttpd+sqlite3+php)环境 1. 准备 1. 智龙开发板V3 2. 软件编译环境:VirtualBox6+CentOS6.10-i386.min ...