E - Ameba

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

あなたはアメーバの観察記録をつけました。

最初 1 匹のアメーバがおり、番号は 1 です。

観察記録は時系列順に N 個あり、i 番目の観察記録は「番号 A_i のアメーバが分裂して消滅し、新たに 2 匹のアメーバが生まれ、それらにそれぞれ 2i,2i+1 と番号をつけた」というものです。
このとき、アメーバ A_i を アメーバ 2i,2i+1 の親と呼びます。

k=1,\ldots,2N+1 について、アメーバ k から何代親を遡るとアメーバ 1 になるか求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 観察記録は矛盾していない。すなわち
    • 1\leq A_i \leq 2i-1
    • A_i は相異なる整数

入力

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

N
A_1 A_2 \ldots A_N

出力

2N+1 行出力せよ。k 行目にはアメーバ k から何代親を遡るとアメーバ 1 になるかを出力せよ。


入力例 1

2
1 2

出力例 1

0
1
1
2
2

アメーバ 1 からアメーバ 2,3 が生まれ、アメーバ 2 からアメーバ 4,5 が生まれました。

  • アメーバ 10 代遡るとアメーバ 1 になります。
  • アメーバ 21 代遡るとアメーバ 1 になります。
  • アメーバ 31 代遡るとアメーバ 1 になります。
  • アメーバ 41 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。
  • アメーバ 51 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。

入力例 2

4
1 3 5 2

出力例 2

0
1
1
2
2
3
3
2
2

Score : 300 points

Problem Statement

You observed amoebae and kept some records.

Initially, there was one amoeba, numbered 1.

You made N records. According to the i-th record, the amoeba numbered A_i disappeared by dividing itself into two new amoebae, which were then numbered 2i and 2i+1.
Here, amoeba A_i is said to be the parent of amoebae 2i and 2i+1.

For each k=1,\ldots,2N+1, how many generations away is amoeba k from amoeba 1?

Constraints

  • 1 \leq N \leq 2\times 10^5
  • The records are consistent. That is:
    • 1\leq A_i \leq 2i-1.
    • A_i are distinct integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print 2N+1 lines. The k-th line should contain the generation distance between amoeba 1 and amoeba k.


Sample Input 1

2
1 2

Sample Output 1

0
1
1
2
2

From amoeba 1, amoebae 2 and 3 were born. From amoeba 2, amoebae 4 and 5 were born.

  • Amoeba 1 is zero generations away from amoeba 1.
  • Amoeba 2 is one generation away from amoeba 1.
  • Amoeba 3 is one generation away from amoeba 1.
  • Amoeba 4 is one generation away from amoeba 2, and two generations away from amoeba 1.
  • Amoeba 5 is one generation away from amoeba 2, and two generations away from amoeba 1.

Sample Input 2

4
1 3 5 2

Sample Output 2

0
1
1
2
2
3
3
2
2
F - The Kth Time Query

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の数列 A = (a_1, a_2, \dots, a_N) があります。
以下で説明される Q 個のクエリに答えてください。

  • クエリ i : 整数の組 (x_i, k_i) が与えられます。A の要素を a_1, a_2, \dots と前から順に見ていったときに、数 x_ik_i 回目に登場するのは A の前から何番目の要素を見たときかを出力してください。
    ただし条件を満たす要素が存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
  • 1 \leq k_i \leq N (1 \leq i \leq Q)
  • 入力はすべて整数である。

入力

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

N Q
a_1 a_2 \dots a_N
x_1 k_1
x_2 k_2
\vdots
x_Q k_Q

出力

Q 行出力せよ。i 行目ではクエリ i に対する答えを出力せよ。


入力例 1

6 8
1 1 2 3 1 2
1 1
1 2
1 3
1 4
2 1
2 2
2 3
4 1

出力例 1

1
2
5
-1
3
6
-1
-1

A の中で 1a_1, a_2, a_5 に登場します。よって、クエリ 1 からクエリ 4 の答えは順に 1, 2, 5, -1 となります。


入力例 2

3 2
0 1000000000 999999999
1000000000 1
123456789 1

出力例 2

2
-1

Score : 300 points

Problem Statement

