A - AtCoder Reverse Contest

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

英大文字からなる文字列 S に対し、以下の操作を 0 回以上繰り返すとき行うことのできる操作回数の最大値を f(S) とします。

  • 連続する 3 文字が ARC である箇所を 1 つ選び、CRA に置き換える。

例えば S= ARARC に対し f(S)=2 です(ARARC \to ARCRA \to CRARA と操作が出来て、これが最大である)。

0 以上 600 以下の整数 X が与えられます。

f(S)=X を満たす長さ 1 以上 100 以下の英大文字からなる文字列 S を 1 つ求めてください。

ただし、制約下でそのような文字列が必ず存在することが証明できます。

制約

  • 0\le X\le 600
  • 入力される値は整数

入力

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

X

出力

f(S)=X を満たす長さ 1 以上 100 以下の英大文字からなる文字列 S を 1 つ出力せよ。

そのような文字列 S が複数存在する場合、どれを出力しても正答となる。


入力例 1

2

出力例 1

ARARC

S= ARARC は英大文字からなる長さ 5 の文字列で、f(S)=2 を満たします。したがって、ARARC を出力すると正答となります。

この他にも、ARCXXXARC などを出力しても正答となります。


入力例 2

0

出力例 2

ATCODER

入力例 3

3

出力例 3

ARARCDARC

Score : 400 points

Problem Statement

For a string S consisting of uppercase English letters, let f(S) be the maximum number of times the following operation can be performed when it is repeated zero or more times.

  • Choose one occurrence of the substring ARC and replace it with CRA.

For example, for S= ARARC, f(S)=2 (the operations ARARC \to ARCRA \to CRARA can be performed, and this is the maximum).

You are given an integer X between 0 and 600 (inclusive).

Find one string S consisting of uppercase English letters, of length between 1 and 100 (inclusive), satisfying f(S)=X.

It can be proved that such a string always exists under the constraints.

Constraints

  • 0\le X\le 600
  • The input value is an integer.

Input

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

X

Output

Output one string S consisting of uppercase English letters, of length between 1 and 100 (inclusive), satisfying f(S)=X.

If there are multiple such strings S, any of them will be accepted.


Sample Input 1

2

Sample Output 1

ARARC

S= ARARC is a string of length 5 consisting of uppercase English letters, satisfying f(S)=2. Thus, outputting ARARC will be accepted.

Besides this, ARCXXXARC, for example, will also be accepted.


Sample Input 2

0

Sample Output 2

ATCODER

Sample Input 3

3

Sample Output 3

ARARCDARC
B - Halving Subtraction

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

あなたは A に対して以下の操作を 0 回以上好きな回数行うことができます:

  • 正整数 x_0 を選び、x=x_0 とする。その後、i=1,2,\ldots,N の順に以下を行う:
    • A_i から x を引き、x を \displaystyle \left\lfloor \frac x2\right\rfloor で置き換える。

A の要素を全て 0 にすることが可能か判定し、可能な場合は全て 0 にするために必要な操作回数の最小値を求めてください。

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

制約

  • 1\le T\le 10^4
  • 1\le N\le 30
  • 0\le A_i\le 10^9
  • 入力される値は全て整数

入力

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

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

各テストケースは以下の形式で与えられる。

N
A_1 A_2 \ldots A_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ。

各テストケースについて、A の要素を全て 0 にすることが可能ならば必要な操作回数の最小値を、不可能ならば -1 を出力せよ。


入力例 1

4
2
10 4
2
0 1
3
0 0 0
5
16 7 2 1 0

出力例 1

2
-1
0
3

1 番目のテストケースについて考えます。

以下のように操作することで 2 回の操作で A の要素を全て 0 にすることができます:

  • x_0=3 を選ぶ。A=(10,4) から A=(7,3) となる。
  • x_0=7 を選ぶ。A=(7,3) から A=(0,0) となる。

2 回未満の操作で A の要素を全て 0 にすることはできないので、1 行目には 2 を出力してください。

Score : 500 points

Problem Statement

You are given a length-N sequence of non-negative integers A=(A_1,A_2,\dots,A_N).

