博弈论+位运算,CF 1934D2 - XOR Break --- Game Version

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

1934D2 - XOR Break --- Game Version


二、解题报告

1、思路分析

比较好想的博弈论

如果拿到两个bitcount = 1的数就完了

对于bitcount 为偶数的数,我们可以拆出两个bitcount 为奇数的数

对于bitcount 为奇数的数,我们可以至少拆出一个bitcount 为偶数的数

也就是说,bitcount 为偶数的数是必胜态,bitcount 为奇数的数为必败态

那么n如果是bitcount 为偶数的数,我们先手

否则后手

保证我们每次都能拿到一个bitcount 为偶数的数,然后怎么拆呢?

拆为 最高位1 和 剩下的部分,这样会使得我们后面拿到的bitcount 为偶数的数越来越小,一定能拆出俩bitcount 为1的数使得bob必输

2、复杂度

时间复杂度: O(log n)空间复杂度:O(1)

3、代码详解

 ​
#include <bits/stdc++.h>
// #include <ranges>
// #define DEBUG
using i64 = long long;
using u32 = unsigned;
using u64 = unsigned long long;
constexpr int inf32 = 1E9 + 7;
constexpr i64 inf64 = 1E18 + 7;
constexpr double eps = 1e-9;

void solve() {
    i64 n;
    std::cin >> n;

    int t = 0; // 轮次

    if (__builtin_parityll(n) == 0) {
        std::cout << "first" << std::endl;
    }
    else {
        std::cout << "second" << std::endl;
        t = 1;
    }

    while (true) {
        if (t == 0) {
            i64 p1 = 1LL << std::__lg(n);
            i64 p2 = n ^ p1;
            std::cout << p1 << ' ' << p2 << std::endl;
        }
        else {
            i64 p1, p2;
            std::cin >> p1 >> p2;
            if (p1 == 0 && p2 == 0) {
                break;
            } 
            if (__builtin_parityll(p1) == 0) {
                n = p1;
            }
            else {
                n = p2;
            }
        }
        t ^= 1;
    }
}

auto FIO = []{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return 0;
} ();

int main() {
    #ifdef DEBUG
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif     

    int t = 1;
    std::cin >> t;
    while (t --)
        solve();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值