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. linux上MySQL改动password的各种方法,yc整理

    MySQL改动password的各种方法 整理了下面四种在MySQL中改动rootpassword的方法,可能对大家有所帮助! 方法1: 用SET PASSWORD命令 mysql -uroot my ...

  2. spring的jar包maven地址,统一下载很方便

    最近spring的官网改版了,想下个jar包,还得maven什么的,由于急于开发,懒得整那些个啦,在网上找了个spring的各版本的jar包地址,特此共享下: http://maven.springf ...

  3. android studio 警告 synchronization on non-final field

    测试代码如下: public class SyncNonFinalField { private Object object = new Object(); public void start() { ...

  4. FastGUI for NGUI教程

    原地址:http://blog.csdn.net/asd237241291/article/details/8499430 FastGUI是NGUI的一个扩展,所以必须要有NGUI才能使用.FastG ...

  5. 查看tomcat启动文件都干点啥---catalina.bat

    在上一次查看tomcat启动文件都干点啥一文中,我们总结出,startup.bat文件的作用就是找到catalina.bat文件,然后把参数传递给它,在startup.bat中,调用catalina. ...

  6. jQuery--百度百科

    JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Oper ...

  7. java 发送html邮件,苹果或者某些邮件客户端收到的内容为空白解决方案

    需要把网页标签中的双引号替换为  "  或者  \\\" 例如 <div id=\\\"container\\\" style=\\\"widt ...

  8. Python 实现的猫脸识别、人脸识别器。

    代码地址如下:http://www.demodashi.com/demo/13071.html 前言: OpenCV是开源的跨平台计算机视觉库,提供了Python等语言的接口,实现了图像处理和计算机视 ...

  9. svn your working copy appears to be locked run cleanup to amend the situation

    cleanup  则解决

  10. linux 设置tomcat快捷启动方式

    在linux下搭建好tomcat之后,每次启动和关闭都要去tomcat的bin目录下执行./startup.sh和./shutdown.sh 这是很不方便的,下面介绍如何像执行ls mv cp等命令一 ...