A - Apple

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

果物屋さんでりんごが売られています。
あなたは次の操作を好きな順で好きなだけ繰り返すことができます。

  • X 円を払ってりんごを 1 個手に入れる。
  • Y 円を払ってりんごを 3 個手に入れる。

りんごをちょうど N 個手に入れるには最低何円必要ですか?

制約

  • 1 \leq X \leq Y \leq 100
  • 1 \leq N \leq 100
  • 入力される値はすべて整数

入力

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

X Y N

出力

答えを整数として出力せよ。


入力例 1

10 25 10

出力例 1

85

25 円払って 3 個のりんごを手に入れる操作を 3 回繰り返した後、10 円払って 1 個のりんごを手に入れると丁度 10 個のりんごを手に入れられます。このときあなたは 85 円を消費します。
これより少ない金額でちょうど 10 個のりんごを手に入れることはできないので、答えは 85 円になります。


入力例 2

10 40 10

出力例 2

100

10 円払って 1 個のりんごを手に入れる操作を 10 回繰り返すのが最適です。


入力例 3

100 100 2

出力例 3

200

100 円を払って 1 個のりんごを手に入れる操作を 2 回繰り返す以外に ちょうど 2 個のりんごを手に入れる方法はありません。


入力例 4

100 100 100

出力例 4

3400

Score : 100 points

Problem Statement

A fruit store sells apples.
You may perform the following operations as many times as you want in any order:

  • Buy one apple for X yen (the currency in Japan).
  • Buy three apples for Y yen.

How much yen do you need to pay to obtain exactly N apples?

Constraints

  • 1 \leq X \leq Y \leq 100
  • 1 \leq N \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y N

Output

Print the answer as an integer.


Sample Input 1

10 25 10

Sample Output 1

85

Buy three apples for 25 yen three times and one apple for 10 yen, and you will obtain exactly 10 apples for a total of 85 yen.
You cannot obtain exactly 10 apples for a lower cost, so the answer is 85 yen.


Sample Input 2

10 40 10

Sample Output 2

100

It is optimal to buy an apple for 10 yen 10 times.


Sample Input 3

100 100 2

Sample Output 3

200

The only way to obtain exactly 2 apples is to buy an apple for 100 yen twice.


Sample Input 4

100 100 100

Sample Output 4

3400
B - Last Card

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

1,2,\ldots,N の番号のついた N 人の人に合計 K 枚のカードを配ります。

A から始めて 人 A,A+1,A+2,\ldots,N,1,2,\ldots の順に 1 枚ずつカードを配るとき、最後のカードは誰に配られるでしょうか?

厳密には、人 x(1 \leq x < N) の次には人 x+1 にカードを配り、人 N の次には人 1 にカードを配ります。

制約

  • 1 \leq N,K \leq 1000
  • 1 \leq A \leq N
  • 入力は全て整数

入力

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

N K A

出力

最後のカードが配られた人の番号を出力せよ。


入力例 1

3 3 2

出力例 1

1

2、人 3、人 1 の順にカードを配ります。


入力例 2

1 100 1

出力例 2

1

入力例 3

3 14 2

出力例 3

3

Score : 100 points

Problem Statement

We will hand out a total of K cards to N people numbered 1, 2, \ldots, N.

Beginning with Person A, we will give the cards one by one to the people in this order: A, A+1, A+2, \ldots, N, 1, 2, \ldots. Who will get the last card?

Formally, after Person x(1 \leq x < N) gets a card, Person x+1 will get a card. After Person N gets a card, Person 1 gets a card.

Constraints

  • 1 \leq N,K \leq 1000
  • 1 \leq A \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K A

Output

Print a number representing the person who will get the last card.


Sample Input 1

3 3 2

Sample Output 1

1

The cards are given to Person 2, 3, 1 in this order.


Sample Input 2

1 100 1

Sample Output 2

1

Sample Input 3

3 14 2

Sample Output 3

3
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 - Longest Palindrome

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

文字列 S が与えられます。 S の連続する部分文字列のうち、回文であるものの長さの最大値を求めてください。
ただし、S の連続する部分文字列であって回文であるものは常に存在します。

制約

  • S は長さ 2 以上 100 以下の英大文字からなる文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

TOYOTA

出力例 1

5

TOYOTA の連続する部分文字列 TOYOT は長さ 5 の回文です。
TOYOTA の唯一の長さ 6 の連続する部分文字列 TOYOTA は回文でないので、5 を出力します。


入力例 2

ABCDEFG

出力例 2

1

すべての長さ 1 の連続する部分文字列は回文です。


