A - mod M

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

数列 A = (A_1, A_2, ..., A_N) が与えられます。
あなたは次の操作をちょうど 1 回行うことができます。

  • 2 以上の整数 M1 つ選ぶ。その後、1 \leq i \leq N を満たすすべての整数 i に対して、 A_i を 「A_iM で割ったあまり」に置き換える。

例えば A = (2, 7, 4)M = 4 を選んだ時、操作後の A(2 \bmod 4, 7 \bmod 4, 4 \bmod 4) = (2, 3, 0) になります。

操作を行った後の A に含まれる要素の種類数は最小で何種類になりますか?

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
1 4 8

出力例 1

2

操作で M = 3 を選ぶと A = (1 \bmod 3, 4 \bmod 3, 8 \bmod 3) = (1, 1, 2) になり、操作後の A の要素の種類数は 2 種類になります。
A の要素の種類数を 1 種類にすることはできないので 2 が答えです。


入力例 2

4
5 10 15 20

出力例 2

1

操作で M = 5 を選ぶと A = (0, 0, 0, 0) になり、これが最適です。


入力例 3

10
3785 5176 10740 7744 3999 3143 9028 2822 4748 6888

出力例 3

1

Score : 300 points

Problem Statement

You are given a sequence A = (A_1, A_2, ..., A_N).
You may perform the following operation exactly once.

  • Choose an integer M at least 2. Then, for every integer i (1 \leq i \leq N), replace A_i with the remainder when A_i is divided by M.

For instance, if M = 4 is chosen when A = (2, 7, 4), A becomes (2 \bmod 4, 7 \bmod 4, 4 \bmod 4) = (2, 3, 0).

Find the minimum possible number of different elements in A after the operation.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3
1 4 8

Sample Output 1

2

If you choose M = 3, you will have A = (1 \bmod 3, 4 \bmod 3, 8 \bmod 3) = (1, 1, 2), where A contains two different elements.
The number of different elements in A cannot become 1, so the answer is 2.


Sample Input 2

4
5 10 15 20

Sample Output 2

1

If you choose M = 5, you will have A = (0, 0, 0, 0), which is optimal.


Sample Input 3

10
3785 5176 10740 7744 3999 3143 9028 2822 4748 6888

Sample Output 3

1
B - dp

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

dp からなる長さ L の文字列 T に対して、T180 度回転した文字列を f(T) と表します。より厳密には、f(T) を次の条件を満たす文字列として定めます。

  • f(T)dp からなる長さ L の文字列である。
  • 1 \leq i \leq L であるすべての整数 i について、f(T)i 文字目は TL + 1 - i 文字目と異なる。

例えば T = ddddd のとき f(T) = ppppp, T = dpdppp のとき f(T)= dddpdp です。

dp からなる長さ N の文字列 S が与えられます。
あなたは次の操作を 0 回以上 1 回以下行うことができます。

  • 1 \leq L \leq R \leq N である整数の組 (L, R)1 つ選び、SL 文字目から R 文字目までからなる部分文字列を T とする。そして、SL 文字目から R 文字目までを f(T) に置き換える。

例えば S= dpdpp, (L,R)=(2,4) の場合、T= pdp, f(T)= dpd なので Sddpdp に変化します。

最終的な S としてあり得る文字列のうち辞書順最小のものを出力してください。

辞書順とは?

文字列 S = S_1S_2\ldots S_{|S|} が文字列 T = T_1T_2\ldots T_{|T|} より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。

  1. |S| \lt |T| かつ S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
    • S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
    • S_iT_i よりアルファベット順で小さい文字である。

制約

  • 1 \leq N \leq 5000
  • Sdp からなる長さ N の文字列
  • N は整数

入力

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

N
S

出力

答えを出力せよ。


入力例 1

6
dpdppd

出力例 1

dddpdd

(L, R) = (2, 5) とします。T = pdpp, f(T) = ddpd なので、操作後の Sdddpdd になります。
得られる文字列のうち dddpdd が辞書順最小なので、これを出力します。


