Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
1 から N の番号がついた N 個の箱と 1 から N の番号がついた N 個の荷物があります。荷物 i (1 \leq i \leq N) は箱 A_i の中にあり、重さは W_i です。
あなたは荷物を一つ選び、他の箱の中に移動させる操作を 0 回以上繰り返し行うことができます。1 回の操作で移動させる荷物の重さが w であるとき、w のコストがかかります。
全ての箱に荷物が 1 つずつ入っている状態にするためにかかるコストの総和の最小値を求めてください。
制約
- 1 \leq N \leq 10^{5}
- 1 \leq A_i \leq N (1 \leq i \leq N)
- 1 \leq W_i \leq 10^{4} (1 \leq i \leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N W_1 W_2 \ldots W_N
出力
全ての箱に荷物が 1 つずつ入っている状態にするためにかかるコストの総和の最小値を出力せよ。
入力例 1
5 2 2 3 3 5 33 40 2 12 16
出力例 1
35
以下の 2 回の荷物の移動で、すべての箱に荷物が 1 つずつ入っている状態にすることができます。
- 荷物 1 を箱 2 から箱 1 に移す。このとき、コストは 33 である。
- 荷物 3 を箱 3 から箱 4 に移す。このとき、コストは 2 である。
この 2 回の荷物の移動は合計 35 のコストかかります。 35 未満のコストですべての箱に荷物が 1 つずつ入っている状態にすることはできないため、 35 を出力します。
入力例 2
12 3 6 7 4 12 4 8 11 11 1 8 11 3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309
出力例 2
17254
Score : 250 points
Problem Statement
There are N boxes numbered 1 to N and N items numbered 1 to N. Item i (1 \leq i \leq N) is in box A_i and has a weight of W_i.
You can repeatedly perform the operation of choosing an item and moving it to another box zero or more times. If the weight of the item being moved is w, the cost of the operation is w.
Find the minimum total cost required to make each box contain exactly one item.
Constraints
- 1 \leq N \leq 10^{5}
- 1 \leq A_i \leq N (1 \leq i \leq N)
- 1 \leq W_i \leq 10^{4} (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N W_1 W_2 \ldots W_N
Output
Print the minimum total cost required to make each box contain exactly one item.
Sample Input 1
5 2 2 3 3 5 33 40 2 12 16
Sample Output 1
35
With the following two moves, you can make each box contain exactly one item:
- Move item 1 from box 2 to box 1. The cost is 33.
- Move item 3 from box 3 to box 4. The cost is 2.
The total cost of these two moves is 35. It is impossible to make each box contain exactly one item with a cost less than 35, so print 35.
Sample Input 2
12 3 6 7 4 12 4 8 11 11 1 8 11 3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309
Sample Output 2
17254
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
非負整数 N が与えられるので、以下の条件を満たす非負整数 x を昇順に全て出力してください。
- x を 2 進数として表記した時に 1 となる位の集合が、 N を 2 進数として表記した時に 1 となる位の集合の部分集合となる。
- すなわち、全ての非負整数 k について、「 x の 2^k の位が 1 ならば、 N の 2^k の位は 1 」が成り立つ。
制約
- N は整数
- 0 \le N < 2^{60}
- N を 2 進数として表記した時、 1 となる位は 15 個以下である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを 1 行に 1 つずつ、10 進法の整数として昇順に出力せよ。
入力例 1
11
出力例 1
0 1 2 3 8 9 10 11
N = 11_{(10)} を 2 進数で表記すると、 1011_{(2)} となります。
条件を満たす非負整数 x は以下の通りです。
- 0000_{(2)}=0_{(10)}
- 0001_{(2)}=1_{(10)}
- 0010_{(2)}=2_{(10)}
- 0011_{(2)}=3_{(10)}
- 1000_{(2)}=8_{(10)}
- 1001_{(2)}=9_{(10)}
- 1010_{(2)}=10_{(10)}
- 1011_{(2)}=11_{(10)}
入力例 2
0
出力例 2
0
入力例 3
576461302059761664
出力例 3
0 524288 549755813888 549756338176 576460752303423488 576460752303947776 576461302059237376 576461302059761664
入力は 32bit 符号付き整数に収まらない可能性があります。
Score : 300 points
Problem Statement
You are given a non-negative integer N. Print all non-negative integers x that satisfy the following condition in ascending order.
- The set of the digit positions containing 1 in the binary representation of x is a subset of the set of the digit positions containing 1 in the binary representation of N.
- That is, the following holds for every non-negative integer k: if the digit in the "2^ks" place of x is 1, the digit in the 2^ks place of N is 1.
Constraints
- N is an integer.
- 0 \le N < 2^{60}
- In the binary representation of N, at most 15 digit positions contain 1.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as decimal integers in ascending order, each in its own line.
Sample Input 1
11
Sample Output 1
0 1 2 3 8 9 10 11
The binary representation of N = 11_{(10)} is 1011_{(2)}.
The non-negative integers x that satisfy the condition are:
- 0000_{(2)}=0_{(10)}
- 0001_{(2)}=1_{(10)}
- 0010_{(2)}=2_{(10)}
- 0011_{(2)}=3_{(10)}
- 1000_{(2)}=8_{(10)}
- 1001_{(2)}=9_{(10)}
- 1010_{(2)}=10_{(10)}
- 1011_{(2)}=11_{(10)}
Sample Input 2
0
Sample Output 2
0
Sample Input 3
576461302059761664
Sample Output 3
0 524288 549755813888 549756338176 576460752303423488 576460752303947776 576461302059237376 576461302059761664
The input may not fit into a 32-bit signed integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
縦横 10^9 マスのグリッドがあります。上から i + 1 行目、左から j + 1 列目 (0 \leq i, j \lt 10^9) にあるマスを (i, j) と呼びます。(通常と異なる index の割り当て方に注意してください。)
各マスは黒マスか白マスのいずれかです。マス (i, j) の色は文字 P[i \bmod N][j \bmod N] によって表されて、B ならばマス (i, j) は黒マス、W ならば白マスです。ここで a \bmod b は a を b で割った余りを意味します。
Q 個のクエリが与えられるので順に処理してください。
各クエリでは 4 つの整数 A, B, C, D が与えられるので、(A, B) を左上隅、(C, D) を右下隅とする長方形領域に含まれる黒マスの個数を求めてください。
制約
- 1 \leq N \leq 1000
- P[i][j] は
WまたはB - 1 \leq Q \leq 2 \times 10^5
- 0 \leq A \leq C \lt 10^9
- 0 \leq B \leq D \lt 10^9
- N, Q, A, B, C, D は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \text{query}_i は i 番目に処理するクエリである。
N Q
P[0][0]P[0][1]\dots P[0][N-1]
P[1][0]P[1][1]\dots P[1][N-1]
\vdots
P[N-1][0]P[N-1][1]\dots P[N-1][N-1]
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
各クエリは以下の形式で与えられる。
A B C D
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
3 2 WWB BBW WBW 1 2 3 4 0 3 4 5
出力例 1
4 7
グリッドの左上部分を図示すると次の図のようになります。

1 番目のクエリについて、(1, 2) を左上隅、(3, 4) を右下隅とする長方形領域は図の赤い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 4 個です。
2 番目のクエリについて、(0, 3) を左上隅、(4, 5) を右下隅とする長方形領域は図の青い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 7 個です。
入力例 2
10 5 BBBWWWBBBW WWWWWBBBWB BBBWBBWBBB BBBWWBWWWW WWWWBWBWBW WBBWBWBBBB WWBBBWWBWB WBWBWWBBBB WBWBWBBWWW WWWBWWBWWB 5 21 21 93 35 35 70 43 55 72 61 84 36 33 46 95 0 0 999999999 999999999
出力例 2
621 167 44 344 500000000000000000
Score : 450 points
Problem Statement
There is a grid with 10^9 by 10^9 squares. Let (i, j) denote the square at the (i + 1)-th row from the top and the (j + 1)-th column from the left (0 \leq i, j \lt 10^9). (Note the unusual index assignment.)
Each square is black or white. The color of the square (i, j) is represented by a character P[i \bmod N][j \bmod N], where B means black, and W means white. Here, a \bmod b denotes the remainder when a is divided by b.
Answer Q queries.
Each query gives you four integers A, B, C, D and asks you to find the number of black squares contained in the rectangular area with (A, B) as the top-left corner and (C, D) as the bottom-right corner.
Constraints
- 1 \leq N \leq 1000
- P[i][j] is
WorB. - 1 \leq Q \leq 2 \times 10^5
- 0 \leq A \leq C \lt 10^9
- 0 \leq B \leq D \lt 10^9
- N, Q, A, B, C, D are all integers.
Input
The input is given from Standard Input in the following format. Here, \text{query}_i is the i-th query to be processed.
N Q
P[0][0]P[0][1]\dots P[0][N-1]
P[1][0]P[1][1]\dots P[1][N-1]
\vdots
P[N-1][0]P[N-1][1]\dots P[N-1][N-1]
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Each query is given in the following format:
A B C D
Output
Follow the instructions in the problem statement and print the answers to the queries, separated by newlines.
Sample Input 1
3 2 WWB BBW WBW 1 2 3 4 0 3 4 5
Sample Output 1
4 7
The figure below illustrates the upper left part of the grid.

For the first query, the rectangular area with (1, 2) as the top-left corner and (3, 4) as the bottom-right corner, surrounded by the red frame in the figure, contains four black squares.
For the second query, the rectangular area with (0, 3) as the top-left corner and (4, 5) as the bottom-right corner, surrounded by the blue frame in the figure, contains seven black squares.
Sample Input 2
10 5 BBBWWWBBBW WWWWWBBBWB BBBWBBWBBB BBBWWBWWWW WWWWBWBWBW WBBWBWBBBB WWBBBWWBWB WBWBWWBBBB WBWBWBBWWW WWWBWWBWWB 5 21 21 93 35 35 70 43 55 72 61 84 36 33 46 95 0 0 999999999 999999999
Sample Output 2
621 167 44 344 500000000000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点は 1, \dots, N と番号付けられ、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i, V_i を結んでいます。
それぞれの頂点を赤または青で塗る方法は全部で 2^N 通りあります。これらのうち、以下の条件を全て満たすものの総数を 998244353 で割った余りを求めてください。
- 赤く塗られた頂点がちょうど K 個ある
- 異なる色で塗られた頂点を結ぶ辺の本数は偶数である
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K U_1 V_1 \vdots U_M V_M
出力
答えを出力せよ。
入力例 1
4 4 2 1 2 1 3 2 3 3 4
出力例 1
2
以下の 2 通りが条件を満たします。
- 頂点 1, 2 を赤く、頂点 3, 4 を青く塗る。
- 頂点 3, 4 を赤く、頂点 1, 2 を青く塗る。
上記の塗り方について、異なる色で塗られた頂点を結ぶ辺は 2 番目の辺と 3 番目の辺です。
入力例 2
10 10 3 1 2 2 4 1 5 3 6 3 9 4 10 7 8 9 10 5 9 3 4
出力例 2
64
Score : 500 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertices U_i and V_i.
There are 2^N ways to paint each vertex red or blue. Find the number, modulo 998244353, of such ways that satisfy all of the following conditions:
- There are exactly K vertices painted red.
- There is an even number of edges connecting vertices painted in different colors.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K U_1 V_1 \vdots U_M V_M
Output
Print the answer.
Sample Input 1
4 4 2 1 2 1 3 2 3 3 4
Sample Output 1
2
The following two ways satisfy the conditions.
- Paint Vertices 1 and 2 red and Vertices 3 and 4 blue.
- Paint Vertices 3 and 4 red and Vertices 1 and 2 blue.
In either of the ways above, the 2-nd and 3-rd edges connect vertices painted in different colors.
Sample Input 2
10 10 3 1 2 2 4 1 5 3 6 3 9 4 10 7 8 9 10 5 9 3 4
Sample Output 2
64
Time Limit: 3.5 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
あなたの前に体力 H のモンスターが現れ、ターン制の戦闘が開始しました。
あなたは、ターン 1,2,… のそれぞれに呪文 1,…,N の N 種類の呪文のうち一つを唱えます。
ターン i に呪文 j を唱えると、その呪文の効果としてターン i,i+1,…,i+t_j -1 のそれぞれでモンスターの体力が d_j 減ります。
モンスターの体力を 0 以下にすることが可能な最も早いターンを求めてください。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq H \leq 10^{18}
- 1 \leq t_i,d_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H t_1 d_1 \vdots t_N d_N
出力
答えを出力せよ。
入力例 1
2 20 2 2 5 1
出力例 1
6
以下のようにするとターン 6 にモンスターの体力を 0 以下に出来ます。これが最も早いターンです。
- ターン 1 に魔法 1 を使う。ターン 1 に使った魔法の効果でモンスターの体力が 2 減り、18 になる。
- ターン 2 に魔法 2 を使う。ターン 1,2 に使った魔法の効果でモンスターの体力が 2+1=3 減り、15 になる。
- ターン 3 に魔法 1 を使う。ターン 2,3 に使った魔法の効果でモンスターの体力が 1+2=3 減り、12 になる。
- ターン 4 に魔法 2 を使う。ターン 2,3,4 に使った魔法の効果でモンスターの体力が 1+2+1=4 減り、8 になる。
- ターン 5 に魔法 1 を使う。ターン 2,4,5 に使った魔法の効果でモンスターの体力が 1+1+2=4 減り、4 になる。
- ターン 6 に魔法 2 を使う。ターン 2,4,5,6 に使った魔法の効果でモンスターの体力が 1+1+2+1=5 減り、-1 になる。
入力例 2
10 200 1 21 1 1 1 1 8 4 30 1 3 1 10 2 8 1 9 1 4 4
出力例 2
9
Score : 525 points
Problem Statement
A monster with health H has appeared in front of you, and a turn-based battle has started.
In each turn 1,2,…, you cast one of N spells, spells 1,…,N.
If you cast spell i in turn j, the monster's health reduces by d_i in each turn j,j+1,…,j+t_i -1.
Find the earliest turn that you can make the monster's health 0 or less.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq H \leq 10^{18}
- 1 \leq t_i,d_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N H t_1 d_1 \vdots t_N d_N
Output
Print the answer.
Sample Input 1
2 20 2 2 5 1
Sample Output 1
6
The following procedure makes the monster's health 0 or less in turn 6, which is the earliest.
- Cast spell 1 in turn 1. Due to the spell cast in turn 1, the monster's health reduces by 2 and becomes 18.
- Cast spell 2 in turn 2. Due to the spells cast in turns 1 and 2, the monster's health reduces by 2+1=3 and becomes 15.
- Cast spell 1 in turn 3. Due to the spells cast in turns 2 and 3, the monster's health reduces by 1+2=3 and becomes 12.
- Cast spell 2 in turn 4. Due to the spells cast in turns 2,3 and 4, the monster's health reduces by 1+2+1=4 and becomes 8.
- Cast spell 1 in turn 5. Due to the spells cast in turns 2,4 and 5, the monster's health reduces by 1+1+2=4 and becomes 4.
- Cast spell 2 in turn 6. Due to the spells cast in turns 2,4,5 and 6, the monster's health reduces by 1+1+2+1=5 and becomes -1.
Sample Input 2
10 200 1 21 1 1 1 1 8 4 30 1 3 1 10 2 8 1 9 1 4 4
Sample Output 2
9