#P16642. [GKS 2018 #B] Sherlock and the Bit Strings

[GKS 2018 #B] Sherlock and the Bit Strings

Problem Description

Sherlock and Watson are playing a game involving bit strings, i.e., strings consisting only of the digits 00 and 11. Watson has challenged Sherlock to generate a bit string SS of NN characters S1S_1, S2S_2, ..., SNS_N. The string must obey each of KK different constraints; each of these constraints is specified via three integers AiA_i, BiB_i, and CiC_i. The number of 11s in the substring SAi,SAi+1,,SBiS_{A_i}, S_{A_i+1}, \dots, S_{B_i} must be equal to CiC_i.

Watson chooses the constraints in a way that guarantees that there is at least one string of the right length that obeys all of the constraints. However, since there could be multiple such strings, Watson wants Sherlock to choose the string from this set that is P{P}-th in lexicographic order, with P{P} counted starting from 11.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case begins with one line containing three integers NN, KK, and PP, as described above. Then, there are KK more lines; the ii-th of these contains three integers AiA_i, BiB_i and CiC_i, representing the parameters of the ii-th constraint, as described above.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the P{P}th lexicographically smallest bit string among all possible strings following the K{K} specified constraints.

2
3 1 2
2 2 1
3 1 1
2 2 0
Case #1: 011
Case #2: 000

Hint

In Sample Case #1, the bit strings that obey the only constraint in lexicographically increasing order are [010,011,110,111][010, 011, 110, 111].

In Sample Case #2, the bit strings that obey the only constraint in lexicographically increasing order are [000,001,100,101][000, 001, 100, 101].

Limits

1T1001 \le T \le 100.

1N1001 \le N \le 100.

1K1001 \le K \le 100.

$1 \le P \le \min(10^{18}, \text{the number of bit strings that obey all of the constraints})$.

1AiBiN1 \le A_i \le B_i \le N for all 1iK1 \le i \le K.

0CiN0 \le C_i \le N, for all 1iK1 \le i \le K.

(Ai,Bi)(Aj,Bj)(A_i, B_i) \neq (A_j, B_j), for all 1i<jK1 \le i < j \le K.

Small dataset (Test set 1 - Visible)

Ai=BiA_i = B_i for all 1iK1 \le i \le K.

Large dataset (Test set 2 - hidden)

BiAi15B_i - A_i \le 15 for all 1iK1 \le i \le K.