A - flip

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

012 種類の文字からなる文字列 s が与えられます。 s に含まれる 01 に、10 に置き換えた文字列を出力してください。

制約

  • s の長さは 1 以上 10 以下
  • s012 種類の文字からなる

入力

入力は以下の形式で標準入力から与えられる。

s

出力

答えを 1 行で出力せよ。


入力例 1

01

出力例 1

10

s1 文字目は 1 なので、1 文字目に出力すべき文字は 0 です。 s2 文字目は 0 なので、2 文字目に出力すべき文字は 1 です。


入力例 2

1011

出力例 2

0100

入力例 3

100100001

出力例 3

011011110

Score : 100 points

Problem Statement

You are given a string s consisting of two kinds of characters, 0 and 1. Print the string obtained by replacing 0 with 1 and 1 with 0 in s.

Constraints

  • The length of s is between 1 and 10, inclusive.
  • s consists of two kinds of characters, 0 and 1.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer in a single line.


Sample Input 1

01

Sample Output 1

10

The 1-st character of s is 1, so the 1-st character to print is 0. The 2-nd character of s is 0, so the 2-nd character to print is 1.


Sample Input 2

1011

Sample Output 2

0100

Sample Input 3

100100001

Sample Output 3

011011110
B - 1-2-4 Test

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

3 問の問題からなる試験があり、それぞれの問題の配点は 1 点、2 点、4 点でした。

高橋君、青木君、すぬけ君の 3 人がこの試験を受け、 高橋君は A 点、青木君は B 点を取りました。

すぬけ君は、高橋君と青木君のうち少なくとも一方が解けた問題は解け、 2 人とも解けなかった問題は解けませんでした。

すぬけ君の点数を求めてください。

ただし、この問題の制約下で、すぬけ君の点数は一意に定まる事が証明できます。

制約

  • 0\leq A,B \leq 7
  • A,B は整数

入力

入力は以下の形式で標準入力から与えられる。

A B

出力

すぬけ君の点数を整数で出力せよ。


入力例 1

1 2

出力例 1

3

高橋君は 1 点を取った事から、 1 点の問題のみを正解し、それ以外の 2 問は解けなかったことがわかります。
同様に、青木君は 2 点を取った事から、 2 点の問題のみを正解し、それ以外の 2 問は解けなかったことがわかります。

よって、すぬけ君は 1 点の問題と 2 点の問題を正解し、高橋君と青木君がともに解けなかった 4 点の問題はすぬけ君も解けなかったことになるので、3 点を取ったことがわかります。よって、3 を出力します。


入力例 2

5 3

出力例 2

7

高橋君は 5 点を取った事から、 1 点の問題と 4 点の問題を正解し、 2 点の問題は解けなかったことがわかります。
同様に、青木君は 3 点を取った事から、 1 点の問題と 2 点の問題を正解し、 4 点の問題は解けなかったことがわかります。

よって、3 問すべてについて、高橋君と青木君の少なくとも一方が正解しているため、すぬけ君はすベての問題に正解し、7 点を取ったことがわかります。 よって、7 を出力します。


入力例 3

0 0

出力例 3

0

高橋君と青木君は 2 人ともいずれの問題も解けていません。 よって、すぬけ君もいずれの問題も解けておらず、 0 を出力します。

Score : 100 points

Problem Statement

There was an exam consisting of three problems worth 1, 2, and 4 points.

Takahashi, Aoki, and Snuke took this exam. Takahashi scored A points, and Aoki scored B points.

Snuke solved all of the problems solved by at least one of Takahashi and Aoki, and failed to solve any of the problems solved by neither of them.

Find Snuke's score.

It can be proved that Snuke's score is uniquely determined under the Constraints of this problem.

Constraints

  • 0\leq A,B \leq 7
  • A and B are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Print Snuke's score as an integer.


Sample Input 1

1 2

Sample Output 1

3

Since Takahashi scored 1 point, we see that he solved only the 1-point problem and failed to solve the other two.
Similarly, since Aoki scored 2 points, we see that he solved only the 2-point problem and failed to solve the other two.

Therefore, Snuke must have solved the 1- and 2-point problems, but not the 4-point one, which Takahashi and Aoki both failed to solve, for a score of 3 points. Thus, 3 should be printed.


Sample Input 2