入力例 2

3
ddd

出力例 2

ddd

操作を行わないことが最適な場合もあります。


入力例 3

11
ddpdpdppddp

出力例 3

ddddpdpdddp

Score : 500 points

Problem Statement

For a string T of length L consisting of d and p, let f(T) be T rotated 180 degrees. More formally, let f(T) be the string that satisfies the following conditions.

  • f(T) is a string of length L consisting of d and p.
  • For every integer i such that 1 \leq i \leq L, the i-th character of f(T) differs from the (L + 1 - i)-th character of T.

For instance, if T = ddddd, f(T) = ppppp; if T = dpdppp, f(T)= dddpdp.

You are given a string S of length N consisting of d and p.
You may perform the following operation zero or one time.

  • Choose a pair of integers (L, R) such that 1 \leq L \leq R \leq N, and let T be the substring formed by the L-th through R-th characters of S. Then, replace the L-th through R-th characters of S with f(T).

For instance, if S= dpdpp and (L,R)=(2,4), we have T= pdp and f(T)= dpd, so S becomes ddpdp.

Print the lexicographically smallest string that S can become.

What is lexicographical order?

A string S = S_1S_2\ldots S_{|S|} is said to be lexicographically smaller than a string T = T_1T_2\ldots T_{|T|} if one of the following 1. and 2. holds. Here, |S| and |T| denote the lengths of S and T, respectively.

  1. |S| \lt |T| and S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}.
  2. There is an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace that satisfies the following two conditions:
    • S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}.
    • S_i is smaller than T_i in alphabetical order.

Constraints

  • 1 \leq N \leq 5000
  • S is a string of length N consisting of d and p.
  • N is an integer.

Input

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

N
S

Output

Print the answer.


Sample Input 1

6
dpdppd

Sample Output 1

dddpdd

Let (L, R) = (2, 5). Then, we have T = pdpp and f(T) = ddpd, so S becomes dddpdd.
This is the lexicographically smallest string that can be obtained, so print it.


Sample Input 2

3
ddd

Sample Output 2

ddd

It may be optimal to skip the operation.


Sample Input 3

11
ddpdpdppddp

Sample Output 3

ddddpdpdddp
C - Lights Out on Tree

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

頂点に 1 から N の番号がついた N 頂点の根付き木があります。頂点 1 が根で、頂点 i (2 \leq i \leq N) の親は頂点 P_i です。
表裏のあるコインが N 枚あります。コインは全ての頂点の上に 1 枚ずつ載っています。
また、1 から N までの番号がついた N 個のボタンがあります。ボタン n を押すと n を根とする部分木に含まれる頂点に載っている全てのコインが裏返ります。

以下で説明するクエリを Q 個処理してください。

i 番目のクエリではサイズ M_i の頂点集合 S_i = \lbrace v_{i,1}, v_{i,2},\dots, v_{i,M_i} \rbrace が与えられます。
今、S_i に含まれる頂点の上に載っているコインは表を、それ以外のコインは裏を向いています。ボタンを 1 つ選んで押すことを繰り返して、N 枚のコイン全てを裏向きにするには、最小で何回ボタンを押す必要がありますか?答えを出力してください。ただし、どのようにボタンを押しても N 枚のコイン全てが裏向きにならない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \lt i
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq M_i
  • \displaystyle \sum_{i=1}^Q M_i \leq 2 \times 10^5
  • 1 \leq v_{i,j} \leq N
  • v_{i,1}, v_{i,2},\dots,v_{i,M_i} は互いに異なる
  • 入力される値はすべて整数

入力

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

N Q
P_2 P_3 \dots P_N
M_1 v_{1,1} v_{1,2} \dots v_{1,M_1}
M_2 v_{2,1} v_{2,2} \dots v_{2,M_2}
\vdots
M_Q v_{Q,1} v_{Q,2} \dots v_{Q,M_Q}

出力

Q 行出力せよ。i 行目には i 番目のクエリの答えを出力せよ。


入力例 1

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

