実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
ある日、高橋君は A 時 B 分ちょうどに、青木君は C 時 D 分 1 秒に起きました。
高橋君の起床時刻が青木君より早いならば Takahashi を、そうでないならば Aoki を出力してください。
制約
- 0 \leq A \leq 23
- 0 \leq B \leq 59
- 0 \leq C \leq 23
- 0 \leq D \leq 59
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
高橋君の起床時刻が青木君より早いならば Takahashi を、そうでないならば Aoki を出力せよ。
入力例 1
7 0 6 30
出力例 1
Aoki
高橋君は 7 時ちょうどに、青木君は 6 時 30 分 1 秒に起きました。
青木君の起床時刻の方が早いため、Aoki を出力します。
入力例 2
7 30 7 30
出力例 2
Takahashi
高橋君は 7 時 30 分ちょうどに、青木君は 7 時 30 分 1 秒に起きました。
高橋君の起床時刻の方が 1 秒だけ早いため、Takahashi を出力します。
入力例 3
0 0 23 59
出力例 3
Takahashi
ある日の 0 時 0 分ちょうどはその日の 0 時 1 分の 1 分前であり、
その日の 23 時 59 分の 1 分後、すなわちいわゆる 24 時ちょうどのことではありません。
よって、高橋君の起床時刻の方が早く、Takahashi を出力します。
Score : 100 points
Problem Statement
One day, Takahashi got up at exactly B minutes past A o'clock (in 24-hour clock), and Aoki got up at exactly D minutes and 1 second past C o'clock.
If Takahashi got up earlier than Aoki, print Takahashi; otherwise, print Aoki.
Constraints
- 0 \leq A \leq 23
- 0 \leq B \leq 59
- 0 \leq C \leq 23
- 0 \leq D \leq 59
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
If Takahashi got up earlier than Aoki, print Takahashi; otherwise, print Aoki.
Sample Input 1
7 0 6 30
Sample Output 1
Aoki
Takahashi got up at 7 sharp, and Aoki got up at 30 minutes and 1 second past 6 o'clock.
Aoki was the first to get up, so Aoki should be printed.
Sample Input 2
7 30 7 30
Sample Output 2
Takahashi
Takahashi got up at exactly half past 7, and Aoki got up at 30 minutes and 1 second past 7 o'clock.
Just by one second, Takahashi was the first to get up, so Takahashi should be printed.
Sample Input 3
0 0 23 59
Sample Output 3
Takahashi
0:00 in a day is one minute before 0:01, not one minute past 23:59 ("24:00").
Thus, Takahashi was the first to get up, so Takahashi should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
英小文字からなる長さが偶数の文字列 S が与えられます。S の長さを |S|、S の i 文字目を S_i で表します。
i = 1, 2, \ldots, \frac{|S|}{2} の順に以下の操作を行い、すべての操作を終えた後の S を出力してください。
- S_{2i-1} と S_{2i} を入れ替える
制約
- S は英小文字からなる長さが偶数の文字列
- S の長さは 100 以下
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
abcdef
出力例 1
badcfe
操作を行う前は S = abcdef です。
i = 1 について操作を行うと、S_1 と S_2 が入れ替わるので S = bacdef になります。
i = 2 について操作を行うと、S_3 と S_4 が入れ替わるので S = badcef になります。
i = 3 について操作を行うと、S_5 と S_6 が入れ替わるので S = badcfe になります。
したがって、badcfe を出力します。
入力例 2
aaaa
出力例 2
aaaa
入力例 3
atcoderbeginnercontest
出力例 3
taocedbrgeniencrnoetts
Score : 100 points
Problem Statement
You are given a string S of even length consisting of lowercase English letters. Let |S| be the length of S, and S_i be the i-th character of S.
Perform the following operation for each i = 1, 2, \ldots, \frac{|S|}{2} in this order, and print the final S.
- Swap S_{2i-1} and S_{2i}.
Constraints
- S is a string of even length consisting of lowercase English letters.
- The length of S is at most 100.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
abcdef
Sample Output 1
badcfe
Initially, S = abcdef.
Performing the operation for i = 1 swaps S_1 and S_2, making S = bacdef.
Performing the operation for i = 2 swaps S_3 and S_4, making S = badcef.
Performing the operation for i = 3 swaps S_5 and S_6, making S = badcfe.
Thus, badcfe should be printed.
Sample Input 2
aaaa
Sample Output 2
aaaa
Sample Input 3
atcoderbeginnercontest
Sample Output 3
taocedbrgeniencrnoetts
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
1, \dots, N と番号付けられた N 個の都市と、都市間を結ぶ M 本の道路があります。
i \, (1 \leq i \leq M) 番目の道路は都市 A_i と都市 B_i を結んでいます。
以下の指示に従い、N 行にわたって出力してください。
- 都市 i \, (1 \leq i \leq N) と道路で直接結ばれた都市が d_i 個あるとし、それらを昇順に都市 a_{i, 1}, \dots, a_{i, d_i} とおく。
- i \, (1 \leq i \leq N) 行目には、d_i + 1 個の整数 d_i, a_{i, 1}, \dots, a_{i, d_i} を、この順番で空白区切りで出力せよ。
制約
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
- (i \neq j) ならば (A_i, B_i) \neq (A_j, B_j)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
問題文の指示に従い、N 行にわたって出力せよ。
入力例 1
6 6 3 6 1 3 5 6 2 5 1 2 1 6
出力例 1
3 2 3 6 2 1 5 2 1 6 0 2 2 6 3 1 3 5
都市 1 と道路で直接結ばれているのは都市 2, 3, 6 です。よって、d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6 であるので、1 行目には 3, 2, 3, 6 をこの順番で空白区切りで出力します。
a_{i, 1}, \dots, a_{i, d_i} は昇順に並んでいなければならないことに注意してください。例えば、1 行目に 3, 3, 2, 6 をこの順番で出力した場合、不正解となります。
入力例 2
5 10 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
出力例 2
4 2 3 4 5 4 1 3 4 5 4 1 2 4 5 4 1 2 3 5 4 1 2 3 4
Score : 200 points
Problem Statement
There are N cities numbered 1, \dots, N, and M roads connecting cities.
The i-th road (1 \leq i \leq M) connects city A_i and city B_i.
Print N lines as follows.
- Let d_i be the number of cities directly connected to city i \, (1 \leq i \leq N), and those cities be city a_{i, 1}, \dots, city a_{i, d_i}, in ascending order.
- The i-th line (1 \leq i \leq N) should contain d_i + 1 integers d_i, a_{i, 1}, \dots, a_{i, d_i} in this order, separated by spaces.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
- (A_i, B_i) \neq (A_j, B_j) if (i \neq j).
- All values in the input 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 N lines as specified in the Problem Statement.
Sample Input 1
6 6 3 6 1 3 5 6 2 5 1 2 1 6
Sample Output 1
3 2 3 6 2 1 5 2 1 6 0 2 2 6 3 1 3 5
The cities directly connected to city 1 are city 2, city 3, and city 6. Thus, we have d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6, so you should print 3, 2, 3, 6 in the first line in this order, separated by spaces.
Note that a_{i, 1}, \dots, a_{i, d_i} must be in ascending order. For instance, it is unacceptable to print 3, 3, 2, 6 in the first line in this order.
Sample Input 2
5 10 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
Sample Output 2
4 2 3 4 5 4 1 3 4 5 4 1 2 4 5 4 1 2 3 5 4 1 2 3 4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
正整数 N が与えられるので、下記で定まる長さ (N+1) の文字列 s_0s_1\ldots s_N を出力してください。
各 i = 0, 1, 2, \ldots, N について、
- 1 以上 9 以下の N の約数 j であって、i が N/j の倍数であるものが存在するとき、そのような j のうち最小のものに対応する数字を s_i とする。(よって、この場合 s_i は
1、2、\ldots 、9のいずれかである。)- そのような j が存在しないとき、s_i は
-とする。
制約
- 1 \leq N \leq 1000
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
12
出力例 1
1-643-2-346-1
以下で、いくつかの i について s_i の決め方を説明します。
-
i = 0 について、1 以上 9 以下の N の約数 j であって i が N/j の倍数であるものは、j = 1, 2, 3, 4, 6 の 5 個です。そのうち最小のものは 1 であるので、s_0 =
1です。 -
i = 4 について、1 以上 9 以下の N の約数 j であって i が N/j の倍数であるものは、j = 3, 6 の 2 個です。そのうち最小のものは 3 であるので、s_4 =
3です。 -
i = 11 について、1 以上 9 以下の N の約数 j であって i が N/j の倍数であるものは存在しないので、s_{11} =
-です。
入力例 2
7
出力例 2
17777771
入力例 3
1
出力例 3
11
Score : 200 points
Problem Statement
You are given a positive integer N. Print a string of length (N+1), s_0s_1\ldots s_N, defined as follows.
For each i = 0, 1, 2, \ldots, N,
- if there is a divisor j of N that is between 1 and 9, inclusive, and i is a multiple of N/j, then s_i is the digit corresponding to the smallest such j (s_i will thus be one of
1,2, ...,9);- if no such j exists, then s_i is
-.
Constraints
- 1 \leq N \leq 1000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
12
Sample Output 1
1-643-2-346-1
We will explain how to determine s_i for some i.
-
For i = 0, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 1, 2, 3, 4, 6. The smallest of these is 1, so s_0 =
1. -
For i = 4, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 3, 6. The smallest of these is 3, so s_4 =
3. -
For i = 11, there are no divisors j of N between 1 and 9 such that i is a multiple of N/j, so s_{11} =
-.
Sample Input 2
7
Sample Output 2
17777771
Sample Input 3
1
Sample Output 3
11
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
xy 座標平面上に N 人の人がいます。人 i は (X_i, Y_i) にいます。すべての人は異なる地点にいます。
L, R からなる長さ N の文字列 S があります。
人 i は S_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。
たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq X_i \leq 10^9
- 0 \leq Y_i \leq 10^9
- i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
- X_i, Y_i はすべて整数である。
- S は
LおよびRからなる長さ N の文字列である。
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N S
出力
衝突が発生するならば Yes を、発生しないならば No を出力せよ。
入力例 1
3 2 3 1 1 4 1 RRL
出力例 1
Yes
この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。
入力例 2
2 1 1 2 1 RR
出力例 2
No
人 1 と人 2 は同じ向きに歩いているので衝突することはありません。
入力例 3
10 1 3 1 4 0 0 0 2 0 4 3 1 2 4 4 2 4 4 3 3 RLRRRLRLRR
出力例 3
Yes
Score : 300 points
Problem Statement
There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.
We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.
For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq X_i \leq 10^9
- 0 \leq Y_i \leq 10^9
- (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
- All X_i and Y_i are integers.
- S is a string of length N consisting of
LandR.
Input
Input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N S
Output
If there will be a collision, print Yes; otherwise, print No.
Sample Input 1
3 2 3 1 1 4 1 RRL
Sample Output 1
Yes
This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.
Sample Input 2
2 1 1 2 1 RR
Sample Output 2
No
Since Person 1 and Person 2 walk in the same direction, they never collide.
Sample Input 3
10 1 3 1 4 0 0 0 2 0 4 3 1 2 4 4 2 4 4 3 3 RLRRRLRLRR
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。
以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。
1 x: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。2 x: S の x 番目の文字を出力する。
制約
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S は英小文字からなる。
2 xの形式のクエリが 1 個以上与えられる。- N,Q,x はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
それぞれのクエリは以下の形式で与えられる。ここで、t は 1 または 2 である。
t x
出力
2 x の形式の各クエリについて、答えを一行に出力せよ。
入力例 1
3 3 abc 2 2 1 1 2 2
出力例 1
b a
1 個目のクエリのとき、S は abc なので 2 文字目の b を出力します。
2 個目のクエリのとき、S は abc から cab に変わります。
3 個目のクエリのとき、S は cab なので 2 文字目の a を出力します。
入力例 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
出力例 2
c u c u
Score : 300 points
Problem Statement
You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.
Process Q queries. Each query is of one of the following two types.
1 x: Perform the following x times in a row: delete the last character of S and append it to the beginning.2 x: Print the x-th character of S.
Constraints
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S consists of lowercase English letters.
- At least one query in the format
2 x. - N, Q, x are all integers.
Input
Input is given from Standard Input in the following format:
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in the following format, where t is 1 or 2:
t x
Output
For each query in the format 2 x, print the answer in a single line.
Sample Input 1
3 3 abc 2 2 1 1 2 2
Sample Output 1
b a
In the 1-st query, S is abc, so the 2-nd character b should be printed.
In the 2-nd query, S is changed from abc to cab.
In the 3-rd query, S is cab, so the 2-nd character a should be printed.
Sample Input 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
Sample Output 2
c u c u
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
いくつかの正方形を辺でつなげてできる、連結な多角形の形をしたパズルのピースのことを ポリオミノ と呼びます。
縦 4 マス、横 4 マスのグリッドと、グリッドに収まる大きさの 3 個のポリオミノがあります。
i 番目のポリオミノの形は 16 個の文字 P_{i,j,k} (1 \leq j, k \leq 4) によって表されます。P_{i, j, k} は何も置かれていないグリッドに i 番目のポリオミノを置いたときの状態を意味して、P_{i, j, k} が # の場合は上から j 行目、左から k 列目のマスにポリオミノが置かれていることを、. の場合は置かれていないことを意味します。(入出力例 1 の図も参考にしてください。)
あなたは次の条件を全て満たすように 3 個のポリオミノ全てをグリッドに敷き詰めることにしました。
- グリッドの全てのマスはポリオミノで覆われている。
- ポリオミノ同士が重なるように置くことはできない。
- ポリオミノがグリッドからはみ出るように置くことはできない。
- ポリオミノの平行移動と回転は自由に行うことができるが、裏返すことはできない。
条件を満たすようにグリッドにポリオミノを敷き詰めることは可能ですか?
制約
- P_{i, j, k} は
#または. - 与えられるポリオミノは連結である。つまり、ポリオミノを構成する正方形同士は、正方形のみを上下左右に辿って互いに行き来できる
- 与えられるポリオミノは空でない
入力
入力は以下の形式で標準入力から与えられる。
P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}
出力
問題文の条件を満たすようにポリオミノを敷き詰めることが可能である場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
.... ###. .#.. .... .... .### .##. .... ..#. .##. .##. .##.
出力例 1
Yes
入力例 1 に対応するポリオミノの形は次の図のようになります。

