LeetCode 1025 Divisor Game (博弈)

博客围绕除数博弈游戏展开,Alice和Bob轮流操作,从黑板上的数字开始,选择符合条件的数替换原数字,无法操作则输。分析得出偶数先手必胜、奇数先手必败的结论,并给出证明,还给出了题目链接。

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

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number N on the chalkboard.  On each player's turn, that player makes a move consisting of:

  • Choosing any x with 0 < x < N and N % x == 0.
  • Replacing the number N on the chalkboard with N - x.

Also, if a player cannot make a move, they lose the game.

Return True if and only if Alice wins the game, assuming both players play optimally.

Example 1:

Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.

Example 2:

Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.

Note:

  1. 1 <= N <= 1000

题目链接:https://leetcode.com/problems/divisor-game/

题目分析:手推几个可以发现偶数是先手必胜,奇数是先手必败,下面证明

奇数的约数肯定是奇数(显然),那么面对奇数,一次操作完后必然变成偶数,先手如果是偶数,只需要每次都把这个数字变成奇数(减1即可),这样下去开始的后手最终必然会将数字变成2

class Solution {
    public boolean divisorGame(int N) {
        return N % 2 == 0;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值