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. nodejs-app.js

    设置静态目录 1 2 app.use(express.static(path.join(__dirname, 'public'))); //设置模版渲染的js,css,images的静态文件目录 设置 ...

  2. [using_microsoft_infopath_2010]Chapter13 SharePoint视图和控制板

    本章概要: 1.规划站点的外观,使用View和web部件 2.为登录进来的用户展示相关的表单. 3.为管理员创建视图 4.从表单中收集到的数据中创建报表

  3. http格式(graph)

    http请求格式 http请求头 字段 http响应 http响应头字段

  4. USACO 2.1 Sorting a Three-Valued Sequence

    Sorting a Three-Valued Sequence IOI'96 - Day 2 Sorting is one of the most frequently performed compu ...

  5. USACO 1.5 Number Triangles

    Number Triangles Consider the number triangle shown below. Write a program that calculates the highe ...

  6. Visual Studio2013下Magick++配置方法

    声明:本文系作者原创,如需转载请保持文章完整并注明出处(http://blog.csdn.net/u010281174/article/details/52224829). ImageMagick是一 ...

  7. WEBSERVICE之JDK开发webservice

    转自:https://www.cnblogs.com/w-essay/p/7357262.html 一.开发工具与环境 1. jdk1.6版本以上(jdk1.6.0_21及以上版本) 2 .eclip ...

  8. 11. Container With Most Water[M]盛最多水的容器

    题目 Given \(n\) non-negative integers \(a_1,a_2,\cdots,a_n\), where each represents a point at coordi ...

  9. 2. Add Two Numbers[M]两数相加

    题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  10. ffmpeg键盘命令响应程序详解

    一.对终端进行读写 当一个程序在命令提示符中被调用时, shell负责将标准输入和标准输出流连接到你的程序, 实现程序与用户间的交互.   1. 标准模式和非标准模式 在默认情况下, 只有用户按下回车 ...