この場合、次の図のようにポリオミノを配置することで、問題文の条件を満たすようにグリッドにポリオミノを敷き詰めることができます。

よって答えは Yes になります。
入力例 2
###. #.#. ##.. .... .... ..#. .... .... #### ##.. #... #...
出力例 2
Yes
入力例 2 の 1 番目のポリオミノのように、ポリオミノは穴の空いた多角形の形をしている場合があります。
入力例 3
##.. #..# #### .... .... ##.. .##. .... .#.. .#.. .#.. .#..
出力例 3
No
ポリオミノを敷き詰めるときに、ポリオミノを裏返してはならないのに注意してください。
入力例 4
.... ..#. .... .... .... ..#. .... .... .... ..#. .... ....
出力例 4
No
入力例 5
.... #### #... #... .... #### ...# ..## .... ..## ..#. ..##
出力例 5
No
入力例 6
###. .##. ..#. .### .... ...# ..## ...# .... #... #... #...
出力例 6
Yes
Score : 400 points
Problem Statement
A polyomino is a puzzle piece in the shape of a connected polygon made by connecting several squares by their edges.
There is a grid with four rows and four columns, and three polyominoes that fit within the grid.
The shape of the i-th polyomino is represented by 16 characters P_{i,j,k} (1 \leq j, k \leq 4). They describe the state of the grid when the i-th polyomino is placed on it. If P_{i, j, k} is #, the square at the j-th row from the top and k-th column from the left is occupied by the polyomino; if it is ., the square is not occupied. (Refer to the figures at Sample Input/Output 1.)
You want to fill the grid with all three polyominoes so that all of the following conditions are satisfied.
- All squares of the grid are covered by the polyominoes.
- The polyominoes must not overlap each other.
- The polyominoes must not stick out of the grid.
- The polyominoes may be freely translated and rotated but may not be flipped over.
Can the grid be filled with the polyominoes to satisfy these conditions?
Constraints
- P_{i, j, k} is
#or.. - The given polyominoes are connected. In other words, the squares that make up a polyomino can be reached from each other by following only the squares up, down, left, and right.
- The given polyominoes are not empty.
Input
The input is given from Standard Input in the following format:
P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}
Output
If it is possible to fill the grid with the polyominoes to satisfy the conditions in the problem statement, print Yes; otherwise, print No.
Sample Input 1
.... ###. .#.. .... .... .### .##. .... ..#. .##. .##. .##.
Sample Output 1
Yes
The figure below shows the shapes of the polyominoes corresponding to Sample Input 1.

