J - Saving Beans (HDU - 3037)

- 题目大意

求在n棵树上摘不超过m颗豆子的方案,结果对p取模。

- 解题思路

利用插板法,再套用Lucas定理的模板解决即可。

- 代码

#include<iostream>
using namespace std;
const long long MAX = 2000000;
long long num[MAX];

void zh(long long mod)
{
	num[0] = 1;
	for (long long i = 1; i <mod; i++)
		num[i] = num[i - 1] * i%mod;
}
long long powMod(long long a, long long n, long long mod) {
	long long ans = 1;
	for (; n > 0; n >>= 1)
	{
		if (n & 1)
			ans = ans * a%mod;
		a = a * a%mod;
	}
	return ans;
}
long long Lucas(long long n, long long m, long long p) {
	long long ans = 1;
	while (n&&m)
	{
		if (n%p<m % p)
			return 0;
		ans = (ans*num[n % p] * powMod(num[m % p] * num[n % p - m % p] % p, p - 2, p)) % p;
		n /= p; m /= p;
	}
	return ans;
}
int main()
{
	int t;
	cin >> t;
	long long n, m, p;
	while (t--)
	{
		long long sum = 0;
		cin >> n >> m >> p;
		zh(p);
		sum = Lucas(m + n, n, p);
		cout << sum << endl;
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/alpacadh/p/8448381.html

时间: 2024-11-13 23:34:40

J - Saving Beans (HDU - 3037)的相关文章

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(组合数学)

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定理)

题目大意:豆子数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 组合数取模模板题。。

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2707    Accepted Submission(s): 1014 Problem Description Although winter is far away, squirrels have to work day and night to save b

[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

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2578    Accepted Submission(s): 956 Problem Description Although winter is far away, squirrels have to work day and night to save be

HDU 3073 Saving Beans

Saving Beans Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 303764-bit integer IO format: %I64d      Java class name: Main Although winter is far away, squirrels have to work day and night to save beans. The

HDOJ 3037 Saving Beans

假设有n+1个树,第n+1个树埋不足m的种子,隔板法C[n+m][m] 大组合数取mod用Lucas定理: Lucas(n,m,p) = C[n%p][m%p] × Lucas(n/p,m/p,p) ; Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2314    Accepted Submission(s)

[HDU 3461] Saving Beans &amp; 组合计数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