A - 123233

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

6 桁の正整数 N が与えられます。
この整数が以下の条件を全て満たすか判定してください。

  • N の各桁のうち、 1 は丁度 1 つである。
  • N の各桁のうち、 2 は丁度 2 つである。
  • N の各桁のうち、 3 は丁度 3 つである。

制約

  • N100000 \le N \le 999999 を満たす整数

入力

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

N

出力

N が問題文中の条件を全て満たすなら Yes 、そうでないなら No1 行に出力せよ。


入力例 1

123233

出力例 1

Yes

123233 は問題文中の条件を満たすので、 Yes と出力します。


入力例 2

123234

出力例 2

No

123234 は問題文中の条件を満たさないので、 No と出力します。


入力例 3

323132

出力例 3

Yes

入力例 4

500000

出力例 4

No

Score : 100 points

Problem Statement

You are given a 6-digit positive integer N.
Determine whether N satisfies all of the following conditions.

  • Among the digits of N, the digit 1 appears exactly once.
  • Among the digits of N, the digit 2 appears exactly twice.
  • Among the digits of N, the digit 3 appears exactly three times.

Constraints

  • N is an integer satisfying 100000 \le N \le 999999.

Input

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

N

Output

Print Yes if N satisfies all the conditions described in the problem statement, and No otherwise, in one line.


Sample Input 1

123233

Sample Output 1

Yes

123233 satisfies the conditions in the problem statement, so print Yes.


Sample Input 2

123234

Sample Output 2

No

123234 does not satisfy the conditions in the problem statement, so print No.


Sample Input 3

323132

Sample Output 3

Yes

Sample Input 4

500000

Sample Output 4

No
B - Hurdle Parsing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

いろはちゃんは長さ N ( N \ge 1 ) の正整数列 A=(A_1,A_2,\dots,A_N) を持っています。
いろはちゃんは A を使って、次のように文字列 S を生成しました。

  • S= | から始める。
  • i=1,2,\dots,N の順に、次の操作を行う。
    • S の末尾に -A_i 個追加する。
    • その後、 S の末尾に |1 個追加する。

生成された文字列 S が与えられるので、正整数列 A を復元してください。

制約

  • S は問題文中の方法で生成された長さ 3 以上 100 以下の文字列
  • A は長さ 1 以上の正整数列

入力

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

S

出力

答えを以下の形式で、 1 行に空白区切りで出力せよ。

A_1 A_2 \dots A_N

入力例 1

|---|-|----|-|-----|

出力例 1

3 1 4 1 5

S= |---|-|----|-|-----| が生成されるような A(3,1,4,1,5) です。


入力例 2

|----------|

出力例 2

10

入力例 3

|-|-|-|------|

出力例 3

1 1 1 6

Score : 200 points

Problem Statement

Iroha has a sequence of positive integers A = (A_1, A_2, \dots, A_N) of length N (N \ge 1).
She generated a string S using A as follows:

  • Start with S = |.
  • For i = 1, 2, \dots, N, perform the following operations in order:
    • Append A_i copies of - to the end of S.
    • Then, append one | to the end of S.

Given the generated string S, reconstruct the sequence A.

Constraints

  • S is a string of length between 3 and 100, inclusive, generated by the method in the problem statement.
  • A is a sequence of positive integers of length at least 1.

Input

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

S

Output

Print the answer in the following format, with elements separated by spaces in a single line:

A_1 A_2 \dots A_N

Sample Input 1

|---|-|----|-|-----|

Sample Output 1

3 1 4 1 5

S = |---|-|----|-|-----| is generated by A = (3, 1, 4, 1, 5).


Sample Input 2

|----------|

Sample Output 2

10

Sample Input 3

|-|-|-|------|

Sample Output 3

1 1 1 6
C - Move Segment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

0, 1 のみからなる長さ N の文字列 S が与えられます。
S の中で先頭から K 番目の 1 の塊を K-1 番目の 1 の塊の直後まで移動した文字列を出力してください。

なお、S には 1 の塊が K 個以上含まれることが保証されます。