In this case, you can fill the grid with them to satisfy the conditions in the problem statement by placing them as shown in the figure below.

Thus, the answer is Yes.
Sample Input 2
###. #.#. ##.. .... .... ..#. .... .... #### ##.. #... #...
Sample Output 2
Yes
As in the first polyomino in Sample Input 2, a polyomino may be in the shape of a polygon with a hole.
Sample Input 3
##.. #..# #### .... .... ##.. .##. .... .#.. .#.. .#.. .#..
Sample Output 3
No
Note that the polyominoes may not be flipped over when filling the grid.
Sample Input 4
.... ..#. .... .... .... ..#. .... .... .... ..#. .... ....
Sample Output 4
No
Sample Input 5
.... #### #... #... .... #### ...# ..## .... ..## ..#. ..##
Sample Output 5
No
Sample Input 6
###. .##. ..#. .### .... ...# ..## ...# .... #... #... #...
Sample Output 6
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
1 から N までの番号が付けられた N 頂点からなる木があります。 各 i\ (2 \leq i \leq N) について、頂点 i と頂点 \lfloor \frac{i}{2} \rfloor を結ぶ辺が張られています。 逆に、これら以外の辺は存在しません。
この木において、頂点 X との距離が K である頂点の数を求めてください。 ただし、2 頂点 u,v の距離は、頂点 u,v を結ぶ単純パスに含まれる辺の個数として定義されます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T \leq 10^5
- 1\leq N \leq 10^{18}
- 1\leq X \leq N
- 0\leq K \leq N-1
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。 ここで、\mathrm{test}_i は i 番目のテストケースを意味する。
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
各テストケースは以下の形式で与えられる。
N X K
出力
T 行出力せよ。
i\ (1 \leq i \leq T) 行目には、i 番目のテストケースに対する答えを整数として出力せよ。
入力例 1
5 10 2 0 10 2 1 10 2 2 10 2 3 10 2 4
出力例 1
1 3 4 2 0
N=10 のとき、木は以下の図のようになります。

