#P7829. [CCO 2021] Weird Numeral System

    ID: 8899 远端评测题 1500ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2021Special JudgeCCO(加拿大)记忆化搜索数位 DP进制

[CCO 2021] Weird Numeral System

Problem Description

Alice is thinking about a problem involving base kk integers.

In standard base kk, an integer nn can be written as dm1dm2d0d_{m - 1} d_{m - 2} \cdots d_0, satisfying:

  1. 0di<k0 \leq d_i < k.
  2. n=i=0m1dikin = \displaystyle\sum_{i = 0}^{m - 1} d_i k^i.

However, standard base kk integers are too easy for Alice. Alice prefers weird base kk integers. The only difference from standard base kk integers is that the condition 0di<k0 \leq d_i < k is replaced by diad_i \in a, where aa is a sequence of length DD.

Now, a fixed sequence a1,a2,,aDa_1, a_2, \cdots, a_D is given. Alice wants to convert qq decimal integers n1,n2,,nqn_1, n_2, \cdots, n_q into weird base kk integers. This kind of problem is clearly more suitable to be solved by writing a program.

Input Format

The first line contains four integers k,q,D,Mk, q, D, M.

The second line contains DD integers a1,a2,,aDa_1, a_2, \cdots, a_D.

The next qq lines each contain one integer nin_i.

Output Format

Output qq lines. The ii-th line should be the result of converting nin_i. Output each digit from the highest power to the lowest power, separated by a single space.

When aia_i contains 00, your result may contain leading zeros, but it is better not to have too many. When ni=0n_i = 0, your result must not be empty.

If there are multiple valid answers, you may output any one of them. If it is impossible to convert, output IMPOSSIBLE.

3 3 3 1
-1 0 1
15
8
-5
1 -1 -1 0
1 0 -1
-1 1 1
10 1 3 2
0 2 -2
17
IMPOSSIBLE

Hint

**This problem is provided with an SPJ by

https://www.luogu.com.cn/user/201007

Constraints

For 100%100\% of the testdata, 2k1062 \leq k \leq 10^6, 1q51 \leq q \leq 5, 1D8011 \leq D \leq 801, 1M4001 \leq M \leq 400, MaiM-M \leq a_i \leq M, 1018ni1018-10^{18} \leq n_i \leq 10^{18}.

Source

CCO2021 D1T2.

Translated by ChatGPT 5