We have a sequence of N numbers: A = (a_1, a_2, \dots, a_N).
Process the Q queries explained below.

  • Query i: You are given a pair of integers (x_i, k_i). Let us look at the elements of A one by one from the beginning: a_1, a_2, \dots Which element will be the k_i-th occurrence of the number x_i?
    Print the index of that element, or -1 if there is no such element.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
  • 1 \leq k_i \leq N (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 a_2 \dots a_N
x_1 k_1
x_2 k_2
\vdots
x_Q k_Q

Output

Print Q lines. The i-th line should contain the answer to Query i.


Sample Input 1

6 8
1 1 2 3 1 2
1 1
1 2
1 3
1 4
2 1
2 2
2 3
4 1

Sample Output 1

1
2
5
-1
3
6
-1
-1

1 occurs in A at a_1, a_2, a_5. Thus, the answers to Query 1 through 4 are 1, 2, 5, -1 in this order.


Sample Input 2

3 2
0 1000000000 999999999
1000000000 1
123456789 1

Sample Output 2

2
-1
G - Prime Sum Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君と青木君が次のようなゲームをします。

  • まず、高橋君が A 以上 B 以下の好きな整数を選び、青木君に伝える
  • 次に、青木君が C 以上 D 以下の好きな整数を選ぶ
  • 二人の選んだ整数の和が素数なら青木君の勝ち、そうでなければ高橋君の勝ち

二人が最適な戦略を取るとき、どちらが勝ちますか?

制約

  • 1 \leq A \leq B \leq 100
  • 1 \leq C \leq D \leq 100
  • 入力に含まれる値は全て整数である

入力

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

A B C D

出力

二人が最適な戦略をとったとき、高橋君が勝つなら Takahashi、青木君が勝つなら Aoki を出力せよ。


入力例 1

2 3 3 4

出力例 1

Aoki

例えば高橋君が 2 を選んだときは、青木君は 3 を選ぶことで、和を素数である 5 にすることができます。


入力例 2

1 100 50 60

出力例 2

Takahashi

最適な戦略を取ると高橋君が必ず勝ちます。


入力例 3

3 14 1 5

出力例 3

Aoki

Score : 400 points

Problem Statement

Takahashi and Aoki are playing a game.

  • First, Takahashi chooses an integer between A and B (inclusive) and tells it to Aoki.
  • Next, Aoki chooses an integer between C and D (inclusive).
  • If the sum of these two integers is a prime, then Aoki wins; otherwise, Takahashi wins.

When the two players play optimally, which player will win?

Constraints

  • 1 \leq A \leq B \leq 100
  • 1 \leq C \leq D \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

If Takahashi wins when the two players play optimally, print Takahashi; if Aoki wins, print Aoki.


Sample Input 1

2 3 3 4

Sample Output 1

Aoki

For example, if Takahashi chooses 2, Aoki can choose 3 to make the sum 5, which is a prime.


Sample Input 2

1 100 50 60

Sample Output 2

Takahashi

If they play optimally, Takahashi always wins.


Sample Input 3

3 14 1 5

Sample Output 3

Aoki
H - Sandwiches

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。以下の条件を全て満たす正整数組 (i,j,k) の個数を求めてください。

  • 1\leq i < j < k\leq N
  • A_i = A_k
  • A_i \neq A_j

制約

  • 3\leq N\leq 3\times 10^5
  • 1\leq A_i \leq N
  • 入力される数値は全て整数

入力

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

N 
A_1 A_2 \ldots A_N

出力

答えを整数として出力せよ。


入力例 1

5
1 2 1 3 2

出力例 1

3

条件を全て満たす正整数組 (i,j,k) は以下の 3 個です。

  • (i,j,k)=(1,2,3)
  • (i,j,k)=(2,3,5)
  • (i,j,k)=(2,4,5)

入力例 2

7
1 2 3 4 5 6 7

出力例 2

0

条件を全て満たす正整数組 (i,j,k) が存在しない場合もあります。


入力例 3

13
9 7 11 7 3 8 1 13 11 11 11 6 13

出力例 3

20

Score : 450 points

Problem Statement

You are given a sequence of positive integers of length N: A=(A_1,A_2,\ldots,A_N). Find the number of triples of positive integers (i,j,k) that satisfy all of the following conditions:

  • 1\leq i < j < k\leq N,
  • A_i = A_k,
  • A_i \neq A_j.

Constraints

  • 3\leq N\leq 3\times 10^5
  • 1\leq A_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

Output

Print the answer as an integer.


Sample Input 1

5
1 2 1 3 2

Sample Output 1

3

The following three triples of positive integers (i,j,k) satisfy the conditions:

  • (i,j,k)=(1,2,3)
  • (i,j,k)=(2,3,5)
  • (i,j,k)=(2,4,5)

Sample Input 2

7
1 2 3 4 5 6 7

Sample Output 2

0

There may be no triples of positive integers (i,j,k) that satisfy the conditions.


Sample Input 3

13
9 7 11 7 3 8 1 13 11 11 11 6 13

Sample Output 3

20
I - Simple Operations on Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N2 つの整数列 A = (A_1, A_2, \ldots, A_N) および B = (B_1, B_2 \ldots, B_N) が与えられます。

整数列 A に対して、「下記の 2 つの操作のうちどちらかを行う」ということを好きな回数( 0 回でもよい)繰り返すことができます。

  • 1 \leq i \leq N を満たす整数 i を選び、A_i の値を 1 増やすか 1 減らす。この操作には X 円の費用がかかる。
  • 1 \leq i \leq N-1 を満たす整数 i を選び、A_i の値と A_{i+1} の値を入れ替える。この操作には Y 円の費用がかかる。

上記の繰り返しによって整数列 A を整数列 B に一致させるためにかかる合計費用としてあり得る最小値を出力してください。

制約

  • 2 \leq N \leq 18
  • 1 \leq X \leq 10^8
  • 1 \leq Y \leq 10^{16}
  • 1 \leq A_i, B_i \leq 10^8
  • 入力はすべて整数

入力

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

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

AB に一致させるためにかかる合計費用としてあり得る最小値を求めよ。


入力例 1

4 3 5
4 2 5 2
6 4 2 1

出力例 1

16

はじめ、A = (4, 2, 5, 2) です。
下記の通りに操作を行うと、AB に一致させることができます。

  • X = 3 円払い、A_3 の値を 1 増やす。その結果 A = (4, 2, 6, 2) となる。
  • Y = 5 円払い、A_2 の値と A_3 の値を入れ替える。その結果 A = (4, 6, 2, 2) となる。
  • Y = 5 円払い、A_1 の値と A_2 の値を入れ替える。その結果 A = (6, 4, 2, 2) となる。
  • X = 3 円払い、A_4 の値を 1 減らす。その結果 A = (6, 4, 2, 1) = B となる。

上記の操作にかかる費用の合計は 3+5+5+3 = 16 円であり、これが最小となります。


入力例 2

5 12345 6789
1 2 3 4 5
1 2 3 4 5

出力例 2

0

AB は初めから一致しているため、一度も操作を行う必要がありません。


入力例 3

18 20719114 5117250357733867
10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712
14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077

出力例 3

13104119429316474

入力や出力が 32 bit 整数型に収まらないことがあることに注意してください。

Score : 500 points

Problem Statement

Given are two sequences of N integers each: A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2 \ldots, B_N).

