acwing 291

本文解析了如何使用位操作和动态规划解决一个涉及状态转移和计数的问题。通过定义状态和状态转移规则,利用f数组记录中间状态,最终计算出特定阶段的目标结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define _zero(a) memset(a, 0, sizeof(a))
#define _far(a) memset(a, 0x3f,sizeof (a))
#define __far(a) memset(a, -0x3f,sizeof (a))
#define far 0x3f3f3f3f
#define endl '\n'
#define int long long
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define rep(a,b,c) for(int (a) = (b);(a)<=(c);(a)++)
#define per(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
#define sqr(a) (a)*(a)
#define VI vector<int>
#define VII vector<vector<int>>
#define pb push_back
#define PII pair<int,int>
typedef long long ll;
typedef double dd;
typedef long double ld;
using namespace std;

const int inf = 1e10;
const int mod = 998244353;
const int maxn = 2e5 + 1000;
const ld pi = atan2(0, -1);//arctan(y/x);
const ld eps = 1e-4;
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int N = 12, M = 1 << N;
int f[N][M];
VI state[M];
bool st[M];
int n,m;

signed main()
{
    IOS;
    while (cin >> n >> m, n || m)
    {
        for (int i = 0; i < 1 << n; i ++)
        {
            int cnt = 0;
            bool use = true;
            for (int j = 0; j < n; j ++)
            {
                if (i >> j & 1)
                {
                    if (cnt % 2) use = false;
                    cnt = 0;
                }
                else cnt ++;
            }
            if (cnt % 2) use = false;
            st[i] = use;
        }

        for (int i = 0; i < 1 << n; i ++)
        {
            //if (!st[i]) continue;
            state[i].clear();
            for (int j = 0; j < 1 << n; j ++)
            {
                //if (!st[j]) continue;
                if ((i&j) == 0 && st[i | j]) state[i].pb(j);
            }
        }
        _zero(f);
        f[0][0] = 1;
        rep(i,1,m)
            for (int j = 0; j < 1 << n; j ++)
                for (int k : state[j])
                    f[i][j] += f[i-1][k];
        cout << f[m][0] << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值