A - AtCoder Language

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

配点 : 100 点

問題文

高橋君は、AtCoder 語を勉強しています。

高橋君は英単語に対して AtCoder 語の単語を対応させて覚えています。

高橋君は英語における red, blue, green は AtCoder 語ではそれぞれ SSS, FFF, MMM に対応していることを覚えており、他の単語は覚えていません。

英小文字からなる文字列 S が与えられます。S が高橋君が AtCoder 語との対応を覚えている英単語の表記と一致しているならば S に対応している AtCoder 語の単語を、そうでないならば文字列 Unknown を出力してください。

制約

  • S は長さ 1 以上 10 以下の英小文字からなる文字列

入力

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

S

出力

問題文中の指示に従い、文字列を出力せよ。


入力例 1

red

出力例 1

SSS

英語における red は AtCoder 語における SSS に対応します。


入力例 2

atcoder

出力例 2

Unknown

Score : 100 points

Problem Statement

Takahashi is learning AtCoderish Language.

He memorizes AtCoderish words corresponding to English words.

He knows that red, blue, and green in English respectively correspond to SSS, FFF, and MMM in AtCoderish, and he knows no other words.

You are given a string S consisting of lowercase English letters. If S equals an English word that Takahashi knows corresponds to an AtCoderish word, output the AtCoderish word corresponding to S; otherwise, output the string Unknown.

Constraints

  • S is a string of length between 1 and 10, inclusive, consisting of English letters.

Input

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

S

Output

Output a string according to the instructions in the problem statement.


Sample Input 1

red

Sample Output 1

SSS

red in English corresponds to SSS in AtCoderish.


Sample Input 2

atcoder

Sample Output 2

Unknown
B - Find Multiple

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

配点 : 100 点

問題文

A 以上 B 以下であるような C の倍数を、1 つ出力してください。

条件を満たす数が存在しない場合は -1 を出力してください。

制約

  • 1 \leq A \leq B \leq 1000
  • 1 \leq C \leq 1000
  • 入力は全て整数

入力

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

A B C

出力

答えを出力せよ。
条件を満たす数が存在しない場合は -1 を出力せよ。


入力例 1

123 456 100

出力例 1

200

300 や 400 も正解です。


入力例 2

630 940 314

出力例 2

-1

Score : 100 points

Problem Statement

Print a number between A and B (inclusive) that is a multiple of C.

If there is no such number, print -1.

Constraints

  • 1 \leq A \leq B \leq 1000
  • 1 \leq C \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the answer.
If there is no number with the desired property, print -1.


Sample Input 1

123 456 100

Sample Output 1

200

300 or 400 would also be accepted.


Sample Input 2

630 940 314

Sample Output 2

-1
C - Fibonacci Reversed

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

配点 : 200 点

問題文

正整数 x に対し、f(x) を以下のように定義します。

  • x を(先頭に余分な 0 をつけずに)十進表記して得られる文字列を s_x、s_x を反転して得られる文字列を \text{rev}(s_x) とおく。 f(x) の値は、\text{rev}(s_x) を整数の十進表記としてみなすことで得られる整数である。

例えば、x=13 のとき \text{rev}(s_x)=\ 31 より f(x)=31 であり、x=10 のとき \text{rev}(s_x)=\ 01 より f(x)=1 です。 特に、どのような正整数 x に対しても f(x) の値は正整数です。

正整数 X,Y が与えられます。 正整数列 A=(a_1,a_2,\dots,a_{10}) を以下のように定義します。

  • a_1 = X
  • a_2 = Y
  • a_i = f(a_{i-1}+a_{i-2})\ (i\geq 3)

a_{10} の値を求めてください。

制約

  • 1\leq X,Y \leq 10^5
  • 入力は全て整数

入力

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

X Y

出力

a_{10} の値を出力せよ。


入力例 1

1 1

出力例 1

415

A の各要素の値は以下の通りです。

  • a_1=1
  • a_2=1
  • a_3=2
  • a_4=3
  • a_5=5
  • a_6=8
  • a_7=31
  • a_8=93
  • a_9=421
  • a_{10}=415

よって 415 を出力します。


入力例 2

3 7

出力例 2

895

入力例 3

90701 90204

出力例 3

9560800101

Score : 200 points

Problem Statement

For a positive integer x, define f(x) as follows:

  • Let s_x be the string obtained by representing x in decimal notation (without leading zeros), and let \text{rev}(s_x) be the string obtained by reversing s_x. The value of f(x) is the integer obtained by interpreting \text{rev}(s_x) as a decimal representation of an integer.

