HackerRank - powers-game-1 【博弈论】

本文解析了HackerRank上powers-game题目,通过分析2^n序列的组合特性,利用同余定理简化计算过程,并最终得出判断赢家的规律。适用于博弈论初学者。

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

HackerRank - powers-game-1 【博弈论】

这里写图片描述

这里写图片描述

题意
给出 * 2^1 * 2^2 * 2^3 * 2^4 * 2^5 * 2^n 这一串东西 ,然后有两个玩家,*号是可以被替换掉的东西,可以换成+ 或者 - 然后最后的式子求出来后MOD 17 如果最后的结果 == 0 则 P2 wins 否则 P1 wins

思路
因为MOD 17 根据同余定理,我们可以 在替换*号之前就MOD 比如

2^1 2^2 2^3 2^4 2^5 2^6 2^7 2^8
2 4 8 16 15 13 9 1

然后我们发现 得到的都是一堆 0 - 15 的数字

因为都是双方出的都是最优的策略

我们发现 N = 8 的时候

分别是
1 16
2 15
4 13
8 9

这四组数据的特点是 相加都是 17

玩家一先出 如果玩家一对某一个数 换号 玩家二只要对配套数换成相同符号 即可

最后发现规律 只要N % 8 == 0 就是玩家二赢

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E  = 2.718281828459;  
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        if (n % 8 ==0)
            cout << "Second\n";
        else 
            cout << "First\n";
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值