A - Digit Machine

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

1 桁の数字が表示される画面と、ボタンからなる機械があります。

画面に数字 k が表示されているとき、ボタンを 1 回押すと画面の数字が a_k に変わります。

0 が表示されている状態からボタンを 3 回押すと、画面には何が表示されますか?

制約

  • 0\leq a_i \leq 9
  • 入力は全て整数である

入力

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

a_0 a_1 \dots a_9

出力

答えを出力せよ。


入力例 1

9 0 1 2 3 4 5 6 7 8

出力例 1

7

画面に表示される数字は、0 \rightarrow 9 \rightarrow 8 \rightarrow 7 と変化します。


入力例 2

4 8 8 8 0 8 8 8 8 8

出力例 2

4

画面に表示される数字は、0 \rightarrow 4 \rightarrow 0 \rightarrow 4 と変化します。


入力例 3

0 0 0 0 0 0 0 0 0 0

出力例 3

0

Score : 100 points

Problem Statement

There is a device with a screen that shows a single-digit number, and a button.

When the screen is showing a number k, pressing the button once changes the number on the screen to a_k.

The device currently shows 0. After pressing the button 3 times, what will be shown on the screen?

Constraints

  • 0\leq a_i \leq 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a_0 a_1 \dots a_9

Output

Print the answer.


Sample Input 1

9 0 1 2 3 4 5 6 7 8

Sample Output 1

7

The number on the screen transitions as 0 \rightarrow 9 \rightarrow 8 \rightarrow 7.


Sample Input 2

4 8 8 8 0 8 8 8 8 8

Sample Output 2

4

The number on the screen transitions as 0 \rightarrow 4 \rightarrow 0 \rightarrow 4.


Sample Input 3

0 0 0 0 0 0 0 0 0 0

Sample Output 3

0
B - Similar String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

二つの文字 xy は以下の 3 つの条件のうちどれか 1 つを満たすとき、似た文字と呼ばれます。

  • xy は同じ文字
  • xy の片方が 1 で、もう片方が l
  • xy の片方が 0 で、もう片方が o

また、長さ N の文字列 ST は以下の条件を満たすとき、似た文字列と呼ばれます。

  • 任意の i\ (1\leq i\leq N) について、 Si 番目の文字と Ti 番目の文字は似た文字

英小文字及び数字からなる長さ N の文字列 S,T が与えられます。 ST が似た文字列か判定してください。

制約

  • N1 以上 100 以下の整数
  • S,T は英小文字及び数字からなる長さ N の文字列

入力

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

N
S
T

出力

ST が似た文字列の場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3
l0w
1ow

出力例 1

Yes

S1 文字目は lで、T1 文字目は 1です。これらは似た文字です。

S2 文字目は 0で、T2 文字目は oです。これらは似た文字です。

S3 文字目は wで、T3 文字目は wです。これらは似た文字です。

よって ST は似た文字列です。


入力例 2

3
abc
arc

出力例 2

No

S2 文字目は bで、T2 文字目は rです。これらは似た文字ではありません。

よって ST は似た文字列ではありません。


入力例 3

4
nok0
n0ko

出力例 3

Yes

Score : 100 points

Problem Statement

Two characters x and y are called similar characters if and only if one of the following conditions is satisfied:

  • x and y are the same character.
  • One of x and y is 1 and the other is l.
  • One of x and y is 0 and the other is o.

Two strings S and T, each of length N, are called similar strings if and only if:

  • for all i\ (1\leq i\leq N), the i-th character of S and the i-th character of T are similar characters.

Given two length-N strings S and T consisting of lowercase English letters and digits, determine if S and T are similar strings.

Constraints

  • N is an integer between 1 and 100.
  • Each of S and T is a string of length N consisting of lowercase English letters and digits.

Input

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

N
S
T

Output

Print Yes if S and T are similar strings, and No otherwise.


Sample Input 1

3
l0w
1ow

Sample Output 1

Yes

The 1-st character of S is l, and the 1-st character of T is 1. These are similar characters.

The 2-nd character of S is 0, and the 2-nd character of T is o. These are similar characters.

The 3-rd character of S is w, and the 3-rd character of T is w. These are similar characters.

