题解 UVA10165 【Stone Game】

· · 题解

事实上这题就是一个经典的 \operatorname{Nim} 问题。

假设有一个序列 a, 其中有 n 个元素,如果 a_1\space xor\space a_2 \space xor x_3 \space \ldots xor \space a_{n - 1} \space xor \space a_n \space !\ne 0, 则输出 \operatorname{Yes}, 否则输出 \operatorname{No}。

下面是代码:



#include <bits/stdc++.h>

using namespace std;
int _res;
int _num;

void ParseInCore() {
    for(int i = 1; i < _num + 1; i++) {
        int opt; 
        cin >> opt;
        _res ^= opt;
    }
}

void WriteOut() {
    puts(_res == 0 ? "No" : "Yes");
}

int main() {

    while(cin >> _num && _num != 0) {
        _res = 0;
        ParseInCore();
        WriteOut();
    }

    return 0;
}