1094 字
5 分钟
2289A题解

2289A Problem from Dolgoderevenskoye#

题目描述#

You are given a positive integer xx. Consider a set of cards such that for every positive divisor dd of the number xx, there is exactly one card with the number dd.

Two players play the following game. The players take turns, with Artem moving first. On each move, a player must remove one card from the set.

On the first move, Artem may remove any card. On each subsequent move, the player must remove a card whose parity is opposite to the parity of the card removed on the previous move. In other words, if the previous player removed a card with an even number, the next player must remove a card with an odd number, and vice versa. The player who cannot make a move loses.

For example, if x=6x=6, then initially there are cards with numbers 11, 22, 33, and 66. If Artem removes the card with number 66 on his first move, then the second player must remove the card with number 11 or 33. After the second player’s move, Artem will be able to remove only the card with number 22 from the set.

Determine whether Artem will win if both players play optimally.

输入格式#

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041\le t\le 10^4). The description of the test cases follows.

The only line of each test case contains one integer xx (1≤x≤10181\le x\le 10^{18}).

输出格式#

For each test case, output Yes if Artem wins, and No otherwise.

You may print each letter in any case (lowercase or uppercase). For example, the strings yEs, yes, Yes, and YES will be accepted as positive answers.

说明/提示#

题面里的样例没有拿到,此处从略。

思路#

牌就是 xx 的全部正约数,按奇偶分成两堆。记

  • oo = 奇数约数的个数,
  • ee = 偶数约数的个数。

把 xx 写成 x=2a⋅mx=2^a\cdot m(mm 是奇数),那么 mm 的约数有 σ0(m)=o\sigma_0(m)=o 个,偶数约数则是「21,…,2a2^1,\dots,2^a 各配一个 mm 的约数」,所以

e=a⋅oe=a\cdot o

整局只能按「奇、偶、奇、偶……」这种交替顺序取牌,谁接不上谁输。Artem 只在第一手有选择权,所以他的胜负完全由 ee 和 oo 的大小关系决定。

分三种情况#

a=0a=0(xx 是奇数):e=0e=0,牌全是奇数。Artem 随手取一张奇数,下一手对手必须取偶数——没有,对手直接输。

a=1a=1(x≡2(mod4)x\equiv2\pmod4):e=oe=o,两堆一样多。不管 Artem 第一手取奇还是取偶,接下来两边都被迫交替,总共 2o2o 张牌正好取完,最后一张由对手取走,轮到 Artem 时无牌可取。所以是必败。

a≥2a\ge2(4∣x4\mid x):e=a⋅o≥2o>oe=a\cdot o\ge2o>o,偶数牌严格多于奇数牌。Artem 第一手取偶数,之后奇数那堆会先耗光:对手取走最后一张奇数时,Artem 还能再取一张偶数(因为偶数配额够),轮到对手要奇数——没了,对手输。

三种情况合起来:只有 x≡2(mod4)x\equiv2\pmod4 是必败,其余全胜。

代码怎么写#

判据就是一句 x % 4 != 2。这份代码换了个写法:先看奇数(x & 1),否则数一数 xx 能被 22 除几次(while(!(x & 1)) k++, x /= 2),k≥2k\ge2 就赢、k=1k=1 就输。等价。

正确代码#

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T;
int x;
signed main()
{
cin>>T;
while(T--){
cin>>x;int k=0;
if(x&1) {cout<<"YES"<<endl; continue;}
while(!(x&1)){
k++;x/=2;
}
if(k>=2) {cout<<"YES"<<endl;}
else{cout<<"NO"<<endl;}
}
return 0;
}

我想了什么#

这题的关键量是「偶数约数和奇数约数的个数比」,不是总约数。 一开始容易去数 σ0(x)\sigma_0(x) 然后按奇偶分,那会绕远——真正决定胜负的是 ee 和 oo 谁多。

为什么 x≡2(mod4)x\equiv2\pmod4 是唯一必败? 因为只有这时 a=1a=1,ee 才恰好等于 oo。a=0a=0 时偶数牌是 00(对手第一手就断),a≥2a\ge2 时偶数牌至少是奇数牌的两倍(自己不会先断)。

代码里那个 while 把 x 改掉了。 循环结束 x 已经不是原数,只是后面不再用。要是这题还要求输出 xx 的什么信息,就得另开变量。

k >= 2 而不是 k >= 1。 这里很容易写成 k >= 1——那样 x≡2(mod4)x\equiv2\pmod4 的会被判成赢,正好反了。想清楚「kk 是 v2(x)v_2(x)」之后这个边界才稳。

int k=0; 写在 while(T--) 里面。 每次重新定义,不用手动清。

#define int long long 是必要的。 x≤1018x\le10^{18} 放不进 int,这个宏在这题上不是习惯。

输出用 YES / NO 全大写。 题面说任意大小写都接受,所以没问题。

复杂度#

每组 O(log⁡x)O(\log x)(数末尾 0 的循环),总 O(tlog⁡x)O(t\log x),空间 O(1)O(1)。

小结#

这题的结论短得可以背下来:x≡2(mod4)x\equiv2\pmod4 输出 No,其余 Yes。但它背后是「偶数约数有几个、奇数约数有几个」的博弈分析——e=aoe=ao 这个关系是关键。

代码用「除干净 22、数次数」来表达 v2(x)v_2(x),逻辑对,只是比直接取模长一点。

第 117 篇,共 147 篇本文属于系列

2289A题解
https://www.hhluckyex.cn/posts/problen_solution/2289a/
作者
HHluckyex
发布于
2026-11-22
许可协议
CC BY-NC-SA 4.0

分享文章

生成精美分享图或复制链接,与更多人分享本文。

继续阅读

沿着主题读

基于共同的标签与分类

换条路线

从其他文章中稳定抽取