A - Repdigit Number

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300

問題文

正整数 N, M が与えられます.次の条件をすべて満たす正整数 X のうち,最大であるものを答えてください.

  • X10^N 未満の正整数で,X10 進法表記したときのどの桁の数字も同じである.
  • XM の倍数である.

ただし,条件を満たす正整数 X が存在しない場合には -1 と出力してください.

制約

  • 1\leq N\leq 10^5
  • 1\leq M\leq 10^9

入力

入力は以下の形式で標準入力から与えられます.

N M

出力

条件をすべて満たす正整数 X のうち最大であるものを出力してください.ただし,そのような正整数 X が存在しない場合には -1 と出力してください.


入力例 1

7 12

出力例 1

888888

条件を満たす正整数 X は,444, 888, 444444, 8888884 つです.このうち最大のものである 888888 が答となります.


入力例 2

9 12

出力例 2

888888888

条件を満たす正整数 X は,444, 888, 444444, 888888, 444444444, 8888888886 つです.


入力例 3

1 3

出力例 3

9

条件を満たす正整数 X は,3, 6, 93 つです.


入力例 4

1000 25

出力例 4

-1

条件を満たす正整数 X は存在しません.


入力例 5

30 1

出力例 5

999999999999999999999999999999

Score : 300 points

Problem Statement

You are given positive integers N and M. Find the maximum positive integer X that satisfies all of the following conditions.

  • X is a positive integer less than 10^N, and all digits in the decimal representation of X are the same.
  • X is a multiple of M.

If no positive integer X satisfies the conditions, print -1.

Constraints

  • 1\leq N\leq 10^5
  • 1\leq M\leq 10^9

Input

The input is given from Standard Input in the following format:

N M

Output

Print the maximum positive integer X that satisfies all of the conditions, or -1 if no such positive integer X exists.


Sample Input 1

7 12

Sample Output 1

888888

Four positive integers X satisfy the conditions: 444, 888, 444444, 888888. The answer is the maximum of them, which is 888888.


Sample Input 2

9 12

Sample Output 2

888888888

Six positive integers X satisfy the conditions: 444, 888, 444444, 888888, 444444444, 888888888.


Sample Input 3

1 3

Sample Output 3

9

Three positive integers X satisfy the conditions: 3, 6, 9.


Sample Input 4

1000 25

Sample Output 4

-1

No positive integers X satisfy the conditions.


Sample Input 5

30 1

Sample Output 5

999999999999999999999999999999
B - Two LIS Sum

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 500

問題文

数列 P = (P_1, \ldots, P_N) に対し,その最長増加部分列の長さを \mathrm{LIS}(P) と書くことにします.

1 以上 N 以下の整数からなる順列 A = (A_1, \ldots, A_N) および B = (B_1, \ldots, B_N) が与えられます.これらの数列に対して,以下の操作を何度でも行うことができます(0 回でもよいです).

  • 1\leq i\leq N-1 となる整数 i をひとつ選ぶ.A_iA_{i+1} をスワップし,B_iB_{i+1} をスワップする.

操作結果の \mathrm{LIS}(A) + \mathrm{LIS}(B) としてありうる最大値を答えてください.

最長増加部分列とは

数列の部分列とは,数列から 0 個以上の要素を取り除いた後,残りの要素を元の順序で連結して得られる数列のことをいいます. 例えば,(10,30)(10,20,30) の部分列ですが,(20,10)(10,20,30) の部分列ではありません.

数列の最長増加部分列とは,数列の狭義単調増加な部分列のうち,長さが最大のもののことをいいます.

制約

  • 2\leq N\leq 3\times 10^5
  • 1\leq A_i\leq N
  • 1\leq B_i\leq N
  • i\neq j ならば A_i\neq A_j かつ B_i\neq B_j

入力

入力は以下の形式で標準入力から与えられます.

N
A_1 \ldots A_N
B_1 \ldots B_N

出力

操作結果の \mathrm{LIS}(A) + \mathrm{LIS}(B) としてありうる最大値を出力してください.


入力例 1

5
5 2 1 4 3
3 1 2 5 4

出力例 1

8

例えば次のように操作を行うことで,\mathrm{LIS}(A) + \mathrm{LIS}(B) = 8 を達成できます.

  • i = 2 として操作を行う.A = (5,1,2,4,3), B = (3,2,1,5,4) となる.
  • i = 1 として操作を行う.A = (1,5,2,4,3), B = (2,3,1,5,4) となる.
  • i = 4 として操作を行う.A = (1,5,2,3,4), B = (2,3,1,4,5) となる.

