[BZOJ1228][[SDOI2009]E&D(SG函数+找规律)

本文介绍了一个基于石子堆的游戏博弈论问题,通过分析得出当两边石子数量均为奇数时,游戏状态的SG值为0;否则,SG值等于各自除以2向上取整后的SG值加1。通过递归计算每组石子的状态并利用异或操作确定先手胜负。

可以发现这个游戏其实是n2n2个独立的游戏,只要分别求出这些独立游戏的SGSG函数,把它们异或起来之后,如果为00则先手败,否则先手胜。
SG(a,b)表示左边一堆aa个石子,右边一堆b个石子,这种状态下的SGSG值。但是aab2×1092×109级别的。
找规律大法好
aab都是奇数时,SG(a,b)=0SG(a,b)=0
否则SG(a,b)=SG(a2b2)+1SG(a,b)=SG(⌈a2⌉⌈b2⌉)+1
代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
    int res = 0; bool bo = 0; char c;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-') bo = 1; else res = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        res = (res << 3) + (res << 1) + (c - 48);
    return bo ? ~res + 1 : res;
}
const int N = 2e4 + 5;
int n, a[N];
int SG(int x, int y) {
    if ((x & 1) && (y & 1)) return 0; if (x & 1) x++; if (y & 1) y++;
    return SG(x >> 1, y >> 1) + 1;
}
void work() {
    int i, ans = 0; n = read(); for (i = 1; i <= n; i++) a[i] = read();
    for (i = 1; i <= n; i += 2) ans ^= SG(a[i], a[i + 1]);
    puts(ans != 0 ? "YES" : "NO");
}
int main() {
    int T = read(); while (T--) work();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值