Thus, S and T are similar strings.


Sample Input 2

3
abc
arc

Sample Output 2

No

The 2-nd character of S is b, and the 2-nd character of T is r. These are not similar characters.

Thus, S and T are not similar strings.


Sample Input 3

4
nok0
n0ko

Sample Output 3

Yes
C - MissingNo.

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

ナオヒロ君は N+1 個の連続する整数を 1 個ずつ持っていましたが、そのうち 1 個をなくしてしまいました。

残っている N 個の整数が順不同で A_1,\ldots,A_N として与えられるので、なくした整数を求めてください。

なお、なくした整数が一意に定まるような入力のみが与えられます。

制約

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 1000
  • 入力は全て整数である
  • なくした整数は一意に定まる

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
2 3 5

出力例 1

4

ナオヒロ君は初め 2,3,4,54 個の整数を持っており、4 をなくし、2,3,5 が残っていました。

なくした整数である 4 を出力します。


入力例 2

8
3 1 4 5 9 2 6 8

出力例 2

7

入力例 3

16
152 153 154 147 148 149 158 159 160 155 156 157 144 145 146 150

出力例 3

151

Score : 200 points

Problem Statement

Naohiro had N+1 consecutive integers, one of each, but he lost one of them.

The remaining N integers are given in arbitrary order as A_1,\ldots,A_N. Find the lost integer.

The given input guarantees that the lost integer is uniquely determined.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 1000
  • All input values are integers.
  • The lost integer is uniquely determined.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

3
2 3 5

Sample Output 1

4

Naohiro originally had four integers, 2,3,4,5, then lost 4, and now has 2,3,5.

Print the lost integer, 4.


Sample Input 2

8
3 1 4 5 9 2 6 8

Sample Output 2

7

Sample Input 3

16
152 153 154 147 148 149 158 159 160 155 156 157 144 145 146 150

Sample Output 3

151
D - Qualification Contest

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の人があるコンテストに参加し、i 位の人のハンドルネームは S_i でした。
上位 K 人のハンドルネームを辞書順に出力してください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. ST のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、 ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • 1 \leq K \leq N \leq 100
  • K, N は整数
  • S_i は英小文字からなる長さ 10 以下の文字列
  • i \neq j ならば S_i \neq S_j

入力

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

N K
S_1
S_2
\vdots
S_N

出力

答えを改行区切りで出力せよ。


入力例 1

5 3
abc
aaaaa
xyz
a
def

出力例 1

aaaaa
abc
xyz

このコンテストには 5 人が参加し、1 位の人のハンドルネームは abc2 位の人のハンドルネームは aaaaa3 位の人のハンドルネームは xyz4 位の人のハンドルネームは a5 位の人のハンドルネームは def でした。

上位 3 人のハンドルネームは abcaaaaaxyz であるため、これを辞書順に並べ替えて aaaaaabcxyz の順に出力します。


入力例 2

4 4
z
zyx
zzz
rbg

出力例 2

rbg
z
zyx
zzz

入力例 3

3 1
abc
arc
agc

出力例 3

abc

Score : 200 points

Problem Statement

There were N participants in a contest. The participant ranked i-th had the nickname S_i.
Print the nicknames of the top K participants in lexicographical order.

What is lexicographical order?

Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings S and T.

Let S_i denote the i-th character of a string S. We write S \lt T if S is lexicographically smaller than T, and S \gt T if S is larger.

  1. Let L be the length of the shorter of S and T. For i=1,2,\dots,L, check whether S_i equals T_i.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, we get S \lt T; if S_j is larger, we get S \gt T.
  3. If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, we get S \lt T; if S is longer, we get S \gt T.

Constraints

  • 1 \leq K \leq N \leq 100
  • K and N are integers.
  • S_i is a string of length 10 consisting of lowercase English letters.
  • S_i \neq S_j if i \neq j.

Input

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

N K
S_1
S_2
\vdots
S_N

Output

Print the nicknames, separated by newlines.


Sample Input 1

5 3
abc
aaaaa
xyz
a
def

Sample Output 1

aaaaa
abc
xyz

