E - Rotate Colored Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

英小文字からなる長さ N の文字列 S が与えられます。 S の各文字は色 1 、色 2 、\ldots 、色 M の M 色のうちのいずれかで塗られており、 i = 1, 2, \ldots, N について、S の i 文字目は色 C_i で塗られています。

各 i = 1, 2, \ldots, M について、この順番に下記の操作を行います。

  • S の色 i で塗られた文字からなる部分を、右に 1 つ巡回シフトする。 すなわち、S の 色 i で塗られた文字の位置が先頭のものから順に p_1, p_2, p_3, \ldots, p_k 文字目であるとき、 S の p_1, p_2, p_3, \ldots, p_k 文字目を、それぞれ、S の p_k, p_1,p_2, \ldots, p_{k-1} 文字目で同時に置き換える。

上記の操作をすべて行った後の、最終的な S を出力してください。

なお、M 色あるどの色についても、その色で塗られた S の文字が必ず 1 つ以上存在することが、制約として保証されます。

制約

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, C_i はすべて整数
  • S は英小文字からなる長さ N の文字列
  • 任意の整数 1 \leq i \leq M に対して、ある整数 1 \leq j \leq N が存在して C_j = i が成り立つ

入力

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

N M
S
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

出力例 1

cszapqbr

はじめ、S = apzbqrcs です。

  • i = 1 に対する操作では、S の 1, 4, 7 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cpzaqrbs となります。
  • i = 2 に対する操作では、S の 2, 5, 6, 8 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります。
  • i = 3 に対する操作では、S の 3 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります(操作の前後で S は変わりません)。

よって、最終的な S である cszapqbr を出力します。


入力例 2

2 1
aa
1 1

出力例 2

aa

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters. Each character of S is painted in one of the M colors: color 1, color 2, ..., color M; for each i = 1, 2, \ldots, N, the i-th character of S is painted in color C_i.

For each i = 1, 2, \ldots, M in this order, let us perform the following operation.

  • Perform a right circular shift by 1 on the part of S painted in color i. That is, if the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters are painted in color i from left to right, then simultaneously replace the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters of S with the p_k-th, p_1-th, p_2-th, \ldots, p_{k-1}-th characters of S, respectively.

Print the final S after the above operations.

The constraints guarantee that at least one character of S is painted in each of the M colors.

Constraints

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, and C_i are all integers.
  • S is a string of length N consisting of lowercase English letters.
  • For each integer 1 \leq i \leq M, there is an integer 1 \leq j \leq N such that C_j = i.

Input

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

N M
S
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

Sample Output 1

cszapqbr

Initially, S = apzbqrcs.

  • For i = 1, perform a right circular shift by 1 on the part of S formed by the 1-st, 4-th, 7-th characters, resulting in S = cpzaqrbs.
  • For i = 2, perform a right circular shift by 1 on the part of S formed by the 2-nd, 5-th, 6-th, 8-th characters, resulting in S = cszapqbr.
  • For i = 3, perform a right circular shift by 1 on the part of S formed by the 3-rd character, resulting in S = cszapqbr (here, S is not changed).

Thus, you should print cszapqbr, the final S.


Sample Input 2

2 1
aa
1 1

Sample Output 2

aa
F - AtCoder AAC Contest

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋くんは、文字をいくつか持っています。 高橋くんが持っている文字は A, B, C のいずれかです。 はじめ、高橋くんは A を n _ A 個、B を n _ B 個、C を n _ C 個持っています。

高橋くんは、A を 1 つ、C を 1 つ、さらに追加で任意の 1 つの文字の合計 3 文字を使うことで、コンテストを 1 つ開催することができます。 具体的には、A を 2 つ、C を 1 つ使うと AAC を、A, B, C を 1 つずつ使うと ABC を、A を 1 つ、C を 2 つ使うと ACC を開催することができます。

高橋くんは、現在持っている文字を使ってコンテストをなるべく多く開催したいです。 高橋くんが最大でいくつのコンテストを開催することができるか求めてください。

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

制約

  • 1\le T\le2\times10 ^ 5
  • それぞれのテストケースについて、
    • 0\le n _ A\le10 ^ 9
    • 0\le n _ B\le10 ^ 9
    • 0\le n _ C\le10 ^ 9
  • 入力はすべて整数

入力

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

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

\mathrm{testcase} _ i\ (1\le i\le T) は i 番目のテストケースを表しており、次の形式で与えられる。

n _ A n _ B n _ C

出力

T 行にわたって出力せよ。 i 行目 (1\le i\le T) には、i 番目のテストケースの答えを出力せよ。


入力例 1

5
3 1 2
100 0 0
1000000 1000000 1000000
31 41 59
1000000000 10000 1

出力例 1

2
0
1000000
31
1

1 つめのテストケースでは、AAC を 1 回、ABC を 1 回の計 2 回のコンテストを開催することができます。 よって、1 行目には 2 を出力してください。

Score : 300 points

Problem Statement

Takahashi has some letters. Each letter he has is A, B, or C. Initially, he has n _ A letters A, n _ B letters B, and n _ C letters C.

