A - Median?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

整数 a, b, c が与えられます。b がこれらの整数の中央値であるかどうか判定してください。

制約

  • 1 \leq a, b, c \leq 100
  • 入力は全て整数

入力

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

a b c

出力

b が与えられた整数の中央値であるならば Yes、そうでないならば No と出力せよ。


入力例 1

5 3 2

出力例 1

Yes

与えられた整数を小さい順に並べると 2, 3, 5 となり、b はこれらの整数の中央値です。


入力例 2

2 5 3

出力例 2

No

b は与えられた整数の中央値ではありません。


入力例 3

100 100 100

出力例 3

Yes

Score : 100 points

Problem Statement

Given integers a, b, and c, determine if b is the median of these integers.

Constraints

  • 1 \leq a, b, c \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

If b is the median of the given integers, then print Yes; otherwise, print No.


Sample Input 1

5 3 2

Sample Output 1

Yes

The given integers are 2, 3, 5 when sorted in ascending order, of which b is the median.


Sample Input 2

2 5 3

Sample Output 2

No

b is not the median of the given integers.


Sample Input 3

100 100 100

Sample Output 3

Yes
B - Mod While Positive

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

正整数 N, M が与えられます。

M の値が 0 でない間以下の操作を繰り返すとき、操作を行う回数を求めてください。

  • N を M で割った余りを x とする。M の値を x で置き換える。

なお、この操作を有限回行うことにより M = 0 になることが証明できます。

制約

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

入力

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

N M

出力

答えを出力せよ。


入力例 1

8 5

出力例 1

3

はじめ、N = 8, M = 5 です。

8 を 5 で割った余りは 3 であるため、1 回操作を行うと M = 3 となります。

8 を 3 で割った余りは 2 であるため、2 回操作を行うと M = 2 となります。

8 を 2 で割った余りは 0 であるため、3 回操作を行うと M = 0 となります。

したがって、3 を答えとして出力します。


入力例 2

14 6

出力例 2

2

入力例 3

460 33

出力例 3

5

Score : 100 points

Problem Statement

You are given positive integers N and M.

If the following operation is repeated while the value of M is not 0, find the number of operations performed.

  • Let x be the remainder when N is divided by M. Replace the value of M with x.

It can be proved that M becomes 0 after a finite number of operations.

Constraints

  • 1 \leq N, M \leq 1000
  • All input values are integers.

Input

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

N M

Output

Output the answer.


Sample Input 1

8 5

Sample Output 1

3

Initially, N = 8 and M = 5.

The remainder when 8 is divided by 5 is 3, so M = 3 after one operation.

The remainder when 8 is divided by 3 is 2, so M = 2 after two operations.

The remainder when 8 is divided by 2 is 0, so M = 0 after three operations.

Thus, output 3 as the answer.


Sample Input 2

14 6

Sample Output 2

2

Sample Input 3

460 33

Sample Output 3

5
C - Two Languages

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

AtCoder 国の公用語は、高橋語と青木語の 2 つの言語です。

高橋語と青木語は、どちらもその言語に含まれる単語を表記するのに英小文字の一部を使います。 高橋語では長さ N の文字列 S に含まれる文字のみを使い、青木語では長さ M の文字列 T に含まれる文字のみを使います。

AtCoder 国の公用語に含まれる Q 個の単語 w _ 1,w _ 2,\ldots,w _ Q が与えられます。 それぞれの単語について、その単語に含まれる文字からその単語が次のうちどれに該当するか判定してください。

  • 高橋語の単語であることが確定する
  • 青木語の単語であることが確定する
  • どちらともいえない