入力例 3

AAAAAAAAAA

出力例 3

10

Score : 200 points

Problem Statement

You are given a string S. Find the maximum length of a contiguous substring of S that is a palindrome.
Note that there is always a contiguous substring of S that is a palindrome.

Constraints

  • S is a string of length between 2 and 100, inclusive, consisting of uppercase English letters.

Input

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

S

Output

Print the answer.


Sample Input 1

TOYOTA

Sample Output 1

5

TOYOT, a contiguous substring of TOYOTA, is a palindrome of length 5.
TOYOTA, the only length-6 contiguous substring of TOYOTA, is not a palindrome, so print 5.


Sample Input 2

ABCDEFG

Sample Output 2

1

Every contiguous substring of length 1 is a palindrome.


Sample Input 3

AAAAAAAAAA

Sample Output 3

10
E - Knight Fork

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

xy 座標平面上の 2 つの格子点 (x_1, y_1), (x_2, y_2) からの距離がともに \sqrt{5} である格子点は存在しますか?

注記

xy 座標平面上にある点のうち、x 座標と y 座標がともに整数である点を格子点と呼びます。
また、xy 平面上の 2(a, b), (c, d) の距離をユークリッド距離 \sqrt{(a - c)^2 + (b-d)^2} として定義します。

参考として、xy 平面の (0, 0) の上に黒丸を、(0, 0) からの距離が \sqrt{5} となる格子点の上に白丸を書いた図は以下のようになります。(x,y いずれかが整数となる部分に目盛り線を引いています。)

image

制約

  • -10^9 \leq x_1 \leq 10^9
  • -10^9 \leq y_1 \leq 10^9
  • -10^9 \leq x_2 \leq 10^9
  • -10^9 \leq y_2 \leq 10^9
  • (x_1, y_1) \neq (x_2, y_2)
  • 入力はすべて整数である。

入力

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

x_1 y_1 x_2 y_2

出力

条件を満たす格子点が存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

0 0 3 3

出力例 1

Yes
  • (2,1)(x_1, y_1) の距離は \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5}
  • (2,1)(x_2, y_2) の距離は \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5}
  • (2, 1) は格子点

なので点 (2, 1) は条件を満たします。よって Yes を出力します。
なお、点 (1, 2) も条件を満たすことが同様にして確認できます。


入力例 2

0 1 2 3

出力例 2

No

条件を満たす格子点は存在しません。よって No を出力します。


入力例 3

1000000000 1000000000 999999999 999999999

出力例 3

Yes

(10^9 + 1, 10^9 - 2) および点 (10^9 - 2, 10^9 + 1)が条件を満たします。

Score : 300 points

Problem Statement

On an xy-coordinate plane, is there a lattice point whose distances from two lattice points (x_1, y_1) and (x_2, y_2) are both \sqrt{5}?

Notes

A point on an xy-coordinate plane whose x and y coordinates are both integers is called a lattice point.
The distance between two points (a, b) and (c, d) is defined to be the Euclidean distance between them, \sqrt{(a - c)^2 + (b-d)^2}.

The following figure illustrates an xy-plane with a black circle at (0, 0) and white circles at the lattice points whose distances from (0, 0) are \sqrt{5}. (The grid shows where either x or y is an integer.)

image

Constraints

  • -10^9 \leq x_1 \leq 10^9
  • -10^9 \leq y_1 \leq 10^9
  • -10^9 \leq x_2 \leq 10^9
  • -10^9 \leq y_2 \leq 10^9
  • (x_1, y_1) \neq (x_2, y_2)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

If there is a lattice point satisfying the condition, print Yes; otherwise, print No.


Sample Input 1

0 0 3 3

Sample Output 1

Yes
  • The distance between points (2,1) and (x_1, y_1) is \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5};
  • the distance between points (2,1) and (x_2, y_2) is \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5};
  • point (2, 1) is a lattice point,

so point (2, 1) satisfies the condition. Thus, Yes should be printed.
One can also assert in the same way that (1, 2) also satisfies the condition.


Sample Input 2

0 1 2 3

Sample Output 2

No

No lattice point satisfies the condition, so No should be printed.


Sample Input 3

1000000000 1000000000 999999999 999999999

Sample Output 3

Yes

Point (10^9 + 1, 10^9 - 2) and point (10^9 - 2, 10^9 + 1) satisfy the condition.

F - Slot Strategy 2 (Easy)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

この問題は G 問題の簡易版です。

3 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。ここで、S_i は数字のみからなる長さ M の文字列です。

