Under construction

Still being built. Things may change

The divisibility game

Time limit2sMemory limit512 MB

Summary
Two players alternately delete numbers from a set; decide whether X can force the remaining sum to be divisible by P after exactly K deletions.
Level

Hard8 of 10

Topics
Game theory, Combinatorics, Math
Solved
No attempts yet

Problem

Two players, X and Y, play the following game.

  • They are given a positive integer PP and a set A={a1,a2,…,aN}A = \{a_1, a_2, \ldots, a_N\} of NN distinct nonnegative integers. Every aia_i is less than PP.
  • The players take turns alternately. On each turn, the player to move deletes one number from AA.
  • After exactly KK turns, if the sum of the numbers remaining in AA is divisible by PP, X wins. Otherwise, Y wins.

Write a program that determines who wins when both players play optimally.

Input

The first line contains a positive integer TT, the number of games in the input.

Then, for each i=0,1,…,T−1i = 0, 1, \ldots, T-1:

  • line 3i+23i+2 contains NN, KK, and PP, separated by spaces;
  • line 3i+33i+3 contains a single character, X or Y, which is the player who moves first;
  • line 3i+43i+4 contains a1,a2,…,aNa_1, a_2, \ldots, a_N, separated by spaces.

Output

Print one line with TT characters and no separators, one character per game. The ii-th character is X if X wins the ii-th game no matter how Y plays, and Y otherwise.

Limits

  • 1≤K≤N≤50001 \le K \le N \le 5000
  • P≤1018P \le 10^{18}
  • 0≤ai<P0 \le a_i < P for every ii, and ai≠aja_i \ne a_j for every i≠ji \ne j

Examples1

  1. Example 1

    Input
    3
    5 3 7
    X
    1 2 3 4 6
    8 4 13
    Y
    5 10 6 11 2 8 9 3
    6 1 12
    X
    1 4 5 7 9 11
    
    Expected output
    XYX