制約

  • 1\le N\le26
  • 1\le M\le26
  • S は英小文字からなる長さ N の文字列
  • S に含まれる文字は先頭からアルファベット順で昇順に並んでいる
  • S に含まれる文字はすべて異なる
  • T は英小文字からなる長さ M の文字列
  • T に含まれる文字は先頭からアルファベット順で昇順に並んでいる
  • T に含まれる文字はすべて異なる
  • 1\le Q\le100
  • w _ i は英小文字からなる長さ 1 以上 100 以下の文字列 (1\le i\le Q)
  • w _ i は高橋語か青木語のどちらかの単語 (1\le i\le Q)
  • N,M,Q は整数

入力

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

N M
S
T
Q
w _ 1
w _ 2
\vdots
w _ Q

出力

Q 行にわたって出力せよ。 i 行目には、w _ i が高橋語の単語であることが確定するなら Takahashi 、青木語の単語であることが確定するなら Aoki 、どちらとも確定しないなら Unknown と出力せよ。


入力例 1

6 5
ahikst
aikot
5
asahi
okita
kiai
hash
it

出力例 1

Takahashi
Aoki
Unknown
Takahashi
Unknown

例えば、a, s, h, i はすべて高橋語で使われる文字で、h は青木語で使われる文字ではないので asahi は高橋語の単語であることが確定します。 よって、1 行目には Takahashi と出力してください。

i および t はどちらも高橋語でも青木語でも使われる文字なので it は高橋語の単語であるとも青木語の単語であるとも確定しません。 よって、5 行目には Unknown と出力してください。


入力例 2

7 6
ahiknst
ahikos
5
kioki
ohiki
tashi
nishi
kashi

出力例 2

Aoki
Aoki
Takahashi
Takahashi
Unknown

o は高橋語で使われる文字ではないので、はじめ 2 つの単語は青木語の単語であることが確定します。 よって、1 行目と 2 行目には Aoki と出力してください。

t や n は青木語で使われる文字ではないので、続く 2 つの単語は高橋語の単語であることが確定します。 よって、3 行目と 4 行目には Takahashi と出力してください。

はじめ 4 つの単語については、末尾が shi なら高橋語、末尾が ki なら青木語という法則がありますが、k, a, s, h, i はいずれも高橋語と青木語の両方で使われる文字なので kashi がどちらの言語の単語であるかを使われている文字から確定させることはできません。 よって、5 行目には Unknown と出力してください。


入力例 3

13 11
defghiqsvwxyz
acejmoqrtwx
15
qhsqzhd
jcareec
wwqxqew
wxqxwex
jxxrtwa
trtqjxe
sqyggse
xxqwxew
xewwxxw
wwqxwex
xqqxqwq
qxxexxe
teqeroc
eeeqqee
vxdevyy

出力例 3

Takahashi
Aoki
Unknown
Unknown
Aoki
Aoki
Takahashi
Unknown
Unknown
Unknown
Unknown
Unknown
Aoki
Unknown
Takahashi

Score : 200 points

Problem Statement

The AtCoder country has two official languages: Takahashi-go and Aoki-go.

Both Takahashi-go and Aoki-go use some lowercase English letters to write words in those languages. Takahashi-go uses only the characters contained in a string S of length N, and Aoki-go uses only the characters contained in a string T of length M.

You are given Q words w _ 1,w _ 2,\ldots,w _ Q that are in the official languages of the AtCoder country. For each word, determine which of the following applies based on the characters contained in that word:

  • It is confirmed to be a word in Takahashi-go
  • It is confirmed to be a word in Aoki-go
  • Neither can be determined

Constraints

  • 1\le N\le26
  • 1\le M\le26
  • S is a string of length N consisting of lowercase English letters.
  • The characters in S are arranged in alphabetical order.
  • All characters in S are distinct.
  • T is a string of length M consisting of lowercase English letters.
  • The characters in T are arranged in alphabetical order.
  • All characters in T are distinct.
  • 1\le Q\le100
  • w _ i is a string of length at least 1 and at most 100 consisting of lowercase English letters. (1\le i\le Q)
  • w _ i is a word in Takahashi-go or Aoki-go. (1\le i\le Q)
  • N,M,Q are integers.

Input

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

