排列组合:构造RGB序列https://atcoder.jp/contests/abc266/tasks/abc266_g
输入 R,G,B,K,要求构造字符串(只由 R,G,B 构成),满足:R 出现 R 次,G 出现 G 次,B 出现 B 次,RG 出现 K 次。问可以构造出多少种这样的串。
首先考虑 G,B 的分配(不会影响到 RG),方案数为\(C_{G+B}^G\);
然后在排好的 GB 串中选 K 个 G,插入 R, 变成 RG,方案数为 \(C_G^K\);
最后还剩 R−K 个 R,放进去不能产生新的 RG,所以只能填在 B 前或 R 前,方案数为\(C_{R-K+B+K}^{R-K}=C_{R+B}^{R-K}\)
故答案为 \(C_{G+B}^G *C_G^K *C_{R+B}^{R-K}\)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int P = 998244353, N = 3e6 + 5;
int R, G, B, K;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int x = 0) : x(norm(x)) {}
Z(i64 x) : x(norm(x % P)) {}
int val() const {
return x;
}
Z operator-() const {
return Z(norm(P - x));
}
Z inv() const {
assert(x != 0);
return power(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = norm(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, Z &a) {
i64 v;
is >> v;
a = Z(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Z &a) {
return os << a.val();
}
};
Z fac[N], inv[N];
Z C(int x,int y){
return fac[x] * inv[y] * inv[x-y];
}
signed main () {
cin >> R >> G >> B >> K;
int N = R + G + B;
fac[0] = 1;
for (int i = 1; i <= N; i++) fac[i] = fac[i-1] * i;
inv[N] = fac[N].inv();
for (int i = N; i; i--) inv[i-1] = inv[i] * i;
Z ans = C(G+B, G) * C(G, K) * C(B+R, R-K);
cout << ans;
}