This contest had five participants. The participants ranked first, second, third, fourth, and fifth had the nicknames abc, aaaaa, xyz, a, and def, respectively.

The nicknames of the top three participants were abc, aaaaa, xyz, so print these in lexicographical order: aaaaa, abc, xyz.


Sample Input 2

4 4
z
zyx
zzz
rbg

Sample Output 2

rbg
z
zyx
zzz

Sample Input 3

3 1
abc
arc
agc

Sample Output 3

abc
E - Find it!

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

N 頂点 N 辺の有向グラフがあります。
i 番目の辺は頂点 i から 頂点 A_i に伸びます。 ( i \neq A_i であることは制約より保証されます )
同一頂点を複数回含まない有向閉路をひとつ求めてください。
なお、この問題の制約下で答えが存在することが示せます。

注釈

この問題では、有向閉路とは以下の条件を全て満たす頂点の列 B=(B_1,B_2,\dots,B_M) であるとします。

  • M \ge 2
  • B_i から B_{i+1} に辺が伸びている。 ( 1 \le i \le M-1 )
  • B_M から B_1 に辺が伸びている。
  • i \neq j ならば B_i \neq B_j

制約

  • 入力は全て整数
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

入力

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

N
A_1 A_2 \dots A_N

出力

以下の形式で出力せよ。

M
B_1 B_2 \dots B_M

M は出力する有向閉路の頂点数であり、 B_i は有向閉路の i 番目の頂点である。
出力は以下の条件を満たす必要がある。

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

答えとして考えられるものが複数ある場合、どれを出力しても正解となる。


入力例 1

7
6 7 2 1 3 4 5

出力例 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。

他の正解となる出力の例は以下の通りです。

4
2 7 5 3
3
4 1 6

グラフが連結であるとは限らないことに注意してください。


入力例 2

2
2 1

出力例 2

2
1 2

1 \rightarrow 22 \rightarrow 1 の辺が双方存在するケースです。
この場合、 1 \rightarrow 2 \rightarrow 1 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。
図中 1 \leftrightarrow 21 \rightarrow 22 \rightarrow 1 の辺が双方あることを表現しています。


入力例 3

8
3 7 4 7 3 3 8 2

出力例 3

3
2 7 8

この入力に対応するグラフは以下の通りです。

Score : 350 points

Problem Statement

