実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君の住む AtCoder 国には「長さが 5 の倍数である文字列を書いてはならない」という奇妙な法律があります。
高橋君は文字列 S を書きました。高橋君がこの法律に違反しているかどうか判定してください。
制約
- S は長さ 1 以上 10 以下の文字列
- S は英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
高橋君が法律に違反しているのであれば Yes を、違反していないのであれば No を出力せよ。
入力例 1
legal
出力例 1
Yes
legal は長さが 5 の文字列です。よって、高橋君は法律に違反しているため Yes と出力してください。
入力例 2
atcoder
出力例 2
No
atcoder は長さが 7 の文字列です。よって、高橋君は法律に違反していないため No と出力してください。
入力例 3
illegal
出力例 3
No
Score : 100 points
Problem Statement
In the country of AtCoder where Takahashi lives, there is a peculiar law: "You must not write a string whose length is a multiple of 5."
Takahashi wrote a string S. Determine whether he is violating this law.
Constraints
- S is a string of length between 1 and 10, inclusive.
- S consists of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
If Takahashi is violating the law, output Yes; otherwise, output No.
Sample Input 1
legal
Sample Output 1
Yes
legal is a string of length 5. Thus, Takahashi is violating the law, so output Yes.
Sample Input 2
atcoder
Sample Output 2
No
atcoder is a string of length 7. Thus, Takahashi is not violating the law, so output No.
Sample Input 3
illegal
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君の勤めている会社には N 人の社員がおり、各社員には 1,2,\dots,N の社員番号が割り当てられています。 社内には M 個の部門があり、部門 1,2,\dots,M と呼ばれています。
社員番号 i の社員は、今期は部門 A_i に所属しており、来期は部門 B_i に所属します。
部門 1,2,\dots,M それぞれに対して、来期の所属人数から今期の所属人数を引いた値を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq A_i \leq M
- 1 \leq B_i \leq M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
M 行出力せよ。j 行目には部門 j についての答えを出力せよ。
入力例 1
5 4 1 2 2 1 3 1 2 2 2 4
出力例 1
1 -1 -1 1
- 部門 1 について、今期は社員番号 1 の 1 人が所属し、来期は社員番号 2,3 の 2 人が所属します。
- 部門 2 について、今期は社員番号 2,4,5 の 3 人が所属し、来期は社員番号 1,4 の 2 人が所属します。
- 部門 3 について、今期は社員番号 3 の 1 人が所属し、来期は誰も所属しません。
- 部門 4 について、今期は誰も所属せず、来期は社員番号 5 の 1 人が所属します。
入力例 2
10 5 3 2 3 4 1 2 2 2 4 4 3 1 3 4 4 2 3 3 3 2
出力例 2
0 4 -5 1 0
Score : 200 points
Problem Statement
The company where Takahashi works has N employees, each assigned an employee number from 1, 2, \dots, N. There are M departments in the company, called departments 1, 2, \dots, M.
Employee i belongs to department A_i this term and will belong to department B_i next term.
For each of departments 1, 2, \dots, M, find the number of members next term minus the number of members this term.
Constraints
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq A_i \leq M
- 1 \leq B_i \leq M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Output M lines. The j-th line should contain the answer for department j.
Sample Input 1
5 4 1 2 2 1 3 1 2 2 2 4
Sample Output 1
1 -1 -1 1
- For department 1: this term, one employee (employee number 1) belongs; next term, two employees (employee numbers 2, 3) belong.
- For department 2: this term, three employees (employee numbers 2, 4, 5) belong; next term, two employees (employee numbers 1, 4) belong.
- For department 3: this term, one employee (employee number 3) belongs; next term, no one belongs.
- For department 4: this term, no one belongs; next term, one employee (employee number 5) belongs.
Sample Input 2
10 5 3 2 3 4 1 2 2 2 4 4 3 1 3 4 4 2 3 3 3 2
Sample Output 2
0 4 -5 1 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋くんは、庭にある木の本数を管理しています。はじめ、庭に木は 1 本もありません。
Q 個のクエリが順に与えられます。クエリは次の 2 種類のいずれかです。各クエリを処理した直後に庭にある木の本数を出力してください。
1 h: 庭に高さ h の木を新たに 1 本追加する。2 h: いま庭にある木のうち、高さが h 以下の木をすべて削除する。
制約
- 1 \le Q \le 3 \times 10^5
- 1 \le h \le 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
i 番目のクエリを表す \text{query}_i は以下の 2 種類のいずれかである。
1 h
2 h
出力
Q 行出力せよ。
i 行目には i 番目のクエリを処理した直後の庭にある木の本数を出力せよ。
入力例 1
5 1 5 1 7 1 8 2 7 1 3
出力例 1
1 2 3 1 2
以下のように木の本数は変化します。
- 高さ 5 の木が追加される。庭にある木は高さ 5 の 1 本である。
- 高さ 7 の木が追加される。庭にある木は高さ 5, 7 の 2 本である。
- 高さ 8 の木が追加される。庭にある木は高さ 5, 7, 8 の 3 本である。
- 高さ 7 以下の木が削除される。庭にある木は高さ 8 の 1 本である。
- 高さ 3 の木が追加される。庭にある木は高さ 8, 3 の 2 本である。
入力例 2
12 2 256601193 1 85138616 1 202564041 2 276477192 1 55551662 1 170271057 2 754166580 1 854388209 1 772036624 2 651124113 1 301137866 2 290875185
出力例 2
0 1 2 0 1 2 0 1 2 2 3 3
Score : 300 points
Problem Statement
Takahashi is managing the number of trees in his garden. Initially, there are no trees in the garden.
Q queries are given in order. Each query is one of the following two types. Immediately after processing each query, output the number of trees currently in the garden.
1 h: Add one new tree of height h to the garden.2 h: Remove all trees currently in the garden whose height is at most h.
Constraints
- 1 \le Q \le 3 \times 10^5
- 1 \le h \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
\text{query}_i, representing the i-th query, is one of the following two types:
1 h
2 h
Output
Output Q lines.
The i-th line should contain the number of trees in the garden immediately after processing the i-th query.
Sample Input 1
5 1 5 1 7 1 8 2 7 1 3
Sample Output 1
1 2 3 1 2
The number of trees changes as follows.
- A tree of height 5 is added. The garden contains one tree of height 5.
- A tree of height 7 is added. The garden contains two trees of heights 5, 7.
- A tree of height 8 is added. The garden contains three trees of heights 5, 7, 8.
- Trees of height at most 7 are removed. The garden contains one tree of height 8.
- A tree of height 3 is added. The garden contains two trees of heights 8, 3.
Sample Input 2
12 2 256601193 1 85138616 1 202564041 2 276477192 1 55551662 1 170271057 2 754166580 1 854388209 1 772036624 2 651124113 1 301137866 2 290875185
Sample Output 2
0 1 2 0 1 2 0 1 2 2 3 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
以下の条件を満たす正整数を 良い整数 とします。
- 条件:一つ以上の 2 の冪(1,2,4,8,16,\dots)を(重複と並び替えを許して)選んで文字列として結合し、それを整数として解釈することで得られる。
良い整数のうち N 番目に小さいものを求めてください。 ただし N 番目に小さい良い整数は 10^9 以下であることが保証されます。
制約
- N は正整数
- N 番目に小さい良い整数は 10^9 以下
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
10
出力例 1
21
良い整数を小さい方から列挙すると 1, 2, 4, 8, 11, 12, 14, 16, 18, 21, \dots です。
入力例 2
69
出力例 2
328
入力例 3
1099898
出力例 3
819264512
Score : 400 points
Problem Statement
We call a positive integer a good integer if it satisfies the following condition.
- Condition: It can be obtained by choosing one or more powers of 2 (1, 2, 4, 8, 16, \dots) (repetition and reordering allowed), concatenating them as strings, and interpreting the result as an integer.
Find the N-th smallest good integer. It is guaranteed that the N-th smallest good integer is at most 10^9.
Constraints
- N is a positive integer.
- The N-th smallest good integer is at most 10^9.
Input
The input is given from Standard Input in the following format:
N
Output
Output the answer.
Sample Input 1
10
Sample Output 1
21
Listing good integers in ascending order gives 1, 2, 4, 8, 11, 12, 14, 16, 18, 21, \dots.
Sample Input 2
69
Sample Output 2
328
Sample Input 3
1099898
Sample Output 3
819264512
実行時間制限: 6 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
N 頂点の重み付き無向木であって、以下の条件を満たすものが存在するか判定して下さい。
- 1 \le i \lt j \le N を満たす任意の 2 整数 i,j について頂点 i と頂点 j の距離が A_{i,j} である。
ただし、頂点 i と頂点 j の距離とは、この 2 頂点を結ぶ唯一の単純パスに含まれる辺の重みの総和のことです。
制約
- 2 \le N \le 3000
- 1 \le A_{i,j} \le 9999
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1, 2} A_{1, 3} \ldots A_{1, N}
A_{2, 3} \ldots A_{2, N}
\vdots
A_{N-1,N}
出力
条件を満たす木が存在するなら Yes を、存在しないなら No を出力せよ。
入力例 1
4 2 5 4 3 2 5
出力例 1
Yes
例えば以下の辺をもつ木が条件を満たします。
- 辺 (1, 2) の重みが 2
- 辺 (2, 3) の重みが 3
- 辺 (2, 4) の重みが 2
よって Yes と出力してください。
入力例 2
4 2 5 4 3 2 6
出力例 2
No
条件を満たす木は存在しません。よって No と出力してください。
入力例 3
10 1039 1802 3781 231 5828 1944 392 262 1481 763 2742 1270 4789 905 1431 1301 442 1979 2033 5552 142 2194 2064 1205 4012 7531 2121 4173 4043 3184 6059 2175 161 493 1712 5694 6220 6090 5231 2336 2206 1347 654 1873 1743
出力例 3
Yes
Score : 475 points
Problem Statement
Determine whether there exists a weighted undirected tree with N vertices satisfying the following condition.
- For any two integers i and j with 1 \le i \lt j \le N, the distance between vertices i and j is A_{i,j}.
Here, the distance between vertices i and j is defined as the sum of the weights of the edges on the unique simple path connecting these two vertices.
Constraints
- 2 \le N \le 3000
- 1 \le A_{i,j} \le 9999
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_{1, 2} A_{1, 3} \ldots A_{1, N}
A_{2, 3} \ldots A_{2, N}
\vdots
A_{N-1,N}
Output
If a tree satisfying the condition exists, output Yes; otherwise, output No.
Sample Input 1
4 2 5 4 3 2 5
Sample Output 1
Yes
For example, the tree with the following edges satisfies the condition.
- Edge (1, 2) has weight 2.
- Edge (2, 3) has weight 3.
- Edge (2, 4) has weight 2.
Thus, output Yes.
Sample Input 2
4 2 5 4 3 2 6
Sample Output 2
No
No tree satisfying the condition exists. Thus, output No.
Sample Input 3
10 1039 1802 3781 231 5828 1944 392 262 1481 763 2742 1270 4789 905 1431 1301 442 1979 2033 5552 142 2194 2064 1205 4012 7531 2121 4173 4043 3184 6059 2175 161 493 1712 5694 6220 6090 5231 2336 2206 1347 654 1873 1743
Sample Output 3
Yes
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N までの番号が付いた、N 頂点 0 辺の無向グラフ G があります。
Q 個のクエリが与えられます。i 番目のクエリでは、グラフ G に頂点 u_i と頂点 v_i を結ぶ辺を追加します。
それぞれのクエリを処理した直後のグラフ G において、以下の条件を満たすように G の各頂点を白または黒のどちらか一色で塗ることができるか判定し、可能な場合は黒に塗る頂点の個数としてありうる最小値を求めてください。
- どの辺についても、両端の頂点が異なる色で塗られている。
制約
- 2 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- 1 \le u_i \lt v_i \le N
- (u_i, v_i) \ne (u_j, v_j) \ (i \ne j)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q u_1 v_1 u_2 v_2 \vdots u_Q v_Q
出力
Q 行出力せよ。
i 行目には i 番目のクエリを処理した直後のグラフ G について、以下の値を出力せよ。
- 条件を満たす塗り方が可能な場合、黒に塗る頂点の個数としてありうる最小値
- 条件を満たす塗り方が不可能な場合、 -1
入力例 1
4 4 1 2 2 3 3 4 1 3
出力例 1
1 1 2 -1
1, 2, 3 番目のクエリを処理した直後については、条件を満たす塗り方が存在します。
例えば、 3 番目のクエリを処理した直後は、頂点 1, 2, 3, 4 をそれぞれ 白 黒 白 黒 と塗ることができます。これより 黒 に塗る頂点の個数が少なく、条件を満たす塗り方は存在しないので 2 を出力してください。
4 番目のクエリを処理した直後は、条件を満たすように塗ることができません。よって -1 を出力してください。
入力例 2
10 10 1 10 6 7 2 7 4 9 5 9 6 10 7 8 2 5 3 4 8 10
出力例 2
1 2 2 3 3 3 3 4 4 4
Score : 500 points
Problem Statement
There is an undirected graph G with N vertices numbered 1 through N and zero edges.
Q queries are given. In the i-th query, an edge connecting vertices u_i and v_i is added to graph G.
Immediately after processing each query, determine whether it is possible to color each vertex of G white or black so that the following condition is satisfied, and if possible, find the minimum possible number of vertices colored black.
- For every edge, the two endpoints have different colors.
Constraints
- 2 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- 1 \le u_i \lt v_i \le N
- (u_i, v_i) \ne (u_j, v_j) \ (i \ne j)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q u_1 v_1 u_2 v_2 \vdots u_Q v_Q
Output
Output Q lines.
The i-th line should contain the following value for graph G immediately after processing the i-th query.
- If a valid coloring is possible, the minimum possible number of vertices colored black.
- If no valid coloring is possible, -1.
Sample Input 1
4 4 1 2 2 3 3 4 1 3
Sample Output 1
1 1 2 -1
Immediately after processing queries 1, 2, 3, a valid coloring exists.
For example, immediately after processing the third query, vertices 1, 2, 3, 4 can be colored white, black, white, black, respectively. No valid coloring with fewer black vertices exists, so output 2.
Immediately after processing the fourth query, no valid coloring exists. Thus, output -1.
Sample Input 2
10 10 1 10 6 7 2 7 4 9 5 9 6 10 7 8 2 5 3 4 8 10
Sample Output 2
1 2 2 3 3 3 3 4 4 4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
N 頂点 M 辺からなる単純連結無向グラフが与えられます。 頂点には 1 から N までの、辺には 1 から M までの番号がそれぞれ付けられており、辺 i は頂点 U_i と頂点 V_i を結ぶ重み W_i の無向辺です。
2 頂点を結ぶウォークの重みを、そのウォークが含む辺の重みの総 XOR とします。 ただし同じ辺を複数回通る場合、その辺の重みは通った回数分総 XOR に寄与するものとします。
非負整数 K が与えられます。整数の組 (x,y) (1\leq x\lt y\leq N) であって、頂点 x,y を結ぶウォークの重みの最小値が K 以下であるものの個数を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
ウォークとは
無向グラフ G 上の頂点 X,Y に対して、k 個の頂点と k-1 個の辺を交互に並べた列 (v_1,e_1,v_2,\dots,v_{k-1},e_{k-1},v_k) であって、v_1=X,v_k=Y かつ i=1,2,\dots,k-1 に対して e_i が v_i と v_{i+1} を結ぶ辺であるようなものを頂点 X から頂点 Y への ウォーク と呼びます。
XOR とは
非負整数 A, B のビット単位 XOR、A \oplus B は、以下のように定義されます。
- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。 一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 XOR は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq 2 \times 10^5
- 0 \leq K \lt 2^{30}
- 1 \leq U_i \lt V_i \leq N
- 0 \leq W_i \lt 2^{30}
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
- 1 つの入力に含まれるテストケースについて、N の総和は 2 \times 10^5 以下
- 1 つの入力に含まれるテストケースについて、M の総和は 2 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
ここで \mathrm{case}_i は i 番目のテストケースの入力を意味する。各テストケースは以下の形式で与えられる。
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
出力
T 行出力せよ。i 行目には i 番目のテストケースについての答えを出力せよ。
入力例 1
3 4 4 2 3 4 3 1 3 4 1 2 3 2 3 2 5 7 14032 1 2 24681 3 5 25665 1 5 14154 2 3 23215 1 3 21259 4 5 24874 3 4 26495 8 10 109312507 6 8 793188457 7 8 501937135 1 2 954888411 2 7 109497327 1 6 791995625 2 6 665857693 1 3 101233808 1 7 114788578 4 6 953503358 5 8 624700613
出力例 1
4 9 22
一つ目のテストケースにおいて、例えば頂点 1,3,2,1,3 を順に通るウォークの重みは 4 \oplus 2 \oplus 3 \oplus 4 = 1 です。
相異なる頂点の組それぞれに対し、それらを結ぶウォークの重みの最小値は以下のようになることが示せます。
- 頂点 1 と頂点 2:最小値 3
- 頂点 1 と頂点 3:最小値 1
- 頂点 1 と頂点 4:最小値 2
- 頂点 2 と頂点 3:最小値 2
- 頂点 2 と頂点 4:最小値 1
- 頂点 3 と頂点 4:最小値 3
このうち最小値が K(=2) 以下の組は 4 組あります。したがって一行目には 4 を出力してください。
Score : 600 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 through N and the edges are numbered 1 through M; edge i is an undirected edge of weight W_i connecting vertices U_i and V_i.
The weight of a walk connecting two vertices is defined as the total XOR of the weights of the edges contained in the walk. If the same edge is traversed multiple times, that edge's weight contributes to the total XOR that many times.
You are given a non-negative integer K. Find the number of pairs of integers (x, y) (1 \leq x \lt y \leq N) such that the minimum weight of a walk connecting vertices x and y is at most K.
T test cases are given; solve each one.
What is a walk?
For vertices X and Y in an undirected graph G, a sequence of k vertices and k-1 edges alternately arranged (v_1, e_1, v_2, \dots, v_{k-1}, e_{k-1}, v_k) such that v_1 = X, v_k = Y, and for each i = 1, 2, \dots, k-1, e_i is an edge connecting v_i and v_{i+1}, is called a walk from vertex X to vertex Y.
What is XOR?
The bitwise XOR of non-negative integers A and B, A \oplus B, is defined as follows.
- The digit at the 2^k (k \geq 0) place in the binary representation of A \oplus B is 1 if exactly one of the digits at the 2^k place in the binary representations of A and B is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110). In general, the bitwise XOR of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq 2 \times 10^5
- 0 \leq K \lt 2^{30}
- 1 \leq U_i \lt V_i \leq N
- 0 \leq W_i \lt 2^{30}
- The given graph is simple and connected.
- All input values are integers.
- The sum of N over all test cases in a single input is at most 2 \times 10^5.
- The sum of M over all test cases in a single input is at most 2 \times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Here, \mathrm{case}_i denotes the input for the i-th test case. Each test case is given in the following format:
N M K U_1 V_1 W_1 U_2 V_2 W_2 \vdots U_M V_M W_M
Output
Output T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
3 4 4 2 3 4 3 1 3 4 1 2 3 2 3 2 5 7 14032 1 2 24681 3 5 25665 1 5 14154 2 3 23215 1 3 21259 4 5 24874 3 4 26495 8 10 109312507 6 8 793188457 7 8 501937135 1 2 954888411 2 7 109497327 1 6 791995625 2 6 665857693 1 3 101233808 1 7 114788578 4 6 953503358 5 8 624700613
Sample Output 1
4 9 22
In the first test case, for example, the weight of the walk visiting vertices 1, 3, 2, 1, 3 in order is 4 \oplus 2 \oplus 3 \oplus 4 = 1.
It can be shown that the minimum weight of a walk connecting each pair of distinct vertices is as follows.
- Vertices 1 and 2: minimum 3
- Vertices 1 and 3: minimum 1
- Vertices 1 and 4: minimum 2
- Vertices 2 and 3: minimum 2
- Vertices 2 and 4: minimum 1
- Vertices 3 and 4: minimum 3
Among these, four pairs have a minimum value of at most K(=2). Thus, output 4 on the first line.