For example, when x=13, we have \text{rev}(s_x)= 31, so f(x)=31; when x=10, we have \text{rev}(s_x)= 01, so f(x)=1. Particularly, for any positive integer x, the value of f(x) is a positive integer.

You are given positive integers X and Y. Define a sequence of positive integers A=(a_1,a_2,\dots,a_{10}) as follows:

  • a_1 = X
  • a_2 = Y
  • a_i = f(a_{i-1}+a_{i-2})\ (i\geq 3)

Find the value of a_{10}.

Constraints

  • 1\leq X,Y \leq 10^5
  • All input values are integers.

Input

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

X Y

Output

Print the value of a_{10}.


Sample Input 1

1 1

Sample Output 1

415

The values of the elements of A are as follows:

  • a_1=1
  • a_2=1
  • a_3=2
  • a_4=3
  • a_5=5
  • a_6=8
  • a_7=31
  • a_8=93
  • a_9=421
  • a_{10}=415

Thus, print 415.


Sample Input 2

3 7

Sample Output 2

895

Sample Input 3

90701 90204

Sample Output 3

9560800101
D - Maintain Multiple Sequences

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

配点 : 200 点

問題文

整数からなる数列が N 個あります。
i \, (1 \leq i \leq N) 番目の数列は L_i 項からなり、i 番目の数列の第 j \, (1 \leq j \leq L_i) 項 は a_{i, j} です。

Q 個のクエリが与えられます。k \, (1 \leq k \leq Q) 番目のクエリでは、整数 s_k, t_k が与えられるので、s_k 番目の数列の第 t_k 項を求めてください。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • 入力は全て整数

入力

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

出力

Q 行出力せよ。k \, (1 \leq k \leq Q) 行目には、k 番目のクエリに対する答えを出力せよ。


入力例 1

2 2
3 1 4 7
2 5 9
1 3
2 1

出力例 1

7
5

1 番目の数列は (1, 4, 7)、2 番目の数列は (5, 9) です。
それぞれのクエリに対する答えは次のようになります。

  • 1 番目の数列の第 3 項は 7 です。
  • 2 番目の数列の第 1 項は 5 です。

入力例 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

出力例 2

128
1
26535
901

Score : 200 points

Problem Statement

There are N sequences of integers.
The i-th (1 \leq i \leq N) sequence has L_i terms; the j-th (1 \leq j \leq L_i) term of the i-th sequence is a_{i, j}.

You are given Q queries. For the k-th (1 \leq k \leq Q) query, given integers s_k and t_k, find the t_k-th term of the s_k-th sequence.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • All values in the input are integers.

Input

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

Output

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


Sample Input 1

2 2
3 1 4 7
2 5 9
1 3
2 1

Sample Output 1

7
5

The 1-st sequence is (1, 4, 7) and the 2-nd is (5, 9).
The answer to each query is as follows:

  • The 3-rd term of the 1-st sequence is 7.
  • The 1-st term of the 2-nd sequence is 5.

Sample Input 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

Sample Output 2

128
1
26535
901
E - Four Variables

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

配点 : 300 点

問題文

正整数 N が与えられます。
正整数の組 (A,B,C,D) であって、AB + CD = N を満たすものの個数を求めてください。

なお、本問の制約の下、答えが 9 \times 10^{18} 以下であることが証明できます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

8

(A,B,C,D) として以下の 8 個が考えられます。

  • (A,B,C,D)=(1,1,1,3)
  • (A,B,C,D)=(1,1,3,1)
  • (A,B,C,D)=(1,2,1,2)
  • (A,B,C,D)=(1,2,2,1)
  • (A,B,C,D)=(1,3,1,1)
  • (A,B,C,D)=(2,1,1,2)
  • (A,B,C,D)=(2,1,2,1)
  • (A,B,C,D)=(3,1,1,1)

入力例 2

292

出力例 2

10886

入力例 3

19876

出力例 3

2219958

Score : 300 points

Problem Statement

You are given a positive integer N.
Find the number of quadruples of positive integers (A,B,C,D) such that AB + CD = N.

Under the constraints of this problem, it can be proved that the answer is at most 9 \times 10^{18}.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

8