より正確な説明は以下の通りです。

  • Sl 文字目から r 文字目までからなる部分文字列を S_{l\ldots r} と表す。
  • S の部分文字列 S_{l\ldots r}1 の塊であるとは、以下の条件を全て満たすことと定める。
    • S_l=S_{l+1}=\cdots=S_r= 1
    • l=1 または S_{l-1}= 0
    • r=N または S_{r+1}= 0
  • S に含まれる 1 の塊が S_{l_1\ldots r_1},\ldots,S_{l_m\ldots r_m} で全てであり、l_1 < \cdots < l_m を満たしているとする。
    このとき、以下で定義される長さ N の文字列 T を、「S の中で先頭から K 番目の 1 の塊を K-1 番目の 1 の塊の直後まで移動した文字列」と定める
    • 1 \leq i \leq r_{K-1} のとき T_i = S_i
    • r_{K-1}+1 \leq i \leq r_{K-1}+(r_K-l_K)+1 のとき T_i= 1
    • r_{K-1}+(r_K-l_K)+2 \leq i \leq r_K のとき T_i= 0
    • r_K+1 \leq i \leq N のとき T_i=S_i

制約

  • 1 \leq N \leq 5\times 10^5
  • S0, 1 のみからなる長さ N の文字列
  • 2 \leq K
  • S には 1 の塊が K 個以上含まれる

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

15 3
010011100011001

出力例 1

010011111000001

S には、2 文字目から 2 文字目、5 文字目から 7 文字目、11 文字目から 12 文字目、15 文字目から 15 文字目の 4 つの 1 の塊があります。


入力例 2

10 2
1011111111

出力例 2

1111111110

Score : 300 points

Problem Statement

You are given a string S of length N consisting of 0 and 1.
Move the K-th 1-block from the beginning in S to immediately after the (K-1)-th 1-block, and print the resulting string.

It is guaranteed that S contains at least K 1-blocks.

Here is a more precise description.

  • Let S_{l\ldots r} denote the substring of S from the l-th character through the r-th character.
  • We define a substring S_{l\ldots r} of S to be a 1-block if it satisfies all of the following conditions:
    • S_l = S_{l+1} = \cdots = S_r = 1
    • l = 1 or S_{l-1} = 0
    • r = N or S_{r+1} = 0
  • Suppose that all 1-blocks in S are S_{l_1\ldots r_1}, \ldots, S_{l_m\ldots r_m}, where l_1 < l_2 < \cdots < l_m.

    Then, we define the length N string T, obtained by moving the K-th 1-block to immediately after the (K-1)-th 1-block, as follows:

    • T_i = S_i for 1 \leq i \leq r_{K-1}
    • T_i = 1 for r_{K-1} + 1 \leq i \leq r_{K-1} + (r_K - l_K) + 1
    • T_i = 0 for r_{K-1} + (r_K - l_K) + 2 \leq i \leq r_K
    • T_i = S_i for r_K + 1 \leq i \leq N

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 2 \leq K
  • S contains at least K 1-blocks.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

15 3
010011100011001

Sample Output 1

010011111000001

S has four 1-blocks: from the 2nd to the 2nd character, from the 5th to the 7th character, from the 11th to the 12th character, and from the 15th to the 15th character.


Sample Input 2

10 2
1011111111

Sample Output 2

1111111110
D - Strange Mirroring

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

英大小文字からなる文字列 S が与えられます。

S に以下の操作を 10^{100} 回繰り返します。

  • まず、 S の大文字を小文字に、小文字を大文字に書き換えた文字列を T とする。
  • その後、 ST とをこの順に連結した文字列を新たな S とする。

Q 個の質問に答えて下さい。 そのうち i 個目は次の通りです。

  • 全ての操作を終えた後の S の先頭から K_i 文字目を求めよ。

制約

  • S は英大小文字からなる長さ 1 以上 2 \times 10^5 以下の文字列
  • Q,K_i は整数
  • 1 \le Q \le 2 \times 10^5
  • 1 \le K_i \le 10^{18}

入力

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

S
Q
K_1 K_2 \dots K_Q

出力

i 個目の質問の答えを C_i とする時、以下の形式で 1 行に空白区切りで出力せよ。

C_1 C_2 \dots C_Q

入力例 1

aB
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

出力例 1

