C - Garbage Collection

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

配点 : 200

問題文

AtCoder 市では、N 種類のゴミを定期的に収集しています。i\;(=1,2,\dots,N) 種類目のゴミは、日付を q_i で割ったあまりが r_i の日に収集されます。

Q 個の質問に答えてください。j\;(=1,2,\dots,Q) 番目の質問では、d_j 日に t_j 種類目のゴミが出たときに、次にそれが収集される日を答えてください。

ただし、i 種類目のゴミが出た日が、 i 種類目のゴミが回収される日であった場合、そのゴミは同じ日に収集されるとします。

制約

  • 1 \leq N \leq 100
  • 0 \leq r_i < q_i \leq 10^9
  • 1 \leq Q \leq 100
  • 1 \leq t_j \leq N
  • 1 \leq d_j \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
q_1 r_1
q_2 r_2
\vdots
q_N r_N
Q
t_1 d_1
t_2 d_2
\vdots
t_Q d_Q

出力

Q 行出力せよ。j\;(1\leq j \leq Q) 行目には、j 番目の質問に対する答えを出力せよ。


入力例 1

2
7 3
4 2
5
1 1
1 3
1 4
1 15
2 7

出力例 1

3
3
10
17
10
  • 1 番目の質問:1 種類目のゴミが 1 日以降に初めて回収されるのは 3 日です。
  • 2 番目の質問:1 種類目のゴミが 3 日以降に初めて回収されるのは 3 日です。
  • 3 番目の質問:1 種類目のゴミが 4 日以降に初めて回収されるのは 10 日です。

Score : 200 points

Problem Statement

In AtCoder City, N types of garbage are collected regularly. The i-th type of garbage (i=1,2,\dots,N) is collected on days when the date modulo q_i equals r_i.

Answer Q queries. In the j-th query (j=1,2,\dots,Q), given that the t_j-th type of garbage is put out on day d_j, answer the next day on which it will be collected.

Here, if the i-th type of garbage is put out on a day when that type of garbage is collected, then the garbage will be collected on the same day.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq r_i < q_i \leq 10^9
  • 1 \leq Q \leq 100
  • 1 \leq t_j \leq N
  • 1 \leq d_j \leq 10^9
  • All input values are integers.

Input

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

N
q_1 r_1
q_2 r_2
\vdots
q_N r_N
Q
t_1 d_1
t_2 d_2
\vdots
t_Q d_Q

Output

Print Q lines. The j-th line (1\leq j \leq Q) should contain the answer to the j-th query.


Sample Input 1

2
7 3
4 2
5
1 1
1 3
1 4
1 15
2 7

Sample Output 1

3
3
10
17
10
  • 1st query: The 1st type of garbage is collected on day 3 for the first time after day 1.
  • 2nd query: The 1st type of garbage is collected on day 3 for the first time after day 3.
  • 3rd query: The 1st type of garbage is collected on day 10 for the first time after day 4.
D - Vertical Writing

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

配点 : 200

問題文

横書きの文章が与えられます。縦書きに直してください。空白を * で埋めてください。

N 個の、英小文字からなる文字列 S_1,S_2,\dots,S_N が与えられます。これらの文字列の長さの最大値を M とします。

以下の条件を満たす M 個の文字列 T_1,T_2,\dots,T_M を出力してください。

  • T_i は英小文字および * からなる
  • T_i の末尾は * でない
  • 1 \leq i \leq N について、次が成り立つ
    • 1 \leq j \leq |S_i| について、T_jN-i+1 文字目が存在し、T_1,T_2,\dots,T_{|S_i|} それぞれの N-i+1 文字目をこの順に連結したものは S_i と一致する
    • |S_i| + 1 \leq j \leq M について、T_jN-i+1 文字目は存在しないか、 * である

ただし、|S_i| で文字列 S_i の長さを表します。

制約

  • N1 以上 100 以下の整数
  • S_i は長さ 1 以上 100 以下の英小文字からなる文字列

入力

入力は以下の形式で標準入力から与えられる。

N
S_1
S_2
\vdots
S_N

出力

答えを以下の形式で出力せよ。

T_1
T_2
\vdots
T_M

入力例 1

3
abc
de
fghi

出力例 1

fda
geb
h*c
i

T_32 文字目を * とすることで、 c が正しい位置に来ます。 T_42,3 文字目を * とした場合、T_4 の末尾が * となり、条件を満たしません。


