Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
二つの文字 x と y は以下の 3 つの条件のうちどれか 1 つを満たすとき、似た文字と呼ばれます。
- x と y は同じ文字
- x と y の片方が
1
で、もう片方がl
- x と y の片方が
0
で、もう片方がo
また、長さ N の文字列 S と T は以下の条件を満たすとき、似た文字列と呼ばれます。
- 任意の i\ (1\leq i\leq N) について、 S の i 番目の文字と T の i 番目の文字は似た文字
英小文字及び数字からなる長さ N の文字列 S,T が与えられます。 S と T が似た文字列か判定してください。
制約
- N は 1 以上 100 以下の整数
- S,T は英小文字及び数字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
S と T が似た文字列の場合 Yes
を、そうでない場合 No
を出力せよ。
入力例 1
3 l0w 1ow
出力例 1
Yes
S の 1 文字目は l
で、T の 1 文字目は 1
です。これらは似た文字です。
S の 2 文字目は 0
で、T の 2 文字目は o
です。これらは似た文字です。
S の 3 文字目は w
で、T の 3 文字目は w
です。これらは似た文字です。
よって S と T は似た文字列です。
入力例 2
3 abc arc
出力例 2
No
S の 2 文字目は b
で、T の 2 文字目は r
です。これらは似た文字ではありません。
よって S と T は似た文字列ではありません。
入力例 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 isl
. - One of x and y is
0
and the other iso
.
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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
整数 a, b, c が与えられます。b がこれらの整数の中央値であるかどうか判定してください。
制約
- 1 \leq a, b, c \leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
a b c
出力
b が与えられた整数の中央値であるならば Yes
、そうでないならば No
と出力せよ。
入力例 1
5 3 2
出力例 1
Yes
与えられた整数を小さい順に並べると 2, 3, 5 となり、b はこれらの整数の中央値です。
入力例 2
2 5 3
出力例 2
No
b は与えられた整数の中央値ではありません。
入力例 3
100 100 100
出力例 3
Yes
Score : 100 points
Problem Statement
Given integers a, b, and c, determine if b is the median of these integers.
Constraints
- 1 \leq a, b, c \leq 100
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
a b c
Output
If b is the median of the given integers, then print Yes
; otherwise, print No
.
Sample Input 1
5 3 2
Sample Output 1
Yes
The given integers are 2, 3, 5 when sorted in ascending order, of which b is the median.
Sample Input 2
2 5 3
Sample Output 2
No
b is not the median of the given integers.
Sample Input 3
100 100 100
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
無限に長いピアノの鍵盤があります。 この鍵盤内の連続する区間であって、白鍵 W 個と黒鍵 B 個からなるものは存在しますか?
文字列 wbwbwwbwbwbw
を無限に繰り返してできる文字列を S とおきます。
S の部分文字列であって、W 個の w
と B 個の b
からなるものは存在しますか?
S の部分文字列とは
S の部分文字列とは、ある 2 つの正整数 l,r\ (l\leq r) に対して、S の l 文字目、l+1 文字目、\dots、r 文字目をこの順に繋げてできる文字列のことをいいます。制約
- W,B は整数
- 0\leq W,B \leq 100
- W+B \geq 1
入力
入力は以下の形式で標準入力から与えられる。
W B
出力
S の部分文字列であって、W 個の w
と B 個の b
からなるものが存在するならば Yes
を、存在しないならば No
を出力せよ。
入力例 1
3 2
出力例 1
Yes
S の最初の 15 文字は wbwbwwbwbwbwwbw
であり、S の 11 文字目から 15 文字目までを取り出してできる文字列 bwwbw
は 3 個の w
と 2 個の b
からなる部分文字列の一例です。
入力例 2
3 0
出力例 2
No
3 個の w
と 0 個の b
からなる文字列は www
のみですが、これは S の部分文字列ではありません。
入力例 3
92 66
出力例 3
Yes
Score: 200 points
Problem Statement
There is an infinitely long piano keyboard. Is there a continuous segment within this keyboard that consists of W white keys and B black keys?
Let S be the string formed by infinitely repeating the string wbwbwwbwbwbw
.
Is there a substring of S that consists of W occurrences of w
and B occurrences of b
?
What is a substring of S?
A substring of S is a string that can be formed by concatenating the l-th, (l+1)-th, \dots, r-th characters of S in this order for some two positive integers l and r (l\leq r).Constraints
- W and B are integers.
- 0\leq W,B \leq 100
- W+B \geq 1
Input
The input is given from Standard Input in the following format:
W B
Output
If there is a substring of S that consists of W occurrences of w
and B occurrences of b
, print Yes
; otherwise, print No
.
Sample Input 1
3 2
Sample Output 1
Yes
The first 15 characters of S are wbwbwwbwbwbwwbw
. You can take the 11-th through 15-th characters to form the string bwwbw
, which is a substring consisting of three occurrences of w
and two occurrences of b
.
Sample Input 2
3 0
Sample Output 2
No
The only string consisting of three occurrences of w
and zero occurrences of b
is www
, which is not a substring of S.
Sample Input 3
92 66
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
プレイヤー 1 、プレイヤー 2 、\ldots 、プレイヤー N と番号がつけられた N 人のプレイヤーがカードゲームで対戦します。
各プレイヤーはカードを 1 枚場に出します。
各カードは色と値の 2 つの属性を持ち、どちらの属性も正整数で表されます。
i = 1, 2, \ldots, N について、プレイヤー i が場に出したカードの色は C_i であり、値は R_i です。
R_1, R_2, \ldots, R_N はすべて異なります。
N 人のプレイヤーの中から 1 人の勝者を下記の方法で決めます。
- 色が T であるカードが 1 枚以上場に出された場合、色が T であるカードのうち値が最大のものを出したプレイヤーが勝者である。
- 色が T であるカードが場に 1 枚も出されなかった場合、プレイヤー 1 が出したカードと同じ色のカードのうち値が最大のものを出したプレイヤーが勝者である。(プレイヤー 1 自身も勝者となり得ることに注意してください。)
勝者となるプレイヤーの番号を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq T \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq R_i \leq 10^9
- i \neq j \implies R_i \neq R_j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N T C_1 C_2 \ldots C_N R_1 R_2 \ldots R_N
出力
答えを出力せよ。
入力例 1
4 2 1 2 1 2 6 3 4 5
出力例 1
4
色が 2 であるカードが 1 枚以上場に出されています。 よって、色が 2 であるカードのうち値が最大の 5 のカードを出した、プレイヤー 4 が勝者です。
入力例 2
4 2 1 3 1 4 6 3 4 5
出力例 2
1
色が 2 であるカードが 1 枚も場に出されていません。 よって、プレイヤー 1 が出したカードの色と同じ色(すなわち色 1 )のカードのうち値が最大の 6 のカードを出した、プレイヤー 1 が勝者です。
入力例 3
2 1000000000 1000000000 1 1 1000000000
出力例 3
1
Score : 200 points
Problem Statement
N players with ID numbers 1, 2, \ldots, N are playing a card game.
Each player plays one card.
Each card has two parameters: color and rank, both of which are represented by positive integers.
For i = 1, 2, \ldots, N, the card played by player i has a color C_i and a rank R_i.
All of R_1, R_2, \ldots, R_N are different.
Among the N players, one winner is decided as follows.
- If one or more cards with the color T are played, the player who has played the card with the greatest rank among those cards is the winner.
- If no card with the color T is played, the player who has played the card with the greatest rank among the cards with the color of the card played by player 1 is the winner. (Note that player 1 may win.)
Print the ID number of the winner.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq T \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq R_i \leq 10^9
- i \neq j \implies R_i \neq R_j
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N T C_1 C_2 \ldots C_N R_1 R_2 \ldots R_N
Output
Print the answer.
Sample Input 1
4 2 1 2 1 2 6 3 4 5
Sample Output 1
4
Cards with the color 2 are played. Thus, the winner is player 4, who has played the card with the greatest rank, 5, among those cards.
Sample Input 2
4 2 1 3 1 4 6 3 4 5
Sample Output 2
1
No card with the color 2 is played. Thus, the winner is player 1, who has played the card with the greatest rank, 6, among the cards with the color of the card played by player 1 (color 1).
Sample Input 3
2 1000000000 1000000000 1 1 1000000000
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
整数列 A=(A_1,A_2,\dots,A_N) があります。 あなたは次の操作を好きな回数(0 回でもよい)行うことができます。
- 1\leq i,j \leq N を満たす整数 i,j を選ぶ。A_i を 1 減らし、A_j を 1 増やす。
A の最小値と最大値の差を 1 以下にするために必要な最小の操作回数を求めてください。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 4 7 3 7
出力例 1
3
以下のように 3 回の操作を行うことで、A の最小値と最大値の差を 1 以下にすることができます。
- i=2,j=3 として操作を行う。A=(4,6,4,7) になる。
- i=4,j=1 として操作を行う。A=(5,6,4,6) になる。
- i=4,j=3 として操作を行う。A=(5,6,5,5) になる。
3 回未満の操作で A の最小値と最大値の差を 1 以下にすることはできません。よって答えは 3 です。
入力例 2
1 313
出力例 2
0
入力例 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
出力例 3
2499999974
Score : 400 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).
- Choose integers i and j with 1\leq i,j \leq N. Decrease A_i by one and increase A_j by one.
Find the minimum number of operations required to make the difference between the minimum and maximum values of A at most one.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
4 4 7 3 7
Sample Output 1
3
By the following three operations, the difference between the minimum and maximum values of A becomes at most one.
- Choose i=2 and j=3 to make A=(4,6,4,7).
- Choose i=4 and j=1 to make A=(5,6,4,6).
- Choose i=4 and j=3 to make A=(5,6,5,5).
You cannot make the difference between maximum and minimum values of A at most one by less than three operations, so the answer is 3.
Sample Input 2
1 313
Sample Output 2
0
Sample Input 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
Sample Output 3
2499999974
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder社ではカードを使った 1 人ゲームが流行っています。
ゲームで使う各カードには、英小文字 1 文字または @
の文字が書かれており、いずれのカードも十分多く存在します。
ゲームは以下の手順で行います。
- カードを同じ枚数ずつ 2 列に並べる。
@
のカードを、それぞれa
,t
,c
,o
,d
,e
,r
のいずれかのカードと置き換える。- 2 つの列が一致していれば勝ち。そうでなければ負け。
このゲームに勝ちたいあなたは、次のようなイカサマをすることにしました。
- 手順 1 以降の好きなタイミングで、列内のカードを自由に並び替えてよい。
手順 1 で並べられた 2 つの列を表す 2 つの文字列 S,T が与えられるので、イカサマをしてもよいときゲームに勝てるか判定してください。
制約
- S,T は英小文字と
@
からなる - S,T の長さは等しく 1 以上 2\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
イカサマをしてもよいとき、ゲームに勝てるなら Yes
、勝てないなら No
と出力せよ。
入力例 1
ch@ku@ai choku@@i
出力例 1
Yes
@
をうまく置き換えることによって、両方とも chokudai
と一致させることが可能です。
入力例 2
ch@kud@i akidu@ho
出力例 2
Yes
イカサマをし、@
をうまく置き換えることによって、両方とも chokudai
と一致させることが可能です。
入力例 3
aoki @ok@
出力例 3
No
イカサマをしても勝つことはできません。
入力例 4
aa bb
出力例 4
No
Score : 300 points
Problem Statement
A single-player card game is popular in AtCoder Inc.
Each card in the game has a lowercase English letter or the symbol @
written on it. There is plenty number of cards for each kind.
The game goes as follows.
- Arrange the same number of cards in two rows.
- Replace each card with
@
with one of the following cards:a
,t
,c
,o
,d
,e
,r
. - If the two rows of cards coincide, you win. Otherwise, you lose.
To win this game, you will do the following cheat.
- Freely rearrange the cards within a row whenever you want after step 1.
You are given two strings S and T, representing the two rows you have after step 1. Determine whether it is possible to win with cheating allowed.
Constraints
- S and T consist of lowercase English letters and
@
. - The lengths of S and T are equal and between 1 and 2\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S T
Output
If it is possible to win with cheating allowed, print Yes
; otherwise, print No
.
Sample Input 1
ch@ku@ai choku@@i
Sample Output 1
Yes
You can replace the @
s so that both rows become chokudai
.
Sample Input 2
ch@kud@i akidu@ho
Sample Output 2
Yes
You can cheat and replace the @
s so that both rows become chokudai
.
Sample Input 3
aoki @ok@
Sample Output 3
No
You cannot win even with cheating.
Sample Input 4
aa bb
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
正の整数 a があります。また、黒板に 1 個の数が 10 進表記で書かれています。
黒板に現在書かれている数を x としたとき、高橋君は次のいずれかの操作を行い、黒板に書かれている数を変化させることができます。
- x を消し、 x を a 倍した数を 10 進表記で新たに書きこむ。
- x を文字列とみなして、列の末尾の数字を文字列の先頭に移動させる。
ただし、この操作は x \geq 10 かつ x が 10 で割り切れないときにしか行えない。
たとえば a = 2, x = 123 であるとき、高橋君は次のいずれかの操作を行うことができます。
- x を消して、 x \times a = 123 \times 2 = 246 を新たに書きこむ。
- x を文字列とみなして、
123
の末尾の数字である3
を先頭に移動させる。黒板に書かれている数は 123 から 312 に変化する。
はじめ、黒板には 1 が書かれています。書かれている数を N に変化させるには最小で何回の操作が必要ですか?ただし、どのように操作しても書かれている数を N に変化させられない場合は -1 を出力してください。
制約
- 2 \leq a \lt 10^6
- 2 \leq N \lt 10^6
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
a N
出力
答えを出力せよ。
入力例 1
3 72
出力例 1
4
以下に説明する操作を行うことで、 黒板に書かれている数を 4 回で 1 から 72 に変化させることができます。
- 1 つ目の操作を行う。黒板に書かれている数は 1 \to 3 に変わる。
- 1 つ目の操作を行う。黒板に書かれている数は 3 \to 9 に変わる。
- 1 つ目の操作を行う。黒板に書かれている数は 9 \to 27 に変わる。
- 2 つ目の操作を行う。黒板に書かれている数は 27 \to 72 に変わる。
3 回以下の操作で 72 に変化させることはできないため、答えは 4 になります。
入力例 2
2 5
出力例 2
-1
どのように操作しても黒板に書かれている数を 5 に変化させることはできません。
入力例 3
2 611
出力例 3
12
適切に操作を選ぶことで、 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 611 と 12 回の操作で黒板に書かれている数を 611 に変化させることができ、これが最小です。
入力例 4
2 767090
出力例 4
111
Score : 400 points
Problem Statement
We have a positive integer a. Additionally, there is a blackboard with a number written in base 10.
Let x be the number on the blackboard. Takahashi can do the operations below to change this number.
- Erase x and write x multiplied by a, in base 10.
- See x as a string and move the rightmost digit to the beginning.
This operation can only be done when x \geq 10 and x is not divisible by 10.
For example, when a = 2, x = 123, Takahashi can do one of the following.
- Erase x and write x \times a = 123 \times 2 = 246.
- See x as a string and move the rightmost digit
3
of123
to the beginning, changing the number from 123 to 312.
The number on the blackboard is initially 1. What is the minimum number of operations needed to change the number on the blackboard to N? If there is no way to change the number to N, print -1.
Constraints
- 2 \leq a \lt 10^6
- 2 \leq N \lt 10^6
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
a N
Output
Print the answer.
Sample Input 1
3 72
Sample Output 1
4
We can change the number on the blackboard from 1 to 72 in four operations, as follows.
- Do the operation of the first type: 1 \to 3.
- Do the operation of the first type: 3 \to 9.
- Do the operation of the first type: 9 \to 27.
- Do the operation of the second type: 27 \to 72.
It is impossible to reach 72 in three or fewer operations, so the answer is 4.
Sample Input 2
2 5
Sample Output 2
-1
It is impossible to change the number on the blackboard to 5.
Sample Input 3
2 611
Sample Output 3
12
There is a way to change the number on the blackboard to 611 in 12 operations: 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 611, which is the minimum possible.
Sample Input 4
2 767090
Sample Output 4
111
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder国には 1 から N の番号がついた N 個の街と 1 から N-1 の番号がついた N-1 本の道路があります。
道路 i は街 A_i と街 B_i を双方向に結び、長さは C_i です。どの街同士も、いくつかの道路を通って互いに行き来することができます。
いずれかの街を出発し、道路による移動で全ての街を 1 度以上訪れるための移動距離の最小値を求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- 1 \leq C_i \leq 10^9
- 入力は全て整数である
- どの街同士も、いくつかの道路を通って互いに行き来できる
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 C_1 \vdots A_{N-1} B_{N-1} C_{N-1}
出力
答えを出力せよ。
入力例 1
4 1 2 2 1 3 3 1 4 4
出力例 1
11
4 \to 1 \to 2 \to 1 \to 3 と移動すると移動距離の合計は 11 となり、これが最小値です。
最初の街に戻ってくる必要はないことに注意してください。
入力例 2
10 10 9 1000000000 9 8 1000000000 8 7 1000000000 7 6 1000000000 6 5 1000000000 5 4 1000000000 4 3 1000000000 3 2 1000000000 2 1 1000000000
出力例 2
9000000000
オーバーフローに注意してください。
Score : 500 points
Problem Statement
In the nation of AtCoder, there are N cities numbered 1 to N and N-1 roads numbered 1 to N-1.
Road i connects cities A_i and B_i bidirectionally, and its length is C_i. Any pair of cities can be reached from each other by traveling through some roads.
Find the minimum travel distance required to start from a city and visit all cities at least once using the roads.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i, B_i \leq N
- 1 \leq C_i \leq 10^9
- All input values are integers.
- Any pair of cities can be reached from each other by traveling through some roads.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 C_1 \vdots A_{N-1} B_{N-1} C_{N-1}
Output
Print the answer.
Sample Input 1
4 1 2 2 1 3 3 1 4 4
Sample Output 1
11
If you travel as 4 \to 1 \to 2 \to 1 \to 3, the total travel distance is 11, which is the minimum.
Note that you do not need to return to the starting city.
Sample Input 2
10 10 9 1000000000 9 8 1000000000 8 7 1000000000 7 6 1000000000 6 5 1000000000 5 4 1000000000 4 3 1000000000 3 2 1000000000 2 1 1000000000
Sample Output 2
9000000000
Beware overflow.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
とあるゲームの大会に、プレイヤー 1 、プレイヤー 2 、\ldots 、プレイヤー N の N 人のプレイヤーが参加します。 大会の開始直前、各プレイヤーはそれぞれ 1 人のみからなるチームをなし、全部で N 個のチームがあります。
大会では全部で N−1 回の試合があり、各試合では 2 つの異なるチームが選ばれ、一方が先攻を、もう一方が後攻を受け持って対戦し、その結果ちょうど一方のチームが勝ちます。 具体的には、i = 1, 2, \ldots, N-1 について i 回目の試合は下記の通りに進行します。
- プレイヤー p_i の属するチームが先攻、プレイヤー q_i の属するチームが後攻として、対戦を行う。
- その結果、先攻チームの人数を a 、後攻チームの人数を b として、\frac{a}{a+b} の確率で先攻のチームが、\frac{b}{a+b} の確率で後攻のチームが勝つ。
- その後、勝負した 2 チームは 1 つのチームに併合される。
なお、各試合の対戦結果は他の試合の対戦結果とは独立です。
N 人のプレイヤーそれぞれについて、大会全体で自分が所属するチームが勝つという出来事が起こる回数の期待値 \text{mod } 998244353 を出力してください。
期待値 \text{mod } 998244353 の定義
この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac{y}{x} で表したときに x が 998244353 で割り切れないことが保証されます。
このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq p_i, q_i \leq N
- i 回目の試合の直前、プレイヤー p_i が属するチームとプレイヤー q_i が属するチームは異なる。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N p_1 q_1 p_2 q_2 \vdots p_{N-1} q_{N-1}
出力
各 i = 1, 2, \ldots, N について、大会全体でプレイヤー i が所属するチームが勝つという出来事が起こる回数の期待値 \text{mod } 998244353 である E_i を、 下記の形式にしたがって空白区切りで出力せよ。
E_1 E_2 \ldots E_N
入力例 1
5 1 2 4 3 5 3 1 4
出力例 1
698771048 698771048 964969543 964969543 133099248
チームに所属するプレイヤーの番号が x_1, x_2, \ldots, x_k であるチームを、チーム \lbrace x_1, x_2, \ldots, x_k \rbrace と呼びます。
- 1 回目の試合では、プレイヤー 1 が所属するチーム \lbrace 1 \rbrace とプレイヤー 2 が所属するチーム \lbrace 2 \rbrace が対戦し、 \frac{1}{2} の確率でチーム \lbrace 1 \rbrace が、\frac{1}{2} の確率でチーム \lbrace 2 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 1, 2 \rbrace になります。
- 2 回目の試合では、プレイヤー 4 が所属するチーム \lbrace 4 \rbrace とプレイヤー 3 が所属するチーム \lbrace 3 \rbrace が対戦し、 \frac{1}{2} の確率でチーム \lbrace 4 \rbrace が、\frac{1}{2} の確率でチーム \lbrace 3 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 3, 4 \rbrace になります。
- 3 回目の試合では、プレイヤー 5 が所属するチーム \lbrace 5 \rbrace とプレイヤー 3 が所属するチーム \lbrace 3, 4 \rbrace が対戦し、 \frac{1}{3} の確率でチーム \lbrace 5 \rbrace が、\frac{2}{3} の確率でチーム \lbrace 3, 4 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 3, 4, 5 \rbrace になります。
- 4 回目の試合では、プレイヤー 1 が所属するチーム \lbrace 1, 2 \rbrace とプレイヤー 4 が所属するチーム \lbrace 3, 4, 5 \rbrace が対戦し、 \frac{2}{5} の確率でチーム \lbrace 1, 2 \rbrace が、\frac{3}{5} の確率でチーム \lbrace 3, 4, 5 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 1, 2, 3, 4, 5 \rbrace になります。
プレイヤー 1, 2, 3, 4, 5 それぞれの、大会全体で自分が所属するチームが勝つという出来事が起こる回数の期待値 E_1, E_2, E_3, E_4, E_5 は、それぞれ \frac{9}{10}, \frac{9}{10}, \frac{53}{30}, \frac{53}{30}, \frac{14}{15} です。
入力例 2
15 9 2 8 10 13 6 12 11 7 10 4 10 14 2 5 4 1 15 15 2 6 9 8 11 6 3 2 8
出力例 2
43970290 310168785 806914186 501498951 950708909 272140427 335124893 168750835 310168785 168750835 280459129 280459129 272140427 476542843 43970290
Score : 475 points
Problem Statement
N players, player 1, player 2, ..., player N, participate in a game tournament. Just before the tournament starts, each player forms a one-person team, so there are N teams in total.
The tournament has a total of N-1 matches. In each match, two different teams are chosen. One team goes first, and the other goes second. Each match will result in exactly one team winning. Specifically, for each i = 1, 2, \ldots, N-1, the i-th match proceeds as follows.
- The team with player p_i goes first, and the team with player q_i goes second.
- Let a and b be the numbers of players in the first and second teams, respectively. The first team wins with probability \frac{a}{a+b}, and the second team wins with probability \frac{b}{a+b}.
- Then, the two teams are combined into a single team.
The result of each match is independent of those of the others.
For each of the N players, print the expected number of times the team with that player wins throughout the tournament, modulo 998244353.
How to print an expected value modulo 998244353
It can be proved that the sought expected value is always rational. Also, the constraints of this problem guarantee that if the sought expected value is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.
Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq p_i, q_i \leq N
- Just before the i-th match, player p_i and player q_i belong to different teams.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N p_1 q_1 p_2 q_2 \vdots p_{N-1} q_{N-1}
Output
For each i = 1, 2, \ldots, N, print E_i, the expected number, modulo 998244353, of times the team with player i wins throughout the tournament, separated by spaces, in the following format:
E_1 E_2 \ldots E_N
Sample Input 1
5 1 2 4 3 5 3 1 4
Sample Output 1
698771048 698771048 964969543 964969543 133099248
We call a team formed by player x_1, player x_2, \ldots, player x_k as team \lbrace x_1, x_2, \ldots, x_k \rbrace.
- The first match is played by team \lbrace 1 \rbrace, with player 1, and team \lbrace 2 \rbrace, with player 2. Team \lbrace 1 \rbrace wins with probability \frac{1}{2}, and team \lbrace 2 \rbrace wins with probability \frac{1}{2}. Then, the two teams are combined into a single team \lbrace 1, 2 \rbrace.
- The second match is played by team \lbrace 4 \rbrace, with player 4, and team \lbrace 3 \rbrace, with player 3. Team \lbrace 4 \rbrace wins with probability \frac{1}{2}, and team \lbrace 3 \rbrace wins with probability \frac{1}{2}. Then, the two teams are combined into a single team \lbrace 3, 4 \rbrace.
- The third match is played by team \lbrace 5 \rbrace, with player 5, and team \lbrace 3, 4 \rbrace, with player 3. Team \lbrace 5 \rbrace wins with probability \frac{1}{3}, and team \lbrace 3, 4 \rbrace wins with probability \frac{2}{3}. Then, the two teams are combined into a single team \lbrace 3, 4, 5 \rbrace.
- The fourth match is played by team \lbrace 1, 2 \rbrace, with player 1, and team \lbrace 3, 4, 5 \rbrace, with player 4. Team \lbrace 1, 2 \rbrace wins with probability \frac{2}{5}, and team \lbrace 3, 4, 5 \rbrace wins with probability \frac{3}{5}. Then, the two teams are combined into a single team \lbrace 1, 2, 3, 4, 5 \rbrace.
The expected numbers of times the teams with players 1, 2, 3, 4, 5 win throughout the tournament, E_1, E_2, E_3, E_4, E_5, are \frac{9}{10}, \frac{9}{10}, \frac{53}{30}, \frac{53}{30}, \frac{14}{15}, respectively.
Sample Input 2
15 9 2 8 10 13 6 12 11 7 10 4 10 14 2 5 4 1 15 15 2 6 9 8 11 6 3 2 8
Sample Output 2
43970290 310168785 806914186 501498951 950708909 272140427 335124893 168750835 310168785 168750835 280459129 280459129 272140427 476542843 43970290