5 3

Sample Output 2

7

Since Takahashi scored 5 points, we see that he solved the 1- and 4-point problems but not the 2-point one.
Similarly, since Aoki scored 3 points, we see that he solved the 1- and 2-point problems but not the 4-point one.

Therefore, each of the three problems is solved by at least one of Takahashi and Aoki, so we see that Snuke solved all of the problems, for a score of 7 points. Thus, 7 should be printed.


Sample Input 3

0 0

Sample Output 3

0

Both Takahashi and Aoki solved none of the problems. Therefore, so did Snuke. Thus, 0 should be printed.

C - Langton's Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

HW 列のグリッドがあり、はじめすべてのマスが白で塗られています。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

このグリッドはトーラス状であるとみなします。すなわち、各 1 \leq i \leq H に対して (i, W) の右に (i, 1) があり、各 1 \leq j \leq W に対して (H, j) の下に (1, j) があるとします。

高橋君が (1, 1) にいて上を向いています。高橋君が以下の操作を N 回繰り返した後のグリッドの各マスがどの色で塗られているか出力してください。

  • 現在いるマスが白で塗られている場合は、現在いるマスを黒に塗り替え、時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。そうでない場合は、現在いるマスを白に塗り替え、反時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。

制約

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • 入力される数値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

H W N

出力

H 行出力せよ。i 行目には長さ W の文字列であって、(i, j) が白で塗られている場合は j 文字目が .、黒で塗られている場合は j 文字目が # であるものを出力せよ。


入力例 1

3 4 5

出力例 1

.#..
##..
....

グリッドの各マスは操作によって以下のように変化します。

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

入力例 2

2 2 1000

出力例 2

..
..

入力例 3

10 10 10

出力例 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#

Score: 250 points

Problem Statement

There is a grid with H rows and W columns; initially, all cells are painted white. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

This grid is considered to be toroidal. That is, (i, 1) is to the right of (i, W) for each 1 \leq i \leq H, and (1, j) is below (H, j) for each 1 \leq j \leq W.

Takahashi is at (1, 1) and facing upwards. Print the color of each cell in the grid after Takahashi repeats the following operation N times.

  • If the current cell is painted white, repaint it black, rotate 90^\circ clockwise, and move forward one cell in the direction he is facing. Otherwise, repaint the current cell white, rotate 90^\circ counterclockwise, and move forward one cell in the direction he is facing.

Constraints

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

H W N

Output

Print H lines. The i-th line should contain a string of length W where the j-th character is . if the cell (i, j) is painted white, and # if it is painted black.


Sample Input 1

3 4 5

Sample Output 1

.#..
##..
....

The cells of the grid change as follows due to the operations:

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

Sample Input 2

2 2 1000

Sample Output 2

..
..

Sample Input 3

10 10 10

Sample Output 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#
D - Parking 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

駐車場があります。この駐車場に駐車するときの料金は以下の通りです。

  • L 時ちょうどから R 時ちょうどまでのあいだは、1 時間停めるごとに X の料金がかかる
  • 上に該当しない時間のあいだは、1 時間停めるごとに Y の料金がかかる

この駐車場に、日をまたぐことなく車を A 時ちょうどから B 時ちょうどまで停めたとき、料金はいくらになりますか?

制約

  • 1 \leq X, Y \leq 1000
  • 1 \leq L < R \leq 23
  • 1 \leq A < B \leq 23
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

X Y L R A B

出力

答えを出力せよ。


入力例 1

700 300 9 17 7 21

出力例 1

7400

車を 7 時から 21 時のあいだ停めたとき、かかる料金は以下のようになります。

  • 7 時から 9 時までの 2 時間は 300 \times 2 = 600 の料金がかかる
  • 9 時から 17 時までの 8 時間は 700 \times 8 = 5600 の料金がかかる
  • 17 時から 21 時までの 4 時間は 300 \times 4 = 1200 の料金がかかる

600 + 5600 + 1200 = 7400 が求める答えです。


入力例 2

600 500 9 17 17 20

出力例 2

1500

車を 17 時から 20 時のあいだ停めたとき、かかる料金は以下のようになります。

  • 17 時から 20 時までの 3 時間は 500 \times 3 = 1500 の料金がかかる

1500 が求める答えです。


入力例 3