それぞれのリールには対応するボタンがついています。高橋君は各非負整数 t について、スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す、または何もしないことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、i 番目のリールは S_i(t \bmod M)+1 文字目を表示して止まります。
ただし、t \bmod MtM で割ったあまりを表します。

高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
そのようなことが不可能であればそのことを報告してください。

制約

  • 1 \leq M \leq 100
  • M は整数
  • S_i は数字のみからなる長さ M の文字列

入力

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

M
S_1
S_2
S_3

出力

全てのリールを止めた上で、表示されている文字が全て同じであるようにすることができないなら -1 を出力せよ。
できるなら、スロットが回り始めてからそのような状態にするまでに最小で何秒かかるか出力せよ。


入力例 1

10
1937458062
8124690357
2385760149

出力例 1

6

高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。

  • スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2(0 \bmod 10)+1=1 文字目である 8 を表示して止まります。
  • スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3(2 \bmod 10)+1=3 文字目である 8 を表示して止まります。
  • スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1(6 \bmod 10)+1=7 文字目である 8 を表示して止まります。

5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。


入力例 2

20
01234567890123456789
01234567890123456789
01234567890123456789

出力例 2

20

全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。


入力例 3

5
11111
22222
33333

出力例 3

-1

表示されている文字が全て同じであるようにリールを止めることはできません。
このとき -1 を出力してください。

Score : 300 points

Problem Statement

This problem is an easier version of Problem G.

There is a slot machine with three reels.
The arrangement of symbols on the i-th reel is represented by the string S_i. Here, S_i is a string of length M consisting of digits.

Each reel has a corresponding button. For each non-negative integer t, Takahashi can either choose and press one button or do nothing exactly t seconds after the reels start spinning.
If he presses the button corresponding to the i-th reel exactly t seconds after the reels start spinning, the i-th reel will stop and display the ((t \bmod M)+1)-th character of S_i.
Here, t \bmod M denotes the remainder when t is divided by M.

Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.

Constraints

  • 1 \leq M \leq 100
  • M is an integer.
  • S_i is a string of length M consisting of digits.

Input

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

M
S_1
S_2
S_3

Output

If it is impossible to stop all the reels so that all the displayed characters are the same, print -1.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.


Sample Input 1

10
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can stop each reel as follows so that 6 seconds after the reels start spinning, all the reels display 8.

  • Press the button corresponding to the second reel 0 seconds after the reels start spinning. The second reel stops and displays 8, the ((0 \bmod 10)+1=1)-st character of S_2.
  • Press the button corresponding to the third reel 2 seconds after the reels start spinning. The third reel stops and displays 8, the ((2 \bmod 10)+1=3)-rd character of S_3.
  • Press the button corresponding to the first reel 6 seconds after the reels start spinning. The first reel stops and displays 8, the ((6 \bmod 10)+1=7)-th character of S_1.

There is no way to make the reels display the same character in 5 or fewer seconds, so print 6.


Sample Input 2

20
01234567890123456789
01234567890123456789
01234567890123456789

Sample Output 2

20

Note that he must stop all the reels and make them display the same character.


Sample Input 3

5
11111
22222
33333

Sample Output 3

-1

It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1.

G - Robot Arms 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N の正整数列 A = (A_1, A_2, \dots, A_N) および整数 x, y が与えられます。
次の条件をすべて満たすように、xy 座標平面上に N+1 個の点 p_1, p_2, \dots, p_N, p_{N+1} を配置することができるか判定してください。(同じ座標に 2 個以上の点を配置してもよいです。)

  • p_1 = (0, 0)
  • p_2 = (A_1, 0)
  • p_{N+1} = (x, y)
  • p_i と点 p_{i+1} の距離は A_i (1 \leq i \leq N)
  • 線分 p_i p_{i+1} と線分 p_{i+1} p_{i+2} のなす角は 90 度 (1 \leq i \leq N - 1)

制約

  • 2 \leq N \leq 10^3
  • 1 \leq A_i \leq 10
  • |x|, |y| \leq 10^4
  • 入力される値はすべて整数

入力

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

N x y
A_1 A_2 \dots A_N

出力

問題文の条件をすべて満たすように p_1, p_2, \dots, p_N, p_{N+1} を配置することができる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3 -1 1
2 1 3

出力例 1

Yes

xy 座標平面に p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 1), p_4 = (-1, 1) として点を配置したのが以下の図です。これは問題文の条件をすべて満たしています。


入力例 2

5 2 0
2 2 2 2 2

出力例 2

Yes

