sgu 223 状态压缩+方案数

由于每颗棋子只影响下一行状态,首先dfs出两行合法转移状态,再枚举状态和个数求解。


ACcode:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int LIM=1024;
const int NS=1000000;

int num[LIM];
LL dp[2][LIM][26];
int state[NS][2],top;

int getone(int x)
{
    int cnt=0;
    for (; x; cnt++,x&=(x-1));
    return cnt;
}

void dfs(int x,int y,int pos)
{
    if (pos<0) return ;
    if (pos==0)
    {
        state[top][0]=x;
        state[top++][1]=y;
        return ;
    }
    int a=x&1,b=y&1;
    dfs(x<<1,y<<1,pos-1);
    if (!(a|b))
    dfs(x<<1|1,y<<1,pos-1),
    dfs(x<<1,y<<1|1,pos-1);
}

void print(int x,int y)
{
    for (int i=0; i<y; i++,x>>=1)
        printf("%d",x&1);
    puts("");
}

int move(int x)
{
    return (x|(x<<1)|(x>>1));
}

void ttt(int n)
{
    int cnt=0;
    printf("top=%d\n",top);
    for (int i=0; i<top; i+=100)
    {
        printf("i=%d\n",i);
        print(state[i][0],n);
        print(state[i][1],n);
    }
    printf("cnt=%d\n",cnt);
}

void init(int x,int y,int z)
{
    for (int i=0;i<LIM;i++)
    for (int k=0;k<=z;k++)
    dp[x][i][k]=0;
}

int main()
{
    int n,k,p,q,r,s,t;
    for (int i=0; i<LIM; i++)
        num[i]=getone(i);
    while (~scanf("%d%d",&n,&k))
    {
        t=(n+1)>>1;
        if (t*t<k)
        {
            printf("0\n");
            continue;
        }
        s=top=0;
        dfs(0,0,n),init(0,n,k);
       // ttt(n);
        for (int i=0;i<top;i++)
        {
            p=state[i][0];
            dp[0][p][num[p]]=1;
        }
        for (int i=2;i<=n;i++)
        {
            s^=1,init(s,n,k);
            for (int j=0;j<top;j++)
            {
                p=state[j][0],q=state[j][1],t=num[q];
                for (int c=num[p];c<=(k-t);c++)
                dp[s][q][c+t]+=dp[1-s][p][c];
            }
        }
        LL res=0;
        for (int i=0;i<LIM;i++)
        res+=dp[s][i][k];
      //  cout<<res<<endl;
        printf("%I64d\n",res);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值