a B A b A b a B A b a B a B A b

操作前の S = aB です。

  • aB1 回操作を行うと aBAb となります。
  • aB2 回操作を行うと aBAbAbaB となります。
  • \dots

10^{100} 回の操作を終えた後の S = aBAbAbaBAbaBaBAb... です。


入力例 2

qWeRtYuIoP
8
1 1 2 3 5 8 13 21

出力例 2

q q W e t I E Q

入力例 3

AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay
5
1000000000000000000 123456789 1 987654321 999999999999999999

出力例 3

K a A Z L

Score : 350 points

Problem Statement

You are given a string S consisting of uppercase and lowercase English letters.

We perform the following operation on S 10^{100} times:

  • First, create a string T by changing uppercase letters in S to lowercase, and lowercase letters to uppercase.
  • Then, concatenate S and T in this order to form a new S.

Answer Q queries. The i-th query is as follows:

  • Find the K_i-th character from the beginning of S after all operations are completed.

Constraints

  • S is a string consisting of uppercase and lowercase English letters, with length between 1 and 2 \times 10^5, inclusive.
  • Q and K_i are integers.
  • 1 \le Q \le 2 \times 10^5
  • 1 \le K_i \le 10^{18}

Input

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

S
Q
K_1 K_2 \dots K_Q

Output

Let C_i be the answer to the i-th query. Print them in a single line, separated by spaces, in the following format:

C_1 C_2 \dots C_Q

Sample Input 1

aB
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Sample Output 1

a B A b A b a B A b a B a B A b

Before the operations, S = aB.

  • After performing the operation once on aB, it becomes aBAb.
  • After performing the operation twice on aB, it becomes aBAbAbaB.
  • \dots

After performing the operation 10^{100} times, S = aBAbAbaBAbaBaBAb...


Sample Input 2

qWeRtYuIoP
8
1 1 2 3 5 8 13 21

Sample Output 2

q q W e t I E Q

Sample Input 3

AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay
5
1000000000000000000 123456789 1 987654321 999999999999999999

Sample Output 3

K a A Z L
E - 1D Bucket Tool

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

1 から N の番号がついた N 個のマスが一列に並んでいます。
1 \leq i < N について、マス i とマス i+1 は隣接しています。

最初、マス i は色 i で塗られています。

クエリが Q 個与えられるので、順に処理してください。クエリは次の 2 種類のいずれかです。

  • 1 x c: マス x から始めて「いまいるマスと同じ色に塗られている隣接するマス」への移動を繰り返すことで到達可能なマスを全て色 c に塗り替える
  • 2 c: 色 c で塗られているマスの個数を答える

制約

  • 1 \leq N \leq 5\times 10^5
  • 1 \leq Q \leq 2\times 10^5
  • 1 種類目のクエリにおいて、1 \leq x \leq N
  • 1,2 種類目のクエリにおいて、1 \leq c \leq N
  • 2 種類目のクエリが少なくとも 1 つ存在する
  • 入力は全て整数である

入力

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

各クエリは以下の 2 種類のいずれかの形式で与えられる。

1 x c
2 c

出力

2 種類目のクエリの回数を q として、q 行出力せよ。
i 行目には、i 回目のそのようなクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

3
4

クエリにより、マスの色は図のように塗り替えられていきます。

図

Score : 450 points

Problem Statement

There are N cells in a row, numbered 1 to N.
For each 1 \leq i < N, cells i and i+1 are adjacent.

Initially, cell i is painted with color i.

You are given Q queries. Process them in order. Each query is of one of the following two types.

  • 1 x c: Repaint the following to color c: all reachable cells reachable from cell x by repeatedly moving to an adjacent cell painted in the same color as the current cell.
  • 2 c: Print the number of cells painted with color c.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • In queries of the first type, 1 \leq x \leq N.
  • In queries of the first and second types, 1 \leq c \leq N.
  • There is at least one query of the second type.
  • All input values are integers.

Input

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Each query is given in one of the following two formats:

1 x c
2 c

Output

Let q be the number of queries of the second type. Print q lines.

The i-th line should contain the answer to the i-th such query.


Sample Input 1

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

Sample Output 1

3
4