There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex A_i. (The constraints guarantee that i \neq A_i.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.

Notes

The sequence of vertices B = (B_1, B_2, \dots, B_M) is called a directed cycle when all of the following conditions are satisfied:

  • M \geq 2
  • The edge from vertex B_i to vertex B_{i+1} exists. (1 \leq i \leq M-1)
  • The edge from vertex B_M to vertex B_1 exists.
  • If i \neq j, then B_i \neq B_j.

Constraints

  • All input values are integers.
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

Input

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

N
A_1 A_2 \dots A_N

Output

Print a solution in the following format:

M
B_1 B_2 \dots B_M

M is the number of vertices, and B_i is the i-th vertex in the directed cycle.
The following conditions must be satisfied:

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

If multiple solutions exist, any of them will be accepted.


Sample Input 1

7
6 7 2 1 3 4 5

Sample Output 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 is indeed a directed cycle.

Here is the graph corresponding to this input:

Here are other acceptable outputs:

4
2 7 5 3
3
4 1 6

Note that the graph may not be connected.


Sample Input 2

2
2 1

Sample Output 2

2
1 2

This case contains both of the edges 1 \rightarrow 2 and 2 \rightarrow 1.
In this case, 1 \rightarrow 2 \rightarrow 1 is indeed a directed cycle.

Here is the graph corresponding to this input, where 1 \leftrightarrow 2 represents the existence of both 1 \rightarrow 2 and 2 \rightarrow 1:


Sample Input 3

8
3 7 4 7 3 3 8 2

Sample Output 3

3
2 7 8

Here is the graph corresponding to this input:

F - Route Map

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder 鉄道のとある路線には N 個の駅が存在し、始点から終点に向かって i \, (1 \leq i \leq N) 番目の駅の名前は S_i です。

普通列車は全ての駅に止まりますが、急行列車は全ての駅に止まるとは限りません。具体的には、急行列車は M \, (M \leq N) 個の駅にのみ止まり、j \, (1 \leq j \leq M) 番目に止まる駅の名前は T_j です。
ただし、T_1 = S_1 かつ T_M = S_N、すなわち急行列車は始点と終点の両方に止まることが保証されます。

N 個の駅それぞれについて、その駅に急行列車が止まるかどうか判定してください。

制約

  • 2 \leq M \leq N \leq 10^5
  • N, M は整数
  • S_i \, (1 \leq i \leq N) は英小文字のみからなる 1 文字以上 10 文字以下の文字列
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 かつ T_M = S_N
  • (T_1, \dots, T_M)(S_1, \dots, S_N) から 0 個以上の文字列を選んで取り除き、残った文字列を元の順序で並べることで得られる

入力

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

N M
S_1 \ldots S_N
T_1 \ldots T_M

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、始点から終点に向かって i 番目の駅に急行列車が止まるなら Yes、そうでないなら No と出力せよ。


入力例 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

出力例 1

Yes
No
Yes
No
Yes

入力例 2

7 7
a t c o d e r
a t c o d e r

出力例 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

急行列車が全ての駅に止まることもあります。

Score : 300 points

Problem Statement

There are N stations on a certain line operated by AtCoder Railway. The i-th station (1 \leq i \leq N) from the starting station is named S_i.

Local trains stop at all stations, while express trains may not. Specifically, express trains stop at only M \, (M \leq N) stations, and the j-th stop (1 \leq j \leq M) is the station named T_j.
Here, it is guaranteed that T_1 = S_1 and T_M = S_N, that is, express trains stop at both starting and terminal stations.

For each of the N stations, determine whether express trains stop at that station.

Constrains

  • 2 \leq M \leq N \leq 10^5
  • N and M are integers.
  • S_i (1 \leq i \leq N) is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 and T_M = S_N.
  • (T_1, \dots, T_M) is obtained by removing zero or more strings from (S_1, \dots, S_N) and lining up the remaining strings without changing the order.

Input

Input is given from Standard Input in the following format:

N M
S_1 \ldots S_N
T_1 \ldots T_M

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if express trains stop at the i-th station from the starting station, and No otherwise.


Sample Input 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

Sample Output 1

Yes
No
Yes
No
Yes

Sample Input 2

7 7
a t c o d e r
a t c o d e r

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

Express trains may stop at all stations.

G - Medicines on Grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

HW 列のグリッドがあります。上から i 行目、左から j 列目のマス目を (i, j) と表します。各マスの状態は文字 A_{i,j} で表され、意味は以下の通りです。

  • . : 空きマス。
  • # : 障害物。
  • S : 空きマスかつスタート地点。
  • T : 空きマスかつゴール地点。

高橋君は、今いるマスから上下左右に隣り合う空きマスへ、エネルギーを 1 消費して移動することができます。ただし、エネルギーが 0 の状態で移動することはできず、またグリッドの外へ移動することはできません。

グリッドには合計で N 個の薬があります。i 番目の薬は空きマス (R_i, C_i) にあり、使うとエネルギーを E_i にすることができます。必ずしもエネルギーが増えるとは限らないことに注意してください。高橋君は自分のいるマスにある薬を使うことができます。使った薬はなくなります。

高橋君ははじめエネルギー 0 の状態でスタート地点にいて、ゴール地点まで移動したいです。これが可能かどうか判定してください。

制約

  • 1 \leq H, W \leq 200
  • A_{i, j}., #, S, T のいずれかである。
  • STA_{i, j} にそれぞれちょうど 1 つ存在する。
  • 1 \leq N \leq 300
  • 1 \leq R_i \leq H
  • 1 \leq C_i \leq W
  • i \neq j ならば (R_i, C_i) \neq (R_j, C_j)
  • A_{R_i, C_i}# でない。
  • 1 \leq E_i \leq HW

入力

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

H W
A_{1, 1}A_{1, 2}\cdotsA_{1, W}
A_{2, 1}A_{2, 2}\cdotsA_{2, W}
\vdots
A_{H, 1}A_{H, 2}\cdotsA_{H, W}
N
R_1 C_1 E_1
R_2 C_2 E_2
\vdots
R_N C_N E_N

出力

高橋君がスタート地点からゴール地点へ移動することが可能なら Yes 、不可能なら No を出力せよ。


入力例 1

4 4
S...
#..#
#...
..#T
4
1 1 3
1 3 5
3 2 1
2 3 1

出力例 1

Yes

例えば、以下のようにしてゴール地点へ移動することができます。

  • 1 を使う。エネルギーが 3 になる。
  • (1, 2) へ移動する。エネルギーが 2 になる。
  • (1, 3) へ移動する。エネルギーが 1 になる。
  • 2 を使う。エネルギーが 5 になる。
  • (2, 3) へ移動する。エネルギーが 4 になる。
  • (3, 3) へ移動する。エネルギーが 3 になる。
  • (3, 4) へ移動する。エネルギーが 2 になる。
  • (4, 4) へ移動する。エネルギーが 1 になる。

この移動の途中には (2, 3) にも薬がありますが、これを使うとゴールできません。


入力例 2

2 2
S.
T.
1
1 2 4

出力例 2

No

高橋君はスタート地点から移動することができません。


入力例 3

4 5
..#..
.S##.
.##T.
.....
3
3 1 5
1 2 3
2 2 1

出力例 3

Yes

Score: 425 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left. The state of each cell is represented by the character A_{i,j}, which means the following:

  • .: An empty cell.
  • #: An obstacle.
  • S: An empty cell and the start point.
  • T: An empty cell and the goal point.

Takahashi can move from his current cell to a vertically or horizontally adjacent empty cell by consuming 1 energy. He cannot move if his energy is 0, nor can he exit the grid.

There are N medicines in the grid. The i-th medicine is at the empty cell (R_i, C_i) and can be used to set the energy to E_i. Note that the energy does not necessarily increase. He can use the medicine in his current cell. The used medicine will disappear.

Takahashi starts at the start point with 0 energy and wants to reach the goal point. Determine if this is possible.

Constraints

  • 1 \leq H, W \leq 200
  • A_{i, j} is one of ., #, S, and T.
  • Each of S and T exists exactly once in A_{i, j}.
  • 1 \leq N \leq 300
  • 1 \leq R_i \leq H
  • 1 \leq C_i \leq W
  • (R_i, C_i) \neq (R_j, C_j) if i \neq j.
  • A_{R_i, C_i} is not #.
  • 1 \leq E_i \leq HW

Input

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

H W
A_{1, 1}A_{1, 2}\cdotsA_{1, W}
A_{2, 1}A_{2, 2}\cdotsA_{2, W}
\vdots
A_{H, 1}A_{H, 2}\cdotsA_{H, W}
N
R_1 C_1 E_1
R_2 C_2 E_2
\vdots
R_N C_N E_N

Output

If Takahashi can reach the goal point from the start point, print Yes; otherwise, print No.


Sample Input 1

4 4
S...
#..#
#...
..#T
4
1 1 3
1 3 5
3 2 1
2 3 1

Sample Output 1

Yes

For example, he can reach the goal point as follows:

  • Use medicine 1. Energy becomes 3.
  • Move to (1, 2). Energy becomes 2.
  • Move to (1, 3). Energy becomes 1.
  • Use medicine 2. Energy becomes 5.
  • Move to (2, 3). Energy becomes 4.
  • Move to (3, 3). Energy becomes 3.
  • Move to (3, 4). Energy becomes 2.
  • Move to (4, 4). Energy becomes 1.

There is also medicine at (2, 3) along the way, but using it will prevent him from reaching the goal.


Sample Input 2

2 2
S.
T.
1
1 2 4

Sample Output 2

No

Takahashi cannot move from the start point.


Sample Input 3

4 5
..#..
.S##.
.##T.
.....
3
3 1 5
1 2 3
2 2 1

Sample Output 3

Yes
H - Last Train

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

AtCoder 国には駅 1,2,\ldots,NN 個の駅があります。

AtCoder 国に存在する電車の情報が M 個与えられます。 i 番目 (1\leq i\leq M) の情報は正整数の 6 つ組 (l _ i,d _ i,k _ i,c _ i,A _ i,B _ i) で表され、次のような情報に対応しています。

  • t=l _ i,l _ i+d _ i,l _ i+2d _ i,\ldots,l _ i+(k _ i-1)d _ i それぞれについて、次のような電車が存在する。
    • 時刻 t に 駅 A _ i を出発し、時刻 t+c _ i に駅 B _ i に到着する。

これらの情報にあてはまらない電車は存在せず、電車以外の方法である駅から異なる駅へ移動することはできません。
また、乗り換えにかかる時間は無視できるとします。

S から駅 N に到着できる最終時刻を f(S) とします。
より厳密には、整数の 4 つ組の列 \big((t _ i,c _ i,A _ i,B _ i)\big) _ {i=1,2,\ldots,k} であって、次のすべての条件を満たすものが存在するような t の最大値を f(S) とします。

  • t\leq t _ 1
  • A _ 1=S,B _ k=N
  • すべての 1\leq i\lt k について、B _ i=A _ {i+1}
  • すべての 1\leq i\leq k について、時刻 t _ i に駅 A _ i を出発して時刻 t _ i+c _ i に駅 B _ i に到着する電車が存在する。
  • すべての 1\leq i\lt k について、t _ i+c _ i\leq t _ {i+1}

ただし、そのような t が存在しないとき f(S)=-\infty とします。

f(1),f(2),\ldots,f(N-1) を求めてください。

制約

  • 2\leq N\leq2\times10 ^ 5
  • 1\leq M\leq2\times10 ^ 5
  • 1\leq l _ i,d _ i,k _ i,c _ i\leq10 ^ 9\ (1\leq i\leq M)
  • 1\leq A _ i,B _ i\leq N\ (1\leq i\leq M)
  • A _ i\neq B _ i\ (1\leq i\leq M)
  • 入力はすべて整数

入力

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

N M
l _ 1 d _ 1 k _ 1 c _ 1 A _ 1 B _ 1
l _ 2 d _ 2 k _ 2 c _ 2 A _ 2 B _ 2
\vdots
l _ M d _ M k _ M c _ M A _ M B _ M

出力

N-1 行出力せよ。 k 行目には f(k)\neq-\infty ならば f(k) を、f(k)=-\infty ならば Unreachable を出力せよ。


入力例 1

6 7
10 5 10 3 1 3
13 5 10 2 3 4
15 5 10 7 4 6
3 10 2 4 2 5
7 10 2 3 5 6
5 3 18 2 2 3
6 3 20 4 2 1

出力例 1

55
56
58
60
17

AtCoder 国に走っている電車は以下の図のようになります(発着時間の情報は図には含まれていません)。

2 から駅 6 に到着できる最終時刻について考えます。 次の図のように、駅 2 を時刻 56 に出発して駅 2\rightarrow3\rightarrow4\rightarrow6 のように移動することで駅 6 に到着することができます。

2 を時刻 56 より遅く出発して駅 6 に到着することはできないため、f(2)=56 です。


入力例 2

5 5
1000000000 1000000000 1000000000 1000000000 1 5
5 9 2 6 2 3
10 4 1 6 2 3
1 1 1 1 3 5
3 1 4 1 5 1

出力例 2

1000000000000000000
Unreachable
1
Unreachable

1 を時刻 10 ^ {18} に出発して駅 5 に時刻 10 ^ {18}+10 ^ 9 に到着するような電車が存在します。それ以降に駅 1 を出発する電車はないため、f(1)=10 ^ {18} です。 このように、答えが 32\operatorname{bit} 整数におさまらない場合もあります。

また、時刻 14 に駅 2 を出発して時刻 20 に駅 3 に到着するような電車は 2 番目の情報と 3 番目の情報の両方で存在が保証されています。 このように、複数の情報に重複している電車もあります。


入力例 3

16 20
4018 9698 2850 3026 8 11
2310 7571 7732 1862 13 14
2440 2121 20 1849 11 16
2560 5115 190 3655 5 16
1936 6664 39 8822 4 16
7597 8325 20 7576 12 5
5396 1088 540 7765 15 1
3226 88 6988 2504 13 5
1838 7490 63 4098 8 3
1456 5042 4 2815 14 7
3762 6803 5054 6994 10 9
9526 6001 61 8025 7 8
5176 6747 107 3403 1 5
2014 5533 2031 8127 8 11
8102 5878 58 9548 9 10
3788 174 3088 5950 3 13
7778 5389 100 9003 10 15
556 9425 9458 109 3 11
5725 7937 10 3282 2 9
6951 7211 8590 1994 15 12

出力例 3

720358
77158
540926
255168
969295
Unreachable
369586
466218
343148
541289
42739
165772
618082
16582
591828

Score: 450 points

Problem Statement

In the country of AtCoder, there are N stations: station 1, station 2, \ldots, station N.

You are given M pieces of information about trains in the country. The i-th piece of information (1\leq i\leq M) is represented by a tuple of six positive integers (l _ i,d _ i,k _ i,c _ i,A _ i,B _ i), which corresponds to the following information:

  • For each t=l _ i,l _ i+d _ i,l _ i+2d _ i,\ldots,l _ i+(k _ i-1)d _ i, there is a train as follows:
    • The train departs from station A _ i at time t and arrives at station B _ i at time t+c _ i.

No trains exist other than those described by this information, and it is impossible to move from one station to another by any means other than by train.
Also, assume that the time required for transfers is negligible.

Let f(S) be the latest time at which one can arrive at station N from station S.
More precisely, f(S) is defined as the maximum value of t for which there is a sequence of tuples of four integers \big((t _ i,c _ i,A _ i,B _ i)\big) _ {i=1,2,\ldots,k} that satisfies all of the following conditions:

  • t\leq t _ 1
  • A _ 1=S,B _ k=N
  • B _ i=A _ {i+1} for all 1\leq i\lt k,
  • For all 1\leq i\leq k, there is a train that departs from station A _ i at time t _ i and arrives at station B _ i at time t _ i+c _ i.
  • t _ i+c _ i\leq t _ {i+1} for all 1\leq i\lt k.

If no such t exists, set f(S)=-\infty.

Find f(1),f(2),\ldots,f(N-1).

Constraints

  • 2\leq N\leq2\times10 ^ 5
  • 1\leq M\leq2\times10 ^ 5
  • 1\leq l _ i,d _ i,k _ i,c _ i\leq10 ^ 9\ (1\leq i\leq M)
  • 1\leq A _ i,B _ i\leq N\ (1\leq i\leq M)
  • A _ i\neq B _ i\ (1\leq i\leq M)
  • All input values are integers.

Input

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

N M
l _ 1 d _ 1 k _ 1 c _ 1 A _ 1 B _ 1
l _ 2 d _ 2 k _ 2 c _ 2 A _ 2 B _ 2
\vdots
l _ M d _ M k _ M c _ M A _ M B _ M

Output

Print N-1 lines. The k-th line should contain f(k) if f(k)\neq-\infty, and Unreachable if f(k)=-\infty.


Sample Input 1

6 7
10 5 10 3 1 3
13 5 10 2 3 4
15 5 10 7 4 6
3 10 2 4 2 5
7 10 2 3 5 6
5 3 18 2 2 3
6 3 20 4 2 1

Sample Output 1

55
56
58
60
17

The following diagram shows the trains running in the country (information about arrival and departure times is omitted).

Consider the latest time at which one can arrive at station 6 from station 2. As shown in the following diagram, one can arrive at station 6 by departing from station 2 at time 56 and moving as station 2\rightarrow station 3\rightarrow station 4\rightarrow station 6.

It is impossible to depart from station 2 after time 56 and arrive at station 6, so f(2)=56.


Sample Input 2

5 5
1000000000 1000000000 1000000000 1000000000 1 5
5 9 2 6 2 3
10 4 1 6 2 3
1 1 1 1 3 5
3 1 4 1 5 1

Sample Output 2

1000000000000000000
Unreachable
1
Unreachable

There is a train that departs from station 1 at time 10 ^ {18} and arrives at station 5 at time 10 ^ {18}+10 ^ 9. There are no trains departing from station 1 after that time, so f(1)=10 ^ {18}. As seen here, the answer may not fit within a 32\operatorname{bit} integer.

Also, both the second and third pieces of information guarantee that there is a train that departs from station 2 at time 14 and arrives at station 3 at time 20. As seen here, some trains may appear in multiple pieces of information.


Sample Input 3

16 20
4018 9698 2850 3026 8 11
2310 7571 7732 1862 13 14
2440 2121 20 1849 11 16
2560 5115 190 3655 5 16
1936 6664 39 8822 4 16
7597 8325 20 7576 12 5
5396 1088 540 7765 15 1
3226 88 6988 2504 13 5
1838 7490 63 4098 8 3
1456 5042 4 2815 14 7
3762 6803 5054 6994 10 9
9526 6001 61 8025 7 8
5176 6747 107 3403 1 5
2014 5533 2031 8127 8 11
8102 5878 58 9548 9 10
3788 174 3088 5950 3 13
7778 5389 100 9003 10 15
556 9425 9458 109 3 11
5725 7937 10 3282 2 9
6951 7211 8590 1994 15 12

Sample Output 3

720358
77158
540926
255168
969295
Unreachable
369586
466218
343148
541289
42739
165772
618082
16582
591828
I - Cleaning Robot

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

無限に広がる二次元グリッドのマス (0, 0) に掃除ロボットが置かれています。

このロボットに、4 種類の文字 LRUD からなる文字列で表されたプログラムが与えられます。
ロボットは、与えられたプログラムの各文字を先頭の文字から順に読み、各文字について以下の行動を行います。

  1. ロボットの現在地をマス (x, y) とする
  2. 読んだ文字に応じて以下の通りに移動する:
    • L を読んだとき: マス (x-1, y) に移動する。
    • R を読んだとき: マス (x+1, y) に移動する。
    • U を読んだとき: マス (x, y-1) に移動する。
    • D を読んだとき: マス (x, y+1) に移動する。

LRUD からなる文字列 S が与えられます。 ロボットが実行するプログラムは、文字列 SK 個連接したものです。

ロボットが一度でも存在したことのあるマス(ロボットの初期位置であるマス (0, 0) を含む)は掃除されます。
ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力して下さい。

制約

  • SLRUD からなる長さ 1 以上 2 \times 10^5 以下の文字列
  • 1 \leq K \leq 10^{12}

入力

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

S
K

出力

ロボットがプログラムの実行を終えた後の時点で、掃除されているマスの個数を出力せよ。


入力例 1

RDRUL
2

出力例 1

7

ロボットが実行するプログラムは RDRULRDRUL です。ロボットは初期位置であるマス (0, 0) からはじめ、
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0) と移動します。
その後掃除されているマスは、(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)7 個です。


