CXXV.[Topcoder16346]TwoPerLine
跟一年半以前就刷过的经典老题[AHOI2009]中国象棋完全一致,道理非常simple,设 \(f_{i,j,k}\) 表示DP到第 \(i\) 列,其中有 \(j\) 行内恰有 \(2\) 枚棋,\(k\) 行里恰有 \(1\) 枚棋,然后就依据第 \(i\) 列里填的东西填到哪进行转移即可。复杂度 \(O(n^3)\)。
代码:
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
class TwoPerLine{
private:
int f[210][210][210];
void add(int &x,int y){x+=y;if(x>=mod)x-=mod;}
int sqr(int x){return (1ll*x*(x-1)/2)%mod;}
public:
int count(int n,int m){
f[0][0][0]=1;
for(int i=0;i<n;i++)for(int j=0;j<=n;j++)for(int k=0;j+k<=n;k++)if(j*2+k<=m){
add(f[i+1][j][k],f[i][j][k]);//put nothing on this colomn
if(k)add(f[i+1][j+1][k-1],1ll*f[i][j][k]*k%mod);//put one chess on an 1-row
if(k>=2)add(f[i+1][j+2][k-2],1ll*f[i][j][k]*sqr(k)%mod);//put both chesses on 1-rows
if(j+k+1<=n)add(f[i+1][j][k+1],1ll*f[i][j][k]*(n-j-k)%mod);//put one chess on a 0-row
if(j+k+2<=n)add(f[i+1][j][k+2],1ll*f[i][j][k]*sqr(n-j-k)%mod);//put both chesses on 0-rows
if(k&&j+k+1<=n)add(f[i+1][j+1][k],1ll*f[i][j][k]*k%mod*(n-j-k)%mod);//put one chess on an 1-row, the other on a 0-row
}
int res=0;
for(int j=0;j<=n;j++)for(int k=0;j+k<=n;k++)if(j*2+k==m)add(res,f[n][j][k]);
return res;
}
}my;
/*
int main(){
// printf("%d\n",my.count(3,6));
// printf("%d\n",my.count(7,0));
// printf("%d\n",my.count(100,3));
// printf("%d\n",my.count(6,4));
return 0;
}
*/