You can perform the following operation on A any number of times, possibly zero:

  • Choose a positive integer x_0, and set x=x_0. Then, for i=1,2,\ldots,N in this order, do the following:
    • Subtract x from A_i, and replace x with \displaystyle \left\lfloor \frac x2\right\rfloor.

Determine whether it is possible to make all elements of A equal to 0, and if it is possible, find the minimum number of operations required to do so.

You are given T test cases; solve each of them.

Constraints

  • 1\le T\le 10^4
  • 1\le N\le 30
  • 0\le A_i\le 10^9
  • All input values are integers.

Input

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

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

Each test case is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Output the answers for the test cases in order, separated by newlines.

For each test case, if it is possible to make all elements of A equal to 0, output the minimum number of operations required to do so; if it is not possible, output -1.


Sample Input 1

4
2
10 4
2
0 1
3
0 0 0
5
16 7 2 1 0

Sample Output 1

2
-1
0
3

Consider the first test case.

By performing the operations as follows, you can make all elements of A equal to 0 in two operations:

  • Choose x_0=3. A=(10,4) becomes A=(7,3).
  • Choose x_0=7. A=(7,3) becomes A=(0,0).

It is impossible to make all elements of A equal to 0 in fewer than two operations, so output 2 on the first line.

C - Sum of Average 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500 点

問題文

長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A を好きな順序に並べ替えた後の \displaystyle \sum_{i=1}^{N-1}\left\lfloor\frac{A_i+A_{i+1}}2 \right\rfloor としてあり得る最小値を求めてください。

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

制約

  • 1\le T\le 10^5
  • 2\le N\le 2\times 10^5
  • 1\le A_i\le 10^9
  • 全てのテストケースにおける N の総和は 2\times 10^5 以下
  • 入力される値は全て整数

入力

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

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

各テストケースは以下の形式で与えられる。

N
A_1 A_2 \ldots A_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ。


入力例 1

4
4
1 2 4 3
3
4 4 4
5
3 1 4 1 5
10
278117031 167642909 517897721 148434323 567739597 319926999 481642530 659199879 252516557 49913403

出力例 1

5
8
9
2829561210

1 番目のテストケースについて考えます。

たとえば A=(3,2,1,4) に並べ替えることで \displaystyle \sum_{i=1}^{N-1}\left\lfloor\frac{A_i+A_{i+1}}2 \right\rfloor=\left\lfloor\frac52\right\rfloor+\left\lfloor\frac32\right\rfloor+\left\lfloor\frac52\right\rfloor=5 となります。

どのように並べ替えても \displaystyle \sum_{i=1}^{N-1}\left\lfloor\frac{A_i+A_{i+1}}2 \right\rfloor の値は 5 以上となるため、答えは 5 になります。

Score : 500 points

Problem Statement

You are given a length-N sequence of positive integers A=(A_1,A_2,\ldots,A_N).

Find the minimum possible value of \displaystyle \sum_{i=1}^{N-1}\left\lfloor\frac{A_i+A_{i+1}}2 \right\rfloor after rearranging A in any order.

You are given T test cases; solve each of them.

Constraints

  • 1\le T\le 10^5
  • 2\le N\le 2\times 10^5
  • 1\le A_i\le 10^9
  • The sum of N over all test cases is at most 2\times 10^5.
  • All input values are integers.

Input

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

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

Each test case is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Output the answers for the test cases in order, separated by newlines.


Sample Input 1

4
4
1 2 4 3
3
4 4 4
5
3 1 4 1 5
10
278117031 167642909 517897721 148434323 567739597 319926999 481642530 659199879 252516557 49913403

Sample Output 1

5
8
9
2829561210

Consider the first test case.

For example, rearranging A as (3,2,1,4) gives \displaystyle \sum_{i=1}^{N-1}\left\lfloor\frac{A_i+A_{i+1}}2 \right\rfloor=\left\lfloor\frac52\right\rfloor+\left\lfloor\frac32\right\rfloor+\left\lfloor\frac52\right\rfloor=5.

