#P2599. [ZJOI2009] 取石子游戏

    ID: 1612 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学博弈论2009各省省选浙江Nim游戏

[ZJOI2009] 取石子游戏

Description

After studying the Nim game and its variants, Orez discovered a brand-new stone-taking game, defined as follows:

There are nn piles of stones arranged in a row. Two players take turns. On each turn, a player may take any positive number of stones from either the leftmost pile or the rightmost pile; the entire pile may be taken, but taking zero stones is not allowed. The player who cannot move loses.

Orez asks: given any initial position, does there exist a winning strategy for the first player?

Input Format

The first line contains an integer TT, the number of test cases.
For each test case:

  • The first line contains an integer nn, the number of piles.
  • The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n, where aia_i is the number of stones in the ii-th pile.

Output Format

For each test case, output a single integer 00 or 11. Here, 11 means there exists a winning strategy for the first player, and 00 means there is not.

1
4
3 1 9 4

0

Hint

  • For 30%30\% of the testdata, n5n \le 5, ai105a_i \le 10^5.
  • For 100%100\% of the testdata, 1T101 \le T \le 10, 1n10001 \le n \le 1000, 1ai1091 \le a_i \le 10^9.

Translated by ChatGPT 5