题意极其有毒,注意给的行列都是从0开始的。
状压DP,f[i][S]表示第i行状态为S的方案数,枚举上一行的状态转移。$O(n2^{2m})$
使用矩阵加速,先构造矩阵a[S1][S2]表示上一行为S1是下一行是否能为S2,快速幂加速后得解。$O(2^{3m}m^2+2^{3m}log n)$
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 5 typedef unsigned int ul; 6 using namespace std; 7 8 const int N=70; 9 ul ans; 10 int n,m,p,k,ed,x,tot; 11 struct P{ int x,y; }d[110]; 12 13 struct Mat{ 14 ul a[N][N]; 15 ul* operator [](int x){ return a[x]; } 16 Mat (){ memset(a,0,sizeof(a)); } 17 }a,res; 18 19 Mat mul(Mat a,Mat b){ 20 Mat c; 21 rep(i,0,ed) rep(j,0,ed) rep(k,0,ed) c[i][k]+=a[i][j]*b[j][k]; 22 return c; 23 } 24 25 Mat ksm(Mat a,int b){ 26 Mat res; 27 rep(i,0,ed) res[i][i]=1; 28 for (; b; a=mul(a,a),b>>=1) 29 if (b & 1) res=mul(res,a); 30 return res; 31 } 32 33 int main(){ 34 freopen("bzoj4000.in","r",stdin); 35 freopen("bzoj4000.out","w",stdout); 36 scanf("%d%d%d%d",&n,&m,&p,&k); k++; ed=(1<<m)-1; 37 rep(i,1,3) rep(j,1,p){ 38 scanf("%d",&x); 39 if (x) d[++tot]=(P){i-2,j-k}; 40 } 41 rep(S1,0,ed) rep(S2,0,ed){ 42 bool flag=0; 43 rep(i,0,m-1) if (S1&(1<<i)) 44 rep(j,0,m-1) if (S2&(1<<j)) 45 rep(k,1,tot) if ((d[k].x==1 && d[k].y==j-i) || (d[k].x==-1 && d[k].y==i-j)) 46 { flag=1; break; } 47 rep(i,0,m-2) if (S2&(1<<i)) 48 rep(j,i+1,m-1) if (S2&(1<<j)) 49 rep(k,1,tot) if (d[k].x==0 && (d[k].y==j-i || d[k].y==i-j)) 50 { flag=1; break; } 51 a[S1][S2]=!flag; 52 } 53 res[0][0]=1; res=mul(res,ksm(a,n)); 54 rep(i,0,ed) ans+=res[0][i]; 55 printf("%u ",ans); 56 return 0; 57 }