Saving Beans

Time Limit: 3000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3037
64-bit integer IO format: %I64d      Java class name: Main

 
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 解题:Lucas 求组合数取模
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL F[] = {};
void init(LL mod) {
for(int i = ; i <= mod; ++i)
F[i] = F[i-]*i%mod;
}
LL gcd(LL a,LL b,LL &x,LL &y) {
if(!b) {
x = ;
y = ;
return a;
}
LL ret = gcd(b,a%b,y,x);
y -= x*(a/b);
return ret;
}
LL Inv(LL b,LL mod) {
LL x,y,d = gcd(b,mod,x,y);
return d == ?(x%mod + mod)%mod:-;
}
LL inv(LL b,LL mod) {
if(b == ) return ;
return inv(mod%b,mod)*(mod-mod/b)%mod;
}
LL Lucas(LL n,LL m,LL mod) {
LL ret = ;
while(n && m) {
LL a = n%mod;
LL b = m%mod;
if(a < b) return ;
ret = ret*F[a]%mod*Inv(F[b]*F[a-b]%mod,mod)%mod;
n /= mod;
m /= mod;
}
return ret;
}
int main() {
int kase,n,m,mod;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%d",&n,&m,&mod);
init(mod);
printf("%I64d\n",Lucas(n+m,n,mod));
}
return ;
}

HDU 3073 Saving Beans的更多相关文章

  1. hdu 3037 Saving Beans(组合数学)

    hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...

  2. hdu 3037 Saving Beans Lucas定理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. hdu 3037 Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 3037——Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. Hdu 3037 Saving Beans(Lucus定理+乘法逆元)

    Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...

  6. HDU 3037 Saving Beans(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  7. HDU 3037 Saving Beans (Lucas法则)

    主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...

  8. HDU 3037 Saving Beans(Lucas定理的直接应用)

    解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...

  9. HDU 3037 Saving Beans (数论,Lucas定理)

    题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...

随机推荐

  1. H3C防火墙——回环流量问题(内网终端通过外网IP访问内部服务器)

    http://www.bubuko.com/infodetail-1533703.html

  2. spring boot不同环境读取不同配置

    具体做法: 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中:prod环境下的配置配置在application-prod.prope ...

  3. Scapy介绍官方文档翻译

    关于Scapy Scapy为何如此特别 高速的报文设计 一次探測多次解释 Scapy解码而不解释 高速展示Quick demo 合理的默认值 学习Python 本人英文水平有限,翻译不当之处,请參考官 ...

  4. [HTML5] Accessibility Implementation for complex component

    When you developing a complex component by your own, one thing you cannot ignore is Accessibility. C ...

  5. 再谈p2p投融资真相

    近来亲自调查眼下各类p2p.重度调查对象有:人人贷.陆金所.前金所.开鑫贷.礼德財富.招財宝. 投资的有几个小观念: 首先,大家投资都习惯性的细分政府背景和非政府背景.说句实话,这对一个投资人角度来讲 ...

  6. iOS-获取Model(设备型号)、Version(设备版本)、app(程序版本)等

    IOS-获取Model(设备型号).Version(设备版本).app(程序版本)等 NSLog(@"uniqueIdentifier: %@", [[UIDevice curre ...

  7. Android学习笔记之ProgressBar案例分析

    (1) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  8. Mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。

    将select出的结果再通过中间表select一遍,这样就规避了错误.注意,这个问题只出现于mysql,mssql和oracle不会出现此问题. mysql中You can't specify tar ...

  9. webapi+DataTables

    webapi + datatables 前言 之前写过一个关于DataTables的记录,是之前做webform的时候从后台一次性生成html代码,有很多弊端,就不多说了. 这次把最近研究的DataT ...

  10. Swagger 隐藏具体API

    一.why 在swagger ui界面中有时候不想显示某些api,通过下面的方式可以实现. 1.1.新建一个类实现IDocumentFilter接口 using Swashbuckle.Swagger ...