N M
S
T
Q
w _ 1
w _ 2
\vdots
w _ Q

Output

Print Q lines. The i-th line should contain Takahashi if it is confirmed that w _ i is a word in Takahashi-go, Aoki if it is confirmed to be a word in Aoki-go, and Unknown if neither can be determined.


Sample Input 1

6 5
ahikst
aikot
5
asahi
okita
kiai
hash
it

Sample Output 1

Takahashi
Aoki
Unknown
Takahashi
Unknown

For example, all of a, s, h, i are used in Takahashi-go, and h is not used in Aoki-go, so it is confirmed that asahi is a word in Takahashi-go. Thus, print Takahashi on the first line.

Both i and t are used in both Takahashi-go and Aoki-go, so it cannot be determined whether it is a word in Takahashi-go or Aoki-go. Thus, print Unknown on the fifth line.


Sample Input 2

7 6
ahiknst
ahikos
5
kioki
ohiki
tashi
nishi
kashi

Sample Output 2

Aoki
Aoki
Takahashi
Takahashi
Unknown

o is not used in Takahashi-go, so the first two words are confirmed to be words in Aoki-go. Thus, print Aoki on the first and second lines.

t and n are not used in Aoki-go, so the following two words are confirmed to be words in Takahashi-go. Thus, print Takahashi on the third and fourth lines.

For the first four words, there is a rule that words ending in shi are Takahashi-go, and words ending in ki are Aoki-go. However, all of k, a, s, h, i are used in both Takahashi-go and Aoki-go, so it is impossible to determine which language the word kashi belongs to based on the characters used. Thus, print Unknown on the fifth line.


Sample Input 3

13 11
defghiqsvwxyz
acejmoqrtwx
15
qhsqzhd
jcareec
wwqxqew
wxqxwex
jxxrtwa
trtqjxe
sqyggse
xxqwxew
xewwxxw
wwqxwex
xqqxqwq
qxxexxe
teqeroc
eeeqqee
vxdevyy

Sample Output 3

Takahashi
Aoki
Unknown
Unknown
Aoki
Aoki
Takahashi
Unknown
Unknown
Unknown
Unknown
Unknown
Aoki
Unknown
Takahashi
D - Better Students Are Needed!

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

入学試験の受験者が N 人います。
試験の結果、 i 番の受験生は数学で A_i 点、英語で B_i 点を取りました。

試験の合格者は次のように決められます。

  1. 数学の点が高い方から X 人を合格とする。
  2. 次に、この時点でまだ合格となっていない受験者のうち、英語の点が高い方から Y 人を合格とする。
  3. 次に、この時点でまだ合格となっていない受験者のうち、数学と英語の合計点が高い方から Z 人を合格とする。
  4. ここまでで合格となっていない受験者は、不合格とする。

ただし、 1. から 3. までのどの段階についても、同点であった場合は受験生の番号の小さい方を優先します。入出力例も参照してください。

以上の手順で合格者を決める時、合格となった受験生の番号を小さい方から順に改行区切りで出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 1000
  • 0 \le X,Y,Z \le N
  • 1 \le X+Y+Z \le N
  • 0 \le A_i,B_i \le 100

入力

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

N X Y Z
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

合格となった受験生の番号を小さい方から順に改行区切りで出力せよ。


入力例 1

6 1 0 2
80 60 80 60 70 70
40 20 50 90 90 80

出力例 1

1
4
5
  • まず、数学の点が高い方から 1 人が合格となります。
    • 数学の最高点は 80 点で 1 番の受験生と 3 番の受験生が並んでいますが、受験生の番号が小さい方が優先され 1 番の受験生が合格となります。
  • 次に、まだ合格となっていない受験者のうち、英語の点が高い方から 0 人が合格となります。
    • 明らかに、ここで合格者が増えることはありません。
  • 次に、まだ合格となっていない受験者のうち、数学と英語の合計点が高い方から 2 人が合格となります。
    • まず、まだ合格となっていない受験者の中で、合計点が 160 点と最も高い 5 番の受験生が合格となります。
    • 次に、まだ合格となっていない受験者の中で、合計点が 150 点の 4 番の受験生と 6 番の受験生が並んでいます。受験生の番号の小さい方が優先され、 4 番の受験生が合格となります。