He can hold one contest by using one letter A, one letter C, and additionally any one letter, for a total of three letters. Specifically, he can hold AAC by using two letters A and one letter C, ABC by using one letter each of A, B, and C, and ACC by using one letter A and two letters C.

He wants to hold as many contests as possible using the letters he currently has. Find the maximum number of contests he can hold.

T test cases are given, so report the answer for each of them.

Constraints

  • 1\le T\le2\times10 ^ 5
  • For each test case,
    • 0\le n _ A\le10 ^ 9
    • 0\le n _ B\le10 ^ 9
    • 0\le n _ C\le10 ^ 9
  • All input values are integers.

Input

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

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

\mathrm{testcase} _ i\ (1\le i\le T) represents the i-th test case and is given in the following format:

n _ A n _ B n _ C

Output

Output over T lines. On the i-th line (1\le i\le T), output the answer to the i-th test case.


Sample Input 1

5
3 1 2
100 0 0
1000000 1000000 1000000
31 41 59
1000000000 10000 1

Sample Output 1

2
0
1000000
31
1

In the first test case, he can hold AAC once and ABC once, for a total of two contests. Therefore, output 2 on the first line.

G - Make Geometric Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB



配点 : 425 点

問題文

長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。 ここで、どの i\ (1\le i\le N) についても A _ i が 0 でないことが保証されます。

A を適切に並べ替えた数列 B=(B _ 1,B _ 2,\ldots,B _ N) が等比数列になることがあるか判定してください。

ただし、数列 S=(S _ 1,S _ 2,\ldots,S _ N) が等比数列であるとは、ある実数 r が存在してすべての整数 1\le i\lt N に対して S _ {i+1}=rS _ i が成り立つことをいいます。

1 つの入力ファイルにつき、T 個のテストケースを解いてください。

制約

  • 1\le T\le10 ^ 5
  • 2\le N\le2\times10 ^ 5
  • -10 ^ 9\le A _ i\le10 ^ 9\ (1\le i\le N)
  • A _ i\ne0\ (1\le i\le N)
  • 1 つの入力ファイルにおける N の総和は 2\times10 ^ 5 以下
  • 入力はすべて整数

入力

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

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

ここで、\mathrm{testcase} _ i は i 番目 (1\le i\le T) のテストケースであり、各テストケースは以下の形式で与えられる。

N
A _ 1 A _ 2 \ldots A _ N

出力

T 行にわたって出力せよ。 i 行目 (1\le i\le T) には、i 番目のテストケースにおいて A を並べ替えて等比数列にできる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3
5
1 8 2 4 16
5
-16 24 54 81 -36
7
90000 8100 -27000 729 -300000 -2430 1000000

出力例 1

Yes
No
Yes

1 つめのテストケースでは、A を並べ替えた (16,8,4,2,1) は、公比 r=\dfrac12 の等比数列になります。 よって、1 行目には Yes と出力してください。

2 つめのテストケースでは、A をどのように並べ替えても条件を満たしません。 よって、2 行目には No と出力してください。

Score : 425 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N. It is guaranteed that for any i\ (1\le i\le N), A_i is not 0.

Determine whether there exists a permutation B=(B_1,B_2,\ldots,B_N) of A such that B forms a geometric sequence.

A sequence S=(S_1,S_2,\ldots,S_N) is a geometric sequence if there exists a real number r such that S_{i+1}=rS_i for all integers 1\le i\lt N.

Solve T test cases per input file.

Constraints

  • 1\le T\le10^5
  • 2\le N\le2\times10^5
  • -10^9\le A_i\le10^9\ (1\le i\le N)
  • A_i\ne0\ (1\le i\le N)
  • The sum of N over all test cases in a single input file is at most 2\times10^5.
  • All input values are integers.

Input

The input is given from standard input in the following format:

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

where \mathrm{testcase}_i is the i-th test case (1\le i\le T), and each test case is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Output T lines. The i-th line (1\le i\le T) should contain Yes if A can be rearranged to form a geometric sequence in the i-th test case, and No otherwise.


Sample Input 1

3
5
1 8 2 4 16
5
-16 24 54 81 -36
7
90000 8100 -27000 729 -300000 -2430 1000000

Sample Output 1

Yes
No
Yes

In the first test case, the rearrangement (16,8,4,2,1) of A forms a geometric sequence with common ratio r=\frac{1}{2}. Thus, print Yes on the first line.

In the second test case, no rearrangement of A satisfies the condition. Thus, print No on the second line.

H - Geometric Progression

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

整数 A, X, M が与えられます。\displaystyle \sum_{i = 0}^{X-1} A^i を M で割った余りを求めてください。

制約

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • 入力はすべて整数

入力

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

A X M

出力

答えを出力せよ。


入力例 1

3 4 7

出力例 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40 です。40 を 7 で割った余りは 5 であるため、5 を出力します。


入力例 2

8 10 9

出力例 2

0

入力例 3

1000000000 1000000000000 998244353

出力例 3

919667211

Score : 500 points

Problem Statement

