CF EDU 107 E - Colorings and Dominoes
E - Colorings and Dominoes
DP + 算贡献
首先观察到每一行的贡献和每一列的贡献是独立的,所以可以单独算出每一行和每一列的贡献之和
对某一行考虑
设 \(f[i][0]\) 为第 \(i\) 个元素可作为放骨牌的第一个位置的概率,\(f[i][1]\) 为第 \(i\) 个元素可作为放骨牌的第二个位置的概率
转移:
\(f[i][0]=\frac12*(1-f[i-1][0])\)
\(f[i][1] = \frac12*f[i-1][0]\)
\(*\frac12\) 为只有一半的概率是该颜色
答案为 \(f[i][1]\) 之和 * 总方案数(\(2^{白色的方格数}\))
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 3e5 + 10;
ll f[N][2];
int n, m, cnt;
ll qmi(ll a, ll b)
{
ll ans = 1;
while(b)
{
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans % mod;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
vector<vector<char> > s(n + 10, vector<char> (m + 10));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> s[i][j];
if (s[i][j] == 'o')
cnt++;
}
}
ll ans = 0;
ll tot = qmi(2, cnt), inv2 = qmi(2, mod - 2);
for (int i = 1; i <= n; i++)
{
f[0][0] = 0;
ll now = 0;
for (int j = 1; j <= m; j++)
{
if (s[i][j] == '*')
{
f[j][0] = f[j][1] = 0;
continue;
}
f[j][0] = inv2 * (1 - f[j-1][0] + mod) % mod;
f[j][1] = inv2 * f[j-1][0] % mod;
now = (now + f[j][1]) % mod;
}
ans = (ans + tot * now % mod) % mod;
}
for (int j = 1; j <= m; j++)
{
f[0][0] = 0;
ll now = 0;
for (int i = 1; i <= n; i++)
{
if (s[i][j] == '*')
{
f[i][0] = f[i][1] = 0;
continue;
}
f[i][0] = inv2 * (1 - f[i-1][0] + mod) % mod;
f[i][1] = inv2 * f[i-1][0] % mod;
now = (now + f[i][1]) % mod;
}
ans = (ans + tot * now % mod) % mod;
}
cout << ans << endl;
return 0;
}