p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 2), p_4 = (0, 2), p_5 = (0, 0), p_6 = (2, 0) とすれば問題文の条件をすべて満たすことができます。同じ座標に複数の点を置いてもよいのに注意してください。


入力例 3

4 5 5
1 2 3 4

出力例 3

No

入力例 4

3 2 7
2 7 4

出力例 4

No

入力例 5

10 8 -7
6 10 4 1 5 9 8 6 5 1

出力例 5

Yes

Score : 400 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N consisting of positive integers, and integers x and y.
Determine whether it is possible to place N+1 points p_1, p_2, \dots, p_N, p_{N+1} in the xy-coordinate plane to satisfy all of the following conditions. (It is allowed to place two or more points at the same coordinates.)

  • p_1 = (0, 0).
  • p_2 = (A_1, 0).
  • p_{N+1} = (x, y).
  • The distance between the points p_i and p_{i+1} is A_i. (1 \leq i \leq N)
  • The segments p_i p_{i+1} and p_{i+1} p_{i+2} form a 90 degree angle. (1 \leq i \leq N - 1)

Constraints

  • 2 \leq N \leq 10^3
  • 1 \leq A_i \leq 10
  • |x|, |y| \leq 10^4
  • All values in the input are integers.

Input

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

N x y
A_1 A_2 \dots A_N

Output

If it is possible to place p_1, p_2, \dots, p_N, p_{N+1} to satisfy all of the conditions in the Problem Statement, print Yes; otherwise, print No.


Sample Input 1

3 -1 1
2 1 3

Sample Output 1

Yes

The figure below shows a placement where p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 1), and p_4 = (-1, 1). All conditions in the Problem Statement are satisfied.


Sample Input 2

5 2 0
2 2 2 2 2

Sample Output 2

Yes

Letting p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 2), p_4 = (0, 2), p_5 = (0, 0), and p_6 = (2, 0) satisfies all the conditions. Note that multiple points may be placed at the same coordinates.


Sample Input 3

4 5 5
1 2 3 4

Sample Output 3

No

Sample Input 4

3 2 7
2 7 4

Sample Output 4

No

Sample Input 5

10 8 -7
6 10 4 1 5 9 8 6 5 1

Sample Output 5

Yes
H - Σ[k=0..10^100]floor(X/10^k)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

\displaystyle \sum_{k=0}^{10^{100}} \left \lfloor \frac{X}{10^k} \right \rfloor を求めてください。

注釈

\lfloor A \rfloor は、 A の小数点以下を切り捨てた値を指します。

制約

  • X は整数
  • 1 \le X < 10^{500000}

入力

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

X

出力

答えを整数として出力せよ。
但し、たとえ答えが大きな整数であっても、求める答えを正確に整数として出力する必要がある。たとえば、 2.33e+21 のような指数表記や、 0523 のような先頭に不要な 0 を付けたような表記は許されない。


入力例 1

1225

出力例 1

1360

求める値は、 1225+122+12+1+0+0+\dots+0=1360 となります。


入力例 2

99999

出力例 2

111105

繰り上がりに注意してください。


入力例 3

314159265358979323846264338327950288419716939937510

出力例 3

349065850398865915384738153697722542688574377708317

入力される値も出力すべき値も非常に大きくなる場合があります。

Score : 500 points

Problem Statement

Find \displaystyle \sum_{k=0}^{10^{100}} \left \lfloor \frac{X}{10^k} \right \rfloor.

Notes

\lfloor A \rfloor denotes the value of A truncated to an integer.

Constraints

  • X is an integer.
  • 1 \le X < 10^{500000}

Input

Input is given from Standard Input in the following format:

X

Output

Print the answer as an integer.
Here, the answer must be precisely printed as an integer, even if it is large. It is not allowed to use exponential notation, such as 2.33e+21, or print unnecessary leading zeros, as in 0523.


Sample Input 1

1225

Sample Output 1

1360

The value we seek is 1225+122+12+1+0+0+\dots+0=1360.


Sample Input 2

99999

Sample Output 2

111105

Beware of carries.


Sample Input 3

314159265358979323846264338327950288419716939937510

Sample Output 3

349065850398865915384738153697722542688574377708317

The values in input and output can both be enormous.

I - BOX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 個の箱 1,2,\dots,N と、 10^{100} 個のボール 1,2,\dots,10^{100} があります。 最初、箱 i にはボール i のみが入っています。

ここに以下の操作が合計 Q 回行われるので、処理してください。

操作にはタイプ 1,2,33 種類があります。

