Saving Beans

Time Limit: 3000 MS Memory Limit: 32768 K

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

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.

题意:

由n个不同的盒子,在每个盒子中放一些球(可以不放),使得总球数<=m,求方案数模p后的值.

1<=n,m<=10^9,1< p < 10^5,保证p是素数.

题解(第一次用数学编辑器2333)

#include<iostream>
#include<cstdio>
#define MAXN 100001
#define LL long long
using namespace std;
LL M[MAXN];
LL mi(LL a,LL b,LL p)
{
LL tot=1;
while(b)
{
if(b&1) tot=tot*a%p;
a=a*a%p;
b>>=1;
}
return tot;
}
LL C(LL n,LL m,LL p)
{
if(m>n) return 0;
LL tot=1;
return M[n]*mi(M[n-m],p-2,p)%p*mi(M[m],p-2,p)%p;
}
LL lucus(LL n,LL m,LL p)
{
if(!m) return 1;
return lucus(n/p,m/p,p)*C(n%p,m%p,p)%p;
}
int main()
{
LL n,m,p,t;
cin>>t;
while(t--)
{
cin>>n>>m>>p;
M[0]=1;
for(int i=1;i<=p;i++) M[i]=M[i-1]*i%p;
printf("%lld\n",lucus(n+m,m,p));
}
return 0;
}

Hdu 3037 Saving Beans(Lucus定理+乘法逆元)的更多相关文章

  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(Lucas定理的直接应用)

    解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...

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

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

  4. hdu 3037——Saving Beans

    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(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  7. HDU 3037 Saving Beans (数论,Lucas定理)

    题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...

  8. HDU 3037 Saving Beans (Lucas法则)

    主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...

  9. bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)

    Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...

随机推荐

  1. 【1】TOPK最小的K个数(多种方法比较)

    (头条) 最小的第K个数也是和这题topK一样的思路 1.全排序  时间复杂度O(nlogn) 2.Partiton思想 时间复杂度O(n)  (因为不需要像快排一样对所有的分段都两两Partitio ...

  2. 解决使用RabbitTemplate操作RabbitMQ,发生The channelMax limit is reached. Try later.问题

    使用RabbitTemplate操纵RabbitMQ,每个RabbitTemplate等于一个connection,每个connection最多支持2048个channel,当hannel达到2048 ...

  3. kafka学习笔记(二)——基础入门

    1.集群规划 从官网下载jar包 http://kafka.apache.org/downloads.html,我选择的是kafka_2.11-0.11.0.0.tgz版本 规划一下集群环境先~ ha ...

  4. java之抽象类介绍

    什么抽象方法和抽象类 抽象方法 在类里面定义的没有方法体且用关键字“abstract”来修饰的方法就是抽象方法,所谓的没有方法体指的是在方法声明的时候没有大括号以及其中的内容,而是直接在声明时在方法名 ...

  5. AngularJS 插值字符串 $interpolate

    定义: $interpolate:编译一段带有嵌入标记的语句,然后返回一个interpolate(插值)函数.使用: $interpolate(text,[mustHaveException],[tr ...

  6. token jwt配置

    1. token jwt配置 1.1. pom <!-- token验证 --> <dependency> <groupId>io.jsonwebtoken< ...

  7. react学习记录(一)

    一.React是什么 声明式写法(强调结果,命令式编程强调过程) 组件化 一次学习,随处编写(多种应用场景,web程序,原生手机应用,系统应用,命令行工具) 二.为什么学习react 大公司加持-fa ...

  8. SQL常见的一些面试题(太有用啦)

    SQL常见面试题 1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name   kecheng   fenshu张三    语文       81张三     数学       75李四 ...

  9. MVC、MVP及MVVM之间的关系

    介绍 写这篇随笔完全是为了加深自己的印象,毕竟写比看能获得得更多,另外本人对这三种模式的认识还是浅薄的,有待在以后的工作学习中有更深入的理解,因此不免会有误解,这里推荐大家阅读廖雪峰关于MVVM的介绍 ...

  10. 软工作业 wc-java

    项目要求: 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处理多个文件. 具体功能 -c 返回文件字符数 -w 返回词的数目 -l 返回行数 扩展 ...