Duel of Frequencies#12467

Duel of Frequencies (Editorial)

Tutorial

Let be the number of stones divisible only by , and be the number of stones divisible only by . Stones divisible by neither are useless.

Stones divisible by both and grant an immediate extra move, meaning taking one does not pass the turn to the opponent. Because a player's turn sequence only truly ends when they pick an exclusive stone (an stone for Kael, or a stone for Liora), the shared stones merely delay the inevitable without altering the overall balance of turns.

The game reduces to a simple battle of attrition over the exclusive stones:

  • Kael consumes exactly one stone per turn sequence, and Liora consumes exactly one stone.

  • Since Kael goes first, he must have strictly more exclusive stones than Liora to outlast her.

  • If A > B, Kael wins. Otherwise, if A \le B, Liora wins because she has enough stones to match Kael's moves until he runs out.

Complexity Time Complexity: per testcase to check divisibility and count stones, resulting in \mathcal{O}(\sum N) overall.

Space Complexity: auxiliary space.