BZOJ4000 TJOI2015棋盘(状压dp+矩阵快速幂)

  显然每一行棋子的某种放法是否合法只与上一行有关,状压起来即可。然后n稍微有点大,矩阵快速幂即可。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
#define N 1000010
#define ul unsigned int
#define S 64
int n,m,p,k;
bool flag[3][6];
struct matrix
{
    int n;ul a[S][S];
    matrix operator *(const matrix&b) const
    {
        matrix c;c.n=n;memset(c.a,0,sizeof(c.a));
        for (int i=0;i<n;i++)
            for (int j=0;j<S;j++)
                for (int k=0;k<S;k++)
                c.a[i][j]+=a[i][k]*b.a[k][j];
        return c;
    }
}f,a;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj4000.in","r",stdin);
    freopen("bzoj4000.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read(),m=read(),p=read(),k=read();
    for (int i=0;i<3;i++)
        for (int j=0;j<p;j++)
        flag[i][j]=read();
    flag[1][k]=0;
    f.n=1;f.a[0][0]=1;
    a.n=1<<m;
    for (int i=0;i<(1<<m);i++)
        for (int j=0;j<(1<<m);j++)
        {
            a.a[i][j]=1;
            for (int v=0;v<m;v++)
            if (i&(1<<v))
                for (int x=max(0,v-k);x<min(m,v+p-k);x++)
                {    
                    if (flag[1][x-(v-k)]&&(i&(1<<x))) a.a[i][j]=0;
                    if (flag[2][x-(v-k)]&&(j&(1<<x))) a.a[i][j]=0;
                }
            for (int v=0;v<m;v++)
            if (j&(1<<v))
                for (int x=max(0,v-k);x<min(m,v+p-k);x++)
                {    
                    if (flag[1][x-(v-k)]&&(j&(1<<x))) a.a[i][j]=0;
                    if (flag[0][x-(v-k)]&&(i&(1<<x))) a.a[i][j]=0;
                }
        }
    for (;n;n>>=1,a=a*a) if (n&1) f=f*a;
    ul ans=0;
    for (int i=0;i<(1<<m);i++) ans+=f.a[0][i];
    cout<<ans;
    return 0;
}

 

posted @ 2018-10-20 23:31  Gloid  阅读(236)  评论(0编辑  收藏  举报