# P5664 [CSP-S2019] Emiya 家今天的饭

## Solution

$dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k] \cdot a[i][col] + dp[i - 1][j][k - 1] \cdot (s[i] - a[i][col])$

$dp[i][x] = dp[i - 1][x] + dp[i - 1][x - 1] \cdot a[i][col] + dp[i - 1][x + 1] \cdot (s[i] - a[i][col])$

#include <bits/stdc++.h>
using namespace std;
# define int long long
const int N = 105,M = 2005;
const int mod = 998244353;
int n,m;
int f[N][N],dp[N][N << 1];
int a[N][M],s[N];
int Hash(int x)
{
return x + n + 1;
}
signed main(void)
{
scanf("%lld%lld",&n,&m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%lld",&a[i][j]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) s[i] = (s[i] + a[i][j]) % mod;
f[0][0] = 1;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= i; j++) f[i][j] = (f[i - 1][j] + (f[i - 1][j - 1] * (s[i] % mod))) % mod;
}
dp[0][Hash(0)] = 1;
long long ans1 = 0,ans2 = 0;
for(int col = 1; col <= m; col++)
{
memset(dp,0,sizeof(dp));
dp[0][Hash(0)] = 1ll;
for(int i = 1; i <= n; i++)
{
for(int j = Hash(-i); j <= Hash(i); j++)
{
dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1] * (a[i][col] % mod) + dp[i - 1][j + 1] * ((s[i] - a[i][col] + mod) % mod)) % mod;
}
}
for(int i = Hash(1); i <= Hash(n); i++) ans1 = (ans1 + dp[n][i]) % mod;
}
for(int i = 1; i <= n; i++) ans2 = (ans2 + f[n][i]) % mod;
printf("%lld\n",(ans2 - ans1 + mod) % mod);

return 0;
}

posted @ 2021-07-29 10:30  luyiming123  阅读(38)  评论(0编辑  收藏  举报