このとき A は最長増加部分列 (1,2,3,4) を持ち,\mathrm{LIS}(A)=4 が成り立ちます.B は最長増加部分列 (2,3,4,5) を持ち,\mathrm{LIS}(B)=4 が成り立ちます.


入力例 2

5
1 2 3 4 5
1 2 3 4 5

出力例 2

10

操作を 1 度も行わないことにより,\mathrm{LIS}(A) + \mathrm{LIS}(B) = 10 を達成できます.

Score : 500 points

Problem Statement

For a sequence P = (P_1, \ldots, P_N), let \mathrm{LIS}(P) denote the length of a longest increasing subsequence.

You are given permutations A = (A_1, \ldots, A_N) and B = (B_1, \ldots, B_N) of integers from 1 through N. You may perform the following operation on these sequences any number of times (possibly zero).

  • Choose an integer i such that 1\leq i\leq N-1. Swap A_i and A_{i+1}, and swap B_i and B_{i+1}.

Find the maximum possible final value of \mathrm{LIS}(A) + \mathrm{LIS}(B).

What is a longest increasing subsequence?

A subsequence of a sequence is a sequence obtained by removing zero or more elements from the original sequence and then concatenating the remaining elements without changing the order. For instance, (10,30) is a subsequence of (10,20,30), but (20,10) is not a subsequence of (10,20,30).

A longest increasing subsequence of a sequence is a subsequence of that sequence with the greatest length among its subsequences that are strictly increasing.

Constraints

  • 2\leq N\leq 3\times 10^5
  • 1\leq A_i\leq N
  • 1\leq B_i\leq N
  • A_i\neq A_j and B_i\neq B_j, if i\neq j.

Input

The input is given from Standard Input in the following format:

N
A_1 \ldots A_N
B_1 \ldots B_N

Output

Print the maximum possible final value of \mathrm{LIS}(A) + \mathrm{LIS}(B).


Sample Input 1

5
5 2 1 4 3
3 1 2 5 4

Sample Output 1

8

Here is one way to achieve \mathrm{LIS}(A) + \mathrm{LIS}(B) = 8.

  • Do the operation with i = 2. Now, A = (5,1,2,4,3), B = (3,2,1,5,4).
  • Do the operation with i = 1. Now, A = (1,5,2,4,3), B = (2,3,1,5,4).
  • Do the operation with i = 4. Now, A = (1,5,2,3,4), B = (2,3,1,4,5).

Here, A has a longest increasing subsequence (1,2,3,4), so \mathrm{LIS}(A)=4, and B has a longest increasing subsequence (2,3,4,5), so \mathrm{LIS}(B)=4.


Sample Input 2

5
1 2 3 4 5
1 2 3 4 5

Sample Output 2

10

You can decide not to perform the operation at all to achieve \mathrm{LIS}(A) + \mathrm{LIS}(B) = 10.

C - Avoid Prime Sum

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 500

問題文

正整数 N が与えられます.

NN 列からなるマス目の各マスに N^2 以下の正整数を 1 つずつ書き込んで,以下の条件がすべて成り立つようにしてください.

  • 上下左右の 4 方向いずれかに隣接する 2 マスに書き込まれた正整数の和は,どれも素数ではない.
  • N^2 以下の正整数はすべてどれかのマスに 1 度ずつ書き込まれている.

なお本問題の制約のもと,このような書き込み方が必ず存在することが証明できます.

制約

  • 3\leq N\leq 1000

入力

入力は以下の形式で標準入力から与えられます.

N

出力

ij 列に書き込む正整数を A_{ij} として,条件を満たす書き込み方を,以下の形式で出力してください.

A_{11} \ldots A_{1N}
\vdots
A_{N1} \ldots A_{NN}

条件を満たす書き込み方が複数存在する場合は,どれを出力しても正解となります.


入力例 1

4

出力例 1

15 11 16 12
13 3 6 9
14 7 8 1
4 2 10 5

このマス目には 1 以上 16 以下の正整数がすべて 1 度ずつ書き込まれています.また隣接する 2 マスに書き込まれた正整数の和には 15+11=26, 11+16=27, 15+13=28 などがありますが,これらはすべて素数ではありません.

Score : 500 points

Problem Statement

