#P1288. Coin‐On‐Ring Game

    ID: 14575 Type: Default 1000ms 256MiB

Coin‐On‐Ring Game

Coin‐On‐Ring Game

Two players play a game on a ring. The ring has n edges, each labeled with a nonnegative integer; it is guaranteed that at least one edge carries the value \(0\). The vertices of the ring are numbered from \(0\) to \(n-1\) in clockwise order. An integer is written on each edge: the edge between vertex i and vertex \((i+1) \bmod n\) carries the number \(a_i\).

A coin is initially placed at a given vertex \(s\) (0-indexed). The two players take turns (the first mover is the one to move from vertex \(s\)) according to the following rules:

  1. On a turn, if the coin is at vertex \(i\), let the two edges adjacent to it be \(a_{(i-1+n)\bmod n}\) (to the left) and \(a_i\) (to the right). The player must choose one of these two edges whose current value is not \(0\);
  2. The player then decreases the chosen edge's number to any nonnegative integer strictly less than its current value (i.e. a positive amount is subtracted);
  3. Finally, the coin is moved along the chosen edge to the other vertex.

A player loses if, at the start of their turn, the two edges adjacent to the coin are both \(0\) (so that no move is possible).

The task is: given the ring (the integer on each edge) and the starting vertex, determine whether the first player has a winning strategy if both sides play optimally.

Note on formulas: All formulas are rendered in \(\LaTeX\) format. For example, an edge value is denoted by \(a_i\) and the modular arithmetic by \( (i+1) \bmod n \).

inputFormat

The first line contains two integers \(n\) and \(s\) (with \(2 \le n \le 50\) and \(0 \le s < n\)), representing the number of edges (and vertices) of the ring and the starting vertex, respectively.

The second line contains \(n\) nonnegative integers \(a_0, a_1, \ldots, a_{n-1}\), where \(a_i\) is the number written on the edge between vertex \(i\) and vertex \((i+1) \bmod n\). It is guaranteed that at least one \(a_i = 0\).

outputFormat

Output a single line containing YES if the first player can force a win, or NO otherwise.

sample

2 0
1 1
NO