実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
配信者の高橋君は L 時から R 時に配信をすることにしました。
高橋君には N 人のリスナーがおり、i 人目のリスナーは X_i 時から Y_i 時まで配信を見ることができます。
高橋君の配信を最初から最後まで見ることができるリスナーは何人いますか?
制約
- 1\leq N\leq 100
- 0\leq L\lt R\leq 23
- 0\leq X_i\lt Y_i\leq 23
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L R X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
高橋君の配信を最初から最後まで見ることができるリスナーの数を出力せよ。
入力例 1
5 19 22 17 23 20 23 19 22 0 23 12 20
出力例 1
3
高橋君の配信を最初から最後まで見ることができるのは 1,3,4 人目のリスナーです。
入力例 2
3 12 13 0 1 0 1 0 1
出力例 2
0
高橋君の配信を最初から最後まで見ることができるリスナーはいません。
入力例 3
10 8 14 5 20 14 21 9 21 5 23 8 10 0 14 3 8 2 6 0 16 5 20
出力例 3
5
Score : 100 points
Problem Statement
Streamer Takahashi has decided to stream from L o'clock to R o'clock (using the 24-hour clock).
He has N listeners, and the i-th listener can watch the stream from X_i o'clock to Y_i o'clock.
How many listeners can watch Takahashi's stream from beginning to end?
Constraints
- 1\leq N\leq 100
- 0\leq L\lt R\leq 23
- 0\leq X_i\lt Y_i\leq 23
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L R X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Output the number of listeners who can watch Takahashi's stream from beginning to end.
Sample Input 1
5 19 22 17 23 20 23 19 22 0 23 12 20
Sample Output 1
3
The listeners who can watch Takahashi's stream from beginning to end are the 1st, 3rd, and 4th listeners.
Sample Input 2
3 12 13 0 1 0 1 0 1
Sample Output 2
0
No listeners can watch Takahashi's stream from beginning to end.
Sample Input 3
10 8 14 5 20 14 21 9 21 5 23 8 10 0 14 3 8 2 6 0 16 5 20
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
連長圧縮(ランレングス圧縮)を復元してください。ただし、長すぎる場合には
Too Longと出力してください。
N 個の文字と整数の組 (c_1,l_1),(c_2,l_2),\ldots,(c_N,l_N) が与えられます。
l_1 個の文字 c_1、l_2 個の文字 c_2、\ldots、l_N 個の文字 c_N をこの順に連結させた文字列を S とします。
S を出力してください。ただし、S の長さが 100 を超える場合には代わりに Too Long と出力してください。
制約
- 1\leq N\leq 100
- 1\leq l_i\leq 10^{18}
- N,l_i は整数
- c_i は英小文字
- c_i\neq c_{i+1}
入力
入力は以下の形式で標準入力から与えられる。
N c_1 l_1 c_2 l_2 \vdots c_N l_N
出力
S の長さが 100 以下なら S を、そうでないなら Too Long と出力せよ。
入力例 1
8 m 1 i 1 s 2 i 1 s 2 i 1 p 2 i 1
出力例 1
mississippi
S は mississippi です。S の長さは 100 以下であるため S を出力します。
入力例 2
7 a 1000000000000000000 t 1000000000000000000 c 1000000000000000000 o 1000000000000000000 d 1000000000000000000 e 1000000000000000000 r 1000000000000000000
出力例 2
Too Long
S の長さは 7\times 10^{18} であるため、Too Long を出力します。
入力例 3
1 a 100
出力例 3
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
入力例 4
6 g 4 j 1 m 4 e 4 d 3 i 4
出力例 4
ggggjmmmmeeeedddiiii
Score : 200 points
Problem Statement
Restore run-length encoding. If the result is too long, output
Too Long.
You are given N pairs of characters and integers (c_1,l_1),(c_2,l_2),\ldots,(c_N,l_N).
Let S be the string formed by concatenating l_1 characters c_1, l_2 characters c_2, \ldots, and l_N characters c_N in this order.
Output S. However, if the length of S exceeds 100, output Too Long instead.
Constraints
- 1\leq N\leq 100
- 1\leq l_i\leq 10^{18}
- N and l_i are integers.
- Each c_i is a lowercase English letter.
- c_i\neq c_{i+1}
Input
The input is given from Standard Input in the following format:
N c_1 l_1 c_2 l_2 \vdots c_N l_N
Output
If the length of S is at most 100, output S; otherwise, output Too Long.
Sample Input 1
8 m 1 i 1 s 2 i 1 s 2 i 1 p 2 i 1
Sample Output 1
mississippi
S is mississippi. Since the length of S is not greater than 100, output S.
Sample Input 2
7 a 1000000000000000000 t 1000000000000000000 c 1000000000000000000 o 1000000000000000000 d 1000000000000000000 e 1000000000000000000 r 1000000000000000000
Sample Output 2
Too Long
The length of S is 7\times 10^{18}, so output Too Long.
Sample Input 3
1 a 100
Sample Output 3
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Sample Input 4
6 g 4 j 1 m 4 e 4 d 3 i 4
Sample Output 4
ggggjmmmmeeeedddiiii
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
414 の十進法での表記は 414 であり、これは回文です。
また、414 の八進法での表記は 636 であり、これも回文です。
これを踏まえて、以下の問題を解いてください。
正の整数 A, N が与えられます。 1 以上 N 以下の整数のうち、十進法での表記も A 進法での表記も回文であるようなものの総和を求めてください。
なお、この問題の制約下で答えは 2^{63} 未満であることが証明できます。
制約
- 2 \leq A \leq 9
- 1 \leq N \le 10^{12}
- 入力される数値は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
A N
出力
答えを 1 行で出力せよ。
入力例 1
8 1000
出力例 1
2155
条件を満たす整数は 1,2,3,4,5,6,7,9,121,292,333,373,414,585 の 14 個であり、その総和は 2155 です。
入力例 2
8 999999999999
出力例 2
914703021014
入力例 3
6 999999999999
出力例 3
283958331810
Score : 350 points
Problem Statement
The decimal representation of 414 is 414, which is a palindrome.
Also, the octal representation of 414 is 636, which is also a palindrome.
Based on this, solve the following problem.
You are given positive integers A and N. Find the sum of all integers between 1 and N, inclusive, whose decimal representation and base-A representation are both palindromes.
Under the constraints of this problem, it can be proved that the answer is less than 2^{63}.
Constraints
- 2 \leq A \leq 9
- 1 \leq N \le 10^{12}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A N
Output
Output the answer in one line.
Sample Input 1
8 1000
Sample Output 1
2155
The integers satisfying the condition are 1,2,3,4,5,6,7,9,121,292,333,373,414,585 (14 integers), and their sum is 2155.
Sample Input 2
8 999999999999
Sample Output 2
914703021014
Sample Input 3
6 999999999999
Sample Output 3
283958331810
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
数直線上に N 棟の家があり、1 から N までの番号が付けられています。 家 i は座標 X_i に位置しています。同じ座標に複数の家が位置していることもあります。
あなたは M 個の基地局を数直線上の任意の実数座標に配置します。そして、それぞれの基地局に対して非負整数の値の電波強度を設定します。
ある基地局の電波強度を x にしたとき、その基地局からの電波が家に届く条件は、その基地局とその家の距離が \displaystyle\frac{x}{2} 以下であることです。 特に x=0 の場合、その基地局と同じ座標に位置する家にのみ電波が届きます。
どの家にも少なくとも 1 つの基地局から電波が届くように基地局の位置と電波強度を設定するとき、電波強度の総和がとりうる最小値を求めてください。
なお、制約を満たす任意の入力に対して答えが整数であることが証明できます。
制約
- 1 \leq M \leq N \leq 5 \times 10^5
- 1 \leq X_i \leq 10^{12} (1 \leq i \leq N)
- 入力される数値は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 \dots X_N
出力
答えを整数として 1 行で出力せよ。
入力例 1
7 3 5 10 15 20 8 14 15
出力例 1
6
以下のように 3 つの基地局を置くことで、すべての家に電波が届きます。
- 座標 7.5 に電波強度 5 の基地局を置く。この基地局から電波が届くのは家 1,2,5 である。
- 座標 14.5 に電波強度 1 の基地局を置く。この基地局から電波が届くのは家 3,6,7 である。
- 座標 20 に電波強度 0 の基地局を置く。この基地局から電波が届くのは家 4 である。
このときの電波強度の総和は 6 です。
電波強度の総和が 6 より小さい配置で条件を達成することはできないので、6 を出力します。
入力例 2
7 7 5 10 15 20 8 14 15
出力例 2
0
入力例 3
7 1 5 10 15 20 8 14 15
出力例 3
15
Score : 400 points
Problem Statement
There are N houses numbered from 1 to N on a number line. House i is located at coordinate X_i. Multiple houses may be located at the same coordinate.
You place M base stations at arbitrary real coordinates on the number line. Then, you set a non-negative integer signal strength for each base station.
When the signal strength of a base station is set to x, The signal from that base station reaches a house if and only if the distance between the base station and the house is at most \displaystyle\frac{x}{2}. Particularly, when x=0, the signal reaches only houses located at the same coordinate as the base station.
Find the minimum possible sum of signal strengths when the positions and signal strengths of the base stations are set such that at least one base station's signal reaches every house.
It can be proved that the answer is an integer for any input satisfying the constraints.
Constraints
- 1 \leq M \leq N \leq 5 \times 10^5
- 1 \leq X_i \leq 10^{12} (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 \dots X_N
Output
Output the answer as an integer in one line.
Sample Input 1
7 3 5 10 15 20 8 14 15
Sample Output 1
6
By placing three base stations as follows, signals reach all houses.
- Place a base station with signal strength 5 at coordinate 7.5. This base station reaches houses 1,2,5.
- Place a base station with signal strength 1 at coordinate 14.5. This base station reaches houses 3,6,7.
- Place a base station with signal strength 0 at coordinate 20. This base station reaches house 4.
The sum of signal strengths in this case is 6.
It is impossible to satisfy the condition with an arrangement where the sum of signal strengths is smaller than 6, so output 6.
Sample Input 2
7 7 5 10 15 20 8 14 15
Sample Output 2
0
Sample Input 3
7 1 5 10 15 20 8 14 15
Sample Output 3
15
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
整数の組 (a, b, c) であって以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。
- 1 \leq a,b,c \leq N
- a,b,c は相異なる。
- a を b で割ったあまりは c に等しい。
制約
- 3 \leq N \leq 10^{12}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを 1 行で出力せよ。
入力例 1
7
出力例 1
12
条件を満たす整数の組 (a,b,c) は以下の 12 通り存在します。
- (3,2,1)
- (4,3,1)
- (5,2,1)
- (5,3,2)
- (5,4,1)
- (6,4,2)
- (6,5,1)
- (7,2,1)
- (7,3,1)
- (7,4,3)
- (7,5,2)
- (7,6,1)
入力例 2
441
出力例 2
94700
入力例 3
411111111114
出力例 3
462474062
Score : 475 points
Problem Statement
Find the number, modulo 998244353, of integer tuples (a, b, c) that satisfy the following conditions.
- 1 \leq a,b,c \leq N
- a,b,c are pairwise distinct.
- The remainder when a is divided by b is equal to c.
Constraints
- 3 \leq N \leq 10^{12}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Output the answer in one line.
Sample Input 1
7
Sample Output 1
12
There are 12 integer tuples (a,b,c) that satisfy the conditions:
- (3,2,1)
- (4,3,1)
- (5,2,1)
- (5,3,2)
- (5,4,1)
- (6,4,2)
- (6,5,1)
- (7,2,1)
- (7,3,1)
- (7,4,3)
- (7,5,2)
- (7,6,1)
Sample Input 2
441
Sample Output 2
94700
Sample Input 3
411111111114
Sample Output 3
462474062
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
頂点に 1 から N の番号がつけられた N 頂点の木および整数 K が与えられます。i 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。
あなたはいま頂点 1 にいます。以下の操作を 0 回以上繰り返すことができます。
- あなたが今いる頂点からの距離が K であるような頂点を一つ選び、その頂点に移動する。ただし、2 頂点間の距離は 2 頂点を結ぶ単純パスに含まれる辺の個数とする。
各 k=2,\ldots,N に対して、操作を繰り返して頂点 k に移動することができるかどうか判定し、移動できるならば操作回数の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて解いてください。
制約
- 1\leq T\leq 10^5
- 2\leq N\leq 2\times 10^5
- 1\leq K\leq 20
- 1\leq u_i\lt v_i\leq N
- 与えられるグラフは木
- 全てのテストケースに対する N の総和は 2\times 10^5 以下
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N K
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを以下の形式で出力せよ。
\mathrm{ans}_2 \mathrm{ans}_3 \ldots \mathrm{ans}_N
\mathrm{ans}_i は k=i に対する答えである。操作を繰り返して頂点 i に移動することができるならば操作回数の最小値を、移動することができないならば -1 とせよ。
入力例 1
1 6 2 1 2 2 3 2 4 4 5 5 6
出力例 1
-1 1 1 -1 2
この入出力例は 1 つのテストケースからなります。
頂点 1 との距離が 2 である頂点は 3,4 であるため、この 2 つの頂点には 1 回の操作で移動することができます。
また、頂点 1 から頂点 4 に移動したのち、頂点 4 からの距離が 2 である頂点 6 に移動することで、頂点 6 には 2 回の操作で移動することができます。
頂点 2,5 にはどのように操作しても移動することができません。
入力例 2
3 2 20 1 2 10 2 1 9 1 8 1 5 6 8 4 5 2 8 5 10 7 9 3 5 10 1 2 6 2 9 8 9 9 10 3 9 4 9 7 9 1 6 3 5
出力例 2
-1 1 1 1 -1 1 1 -1 -1 1 2 4 4 5 1 4 4 3 4
Score : 525 points
Problem Statement
You are given a tree with N vertices numbered from 1 to N and an integer K. The i-th edge bidirectionally connects vertices u_i and v_i.
You are currently at vertex 1. You can repeat the following operation zero or more times:
- Choose a vertex whose distance from the vertex you are currently at is K, and move to that vertex. Here, the distance between two vertices is the number of edges in the simple path connecting the two vertices.
For each k=2,\ldots,N, determine whether you can move to vertex k by repeating the operation, and if you can move, find the minimum number of operations.
You have T test cases, so solve each of them.
Constraints
- 1\leq T\leq 10^5
- 2\leq N\leq 2\times 10^5
- 1\leq K\leq 20
- 1\leq u_i\lt v_i\leq N
- The given graph is a tree.
- The sum of N over all test cases is at most 2\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{case}_i means the i-th test case.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N K
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Output T lines. The i-th line should contain the answer for the i-th test case in the following format:
\mathrm{ans}_2 \mathrm{ans}_3 \ldots \mathrm{ans}_N
\mathrm{ans}_i is the answer for k=i. If you can move to vertex i by repeating the operation, \mathrm{ans}_i is the minimum number of operations; otherwise, \mathrm{ans}_i is -1.
Sample Input 1
1 6 2 1 2 2 3 2 4 4 5 5 6
Sample Output 1
-1 1 1 -1 2
This sample input/output consists of one test case.
The vertices whose distance from vertex 1 is 2 are vertices 3 and 4, so you can move to these two vertices with one operation.
Also, by moving from vertex 1 to vertex 4, then moving to vertex 6 whose distance from vertex 4 is 2, you can move to vertex 6 with two operations.
You cannot move to vertices 2 and 5 no matter how you perform the operations.
Sample Input 2
3 2 20 1 2 10 2 1 9 1 8 1 5 6 8 4 5 2 8 5 10 7 9 3 5 10 1 2 6 2 9 8 9 9 10 3 9 4 9 7 9 1 6 3 5
Sample Output 2
-1 1 1 1 -1 1 1 -1 -1 1 2 4 4 5 1 4 4 3 4
実行時間制限: 2.5 sec / メモリ制限: 1024 MiB
配点 : 625 点
問題文
AtCoder 王国には東西に伸びる路線があり、この路線には駅 1,2,\ldots,N があります。駅 i\ (1\leq i\leq N) は座標 x_i に位置しています (x_1 < x_2 < \ldots < x_N)。
この路線で AtCoder Express 社は M 種類の特急列車を運行しています。
i 種類目の特急列車は以下のように運行されます。
- 乗車できる駅は駅 l_i,l_i+1,\ldots,r_i のいずれかである。
- 降車できる駅は駅 L_i, L_i+1, \ldots, R_i のいずれかである。ここで、列車の進行方向は東西のどちらか一方向であり、r_i\lt L_i または R_i\lt l_i のいずれかが成り立つ。
- 運賃は、初乗り運賃として c_i がかかり、乗車駅から降車駅までの距離に応じた金額が加算される。具体的には、駅 s\ (l_i\leq s\leq r_i) から駅 t\ (L_i\leq t\leq R_i) まで移動する場合の運賃は c_i+|x_s-x_t| である。
あなたはいま駅 1 にいます。各 k\ (2\leq k\leq N) について、特急列車を乗り継いで駅 1 から駅 k まで移動することができるかどうか判定し、移動できるならば移動するために必要な運賃の合計の最小値を求めてください。
制約
- 2\leq N\leq 10^5
- 1\leq M\leq 10^5
- 0\leq x_1 < x_2 < \ldots < x_N \leq 10^{12}
- 1\leq l_i\leq r_i\leq N, 1\leq L_i\leq R_i\leq N
- r_i\lt L_i または R_i\lt l_i のいずれかが成り立つ
- 1\leq c_i\leq 10^{12}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M x_1 x_2 \ldots x_N l_1 r_1 L_1 R_1 c_1 l_2 r_2 L_2 R_2 c_2 \vdots l_M r_M L_M R_M c_M
出力
答えを以下の形式で出力せよ。
\mathrm{ans}_2 \mathrm{ans}_3 \ldots \mathrm{ans}_N
\mathrm{ans}_i は k=i に対する答えである。駅 1 から駅 i まで移動できる場合は移動に必要な運賃の合計の最小値を、移動できない場合は -1 とせよ。
入力例 1
6 3 0 20 50 90 110 150 1 2 5 6 100 1 1 2 3 10000 6 6 1 2 30
出力例 1
410 10050 -1 210 250
駅 2 には以下のようにして移動することができます。
- 1 種類目の特急列車に駅 1 から乗車して駅 6 で降車する。運賃は c_1+|x_1-x_6|=100+|0-150|=250 である。
- 3 種類目の特急列車に駅 6 から乗車して駅 2 で降車する。運賃は c_3+|x_6-x_2|=30+|150-20|=160 である。
このときの運賃の合計は 410 で、これが最小です。
駅 4 には移動することができません。
入力例 2
10 5 4427 6839 17992 39701 46954 76602 81804 91814 95651 95895 3 4 10 10 60978 1 1 4 4 30037 9 10 7 8 66643 4 4 1 2 50872 8 10 3 7 23949
出力例 2
149045 284335 65311 255373 225725 220523 253207 -1 182483
Score : 625 points
Problem Statement
In AtCoder Kingdom, there is a railway line extending east-west, and this line has stations 1,2,\ldots,N. Station i\ (1\leq i\leq N) is located at coordinate x_i (x_1 < x_2 < \ldots < x_N).
On this line, AtCoder Express company operates M types of express trains.
The i-th type of express train operates as follows:
- You can board this train at one of the stations l_i,l_i+1,\ldots,r_i.
- You can get off this train at one of the stations L_i, L_i+1, \ldots, R_i. Here, the train travels in one direction, either east or west, satisfying r_i\lt L_i or R_i\lt l_i.
- The fare consists of a base fare of c_i plus an amount based on the distance from the boarding station to the destination station. Specifically, when traveling from station s\ (l_i\leq s\leq r_i) to station t\ (L_i\leq t\leq R_i), the fare is c_i+|x_s-x_t|.
You are currently at station 1. For each k\ (2\leq k\leq N), determine whether you can travel from station 1 to station k by transferring between express trains, and if you can travel, find the minimum total fare required for the travel.
Constraints
- 2\leq N\leq 10^5
- 1\leq M\leq 10^5
- 0\leq x_1 < x_2 < \ldots < x_N \leq 10^{12}
- 1\leq l_i\leq r_i\leq N, 1\leq L_i\leq R_i\leq N
- r_i\lt L_i or R_i\lt l_i.
- 1\leq c_i\leq 10^{12}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M x_1 x_2 \ldots x_N l_1 r_1 L_1 R_1 c_1 l_2 r_2 L_2 R_2 c_2 \vdots l_M r_M L_M R_M c_M
Output
Output the answer in the following format:
\mathrm{ans}_2 \mathrm{ans}_3 \ldots \mathrm{ans}_N
\mathrm{ans}_i is the answer for k=i. If you can travel from station 1 to station i, \mathrm{ans}_i is the minimum total fare required for the travel; otherwise, \mathrm{ans}_i is -1.
Sample Input 1
6 3 0 20 50 90 110 150 1 2 5 6 100 1 1 2 3 10000 6 6 1 2 30
Sample Output 1
410 10050 -1 210 250
You can travel to station 2 as follows:
- Board the 1st express train at station 1 and get off at station 6. The fare is c_1+|x_1-x_6|=100+|0-150|=250.
- Board the 3rd express train at station 6 and get off at station 2. The fare is c_3+|x_6-x_2|=30+|150-20|=160.
The total fare in this case is 410, which is the minimum.
You cannot travel to station 4.
Sample Input 2
10 5 4427 6839 17992 39701 46954 76602 81804 91814 95651 95895 3 4 10 10 60978 1 1 4 4 30037 9 10 7 8 66643 4 4 1 2 50872 8 10 3 7 23949
Sample Output 2
149045 284335 65311 255373 225725 220523 253207 -1 182483