出力例 1

1
2
1
3
2
3

1 番目のクエリについて、以下に説明するようにボタンを 1 回押すことで条件を満たすことができて、これが最小です。

  • ボタン 1 を押す。頂点 1,2,3,4,5,6 に載っているコインが裏返る。

2 番目のクエリについて、以下に説明するようにボタンを 2 回押すことで条件を満たすことができて、これが最小です。

  • ボタン 4 を押す。頂点 4 に載っているコインが裏返る。
  • ボタン 2 を押す。頂点 2,4,5,6 に載っているコインが裏返る。

Score : 500 points

Problem Statement

We have a rooted tree with N vertices numbered 1 to N. Vertex 1 is the root, and the parent of vertex i is vertex P_i.
There are N coins with heads and tails, one on each vertex.
Additionally, there are N buttons numbered 1 to N. Pressing button n flips all coins on the vertices in the subtree rooted at vertex n.

Process Q queries described below.

In the i-th query, you are given a vertex set of size M_i: S_i = \lbrace v_{i,1}, v_{i,2},\dots, v_{i,M_i} \rbrace.
Now, the coins on the vertices in S_i are facing heads-up, and the others are facing tails-up. In order to make all N coins face tails-up by repeatedly choosing a button and pressing it, at least how many button presses are needed? Print the answer, or -1 if there is no way to make all the coins face tails-up.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \lt i
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq M_i
  • \displaystyle \sum_{i=1}^Q M_i \leq 2 \times 10^5
  • 1 \leq v_{i,j} \leq N
  • v_{i,1}, v_{i,2},\dots,v_{i,M_i} are pairwise different.
  • All values in the input are integers.

Input

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

N Q
P_2 P_3 \dots P_N
M_1 v_{1,1} v_{1,2} \dots v_{1,M_1}
M_2 v_{2,1} v_{2,2} \dots v_{2,M_2}
\vdots
M_Q v_{Q,1} v_{Q,2} \dots v_{Q,M_Q}

Output

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


Sample Input 1

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

Sample Output 1

1
2
1
3
2
3

For the first query, you can satisfy the requirement in one button press, which is the minimum needed, as follows.

  • Press button 1, flipping the coins on vertices 1,2,3,4,5,6.

For the second query, you can satisfy the requirement in two button presses, which is the minimum needed, as follows.

  • Press button 4, flipping the coin on vertex 4.
  • Press button 2, flipping the coins on vertex 2,4,5,6.
D - mod M Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

黒板に 2N 個の整数 A_1, A_2, ..., A_{2N} が書かれています。また、2 以上の整数 M があります。
Alice と Bob はゲームをします。 ゲームは Alice を先攻として、黒板の上の数が全てなくなるまで次の操作を交互に行います。

  • 数を 1 個選び、その数を黒板から消す。

ゲームを終了した時点で、(Alice が消した数の和) \text{mod }M と (Bob が消した数の和) \text{mod }M が一致していれば Bob の勝ち、そうでない場合は Alice の勝ちです。
両者が最善を尽くしたとき、どちらが勝ちますか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • 0 \leq A_i \leq M - 1
  • 入力される値はすべて整数

入力

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

N M
A_1 A_2 \dots A_{2N}

出力

Alice が勝つ場合は Alice を、Bob が勝つ場合は Bob を出力せよ。


入力例 1

2 9
1 4 8 5

出力例 1

Alice

ゲームの進行例として次のようなものが考えられます。

  • Alice が 1 を消す。
  • Bob が 4 を消す。
  • Alice が 5 を消す。
  • Bob が 8 を消す。

このように進んだ場合、(Alice が消した数の和) \text{mod }M(1 + 5) \bmod 9 = 6, (Bob が消した数の和) \text{mod }M(4 + 8) \bmod 9 = 3 で、6 \neq 3 なので Alice の勝ちです。


入力例 2

3 998244353
1 2 3 1 2 3

出力例 2

Bob

Score : 700 points

Problem Statement