900 200 12 14 11 13

出力例 3

1100

Score : 200 points

Problem Statement

There is a parking lot. The fee for parking in this lot is as follows:

  • During the period from exactly L o'clock to exactly R o'clock, a fee of X is charged for each hour parked.
  • During the period not covered above, a fee of Y is charged for each hour parked.

If a car is parked in this lot from exactly A o'clock to exactly B o'clock without crossing midnight, how much is the fee?

Constraints

  • 1 \leq X, Y \leq 1000
  • 1 \leq L < R \leq 23
  • 1 \leq A < B \leq 23
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

X Y L R A B

Output

Output the answer.


Sample Input 1

700 300 9 17 7 21

Sample Output 1

7400

If a car is parked from 7 o'clock to 21 o'clock, the fee is as follows.

  • For the 2 hours from 7 o'clock to 9 o'clock, a fee of 300 \times 2 = 600 is charged.
  • For the 8 hours from 9 o'clock to 17 o'clock, a fee of 700 \times 8 = 5600 is charged.
  • For the 4 hours from 17 o'clock to 21 o'clock, a fee of 300 \times 4 = 1200 is charged.

The answer is 600 + 5600 + 1200 = 7400.


Sample Input 2

600 500 9 17 17 20

Sample Output 2

1500

If a car is parked from 17 o'clock to 20 o'clock, the fee is as follows.

  • For the 3 hours from 17 o'clock to 20 o'clock, a fee of 500 \times 3 = 1500 is charged.

The answer is 1500.


Sample Input 3

900 200 12 14 11 13

Sample Output 3

1100
E - Dislike Foods

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoderレストランでは 1 から N までの番号がつけられている N 種類の食材を扱っています。

また、AtCoderレストランでは 1 から M までの番号がつけられている M 個の料理を提供しています。料理 i には K_i 種類の食材が使われており、食材 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} が使われています。

すぬけくんは現在 N 種類の食材がすべて苦手です。また、すぬけ君は苦手な食材が 1 種類でも使われている料理を食べることができず、苦手な食材が 1 種類も使われていない料理を食べることができます。

すぬけ君はこれから N 日間かけて苦手な食材を克服しようとしています。 すぬけ君は i 日目に食材 B_i を克服し、それ以降苦手な食材でなくなります。

i=1,2,\ldots,N について以下の値を求めてください。

  • i 日目にすぬけ君が食材 B_i を克服した直後、すぬけ君が食べることができるAtCoderレストランの料理の個数

制約

  • 1 \leq N \leq 3 \times 10^{5}
  • 1 \leq M \leq 3 \times 10^{5}
  • 1 \leq K_i \leq N (1 \leq i \leq M)
  • K_i の総和は 3 \times 10^{5} 以下
  • 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
  • A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
  • 1 \leq B_i \leq N (1 \leq i \leq N)
  • B_i \neq B_j (i \neq j )
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N

出力

N 行出力せよ。k 行目には、i=k のときの値を出力せよ。


入力例 1

5 4
2 1 2
3 3 4 5
3 1 2 5
1 3
1 3 2 5 4

出力例 1

0
1
2
3
4

以下のようにすぬけ君は苦手な食材を克服します。

  • 1 日目: 食材 1 を克服する。このとき、どの料理にも苦手な食材が使われているため 0 を出力する。
  • 2 日目: 食材 3 を克服する。このとき、料理 4 は苦手な食材が使われなくなるため食べられるようになる。料理 4 以外の料理は苦手な食材が使われているため、 1 を出力する。
  • 3 日目: 食材 2 を克服する。このとき、料理 1 は苦手な食材が使われなくなるため食べられるようになる。料理 1,4 以外の料理は苦手な食材が使われているため、 2 を出力する。
  • 4 日目: 食材 5 を克服する。このとき、料理 3 は苦手な食材が使われなくなるため食べられるようになる。料理 1,3,4 以外の料理は苦手な食材が使われているため、 3 を出力する。
  • 5 日目: 食材 4 を克服する。このとき、料理 2 は苦手な食材が使われなくなるため食べられるようになる。全ての料理に苦手な食材が使われていないため、 4 を出力する。

入力例 2

9 8
1 4
5 6 9 7 4 3
4 2 4 1 3
1 1
5 7 9 8 1 5
2 9 8
1 2
1 1
6 5 2 7 8 4 1 9 3

