Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
黒板に整数 N が 1 個書かれています。
高橋君は黒板に書かれている 2 以上の整数が全て無くなるまで以下の一連の操作を繰り返します。
- 黒板に書かれている 2 以上の整数を 1 つ選び x とする。
- 黒板から x を 1 個消す。そして、2 個の整数 \left \lfloor \dfrac{x}{2} \right\rfloor, \left\lceil \dfrac{x}{2} \right\rceil を新たに黒板に書く。
- この一連の操作を行うために高橋君は x 円払う必要がある。
ここで \lfloor a \rfloor は a 以下の整数のうち最大のものを、\lceil a \rceil は a 以上の整数のうち最小のものを意味します。
操作を行えなくなった時点で高橋君が払った金額の総和は何円ですか?
なお、どのような順番で操作を行っても高橋君が払う金額の総和は一定であることが証明できます。
制約
- 2 \leq N \leq 10^{17}
入力
入力は以下の形式で標準入力から与えられる。
N
出力
高橋君が払った金額の総和が何円であるかを出力せよ。
入力例 1
3
出力例 1
5
高橋君が行う操作の一例を挙げると次のようになります。
- はじめ、黒板には 3 が 1 個書かれている。
- 高橋君は 3 を選ぶ。高橋君は 3 円を払い、黒板から 3 を 1 個消して \left \lfloor \dfrac{3}{2} \right\rfloor = 1, \left\lceil \dfrac{3}{2} \right\rceil = 2 を新たに黒板に書く。
- 黒板には 2 が 1 個と 1 が 1 個書かれている。
- 高橋君は 2 を選ぶ。高橋君は 2 円を払い、黒板から 2 を 1 個消して \left \lfloor \dfrac{2}{2} \right\rfloor = 1, \left\lceil \dfrac{2}{2} \right\rceil = 1 を新たに黒板に書く。
- 黒板には 1 が 3 個書かれている。
- 黒板から 2 以上の整数が全て無くなったので操作を終了する。
操作全体で高橋君は 3 + 2 = 5 円払ったので、5 を出力します。
入力例 2
340
出力例 2
2888
入力例 3
100000000000000000
出力例 3
5655884811924144128
Score: 300 points
Problem Statement
There is a single integer N written on a blackboard.
Takahashi will repeat the following series of operations until all integers not less than 2 are removed from the blackboard:
- Choose one integer x not less than 2 written on the blackboard.
- Erase one occurrence of x from the blackboard. Then, write two new integers \left \lfloor \dfrac{x}{2} \right\rfloor and \left\lceil \dfrac{x}{2} \right\rceil on the blackboard.
- Takahashi must pay x yen to perform this series of operations.
Here, \lfloor a \rfloor denotes the largest integer not greater than a, and \lceil a \rceil denotes the smallest integer not less than a.
What is the total amount of money Takahashi will have paid when no more operations can be performed?
It can be proved that the total amount he will pay is constant regardless of the order in which the operations are performed.
Constraints
- 2 \leq N \leq 10^{17}
Input
The input is given from Standard Input in the following format:
N
Output
Print the total amount of money Takahashi will have paid, in yen.
Sample Input 1
3
Sample Output 1
5
Here is an example of how Takahashi performs the operations:
- Initially, there is one 3 written on the blackboard.
- He chooses 3. He pays 3 yen, erases one 3 from the blackboard, and writes \left \lfloor \dfrac{3}{2} \right\rfloor = 1 and \left\lceil \dfrac{3}{2} \right\rceil = 2 on the blackboard.
- There is one 2 and one 1 written on the blackboard.
- He chooses 2. He pays 2 yen, erases one 2 from the blackboard, and writes \left \lfloor \dfrac{2}{2} \right\rfloor = 1 and \left\lceil \dfrac{2}{2} \right\rceil = 1 on the blackboard.
- There are three 1s written on the blackboard.
- Since all integers not less than 2 have been removed from the blackboard, the process is finished.
Takahashi has paid a total of 3 + 2 = 5 yen for the entire process, so print 5.
Sample Input 2
340
Sample Output 2
2888
Sample Input 3
100000000000000000
Sample Output 3
5655884811924144128
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1, 2, \ldots, N の番号がついた N 人の人が二次元平面上におり、人 i は座標 (X_i,Y_i) で表される地点にいます。
人 1 がウイルスに感染しました。ウイルスに感染した人から距離が D 以内にいる人にウイルスはうつります。
ただし、距離はユークリッド距離、すなわち 2 点 (a_1, a_2) と (b_1, b_2) に対し、この 2 点間の距離が \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2} であるものとして定められています。
十分に時間が経過した、すなわち人 i がウイルスに感染しているならば 人 i との距離が D 以内にいるすべての人がウイルスに感染している状態になったときに、各 i について人 i がウイルスに感染しているか判定してください。
制約
- 1 \leq N, D \leq 2000
- -1000 \leq X_i, Y_i \leq 1000
- i \neq j のとき (X_i, Y_i) \neq (X_j, Y_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
N 行出力せよ。i 行目には、人 i がウイルスに感染しているならば Yes を、そうでないならば No を出力せよ。
入力例 1
4 5 2 -1 3 1 8 8 0 5
出力例 1
Yes Yes No Yes
人 1 と人 2 の距離は \sqrt 5 であるため、人 2 はウイルスに感染します。
また、人 2 と人 4 の距離は 5 であるため、人 4 はウイルスに感染します。
人 3 は距離 5 以内に人がいないので、ウイルスに感染することはありません。
入力例 2
3 1 0 0 -1000 -1000 1000 1000
出力例 2
Yes No No
入力例 3
9 4 3 2 6 -1 1 6 6 5 -2 -3 5 3 2 -3 2 1 2 6
出力例 3
Yes No No Yes Yes Yes Yes Yes No
Score : 300 points
Problem Statement
There are N people numbered 1, 2, \ldots, N on a two-dimensional plane, and person i is at the point represented by the coordinates (X_i,Y_i).
Person 1 has been infected with a virus. The virus spreads to people within a distance of D from an infected person.
Here, the distance is defined as the Euclidean distance, that is, for two points (a_1, a_2) and (b_1, b_2), the distance between these two points is \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2}.
After a sufficient amount of time has passed, that is, when all people within a distance of D from person i are infected with the virus if person i is infected, determine whether person i is infected with the virus for each i.
Constraints
- 1 \leq N, D \leq 2000
- -1000 \leq X_i, Y_i \leq 1000
- (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print N lines. The i-th line should contain Yes if person i is infected with the virus, and No otherwise.
Sample Input 1
4 5 2 -1 3 1 8 8 0 5
Sample Output 1
Yes Yes No Yes
The distance between person 1 and person 2 is \sqrt 5, so person 2 gets infected with the virus.
Also, the distance between person 2 and person 4 is 5, so person 4 gets infected with the virus.
Person 3 has no one within a distance of 5, so they will not be infected with the virus.
Sample Input 2
3 1 0 0 -1000 -1000 1000 1000
Sample Output 2
Yes No No
Sample Input 3
9 4 3 2 6 -1 1 6 6 5 -2 -3 5 3 2 -3 2 1 2 6
Sample Output 3
Yes No No Yes Yes Yes Yes Yes No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
N 行 N 列のグリッドがあります。高橋君は以下の条件を満たすように各マスを黒または白のいずれかに塗り分けたいと考えています。
- すべての行について以下の条件が成り立つ。
- ある整数 i\ (0\leq i\leq N) が存在して、その行の左から i 個のマスは黒、それ以外は白で塗られている。
- すべての列について以下の条件が成り立つ。
- ある整数 i\ (0\leq i\leq N) が存在して、その列の上から i 個のマスは黒、それ以外は白で塗られている。
M 個のマスがすでに塗られています。そのうち i 個目は上から X_i 行目、左から Y_i 列目のマスで、C_i が B のとき黒で、 W のとき白で塗られています。
高橋君はまだ塗られていない残りの N^2-M 個のマスの色をうまく決めることで条件を満たすことができるか判定してください。
制約
- 1\leq N\leq 10^9
- 1\leq M\leq \min(N^2,2\times 10^5)
- 1\leq X_i,Y_i\leq N
- (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
- C_i は
BまたはW - 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 Y_1 C_1 \vdots X_M Y_M C_M
出力
条件を満たすことができるとき Yes を、そうでないとき No を出力せよ。
入力例 1
4 3 4 1 B 3 2 W 1 3 B
出力例 1
Yes
例えば以下の図のように色を塗り分けると条件を満たすことができます。すでに塗られているマスを赤色の線で囲んでいます。

入力例 2
2 2 1 2 W 2 2 B
出力例 2
No
塗られていない残りの 2 つのマスをどのように塗っても,条件を満たすことはできません。
入力例 3
1 1 1 1 W
出力例 3
Yes
入力例 4
2289 10 1700 1083 W 528 967 B 1789 211 W 518 1708 W 1036 779 B 136 657 B 759 1497 B 902 1309 B 1814 712 B 936 763 B
出力例 4
No
Score : 425 points
Problem Statement
There is an N \times N grid. Takahashi wants to color each cell black or white so that all of the following conditions are satisfied:
- For every row, the following condition holds:
- There exists an integer i\ (0\leq i\leq N) such that the leftmost i cells are colored black, and the rest are colored white.
- For every column, the following condition holds:
- There exists an integer i\ (0\leq i\leq N) such that the topmost i cells are colored black, and the rest are colored white.
Out of these N^2 cells, M of them have already been colored. Among them, the i-th one is at the X_i-th row from the top and the Y_i-th column from the left, and it is colored black if C_i is B and white if C_i is W.
Determine whether he can color the remaining uncolored N^2 - M cells so that all the conditions are satisfied.
Constraints
- 1\leq N\leq 10^9
- 1\leq M\leq \min(N^2,2\times 10^5)
- 1\leq X_i,Y_i\leq N
- (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
- C_i is
BorW. - All input numbers are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 Y_1 C_1 \vdots X_M Y_M C_M
Output
If it is possible to satisfy the conditions, print Yes; otherwise, print No.
Sample Input 1
4 3 4 1 B 3 2 W 1 3 B
Sample Output 1
Yes
For example, one can color the grid as in the following figure to satisfy the conditions. The cells already colored are surrounded by red borders.

Sample Input 2
2 2 1 2 W 2 2 B
Sample Output 2
No
No matter how the remaining two cells are colored, the conditions cannot be satisfied.
Sample Input 3
1 1 1 1 W
Sample Output 3
Yes
Sample Input 4
2289 10 1700 1083 W 528 967 B 1789 211 W 518 1708 W 1036 779 B 136 657 B 759 1497 B 902 1309 B 1814 712 B 936 763 B
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
そうめん流しのイベントに N 人の人が集まりました。人は一列に並んでおり、先頭から順に 1 から N の番号がついています。
そうめん流しでは次の出来事が M 回起こります。
- 時刻 T_i に 量 W_i のそうめんを流す。列の先頭にいる人がその全てを得る(誰も列に並んでいない場合は、誰もそのそうめんを得ない)。その人はいったん列から外れ、時刻 T_i+S_i に列の元の位置に戻ってくる。
時刻 X に列に戻ってくる人は、時刻 X には列に並んでいるとみなします。
M 回の出来事が全て行われたあと、それぞれの人が合計でどれだけそうめんを得ることができたか答えてください。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 0 <T_1 <\ldots < T_M \leq 10^9
- 1 \leq S_i \leq 10^9
- 1 \leq W_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M T_1 W_1 S_1 \vdots T_M W_M S_M
出力
N 行出力せよ。 i 行目には人 i が得たそうめんの量を出力せよ。
入力例 1
3 5 1 1 3 2 10 100 4 100 10000 10 1000 1000000000 100 1000000000 1
出力例 1
101 10 1000
次のように進行します。
- 時刻 1 に量 1 のそうめんが流される。列には人 1,2,3 が並んでおり、先頭の人 1 がそうめんを得て列から外れる。
- 時刻 2 に量 10 のそうめんが流される。列には人 2,3 が並んでおり、先頭の人 2 がそうめんを得て列から外れる。
- 時刻 4 に人 1 が列に戻ってくる。
- 時刻 4 に量 100 のそうめんが流される。列には人 1,3 が並んでおり、先頭の人 1 がそうめんを得て列から外れる。
- 時刻 10 に量 1000 のそうめんが流される。列には人 3 が並んでおり、先頭の人 3 がそうめんを得て列から外れる。
- 時刻 100 に量 1000000000 のそうめんが流される。列には誰も並んでいないため、誰もこのそうめんを得ない。
- 時刻 102 に人 2 が列に戻ってくる。
- 時刻 10004 に人 1 が列に戻ってくる。
- 時刻 1000000010 に人 3 が列に戻ってくる。
それぞれの人が得たそうめんの量の合計は、101,10,1000 です。
入力例 2
3 1 1 1 1
出力例 2
1 0 0
入力例 3
1 8 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8
出力例 3
15
Score : 475 points
Problem Statement
There are N people gathered for an event called Flowing Noodles. The people are lined up in a row, numbered 1 to N in order from front to back.
During the event, the following occurrence happens M times:
- At time T_i, a quantity W_i of noodles is flown down. The person at the front of the row gets all of it (if no one is in the row, no one gets it). That person then steps out of the row and returns to their original position in the row at time T_i+S_i.
A person who returns to the row at time X is considered to be in the row at time X.
After all the M occurrences, report the total amount of noodles each person has got.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 0 <T_1 <\ldots < T_M \leq 10^9
- 1 \leq S_i \leq 10^9
- 1 \leq W_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M T_1 W_1 S_1 \vdots T_M W_M S_M
Output
Print N lines. The i-th line should contain the amount of noodles person i has got.
Sample Input 1
3 5 1 1 3 2 10 100 4 100 10000 10 1000 1000000000 100 1000000000 1
Sample Output 1
101 10 1000
The event proceeds as follows:
- At time 1, a quantity 1 of noodles is flown down. People 1, 2, and 3 are in the row, and the person at the front, person 1, gets the noodles and steps out of the row.
- At time 2, a quantity 10 of noodles is flown down. People 2 and 3 are in the row, and the person at the front, person 2, gets the noodles and steps out of the row.
- At time 4, person 1 returns to the row.
- At time 4, a quantity 100 of noodles is flown down. People 1 and 3 are in the row, and the person at the front, person 1, gets the noodles and steps out of the row.
- At time 10, a quantity 1000 of noodles is flown down. Only person 3 is in the row, and the person at the front, person 3, gets the noodles and steps out of the row.
- At time 100, a quantity 1000000000 of noodles is flown down. No one is in the row, so no one gets these noodles.
- At time 102, person 2 returns to the row.
- At time 10004, person 1 returns to the row.
- At time 1000000010, person 3 returns to the row.
The total amounts of noodles people 1, 2, and 3 have got are 101, 10, and 1000, respectively.
Sample Input 2
3 1 1 1 1
Sample Output 2
1 0 0
Sample Input 3
1 8 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8
Sample Output 3
15
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の木が与えられます。i 番目の辺 (1\leq i\leq N-1) は頂点 u_i と頂点 v_i を双方向に結んでいます。
与えられた木に無向辺を一本追加して得られるグラフは、必ずちょうど一つの閉路を含みます。
そのようなグラフのうち、以下の条件を全て満たすものの個数を求めてください。
- グラフは単純グラフ
- グラフの閉路に含まれる頂点の次数は全て 3
制約
- 3 \leq N \leq 2 \times 10^5
- 1\leq u_i,v_i\leq N
- 与えられるグラフは木である
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
答えを出力せよ。
入力例 1
6 1 2 2 3 3 4 4 5 3 6
出力例 1
1
頂点 2 と頂点 4 を結ぶ辺を追加して得られるグラフは単純グラフであり、閉路に含まれる頂点の次数は全て 3 なので条件を満たします。
入力例 2
7 1 2 2 7 3 5 7 3 6 2 4 7
出力例 2
0
条件を満たすグラフが存在しない場合もあります。
入力例 3
15 1 15 11 14 2 10 1 7 9 8 6 9 4 12 14 5 4 9 8 11 7 4 1 13 3 6 11 10
出力例 3
6
Score : 500 points
Problem Statement
You are given a tree with N vertices. The i-th edge (1 \leq i \leq N-1) connects vertices u_i and v_i bidirectionally.
Adding one undirected edge to the given tree always yields a graph with exactly one cycle.
Among such graphs, how many satisfy all of the following conditions?
- The graph is simple.
- All vertices in the cycle have degree 3.
Constraints
- 3 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Print the answer.
Sample Input 1
6 1 2 2 3 3 4 4 5 3 6
Sample Output 1
1
Adding an edge connecting vertices 2 and 4 yields a simple graph where all vertices in the cycle have degree 3, so it satisfies the conditions.
Sample Input 2
7 1 2 2 7 3 5 7 3 6 2 4 7
Sample Output 2
0
There are cases where no graphs satisfy the conditions.
Sample Input 3
15 1 15 11 14 2 10 1 7 9 8 6 9 4 12 14 5 4 9 8 11 7 4 1 13 3 6 11 10
Sample Output 3
6