You are given a positive integer N.

Fill each square of a grid with N rows and N columns by writing a positive integer not greater than N^2 so that all of the following conditions are satisfied.

  • Two positive integers written in horizontally or vertically adjacent squares never sum to a prime number.
  • Every positive integer not greater than N^2 is written in one of the squares.

Under the Constraints of this problem, it can be proved that such a way to fill the grid always exists.

Constraints

  • 3\leq N\leq 1000

Input

The input is given from Standard Input in the following format:

N

Output

Print a way to fill the grid under the conditions in the following format, where A_{ij} is the positive integer at the i-th row and j-th column:

A_{11} \ldots A_{1N}
\vdots
A_{N1} \ldots A_{NN}

If there are multiple ways to fill the grid under the conditions, any of them will be accepted.


Sample Input 1

4

Sample Output 1

15 11 16 12
13 3 6 9
14 7 8 1
4 2 10 5

In this grid, every positive integer from 1 through 16 is written once. Additionally, among the sums of two positive integers written in horizontally or vertically adjacent squares are 15+11=26, 11+16=27, and 15+13=28, none of which is a prime number.

D - Simultaneous Sugoroku

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 700

問題文

N 個のコマが数直線上の整数座標に置かれています.i 番目のコマは座標 X_i に置かれています.

これらのコマを,次のように M 回移動させます.

  • i 回目の操作では,正整数 D_i が与えられ,各コマを次のように移動させる.
    • 座標が負の整数であるようなコマは,正の方向に距離 D_i 進んだ位置に移動させる.
    • 座標が 0 であるようなコマは動かさない.
    • 座標が正の整数であるようなコマは,負の方向に距離 D_i 進んだ位置に移動させる.

各コマが原点に到達するか否かを判定してください.原点に到達する場合には,はじめて原点に到達するのが何回目の移動によるものかを出力してください.原点に到達しない場合には,M 回の移動がすべて終了したときの座標を出力してください.

制約

  • 1\leq N\leq 3\times 10^5
  • 1\leq M\leq 3\times 10^5
  • 1\leq X_1 < \cdots < X_N \leq 10^6
  • 1\leq D_i \leq 10^6

入力

入力は以下の形式で標準入力から与えられます.

N M
X_1 \ldots X_N
D_1 \ldots D_M

出力

N 行出力してください.i 行目には,i 番目のコマに対する答を,以下に述べる形式で出力してください.

コマが原点に到達する場合には,はじめて原点に到達するのが x 回目の移動であるとして

Yes x

と出力してください.コマが原点に到達しない場合には,M 回の移動がすべて終了したときの座標が x であるとして

No x

と出力してください.


入力例 1

6 4
2 4 6 8 10 12
8 2 5 7

出力例 1

No -6
No -4
Yes 2
Yes 1
Yes 2
No 4

各コマの座標は次のように変化します.

  • 1 番目のコマ:\phantom{0}2\quad \longmapsto \quad -6\quad \longmapsto \quad -4\quad \longmapsto \quad \phantom{-}1 \quad \longmapsto \quad -6.
  • 2 番目のコマ:\phantom{0}4 \quad \longmapsto \quad -4\quad \longmapsto \quad -2 \quad \longmapsto \quad \phantom{-}3 \quad \longmapsto \quad -4.
  • 3 番目のコマ:\phantom{0}6 \quad \longmapsto \quad -2\quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0.
  • 4 番目のコマ:\phantom{0}8 \quad \longmapsto \quad \phantom{-}0\quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0.
  • 5 番目のコマ:10 \quad \longmapsto \quad \phantom{-}2\quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0.
  • 6 番目のコマ:12 \quad \longmapsto \quad \phantom{-}4\quad \longmapsto \quad \phantom{-}2 \quad \longmapsto \quad -3 \quad \longmapsto \quad \phantom{-}4.

Score : 700 points

Problem Statement

There are N pieces placed at integer coordinates on a number line. The coordinate of the i-th piece is X_i.

Let us move these pieces M times as follows.

  • In the i-th move, given a positive integer D_i, we move each piece as follows.
    • A piece whose coordinate is a negative integer is moved a distance of D_i in the positive direction.
    • A piece whose coordinate is 0 is not moved.
    • A piece whose coordinate is a positive integer is moved a distance of D_i in the negative direction.

Determine whether each piece arrives at the origin. If it does, print the number of moves after which it arrives there for the first time. Otherwise, print its coordinate after the M moves.