出力例 2

0
0
1
1
1
2
4
6
8

Score : 300 points

Problem Statement

The AtCoder Restaurant uses N types of ingredients numbered from 1 to N.

The restaurant offers M dishes numbered from 1 to M. Dish i uses K_i types of ingredients, namely A_{i,1}, A_{i,2}, \ldots, A_{i,K_i}.

Snuke currently dislikes all N ingredients. He cannot eat any dish that uses one or more ingredients he dislikes, and he can eat a dish that uses none of the disliked ingredients.

Over the next N days, he will overcome his dislikes one ingredient per day. On day i, he overcomes ingredient B_i, and from then on he no longer dislikes it.

For each i=1,2,\ldots,N, find:

  • the number of dishes at the AtCoder Restaurant that he can eat immediately after overcoming ingredient B_i on day i.

Constraints

  • 1 \leq N \leq 3 \times 10^{5}
  • 1 \leq M \leq 3 \times 10^{5}
  • 1 \leq K_i \leq N (1 \leq i \leq M)
  • The sum of K_i is at most 3 \times 10^{5}.
  • 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
  • A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
  • 1 \leq B_i \leq N (1 \leq i \leq N)
  • B_i \neq B_j (i \neq j )
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N

Output

Print N lines. The k-th line should contain the answer for i=k.


Sample Input 1

5 4
2 1 2
3 3 4 5
3 1 2 5
1 3
1 3 2 5 4

Sample Output 1

0
1
2
3
4

Snuke overcomes his disliked ingredients as follows:

  • Day 1: He overcomes ingredient 1. At this time, every dish still uses a disliked ingredient, so print 0.
  • Day 2: He overcomes ingredient 3. Dish 4 no longer uses any disliked ingredient and becomes edible; all other dishes still use disliked ingredients, so print 1.
  • Day 3: He overcomes ingredient 2. Dish 1 no longer uses any disliked ingredient and becomes edible; all dishes except 1 and 4 still use disliked ingredients, so print 2.
  • Day 4: He overcomes ingredient 5. Dish 3 no longer uses any disliked ingredient and becomes edible; all dishes except 1, 3, and 4 still use disliked ingredients, so print 3.
  • Day 5: He overcomes ingredient 4. Dish 2 no longer uses any disliked ingredient and becomes edible; now all dishes have no disliked ingredients, so print 4.

Sample Input 2

9 8
1 4
5 6 9 7 4 3
4 2 4 1 3
1 1
5 7 9 8 1 5
2 9 8
1 2
1 1
6 5 2 7 8 4 1 9 3

Sample Output 2

0
0
1
1
1
2
4
6
8
F - Sushi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

寿司の材料として、N 個のシャリと M 個のネタがあります。

i 番目のシャリの重さは A_ij 番目のネタの重さは B_j です。

あなたは、シャリとネタを組み合わせることで寿司を作ろうとしています。

寿司を 1 つ作るためには、1 つのシャリと 1 つのネタを組み合わせる必要があります。ただし、ネタの重さはシャリの重さの 2 倍以下でなければなりません。また、1 つのシャリやネタを複数の寿司に使うことはできません。

作ることのできる寿司の個数の最大値を求めてください。

制約

  • 1 \leq N, M \leq 2 \times 10^5
  • 1 \leq A_i, B_j \leq 10^9
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

答えを出力せよ。


入力例 1

4 5
4 2 1 8
14 9 3 2 9

出力例 1

3

1 番目のシャリと 3 番目のネタ、2 番目のシャリと 4 番目のネタ、4 番目のシャリと 1 番目のネタを組み合わせるなどの方法で 3 つの寿司を作ることができます。4 つ以上の寿司を作ることはできないため、3 を出力します。


入力例 2

3 3
5 5 3
11 1000 1000

出力例 2

0

入力例 3

8 7
2 3 4 4 4 3 2 3
8 5 5 9 9 7 1

出力例 3

5

Score : 300 points

Problem Statement

There are N pieces of shari (vinegared rice) and M pieces of neta (toppings) as ingredients for sushi.

The weight of the i-th shari is A_i, and the weight of the j-th neta is B_j.

You will make sushi by combining shari and neta.

