#P15405. [NOISG 2026 Prelim] 魔术戏法(暂无数据)

    ID: 19623 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2026NOISG(新加坡)

[NOISG 2026 Prelim] 魔术戏法(暂无数据)

Problem Description

A new format of cards has been released. The value of each card is a base64 string of length kk. The character set is: {#, $, 0-9, A-Z, a-z}.

There are n=64kn = 64^k cards in total, and each string appears exactly once. The sealed new deck is sorted in lexicographical order from smallest to largest.

Definition: a string ss is lexicographically greater than tt if and only if there exists a position ii such that s1=t1,…,si−1=ti−1s_1 = t_1, \ldots, s_{i-1} = t_{i-1} and si>tis_i > t_i.

The shuffle is repeated infinitely many times. Each shuffle is performed as follows:

  1. Split the deck into two halves: the first half is positions 1∼n/21 \sim n/2, and the second half is positions n/2+1∼nn/2+1 \sim n (the internal order of each half remains unchanged).
  2. Starting from the first half, take cards alternately from the two halves. If before shuffling the positions are numbered 1∼n1 \sim n, then after one shuffle the order becomes $\langle 1, \frac{n}{2} + 1, 2, \frac{n}{2} + 2, \ldots, \frac{n}{2}, n \rangle$.

There are mm tricks. The ii-th trick requires that the card with value xix_i ends up at the position where the card with value yiy_i is in the original sealed deck. Find the minimum number of shuffles needed to meet the condition. Output 00 if it is already satisfied initially; output −1-1 if it can never be satisfied.

Input Format

  • The first line contains two integers k,mk, m. (1≤k,m≤1000)(1 \le k, m \le 1000).
  • The next mm lines each contain two strings xi,yix_i, y_i, both of length kk. All strings use only the 64 characters above.

Output Format

For each trick, output one line with an integer: the number of shuffles needed for the condition to be satisfied for the first time; if it is impossible, output −1-1.

1 7
# #
U $
4 3
t D
D t
2 5
$ 2
0
1
-1
3
3
-1
2
3 8
Hd7 CYZ
mZs 1Z8
iYq poa
JlP edh
SyR uxw
aCp n50
I#9 0q8
wRP t1r
2
-1
15
-1
13
-1
-1
-1

Hint

Constraints

  • 1≤k,m≤10001 \le k, m \le 1000

Subtasks

Subtask ID Properties Score
1 k≤8k \le 8 20
2 k,m≤100k, m \le 100 30
3 No special constraints 50

Translated by ChatGPT 5