首页 > 其他分享 >ACwing1064. 小国王

ACwing1064. 小国王

时间:2024-04-01 19:22:58浏览次数:21  
标签:ch int LegalStates long ACwing1064 include Transformation 国王

线性状压DP

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#define R(x) x = read()
#define For(i, j, n) for (int i = j; i <= n; ++i)
using namespace std;

const int N = 15, K = 105, M = 1 << 10;
typedef long long LL;

int n, k;
vector<int> LegalStates;
vector<int> Transformation[M];

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

inline int lowbit(int x)
{
    return x & (-x);
}

int cnt[M];

bool check(int a, int b)
{
    if (a & b)
        return 1;
    int t = a | b;
    if (t & (t >> 1))
        return 1;
    return 0;
}

void init()
{
    for (int i = 0; i < (1 << n); i++)
    {
        if (i & (i >> 1))
            continue;
        LegalStates.push_back(i);
        int t = i;
        while (t)
        {
            cnt[i]++;
            t -= lowbit(t);
        }
    }
    // Transformation.resize((1 << n), vector<int>(0, 0));
    for (int a : LegalStates)
    {
        for (int j : LegalStates)
        {
            if (!check(a, j))
                Transformation[a].push_back(j);
        }
    }
}

LL f[2][K][M];

LL dp() // 答案会爆int,结果f数组改成long long之后dp函数忘记由int改为long long了,八嘎
{
    f[0][0][0] = 1ll;
    for (int i = 1; i <= n + 1; i++)
        for (int st : LegalStates)
        {
            for (int c = cnt[st]; c <= k; c++)
            {
                f[i & 1][c][st] = 0; //采用滚动数组,这里记得要先清零,否则会重复在同一个位置多次累加,导致答案偏大
                int lc = c - cnt[st];
                for (int j : Transformation[st])
                    f[i & 1][c][st] += f[(i - 1) & 1][lc][j];
            }
        }

    return f[(n + 1) & 1][k][0];
}

int main()
{
    R(n);
    R(k);
    init();
    /*for(int i : LegalStates)
    {
        cout << i << " " << cnt[i] << endl;
        cout << "K:";
        for(int j : Transformation[i])
            cout << j << " ";
        cout << endl;
    }*/
    printf("%lld\n", dp());
    return 0;
}

 

标签:ch,int,LegalStates,long,ACwing1064,include,Transformation,国王
From: https://www.cnblogs.com/smartljy/p/18109201

相关文章

  • 每日一题 第三期 洛谷 国王游戏
    [NOIP2012提高组]国王游戏题目描述恰逢H国国庆,国王邀请nnn位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写......
  • 洛谷题单指南-贪心-P1080 [NOIP2012 提高组] 国王游戏
    原题链接:https://www.luogu.com.cn/problem/P1080题意解读:通过不同的排队方式,让获得最多奖赏的大臣金额尽可能的少。此题如果没有思路,用全排列枚举可以“骗”分,更好的做法直觉上是某种贪心策略,另外基于数据规模考虑,要拿满分,需要上高精度。下面就由浅入深一步一步的解决。解题思......
  • 从国王游戏看邻项交换
    对于这道题,我们只来证明贪心的正确性,并不探究推导过程(这么玄学的贪心真有人能推导出来吗)。我们需要证明按照\(l_i\timesr_i\)是最优的。现在,我们钦定序列按照\(l_i\timesr_i\)排序,证明无论如何,从序列中交换若干对数都是不优的。首先,交换若干对数的本质就是不断选择一对数......
  • 贪心国王游戏
    贪心耍杂技的牛国王游戏同款思路大部分贪心用的都是已经被证明过的知名的数学模型贪心得到的答案>=最优解贪心得到答案<=最优解#include<iostream>#include<algorithm>usingnamespacestd;//给pair<int,int>起个别名PIItypedefpair<int,int>P......
  • 82 贪心 [NOIP2012 提高组] 国王游戏
    视频链接: LuoguP1080[NOIP2012提高组]国王游戏#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>usingnamespacestd;structnode{inta,b;booloperator<(node&t){returna*b<t.a*t.b;}}......
  • 【UR #1】跳蚤国王下江南
    题目链接【UR#1】跳蚤国王下江南做法建出圆方树,考虑行走的路径一定是树形结构,每个环都有一个节点深度是最浅的,遇到环时会有两种走法。考虑DP。令$dp[i][j]$表示$......
  • 从前有个葡萄牙国王,他有三个可爱的女11
    从前有个葡萄牙国王,他有三个可爱的女http://ds.163.com/article/6338aae1d3fdd0000198760e/?2022/10/06_=2022/10/05http://ds.163.com/feed/6338aae1d3fdd0000198760e/?202......
  • 第二天清晨,皮罗斯人把他们国王的儿子49
    第二天清晨,皮罗斯人把他们国王的儿子http://m.ds.163.com/feed/63388b80cb2dc40001ecf937/?2022_1005=20221005uhttp://m.ds.163.com/article/63388b81a1ca540001d373c8/?20......
  • 赫耳墨斯陪着国王一直来到斯卡曼德洛斯55
    赫耳墨斯陪着国王一直来到斯卡曼德洛斯http://m.ds.163.com/feed/6338b095c5e2010001202978/?2022_1005=20221005uhttp://m.ds.163.com/article/6338b097a1ca540001d383df/?......
  • 帕拉斯雅典娜飞到斯巴达,在国王墨涅拉11
    帕拉斯雅典娜飞到斯巴达,在国王墨涅拉http://ds.163.com/article/6338a5aa85eece000185b5f6/?2022_1005=20221005uhttp://ds.163.com/feed/6338a5aa85eece000185b5f6/?2022_1......