Saving Beans

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

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,求方案数(mod p).
1<=n,m<=10^9,1<p<10^5,保证p是素数

分析:
设最后放了k个球,根据"隔板法"由方案数C(k+n-1,n-1),:
ans=C(n-1,n-1)+C(n,n-1)+C(n+1,n-1)+……+C(n+m-2,n-1)+C(n+m-1,n-1)
     =C(n+m,n);(mod p)
由于数据范围很大,C(n,m)=C(n-1,m)+C(n-1,m-1);显然会TLE
最后组合数还要mod p,这时候 Lucas定理 闪亮登场
============================================
Lucas定理(shenben简单总结版)
============================================
Lucas定理1:
  Lucas(n,m,p)=cm(n%p,m%p)*Lucas(n/p,m/p,p);{其中cm(a,b)=C(a,b)%p;Lucas(x,0,p)=1;}
Lucas定理2:
  把n写成p进制a[n]a[n-1]……a[0];
  把m写成p进制b[n]b[n-1]……b[0];(不够位数的话,显然前面是 0)
则:C(a[n],b[n])*C(a[n-1],b[n-1])*……*C(a[0],b[0])
   =C(n,m) (mod p);
ps:Lucas最大的数据处理能力是p在10^5左右。

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=1e5+;
ll n,m,p,a[N]={};
ll fpow(ll a,ll b){
ll res=;
for(;b;b>>=,a=a*a%p) if(b&) res=res*a%p;
return res;
}
ll C(ll n,ll m){
if(m>n) return ;
return a[n]*fpow(a[m],p-)%p*fpow(a[n-m],p-)%p;
}
ll lucas(ll n,ll m){
if(!m) return ;
return C(n%p,m%p)*lucas(n/p,m/p)%p;
}
int main(){
int T;cin>>T;
while(T--){
cin>>n>>m>>p;
for(int i=;i<=p;i++) a[i]=a[i-]*i%p;
cout<<lucas(n+m,n)<<'\n';
}
return ;
}

HDU3037 附Lucas简单整理的更多相关文章

  1. .NET Web开发技术简单整理

    在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...

  2. 转载:.NET Web开发技术简单整理

    在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...

  3. MYBATIS 简单整理与回顾

    这两天简单整理了一下MyBatis 相关api和jar包这里提供一个下载地址,免得找了 链接:http://pan.baidu.com/s/1jIl1KaE 密码:d2yl A.简单搭建跑项目 2.进 ...

  4. 哪些CSS是可以被继承的--简单整理

    那些CSS是可以被继承的--简单整理1.文本相关属性是继承的:font-size,font-family,line-height,text-index等2.列表相关属性是继承的:list-style- ...

  5. .NET Web开发技术简单整理 转

    .NET Web开发技术简单整理 原文:http://www.cnblogs.com/SanMaoSpace/p/3157293.html 在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何 ...

  6. Web请求响应简单整理

      简单对Web请求响应如何处理进行的整理,难免有理解不到位,理解有偏差的地方,如有理解有误的地方,希望大牛批评指正. 1.Web开发的定义首先看看微软对Web开发的定义:Web开发是一个指代网页或网 ...

  7. 面试简单整理之JVM

    194.说一下 jvm 的主要组成部分?及其作用? JVM内存分为“堆”.“栈”和“方法区”三个区域,分别用于存储不同的数据. 堆内存用于存储使用new关键字所创建的对象: 栈内存用于存储程序运行时在 ...

  8. 《web前端设计基础——HTML5、CSS3、JavaScript》 张树明版 简答题简单整理

    web前端设计基础——HTML5.CSS3.JavaScript 简答题整理 第一章 (1)解释一下名词的含义:IP地址.URL.域名   iP定义了如何连入因特网,以及数据如何在主机间传输的标准. ...

  9. 简单整理React的Context API

    之前做项目时经常会遇到某个组件需要传递方法或者数据到其内部的某个子组件,中间跨越了甚至三四层组件,必须层层传递,一不小心哪层组件忘记传递下去了就不行.然而我们的项目其实并没有那么复杂,所以也没有使用r ...

随机推荐

  1. TestNG简单的学习-TestNG运行

    转载:http://topmanopensource.iteye.com/blog/1983735 TestNG简单的学习-TestNG运行 文档来自官方地址: http://testng.org/d ...

  2. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何查看错误代码

    一般模块出错会在Error中显示为TRUE,同时ErrorID中会有一个错误代码,下图所示就是出错之后的效果   在变量表中也可以看到,右击转成16进制查看   由于是NC模块出错,所以可以再NC的E ...

  3. Spine U3D整合流程问题

    Spine U3D整合流程问题 What: 公司2d项目开发,动画外包的spine.本来在spine里面一切正常,但是导入u3d运行库的时候动画切换的时候原来的动画是好的,一旦切换了就乱帧了. 如下结 ...

  4. Jersey 1.18 API文档

    Jersey 1.18 API文档 我自己制作了Jersey 1.18 API CHM文档, 下载地址见: http://download.csdn.net/detail/chszs/7334869 ...

  5. Log4j介绍,log4j.properties配置详解

    http://www.cnblogs.com/simle/archive/2011/09/29/2195341.html本文主要解释log4j的配置文件各个配置项的含义,内容是从网上转载的 1.Log ...

  6. Atitit..net clr il指令集 以及指令分类  与指令详细说明

    Atitit..net clr il指令集 以及指令分类  与指令详细说明 1.1. .NET CLR 和 Java VM 都是堆叠式虚拟机器(Stack-Based VM), 1 1.2. 查看工具 ...

  7. C语言基础(13)-函数

    一. 函数的原型和调用 在使用函数前必须定义或者声明函数. double circle(double r); int main() { ); printf("length = %f\n&qu ...

  8. ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例

    这篇文章主要介绍了ThinkPHP使用PHPExcel实现Excel数据导入导出,非常实用的功能,需要的朋友可以参考下 本文所述实例是使用在Thinkphp的开发框架上,要是使用在其他框架也是同样的方 ...

  9. symbol lookup error

    今天编译代码时出现这样的错误提示: “./test: symbol lookup error: ./test: undefined symbol: ……” 问题原因是:test使用的动态库和makef ...

  10. erlang 洗牌 shuffle

    很简单的一个场景:一副扑克(54张)的乱序洗牌 shuffle_list(List) -> [X || {_, X} <- lists:sort([{random:uniform(), N ...