There are 2N integers A_1, A_2, ..., A_{2N} written on a blackboard, and an integer M at least 2.
Alice and Bob will play a game. They will alternately perform the following operation, with Alice going first, until there is no number on the blackboard.

  • Choose a number and delete it from the blackboard.

At the end of the game, if the sum of numbers deleted by Alice modulo M equals the sum of numbers deleted by Bob modulo M, Bob wins; otherwise, Alice wins.
Which player will win if both plays optimally?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 10^9
  • 0 \leq A_i \leq M - 1
  • All values in the input are integers.

Input

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

N M
A_1 A_2 \dots A_{2N}

Output

If Alice wins, print Alice; if Bob wins, print Bob.


Sample Input 1

2 9
1 4 8 5

Sample Output 1

Alice

The game may proceed as follows.

  • Alice deletes 1.
  • Bob deletes 4.
  • Alice deletes 5.
  • Bob deletes 8.

In this case, the sum of numbers deleted by Alice modulo M is (1 + 5) \bmod 9 = 6, and the sum of numbers deleted by Bob modulo M is (4 + 8) \bmod 9 = 3. Since 6 \neq 3, Alice wins.


Sample Input 2

3 998244353
1 2 3 1 2 3

Sample Output 2

Bob
E - ≥ K

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

長さ N の数列 A = (A_1, ..., A_N) および整数 K が与えられます。
A の要素を並べ替えて得られる数列のうち、隣接する要素の和が K より小さい箇所が存在しない数列は何通りありますか?個数を 998244353 で割ったあまりを求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 5
1 2 3 4

出力例 1

4

条件を満たす数列は次の 4 通りです。

  • (1, 4, 2, 3)
  • (1, 4, 3, 2)
  • (2, 3, 4, 1)
  • (3, 2, 4, 1)

入力例 2

4 3
1 2 3 3

出力例 2

12

A の要素を並べ替えてできる数列としてあり得るのは全部で 12 通りあり、その全てが条件を満たします。


入力例 3

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

出力例 3

108

Score : 800 points

Problem Statement

You are given a sequence of length N, A = (A_1, ..., A_N), and an integer K.
How many permutations of A are there such that no two adjacent elements sum to less than K? Find the count modulo 998244353.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq A_i \leq 10^9
  • All values in the input are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 5
1 2 3 4

Sample Output 1

4

The following four permutations satisfy the condition:

  • (1, 4, 2, 3)
  • (1, 4, 3, 2)
  • (2, 3, 4, 1)
  • (3, 2, 4, 1)

Sample Input 2

4 3
1 2 3 3

Sample Output 2

12

There are 12 permutations of A, all of which satisfy the condition.


Sample Input 3

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

Sample Output 3

108
F - 998244353 → 1000000007

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

この問題は output-only です。

符号無し 64 bit 整数の加算・乗算・ 998244353 を除数とする modulo 演算ができるプログラミング言語があります。
この言語を用いて \text{mod }1000000007 における乗算を行うプログラムを作成してください。

厳密に説明すると、0 以上 1000000006 以下の整数 a,b が与えられたときに a \times b \bmod{1000000007} を計算するプログラムを、以下の 仕様形式 に従って作成してください。

プログラムの仕様

このプログラムでは、英大文字で表される A, B, \dots, Z26 個の 変数 を扱うことが出来る。
各変数は 0 以上 2^{64} 未満の整数値 (以下 符号無し 64 bit 整数 と表記) を保持することが出来る。
プログラムの実行開始時点で、A には整数 a が、B には整数 b が、それ以外の変数には 0 が代入されている。
プログラムの実行終了時点で変数 Ca \times b \bmod{1000000007} が保持されている必要がある。

プログラムの形式

