[HDU 3461] Saving Beans & 组合计数Lucas定理模板

Saving Beans

Time Limit: 6000/3000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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

2009 Multi-University Training Contest 13 - Host by HIT

【题解】

经典的组合计数,求C(n+m,m) mod p

有个著名的定理 Lucas定理:C(n,m) mod p = C(n/p,m/p)*C(n%p,m%p)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 long long f[100005];
 4 inline void getfact(long long p) {
 5     f[0]=1;
 6     for (int i=1;i<=p;++i)
 7         f[i]=(f[i-1]*i)%p;
 8 }
 9 inline long long powx(long long a,long long b,long long p) {
10     long long ret=1;
11     while(b) {
12         if (b&1) ret=(ret*a)%p;
13         a=(a*a)%p;
14         b>>=1;
15     }
16     return ret;
17 }
18 inline long long lucas(long long n,long long m,long long p) {
19     long long ret=1;
20     while(n&&m) {
21         long long a=n%p,b=m%p;
22         if(a<b) return 0;
23         ret=(ret*f[a]*powx((f[b]*f[a-b])%p,p-2,p))%p;
24         n/=p;m/=p;
25     }
26     return ret;
27 }
28 int main() {
29     int t;
30     scanf("%d",&t);
31     while(t--) {
32         long long n,m,p;
33         scanf("%I64d%I64d%I64d",&n,&m,&p);
34         getfact(p);
35         printf("%I64d\n",lucas(n+m,m,p));
36     }
37     return 0;
38 }

时间: 2024-08-01 06:31:18

[HDU 3461] Saving Beans & 组合计数Lucas定理模板的相关文章

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

题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后就求这个值,直接求肯定不好求,所以我们可以运用Lucas定理,来分解这个组合数,也就是Lucas(n,m,p)=C(n%p,m%p)* Lucas(n/p,m/p,p). 然后再根据费马小定理就能做了. 代码如下: 第一种: #pragma comment(linker, "/STACK:10240

HDU 3037 Saving Beans (隔板法+Lucas定理)

<题目链接> 题目大意:用$n$颗树保存不超过$m$颗豆子($n,m\leq10^9$)(不一定所有的树都有豆子保存),问你总共有多少种情况.答案对p取模(p保证是个素数). 解题分析:可以转化成 将$n$个相同的球放入$m$个集合中,有的集合中的球数可能为0的等价问题.很明显这可以用隔板法解决,答案为$C(n+m-1,m-1)$则题目解的个数可以转换成求: $C(n+m-1,0)+C(n+m-1,1)+C(n+m-1,2)+……+C(n+m-1,m-1)$ 利用组合数公式 $C(n,k) =

HDU 3037 Saving Beans 大组合数 lucas定理

直接lucas降到10w以内搞组合数 #include <cstdio> #include <cstring> typedef __int64 LL; LL f[110010]; LL pow(LL a, LL b, LL c) { LL ans = 1; while(b) { if(b&1) ans = (ans*a) % c; b >>= 1; a = (a*a) % c; } return ans; } LL cm(LL n, LL m, LL p) {

Saving Beans 组合数学之 Lucas定理

Saving Beans 题目抽象:有n颗水果树,每科树上有无穷多个水果(同一棵树上的水果相同).现在要从这n棵树上取不超过m个水果,有多少种取法. ps:S={n1*a1,n2*a2,n3*a3,……,nn*an}.若m<ni(i=1,2,...n)   则s的m组合=T={m*1,(n-1)*0} =  (m+n-1)!/(m!)/(n!)=c(m+n-1,m); 思路:利用一一对应的思想,再增加一棵树.从n+1棵树上取m个水果的方案数. ans=T={m*1,n*0}=(m+n)!/m!/

HDU 3037 Saving Beans (Lucas定理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p, 用Lucas定理求大组合数取模的值 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int t; long long n, m, p; long long pow(long long n, long lo

HDU 3037 Saving Beans(lucas定理)

题目大意:豆子数i (1~m)分到n颗树上.  树可以为空,那么对于每个i,分配方式是 C(n+i-1,n-1)......于是我用for(i=0-->m)做,不幸超时,m太大. 不过竟然公式可以化简: for(int i=0;i<=m;i++) C(n+i-1,n-1)=C(n+i-1,i) 组合原理: 公式 C(n,k) = C(n-1,k)+C(n-1,k-1) C(n-1,0)+C(n,1)+...+C(n+m-1,m) = C(n,0)+C(n,1)+C(n+1,2)+...+C(n

hdu 3037 Saving Beans(组合数学)

hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以用到Lucas定理. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; ll n, m, p; ll qPow (ll a

HDU 4349 Xiao Ming&#39;s Hope lucas定理

Xiao Ming's Hope Time Limit:1000MS     Memory Limit:32768KB Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2

hdu 4349 Xiao Ming&#39;s Hope (Lucas定理推导)

题意:求C (n,0),C (n,1),C (n,2)...C (n,n).奇数的个数 思路:我们分析C(n,m)%2,那么由Lucas定理可知,n和m可以写成二进制的形式,假设n=1001101,那么m是0~1001101,我们知道C(0,1)=0,因此如果n=1001101的0对应位置的m二进制位为1那么C(n,m) % 2==0,因此m对应n为0的位置只能填0,而1的位置填0,填1都是1(C(1,0)=C(1,1)=1),不影响结果为奇数,并且保证不会出n的范围,因此所有的情况即是n中1位