The queries recolor the cells as shown in the figure.

Figure

F - Exchange Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

高橋君と青木君が、数の書かれたカードを使ってゲームをします。

最初、高橋君は A_1,\ldots,A_N が書かれた N 枚のカードを、青木君は B_1,\ldots,B_M が書かれた M 枚のカードを手札として持っており、場には C_1,\ldots,C_L が書かれた L 枚のカードがあります。
高橋君と青木君はゲーム中常に、相手の手札も含め、全てのカードに書かれた数を知っている状態にあります。

高橋君と青木君は、高橋君から順に次の行動を交互に行います。

  • 自分の手札から 1 枚選び場に出す。その後、出したカードに書かれていた数未満の数が書かれたカードが場にあれば、そのうち 1 枚を場から自分の手札に移して良い。

先に行動が行えなくなった方が負けであり、負けでない方が勝ちです。互いに最適に行動したとき、どちらが勝つか判定してください。

なおこのゲームは必ず有限回の行動で勝敗がつくことが証明できます。

制約

  • 1 \leq N,M,L
  • N+M+L \leq 12
  • 1 \leq A_i,B_i,C_i \leq 10^9
  • 入力は全て整数である

入力

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

N M L
A_1 \ldots A_N
B_1 \ldots B_M
C_1 \ldots C_L

出力

高橋君が勝つならば Takahashi、青木君が勝つならば Aoki と出力せよ。


入力例 1

1 1 2
2
4
1 3

出力例 1

Aoki

ゲームは例えば次のように進行します。(最適な行動とは限りません)

  • 高橋君が手札から 2 を場に出し、1 を場から自分の手札に移す。高橋君の手札は (1)、青木君の手札は (4)、場札は (2,3) となる。
  • 青木君が手札から 4 を場に出し、2 を場から自分の手札に移す。高橋君の手札は (1)、青木君の手札は (2)、場札は (3,4) となる。
  • 高橋君が手札から 1 を場に出す。高橋君の手札は ()、青木君の手札は (2)、場札は (1,3,4) となる。
  • 青木君が手札から 2 を場に出す。高橋君の手札は ()、青木君の手札は ()、場札は (1,2,3,4) となる。
  • 高橋君は行動できないため負けであり、青木君が勝ち。

入力例 2

4 4 4
98 98765 987654 987654321
987 9876 9876543 98765432
123 12345 1234567 123456789

出力例 2

Takahashi

入力例 3

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

出力例 3

Aoki

Score : 500 points

Problem Statement

Takahashi and Aoki will play a game using cards with numbers written on them.

Initially, Takahashi has N cards with numbers A_1, \ldots, A_N in his hand, Aoki has M cards with numbers B_1, \ldots, B_M in his hand, and there are L cards with numbers C_1, \ldots, C_L on the table.
Throughout the game, both Takahashi and Aoki know all the numbers on all the cards, including the opponent's hand.

Starting with Takahashi, they take turns performing the following action:

  • Choose one card from his hand and put it on the table. Then, if there is a card on the table with a number less than the number on the card he just played, he may take one such card from the table into his hand.

The player who cannot make a move first loses, and the other player wins. Determine who wins if both players play optimally.

It can be proved that the game always ends in a finite number of moves.

Constraints

  • 1 \leq N, M, L
  • N + M + L \leq 12
  • 1 \leq A_i, B_i, C_i \leq 10^9
  • All input values are integers.

Input

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

N M L
A_1 \ldots A_N
B_1 \ldots B_M
C_1 \ldots C_L

Output

Print Takahashi if Takahashi wins, and Aoki if Aoki wins.


Sample Input 1

1 1 2
2
4
1 3

Sample Output 1

Aoki

The game may proceed as follows (not necessarily optimal moves):

  • Takahashi plays 2 from his hand to the table, and takes 1 from the table into his hand. Now, Takahashi's hand is (1), Aoki's hand is (4), and the table cards are (2,3).
  • Aoki plays 4 from his hand to the table, and takes 2 into his hand. Now, Takahashi's hand is (1), Aoki's hand is (2), and the table cards are (3,4).
  • Takahashi plays 1 from his hand to the table. Now, Takahashi's hand is (), Aoki's hand is (2), and the table cards are (1,3,4).
  • Aoki plays 2 from his hand to the table. Now, Takahashi's hand is (), Aoki's hand is (), and the table cards are (1,2,3,4).
  • Takahashi cannot make a move and loses; Aoki wins.

