Saving Beans

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

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.

 
 
 
2 3 107 ==>10 ==>C(5,2)
 
{ 0,0
   0,1  1,0
   1,1, 2,0  0,2
   3,0  0,3   1,2  2,1
}
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
typedef __int64 LL; LL dp[]; void init(LL p){
int i;
dp[]=;
for(i=;i<=p;i++)
dp[i]=(dp[i-]*i)%p;
}
LL pow_mod(LL a,LL n,LL p)
{
LL ans=;
while(n)
{
if(n&) ans=(ans*a)%p;
n=n>>;
a=(a*a)%p;
}
return ans;
}
LL C(LL a,LL b,LL p)
{
if(a<b) return ;
if(b>a-b) b=a-b;
LL sum1=dp[a];
LL sum2=(dp[b]*dp[a-b])%p;
sum1=(sum1*pow_mod(sum2,p-,p));
return sum1;
}
LL Lucas(LL n,LL m,LL p)
{
LL ans=;
while(n&&m&&ans)
{
ans=(ans*C(n%p,m%p,p))%p;
n=n/p;
m=m/p;
}
return ans;
}
int main()
{
int T;
LL n,m,p;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d",&n,&m,&p);
init(p);
if(n>m)swap(n,m);
LL ans= Lucas(n+m,m,p);
printf("%I64d\n",ans);
}
return ;
}

最新文章

  1. 渐进式框架、自底向上增量开发的vue
  2. 学习Linux下s3c2440的USB鼠标驱动笔记
  3. java提高篇---HashMap
  4. linux 查看程序是否运行
  5. 1837. Isenbaev&#39;s Number(floyd)
  6. ZZ的计算器
  7. js事件绑定函数
  8. 【Qt编程】3D迷宫游戏
  9. JS异步操作新体验之 async函数
  10. Elasticsearch Search API
  11. mysql数据表的基本操作
  12. 一张图片资源要占用多大内存xhdpi xxhdpi
  13. 前段基础之CSS
  14. QFileSystemModel中通过flags函数反应代码的层级思考
  15. CF1081A
  16. itemscope itemtype=&quot;http://schema.org/AggregateRating&quot;
  17. 大规模集群下的Hadoop NameNode
  18. LeetCode – Group Shifted Strings
  19. linux ps查进程 kill关闭进程
  20. 清理MVC4 Internaet 项目模板清理

热门文章

  1. Codeforce Round #222 Div2
  2. poj 题目分类(1)
  3. linux扩大swap交换空间
  4. Javascript中函数及变量定义的提升
  5. 远程mysql服务器无法连接解决方案
  6. 关于基于.NET Framework的网络通信程序底层扫盲
  7. django models使用学习记录
  8. mysql单表查询&amp;&amp;多表查询(职员表14+9)
  9. loadruner报错:Step download timeout(120 seconds)的一个解决方法
  10. java中枚举类的使用详解