记录编号 370346 评测结果 AAAAAAAAAA
题目名称 [HZOI 2016]前缀和不小于零 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 0.174 s
提交时间 2017-02-13 16:14:12 内存使用 7.92 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2000010;
int Catalan(int,int);
int Lucas(int,int,int);
int C(int,int,int);
long long China(int);
long long mul(long long,long long,long long);
int qpow(int,int,int);
int n,k,f[maxn],m[5],a[5];
int main(){
	freopen("overzero.in","r",stdin);
	freopen("overzero.out","w",stdout);
	scanf("%d%d",&n,&k);
	for(int i=0;i<k;i++){
		scanf("%d",&m[i]);
		a[i]=Catalan(n,m[i]);
	}
	printf("%lld",China(k));
	return 0;
}
int Catalan(int n,int p){
	f[0]=1;
	for(int i=1;i<p;i++)f[i]=(long long)f[i-1]*i%p;
	return (long long)Lucas(n<<1,n,p)*qpow((n+1)%p,p-2,p)%p;
}
int Lucas(int n,int m,int p){
	if(!n&&!m)return 1;
	return (long long)Lucas(n/p,m/p,p)*C(n%p,m%p,p)%p;
}
int C(int n,int m,int p){
	if(n<m)return 0;
	return f[n]*(long long)qpow((long long)f[m]*f[n-m]%p,p-2,p)%p;
}
long long China(int n){
	long long M=1,ans=0;
	for(int i=0;i<n;i++)M*=m[i];
	for(int i=0;i<n;i++){
		long long Mi=M/m[i];
		ans=(ans+mul(mul(a[i],Mi,M),qpow(Mi%m[i],m[i]-2,m[i]),M))%M;
	}
	return ans;
}
long long mul(long long a,long long b,long long p){
	a%=p;b%=p;
	return ((a*b-(long long)((long double)a*b/p+1e-3)*p)%p+p)%p;
}
int qpow(int a,int b,int p){
	int ans=1;
	for(;b;b>>=1,a=(long long)a*a%p)if(b&1)ans=(long long)ans*a%p;
	return ans;
}
/*
10
2
37
10007
*/