以上より、合格となる受験生の番号は 1,4,5 なので、小さい方から出力してください。


入力例 2

5 2 1 2
0 100 0 100 0
0 0 100 100 0

出力例 2

1
2
3
4
5

全員が合格となることもあります。


入力例 3

15 4 3 2
30 65 20 95 100 45 70 85 20 35 95 50 40 15 85
0 25 45 35 65 70 80 90 40 55 20 20 45 75 100

出力例 3

2
4
5
6
7
8
11
14
15

Score : 200 points

Problem Statement

N examinees took an entrance exam.
The examinee numbered i scored A_i points in math and B_i points in English.

The admissions are determined as follows.

  1. X examinees with the highest math scores are admitted.
  2. Then, among the examinees who are not admitted yet, Y examinees with the highest English scores are admitted.
  3. Then, among the examinees who are not admitted yet, Z examinees with the highest total scores in math and English are admitted.
  4. Those examinees who are not admitted yet are rejected.

Here, in each of the steps 1. to 3., ties are broken by examinees' numbers: an examinee with the smaller examinee's number is prioritized. See also Sample Input and Output.

Print the examinees' numbers of the admitted examinees determined by the steps above in ascending order, separated by newlines.

Constraints

  • All values in input are integers.
  • 1 \le N \le 1000
  • 0 \le X,Y,Z \le N
  • 1 \le X+Y+Z \le N
  • 0 \le A_i,B_i \le 100

Input

Input is given from Standard Input in the following format:

N X Y Z
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print the examinees' number of the admitted examinees in ascending order, separated by newlines.


Sample Input 1

6 1 0 2
80 60 80 60 70 70
40 20 50 90 90 80

Sample Output 1

1
4
5
  • First, 1 examinee with the highest math score is admitted.
    • Examinee 1 is tied with Examinee 3, scoring the highest 80 points in math, and the tie is broken by the examinees' numbers, so Examinee 1 is admitted.
  • Then, among the examinees who are not admitted yet, 0 examinees with the highest English scores are admitted.
    • Obviously, it does not affect the admissions.
  • Then, among the examinees who are not admitted yet, 2 examinees with the highest total scores in math and English are admitted.
    • First, among the examinees who are not admitted yet, Examinee 5 is admitted, scoring the highest total score of 160 points.
    • Next, among the examinees who are not admitted yet, Examinee 4 is tied with Examinee 6, scoring a total score of 150 points. The tie is broken by the examinees' numbers, and Examinee 4 is admitted.

Therefore, the examinees' numbers of the admitted examinees are 1, 4, and 5. Print them in ascending order.


Sample Input 2

5 2 1 2
0 100 0 100 0
0 0 100 100 0

Sample Output 2

1
2
3
4
5

All examinees may be admitted.


Sample Input 3

15 4 3 2
30 65 20 95 100 45 70 85 20 35 95 50 40 15 85
0 25 45 35 65 70 80 90 40 55 20 20 45 75 100

Sample Output 3

2
4
5
6
7
8
11
14
15
E - Find it!

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

N 頂点 N 辺の有向グラフがあります。
i 番目の辺は頂点 i から 頂点 A_i に伸びます。 ( i \neq A_i であることは制約より保証されます )
同一頂点を複数回含まない有向閉路をひとつ求めてください。
なお、この問題の制約下で答えが存在することが示せます。

注釈

この問題では、有向閉路とは以下の条件を全て満たす頂点の列 B=(B_1,B_2,\dots,B_M) であるとします。

  • M \ge 2
  • B_i から B_{i+1} に辺が伸びている。 ( 1 \le i \le M-1 )
  • B_M から B_1 に辺が伸びている。
  • i \neq j ならば B_i \neq B_j