このとき、
- 頂点 2 との距離が 0 である頂点は 2 の 1 つです。
- 頂点 2 との距離が 1 である頂点は 1,4,5 の 3 つです。
- 頂点 2 との距離が 2 である頂点は 3,8,9,10 の 4 つです。
- 頂点 2 との距離が 3 である頂点は 6,7 の 2 つです。
- 頂点 2 との距離が 4 である頂点は存在しません。
入力例 2
10 822981260158260522 52 20 760713016476190629 2314654 57 1312150450968417 1132551176249851 7 1000000000000000000 1083770654 79 234122432773361868 170290518806790 23 536187734191890310 61862 14 594688604155374934 53288633578 39 1000000000000000000 120160810 78 89013034180999835 14853481725739 94 463213054346948152 825589 73
出力例 2
1556480 140703128616960 8 17732923532771328 65536 24576 2147483640 33776997205278720 7881299347898368 27021597764222976
Score : 450 points
Problem Statement
There is a tree with N vertices numbered 1 to N. For each i\ (2 \leq i \leq N), there is an edge connecting vertex i and vertex \lfloor \frac{i}{2} \rfloor. There are no other edges.
In this tree, find the number of vertices whose distance from vertex X is K. Here, the distance between two vertices u and v is defined as the number of edges in the simple path connecting vertices u and v.
You have T test cases to solve.
Constraints
- 1\leq T \leq 10^5
- 1\leq N \leq 10^{18}
- 1\leq X \leq N
- 0\leq K \leq N-1
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{test}_i represents the i-th test case:
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
Each test case is given in the following format:
N X K
Output
Print T lines.
The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case as an integer.
Sample Input 1
5 10 2 0 10 2 1 10 2 2 10 2 3 10 2 4
Sample Output 1
1 3 4 2 0
The tree for N=10 is shown in the following figure.