On the sequence A, you can do the two operations below any number of times (possibly zero) in any order.

  • Choose an integer i satisfying 1 \leq i \leq N, and increase or decrease A_i by 1, for the cost of X yen (Japanese currency).
  • Choose an integer i satisfying 1 \leq i \leq N-1, and swap the values of A_i and A_{i+1}, for the cost of Y yen.

Print the minimum total cost needed to make the sequence A equal the sequence B by repeating the above.

Constraints

  • 2 \leq N \leq 18
  • 1 \leq X \leq 10^8
  • 1 \leq Y \leq 10^{16}
  • 1 \leq A_i, B_i \leq 10^8
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print the minimum total cost needed to make A equal B.


Sample Input 1

4 3 5
4 2 5 2
6 4 2 1

Sample Output 1

16

Initialy, we have A = (4, 2, 5, 2).
The following sequence of operations makes A equal B.

  • Pay X = 3 yen to increase the value of A_3 by 1, making A = (4, 2, 6, 2).
  • Pay Y = 5 yen to swap the values of A_2 and A_3, making A = (4, 6, 2, 2).
  • Pay Y = 5 yen to swap the values of A_1 and A_2, making A = (6, 4, 2, 2).
  • Pay X = 3 yen to decrease the value of A_4 by 1, making A = (6, 4, 2, 1).

The total cost of these operations is 3+5+5+3 = 16 yen, which is the minimum possible.


Sample Input 2

5 12345 6789
1 2 3 4 5
1 2 3 4 5

Sample Output 2

0

A and B are equal from the beginning, so no operation is needed.


Sample Input 3

18 20719114 5117250357733867
10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712
14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077

Sample Output 3

13104119429316474

Note that values in input or output may not fit into 32-bit integer types.