矩乘优化即可
开内存小了1,蛋疼。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #define ll unsigned int using namespace std; struct yts { int x,y; ll a[(1<<6)+1][(1<<6)+1]; }a,b,c,ans; int A[10][10],B[3][10]; int n,m,p,k,size; void mul(yts &a,yts &b,yts &c) { memset(ans.a,0,sizeof(ans.a)); for (int i=1;i<=a.x;i++) for (int j=1;j<=b.y;j++) for (int k=1;k<=a.y;k++) ans.a[i][j]+=a.a[i][k]*b.a[k][j]; c.x=a.x;c.y=b.y; for (int i=1;i<=c.x;i++) for (int j=1;j<=c.y;j++) c.a[i][j]=ans.a[i][j]; } void power(yts &a,int y) { c.x=size;c.y=size; memset(c.a,0,sizeof(c.a)); for (int i=1;i<=size;i++) c.a[i][i]=1; while (y) { if (y&1) mul(c,a,c); mul(a,a,a); y>>=1; } } bool check(int x,int y) { memset(B,0,sizeof(B)); for (int i=1;i<=m;i++) if (x&(1<<(i-1))) B[1][i]=1; for (int i=1;i<=m;i++) if (y&(1<<(i-1))) B[2][i]=1; for (int i=1;i<=2;i++) for (int j=1;j<=m;j++) if (B[i][j]) for (int k=1;k<=A[2][0];k++) { if (j+A[2][k]<1 || j+A[2][k]>m) continue; if (B[i][j+A[2][k]]) return 0; } for (int j=1;j<=m;j++) if (B[1][j]) for (int k=1;k<=A[3][0];k++) { if (j+A[3][k]<1 || j+A[3][k]>m) continue; if (B[2][j+A[3][k]]) return 0; } for (int j=1;j<=m;j++) if (B[2][j]) for (int k=1;k<=A[1][0];k++) { if (j+A[1][k]<1 || j+A[1][k]>m) continue; if (B[1][j+A[1][k]]) return 0; } return 1; } int main() { scanf("%d%d",&n,&m); scanf("%d%d",&p,&k); k++; for (int i=1;i<=3;i++) for (int j=1;j<=p;j++) { int x; scanf("%d",&x); if (x && (i!=2 || j!=k)) A[i][++A[i][0]]=j-k; } size=1<<m; a.x=size;a.y=size; for (int i=0;i<size;i++) for (int j=0;j<size;j++) a.a[i+1][j+1]=check(i,j); power(a,n); b.x=1;b.y=size; b.a[1][1]=1; mul(b,c,b); ll ans=0; for (int i=1;i<=size;i++) ans+=b.a[1][i]; printf("%u\n",ans); return 0; }