タイプ 1 : 箱 X に箱 Y の中身を全て入れる。 この操作では X \neq Y が保証される。

1 X Y

タイプ 2 : 現在いずれかの箱に入っているボールの数の合計を k とすると、箱 X にボール k+1 を入れる。

2 X

タイプ 3 : ボール X が入っている箱の番号を答える。

3 X

制約

  • 入力は全て整数
  • 2 \le N \le 3 \times 10^5
  • 1 \le Q \le 3 \times 10^5
  • タイプ 1 の操作について、 1 \le X,Y \le N かつ X \neq Y
  • タイプ 2 の操作について、 1 \le X \le N
  • タイプ 3 の操作について、その時点でボール X がいずれかの箱に入っている
  • タイプ 3 の操作が少なくとも 1 つ与えられる

入力

入力は以下の形式で標準入力から与えられる。
但し、 op_ii 回目の操作を表す。

N Q
op_1
op_2
\vdots
op_Q

出力

各タイプ 3 の操作に対して、答えを 1 行に 1 つ、整数として出力せよ。


入力例 1

5 10
3 5
1 1 4
2 1
2 4
3 7
1 3 1
3 4
1 1 4
3 7
3 6

出力例 1

5
4
3
1
3

この入力は 10 個の操作を含みます。

  • 1 回目の操作はタイプ 3 です。ボール 5 は箱 5 に入っています。
  • 2 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
    • 1 の中身はボール 1,4 、箱 4 の中身は空になります。
  • 3 回目の操作はタイプ 2 です。箱 1 にボール 6 を入れます。
  • 4 回目の操作はタイプ 2 です。箱 4 にボール 7 を入れます。
  • 5 回目の操作はタイプ 3 です。ボール 7 は箱 4 に入っています。
  • 6 回目の操作はタイプ 1 です。箱 3 に箱 1 の中身を全て入れます。
    • 3 の中身はボール 1,3,4,6 、箱 1 の中身は空になります。
  • 7 回目の操作はタイプ 3 です。ボール 4 は箱 3 に入っています。
  • 8 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
    • 1 の中身はボール 7 、箱 4 の中身は空になります。
  • 9 回目の操作はタイプ 3 です。ボール 7 は箱 1 に入っています。
  • 10 回目の操作はタイプ 3 です。ボール 6 は箱 3 に入っています。

Score : 500 points

Problem Statement

There are N boxes numbered 1,2,\ldots,N, and 10^{100} balls numbered 1,2,\dots,10^{100}. Initially, box i contains just ball i.

Process a total of Q operations that will be performed.

There are three types of operations: 1, 2, and 3.

Type 1: Put all contents of box Y into box X. It is guaranteed that X \neq Y.

1 X Y

Type 2: Put ball k+1 into box X, where k is the current total number of balls contained in the boxes.

2 X

Type 3: Report the number of the box that contains ball X.

3 X

Constraints

  • All values in the input are integers.
  • 2 \le N \le 3 \times 10^5
  • 1 \le Q \le 3 \times 10^5
  • For each type-1 operation, 1 \le X,Y \le N and X \neq Y.
  • For each type-2 operation, 1 \le X \le N.
  • For each type-3 operation, ball X is contained in some box at that point.
  • There is at least one type-3 operation.

Input

The input is given from Standard Input in the following format.
Here, op_i represents the i-th operation.

N Q
op_1
op_2
\vdots
op_Q

Output

For each type-3 operation, print a line containing the response as an integer.


Sample Input 1

5 10
3 5
1 1 4
2 1
2 4
3 7
1 3 1
3 4
1 1 4
3 7
3 6

Sample Output 1

5
4
3
1
3

This input contains ten operations.

  • The first operation is of type 3. Ball 5 is in box 5.
  • The second operation is of type 1. Put all contents of box 4 into box 1.
    • Box 1 now contains balls 1 and 4, and box 4 is now empty.
  • The third operation is of type 2. Put ball 6 into box 1.
  • The fourth operation is of type 2. Put ball 7 into box 4.
  • The fifth operation is of type 3. Ball 7 is in box 4.
  • The sixth operation is of type 1. Put all contents of box 1 into box 3.
    • Box 3 now contains balls 1, 3, 4, and 6, and box 1 is now empty.
  • The seventh operation is of type 3. Ball 4 is in box 3.
  • The eighth operation is of type 1. Put all contents of box 4 into box 1.
    • Box 1 now contains ball 7, and box 4 is now empty.
  • The ninth operation is of type 3. Ball 7 is in box 1.
  • The tenth operation is of type 3. Ball 6 is in box 3.