To make one piece of sushi, you need to combine one shari with one neta. Here, the weight of the neta must be at most twice the weight of the shari. Also, the same shari or neta cannot be used in multiple pieces of sushi.

Find the maximum number of sushi that can be made.

Constraints

  • 1 \leq N, M \leq 2 \times 10^5
  • 1 \leq A_i, B_j \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Output the answer.


Sample Input 1

4 5
4 2 1 8
14 9 3 2 9

Sample Output 1

3

By combining the 1st shari with the 3rd neta, the 2nd shari with the 4th neta, and the 4th shari with the 1st neta, for example, you can make three pieces of sushi. It is impossible to make four or more pieces of sushi, so output 3.


Sample Input 2

3 3
5 5 3
11 1000 1000

Sample Output 2

0

Sample Input 3

8 7
2 3 4 4 4 3 2 3
8 5 5 9 9 7 1

Sample Output 3

5
G - Odd or Even

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

この問題は インタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。

整数 N および N 未満の 奇数 K が与えられます。
ジャッジシステムは、0 および 1 からなる長さ N の数列 A = (A_1, A_2, \dots, A_N) を隠し持っています。

あなたは数列 A の要素の値を直接知ることはできません。
その代わりに、ジャッジシステムに対して以下の質問を N 回まで行うことができます。

  • 1 以上 N 以下の相異なる整数 x_1, x_2, \dots, x_K を選ぶ。そして、A_{x_1} + A_{x_2} + \dots + A_{x_K} の偶奇を聞く。

N 回以下の質問で (A_1, A_2, \dots, A_N) を全て特定して、答えを出力してください。
ただし、ジャッジは適応的です。言い換えると、ジャッジシステムは今までの質問の回答に矛盾しない範囲でA の内容を自由に変更することができます。
そのため、出力が次の条件を満たす場合にあなたの作成したプログラムは正解とみなされます。それ以外の場合は不正解とみなされます。

  • ここまでの質問の回答と矛盾しないような数列が一意に定まっており、かつそれがプログラムが出力した数列と一致している。

制約

  • 1 \leq K \lt N \leq 1000
  • K は奇数
  • A_i0 または 1

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。

最初に、N および K を標準入力から受け取ってください。

N K

次に、(A_1, A_2, \dots, A_N) を全て特定できるまで質問を繰り返してください。
質問は、以下の形式で標準出力に出力してください。ここで x_1, x_2, \dots, x_K1 以上 N 以下の相異なる K 個の整数です。

? x_1 x_2 \dots x_K

これに対する応答は、次の形式で標準入力から与えられます。

T

ここで、T は質問に対する答えで、

  • T0 である場合は A_{x_1} + A_{x_2} + \dots + A_{x_K} は偶数であることを、
  • T1 である場合は A_{x_1} + A_{x_2} + \dots + A_{x_K} は奇数であることを意味します。

ただし、x_1, x_2, \dots, x_K が制約を満たしていないか、質問の回数が N 回を超えた場合は T-1 となります。

ジャッジが -1 を返した場合、プログラムはすでに不正解とみなされています。この場合、ただちにプログラムを終了してください。

A の要素を全て特定できたら、特定した A の要素を以下の形式で出力してください。その後、ただちにプログラムを終了してください。

! A_1 A_2 \dots A_N

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で誤った出力形式による出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
  • 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
  • ジャッジは適応的です。言い換えると、ジャッジシステムは今までの質問の回答に矛盾しない範囲で A の内容を変更することができます。

入出力例

以下の入出力例は N=5, K=3 の場合の入出力例です。この入出力例の通りに出力するとジャッジ結果は WA になることに注意してください。
入出力例では、プログラムが出力した A = (1, 0, 1, 1, 0) はここまでの質問の回答に矛盾しない数列ですが、例えば (0, 0, 1, 0, 0) もここまでの質問の回答に矛盾しない数列であるため、数列 A は一意に定まっていません。そのため、このプログラムは不正解とみなされます。

入力 出力 説明
5 3 まず整数 N および K が与えられます。
? 2 4 1 (x_1, x_2, x_3) = (2, 4, 1) として質問を行います。
0 質問の答えは 0 なので、ジャッジはその値を返します。
? 5 3 2 (x_1, x_2, x_3) = (5, 3, 2) として質問を行います。
1 質問の答えは 1 なので、ジャッジはその値を返します。
! 1 0 1 1 0 A の答えとして (1, 0, 1, 1, 0) を出力します。A を一意に特定できていないのでジャッジ結果は WA になります。

