【博弈】HackerRank _ stone_division

本文介绍了一个关于两个玩家轮流操作的博弈游戏,玩家的目标是在对手无法继续操作时赢得游戏。文章详细阐述了游戏规则,并给出了一段C++代码实现,用于判断初始状态下哪一方能够获胜。

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

Consider the following game:

  • There are two players, First and Second, sitting in front of a pile of  stones. First always plays first.
  • There is a set, , of  distinct integers defined as .
  • The players move in alternating turns. During each turn, a player chooses some  and splits one of the piles into exactly  smaller piles of equal size. If no  exists that will split one of the available piles into exactly  equal smaller piles, the player loses.
  • Both players always play optimally.

Given , and the contents of , find and print the winner of the game. If First wins, print First; otherwise, print Second.

Input Format

The first line contains two space-separated integers describing the respective values of  (the size of the initial pile) and  (the size of the set). 
The second line contains  distinct space-separated integers describing the respective values of .

Constraints

Output Format

Print First if the First player wins the game; otherwise, print Second.

Sample Input 0

15 3
5 2 3

Sample Output 0

Second

Explanation 0

The initial pile has  stones, and . During First's initial turn, they have two options:

  1. Split the initial pile into  equal piles, which forces them to lose after the following sequence of turns: 
    stone-division.png
  2. Split the initial pile into  equal piles, which forces them to lose after the following sequence of turns: 
    stone-division-2.png

Because First never has any possible move that puts them on the path to winning, we print Second as our answer.

/*啊我也不是很懂,看别人的代码补得题*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[15];
int m;
map<LL,int>mp;
int check(LL n){
    if(mp.count(n))
        return mp[n];
    int ans=0;
    for(int i=0;i<m;i++){
        if(n%a[i]==0){
            if(a[i]%2==0)/*能分成偶数块,先手必胜*/
                return mp[n]=1;
            else ans|=!check(n/a[i]);/*在下一次先手必输的情况下,也就是对于这局来说后手必输的情况下,就是这局先手必胜的情况*/
        }
    }
    return mp[n]=ans;
}
int main()
{
    LL n;
    scanf("%lld%d",&n,&m);
    for(int i=0;i<m;i++)
        scanf("%lld",&a[i]);
    printf(check(n)?"First\n":"Second\n");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值