Given integers A, X, and M, find \displaystyle \sum_{i = 0}^{X-1} A^i, modulo M.

Constraints

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • All values in the input are integers.

Input

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

A X M

Output

Print the answer.


Sample Input 1

3 4 7

Sample Output 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40, which equals 5 modulo 7, so 5 should be printed.


Sample Input 2

8 10 9

Sample Output 2

0

Sample Input 3

1000000000 1000000000000 998244353

Sample Output 3

919667211
I - BOX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 個の箱 1,2,\dots,N と、 10^{100} 個のボール 1,2,\dots,10^{100} があります。 最初、箱 i にはボール i のみが入っています。

ここに以下の操作が合計 Q 回行われるので、処理してください。

操作にはタイプ 1,2,3 の 3 種類があります。

タイプ 1 : 箱 X に箱 Y の中身を全て入れる。 この操作では X \neq Y が保証される。

1 X Y

タイプ 2 : 現在いずれかの箱に入っているボールの数の合計を k とすると、箱 X にボール k+1 を入れる。

2 X

タイプ 3 : ボール X が入っている箱の番号を答える。

3 X

制約

  • 入力は全て整数
  • 2 \le N \le 3 \times 10^5
  • 1 \le Q \le 3 \times 10^5
  • タイプ 1 の操作について、 1 \le X,Y \le N かつ X \neq Y
  • タイプ 2 の操作について、 1 \le X \le N
  • タイプ 3 の操作について、その時点でボール X がいずれかの箱に入っている
  • タイプ 3 の操作が少なくとも 1 つ与えられる

入力

入力は以下の形式で標準入力から与えられる。
但し、 op_i は i 回目の操作を表す。

N Q
op_1
op_2
\vdots
op_Q

出力

各タイプ 3 の操作に対して、答えを 1 行に 1 つ、整数として出力せよ。


入力例 1

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

出力例 1

5
4
3
1
3

この入力は 10 個の操作を含みます。

  • 1 回目の操作はタイプ 3 です。ボール 5 は箱 5 に入っています。
  • 2 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
    • 箱 1 の中身はボール 1,4 、箱 4 の中身は空になります。
  • 3 回目の操作はタイプ 2 です。箱 1 にボール 6 を入れます。
  • 4 回目の操作はタイプ 2 です。箱 4 にボール 7 を入れます。
  • 5 回目の操作はタイプ 3 です。ボール 7 は箱 4 に入っています。
  • 6 回目の操作はタイプ 1 です。箱 3 に箱 1 の中身を全て入れます。
    • 箱 3 の中身はボール 1,3,4,6 、箱 1 の中身は空になります。
  • 7 回目の操作はタイプ 3 です。ボール 4 は箱 3 に入っています。
  • 8 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
    • 箱 1 の中身はボール 7 、箱 4 の中身は空になります。
  • 9 回目の操作はタイプ 3 です。ボール 7 は箱 1 に入っています。
  • 10 回目の操作はタイプ 3 です。ボール 6 は箱 3 に入っています。

Score : 500 points

Problem Statement

There are N boxes numbered 1,2,\ldots,N, and 10^{100} balls numbered 1,2,\dots,10^{100}. Initially, box i contains just ball i.

Process a total of Q operations that will be performed.

There are three types of operations: 1, 2, and 3.

Type 1: Put all contents of box Y into box X. It is guaranteed that X \neq Y.

1 X Y

Type 2: Put ball k+1 into box X, where k is the current total number of balls contained in the boxes.

2 X

Type 3: Report the number of the box that contains ball X.

3 X

Constraints

  • All values in the input are integers.
  • 2 \le N \le 3 \times 10^5
  • 1 \le Q \le 3 \times 10^5
  • For each type-1 operation, 1 \le X,Y \le N and X \neq Y.
  • For each type-2 operation, 1 \le X \le N.
  • For each type-3 operation, ball X is contained in some box at that point.
  • There is at least one type-3 operation.

Input

The input is given from Standard Input in the following format.
Here, op_i represents the i-th operation.

N Q
op_1
op_2
\vdots
op_Q

Output

For each type-3 operation, print a line containing the response as an integer.


Sample Input 1

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

Sample Output 1

5
4
3
1
3

This input contains ten operations.

  • The first operation is of type 3. Ball 5 is in box 5.
  • The second operation is of type 1. Put all contents of box 4 into box 1.
    • Box 1 now contains balls 1 and 4, and box 4 is now empty.
  • The third operation is of type 2. Put ball 6 into box 1.
  • The fourth operation is of type 2. Put ball 7 into box 4.
  • The fifth operation is of type 3. Ball 7 is in box 4.
  • The sixth operation is of type 1. Put all contents of box 1 into box 3.
    • Box 3 now contains balls 1, 3, 4, and 6, and box 1 is now empty.
  • The seventh operation is of type 3. Ball 4 is in box 3.
  • The eighth operation is of type 1. Put all contents of box 4 into box 1.
    • Box 1 now contains ball 7, and box 4 is now empty.
  • The ninth operation is of type 3. Ball 7 is in box 1.
  • The tenth operation is of type 3. Ball 6 is in box 3.