Score : 550 points

Problem Statement

This is an interactive task (where your program and the judge interact via Standard Input and Output).

You are given an integer N and an odd number K.
The judge has a hidden length-N sequence A = (A_1, A_2, \dots, A_N) consisting of 0 and 1.

While you cannot directly access the elements of sequence A, you are allowed to ask the judge the following query at most N times.

  • Choose distinct integers x_1, x_2, \dots, and x_K between 1 and N, inclusive, to ask the parity of A_{x_1} + A_{x_2} + \dots + A_{x_K}.

Determine (A_1, A_2, \dots, A_N) by at most N queries, and print the answer.
Here, the judge is adaptive. In other words, the judge may modify the contents of A as long as it is consistent with the responses to the past queries.
Therefore, your program is considered correct if the output satisfies the following condition, and incorrect otherwise:

  • your program prints a sequence consistent with the responses to the queries so far, and that is the only such sequence.

Constraints

  • 1 \leq K \lt N \leq 1000
  • K is odd.
  • A_i is 0 or 1.

Input and Output

This is an interactive task (where your program and the judge interact via Standard Input and Output).

First of all, receive N and K from Standard Input.

N K

Then, repeat asking queries until you can uniquely determine (A_1, A_2, \dots, A_N).
Each query should be printed to Standard Output in the following format, where x_1, x_2, \dots, and x_K are K distinct integers between 1 and N, inclusive.

? x_1 x_2 \dots x_K

The response to the query is given from Standard Input in the following format.

T

Here, T denotes the response to the query.

  • T is 0 when A_{x_1} + A_{x_2} + \dots + A_{x_K} is even, and
  • T is 1 when A_{x_1} + A_{x_2} + \dots + A_{x_K} is odd.

However, if x_1, x_2, \dots and x_K do not satisfy the constraints, or the number of queries exceeds N, then T is -1.

If the judge returns -1, your program is already considered incorrect, so terminate the program immediately.

When you can determine all the elements of A, print those elements in the following format, and terminate the program immediately.

! A_1 A_2 \dots A_N

Notes

  • Print a newline and flush Standard Output at the end of each message. Otherwise, you may get a TLE verdict.
  • The verdict will be indeterminate if there is malformed output during the interaction or your program quits prematurely.
  • Terminate the program immediately after printing the answer, or the verdict will be indeterminate.
  • The judge for this problem is adaptive. This means that the judge may modify the contents of A as long as it is consistent with the responses to the past queries.

Sample Interaction

In the following interaction, N=5 and K=3. Note that the following output itself will result in WA.
Here, A = (1, 0, 1, 1, 0) is indeed consistent with the responses, but so is (0, 0, 1, 0, 0), so sequence A is not uniquely determined. Thus, this program is considered incorrect.

Input Output Description
5 3 First, you are given integers N and K.
? 2 4 1 You ask a query with (x_1, x_2, x_3) = (2, 4, 1).
0 The response to the query is 0, so the judge returns that value.
? 5 3 2 You ask a query with (x_1, x_2, x_3) = (5, 3, 2).
1 The response to the query is 1, so the judge returns that value.
! 1 0 1 1 0 You print (1, 0, 1, 1, 0) to guess A. Since sequence A is not uniquely determined, the verdict will be WA.
H - A Path in A Dictionary

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

N 頂点 M 辺の単純連結無向グラフ G が与えられます。
G の頂点は頂点 1, 頂点 2, \ldots, 頂点 N と番号付けられており、 i (1\leq i\leq M) 本目の辺は頂点 U_i と頂点 V_i を結んでいます。

G における頂点 X から頂点 Y への単純パスのうち辞書順最小のものを求めてください。
すなわち、以下の条件をみたす整数列 P=(P_1,P_2,\ldots,P_{\lvert P\rvert}) の中で辞書順最小のものを求めてください。

  • 1\leq P_i\leq N
  • i\neq j ならば P_i\neq P_j
  • P_1=X かつ P_{\lvert P\rvert}=Y
  • 1\leq i\leq \lvert P\rvert-1 について、頂点 P_i と頂点 P_{i+1} を結ぶ辺が存在する。

