【题解】CF1081C:Colorful Bricks

原题传送门
k k 个颜色不同,就是在 n 1 n-1 个间隔中插入 k k 个板子,每一段内部颜色相同,方案数 C n 1 k C_{n-1}^k
枚举每个块的颜色,第一个块有 m m 中选择,之后都有 m 1 m-1 种选择
所以答案就是 C n 1 k m ( m 1 ) k C_{n-1}^k*m*(m-1)^{k}

Code:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL qy = 998244353;
int n, m, k;
LL fac[2010], inv[2010];

LL ksm(LL n, LL k){
	LL s = 1;
	for (; k; n = n * n % qy, k >>= 1) if (k & 1) s = s * n % qy;
	return s;
}

int main(){
	scanf("%d%d%d", &n, &m, &k);
//	if (m == 1 && k > 0) return puts("0"), 0;
//	if (m == 1) return puts("1"), 0;
//	if (n == 1 || k == 0) return printf("%d\n", m), 0;
	fac[0] = 1;
	for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % qy;
	inv[n] = ksm(fac[n], qy - 2);
	for (int i = n; i; --i) inv[i - 1] = inv[i] * i % qy;
	printf("%lld\n", fac[n - 1] * inv[n - 1 - k] % qy * inv[k] % qy * m % qy * ksm(m - 1, k) % qy);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ModestCoder_/article/details/108329774