昂贵的珍珠垂饰题解

Description
Solution
【T2】一道坑爹的只有一个测试点的题目。

经过不断地讨论,我们终于搞懂了。
先不考虑不合理的方案,那么对于某一个n,它的方案最多就是 ,那么运用容斥原理的话,对于这一个n,它的答案就是
在这里插入图片描述
在这里插入图片描述
最后我们把每一项提出来,就可以发现这是一个等比数列。
在提一下求等比数列之和的方法:
设和为s,公比为q。
那么它们的差就是:
在这里插入图片描述

唉,终于把这道题调出来了,又是因为答案没有加上模数,一定要记得,一定要记得!

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
int T,n,m;
ll fact[31],ans;
const ll mo=1234567891;
ll power(ll a,ll b) {
	ll result=1;
	while(b) {
		if(b&1)result=result*a%mo;
		b>>=1;
		a=a*a%mo;
	}
	return result;
}
ll C(ll x,ll y) {
	if(x==0)return 1;
	return fact[y]*power(fact[x]*fact[y-x]%mo,mo-2)%mo;
}
int main() {
	scanf("%d",&T);
	fact[1]=1;
	for(int i=2;i<=30;i++)
		fact[i]=fact[i-1]*i%mo;
	while(T--) {
		scanf("%d%d",&n,&m);
		if(m==1)ans=n;
		else {
			ans=0;
			for(int i=0;i<m;i++) {
				if(i%2==0) {
					if(m-i>1)ans=(ans+(C(i,m)*power(m-i,n+1)%mo-C(i,m)*power(m-i,1)%mo)%mo*power(m-i-1,mo-2))%mo;
					else ans=(ans+C(i,m)*n%mo)%mo;
				}
				else  {
					if(m-i>1)ans=(ans-(C(i,m)*power(m-i,n+1)%mo-C(i,m)*power(m-i,1)%mo)%mo*power(m-i-1,mo-2))%mo;
					else ans=(ans-C(i,m)*n%mo)%mo;
				}
			}
		}
		printf("%lld\n",(ans+mo)%mo);
	}
}

猜你喜欢

转载自blog.csdn.net/MZHjr/article/details/107472979
今日推荐