なお、本問題の制約下で条件をみたすようなものが必ず存在することが証明できます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

整数列の辞書順 とは 整数列 S=(S_1,S_2,\ldots,S_{\lvert S\rvert}) が整数列 T=(T_1,T_2,\ldots,T_{\lvert T\rvert}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、\lvert S\rvert, \lvert T\rvert はそれぞれ S,T の長さを表します。
  1. \lvert S\rvert<\lvert T\rvert かつ (S_1,S_2,\ldots,S_{\lvert S\rvert})=(T_1,T_2,\ldots,T_{\lvert S\rvert})
  2. ある 1\leq i\leq \min(\lvert S\rvert,\lvert T\rvert) が存在して (S_1,S_2,\ldots,S_{i-1})=(T_1,T_2,\ldots,T_{i-1}) かつ S_i< T_i

制約

  • 1\leq T\leq 500
  • 2\leq N\leq 1000
  • N-1\leq M\leq \min\left( \frac{N(N-1)}{2},5\times 10^4\right)
  • 1\leq X,Y \leq N
  • X\neq Y
  • 1\leq U_i<V_i \leq N
  • i\neq j ならば (U_i,V_i)\neq (U_j,V_j)
  • 与えられるグラフは連結である。
  • 1 つの入力における N の総和は 1000 以下である。
  • 1 つの入力における M の総和は 5\times 10^4 以下である。
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_ii 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。

N M X Y 
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

T 行出力せよ。
i 行目 (1\leq i\leq T) には、i 個目のテストケースの答えとなる単純パス上の頂点番号を、順に空白区切りで出力せよ。
すなわち i 個目のテストケースに対する答えが P=(P_1,P_2,\ldots,P_{\lvert P\rvert}) であるとき、 P_1, P_2, \ldots, P_{\lvert P\rvert}i 行目にこの順に空白区切りで出力せよ。


入力例 1

2
6 10 3 5
1 2
1 3
1 5
1 6
2 4
2 5
2 6
3 4
3 5
5 6
3 2 3 2
1 3
2 3

出力例 1

3 1 2 5
3 2

1 つめのテストケースについて、グラフ G は次のようになっています。

G 上の頂点 3 から頂点 5 への単純パスを辞書順に列挙すると、次のとおりになります。

  • (3,1,2,5)
  • (3,1,2,6,5)
  • (3,1,5)
  • (3,1,6,2,5)
  • (3,1,6,5)
  • (3,4,2,1,5)
  • (3,4,2,1,6,5)
  • (3,4,2,5)
  • (3,4,2,6,1,5)
  • (3,4,2,6,5)
  • (3,5)

このうち、辞書順最小のものは (3,1,2,5) であるため、1 行目には 3,1,2,5 を空白区切りで出力します。

2 つめのテストケースにおいては、(3,2) が頂点 3 から頂点 2 への唯一の単純パスです。

Score : 475 points

Problem Statement

You are given a simple connected undirected graph G with N vertices and M edges.
The vertices of G are numbered vertex 1, vertex 2, \ldots, vertex N, and the i-th (1\leq i\leq M) edge connects vertices U_i and V_i.

Find the lexicographically smallest simple path from vertex X to vertex Y in G.
That is, find the lexicographically smallest among the integer sequences P=(P_1,P_2,\ldots,P_{\lvert P\rvert}) that satisfy the following conditions:

  • 1\leq P_i\leq N
  • If i\neq j, then P_i\neq P_j.
  • P_1=X and P_{\lvert P\rvert}=Y.
  • For 1\leq i\leq \lvert P\rvert-1, there exists an edge connecting vertices P_i and P_{i+1}.

One can prove that such a path always exists under the constraints of this problem.

You are given T test cases, so find the answer for each.

Lexicographic order on integer sequences An integer sequence S=(S_1,S_2,\ldots,S_{\lvert S\rvert}) is lexicographically smaller than an integer sequence T=(T_1,T_2,\ldots,T_{\lvert T\rvert}) if either of the following 1. or 2. holds. Here, \lvert S\rvert and \lvert T\rvert represent the lengths of S and T, respectively.
  1. \lvert S\rvert<\lvert T\rvert and (S_1,S_2,\ldots,S_{\lvert S\rvert})=(T_1,T_2,\ldots,T_{\lvert S\rvert}).
  2. There exists some 1\leq i\leq \min(\lvert S\rvert,\lvert T\rvert) such that (S_1,S_2,\ldots,S_{i-1})=(T_1,T_2,\ldots,T_{i-1}) and S_i< T_i.

Constraints

  • 1\leq T\leq 500
  • 2\leq N\leq 1000
  • N-1\leq M\leq \min\left( \frac{N(N-1)}{2},5\times 10^4\right)
  • 1\leq X,Y \leq N
  • X\neq Y
  • 1\leq U_i<V_i \leq N
  • If i\neq j, then (U_i,V_i)\neq (U_j,V_j).
  • The given graph is connected.
  • The sum of N over all test cases in each input is at most 1000.
  • The sum of M over all test cases in each input is at most 5\times 10^4.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:

N M X Y 
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Output T lines.
The i-th line (1\leq i\leq T) should contain the vertex numbers on the simple path that is the answer to the i-th test case, in order, separated by spaces.
That is, when the answer to the i-th test case is P=(P_1,P_2,\ldots,P_{\lvert P\rvert}), output P_1, P_2, \ldots, P_{\lvert P\rvert} on the i-th line in this order, separated by spaces.


Sample Input 1

2
6 10 3 5
1 2
1 3
1 5
1 6
2 4
2 5
2 6
3 4
3 5
5 6
3 2 3 2
1 3
2 3

Sample Output 1

3 1 2 5
3 2

For the first test case, graph G is as follows:

The simple paths from vertex 3 to vertex 5 on G, listed in lexicographic order, are as follows:

  • (3,1,2,5)
  • (3,1,2,6,5)
  • (3,1,5)
  • (3,1,6,2,5)
  • (3,1,6,5)
  • (3,4,2,1,5)
  • (3,4,2,1,6,5)
  • (3,4,2,5)
  • (3,4,2,6,1,5)
  • (3,4,2,6,5)
  • (3,5)

Among these, the lexicographically smallest is (3,1,2,5), so output 3,1,2,5 separated by spaces on the first line.

For the second test case, (3,2) is the only simple path from vertex 3 to vertex 2.

I - Double Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

整数列 A = (A_1, A_2, \dots, A_N) が与えられます。
次の式を計算してください。

\displaystyle \sum_{i=1}^N \sum_{j=i+1}^N \max(A_j - A_i, 0)


なお、制約下において答えが 2^{63} 未満となることは保証されています。

制約

  • 2 \leq N \leq 4 \times 10^5
  • 0 \leq A_i \leq 10^8
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \dots A_N

出力

式の値を出力せよ。


入力例 1

3
2 5 3

出力例 1

4

(i, j) = (1, 2) のとき \max(A_j - A_i, 0) = \max(3, 0) = 3 です。
(i, j) = (1, 3) のとき \max(A_j - A_i, 0) = \max(1, 0) = 1 です。
(i, j) = (2, 3) のとき \max(A_j - A_i, 0) = \max(-2, 0) = 0 です。
これらを足し合わせた 3 + 1 + 0 = 4 が答えとなります。


入力例 2

10
5 9 3 0 4 8 7 5 4 0

出力例 2

58

Score: 500 points

Problem Statement

You are given an integer sequence A = (A_1, A_2, \dots, A_N).
Calculate the following expression:

\displaystyle \sum_{i=1}^N \sum_{j=i+1}^N \max(A_j - A_i, 0)


The constraints guarantee that the answer is less than 2^{63}.

Constraints

  • 2 \leq N \leq 4 \times 10^5
  • 0 \leq A_i \leq 10^8
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the value of the expression.


Sample Input 1

3
2 5 3

Sample Output 1

4

For (i, j) = (1, 2), we have \max(A_j - A_i, 0) = \max(3, 0) = 3.
For (i, j) = (1, 3), we have \max(A_j - A_i, 0) = \max(1, 0) = 1.
For (i, j) = (2, 3), we have \max(A_j - A_i, 0) = \max(-2, 0) = 0.
Adding these together gives 3 + 1 + 0 = 4, which is the answer.


Sample Input 2

10
5 9 3 0 4 8 7 5 4 0

Sample Output 2

58