description
analysis
- 首先可以知道不符合要求的食材仅有一个,于是可以容斥拿总方案数减去选不合法食材的不合法方案数
- 枚举选取哪一个不合法食材,设\(f[i][j]\)表示到第\(i\)种烹饪方法、操作权值为\(j\)的方案数
- 给每一个操作赋权值,选当前行合法食材列为\(0\),不选当前行为\(1\),选当前行不合法食材列为\(2\)
- 转移是比较容易的,可知选当前列为不合法食材的方案数就是\(\sum_{i=n+1}^{2n}f[n][i]\)
code
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 105
#define MAXM 2005
#define ha 998244353
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)
using namespace std;
ll f[MAXN][MAXN*2];
ll a[MAXN][MAXM],sum[MAXM];
ll n,m,ans;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
int main()
{
n=read(),m=read(),ans=1;
fo(i,1,n)fo(j,1,m)(sum[i]+=(a[i][j]=read()))%=ha;
fo(i,1,n)(ans*=sum[i]+1)%=ha;--ans;
fo(food,1,m)
{
memset(f,0,sizeof(f)),f[0][0]=1;
fo(i,1,n)fo(j,0,n*2)
{
(f[i][j]+=f[i-1][j]*((sum[i]-a[i][food])%ha))%=ha;
if (j+1<=n*2)(f[i][j+1]+=f[i-1][j])%=ha;
if (j+2<=n*2)(f[i][j+2]+=f[i-1][j]*a[i][food]%ha)%=ha;
}
fo(i,n+1,n*2)ans=((ans-f[n][i])%ha+ha)%ha;
}
printf("%lld\n",ans);
return 0;
}
原文地址:https://www.cnblogs.com/horizonwd/p/12051499.html
时间: 2024-10-08 16:40:46