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. [Functional Programming Monad] Map And Evaluate State With A Stateful Monad

    We explore our first stateful transaction, by devising a means to echo our state value into the resu ...

  2. Java8 对多个异步任务进行流水线操作(笔记)

    现在我们要对商店商品进行折扣服务.每个折扣代码对应不同的折扣率,使用一个枚举变量Discount.Code来实现这一想法,具体代码如下所示. 以枚举类型定义的折扣代码 /** * 折扣服务api * ...

  3. python——深刻理解Python中的元类(metaclass)

    译注:这是一篇在Stack overflow上 很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉 ...

  4. Subl 命令

    Subl 是sublime 的命令 添加环境变量后可以,在cmd 或者git 下直接 使用subl 进行 打开sublime Example:     Subl 打开编辑器     Subl . 将当 ...

  5. 微信小程序flex容器属性详解

    flex容器属性详解 flex-direction决定元素的排列方向 flex-wrap决定元素如何换行 flex-flow 是 flex-direction 和flex-wrap的简写 justif ...

  6. sql分组最大值相关

    房产表tf_estate_card,利润中心组profit_group_code,资产号main_assets_number,原值original_value 查出每个利润中心组的最大原值及其资产号 ...

  7. oci学习

    http://www.cnblogs.com/ychellboy/archive/2010/04/16/1713884.html oci官方文档 Call Interface Programmer's ...

  8. Autofc与Mvc,WebForm,Weiapi,Owin整合源码分析

    主要分析一下的几个项目: Autofac.Integration.Mvc Autofac.Integration.WebApi Autofac.Integration.Owin Autofac.Int ...

  9. GoogLeNet模型的微调

    我从零开始训练了GoogLeNet模型. 但它没有给我带来希望的结果. 作为替代,我想对我的数据集中的GoogLeNet模型进行微调. 有谁知道我应该遵循什么步骤? 采纳答案: 假设你正在尝试做图像分 ...

  10. Redis(四):常用数据类型和命令

    命令手册网址 http://doc.redisfans.com/ Redis数据类型 l String l Hash l List l Set l Sorted Set Redis中还有3种特殊的数据 ...