入力例 2

3
atcoder
beginner
contest

出力例 2

cba
oet
ngc
tio
end
sne
ter
*r

Score : 200 points

Problem Statement

You are given a horizontally written text. Convert it to vertical writing, filling spaces with *.

You are given N strings S_1, S_2, \dots, S_N consisting of lowercase English letters. Let M be the maximum length of these strings.

Print M strings T_1, T_2, \dots, T_M that satisfy the following conditions:

  • Each T_i consists of lowercase English letters and *.
  • Each T_i does not end with *.
  • For each 1 \leq i \leq N, the following holds:
    • For each 1 \leq j \leq |S_i|, the (N-i+1)-th character of T_j exists, and the concatenation of the (N-i+1)-th characters of T_1, T_2, \dots, T_{|S_i|} in this order equals S_i.
    • For each |S_i| + 1 \leq j \leq M, the (N-i+1)-th character of T_j either does not exist or is *.

Here, |S_i| denotes the length of the string S_i.

Constraints

  • N is an integer between 1 and 100, inclusive.
  • Each S_i is a string of lowercase English letters with length between 1 and 100, inclusive.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer in the following format:

T_1
T_2
\vdots
T_M

Sample Input 1

3
abc
de
fghi

Sample Output 1

fda
geb
h*c
i

Placing * as the 2nd character of T_3 puts the c in the correct position. On the other hand, placing * as the 2nd and 3rd characters of T_4 would make T_4 end with *, which violates the condition.


Sample Input 2

3
atcoder
beginner
contest

Sample Output 2

cba
oet
ngc
tio
end
sne
ter
*r
E - Security 2

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

配点 : 300

問題文

AtCoder 社の入口には特殊なパスコード入力装置があり、 この装置は文字列を 1 つ表示する画面と 2 つのボタンからなります。

画面に表示される文字列を t とします。 t ははじめ空文字列であり、ボタンを押すことで t に以下の変化が起きます。

  • ボタン A を押すと、t の末尾に 0 が追加される。
  • ボタン B を押すと、t に含まれるすべての数字が、それぞれ次の数字に置き換わる。ここで、0 から 8 までの数字については次の数字は 1 大きな数を表す数字とし、9 の次の数字は 0 とする。

たとえば、t1984 のときにボタン A を押すと t19840 に変化し、さらにボタン B を押すと t20951 に変化します。

文字列 S が与えられます。t が空文字列である状態からボタンを 0 回以上押して tS に一致させるとき、ボタンを最少で何回押す必要があるかを求めてください。

制約

  • S0, 1, 2, 3, 4, 5, 6, 7, 8, 9 からなる文字列
  • 1 \leq |S| \leq 5 \times 10^5 (|S|S の長さをあらわす)

入力

入力は以下の形式で標準入力から与えられる。

S

出力

答えを出力せよ。


入力例 1

21

出力例 1

4

以下の順にボタンを押せば、t21 になります。

  1. ボタン A を押す。t0 になる。
  2. ボタン B を押す。t1 になる。
  3. ボタン A を押す。t10 になる。
  4. ボタン B を押す。t21 になる。

4 回より少ない回数ボタンを押して t21 にすることはできないので、4 を出力します。


入力例 2

407

出力例 2

17

入力例 3

2025524202552420255242025524

出力例 3

150

Score : 300 points

Problem Statement

At the entrance of AtCoder Inc., there is a special pass-code input device. The device consists of a screen displaying one string, and two buttons.

Let t be the string shown on the screen. Initially, t is the empty string. Pressing a button changes t as follows:

  • Pressing button A appends 0 to the end of t.
  • Pressing button B replaces every digit in t with the next digit: for digits 0 through 8 the next digit is the one whose value is larger by 1; the next digit after 9 is 0.

For example, if t is 1984 and button A is pressed, t becomes 19840; if button B is then pressed, t becomes 20951.

You are given a string S. Starting from the empty string, press the buttons zero or more times until t coincides with S. Find the minimum number of button presses required.

Constraints

  • S is a string consisting of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
  • 1 \le |S| \le 5 \times 10^{5}, where |S| is the length of S.

Input

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

S

Output

Output the answer.


Sample Input 1

21

Sample Output 1

4

The following sequence of presses makes t equal to 21.

  1. Press button A. t becomes 0.
  2. Press button B. t becomes 1.
  3. Press button A. t becomes 10.
  4. Press button B. t becomes 21.

