[AGC025B] RGB Coloring

前言

我的思维已经退化到了一个前所未有的境地了,而你们就想趁机把我击垮。——木示木干

题目

洛谷

AtCoder

讲解

有一个之前我从未见过的很牛逼的思路(充分体现了我是一个蒟蒻的事实)。

将互相牵制的条件变为独立的条件。

比如这道题,本来红色和蓝色是互相牵制的,但是如果我们看成它们相互独立,而如果涂到同一个格子就代表涂成绿色,其实和原题是等价的。

既然这样这道题就做完了,枚举红色格子个数,计算蓝色格子个数,算一下方案数即可。

代码

戳我
//12252024832524
#include <cstdio>
#include <cstring>
#include <algorithm>
#define TT template<typename T>
using namespace std; 

typedef long long LL;
const int MAXN = 300005;
const int MOD = 998244353;
LL n,A,B,K,ans;

LL Read()
{
	LL x = 0,f = 1;char c = getchar();
	while(c > '9' || c < '0'){if(c == '-')f = -1;c = getchar();}
	while(c >= '0' && c <= '9'){x = (x*10) + (c^48);c = getchar();}
	return x * f;
}
TT void Put1(T x)
{
	if(x > 9) Put1(x/10);
	putchar(x%10^48);
}
TT void Put(T x,char c = -1)
{
	if(x < 0) putchar('-'),x = -x;
	Put1(x); if(c >= 0) putchar(c);
}
TT T Max(T x,T y){return x > y ? x : y;}
TT T Min(T x,T y){return x < y ? x : y;}
TT T Abs(T x){return x < 0 ? -x : x;}

int fac[MAXN],ifac[MAXN];
int qpow(int x,int y) 
{
	int ret = 1;
	while(y){if(y & 1) ret = 1ll * ret * x % MOD;x = 1ll * x * x % MOD;y >>= 1;}
	return ret;
}
void init(int x) 
{
	ifac[0] = fac[0] = 1;
	for(int i = 1;i <= x;++ i) fac[i] = 1ll * fac[i-1] * i % MOD;
	ifac[x] = qpow(fac[x],MOD-2);
	for(int i = x-1;i >= 1;-- i) ifac[i] = 1ll * ifac[i+1] * (i+1) % MOD;
} 
LL C(int x,int y)
{
	if(x < y || y < 0) return 0;
	return 1ll * fac[x] * ifac[y] % MOD * ifac[x-y] % MOD;
}

int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	n = Read(); A = Read(); B = Read(); K = Read();
	init(n);
	for(int i = 0;i <= n;++ i)
	{
		LL les = K - A * i;
		if(les % B || les / B > n) continue;
		ans = (ans + C(n,i) * C(n,les/B)) % MOD;
	}
	Put(ans);
	return 0;
}

后记

不过有一说一,这个思路我确实不怎么会想到,所以记录一下。

原文地址:https://www.cnblogs.com/PPLPPL/p/15073887.html