Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 100 点
問題文
長さ 6 の文字列 S が与えられます。S の先頭 3 文字は ABC
であり、末尾 3 文字は数字であることが保証されます。
Sが、このコンテスト開始以前に AtCoder上で開催され終了したコンテストの略称であるかどうか判定してください。
ただし、文字列 T が「このコンテスト開始以前に AtCoder上で開催され終了したコンテストの略称」であるとは、以下の 348 個の文字列のうちいずれかに等しいことと定めます。
ABC001
, ABC002
, \ldots, ABC314
, ABC315
, ABC317
, ABC318
, \ldots, ABC348
, ABC349
特に ABC316
が含まれないことに注意してください。
制約
- S は先頭 3 文字が
ABC
、末尾 3 文字が数字である長さ 6 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
Sが、このコンテスト開始以前に AtCoder上で開催され終了したコンテストの略称であるなら Yes
、そうでないなら No
と出力せよ。
入力例 1
ABC349
出力例 1
Yes
ABC349
は先週AtCoder上で開催され終了したコンテストの略称です。
入力例 2
ABC350
出力例 2
No
ABC350
はこのコンテストです。まだ終了していません。
入力例 3
ABC316
出力例 3
No
ABC316
はAtCoder上で開催されていません。
Score: 100 points
Problem Statement
You are given a string S of length 6. It is guaranteed that the first three characters of S are ABC
and the last three characters are digits.
Determine if S is the abbreviation of a contest held and concluded on AtCoder before the start of this contest.
Here, a string T is "the abbreviation of a contest held and concluded on AtCoder before the start of this contest" if and only if it equals one of the following 348 strings:
ABC001
, ABC002
, \ldots, ABC314
, ABC315
, ABC317
, ABC318
, \ldots, ABC348
, ABC349
.
Note that ABC316
is not included.
Constraints
- S is a string of length 6 where the first three characters are
ABC
and the last three characters are digits.
Input
The input is given from Standard Input in the following format:
S
Output
If S is the abbreviation of a contest held and concluded on AtCoder before the start of this contest, print Yes
; otherwise, print No
.
Sample Input 1
ABC349
Sample Output 1
Yes
ABC349
is the abbreviation of a contest held and concluded on AtCoder last week.
Sample Input 2
ABC350
Sample Output 2
No
ABC350
is this contest, which has not concluded yet.
Sample Input 3
ABC316
Sample Output 3
No
ABC316
was not held on AtCoder.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 200 点
問題文
高橋君には、穴 1,2,\dots,N に 1 本ずつ、全部で N 本の歯が生えています。
歯医者の青木君は、これらの歯と穴に対して、 Q 回の治療を行います。
i 回目の治療では、穴 T_i を治療します。治療内容は次の通りです。
- 穴 T_i に歯が生えている場合、穴 T_i から歯を抜く。
- そうでない ( 穴 T_i に歯が生えていない) 場合、穴 T_i に歯を生やす。
全ての治療が終わった後、高橋君に生えている歯の本数は何本ですか?
制約
- 入力は全て整数
- 1 \le N,Q \le 1000
- 1 \le T_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q T_1 T_2 \dots T_Q
出力
答えを整数として出力せよ。
入力例 1
30 6 2 9 18 27 18 9
出力例 1
28
高橋君には最初 30 本の歯が生えており、青木君は 6 回の治療を行います。
- 1 回目の治療では穴 2 を治療します。 穴 2 に歯が生えているため、歯を抜きます。
- 2 回目の治療では穴 9 を治療します。 穴 9 に歯が生えているため、歯を抜きます。
- 3 回目の治療では穴 18 を治療します。 穴 18 に歯が生えているため、歯を抜きます。
- 4 回目の治療では穴 27 を治療します。 穴 27 に歯が生えているため、歯を抜きます。
- 5 回目の治療では穴 18 を治療します。 穴 18 に歯が生えていないため、歯を生やします。
- 6 回目の治療では穴 9 を治療します。 穴 9 に歯が生えていないため、歯を生やします。
最終的な歯の本数は 28 本です。
入力例 2
1 7 1 1 1 1 1 1 1
出力例 2
0
入力例 3
9 20 9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8
出力例 3
5
Score: 200 points
Problem Statement
Takahashi has N teeth, one in each of the holes numbered 1, 2, \dots, N.
Dentist Aoki will perform Q treatments on these teeth and holes.
In the i-th treatment, hole T_i is treated as follows:
- If there is a tooth in hole T_i, remove the tooth from hole T_i.
- If there is no tooth in hole T_i (i.e., the hole is empty), grow a tooth in hole T_i.
After all treatments are completed, how many teeth does Takahashi have?
Constraints
- All input values are integers.
- 1 \le N, Q \le 1000
- 1 \le T_i \le N
Input
The input is given from Standard Input in the following format:
N Q T_1 T_2 \dots T_Q
Output
Print the number of teeth as an integer.
Sample Input 1
30 6 2 9 18 27 18 9
Sample Output 1
28
Initially, Takahashi has 30 teeth, and Aoki performs six treatments.
- In the first treatment, hole 2 is treated. There is a tooth in hole 2, so it is removed.
- In the second treatment, hole 9 is treated. There is a tooth in hole 9, so it is removed.
- In the third treatment, hole 18 is treated. There is a tooth in hole 18, so it is removed.
- In the fourth treatment, hole 27 is treated. There is a tooth in hole 27, so it is removed.
- In the fifth treatment, hole 18 is treated. There is no tooth in hole 18, so a tooth is grown.
- In the sixth treatment, hole 9 is treated. There is no tooth in hole 9, so a tooth is grown.
The final count of teeth is 28.
Sample Input 2
1 7 1 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
9 20 9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8
Sample Output 3
5
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 300 点
問題文
(1,2,\ldots,N) の並び替えである数列 A=(A_1,\ldots,A_N) が与えられます。
次の操作を 0 回以上 N-1 回以下行うことで、A を (1,2,\ldots,N) にしてください。
- 操作:1\leq i < j \leq N を満たす整数の組 (i,j) を自由に選ぶ。A の i 番目と j 番目の要素を入れ替える。
なお、制約の条件下で必ず A を (1,2,\ldots,N) にできることが証明できます。
制約
- 2 \leq N \leq 2\times 10^5
- (A_1,\ldots,A_N) は (1,2,\ldots,N) の並び替えである
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
操作回数を K として、K+1 行出力せよ。
1 行目には K を出力せよ。
l+1 行目 (1\leq l \leq K) には l 回目の操作で選ぶ整数 i,j を空白区切りで出力せよ。
問題文中の条件を満たすどのような出力も正解とみなされる。
入力例 1
5 3 4 1 2 5
出力例 1
2 1 3 2 4
操作により数列は次のように変化します。
- 最初 A=(3,4,1,2,5) である。
- 1 回目の操作で 1 番目の要素と 3 番目の要素を入れ替える。A=(1,4,3,2,5) になる。
- 2 回目の操作で 2 番目の要素と 4 番目の要素を入れ替える。A=(1,2,3,4,5) になる。
この他、次のような出力でも正解とみなされます。
4 2 3 3 4 1 2 2 3
入力例 2
4 1 2 3 4
出力例 2
0
入力例 3
3 3 1 2
出力例 3
2 1 2 2 3
Score: 300 points
Problem Statement
You are given a permutation A=(A_1,\ldots,A_N) of (1,2,\ldots,N).
Transform A into (1,2,\ldots,N) by performing the following operation between 0 and N-1 times, inclusive:
- Operation: Choose any pair of integers (i,j) such that 1\leq i < j \leq N. Swap the elements at the i-th and j-th positions of A.
It can be proved that under the given constraints, it is always possible to transform A into (1,2,\ldots,N).
Constraints
- 2 \leq N \leq 2\times 10^5
- (A_1,\ldots,A_N) is a permutation of (1,2,\ldots,N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Let K be the number of operations. Print K+1 lines.
The first line should contain K.
The (l+1)-th line (1\leq l \leq K) should contain the integers i and j chosen for the l-th operation, separated by a space.
Any output that satisfies the conditions in the problem statement will be considered correct.
Sample Input 1
5 3 4 1 2 5
Sample Output 1
2 1 3 2 4
The operations change the sequence as follows:
- Initially, A=(3,4,1,2,5).
- The first operation swaps the first and third elements, making A=(1,4,3,2,5).
- The second operation swaps the second and fourth elements, making A=(1,2,3,4,5).
Other outputs such as the following are also considered correct:
4 2 3 3 4 1 2 2 3
Sample Input 2
4 1 2 3 4
Sample Output 2
0
Sample Input 3
3 3 1 2
Sample Output 3
2 1 2 2 3
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 350 点
問題文
1 から N の番号がついた N 人のユーザが利用している SNS があります。
この SNS では 2 人のユーザが互いに友達になれる機能があります。
友達関係は双方向的です。すなわち、ユーザ X がユーザ Y の友達であるならば、必ずユーザ Y はユーザ X の友達です。
現在 SNS 上には M 組の友達関係が存在し、i 組目の友達関係はユーザ A_i とユーザ B_i からなります。
以下の操作を行える最大の回数を求めてください。
- 操作:3 人のユーザ X, Y, Z であって、X と Y は友達、Y と Z は友達であり、X と Z は友達でないようなものを選ぶ。X と Z を友達にする。
制約
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \leq 2\times 10^5
- 1\leq A_i < B_i \leq N
- (A_i,B_i) は相異なる
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
4 3 1 2 2 3 1 4
出力例 1
3
次のようにして「友達の友達と新たに友達になる」という操作は 3 回行えます。
- ユーザ 1 が友達(ユーザ 2)の友達であるユーザ 3 と新たに友達になる
- ユーザ 3 が友達(ユーザ 1)の友達であるユーザ 4 と新たに友達になる
- ユーザ 2 が友達(ユーザ 1)の友達であるユーザ 4 と新たに友達になる
4 回以上行うことはできません。
入力例 2
3 0
出力例 2
0
もともと友達関係が存在しないとき、新たな友達関係は発生しません。
入力例 3
10 8 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10
出力例 3
12
Score: 350 points
Problem Statement
There is an SNS used by N users, labeled with numbers from 1 to N.
In this SNS, two users can become friends with each other.
Friendship is bidirectional; if user X is a friend of user Y, user Y is always a friend of user X.
Currently, there are M pairs of friendships on the SNS, with the i-th pair consisting of users A_i and B_i.
Determine the maximum number of times the following operation can be performed:
- Operation: Choose three users X, Y, and Z such that X and Y are friends, Y and Z are friends, but X and Z are not. Make X and Z friends.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i < B_i \leq N
- The pairs (A_i, B_i) are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Print the answer.
Sample Input 1
4 3 1 2 2 3 1 4
Sample Output 1
3
Three new friendships with a friend's friend can occur as follows:
- User 1 becomes friends with user 3, who is a friend of their friend (user 2)
- User 3 becomes friends with user 4, who is a friend of their friend (user 1)
- User 2 becomes friends with user 4, who is a friend of their friend (user 1)
There will not be four or more new friendships.
Sample Input 2
3 0
Sample Output 2
0
If there are no initial friendships, no new friendships can occur.
Sample Input 3
10 8 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10
Sample Output 3
12
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 450 点
問題文
整数 N が与えられます。あなたは次の 2 種類の操作を行うことができます。
- X 円払う。N を \displaystyle\left\lfloor\frac{N}{A}\right\rfloor に置き換える。
- Y 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。
ここで \lfloor s \rfloor は s 以下の最大の整数を表します。例えば \lfloor 3 \rfloor=3、\lfloor 2.5 \rfloor=2 です。
適切に操作を選択したとき、N を 0 にするまでに払う必要がある金額の期待値の最小値を求めてください。
なお、サイコロの出目は操作ごとに独立であり、操作の選択はそれまでの操作の結果を確認してから行うことができます。
制約
- 1 \leq N \leq 10^{18}
- 2 \leq A \leq 6
- 1 \leq X,Y \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A X Y
出力
答えを出力せよ。
真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
3 2 10 20
出力例 1
20.000000000000000
行える操作は 次の 2 種類です。
- 10 円払う。N を \displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
- 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。
前者の操作を 2 回行うのが最適です。
入力例 2
3 2 20 20
出力例 2
32.000000000000000
行える操作は 次の 2 種類です。
- 20 円払う。N を \displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
- 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。
最適な操作は以下のようになります。
- まず後者の操作でサイコロを振ります。
- 4 以上が出た場合 N=0 となります。
- 2 または 3 が出た場合 N=1 となります。続けて前者の操作を行うことで N=0 となります。
- 1 が出た場合最初からやり直します。
入力例 3
314159265358979323 4 223606797 173205080
出力例 3
6418410657.7408381
Score: 450 points
Problem Statement
You are given an integer N. You can perform the following two types of operations:
- Pay X yen to replace N with \displaystyle\left\lfloor\frac{N}{A}\right\rfloor.
- Pay Y yen to roll a die (dice) that shows an integer between 1 and 6, inclusive, with equal probability. Let b be the outcome of the die, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.
Here, \lfloor s \rfloor denotes the greatest integer less than or equal to s. For example, \lfloor 3 \rfloor=3 and \lfloor 2.5 \rfloor=2.
Determine the minimum expected cost paid before N becomes 0 when optimally choosing operations.
The outcome of the die in each operation is independent of other rolls, and the choice of operation can be made after observing the results of the previous operations.
Constraints
- 1 \leq N \leq 10^{18}
- 2 \leq A \leq 6
- 1 \leq X, Y \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A X Y
Output
Print the answer.
Your output will be considered correct if the absolute or relative error from the true answer is at most 10^{-6}.
Sample Input 1
3 2 10 20
Sample Output 1
20.000000000000000
The available operations are as follows:
- Pay 10 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
- Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.
The optimal strategy is to perform the first operation twice.
Sample Input 2
3 2 20 20
Sample Output 2
32.000000000000000
The available operations are as follows:
- Pay 20 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
- Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.
The optimal strategy is as follows:
- First, perform the second operation to roll the die.
- If the outcome is 4 or greater, then N becomes 0.
- If the outcome is 2 or 3, then N becomes 1. Now, perform the first operation to make N = 0.
- If the outcome is 1, restart from the beginning.
Sample Input 3
314159265358979323 4 223606797 173205080
Sample Output 3
6418410657.7408381
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 550 点
問題文
英大小文字と (
、 )
からなる文字列 S=S_1 S_2 S_3 \dots S_{|S|} が与えられます。
文字列 S 中の括弧は、対応が取れています。
次の操作を、操作ができなくなるまで繰り返します。
- まず、以下の条件を全て満たす整数組 (l,r) をひとつ選択する。
- l < r
- S_l =
(
- S_r =
)
- S_{l+1},S_{l+2},\dots,S_{r-1} は全て英大文字または英小文字である
- T=\overline{S_{r-1}S_{r-2} \dots S_{l+1}} とする。
- 但し、 \overline{x} は x の大文字と小文字を反転させた文字列を指す。
- その後、 S の l 文字目から r 文字目までを削除し、削除した位置に T を挿入する。
詳細は入出力例を参照してください。
上記の操作を使って全ての (
と )
を除去することができ、最終的な文字列は操作の方法や順序によらないことが証明できます。
このとき、最終的な文字列を求めてください。
「 S 中の括弧の対応が取れている」とは?
まず、正しい括弧列を次の通り定義します。- 正しい括弧列とは、以下のいずれかの条件を満たす文字列です。
- 空文字列
- ある正しい括弧列 A が存在して、
(
, A,)
をこの順に連結した文字列 - ある空でない正しい括弧列 A,B が存在して、 A,B をこの順に連結した文字列
(
と )
を順序を保って抜き出した時、それが正しい括弧列となることを指す。
制約
- 1 \le |S| \le 5 \times 10^5
- S は英大小文字と
(
、)
からなる - S 中の括弧は対応が取れている
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
((A)y)x
出力例 1
YAx
S= ((A)y)x
に対して操作を行います。
- l=2,r=4 を選択します。このとき削除される文字列は
(A)
で、代わりにa
が挿入されます。- この操作の結果、 S=
(ay)x
となります。
- この操作の結果、 S=
- l=1,r=4 を選択します。このとき削除される文字列は
(ay)
で、代わりにYA
が挿入されます。- この操作の結果、 S=
YAx
となります。
- この操作の結果、 S=
括弧を除去した結果、文字列は YAx
となったので、これを出力してください。
入力例 2
((XYZ)n(X(y)Z))
出力例 2
XYZNXYZ
S= ((XYZ)n(X(y)Z))
に対して操作を行います。
- l=10,r=12 を選択します。このとき削除される文字列は
(y)
で、代わりにY
が挿入されます。- この操作の結果、 S=
((XYZ)n(XYZ))
となります。
- この操作の結果、 S=
- l=2,r=6 を選択します。このとき削除される文字列は
(XYZ)
で、代わりにzyx
が挿入されます。- この操作の結果、 S=
(zyxn(XYZ))
となります。
- この操作の結果、 S=
- l=6,r=10 を選択します。このとき削除される文字列は
(XYZ)
で、代わりにzyx
が挿入されます。- この操作の結果、 S=
(zyxnzyx)
となります。
- この操作の結果、 S=
- l=1,r=9 を選択します。このとき削除される文字列は
(zyxnzyx)
で、代わりにXYZNXYZ
が挿入されます。- この操作の結果、 S=
XYZNXYZ
となります。
- この操作の結果、 S=
括弧を除去した結果、文字列は XYZNXYZ
となったので、これを出力してください。
入力例 3
(((()))(()))(())
出力例 3
操作結果が空文字列になる場合もあります。
入力例 4
dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve
出力例 4
dFGsdccCrFNNnplCtQPOKjsiIwwEysAve
Score: 550 points
Problem Statement
You are given a string S = S_1 S_2 S_3 \dots S_{|S|} consisting of uppercase and lowercase English letters, (
, and )
.
The parentheses in the string S are properly matched.
Repeat the following operation until no more operations can be performed:
- First, select one pair of integers (l, r) that satisfies all of the following conditions:
- l < r
- S_l =
(
- S_r =
)
- Each of the characters S_{l+1}, S_{l+2}, \dots, S_{r-1} is an uppercase or lowercase English letter.
- Let T = \overline{S_{r-1} S_{r-2} \dots S_{l+1}}.
- Here, \overline{x} denotes the string obtained by toggling the case of each character in x (uppercase to lowercase and vice versa).
- Then, delete the l-th through r-th characters of S and insert T at the position where the deletion occurred.
Refer to the sample inputs and outputs for clarification.
It can be proved that it is possible to remove all (
s and )
s from the string using the above operations, and the final string is independent of how and in what order the operations are performed.
Determine the final string.
What does it mean that the parentheses in S are properly matched?
First, a correct parenthesis sequence is defined as follows:- A correct parenthesis sequence is a string that satisfies one of the following conditions:
- It is an empty string.
- There exists a correct parenthesis sequence A, and the string is formed by concatenating
(
, A,)
in this order. - There exist non-empty correct parenthesis sequences A and B, and the string is formed by concatenating A and B in this order.
(
s and )
s extracted from S without changing the order form a correct parenthesis sequence.
Constraints
- 1 \le |S| \le 5 \times 10^5
- S consists of uppercase and lowercase English letters,
(
, and)
. - The parentheses in S are properly matched.
Input
The input is given from Standard Input in the following format:
S
Output
Print the final string.
Sample Input 1
((A)y)x
Sample Output 1
YAx
Let us perform the operations for S = ((A)y)x
.
- Choose l=2 and r=4. The substring
(A)
is removed and replaced witha
.- After this operation, S =
(ay)x
.
- After this operation, S =
- Choose l=1 and r=4. The substring
(ay)
is removed and replaced withYA
.- After this operation, S =
YAx
.
- After this operation, S =
After removing the parentheses, the string becomes YAx
, which should be printed.
Sample Input 2
((XYZ)n(X(y)Z))
Sample Output 2
XYZNXYZ
Let us perform the operations for S = ((XYZ)n(X(y)Z))
.
- Choose l=10 and r=12. The substring
(y)
is removed and replaced withY
.- After this operation, S =
((XYZ)n(XYZ))
.
- After this operation, S =
- Choose l=2 and r=6. The substring
(XYZ)
is removed and replaced withzyx
.- After this operation, S =
(zyxn(XYZ))
.
- After this operation, S =
- Choose l=6 and r=10. The substring
(XYZ)
is removed and replaced withzyx
.- After this operation, S =
(zyxnzyx)
.
- After this operation, S =
- Choose l=1 and r=9. The substring
(zyxnzyx)
is removed and replaced withXYZNXYZ
.- After this operation, S =
XYZNXYZ
.
- After this operation, S =
After removing the parentheses, the string becomes XYZNXYZ
, which should be printed.
Sample Input 3
(((()))(()))(())
Sample Output 3
The final outcome may be an empty string.
Sample Input 4
dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve
Sample Output 4
dFGsdccCrFNNnplCtQPOKjsiIwwEysAve
Time Limit: 3 sec / Memory Limit: 256 MB
配点 : 600 点
問題文
特殊な入力形式に注意してください。 また、メモリ制限が通常より小さいことに注意してください。
頂点 1,2,\dots,N の N 頂点からなる無向グラフがあり、最初辺はありません。
このグラフに対して、以下の Q 個のクエリを処理してください。
1 u v
タイプ 1 : 頂点 u と頂点 v との間に辺を追加する。
辺を追加する前の時点で、 u と v は異なる連結成分に属する。(すなわち、グラフは常に森である。)
2 u v
タイプ 2 : 頂点 u と頂点 v の双方に隣接する頂点があるならその番号を答え、無ければ 0 と答える。
グラフが常に森であることから、このクエリに対する解答は一意に定まることが示せる。
但し、上記のクエリは暗号化して与えられます。
本来のクエリは 3 つの整数 A,B,C として定義され、これをもとに暗号化されたクエリが 3 つの整数 a,b,c として与えられます。
タイプ 2 のクエリのうち、先頭から k 個目のものに対する解答を X_k とします。 さらに、 k = 0 に対して X_k = 0 と定義します。
与えられた a,b,c から以下の通りに A,B,C を復号してください。
- そのクエリより前に与えられたタイプ 2 のクエリの個数を l とする(そのクエリ自身は数えない)。このとき、以下の通りに復号せよ。
- A = 1 + (((a \times (1+X_l)) \mod 998244353) \mod 2)
- B = 1 + (((b \times (1+X_l)) \mod 998244353) \mod N)
- C = 1 + (((c \times (1+X_l)) \mod 998244353) \mod N)
制約
- 入力は全て整数
- 2 \le N \le 10^5
- 1 \le Q \le 10^5
- 1 \le u < v \le N
- 0 \le a,b,c < 998244353
入力
入力は以下の形式で標準入力から与えられる。
但し、 \rm{Query}_i は i 個目のクエリを表す。
N Q \rm{Query}_1 \rm{Query}_2 \vdots \rm{Query}_Q
出力
タイプ 2 のクエリの個数を k としたとき、全体で k 行出力せよ。
そのうち i 行目には、タイプ 2 のクエリのうち、先頭から i 個目のものに対する解答を出力せよ。
入力例 1
6 12 143209915 123840720 97293110 89822758 207184717 689046893 67757230 270920641 26993265 952858464 221010240 871605818 730183361 147726243 445163345 963432357 295317852 195433903 120904472 106195318 615645575 817920568 27584394 770578609 38727673 250957656 506822697 139174867 566158852 412971999 205467538 606353836 855642999 159292205 319166257 51234344
出力例 1
0 2 0 2 6 0 1
全てのクエリを復号した後の入力は以下の通りです。
6 12 2 1 3 1 2 6 1 2 4 1 1 3 2 4 6 2 1 4 1 5 6 1 1 2 2 1 4 2 2 5 2 3 4 2 2 3
この入力について、グラフは 6 頂点であり、 12 個のクエリが含まれます。
- 1 個目のクエリは
2 1 3
である。- 頂点 1 と頂点 3 の双方に隣接する頂点はないので、 0 と答える。
- 2 個目のクエリは
1 2 6
である。- 頂点 2 と頂点 6 との間に辺を追加する。
- 3 個目のクエリは
1 2 4
である。- 頂点 2 と頂点 4 との間に辺を追加する。
- 4 個目のクエリは
1 1 3
である。- 頂点 1 と頂点 3 との間に辺を追加する。
- 5 個目のクエリは
2 4 6
である。- 頂点 4 と頂点 6 の双方に隣接する頂点は、頂点 2 である。
- 6 個目のクエリは
2 1 4
である。- 頂点 1 と頂点 4 の双方に隣接する頂点はないので、 0 と答える。
- 7 個目のクエリは
1 5 6
である。- 頂点 5 と頂点 6 との間に辺を追加する。
- 8 個目のクエリは
1 1 2
である。- 頂点 1 と頂点 2 との間に辺を追加する。
- 9 個目のクエリは
2 1 4
である。- 頂点 1 と頂点 4 の双方に隣接する頂点は、頂点 2 である。
- 10 個目のクエリは
2 2 5
である。- 頂点 2 と頂点 5 の双方に隣接する頂点は、頂点 6 である。
- 11 個目のクエリは
2 3 4
である。- 頂点 3 と頂点 4 の双方に隣接する頂点はないので、 0 と答える。
- 12 個目のクエリは
2 2 3
である。- 頂点 2 と頂点 3 の双方に隣接する頂点は、頂点 1 である。
入力例 2
2 1 377373366 41280240 33617925
出力例 2
出力が空である場合もあります。
Score: 600 points
Problem Statement
Beware the special input format and the smaller memory limit than usual.
There is an undirected graph with vertices 1, 2, \dots, N, initially without edges.
You need to process the following Q queries on this graph:
1 u v
Type 1: Add an edge between vertices u and v.
Before adding the edge, u and v belong to different connected components (i.e., the graph always remains a forest).
2 u v
Type 2: If there is a vertex adjacent to both u and v, report its vertex number; otherwise, report 0.
Given that the graph always remains a forest, the answer to this query is uniquely determined.
The queries are given in an encrypted form.
The original query is defined by three integers A, B, C, and the encrypted query is given as three integers a, b, c.
Let X_k be the answer to the k-th type-2 query. Define X_k = 0 for k = 0.
Restore A, B, C from the given a, b, c as follows:
- Let l be the number of type-2 queries given before this query (not counting the query itself). Then, use the following:
- A = 1 + (((a \times (1+X_l)) \mod 998244353) \mod 2)
- B = 1 + (((b \times (1+X_l)) \mod 998244353) \mod N)
- C = 1 + (((c \times (1+X_l)) \mod 998244353) \mod N)
Constraints
- All input values are integers.
- 2 \le N \le 10^5
- 1 \le Q \le 10^5
- 1 \le u < v \le N
- 0 \le a,b,c < 998244353
Input
The input is given from Standard Input in the following format:
N Q \rm{Query}_1 \rm{Query}_2 \vdots \rm{Query}_Q
Output
Let k be the number of type-2 queries. Print k lines.
The i-th line should contain the answer to the i-th type-2 query.
Sample Input 1
6 12 143209915 123840720 97293110 89822758 207184717 689046893 67757230 270920641 26993265 952858464 221010240 871605818 730183361 147726243 445163345 963432357 295317852 195433903 120904472 106195318 615645575 817920568 27584394 770578609 38727673 250957656 506822697 139174867 566158852 412971999 205467538 606353836 855642999 159292205 319166257 51234344
Sample Output 1
0 2 0 2 6 0 1
After decrypting all queries, the input is as follows:
6 12 2 1 3 1 2 6 1 2 4 1 1 3 2 4 6 2 1 4 1 5 6 1 1 2 2 1 4 2 2 5 2 3 4 2 2 3
This input has a 6-vertex graph and 12 queries.
- The first query is
2 1 3
.- No vertex is adjacent to both vertex 1 and vertex 3, so report 0.
- The second query is
1 2 6
.- Add an edge between vertices 2 and 6.
- The third query is
1 2 4
.- Add an edge between vertices 2 and 4.
- The fourth query is
1 1 3
.- Add an edge between vertices 1 and 3.
- The fifth query is
2 4 6
.- The vertex adjacent to both vertices 4 and 6 is vertex 2.
- The sixth query is
2 1 4
.- No vertex is adjacent to both vertices 1 and 4, so report 0.
- The seventh query is
1 5 6
.- Add an edge between vertices 5 and 6.
- The eighth query is
1 1 2
.- Add an edge between vertices 1 and 2.
- The ninth query is
2 1 4
.- The vertex adjacent to both vertices 1 and 4 is vertex 2.
- The tenth query is
2 2 5
.- The vertex adjacent to both vertices 2 and 5 is vertex 6.
- The eleventh query is
2 3 4
.- No vertex is adjacent to both vertices 3 and 4, so report 0.
- The twelfth query is
2 2 3
.- The vertex adjacent to both vertices 2 and 3 is vertex 1.
Sample Input 2
2 1 377373366 41280240 33617925
Sample Output 2
The output may be empty.