入力例 2

LR
1000000000000

出力例 2

2

入力例 3

UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU
31415926535

出力例 3

219911485785

Score : 500 points

Problem Statement

There is a cleaning robot on the square (0, 0) in an infinite two-dimensional grid.

The robot will be given a program represented as a string consisting of four kind of characters L, R, U, D.
It will read the characters in the program from left to right and perform the following action for each character read.

  1. Let (x, y) be the square where the robot is currently on.
  2. Make the following move according to the character read:
    • if L is read: go to (x-1, y).
    • if R is read: go to (x+1, y).
    • if U is read: go to (x, y-1).
    • if D is read: go to (x, y+1).

You are given a string S consisting of L, R, U, D. The program that will be executed by the robot is the concatenation of K copies of S.

Squares visited by the robot at least once, including the initial position (0, 0), will be cleaned.
Print the number of squares that will be cleaned at the end of the execution of the program.

Constraints

  • S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of L, R, U, D.
  • 1 \leq K \leq 10^{12}

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the number of squares that will be cleaned at the end of the execution of the program.


Sample Input 1

RDRUL
2

Sample Output 1

7

The robot will execute the program RDRULRDRUL. It will start on (0, 0) and travel as follows:
(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) \rightarrow (2, 1) \rightarrow (2, 0) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 0) \rightarrow (2, 0).
In the end, seven squares will get cleaned: (0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1).


Sample Input 2

LR
1000000000000

Sample Output 2

2

Sample Input 3

UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU
31415926535

Sample Output 3

219911485785