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

解题思路:

直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题。

#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <cstdio>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#define LL long long
#define FOR(i, x, y) for(int i=x;i<=y;i++)
using namespace std;
const int maxn = 100000 + 10;
LL Pow_mod(LL a, LL b, LL mod)
{
	LL ret = 1;
	while(b)
	{
		if(b & 1) ret = ret * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ret;
}
LL fac[maxn];
void init(LL p)
{
	fac[0] = 1;
	FOR(i, 1, p) fac[i] = (fac[i-1] * i) % p;
}
LL Lucas(LL n, LL m, LL p)
{
	LL ret = 1;
	while(n && m)
	{
		LL a = n % p, b = m % p;
		if(a < b) return 0;
		ret = (ret * fac[a] * Pow_mod(fac[b] * fac[a-b] % p , p - 2 , p)) % p;
		n /= p, m /= p;
	}
	return ret;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n, m, p;
		scanf("%d%d%d", &n, &m, &p);
		init(p);
		printf("%d\n", (int)Lucas(n + m, m, p));
	}
	return 0;
}
时间: 2024-08-04 02:19:41

HDU 3037 Saving Beans(Lucas定理的直接应用)的相关文章

[ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)

Saving Beans 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 probl

HDU 3037 Saving Beans (Lucas定理)

/*求在n棵树上摘不超过m颗豆子的方案,结果对p取模. 求C(n+m,m)%p. 因为n,m很大,这里可以直接套用Lucas定理的模板即可. Lucas(n,m,p)=C(n%p,m%p,p)*Lucas(n/p,m/p,p): ///这里可以采用对n分段递归求解, Lucas(x,0,p)=1: 将n,m分解变小之后问题又转换成了求C(a/b)%p. 而C(a,b) =a! / ( b! * (a-b)! ) mod p 其实就是求 ( a! / (a-b)!) * ( b! )^(p-2)

HDOJ 3037 Saving Beans (Lucas定理)

题意 给出n个树和m个种子,求把这m个种子放到n棵树中有多少中方法,可以选择不放. 思路 因为有可以不放的选择,所以我们可以看成是多加了n棵空树,所以答案就是C(mn+m) 因为n和m都很大,p比较小,所以直接用lucas就可以了. 代码 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #includ

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 3037 Saving Beans 多重集合的结合 lucas定理

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3037 题目描述: 要求求x1 + x2 + x3 + ...... + xn <= m 非负整数解的个数, 结果对P取模, 输入的变量是n, m, p, P一定是素数 解题思路: x1 + ... + xn = m 非负整数解的个数是C(n+m-1, n) , 所以答案就是 C(n+0-1, 0) + C(n+1-1, 1) + ...... C(n+m-1, n) 对P取模, 由于组合数公式C(

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) =