プログラムの 1 行目にはプログラムの命令数を表す整数 N (1 \leq N \leq 100) が書かれる。
プログラムの 2 行目から N + 1 行目には N 個の命令が書かれる。命令は上から下に順次実行される。
命令は次の 3 つのいずれかである。

  • add x y z
    • x(y + z) \bmod{2^{64}} を代入する。ここでは x は変数、y, z は変数または符号無し 64 bit 整数である。
  • mul x y z
    • xy \times z \bmod{2^{64}} を代入する。ここでは x は変数、y, z は変数または符号無し 64 bit 整数である。
  • rem x y
    • xy \bmod{998244353} を代入する。ここでは x は変数、y は変数または符号無し 64 bit 整数である。

入力

標準入力から与えられる入力は空である。

出力

問題文に書かれている仕様・形式に従ったプログラムを出力せよ。

ジャッジ

提出されたプログラムの形式が誤っていた場合、ジャッジの判定は不定である。
提出されたプログラムの形式が正しい場合、ジャッジは 1 ケース毎に 10^4 個の整数の組 (a, b) (0 \leq a, b \leq 1000000006) に対してプログラムを独立に実行する。(整数の組はジャッジ側があらかじめ用意したものであり、テストケース毎に固定である。)
全ての (a, b) の組に対して実行終了時に変数 Ca \times b \bmod{1000000007} が保持されている場合、ジャッジの判定は AC となる。そうでない場合は WA となる。

出力例

正しい形式で書かれたプログラムの例を示します。(このプログラムは仕様を満たしていないため、提出しても WA となります。)

5
mul C A B
rem C C
add A A 10
add D 2 B
add E 1 0

このプログラムの実行終了時点で各変数に代入されている値は次の通りです。

  • A : a + 10
  • B : b
  • C : a \times b \bmod{998244353}
  • D : b + 2
  • E : 1
  • それ以外の変数 : 0

Score : 1000 points

Problem Statement

This problem is output-only.

We have a programming language equipped with the following operations of unsigned 64-bit integers: addition, multiplication, and a modulo operation where the divisor is 998244353.
Write a program that performs multiplication modulo 1000000007 in this language.

More formally, write a program that receives integers a and b between 0 and 1000000006 and computes a \times b \bmod{1000000007} under the following Specification and Format.

Specification

The program can handle 26 variables represented by uppercase English letters: A, B, \dots, Z.
Each variable can hold an integer value between 0 and 2^{64}-1 (inclusive). Below, such a value is called unsigned 64-bit integer.
At the start of the execution of the program, A is assigned an integer a, B is assigned an integer b, and the other variables are assigned 0.
At the end of the execution, C must hold a \times b \bmod{1000000007}.

Format

The 1-st line of the program contains an integer N (1 \leq N \leq 100) representing the number of instructions in the program.
The 2-nd through (N + 1)-th lines contain N instructions. The instructions are executed one by one from top to bottom.
Each instruction is in one of the following three forms.

  • add x y z
    • Assign (y + z) \bmod{2^{64}} to x, where x is a variable, and each of y and z is a variable or an unsigned 64-bit integer.
  • mul x y z
    • Assign (y \times z) \bmod{2^{64}} to x, where x is a variable, and each of y and z is a variable or an unsigned 64-bit integer.
  • rem x y
    • Assign y \bmod{998244353} to x, where x is a variable, and y is a variable or an unsigned 64-bit integer.

Input

The input given from Standard Input is empty.

Output

Print a program under the Specification and Format.

Judging

If the submitted program is malformed, the verdict will be indeterminate.
If the submitted program is well-formed, for each test case, the judge will execute it against 10^4 pairs of integers (a, b) (0 \leq a, b \leq 1000000006) independently. (These pairs are prepared beforehand and constant for each test case.)
If the variable C holds a \times b \bmod{1000000007} at the end of the execution for all pairs (a, b), the verdict will be AC; otherwise, it will be WA.

Sample Output

Here is an example of a well-formed program. (The Specification is not satisfied, so it will be judged as WA if submitted.)

5
mul C A B
rem C C
add A A 10
add D 2 B
add E 1 0

At the end of the execution of this program, the variables will hold the following values.

  • A: a + 10
  • B: b
  • C: a \times b \bmod{998244353}
  • D: b + 2
  • E: 1
  • The others: 0