Saving Beans

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

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.

 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3033 3038 3036 3035 3034 
 
题意:在n树中有多少种方式可以节省不超过m豆子(他们是一样的)。
思路:

题目可以转换成  x1+x2+……+xn=m 有多少组解,m在题中可以取0~m。

利用插板法可以得出x1+x2+……+xn=m解的个数为C(n+m-1,m);

则题目解的个数可以转换成求   sum=C(n+m-1,0)+C(n+m-1,1)+C(n+m-1,2)……+C(n+m-1,m)

利用公式C(n,r)=C(n-1,r)+C(n-1,r-1)  == >  sum=C(n+m,m)。

就是要求C(n+m,m)%p。

代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll t,n,m,p,ans;
ll read()
{
    ll x=,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
ll qpow(ll n,ll k)
{
    ll res=;
    while(k)
    {
        ) res=res*n%p;
        n=n*n%p; k>>=;
    }return res;
}
ll c(ll n,ll m)
{
    ;
    ll n1=,m1=;
    ;i<=n;i++)
     n1=n1*i%p;
    ;i<=m;i++)
     m1=m1*i%p;
    );
}
ll lus(ll n,ll m)
{
     ) ;
     return c(n%p,m%p)*lus(n/p,m/p)%p;
}
int main()
{
    t=read();
    while(t--)
    {
        n=read(),m=read(),p=read();
        ans=lus(n+m,m);
        printf("%lld\n",ans);
    }
    ;
}

最新文章

  1. wifi强度数据采集器(android)
  2. thinkphp多表关联并且分页
  3. SqlDependency 的使用
  4. Python学习笔记 :01概述
  5. Populating Next Right Pointers in Each Node 解答
  6. 纪中集训 Day 7
  7. js全选checkbox框
  8. Linux 定时任务详解
  9. data-packed volume container - 每天5分钟玩转 Docker 容器技术(43)
  10. python Mysql 库表
  11. [JavaScript] AMD和CMD概述
  12. 计算机网络--差错检测(帧检验序列FCS计算方法)
  13. ASP.NET MVC计划任务实现方法(定时执行某个功能)
  14. (转)python logging模块
  15. react native (一)
  16. 《图解 HTTP 》阅读 —— 第一章
  17. 使用java开发微信公众平台(1)
  18. 使用 puppeteer 创建一个自动化导出 PDF 的服务
  19. WINSOCK网络函数
  20. Source Insight 4.0的使用(转)

热门文章

  1. 转 mysql 5.7版本修改编码为utf-8
  2. 20 如何在C#中存一批数据,数组
  3. Java 创建Excel并逐行写入数据
  4. express模块安装使用命令配置
  5. Python学习日记之字典深复制与浅复制
  6. Android PopupWindow使用时注意
  7. Java 基础入门随笔(7) JavaSE版——面向对象定义、特征:封装、构造函数
  8. HDU_1143_tri tiling
  9. jquery onclick 问题
  10. valgrind检查代码内存泄漏,5种内存泄漏情况