It is impossible to obtain 21 with fewer than four presses, so output 4.


Sample Input 2

407

Sample Output 2

17

Sample Input 3

2025524202552420255242025524

Sample Output 3

150
F - 1 2 1 3 1 2 1

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

配点 : 300

問題文

S_n を次のように定義します。

  • S_11 つの 1 からなる長さ 1 の列である。
  • S_n (n2 以上の整数) は S_{n-1}, n, S_{n-1} をこの順につなげた列である。

たとえば S_2,S_3 は次のような列です。

  • S_2S_1, 2, S_1 をこの順につなげた列なので 1,2,1 である。
  • S_3S_2, 3, S_2 をこの順につなげた列なので 1,2,1,3,1,2,1 である。

N が与えられるので、列 S_N をすべて出力してください。

制約

  • N は整数
  • 1 \leq N \leq 16

入力

入力は以下の形式で標準入力から与えられる。

N

出力

S_N を空白区切りで出力せよ。


入力例 1

2

出力例 1

1 2 1

問題文の説明にある通り、S_21,2,1 となります。


入力例 2

1

出力例 2

1

入力例 3

4

出力例 3

1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

S_4S_3,4,S_3 をこの順につなげた列です。

Score : 300 points

Problem Statement

We define sequences S_n as follows.

  • S_1 is a sequence of length 1 containing a single 1.
  • S_n (n is an integer greater than or equal to 2) is a sequence obtained by concatenating S_{n-1}, n, S_{n-1} in this order.

For example, S_2 and S_3 is defined as follows.

  • S_2 is a concatenation of S_1, 2, and S_1, in this order, so it is 1,2,1.
  • S_3 is a concatenation of S_2, 3, and S_2, in this order, so it is 1,2,1,3,1,2,1.

Given N, print the entire sequence S_N.

Constraints

  • N is an integer.
  • 1 \leq N \leq 16

Input

Input is given from Standard Input in the following format:

N

Output

Print S_N, with spaces in between.


Sample Input 1

2

Sample Output 1

1 2 1

As described in the Problem Statement, S_2 is 1,2,1.


Sample Input 2

1

Sample Output 2

1

Sample Input 3

4

Sample Output 3

1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
  • S_4 is a concatenation of S_3, 4, and S_3, in this order.
G - Coprime 2

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

配点 : 400

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられるので、以下の条件を満たす 1 以上 M 以下の整数 k を全て求めてください。

  • 全ての 1 \le i \le N を満たす整数 i について、 \gcd(A_i,k)=1 である。

制約

  • 入力は全て整数
  • 1 \le N,M \le 10^5
  • 1 \le A_i \le 10^5

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 A_2 \dots A_N

出力

1 行目に、出力する整数の数 x を出力せよ。
続く x 行に、答えとなる整数を小さい方から順に 1 行に 1 つずつ出力せよ。


入力例 1

3 12
6 1 5

出力例 1

3
1
7
11

例えば、 7\gcd(6,7)=1,\gcd(1,7)=1,\gcd(5,7)=1 を満たすので答えとなる整数の集合に含まれます。
一方、 9\gcd(6,9)=3 となるため、答えとなる整数の集合に含まれません。
条件を満たす 1 以上 12 以下の整数は 1,7,113 つです。これらを小さい方から出力することに注意してください。

Score : 400 points

Problem Statement

Given a sequence of N positive integers A=(A_1,A_2,\dots,A_N), find every integer k between 1 and M (inclusive) that satisfies the following condition:

  • \gcd(A_i,k)=1 for every integer i such that 1 \le i \le N.

Constraints

  • All values in input are integers.
  • 1 \le N,M \le 10^5
  • 1 \le A_i \le 10^5

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

In the first line, print x: the number of integers satisfying the requirement.
In the following x lines, print the integers satisfying the requirement, in ascending order, each in its own line.


Sample Input 1

3 12
6 1 5

Sample Output 1

3
1
7
11

For example, 7 has the properties \gcd(6,7)=1,\gcd(1,7)=1,\gcd(5,7)=1, so it is included in the set of integers satisfying the requirement.
On the other hand, 9 has the property \gcd(6,9)=3, so it is not included in that set.
We have three integers between 1 and 12 that satisfy the condition: 1, 7, and 11. Be sure to print them in ascending order.