Here are the eight desired quadruples.

  • (A,B,C,D)=(1,1,1,3)
  • (A,B,C,D)=(1,1,3,1)
  • (A,B,C,D)=(1,2,1,2)
  • (A,B,C,D)=(1,2,2,1)
  • (A,B,C,D)=(1,3,1,1)
  • (A,B,C,D)=(2,1,1,2)
  • (A,B,C,D)=(2,1,2,1)
  • (A,B,C,D)=(3,1,1,1)

Sample Input 2

292

Sample Output 2

10886

Sample Input 3

19876

Sample Output 3

2219958
F - Security 2

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

配点 : 300 点

問題文

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

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

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

たとえば、t が 1984 のときにボタン A を押すと t は 19840 に変化し、さらにボタン B を押すと t は 20951 に変化します。

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

制約

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

入力

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

S

出力

答えを出力せよ。


入力例 1

21

出力例 1

4

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

  1. ボタン A を押す。t は 0 になる。
  2. ボタン B を押す。t は 1 になる。
  3. ボタン A を押す。t は 10 になる。
  4. ボタン B を押す。t は 21 になる。

4 回より少ない回数ボタンを押して t を 21 にすることはできないので、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
G - Cycle

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

配点 : 400 点

問題文

頂点に 1 から N の番号がついた N 頂点 M 辺の単純有向グラフがあります。i 番目の辺 (1 \leq i \leq M) は頂点 a_i から頂点 b_i へ伸びる辺です。
頂点 1 を含む閉路が存在するか判定して、存在する場合はそのような閉路のうち辺数が最小の閉路の辺数を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq \min \left( \frac{N(N-1)}{2}, 2 \times 10^5 \right)
  • 1 \leq a_i \leq N
  • 1 \leq b_i \leq N
  • a_i \neq b_i
  • i \neq j ならば (a_i, b_i) \neq (a_j, b_j) かつ (a_i, b_i) \neq (b_j, a_j)
  • 入力される値は全て整数

入力

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

N M
a_1 b_1
a_2 b_2
\vdots
a_M b_M

出力

頂点 1 を含む閉路が存在する場合は、そのような閉路のうち辺数が最小の閉路の辺数を出力せよ。そうでない場合は -1 を出力せよ。


入力例 1

3 3
1 2
2 3
3 1

出力例 1

3

頂点 1 \to 頂点 2 \to 頂点 3 \to 頂点 1 は辺数が 3 の閉路で、これが頂点 1 を含む唯一の閉路です。


入力例 2

3 2
1 2
2 3

出力例 2

-1

入力例 3

6 9
6 1
1 5
2 6
2 1
3 6
4 2
6 4
3 5
5 4

出力例 3

4

Score : 400 points

Problem Statement

There is a simple directed graph with N vertices numbered from 1 to N and M edges. The i-th edge (1 \leq i \leq M) is a directed edge from vertex a_i to vertex b_i.
Determine whether there exists a cycle that contains vertex 1, and if it exists, find the minimum number of edges among such cycles.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq \min \left( \frac{N(N-1)}{2},\ 2 \times 10^5 \right)
  • 1 \leq a_i \leq N
  • 1 \leq b_i \leq N
  • a_i \neq b_i
  • (a_i, b_i) \neq (a_j, b_j) and (a_i, b_i) \neq (b_j, a_j), if i \neq j.
  • All input values are integers.

Input

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

N M
a_1 b_1
a_2 b_2
\vdots
a_M b_M

Output

If there exists a cycle that contains vertex 1, print the minimum number of edges among such cycles. Otherwise, print -1.


Sample Input 1

3 3
1 2
2 3
3 1

Sample Output 1

3

Vertex 1 \to vertex 2 \to vertex 3 \to vertex 1 is a cycle with three edges, and this is the only cycle that contains vertex 1.


Sample Input 2

3 2
1 2
2 3

Sample Output 2

-1

Sample Input 3

6 9
6 1
1 5
2 6
2 1
3 6
4 2
6 4
3 5
5 4

Sample Output 3

4
H - Digit Sum Divisible 2

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

配点 : 500 点

問題文

正整数 n の桁和を、n を 10 進法で表したときの各桁の和と定義します。例えば 2025 の桁和は 2 + 0 + 2 + 5 = 9 です。
正整数 n が n の桁和で割り切れる時、n を 良い整数 と呼びます。例えば 2025 はその桁和である 9 で割り切れるので良い整数です。
正整数の組 (a, a+1) であって a と a+1 が共に良い整数であるものを 双子の良い整数 と呼びます。例えば (2024, 2025) は双子の良い整数です。