No matter how A is rearranged, the value of \displaystyle \sum_{i=1}^{N-1}\left\lfloor\frac{A_i+A_{i+1}}2 \right\rfloor is at least 5, so the answer is 5.

D - Nim_k ?

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600 点

問題文

K+1 個の石の山があります。i 個目の山には石が A_i 個積まれています。
Alice と Bob はゲームをします。ゲームでは Alice を先攻として交互に手番を行います。 手番では以下の操作をちょうど K 回行います。

  • 石が 1 個以上ある山を選び、そこから石を 1 個以上取り除く。ここで、同じ手番の中で同じ山を複数回選んでも良い。

ゲームは自分の手番で操作を K 回行うことができなかったプレイヤーの負けです。双方が最適に行動した時どちらが勝ちますか?

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 全てのテストケースに対する K の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

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

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

各テストケース \mathrm{case}_t は以下の形式で与えられる。

K
A_1 A_2 \dots A_{K+1}

出力

T 行出力せよ。i 行目には i 番目のテストケースについて、双方が最適に行動した時 Alice が勝つ場合は Alice を、Bob が勝つ場合は Bob を出力せよ。


入力例 1

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

出力例 1

Alice
Bob
Alice

1 番目のテストケースについて考えます。
例えば Alice が最初の手番で以下の操作を行ったとします。

  • 2 番目の山を選び、2 個の石を取り除く。2 番目の山にある石の個数は 0 個になる。
  • 3 番目の山を選び、3 個の石を取り除く。3 番目の山にある石の個数は 0 個になる。

Alice の手番の終了後、石は 1 個しか残っていません。したがって、Bob は操作を 2 回行うことができないため、Alice の勝ちです。

2 番目のテストケースでは、Alice が一方の山から石を取り除くたびに、Bob はもう一方の山から同じ個数の石を取り除くことができます。このように行動することで、Bob は勝つことができます。

Score : 600 points

Problem Statement

There are K+1 piles of stones. The i-th pile has A_i stones.
Alice and Bob play a game. In this game, they alternately take turns with Alice moving first. On each turn, the following operation is performed exactly K times.

  • Choose a pile with one or more stones, and remove one or more stones from it. Here, the same pile may be chosen multiple times within the same turn.

The player who is unable to perform the operation K times on their own turn loses the game. Which player wins when both players play optimally?

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • The sum of K over all test cases is at most 2 \times 10^5.
  • 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

Each test case \mathrm{case}_t is given in the following format:

K
A_1 A_2 \dots A_{K+1}

Output

Output T lines. The i-th line should contain Alice if Alice wins when both players play optimally for the i-th test case, and Bob if Bob wins.


Sample Input 1

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

Sample Output 1

Alice
Bob
Alice

Consider the first test case.
For example, suppose Alice performs the following operations on her first turn.

  • Choose the second pile, and remove two stones from it. The number of stones in the second pile becomes 0.
  • Choose the third pile, and remove three stones from it. The number of stones in the third pile becomes 0.

After her turn ends, only one stone remains. Therefore, Bob cannot perform the operation twice, so Alice wins.

In the second test case, every time Alice removes stones from one pile, Bob can remove the same number of stones from the other pile. By acting in this way, Bob can win.

E - Taka and Hashi

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600 点

問題文

高橋君は有名お笑いコンビ『タカアンドハシ』、すなわちタカとハシに分裂できることで知られています。タカとハシが合体すると元の高橋君に戻ります。
N 頂点 M 辺の連結無向グラフがあり、各辺には 1,2,3 のいずれかのラベルが貼られています。i 番目の辺は頂点 u_i と頂点 v_i を結び、ラベル l_i が貼られています。グラフは自己ループを含みませんが多重辺を含む可能性があります。
各辺は高橋君・タカ・ハシのうち 1 人しか通行できず、ラベル 1,2,3 の辺を通行できるのは、それぞれ高橋君、タカ、ハシのみです。

はじめ、高橋君は頂点 1 にいます。高橋君たちは次の操作を自由な順序で 0 回以上行えます。

  • 高橋君が自身のいる頂点上でタカとハシに分裂する。このとき、タカとハシはともにその頂点にいる。
  • 現在存在する人のうち 1 人が、その人の通行可能な辺を 1 本通って移動する。
  • タカとハシが同じ頂点にいるとき、その頂点で二人が合体して高橋君に戻る。