Here,
- There is 1 vertex, 2, whose distance from vertex 2 is 0.
- There are 3 vertices, 1,4,5, whose distance from vertex 2 is 1.
- There are 4 vertices, 3,8,9,10, whose distance from vertex 2 is 2.
- There are 2 vertices, 6,7, whose distance from vertex 2 is 3.
- There are no vertices whose distance from vertex 2 is 4.
Sample Input 2
10 822981260158260522 52 20 760713016476190629 2314654 57 1312150450968417 1132551176249851 7 1000000000000000000 1083770654 79 234122432773361868 170290518806790 23 536187734191890310 61862 14 594688604155374934 53288633578 39 1000000000000000000 120160810 78 89013034180999835 14853481725739 94 463213054346948152 825589 73
Sample Output 2
1556480 140703128616960 8 17732923532771328 65536 24576 2147483640 33776997205278720 7881299347898368 27021597764222976
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
数直線上の座標 0 から座標 X_N まで行き、折り返して座標 0 まで帰ってくる計画を立てています。ただし、往路では正の方向、復路では負の方向にしか進めません。
移動は車で行います。車は距離 1 進むごとに 1 リットルの燃料を消費します。燃料は H リットルまで所持することができ、燃料を所持していない状態で進むことはできません。
各 i = 1, 2, \ldots, N-1 について、座標 X_i にはガソリンスタンドがあり、P_i 円払うと F_i リットルの燃料が得られます。ただし、H リットルを超えて燃料を所持することはできません。より厳密には、x リットルの燃料を持っているときに座標 X_i にあるガソリンスタンドを使うと P_i 円を払う必要があり、持っている燃料は \min(x + F_i, H) リットルとなります。 各ガソリンスタンドは、往路と復路で合わせて 1 回までしか使うことができません。
はじめに燃料を H リットル所持しているとき、この計画を達成することができるか判定し、可能ならば必要な金額の最小値を求めてください。
制約
- 1 \leq N, H \leq 300
- 0 < X_1 < X_2 < \ldots < X_N \leq 10^5
- 1 \leq P_i \leq 10^5
- 1 \leq F_i \leq H
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H
X_1 X_2 \ldots X_N
P_1 F_1
P_2 F_2
\vdots
P_{N-1} F_{N-1}
出力
計画を達成できる場合は必要な金額の最小値を、できない場合は -1 を出力せよ。
入力例 1
4 10 2 5 9 11 8 10 5 8 4 9
出力例 1
9
往路で座標 5 の、復路で座標 9 のガソリンスタンドを用いることにより合計 9 円払うことで計画を達成することができます。
計画を達成するためにかかる金額を 8 円以下にすることはできません。往路と復路で同じガソリンスタンドを使うことができないことに注意してください。
入力例 2
1 1 100000
出力例 2
-1
入力例 3
5 20 4 13 16 18 23 1 16 2 8 4 11 8 13
出力例 3
13
Score : 550 points
Problem Statement
You are planning to travel from coordinate 0 to coordinate X_N on a number line, then turn around and return to coordinate 0. Here, you can only move in the positive direction on the outbound trip and in the negative direction on the return trip.
You will travel by car. The car consumes one liter of fuel for every unit distance it travels. You can carry up to H liters of fuel and cannot move without fuel.
For each i = 1, 2, \ldots, N-1, there is a gas station at coordinate X_i, where you can get F_i liters of fuel for P_i yen. However, you cannot carry more than H liters of fuel. More precisely, if you have x liters of fuel and use the gas station at coordinate X_i, you must pay P_i yen, and your amount of fuel becomes \min(x + F_i, H) liters. Each gas station can be used at most once in total during the round trip.
Determine if you can achieve this plan when you initially have H liters of fuel, and if it is possible, find the minimum amount of money required.
Constraints
- 1 \leq N, H \leq 300
- 0 < X_1 < X_2 < \ldots < X_N \leq 10^5
- 1 \leq P_i \leq 10^5
- 1 \leq F_i \leq H
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H
X_1 X_2 \ldots X_N
P_1 F_1
P_2 F_2
\vdots
P_{N-1} F_{N-1}
Output
If the plan can be achieved, print the minimum amount of money required; otherwise, print -1.
Sample Input 1
4 10 2 5 9 11 8 10 5 8 4 9
Sample Output 1
9
You can achieve the plan by using the gas station at coordinate 5 on the outbound trip and the one at coordinate 9 on the return trip, paying a total of 9 yen.
It is impossible to achieve the plan by paying 8 yen or less. Note that you cannot use the same gas station on both the outbound and return trips.
Sample Input 2
1 1 100000
Sample Output 2
-1
Sample Input 3
5 20 4 13 16 18 23 1 16 2 8 4 11 8 13
Sample Output 3
13