Saving Beans

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

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定理、组合数公式。
通过分析题目可以得到方案数为C(n+m-1,n-1)%p
再通过化简得:方案数为C(m+n,m)%p
 
由于考虑到T的值很大,p的值较小,我们要对1...p 的阶乘打表
另外,一定要注意0! = 1  !!!
血的教训
 
代码:
 #include <cstdio>
#include <iostream>
using namespace std;
long long n,m,p;
long long sum[]; long long quick_mod(long long a,long long b)
{
long long ans=;
while(b)
{
if(b&)ans=ans*a%p;
b>>=;
a=a*a%p;
}
return ans;
} long long C(long long n,long long m)
{
if(n<m)return ;
return (sum[n]*quick_mod((sum[m]*sum[n-m])%p,p-)%p)%p;
} long long Lucas(long long n,long long m)
{
if(m==)return ;
return C(n%p,m%p)*Lucas(n/p,m/p)%p;
} int main()
{
int cas=;scanf("%d",&cas);
while(cas--)
{
scanf("%I64d%I64d%I64d",&n,&m,&p);
sum[]=;for(int i=;i<=p;i++)sum[i]=sum[i-]*i%p;
printf("%I64d\n",Lucas(n+m,m));
}
return ;
}

点个赞吧↓

hdu 3037——Saving Beans的更多相关文章

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

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

  2. hdu 3037 Saving Beans Lucas定理

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

  3. hdu 3037 Saving Beans

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

  4. Hdu 3037 Saving Beans(Lucus定理+乘法逆元)

    Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...

  5. HDU 3037 Saving Beans (Lucas法则)

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

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

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

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

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

  9. HDU 3073 Saving Beans

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

随机推荐

  1. Oracle day05 索引_数据去重

    索引 自动:当在表上定义一个primary key或者unique 约束条件时,oracle数据库自动创建一个对应的唯一索引. 手动:用户可以创建索引以加速查询 在一列或者多列上创建索引: creat ...

  2. JAVA程序员面试30问(附带答案)

    第一,谈谈final, finally, finalize的区别. 最常被问到.final修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能 ...

  3. 2013年第四届蓝桥杯javaB组 试题 答案 解析

    1.世纪末的星期 曾有邪教称1999年12月31日是世界末日.当然该谣言已经不攻自破. 还有人称今后的某个世纪末的12月31日,如果是星期一则会.... 有趣的是,任何一个世纪末的年份的12月31日都 ...

  4. AJAX跨站处理解决方案

    //直接使用ajax会提示跨站失败 $.ajax({ type : 'POST', url : 'http://www.abc.com/api', data : '', dataType : 'tex ...

  5. 仿9GAG制作过程(一)

    有话要说: 准备开始学习Android应用程序的一个完整的设计过程.准备做一个仿9GAG的APP,前端界面设计+后台数据爬虫+后台接口设计,整个流程体验一遍.今天准备先把前端界面的框架给完成了. 成果 ...

  6. Android 简单登陆 涉及 Button CheckBox TextView EditText简单应用

    GitHub地址:https://github.com/1165863642/LoginDemo 直接贴代码<?xml version="1.0" encoding=&quo ...

  7. C# 仿360悬浮球开发demo程序

    https://files.cnblogs.com/files/wohexiaocai/%E4%BB%BF360%E5%8A%A0%E9%80%9F%E5%99%A8.zip

  8. 从0开始的Python学习003序列

    sequence 序列 序列是一组有顺序数据的集合.不知道怎么说明更贴切,因为python的创建变量是不用定义类型,所以在序列中(因为有序我先把它看作是一个有序数组)的元素也不会被类型限制. 序列可以 ...

  9. 解决sqlserver数据库显示单个用户

    今天突然发现数据库显示为单个用户并且,访问速度超慢,执行以下语句解决了 USE master; GO DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQ ...

  10. python粗谈面向对象(二)

    浅谈super() super并不是一个函数,是一个类名,形如super(B, self)事实上调用了super类的初始化函数,产生了一个super对象:Python的多继承类是通过mro的方式来保证 ...