一連の操作を行った後に高橋君がいることのできる頂点を、全て昇順に列挙してください。

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

制約

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq u_i \lt v_i \leq N
  • 1 \leq l_i \leq 3
  • 入力で与えられるグラフは連結
  • 全てのテストケースに対する N の総和は 2 \times 10^5 以下
  • 全てのテストケースに対する M の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

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

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

各テストケース \mathrm{case}_t は以下の形式で与えられる。

N M
u_1 v_1 l_1
u_2 v_2 l_2
\vdots
u_M v_M l_M

出力

2T 行出力せよ。
各テストケースでは、条件を満たす頂点の番号を昇順に列挙した列を a_1,a_2,\dots,a_k として以下の形式で出力せよ。

k
a_1 a_2 \dots a_k

入力例 1

3
5 5
1 2 1
2 3 2
3 4 2
2 4 3
4 5 1
8 15
1 3 3
1 5 2
1 5 3
1 7 1
2 3 2
2 4 1
2 5 3
2 6 1
2 7 3
2 8 1
3 4 3
3 5 3
4 7 2
5 6 1
6 7 1
10 13
1 3 2
1 4 1
1 6 1
2 7 2
2 8 2
3 6 2
3 9 2
4 5 3
4 7 2
4 9 2
5 8 2
5 10 3
6 7 2

出力例 1

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

1 つ目のテストケースについて考えます。
高橋君は、ラベル 1 の辺を通って頂点 1 から頂点 2 へ移動できます。頂点 2 でタカとハシに分裂した後、タカが 2 \to 3 \to 4、ハシが 2 \to 4 と移動し、頂点 4 で合体することで、高橋君が頂点 4 にいる状態を作ることができます。さらに、高橋君はラベル 1 の辺を通って頂点 5 へ移動できます。
頂点 3 に高橋君がいる状態を作ることはできないため、条件を満たす頂点は 1,2,4,5 です。

Score : 600 points

Problem Statement

Takahashi is known for being able to split into the famous comedy duo "Taka and Hashi", that is, Taka and Hashi. When Taka and Hashi merge, they return to being the original Takahashi.
There is a connected undirected graph with N vertices and M edges, and each edge is labeled with 1, 2, or 3. The i-th edge connects vertices u_i and v_i, and is labeled l_i. The graph does not contain self-loops, but may contain multi-edges.
Each edge can be traversed by only one of Takahashi, Taka, and Hashi, and an edge labeled 1, 2, or 3 can be traversed only by Takahashi, Taka, or Hashi, respectively.

Initially, Takahashi is at vertex 1. Takahashi and the others can perform the following operations in any order, zero or more times.

  • Takahashi splits into Taka and Hashi at the vertex where he currently is. At this point, both Taka and Hashi are at that vertex.
  • One of the currently existing people moves by traversing one edge that they are able to traverse.
  • When Taka and Hashi are at the same vertex, they merge at that vertex and return to being Takahashi.

Enumerate, in ascending order, all vertices where Takahashi can be after performing a sequence of operations.

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq u_i \lt v_i \leq N
  • 1 \leq l_i \leq 3
  • The given graph is connected.
  • The sum of N over all test cases is at most 2 \times 10^5.
  • The sum of M over all test cases is at most 2 \times 10^5.
  • 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

Each test case \mathrm{case}_t is given in the following format:

N M
u_1 v_1 l_1
u_2 v_2 l_2
\vdots
u_M v_M l_M

Output

Output 2T lines.
For each test case, letting a_1,a_2,\dots,a_k be the sequence of numbers of the vertices satisfying the condition, listed in ascending order, output them in the following format:

k
a_1 a_2 \dots a_k

Sample Input 1

