BZOJ 1801 中国象棋(DP)
如果考虑每一行的状态的话,是会有后效性的,解决办法就是改变状态的表示。
注意到每一行只能放0个,1个,或2个炮。那么可以依据这个转移来定义状态。
定义dp[i][j][k]表示前i行有j列0个炮,k列1个炮,m-j-k列两个炮的方法数。
则转移方程是显然的。我们剩下的只需要记忆化搜索一下即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi 3.1415926535 # define eps 1e-9 # define MOD 9999973 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+(ch-'0'); return flag?-res:res; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=105; //Code begin... int dp[N][N][N], n, m; int dfs(int x, int a, int b){ if (~dp[x][a][b]) return dp[x][a][b]; if (a+b>m||a<0||b<0) return 0; if (x==1) { if (a+b!=m||b>2) return 0; if (b==1) return dp[x][a][b]=m; if (b==2) return dp[x][a][b]=m*(m-1)/2; return dp[x][a][b]=1; } int res=(dfs(x-1,a,b)+(a+1)*dfs(x-1,a+1,b-1)%MOD+(b+1)*dfs(x-1,a,b+1)%MOD)%MOD; res=(res+(LL)(a+1)*b*dfs(x-1,a+1,b)%MOD+(LL)(a+2)*(a+1)/2*dfs(x-1,a+2,b-2)%MOD+(LL)(b+2)*(b+1)/2*dfs(x-1,a,b+2)%MOD)%MOD; return dp[x][a][b]=res; } int main () { int ans=0; mem(dp,-1); scanf("%d%d",&n,&m); FOR(i,0,m) FOR(j,0,m-i) ans=(ans+dfs(n,i,j))%MOD; printf("%d\n",ans); return 0; }