比较简单的矩阵快速幂优化dp
#include <bits/stdc++.h>
#define gc getchar()
#define ll unsigned int
#define N 10
#define M 6
using namespace std;
int n,m,p,k;
ll a[3][N<<1],f[1<<M],Ans[1<<M],ans;
struct Matrix
{
ll a[1<<M][1<<M];
friend Matrix operator *(const Matrix &x,const Matrix &y)
{
Matrix ret;
for (int i=0;i<(1<<m);i++)
for (int j=0;j<(1<<m);j++)
{
ret.a[i][j]=0;
for (int k=0;k<(1<<m);k++) ret.a[i][j]+=x.a[i][k]*y.a[k][j];
}
return ret;
}
}A,B;
int read()
{
int x=1;
char ch;
while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
int s=ch-'0';
while (ch=gc,ch<='9'&&ch>='0') s=s*10+ch-'0';
return s*x;
}
Matrix ksm(Matrix A,int n)
{
Matrix ret;
for (int i=0;i<(1<<m);i++) ret.a[i][i]=1;
for (int x=n;x;x>>=1,A=A*A)
if (x&1) ret=ret*A;
return ret;
}
int main()
{
n=read(),m=read(),p=read(),k=read()+1;
for (int i=1;i<=3;i++)
for (int j=1;j<=p;j++) a[i][j]=read();
for (int i=0;i<(1<<m);i++)
for (int j=0;j<(1<<m);j++)
{
A.a[i][j]=1;
for (int l=1;l<=m;l++)
if (i&(1<<(l-1)))
for (int r=1;r<=m;r++)
if (l!=r&&i&(1<<(r-1)))
{
if (r-l+k>0&&a[2][r-l+k]) A.a[i][j]=0;
if (l-r+k>0&&a[2][l-r+k]) A.a[i][j]=0;
}
for (int l=1;l<=m;l++)
if (j&(1<<(l-1)))
for (int r=1;r<=m;r++)
if (l!=r&&j&(1<<(r-1)))
{
if (r-l+k>0&&a[2][r-l+k]) A.a[i][j]=0;
if (l-r+k>0&&a[2][l-r+k]) A.a[i][j]=0;
}
for (int l=1;l<=m;l++)
if (i&(1<<(l-1)))
for (int r=1;r<=m;r++)
if (j&(1<<(r-1)))
{
if (r-l+k>0&&a[3][r-l+k]) A.a[i][j]=0;
if (l-r+k>0&&a[1][l-r+k]) A.a[i][j]=0;
}
}
B=ksm(A,n);
for (int i=0;i<(1<<m);i++) ans+=B.a[0][i];
printf("%u\n",ans);
return 0;
}