Sample Input 2

4 4 4
98 98765 987654 987654321
987 9876 9876543 98765432
123 12345 1234567 123456789

Sample Output 2

Takahashi

Sample Input 3

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

Sample Output 3

Aoki
G - Another Shuffle Window

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 575

問題文

(1,2,\dots,N) の順列 P と整数 K が与えられます。

順列 P に以下の操作を行った後の転倒数の期待値を \text{mod} \ 998244353 で求めてください。

  • まず、整数 i1 以上 N-K+1 以下の整数の中から一様ランダムに選択する。
  • その後、 P_i,P_{i+1},\dots,P_{i+K-1} を一様ランダムにシャッフルする。
転倒数とは? 数列 (A_1,A_2,\dots,A_N) の転倒数とは、 1 \le i < j \le N かつ A_i > A_j を満たす整数組 (i,j) の個数を指します。
期待値 \text{mod} \ 998244353 とは? 求める期待値は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \not \equiv 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 入力は全て整数
  • 1 \le K \le N \le 2 \times 10^5
  • P(1,2,\dots,N) の順列

入力

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

N K
P_1 P_2 \dots P_N

出力

答えを 1 行に出力せよ。


入力例 1

4 2
1 4 2 3

出力例 1

166374061

操作によって、順列 P は以下のように変化します。

  • (1,4,2,3) ... 確率 1/2
  • (4,1,2,3) ... 確率 1/6
  • (1,2,4,3) ... 確率 1/6
  • (1,4,3,2) ... 確率 1/6

転倒数の期待値は、 \displaystyle 2 \times \frac{1}{2} + 3 \times \frac{1}{6} + 1 \times \frac{1}{6} + 3 \times \frac{1}{6} = \frac{13}{6} となります。
\displaystyle \frac{13}{6}\text{mod} \ 998244353 で表現すると 166374061 となるので、これを出力してください。


入力例 2

1 1
1

出力例 2

0

入力例 3

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

出力例 3

499122200

Score : 575 points

Problem Statement

You are given a permutation P of (1,2,\dots,N) and an integer K.

Find the expected value, modulo 998244353, of the inversion number of P after performing the following operation:

  • First, choose an integer i uniformly at random between 1 and N - K + 1, inclusive.
  • Then, shuffle P_i, P_{i+1}, \dots, P_{i+K-1} uniformly at random.
What is the inversion number? The inversion number of a sequence (A_1, A_2, \dots, A_N) is the number of integer pairs (i, j) satisfying 1 \le i < j \le N and A_i > A_j.
What does "expected value modulo 998244353" mean? It can be proved that the sought expected value is always rational. Under the constraints of this problem, when this value is represented as an irreducible fraction \frac{P}{Q}, it can also be proved that Q \not\equiv 0 \pmod{998244353}. Thus, there is a unique integer R satisfying R \times Q \equiv P \pmod{998244353}, \ 0 \le R < 998244353. Report this integer R.

Constraints

  • All input values are integers.
  • 1 \le K \le N \le 2 \times 10^5
  • P is a permutation of (1,2,\dots,N).

Input

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

N K
P_1 P_2 \dots P_N

Output

Print the answer in one line.


Sample Input 1

4 2
1 4 2 3

Sample Output 1

166374061

The operation changes the permutation P into the following:

  • (1,4,2,3) ... probability 1/2
  • (4,1,2,3) ... probability 1/6
  • (1,2,4,3) ... probability 1/6
  • (1,4,3,2) ... probability 1/6

The expected value of the inversion number is \displaystyle 2 \times \frac{1}{2} + 3 \times \frac{1}{6} + 1 \times \frac{1}{6} + 3 \times \frac{1}{6} = \frac{13}{6}.

\displaystyle \frac{13}{6} modulo 998244353 is 166374061, so print this number.


Sample Input 2

1 1
1

Sample Output 2

0

Sample Input 3

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

Sample Output 3

499122200