Constraints

  • 1\leq N\leq 3\times 10^5
  • 1\leq M\leq 3\times 10^5
  • 1\leq X_1 < \cdots < X_N \leq 10^6
  • 1\leq D_i \leq 10^6

Input

The input is given from Standard Input in the following format:

N M
X_1 \ldots X_N
D_1 \ldots D_M

Output

Print N lines. The i-th line should describe the i-th piece in the following format.

If the piece arrives at the origin, let x be the number of moves after which it arrives there for the first time, and print the following:

Yes x

If the piece does not arrive at the origin, let x be its coordinate after the M moves, and print the following:

No x

Sample Input 1

6 4
2 4 6 8 10 12
8 2 5 7

Sample Output 1

No -6
No -4
Yes 2
Yes 1
Yes 2
No 4

The coordinate of each piece changes as follows.

  • The 1-st piece: \phantom{0}2\quad \longmapsto \quad -6\quad \longmapsto \quad -4\quad \longmapsto \quad \phantom{-}1 \quad \longmapsto \quad -6.
  • The 2-nd piece: \phantom{0}4 \quad \longmapsto \quad -4\quad \longmapsto \quad -2 \quad \longmapsto \quad \phantom{-}3 \quad \longmapsto \quad -4.
  • The 3-rd piece: \phantom{0}6 \quad \longmapsto \quad -2\quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0.
  • The 4-th piece: \phantom{0}8 \quad \longmapsto \quad \phantom{-}0\quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0.
  • The 5-th piece: 10 \quad \longmapsto \quad \phantom{-}2\quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0 \quad \longmapsto \quad \phantom{-}0.
  • The 6-th piece: 12 \quad \longmapsto \quad \phantom{-}4\quad \longmapsto \quad \phantom{-}2 \quad \longmapsto \quad -3 \quad \longmapsto \quad \phantom{-}4.
E - Sliding Window Sort

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 700

問題文

正整数 N, M, K が与えられます.正整数列 A = (A_0, \ldots, A_{N-1}) に対する次の操作を考えます.

  • k=0, 1, \ldots, K-1 の順に次を行う.
    • A_{k\bmod N}, A_{(k+1)\bmod N}, \ldots, A_{(k+M-1)\bmod N} を昇順にソートする.つまり A_{k\bmod N}, A_{(k+1)\bmod N}, \ldots, A_{(k+M-1)\bmod N} を小さい方から順に並べたものを (x_0, \ldots, x_{M-1}) とするとき,各 0\leq j < M に対して A_{(k+j)\bmod N}x_j に置き換える.

1 以上 N 以下の整数からなる順列 B = (B_0, \ldots, B_{N-1}) が与えられます.正整数列 A であって,上記の操作を行った結果が B と一致するものの個数を 998244353 で割った余りを答えてください.

制約

  • 2\leq N\leq 3\times 10^5
  • 2\leq M\leq N
  • 1\leq K\leq 10^9
  • 1\leq B_i\leq N
  • i\neq j ならば B_i\neq B_j

入力

入力は以下の形式で標準入力から与えられます.

N M K
B_0 \ldots B_{N-1}

出力

正整数列 A であって,操作を行った結果が B と一致するものの個数を 998244353 で割った余りを出力してください.


入力例 1

6 3 5
6 4 2 3 1 5

出力例 1

18

例えば A = (4,1,5,6,2,3) が条件を満たします.この A に対して,操作は次のように進行します.

  • k=0 に対する操作により,A(1,4,5,6,2,3) になる.
  • k=1 に対する操作により,A(1,4,5,6,2,3) になる.
  • k=2 に対する操作により,A(1,4,2,5,6,3) になる.
  • k=3 に対する操作により,A(1,4,2,3,5,6) になる.
  • k=4 に対する操作により,A(6,4,2,3,1,5) になり,B に一致する.

入力例 2

6 3 5
6 5 4 3 2 1

出力例 2

0

条件を満たす A は存在しません.


入力例 3

20 20 149
13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12

出力例 3

401576539

1 以上 20 以下の整数からなる順列がすべて条件を満たします.

Score : 700 points

Problem Statement

