# [组合数学] Codeforces 1608D Dominoes

## Code

#include <bits/stdc++.h>
using namespace std;

#define LL long long
const LL MOD = 998244353;
LL inv[200010], fact[200010], finv[200010];
string s[100010];
map<string, int> mp;
int cnt[2];
int n;

void Init() {
inv[1] = fact[0] = fact[1] = finv[0] = finv[1] = 1;
for (int i = 2;i <= 200000;++i) {
inv[i] = ((-(MOD / i) * inv[MOD % i]) % MOD + MOD) % MOD;
fact[i] = fact[i - 1] * i % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}

LL qpow(LL b, LL n) {
LL x = 1, Power = b % MOD;
while (n) {
if (n & 1) x = x * Power % MOD;
Power = Power * Power % MOD;
n >>= 1;
}
return x;
}

int main() {
Init();
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1;i <= n;++i) { cin >> s[i]; ++mp[s[i]]; }
for (int i = 1;i <= n;++i) {
if (s[i][0] == 'W') ++cnt[0];
else if (s[i][0] == 'B') ++cnt[1];
if (s[i][1] == 'W') ++cnt[0];
else if (s[i][1] == 'B') ++cnt[1];
}
if (cnt[0] > n || cnt[1] > n) { cout << 0 << endl; return 0; }
LL ans = fact[n * 2 - cnt[0] - cnt[1]] * finv[n - cnt[0]] % MOD * finv[n - cnt[1]] % MOD;
if (mp.count("BB") || mp.count("WW")) { cout << ans << endl; return 0; }
ans = ((ans - qpow(2, mp["??"])) % MOD + MOD) % MOD;
if (!mp.count("BW") && !mp.count("B?") && !mp.count("?W")) ++ans;
if (!mp.count("WB") && !mp.count("W?") && !mp.count("?B")) ++ans;
ans %= MOD;
cout << ans << endl;

return 0;
}

posted @ 2022-01-05 15:12  AE酱  阅读(59)  评论(0编辑  收藏  举报