[AGC025B]RGB Coloring

[AGC025B]RGB Coloring

题目大意:

\(n(n\le3\times10^5)\)个格子,每个格子可以选择涂成红色、蓝色、绿色或不涂色,三种颜色分别产生\(a,b,a+b(a,b\le3\times10^5)\)的收益。问有多少种涂色方案使得总收益为\(k(k\le18\times10^{10})\)

思路:

涂绿色就相当于同时涂了红色和蓝色,因此枚举红色出现次数\(i\)和蓝色出现次数\(j\)。答案就是\(\displaystyle\sum_{\substack{0\le i,j\le n\\ai+bj\le k}}{n\choose i}{n\choose j}\)

时间复杂度\(\mathcal O(n)\)

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
typedef long long int64;
inline int64 getint() {
	register char ch;
	while(!isdigit(ch=getchar()));
	register int64 x=ch^'0';
	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
	return x;
}
const int N=3e5+1,mod=998244353;
int fac[N],ifac[N];
void exgcd(const int &a,const int &b,int &x,int &y) {
	if(!b) {
		x=1,y=0;
		return;
	}
	exgcd(b,a%b,y,x);
	y-=a/b*x;
}
inline int inv(const int &x) {
	int ret,tmp;
	exgcd(x,mod,ret,tmp);
	return (ret%mod+mod)%mod;
}
inline int power(int a,int k) {
	int ret=1;
	for(;k;k>>=1) {
		if(k&1) ret=(int64)ret*a%mod;
		a=(int64)a*a%mod;
	}
	return ret;
}
inline int C(const int &n,const int &m) {
	return (int64)fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
int main() {
	int n=getint(),a=getint(),b=getint();
	int64 k=getint();
	for(register int i=fac[0]=1;i<=n;i++) {
		fac[i]=(int64)fac[i-1]*i%mod;
	}
	ifac[n]=inv(fac[n]);
	for(register int i=n;i>=1;i--) {
		ifac[i-1]=(int64)ifac[i]*i%mod;
	}
	int ans=0;
	for(register int i=0;i<=n&&(int64)a*i<=k;i++) {
		if((k-(int64)a*i)%b!=0) continue;
		const int64 j=(k-(int64)a*i)/b;
		if(j>n) continue;
		(ans+=(int64)C(n,i)*C(n,j)%mod)%=mod;
	}
	printf("%d\n",ans);
	return 0;
}
posted @ 2018-10-02 20:55  skylee03  阅读(285)  评论(0编辑  收藏  举报