You are given positive integers N, M, and K. Consider the following operation on a sequence of positive integers A = (A_0, \ldots, A_{N-1}).

  • Do the following for k=0, 1, \ldots, K-1 in this order.
    • Sort A_{k\bmod N}, A_{(k+1)\bmod N}, \ldots, A_{(k+M-1)\bmod N} in ascending order. That is, replace A_{(k+j)\bmod N} with x_j for each 0\leq j < M, where (x_0, \ldots, x_{M-1}) is the result of sorting A_{k\bmod N}, A_{(k+1)\bmod N}, \ldots, A_{(k+M-1)\bmod N} in ascending order.

You are given a permutation B = (B_0, \ldots, B_{N-1}) of the integers from 1 through N. Find the number of sequences A of positive integers that will equal B after performing the operation above, modulo 998244353.

Constraints

  • 2\leq N\leq 3\times 10^5
  • 2\leq M\leq N
  • 1\leq K\leq 10^9
  • 1\leq B_i\leq N
  • B_i\neq B_j if i\neq j.

Input

The input is given from Standard Input in the following format:

N M K
B_0 \ldots B_{N-1}

Print

Print the number of sequences A of positive integers that will equal B after performing the operation, modulo 998244353.


Sample Input 1

6 3 5
6 4 2 3 1 5

Sample Output 1

18

For instance, A = (4,1,5,6,2,3) satisfies the condition. On this A, the operation will proceed as follows.

  • The action for k=0 changes A to (1,4,5,6,2,3).
  • The action for k=1 changes A to (1,4,5,6,2,3).
  • The action for k=2 changes A to (1,4,2,5,6,3).
  • The action for k=3 changes A to (1,4,2,3,5,6).
  • The action for k=4 changes A to (6,4,2,3,1,5), which equals B.

Sample Input 2

6 3 5
6 5 4 3 2 1

Sample Output 2

0

No sequence A satisfies the condition.


Sample Input 3

20 20 149
13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12

Sample Output 3

401576539

All permutations of the integers from 1 through 20 satisfy the condition.

F - Rational Number System

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 900

問題文

r > 1 を有理数とし,pr の分子,qr の分母とします.つまり p, q は正整数で r = \frac{p}{q} かつ \gcd(p,q) = 1 が成り立つとします.

正整数 n\boldsymbol{r} 進法展開 を,次の条件をすべて満たす整数列 (a_1, \ldots, a_k) のことと定義します.

  • a_i0 以上 p-1 以下の整数
  • a_1\neq 0
  • n = \sum_{i=1}^k a_ir^{k-i}

任意の正整数 n が唯一の r 進法展開を持つことが証明できます.


正整数 p, q, N, L, R が与えられます.ここで,p, q1.01 \leq \frac{p}{q} を満たします.

N 以下の正整数全体を,\frac{p}{q} 進法展開の辞書順が小さい方から順に並べたとき,L, L+1, \ldots, R 番目に並ぶ正整数を答えてください.

なお,正整数の入出力には通常の 10 進法表記が用いられます.

数列の辞書順とは?

数列 A = (A_1, \ldots, A_{|A|})B = (B_1, \ldots, B_{|B|}) より辞書順で小さいとは,下記の 1. と 2. のどちらかが成り立つことを言います.ここで,|A|, |B| はそれぞれ A, B の長さを表します.

  1. |A|<|B| かつ (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}).
  2. ある整数 1\leq i\leq \min\{|A|,|B|\} が存在して,下記の 2 つがともに成り立つ.
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1}).
    • A_i < B_i.

制約

  • 1\leq p, q \leq 10^9
  • \gcd(p,q) = 1
  • 1.01 \leq \frac{p}{q}
  • 1\leq N\leq 10^{18}
  • 1\leq L\leq R\leq N
  • R - L + 1\leq 3\times 10^5

入力

入力は以下の形式で標準入力から与えられます.

p q N L R

出力

R-L+1 行出力してください.i 行目には,N 以下の正整数全体を,\frac{p}{q} 進法展開の辞書順が小さい方から順に並べたとき,L + i - 1 番目に並ぶ正整数を出力してください.

正整数の出力には 10 進法表記を用いてください.


入力例 1

3 1 9 1 9

出力例 1

1
3
9
4
5
2
6
7
8

9 以下の正整数の 3 進法展開は次の通りです.

1: (1),        2: (2),        3: (1, 0),
4: (1, 1),     5: (1, 2),     6: (2, 0),
7: (2, 1),     8: (2, 2),     9: (1, 0, 0).

入力例 2

3 2 9 1 9

出力例 2

1
2
3
4
6
9
7
8
5