3
5 5
1 2 1
2 3 2
3 4 2
2 4 3
4 5 1
8 15
1 3 3
1 5 2
1 5 3
1 7 1
2 3 2
2 4 1
2 5 3
2 6 1
2 7 3
2 8 1
3 4 3
3 5 3
4 7 2
5 6 1
6 7 1
10 13
1 3 2
1 4 1
1 6 1
2 7 2
2 8 2
3 6 2
3 9 2
4 5 3
4 7 2
4 9 2
5 8 2
5 10 3
6 7 2

Sample Output 1

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

Consider the first test case.
Takahashi can move from vertex 1 to vertex 2 by traversing an edge labeled 1. After splitting into Taka and Hashi at vertex 2, Taka moves 2 \to 3 \to 4 and Hashi moves 2 \to 4, and by merging at vertex 4, it is possible to make Takahashi be at vertex 4. Furthermore, Takahashi can move to vertex 5 by traversing an edge labeled 1.
It is impossible to make Takahashi be at vertex 3, so the vertices satisfying the condition are 1,2,4,5.

F - Angst for All Pairs 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600 点

問題文

正整数 N と長さ N の正整数列 C=(C_1,C_2,\ldots,C_N) が与えられます。

カードを 1 枚以上好きな枚数用意し、それぞれのカードの表と裏に 1 以上 N 以下の整数を 1 つずつ書き込むことで、以下の条件を満たすようにしたいです。

  • 1 以上 N 以下の相異なる整数 x,y をどのように選んでも、以下を満たすカードが 1 枚以上存在する。
    • x,y のうちちょうど一方が、そのカードの少なくとも一方の面に書かれている。

1 枚のカードの表と裏に同じ整数を書き込むことも許されます。

ただし、1 枚のカードの表に a を、裏に b を書き込むためにはコストが C_a+C_b かかります。

条件を満たすために必要なコストの総和の最小値を求めてください。

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

制約

  • 1\le T\le 10^5
  • 2\le N\le 2\times 10^5
  • 1\le C_i\le 10^9
  • 全てのテストケースにおける N の総和は 2\times 10^5 以下
  • 入力される値は全て整数

入力

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

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

各テストケースは以下の形式で与えられる。

N
C_1 C_2 \ldots C_N

出力

各テストケースに対する答えを順に改行区切りで出力せよ。


入力例 1

3
3
3 2 5
4
5 6 7 8
7
12 42 21 10 29 33 18

出力例 1

9
23
145

1 番目のテストケースについて考えます。

表に 1 を、裏に 2 を書き込んだカードと、表に 2 を、裏に 2 を書き込んだカードを作ることで条件を満たすことができます。

この際のコストの総和は 3+2+2+2=9 です。コストの総和が 9 未満で条件を満たすことはできないので、1 行目には 9 を出力してください。

Score : 600 points

Problem Statement

You are given a positive integer N and a length-N sequence of positive integers C=(C_1,C_2,\ldots,C_N).

You want to prepare one or more cards, as many as you like, and write one integer between 1 and N (inclusive) on each of the front and back sides of each card, so that the following condition is satisfied.

  • No matter which distinct integers x and y between 1 and N (inclusive) are chosen, there exists at least one card satisfying the following.
    • Exactly one of x and y is written on at least one side of that card.

It is allowed to write the same integer on the front and back of a single card.

Here, the cost of writing a on the front and b on the back of a card is C_a+C_b.

Find the minimum total cost required to satisfy the condition.

You are given T test cases; solve each of them.

Constraints

  • 1\le T\le 10^5
  • 2\le N\le 2\times 10^5
  • 1\le C_i\le 10^9
  • The sum of N over all test cases is at most 2\times 10^5.
  • All input values are integers.

Input

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

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

Each test case is given in the following format:

N
C_1 C_2 \ldots C_N

Output

Output the answers for the test cases in order, separated by newlines.


Sample Input 1

3
3
3 2 5
4
5 6 7 8
7
12 42 21 10 29 33 18

Sample Output 1

9
23
145

Consider the first test case.

By creating a card with 1 written on the front and 2 on the back, and a card with 2 written on the front and 2 on the back, the condition can be satisfied.

The total cost in this case is 3+2+2+2=9. It is impossible to satisfy the condition with a total cost less than 9, so output 9 on the first line.