Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
整数 L,R が与えられます。
L 以上 R 以下の整数がいくつあるか求めてください。
制約
- 1\le L\le R\le 100
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
L R
出力
答えを出力せよ。
入力例 1
3 5
出力例 1
3
3 以上 5 以下の整数は 3,4,5 の 3 つです。したがって、3 を出力してください。
入力例 2
1 7
出力例 2
7
入力例 3
14 79
出力例 3
66
Score : 100 points
Problem Statement
You are given integers L and R.
Find how many integers are between L and R, inclusive.
Constraints
- 1\le L\le R\le 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
L R
Output
Output the answer.
Sample Input 1
3 5
Sample Output 1
3
The integers between 3 and 5, inclusive, are 3,4,5, which is three integers. Thus, output 3.
Sample Input 2
1 7
Sample Output 2
7
Sample Input 3
14 79
Sample Output 3
66
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
音の高さが 1 オクターヴ上がるごとに、音の周波数は 2 倍になります。
周波数 X ヘルツの音の高さを Y オクターヴ上げると、その周波数は何ヘルツになりますか?
制約
- 1 \leq X \leq 444
- 1 \leq Y \leq 3
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
X Y
出力
答えを整数として 1 行に出力せよ。単位 (ヘルツ) は省いて出力すること。
入力例 1
110 2
出力例 1
440
周波数 110 ヘルツの音の 2 オクターヴ上の音について、その周波数は 110 \times 2 \times 2 = 440 ヘルツです。
入力例 2
233 3
出力例 2
1864
周波数 233 ヘルツの音の 3 オクターヴ上の音について、その周波数は 233 \times 2 \times 2 \times 2 = 1864 ヘルツです。
入力例 3
432 1
出力例 3
864
周波数 432 ヘルツの音の 1 オクターヴ上の音について、その周波数は 432 \times 2 = 864 ヘルツです。
Score : 100 points
Problem Statement
The frequency of a sound doubles for every increase of 1 octave in pitch.
If the pitch of a sound with frequency X hertz is raised by Y octaves, what will its frequency be in hertz?
Constraints
- 1 \leq X \leq 444
- 1 \leq Y \leq 3
- All input values are integers.
Input
The input is given from Standard Input in the following format:
X Y
Output
Output the answer as an integer in one line. Omit the unit (hertz).
Sample Input 1
110 2
Sample Output 1
440
For a sound 2 octaves above a sound with frequency 110 hertz, its frequency is 110 \times 2 \times 2 = 440 hertz.
Sample Input 2
233 3
Sample Output 2
1864
For a sound 3 octaves above a sound with frequency 233 hertz, its frequency is 233 \times 2 \times 2 \times 2 = 1864 hertz.
Sample Input 3
432 1
Sample Output 3
864
For a sound 1 octave above a sound with frequency 432 hertz, its frequency is 432 \times 2 = 864 hertz.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
長さ K の文字列 t の出現回数を、以下を満たす整数 i の個数として定義します。
- 1 \leq i \leq N-K+1
- S の i 文字目から i+K-1 文字目までからなる部分文字列が t に一致する。
長さ K の文字列に対する出現回数の最大値 x を求めてください。 また、出現回数が x となるような長さ K の文字列をすべて辞書順昇順に出力してください。
制約
- N, K は整数
- S は英小文字からなる長さ N の文字列
- 1 \leq K \leq N \leq 100
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
2 行出力せよ。
1 行目には、長さ K の文字列に対する出現回数の最大値 x を出力せよ。
2 行目には、出現回数が x となるような長さ K の文字列を辞書順昇順に、空白区切りで出力せよ。
入力例 1
9 3 ovowowovo
出力例 1
2 ovo owo
出現回数 2 の文字列として、以下が挙げられます。
- 文字列
ovoについて、i=1,7 が条件を満たすことから、ovoの出現回数は 2 です。 - 文字列
owoについて、i=3,5 が条件を満たすことから、owoの出現回数は 2 です。
出現回数が 2 よりも大きいような長さ 3 の文字列は存在しないので、求める最大値は 2 です。
一方で、出現回数が 2 よりも小さい文字列として、以下が挙げられます。
- 文字列
vowについて、i=2 が条件を満たすことから、vowの出現回数は 1 です。 - 文字列
oooについて、条件を満たす i が存在しないことから、oooの出現回数は 0 です。
入力例 2
9 1 ovowowovo
出力例 2
5 o
入力例 3
35 3 thequickbrownfoxjumpsoverthelazydog
出力例 3
2 the
Score : 200 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
Define the number of occurrences of a string t of length K as the number of integers i that satisfy the following condition:
- 1 \leq i \leq N-K+1
- The substring of S from the i-th character to the (i+K-1)-th character matches t.
Find the maximum value x of the number of occurrences of a string of length K. Also, output all strings of length K with x occurrences in ascending lexicographical order.
Constraints
- N, K are integers.
- S is a string of length N consisting of lowercase English letters.
- 1 \leq K \leq N \leq 100
Input
The input is given from Standard Input in the following format:
N K S
Output
Output two lines.
The first line should contain the maximum value x of the number of occurrences of a string of length K.
The second line should contain all strings of length K with x occurrences in ascending lexicographical order, separated by spaces.
Sample Input 1
9 3 ovowowovo
Sample Output 1
2 ovo owo
The following strings have two occurrences:
- For the string
ovo, i=1,7 satisfy the condition, so the number of occurrences ofovois 2. - For the string
owo, i=3,5 satisfy the condition, so the number of occurrences ofowois 2.
There is no string of length 3 with more than two occurrences, so the maximum value is 2.
On the other hand, the following are examples of strings with fewer than two occurrences:
- For the string
vow, i=2 satisfies the condition, so the number of occurrences ofvowis 1. - For the string
ooo, there is no i that satisfies the condition, so the number of occurrences ofooois 0.
Sample Input 2
9 1 ovowowovo
Sample Output 2
5 o
Sample Input 3
35 3 thequickbrownfoxjumpsoverthelazydog
Sample Output 3
2 the
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は RPG を作っています。高橋君は 2 枚の RPG 世界のマップが一致しているかを判定するプログラムを書くことにしました。
縦 H マス横 W マスの 2 つのグリッド A, B があります。グリッドの各マスには # と . のいずれかの文字が書かれています。
A と B の上から i 行目、左から j 列目に書かれている文字をそれぞれ A_{i, j}, B_{i, j} と呼びます。
次の 2 種類の操作をそれぞれ 縦方向のシフト, 横方向のシフト と呼びます。
- j=1, 2, \dots, W について次の操作を同時に行う。
- A_{1,j}, A_{2,j}, \dots, A_{H-1, j}, A_{H,j} を A_{2,j}, A_{3,j}, \dots, A_{H,j}, A_{1,j} に同時に置き換える。
- i = 1, 2, \dots, H について次の操作を同時に行う。
- A_{i,1}, A_{i,2}, \dots, A_{i,W-1}, A_{i,W} を A_{i, 2}, A_{i, 3}, \dots, A_{i,W}, A_{i,1} に同時に置き換える。
次の条件を満たす非負整数の組 (s, t) は存在しますか?存在する場合は Yes を、存在しない場合は No を出力してください。
- 縦方向のシフトを s 回行い、次に横方向のシフトを t 回行った時、操作後の A が B と一致する。
ここで、A と B が一致するとは、1 \leq i \leq H, 1 \leq j \leq W を満たす整数の組 (i, j) すべてに対して A_{i, j} = B_{i, j} が成り立つことを言います。
制約
- 2 \leq H, W \leq 30
- A_{i,j},B_{i,j} は
#または. - H, W は整数
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1,1}A_{1,2}\dots A_{1,W}
A_{2,1}A_{2,2}\dots A_{2,W}
\vdots
A_{H,1}A_{H,2}\dots A_{H,W}
B_{1,1}B_{1,2}\dots B_{1,W}
B_{2,1}B_{2,2}\dots B_{2,W}
\vdots
B_{H,1}B_{H,2}\dots B_{H,W}
出力
条件を満たす整数の組 (s, t) が存在する場合は Yes を、存在しない場合は No を出力せよ。
入力例 1
4 3 ..# ... .#. ... #.. ... .#. ...
出力例 1
Yes
(s, t) = (2, 1) とすると A と B を一致させることができます。
(s, t) = (2, 1) の時の操作の手順を説明します。はじめ、A は次の通りです。
..# ... .#. ...
まず、縦方向のシフトを行います。A は次のようになります。
... .#. ... ..#
次に、再び縦方向のシフトを行います。A は次のようになります。
.#. ... ..# ...
最後に、横方向のシフトを行います。A は次のようになり、これは B と一致しています。
#.. ... .#. ...
入力例 2
3 2 ## ## #. .. #. #.
出力例 2
No
どのように (s, t) を選んでも A と B を一致させることはできません。
入力例 3
4 5 ##### .#... .##.. ..##. ...## #...# ##### ...#.
出力例 3
Yes
入力例 4
10 30 ..........##########.......... ..........####....###.....##.. .....##....##......##...#####. ....####...##..#####...##...## ...##..##..##......##..##....# #.##....##....##...##..##..... ..##....##.##..#####...##...## ..###..###..............##.##. .#..####..#..............###.. #..........##................. ................#..........##. ######....................#### ....###.....##............#### .....##...#####......##....##. .#####...##...##....####...##. .....##..##....#...##..##..##. ##...##..##.....#.##....##.... .#####...##...##..##....##.##. ..........##.##...###..###.... ...........###...#..####..#...
出力例 4
Yes
Score : 200 points
Problem Statement
Takahashi is developing an RPG. He has decided to write a code that checks whether two maps are equal.
We have grids A and B with H horizontal rows and W vertical columns. Each cell in the grid has a symbol # or . written on it.
The symbols written on the cell at the i-th row from the top and j-th column from the left in A and B are denoted by A_{i, j} and B_{i, j}, respectively.
The following two operations are called a vertical shift and horizontal shift.
- For each j=1, 2, \dots, W, simultaneously do the following:
- simultaneously replace A_{1,j}, A_{2,j}, \dots, A_{H-1, j}, A_{H,j} with A_{2,j}, A_{3,j}, \dots, A_{H,j}, A_{1,j}.
- For each i = 1, 2, \dots, H, simultaneously do the following:
- simultaneously replace A_{i,1}, A_{i,2}, \dots, A_{i,W-1}, A_{i,W} with A_{i, 2}, A_{i, 3}, \dots, A_{i,W}, A_{i,1}.
Is there a pair of non-negative integers (s, t) that satisfies the following condition? Print Yes if there is, and No otherwise.
- After applying a vertical shift s times and a horizontal shift t times, A is equal to B.
Here, A is said to be equal to B if and only if A_{i, j} = B_{i, j} for all integer pairs (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W.
Constraints
- 2 \leq H, W \leq 30
- A_{i,j} is
#or., and so is B_{i,j}. - H and W are integers.
Input
The input is given from Standard Input in the following format:
H W
A_{1,1}A_{1,2}\dots A_{1,W}
A_{2,1}A_{2,2}\dots A_{2,W}
\vdots
A_{H,1}A_{H,2}\dots A_{H,W}
B_{1,1}B_{1,2}\dots B_{1,W}
B_{2,1}B_{2,2}\dots B_{2,W}
\vdots
B_{H,1}B_{H,2}\dots B_{H,W}
Output
Print Yes if there is a conforming integer pair (s, t); print No otherwise.
Sample Input 1
4 3 ..# ... .#. ... #.. ... .#. ...
Sample Output 1
Yes
By choosing (s, t) = (2, 1), the resulting A is equal to B.
We describe the procedure when (s, t) = (2, 1) is chosen. Initially, A is as follows.
..# ... .#. ...
We first apply a vertical shift to make A as follows.
... .#. ... ..#
Then we apply another vertical shift to make A as follows.
.#. ... ..# ...
Finally, we apply a horizontal shift to make A as follows, which equals B.
#.. ... .#. ...
Sample Input 2
3 2 ## ## #. .. #. #.
Sample Output 2
No
No choice of (s, t) makes A equal B.
Sample Input 3
4 5 ##### .#... .##.. ..##. ...## #...# ##### ...#.
Sample Output 3
Yes
Sample Input 4
10 30 ..........##########.......... ..........####....###.....##.. .....##....##......##...#####. ....####...##..#####...##...## ...##..##..##......##..##....# #.##....##....##...##..##..... ..##....##.##..#####...##...## ..###..###..............##.##. .#..####..#..............###.. #..........##................. ................#..........##. ######....................#### ....###.....##............#### .....##...#####......##....##. .#####...##...##....####...##. .....##..##....#...##..##..##. ##...##..##.....#.##....##.... .#####...##...##..##....##.##. ..........##.##...###..###.... ...........###...#..####..#...
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。
制約
- 入力は全て整数
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 1 5 4 9
出力例 1
2
A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。
- 例えば 2 要素目の 1 、 5 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
- このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。
入力例 2
6 5 1 1 1 1 1 1
出力例 2
0
入力例 3
8 3 31 43 26 6 18 36 22 13
出力例 3
18
Score : 250 points
Problem Statement
You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.
Constraints
- All inputs are integers.
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 1 5 4 9
Sample Output 1
2
Consider removing exactly two elements from A=(3,1,5,4,9).
- For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
- In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.
Sample Input 2
6 5 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
8 3 31 43 26 6 18 36 22 13
Sample Output 3
18
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
座標平面上に 2\times1 の大きさのタイルが敷き詰められています。 タイルは、次の規則に従って敷き詰められています。
- 整数の組 (i,j) に対し、正方形 A _ {i,j}=\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace は 1 つのタイルに含まれる。
- i+j が偶数のとき、A _ {i,j} と A _ {i + 1,j} は同じタイルに含まれる。
ただし、タイルは境界を含むものとし、共通部分が正の面積をもつような 2 つの異なるタイルは存在しないとします。
原点の近くでは、タイルは以下のように敷き詰められています。

高橋君は、はじめ座標平面上の点 (S _ x+0.5,S _ y+0.5) にいます。
高橋君は、次の移動を好きなだけ繰り返します。
- 上下左右の方向と正の整数 n を選ぶ。その方向に n だけ進む。
高橋君が異なるタイルを通るたび、高橋君は通行料を 1 だけ支払います。
高橋君が点 (T _ x+0.5,T _ y+0.5) にたどり着くために支払わなければならない通行料の最小値を求めてください。
制約
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
S _ x S _ y T _ x T _ y
出力
高橋君が支払わなければならない通行料の最小値を出力せよ。
入力例 1
5 0 2 5
出力例 1
5
例えば、以下のように移動することで支払う通行料を 5 にすることができます。

- 左に 1 進む。通行料を 0 支払う。
- 上に 1 進む。通行料を 1 支払う。
- 左に 1 進む。通行料を 0 支払う。
- 上に 3 進む。通行料を 3 支払う。
- 左に 1 進む。通行料を 0 支払う。
- 上に 1 進む。通行料を 1 支払う。
支払う通行料を 4 以下にすることはできないので、5 を出力してください。
入力例 2
3 1 4 1
出力例 2
0
通行料を支払わなくてよい場合もあります。
入力例 3
2552608206527595 5411232866732612 771856005518028 7206210729152763
出力例 3
1794977862420151
出力すべき値が 32\operatorname{bit} 整数の範囲に収まらない場合があることに注意してください。
Score : 350 points
Problem Statement
The coordinate plane is covered with 2\times1 tiles. The tiles are laid out according to the following rules:
- For an integer pair (i,j), the square A _ {i,j}=\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained in one tile.
- When i+j is even, A _ {i,j} and A _ {i + 1,j} are contained in the same tile.
Tiles include their boundaries, and no two different tiles share a positive area.
Near the origin, the tiles are laid out as follows:

Takahashi starts at the point (S _ x+0.5,S _ y+0.5) on the coordinate plane.
He can repeat the following move as many times as he likes:
- Choose a direction (up, down, left, or right) and a positive integer n. Move n units in that direction.
Each time he enters a tile, he pays a toll of 1.
Find the minimum toll he must pay to reach the point (T _ x+0.5,T _ y+0.5).
Constraints
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
S _ x S _ y T _ x T _ y
Output
Print the minimum toll Takahashi must pay.
Sample Input 1
5 0 2 5
Sample Output 1
5
For example, Takahashi can pay a toll of 5 by moving as follows:

- Move left by 1. Pay a toll of 0.
- Move up by 1. Pay a toll of 1.
- Move left by 1. Pay a toll of 0.
- Move up by 3. Pay a toll of 3.
- Move left by 1. Pay a toll of 0.
- Move up by 1. Pay a toll of 1.
It is impossible to reduce the toll to 4 or less, so print 5.
Sample Input 2
3 1 4 1
Sample Output 2
0
There are cases where no toll needs to be paid.
Sample Input 3
2552608206527595 5411232866732612 771856005518028 7206210729152763
Sample Output 3
1794977862420151
Note that the value to be output may exceed the range of a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の数列 A=(A_1,A_2,\ldots,A_N) と、整数 K が与えられます。
A の連続部分列のうち、要素の和が K になるものはいくつありますか?
すなわち、以下の条件を全て満たす整数の組 (l,r) はいくつありますか?
- 1\leq l\leq r\leq N
- \displaystyle\sum_{i=l}^{r}A_i = K
制約
- 1\leq N \leq 2\times 10^5
- |A_i| \leq 10^9
- |K| \leq 10^{15}
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
6 5 8 -3 5 7 0 -4
出力例 1
3
(l,r)=(1,2),(3,3),(2,6) の 3 組が条件を満たします。
入力例 2
2 -1000000000000000 1000000000 -1000000000
出力例 2
0
条件を満たす (l,r) の組が 1 つも存在しないこともあります。
Score : 400 points
Problem Statement
Given is a sequence of length N: A=(A_1,A_2,\ldots,A_N), and an integer K.
How many of the contiguous subsequences of A have the sum of K?
In other words, how many pairs of integers (l,r) satisfy all of the conditions below?
- 1\leq l\leq r\leq N
- \displaystyle\sum_{i=l}^{r}A_i = K
Constraints
- 1\leq N \leq 2\times 10^5
- |A_i| \leq 10^9
- |K| \leq 10^{15}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
6 5 8 -3 5 7 0 -4
Sample Output 1
3
(l,r)=(1,2),(3,3),(2,6) are the three pairs that satisfy the conditions.
Sample Input 2
2 -1000000000000000 1000000000 -1000000000
Sample Output 2
0
There may be no pair that satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
整数列を一堂に集めてその優劣を定める、整数列品評会が行われます。 品評会では、1 以上 K 以下の整数からなる長さ N の整数列すべてが審査対象となり、 審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつけます。
「審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつける方法」が何通りあるかを 998244353 で割ったあまりを出力してください。
ただし、2 つの方法が異なるとは「審査対象となるある数列 A = (A_1, A_2, \ldots, A_N) が存在して、 A に対してつける点数が 2 つの方法で異なる」ことを言います。
制約
- 1 \leq N, K, M \leq 10^{18}
- N, K, M は整数
入力
入力は以下の形式で標準入力から与えられる。
N K M
出力
「審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつける方法」が何通りあるかを 998244353 で割ったあまりを出力せよ。
入力例 1
2 2 2
出力例 1
16
審査対象となる数列は、(1, 1), (1, 2), (2, 1), (2, 2) の 4 つです。「審査対象の数列それぞれに対して 1 以上 2 以下の整数の点数をつける方法」は、以下の 16 通りあります。
- (1, 1) に 1 点、(1, 2) に 1 点、(2, 1) に 1 点、(2, 2) に 1 点をつける方法
- (1, 1) に 1 点、(1, 2) に 1 点、(2, 1) に 1 点、(2, 2) に 2 点をつける方法
- (1, 1) に 1 点、(1, 2) に 1 点、(2, 1) に 2 点、(2, 2) に 1 点をつける方法
- (1, 1) に 1 点、(1, 2) に 1 点、(2, 1) に 2 点、(2, 2) に 2 点をつける方法
- \cdots
- (1, 1) に 2 点、(1, 2) に 2 点、(2, 1) に 2 点、(2, 2) に 2 点をつける方法
よって、16 を出力します。
入力例 2
3 14 15926535
出力例 2
109718301
998244353 で割ったあまりを出力することに注意してください。
Score : 500 points
Problem Statement
Integer Sequence Exhibition is taking place, where integer sequences are gathered in one place and evaluated. Here, every integer sequence of length N consisting of integers between 1 and K (inclusive) is evaluated and given an integer score between 1 and M (inclusive).
Print the number, modulo 998244353, of ways to give each of the evaluated sequences a score between 1 and M.
Here, two ways are said to be different when there is an evaluated sequence A = (A_1, A_2, \ldots, A_N) that is given different scores by the two ways.
Constraints
- 1 \leq N, K, M \leq 10^{18}
- N, K, and M are integers.
Input
Input is given from Standard Input in the following format:
N K M
Output
Print the number, modulo 998244353, of ways to give each of the evaluated sequences a score between 1 and M.
Sample Input 1
2 2 2
Sample Output 1
16
Four sequences are evaluated: (1, 1), (1, 2), (2, 1), and (2, 2). There are 16 ways to give each of these sequences a score between 1 and 2, as follows.
- Give 1 to (1, 1), 1 to (1, 2), 1 to (2, 1), and 1 to (2, 2)
- Give 1 to (1, 1), 1 to (1, 2), 1 to (2, 1), and 2 to (2, 2)
- Give 1 to (1, 1), 1 to (1, 2), 2 to (2, 1), and 1 to (2, 2)
- Give 1 to (1, 1), 1 to (1, 2), 2 to (2, 1), and 2 to (2, 2)
- \cdots
- Give 2 to (1, 1), 2 to (1, 2), 2 to (2, 1), and 2 to (2, 2)
Thus, we print 16.
Sample Input 2
3 14 15926535
Sample Output 2
109718301
Be sure to print the count modulo 998244353.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder 王国は N 個の街と N-1 個の道路からなります。
街には 街 1, 街 2, \dots, 街 N と番号がついています。道路にも同様に 道路 1, 道路 2, \dots, 道路 N-1 と番号が付いています。
道路 i は街 A_i と B_i を双方向に結んでいて、通過するときに C_i の通行料がかかります。すべての異なる街の組 (i, j) に対して、道路を経由して街 i から街 j に行くことができます。
今、列 D = (D_1, D_2, \dots, D_N) が与えられます。 D_i は街 i を観光するときにかかる費用です。 このとき、街 i から街 j への旅費 E_{i,j} を、(同じ道を 2 回以上使わずに街 i から街 j へ向かうときにかかる通行料の和) に D_{j} を足したものとして定めます。
- 厳密に言い換えると、i - j 間の最短パスを i = p_0, p_1, \dots, p_{k-1}, p_k = j として、街 p_{l} と街 p_{l+1} を結ぶ道路の通行料を c_l と置いたときに E_{i,j} = D_j + \displaystyle\sum_{l=0}^{k-1} c_l と定義します。
すべての i に対して、街 i を始点として他の街へ旅行したときにありえる旅費の最大値を求めてください。
- 厳密に言い換えると、すべての i に対して \max_{1 \leq j \leq N, j \neq i} E_{i,j} を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq N (1 \leq i \leq N-1)
- 1 \leq B_i \leq N (1 \leq i \leq N-1)
- 1 \leq C_i \leq 10^9 (1 \leq i \leq N-1)
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- 整数の組 (i,j) が 1 \leq i \lt j \leq N を満たすならば、街 i から街 j へいくつかの道路を通ることで移動できる。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_{N-1} B_{N-1} C_{N-1}
D_1 D_2 \dots D_N
出力
N 行出力せよ。i 行目には \displaystyle \max_{1 \leq j \leq N, j \neq i} E_{i,j} を出力せよ。
入力例 1
3 1 2 2 2 3 3 1 2 3
出力例 1
8 6 6
すべての街の順序つき組 (i,j) に対して E_{i,j} を計算すると次のようになります。
- E_{1,2} = 2 + 2 = 4
- E_{1,3} = 5 + 3 = 8
- E_{2,1} = 2 + 1 = 3
- E_{2,3} = 3 + 3 = 6
- E_{3,1} = 5 + 1 = 6
- E_{3,2} = 3 + 2 = 5
入力例 2
6 1 2 3 1 3 1 1 4 4 1 5 1 1 6 5 9 2 6 5 3 100
出力例 2
105 108 106 109 106 14
入力例 3
6 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 5 6 1000000000 1 2 3 4 5 6
出力例 3
5000000006 4000000006 3000000006 3000000001 4000000001 5000000001
Score : 500 points
Problem Statement
The Kingdom of AtCoder is composed of N towns and N-1 roads.
The towns are labeled as Town 1, Town 2, \dots, Town N.
Similarly, the roads are labeled as Road 1, Road 2, \dots, Road N-1.
Road i connects Towns A_i and B_i bidirectionally, and you have to pay the toll of C_i to go through it. For every pair of different towns (i, j), it is possible to go from Town A_i to Town B_j via the roads.
You are given a sequence D = (D_1, D_2, \dots, D_N), where D_i is the cost to do sightseeing in Town i. Let us define the travel cost E_{i,j} from Town i to Town j as the total toll incurred when going from Town i to Town j, plus D_{j}.
- More formally, E_{i,j} is defined as D_j + \displaystyle\sum_{l=0}^{k-1} c_l, where the shortest path between i and j is i = p_0, p_1, \dots, p_{k-1}, p_k = j and the toll for the road connecting Towns p_{l} and p_{l+1} is c_l.
For every i, find the maximum possible travel cost when traveling from Town i to another town.
- More formally, for every i, find \max_{1 \leq j \leq N, j \neq i} E_{i,j}.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq N (1 \leq i \leq N-1)
- 1 \leq B_i \leq N (1 \leq i \leq N-1)
- 1 \leq C_i \leq 10^9 (1 \leq i \leq N-1)
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- It is possible to travel from Town i to Town j via some number of roads, for a pair of integers (i,j) such that 1 \leq i \lt j \leq N.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_{N-1} B_{N-1} C_{N-1}
D_1 D_2 \dots D_N
Output
Print N lines. The i-th line should contain \displaystyle \max_{1 \leq j \leq N, j \neq i} E_{i,j}.
Sample Input 1
3 1 2 2 2 3 3 1 2 3
Sample Output 1
8 6 6
The value of E_{i,j} for every ordered pair of towns (i,j) is as follows.
- E_{1,2} = 2 + 2 = 4
- E_{1,3} = 5 + 3 = 8
- E_{2,1} = 2 + 1 = 3
- E_{2,3} = 3 + 3 = 6
- E_{3,1} = 5 + 1 = 6
- E_{3,2} = 3 + 2 = 5
Sample Input 2
6 1 2 3 1 3 1 1 4 4 1 5 1 1 6 5 9 2 6 5 3 100
Sample Output 2
105 108 106 109 106 14
Sample Input 3
6 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 5 6 1000000000 1 2 3 4 5 6
Sample Output 3
5000000006 4000000006 3000000006 3000000001 4000000001 5000000001