9 以下の正整数の \frac32 進法展開は次の通りです.

1: (1),        2: (2),        3: (2, 0),     
4: (2, 1),     5: (2, 2),     6: (2, 1, 0),  
7: (2, 1, 1),  8: (2, 1, 2),  9: (2, 1, 0, 0).

例えば 6\frac32 進法展開が (2,1,0) であることは,2\cdot \bigl(\frac32\bigr)^2 + 1\cdot \frac32 + 0\cdot 1 = 6 から分かります.


入力例 3

3 2 9 3 8

出力例 3

3
4
6
9
7
8

入力例 2 の出力のうち 3 番目から 8 番目が答となります.


入力例 4

10 9 1000000000000000000 123456789123456789 123456789123456799

出力例 4

806324968563249278
806324968563249279
725692471706924344
725692471706924345
725692471706924346
725692471706924347
725692471706924348
725692471706924349
653123224536231907
653123224536231908
653123224536231909

Score : 900 points

Problem Statement

Let r > 1 be a rational number, and p and q be the numerator and denominator of r, respectively. That is, p and q are positive integers such that r = \frac{p}{q} and \gcd(p,q) = 1.

Let the base-\boldsymbol{r} expansion of a positive integer n be the integer sequence (a_1, \ldots, a_k) that satisfies all of the following conditions.

  • a_i is an integer between 0 and p-1.
  • a_1\neq 0.
  • n = \sum_{i=1}^k a_ir^{k-i}.

It can be proved that any positive integer n has a unique base-r expansion.


You are given positive integers p, q, N, L, and R. Here, p and q satisfy 1.01 \leq \frac{p}{q}.

Consider sorting all positive integers not greater than N in ascending lexicographical order of their base-\frac{p}{q} expansions. Find the L-th, (L+1)-th, \ldots, R-th positive integers in this list.

As usual, decimal notations are used in the input and output of positive integers.

What is lexicographical order on sequences?

A sequence A = (A_1, \ldots, A_{|A|}) is said to be lexicographically smaller than B = (B_1, \ldots, B_{|B|}) when 1. or 2. below is satisfied. Here, |A| and |B| denote the lengths of A and B, respectively.

  1. |A|<|B| and (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}).
  2. There is an integer 1\leq i\leq \min\{|A|,|B|\} that satisfies both of the following.
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1}).
    • A_i < B_i.

Constraints

  • 1\leq p, q \leq 10^9
  • \gcd(p,q) = 1
  • 1.01 \leq \frac{p}{q}
  • 1\leq N\leq 10^{18}
  • 1\leq L\leq R\leq N
  • R - L + 1\leq 3\times 10^5

Input

The input is given from Standard Input in the following format:

p q N L R

Output

Print R-L+1 lines. The i-th line should contain the (L + i - 1)-th positive integer in the list of all positive integers not greater than N sorted in ascending lexicographical order of their base-\frac{p}{q} expansions.

Use decimal notations to print positive integers.


Sample Input 1

3 1 9 1 9

Sample Output 1

1
3
9
4
5
2
6
7
8

The base-3 expansions of the positive integers not greater than 9 are as follows.

1: (1),        2: (2),        3: (1, 0),
4: (1, 1),     5: (1, 2),     6: (2, 0),
7: (2, 1),     8: (2, 2),     9: (1, 0, 0).

Sample Input 2

3 2 9 1 9

Sample Output 2

1
2
3
4
6
9
7
8
5

The base-\frac32 expansions of the positive integers not greater than 9 are as follows.

1: (1),        2: (2),        3: (2, 0),     
4: (2, 1),     5: (2, 2),     6: (2, 1, 0),  
7: (2, 1, 1),  8: (2, 1, 2),  9: (2, 1, 0, 0).

One can see that the base-\frac32 expansion of 6 is (2,1,0), for instance, from 2\cdot \bigl(\frac32\bigr)^2 + 1\cdot \frac32 + 0\cdot 1 = 6.


Sample Input 3

3 2 9 3 8

Sample Output 3

3
4
6
9
7
8

This is the 3-rd through 8-th lines of the output to Sample Input 2.


Sample Input 4

10 9 1000000000000000000 123456789123456789 123456789123456799

Sample Output 4

806324968563249278
806324968563249279
725692471706924344
725692471706924345
725692471706924346
725692471706924347
725692471706924348
725692471706924349
653123224536231907
653123224536231908
653123224536231909