制約

  • 入力は全て整数
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

入力

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

N
A_1 A_2 \dots A_N

出力

以下の形式で出力せよ。

M
B_1 B_2 \dots B_M

M は出力する有向閉路の頂点数であり、 B_i は有向閉路の i 番目の頂点である。
出力は以下の条件を満たす必要がある。

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

答えとして考えられるものが複数ある場合、どれを出力しても正解となる。


入力例 1

7
6 7 2 1 3 4 5

出力例 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。

他の正解となる出力の例は以下の通りです。

4
2 7 5 3
3
4 1 6

グラフが連結であるとは限らないことに注意してください。


入力例 2

2
2 1

出力例 2

2
1 2

1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方存在するケースです。
この場合、 1 \rightarrow 2 \rightarrow 1 は確かに有向閉路になっています。

この入力に対応するグラフは以下の通りです。
図中 1 \leftrightarrow 2 で 1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方あることを表現しています。


入力例 3

8
3 7 4 7 3 3 8 2

出力例 3

3
2 7 8

この入力に対応するグラフは以下の通りです。

Score : 350 points

Problem Statement

There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex A_i. (The constraints guarantee that i \neq A_i.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.

Notes

The sequence of vertices B = (B_1, B_2, \dots, B_M) is called a directed cycle when all of the following conditions are satisfied:

  • M \geq 2
  • The edge from vertex B_i to vertex B_{i+1} exists. (1 \leq i \leq M-1)
  • The edge from vertex B_M to vertex B_1 exists.
  • If i \neq j, then B_i \neq B_j.

Constraints

  • All input values are integers.
  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i

Input

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

N
A_1 A_2 \dots A_N

Output

Print a solution in the following format:

M
B_1 B_2 \dots B_M

M is the number of vertices, and B_i is the i-th vertex in the directed cycle.
The following conditions must be satisfied:

  • 2 \le M
  • B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
  • B_{1} = A_{B_M}
  • B_i \neq B_j ( i \neq j )

If multiple solutions exist, any of them will be accepted.


Sample Input 1

7
6 7 2 1 3 4 5

Sample Output 1

4
7 5 3 2

7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 is indeed a directed cycle.

Here is the graph corresponding to this input:

Here are other acceptable outputs:

4
2 7 5 3
3
4 1 6

Note that the graph may not be connected.


Sample Input 2

2
2 1

Sample Output 2

2
1 2

This case contains both of the edges 1 \rightarrow 2 and 2 \rightarrow 1.
In this case, 1 \rightarrow 2 \rightarrow 1 is indeed a directed cycle.

Here is the graph corresponding to this input, where 1 \leftrightarrow 2 represents the existence of both 1 \rightarrow 2 and 2 \rightarrow 1:


Sample Input 3

8
3 7 4 7 3 3 8 2

Sample Output 3

3
2 7 8

Here is the graph corresponding to this input:

F - Chinese Restaurant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

回転テーブルの周りに人 0、人 1、\ldots、人 N-1 がこの順番で反時計回りに等間隔で並んでいます。また、人 i の目の前には料理 p_i が置かれています。
あなたは次の操作を 0 回以上何度でも行うことが出来ます。

  • 回転テーブルを反時計回りに 1 周の \frac{1}{N} だけ回す。これによって、(この操作の直前に)人 i の目の前にあった料理は人 (i+1) \bmod N の目の前に移動する。

操作を完了させた後において、人 i は料理 i が人 (i-1) \bmod N、人 i、人 (i+1) \bmod N のいずれかの目の前に置かれていると喜びます。
喜ぶ人数の最大値を求めてください。

a \bmod m とは 整数 a と正整数 m に対し、a \bmod m は a-x が m の倍数となるような 0 以上 m 未満の整数 x を表します。(このような x は一意に定まることが証明できます)

制約

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • i \neq j ならば p_i \neq p_j
  • 入力はすべて整数

入力

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

N
p_0 \ldots p_{N-1}

出力

答えを出力せよ。


入力例 1

4
1 2 0 3

出力例 1

4

操作を 1 回行うと下の画像のようになります。

この時、4 人が喜ぶことを以下のように確かめられます。

  • 人 0 は料理 0 が人 3\ (=(0-1) \bmod 4) の目の前に置かれているので喜びます。
  • 人 1 は料理 1 が人 1\ (=1) の目の前に置かれているので喜びます。
  • 人 2 は料理 2 が人 2\ (=2) の目の前に置かれているので喜びます。
  • 人 3 は料理 3 が人 0\ (=(3+1) \bmod 4) の目の前に置かれているので喜びます。

5 人以上が喜ぶことは無いため、答えは 4 です。


入力例 2

3
0 1 2

出力例 2

3

入力例 3

10
3 9 6 1 7 2 8 0 5 4

出力例 3

5

Score : 300 points

Problem Statement

Person 0, Person 1, \ldots, and Person (N-1) are sitting around a turntable in their counterclockwise order, evenly spaced. Dish p_i is in front of Person i on the table.
You may perform the following operation 0 or more times:

  • Rotate the turntable by one N-th of a counterclockwise turn. As a result, the dish that was in front of Person i right before the rotation is now in front of Person (i+1) \bmod N.

When you are finished, Person i is happy if Dish i is in front of Person (i-1) \bmod N, Person i, or Person (i+1) \bmod N.
Find the maximum possible number of happy people.

What is a \bmod m? For an integer a and a positive integer m, a \bmod m denotes the integer x between 0 and (m-1) (inclusive) such that (a-x) is a multiple of m. (It can be proved that such x is unique.)

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • p_i \neq p_j if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_0 \ldots p_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2 0 3

Sample Output 1

4

The figure below shows the table after one operation.

Here, there are four happy people:

  • Person 0 is happy because Dish 0 is in front of Person 3\ (=(0-1) \bmod 4);
  • Person 1 is happy because Dish 1 is in front of Person 1\ (=1);
  • Person 2 is happy because Dish 2 is in front of Person 2\ (=2);
  • Person 3 is happy because Dish 3 is in front of Person 0\ (=(3+1) \bmod 4).

There cannot be five or more happy people, so the answer is 4.


Sample Input 2

3
0 1 2

Sample Output 2

3

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

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_i は 0 または 1

入出力

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

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

N K

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

? x_1 x_2 \dots x_K

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

T

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

  • T が 0 である場合は A_{x_1} + A_{x_2} + \dots + A_{x_K} は偶数であることを、
  • T が 1 である場合は 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 - Takahashi and Animals

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

高橋君と N 匹の動物がいます。 N 匹の動物はそれぞれ動物 1 、動物 2 、\ldots 、動物 N と呼ばれます。

高橋君は下記の N 種類の行動をそれぞれ好きな回数だけ( 0 回でも良い)行います。

  • A_1 円払い、動物 1 と動物 2 に餌をあげる。
  • A_2 円払い、動物 2 と動物 3 に餌をあげる。
  • A_3 円払い、動物 3 と動物 4 に餌をあげる。
  • \cdots
  • A_i 円払い、動物 i と動物 (i+1) に餌をあげる。
  • \cdots
  • A_{N-2} 円払い、動物 (N-2) と動物 (N-1) に餌をあげる。
  • A_{N-1} 円払い、動物 (N-1) と動物 N に餌をあげる。
  • A_N 円払い、動物 N と動物 1 に餌をあげる。

上記の N 種類目の行動では、「動物 N と動物 1 に」餌をあげることに注意してください。

すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力してください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力せよ。


入力例 1

5
2 5 3 2 5

出力例 1

7

高橋君が 1 種類目、3 種類目、4 種類目の行動をそれぞれ 1 回ずつ行うと、 動物 1 に 1 回、動物 2 に 1 回、動物 3 に 1 回、動物 4 に 2 回、動物 5 に 1 回餌をあげることになり、すべての動物にそれぞれ 1 回以上餌をあげることができます。 このときにかかる費用の合計は A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 円であり、これが考えられる最小値です。


入力例 2

20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62

出力例 2

426

Score : 500 points

Problem Statement

Takahashi is with N animals. The N animals are called Animal 1, Animal 2, \ldots, Animal N.

Takahashi will perform the following N kinds of action. Each action can be performed any number of (possibly zero) times.

  • Pay A_1 yen (the currency in Japan) to feed Animals 1 and 2.
  • Pay A_2 yen to feed Animals 2 and 3.
  • Pay A_3 yen to feed Animals 3 and 4.
  • \cdots
  • Pay A_i yen to feed Animals i and (i+1).
  • \cdots
  • Pay A_{N-2} yen to feed Animals (N-2) and (N-1).
  • Pay A_{N-1} yen to feed Animals (N-1) and N.
  • Pay A_N yen to feed Animals N and 1.

Note that the N-th action above feeds "Animals N and 1."

Print the minimum possible total cost to feed every animal at least once.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the minimum possible total cost to feed every animal at least once.


Sample Input 1

5
2 5 3 2 5

Sample Output 1

7

If Takahashi performs the 1-st, 3-rd, and 4-th actions once each, Animals 1, 2, 3, 4, and 5 are fed once, once, once, twice, once, respectively, so every animal is fed at least once. The total cost to do so is A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 yen, which is the minimum possible.


Sample Input 2

20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62

Sample Output 2

426
I - Teleporter and Closed off

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 個の都市があり、都市 1, 都市 2, \ldots, 都市 N と番号づけられています。
いくつかの異なる都市の間は一方通行のテレポーターによって移動できます。 都市 i (1\leq i\leq N) からテレポーターによって直接移動できる都市は 0 と 1 からなる長さ M の文字列 S_i によって表されます。具体的には、1\leq j\leq N に対して、

  • 1\leq j-i\leq M かつ S_i の (j-i) 文字目が 1 ならば、都市 i から都市 j に直接移動できる。
  • そうでない時、都市 i から都市 j へは直接移動できない。

k=2,3,\ldots, N-1 に対して次の問題を解いてください。

テレポータを繰り返し使用することによって、都市 k を通らずに都市 1 から 都市 N へ移動できるか判定し、 できるならばそのために必要なテレポーターの使用回数の最小値を、 できないならば -1 を出力せよ。

制約

  • 3 \leq N \leq 10^5
  • 1\leq M\leq 10
  • M<N
  • S_i は 0 と 1 のみからなる長さ M の文字列
  • i+j>N ならば S_i の j 文字目は 0
  • N,M は整数

入力

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

N M
S_1
S_2
\vdots
S_N

出力

N-2 個の整数を空白区切りで一行に出力せよ。 i (1\leq i\leq N-2) 番目には、k=i+1 に対する問題の答えを出力せよ。


入力例 1

5 2
11
01
11
10
00

出力例 1

2 3 2

テレポータによって各都市からはそれぞれ以下の都市へ直接移動する事ができます。

  • 都市 1 からは都市 2,3 へ移動できる。
  • 都市 2 からは都市 4 へ移動できる。
  • 都市 3 からは都市 4,5 へ移動できる。
  • 都市 4 からは都市 5 へ移動できる。
  • 都市 5 から移動できる都市は存在しない。

よって、都市 1 から都市 5 へ移動する方法は、

  • 経路 1 : 都市 1 \to 都市 2 \to 都市 4 \to 都市 5
  • 経路 2 : 都市 1 \to 都市 3 \to 都市 4 \to 都市 5
  • 経路 3 : 都市 1 \to 都市 3 \to 都市 5

の 3 つがあり、

  • 都市 2 を通らない経路は経路 2, 経路 3 の 2つであり、そのうちテレポーターの使用回数が最小となるのは経路 3 で、この時 2 回使用する。
  • 都市 3 を通らない経路は経路 1 のみであり、この時テレポーターは 3 回使用する。
  • 都市 4 を通らない経路は経路 3 のみであり、この時テレポーターは 2 回使用する。

となります。よって、2,3,2 をこの順に空白区切りで出力します。


入力例 2

6 3
101
001
101
000
100
000

出力例 2

-1 3 3 -1

都市 1 から都市 6 へ移動する方法は、都市 1 \to 都市 2 \to 都市 5 \to 都市 6 のみであるため、
k=2,5 の場合には都市 k を通らずに都市 1 から都市 6 へ移動する方法は存在せず、
k=3,4 の場合には上の方法が条件をみたし、テレポーターを 3 回使用します。

よって、-1,3,3,-1 をこの順に空白区切りで出力します。

テレポーターは一方通行であるため、 都市 3 から都市 4 へはテレポーターによって移動できますが、 都市 4 から都市 3 へは移動できず、
都市 1 \to 都市 4 \to 都市 3 \to 都市 6 のような移動はできない事に注意してください。

Score : 500 points

Problem Statement

There are N cities numbered city 1, city 2, \ldots, and city N.
There are also one-way teleporters that send you to different cities. Whether a teleporter can send you directly from city i (1\leq i\leq N) to another is represented by a length-M string S_i consisting of 0 and 1. Specifically, for 1\leq j\leq N,

  • if 1\leq j-i\leq M and the (j-i)-th character of S_i is 1, then a teleporter can send you directly from city i to city j;
  • otherwise, it cannot send you directly from city i to city j.

Solve the following problem for k=2,3,\ldots, N-1:

Can you travel from city 1 to city N without visiting city k by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print -1.

Constraints

  • 3 \leq N \leq 10^5
  • 1\leq M\leq 10
  • M<N
  • S_i is a string of length M consisting of 0 and 1.
  • If i+j>N, then the j-th character of S_i is 0.
  • N and M are integers.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print (N-2) integers, separated by spaces, in a single line. The i-th (1\leq i\leq N-2) integer should be the answer to the problem for k=i+1.


Sample Input 1

5 2
11
01
11
10
00

Sample Output 1

2 3 2

A teleporter sends you

  • from city 1 to cities 2 and 3;
  • from city 2 to city 4;
  • from city 3 to cities 4 and 5;
  • from city 4 to city 5; and
  • from city 5 to nowhere.

Therefore, there are three paths to travel from city 1 to city 5:

  • path 1 : city 1 \to city 2 \to city 4 \to city 5;
  • path 2 : city 1 \to city 3 \to city 4 \to city 5; and
  • path 3 : city 1 \to city 3 \to city 5.

Among these paths,

  • two paths, path 2 and path 3, do not visit city 2. Among them, path 3 requires the minimum number of teleporter uses (twice).
  • Path 1 is the only path without city 3. It requires using a teleporter three times.
  • Path 3 is the only path without city 4. It requires using a teleporter twice.

Thus, 2, 3, and 2, separated by spaces, should be printed.


Sample Input 2

6 3
101
001
101
000
100
000

Sample Output 2

-1 3 3 -1

The only path from city 1 to city 6 is city 1 \to city 2 \to city 5 \to city 6.
For k=2,5, there is no way to travel from city 1 to city 6 without visiting city k.
For k=3,4, the path above satisfies the condition; it requires using a teleporter three times.

Thus, -1, 3, 3, and -1, separated by spaces, should be printed.

Note that a teleporter is one-way; a teleporter can send you from city 3 to city 4, but not from city 4 to city 3,
so the following path, for example, is invalid: city 1 \to city 4 \to city 3 \to city 6.