正整数 N が与えられます。N \leq a かつ a + 1 \leq 2N であるような双子の良い整数 (a, a + 1) を発見してください。そのような (a, a + 1) が存在しない場合はそのことを報告してください。

制約

  • N は 1 以上 10^{100000} 未満の整数

入力

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

N

出力

問題文の条件を満たす (a, a+1) が存在する場合は a を、存在しない場合は -1 を出力せよ。a は leading-zeros を省略した表現で出力する必要がある点に注意せよ。
条件を満たす (a, a+1) が複数存在する場合はどれを出力してもよい。


入力例 1

5

出力例 1

8

(8, 9) は問題文の条件を満たす双子の良い整数です。他にも (5, 6), (6, 7), (7, 8), (9, 10) が条件を満たします。


入力例 2

21

出力例 2

-1

問題文の条件を満たす双子の良い整数は存在しません。


入力例 3

1234

出力例 3

2024

(2024, 2025) は問題文の条件を満たす双子の良い整数です。


入力例 4

1234567890123456789012345678901234567890

出力例 4

1548651852734633803438094164372911259190

Score : 500 points

Problem Statement

The digit sum of a positive integer n is defined as the sum of its digits when n is written in decimal. For example, the digit sum of 2025 is 2 + 0 + 2 + 5 = 9.
A positive integer n is called a good integer if it is divisible by its digit sum. For example, 2025 is a good integer because it is divisible by its digit sum of 9.
A pair of positive integers (a, a+1) is called twin good integers if both a and a+1 are good integers. For example, (2024, 2025) is twin good integers.

You are given a positive integer N. Find a pair of twin good integers (a, a + 1) such that N \leq a and a + 1 \leq 2N. If no such pair exists, report that fact.

Constraints

  • N is an integer at least 1 and less than 10^{100000}.

Input

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

N

Output

If there exists such a pair (a, a+1), output a. Otherwise, output -1. Do not print leading zeros.
If there are multiple such pairs, you may print any.


Sample Input 1

5

Sample Output 1

8

(8, 9) is a valid pair of twin good integers satisfying the conditions. Other examples include (5, 6), (6, 7), (7, 8), (9, 10).


Sample Input 2

21

Sample Output 2

-1

No pair of twin good integers satisfies the conditions.


Sample Input 3

1234

Sample Output 3

2024

(2024, 2025) is a valid pair of twin good integers.


Sample Input 4

1234567890123456789012345678901234567890

Sample Output 4

1548651852734633803438094164372911259190
I - Common Prefixes

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

配点 : 500 点

問題文

2 つの文字列 X,Y に対して、その類似度 f(X,Y) を、X と Y を先頭から見て一致している文字数とします。
例えば abc と axbc の類似度は 1 、aaa と aaaa の類似度は 3 です。

長さ N の文字列 S が与えられます。S の i 文字目以降からなる文字列を S_i とします。k=1,\ldots,N のそれぞれについて、f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N) を求めてください。

制約

  • 1 \leq N \leq 10^6
  • S は英小文字のみからなる長さ N の文字列

入力

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

N
S

出力

N 行出力せよ。

i 行目には k=i の問題の答えを出力せよ。


入力例 1

3
abb

出力例 1

3
3
2

S_1,S_2,S_3 はそれぞれ abb, bb, b です。

  • k=1 のとき f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3
  • k=2 のとき f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3
  • k=3 のとき f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2

入力例 2

11
mississippi

出力例 2

11
16
14
12
13
11
9
7
4
3
4

Score : 500 points

Problem Statement

Let the similarity f(X,Y) between two strings X and Y be the length of their longest common prefix.
For example, the similarity between abc and axbc is 1, and the similarity between aaa and aaaa is 3.

You are given a string S of length N. Let S_i be the suffix of S beginning with the i-th character of S. For each k=1,\ldots,N, find f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N).

Constraints

  • 1 \leq N \leq 10^6
  • S is a string of length N consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print N lines.

The i-th line should contain the answer for k=i.


Sample Input 1

3
abb

Sample Output 1

3
3
2

S_1 is abb, S_2 is bb, and S_3 is b.

  • For k=1: f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3.
  • For k=2: f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3.
  • For k=3: f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2.

Sample Input 2

11
mississippi

Sample Output 2

11
16
14
12
13
11
9
7
4
3
4