A - Divide String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の英小文字からなる文字列 S が与えられます。S2 個以上の連続部分文字列に分割し、それらが辞書順で狭義単調増加になるようにすることが出来るか判定してください。

厳密に書くと、以下の条件を全て満たす文字列の列 t=(t_1,t_2,\dots,t_k) が存在するか判定してください。

  • 列の長さ k2 以上である。

  • t_i は空でない。(1 \le i \le k)

  • t_1,t_2,\dots,t_k をこの順で連結すると S と一致する。

  • 1 \le i < k を満たす整数 i に対して、t_it_{i+1} より辞書順で小さい。

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

辞書順とは?

文字列 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 \le T \le 2000
  • 2 \le N \le 2000
  • S は長さ N の英小文字からなる文字列
  • 1 個の入力に含まれるテストケースについて、それらの N の総和は 2000 を超えない。

入力

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

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

ここで、\mathrm{case}_i とは i 個目のテストケースである。各テストケースは以下の形式で与えられる。

N
S

出力

T 行出力せよ。

i(1 \le i \le T) 行目には、i 個目のテストケースにおいて S を条件を満たすように分割できるならば Yes を、そうでないならば No を出力せよ。


入力例 1

5
4
abac
3
cac
2
ab
12
abababababab
5
edcba

出力例 1

Yes
No
Yes
Yes
No

1 個目のテストケースは、Sa,ba,c と分割すればよいです。

2 個目のテストケースは、S をどのように分割しても辞書順で狭義単調増加にすることは出来ません。

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters. Determine whether it is possible to divide S into two or more consecutive substrings so that they are strictly increasing in lexicographical order.

To be precise, determine whether there is a sequence of strings t=(t_1,t_2,\dots,t_k) that satisfies all of the following conditions.

  • The length of the sequence k is at least 2.

  • t_i is not empty. (1 \le i \le k)

  • Concatenating t_1,t_2,\dots,t_k in this order results in S.

  • t_i is lexicographically smaller than t_{i+1} for every integer i such that 1 \le i < k.

You are given T test cases. Find the answer for each of them.

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 either 1. or 2. below holds. Here, |S| and |T| represent 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 such that both of the following hold.
    • S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}.
    • The character S_i comes before T_i in alphabetical order.

Constraints

  • 1 \le T \le 2000
  • 2 \le N \le 2000
  • S is a string of length N consisting of lowercase English letters.
  • The sum of N over all test cases in a single input does not exceed 2000.

Input

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

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

Here, \mathrm{case}_i represents the i-th test case. Each test case is given in the following format:

N
S

Output

Print T lines.

The i-th line (1 \le i \le T) should contain Yes if it is possible to divide S in the i-th test case into substrings that satisfy the conditions, and No otherwise.


Sample Input 1

5
4
abac
3
cac
2
ab
12
abababababab
5
edcba

Sample Output 1

Yes
No
Yes
Yes
No

For the first test case, you can divide S into a, ba, c.

For the second test case, there is no way to divide S into substrings so that they are strictly increasing in lexicographical order.

B - Favorite Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。あなたは、以下の操作を好きな回数(0 回でもよい)行うことが出来ます。

  • 1 \le i \le N を満たす整数 i1 個選び、A_i1 増やすか 1 減らす。

あなたの目標は、A_1 \le A_i \le A_2 を満たす整数 i(3 \le i \le N) の個数を M 個以上にすることです。目標を達成するために必要な最小の操作回数を求めてください。

制約

  • 3 \le N \le 2 \times 10^5
  • 1 \le M \le N-2
  • 1 \le A_i \le 10^9

入力

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

N M
A_1 A_2 \dots A_N

出力

必要な操作回数の最小値を出力せよ。


入力例 1

3 1
2 3 5

出力例 1

2

以下のように操作を行うことで A_1 \le A_i \le A_2 を満たす整数 i(3 \le i \le N) の個数を 1 個以上に出来ます。

  • i=3 を選び、A_i1 減らす。
  • i=2 を選び、A_i1 増やす。

1 回以下の操作回数で目標を達成することは出来ないため、答えは 2 です。


入力例 2

5 2
1 4 2 3 5

出力例 2

0

始めから目標を達成していることもあります。


入力例 3

8 5
15 59 64 96 31 17 88 9

出力例 3

35

Score : 400 points

Problem Statement

You are given an integer sequence of length N: A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).

  • Choose an integer i such that 1 \le i \le N, and increase or decrease A_i by 1.

Your goal is to make at least M integers i(3 \le i \le N) satisfy A_1 \le A_i \le A_2. Find the minimum number of operations required to achieve this goal.

Constraints

  • 3 \le N \le 2 \times 10^5
  • 1 \le M \le N-2
  • 1 \le A_i \le 10^9

Input

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

N M
A_1 A_2 \dots A_N

Output

Print the minimum number of operations required.


Sample Input 1

3 1
2 3 5

Sample Output 1

2

You can make not less than one integer i(3 \le i \le N) satisfy A_1 \le A_i \le A_2 by performing the operation as follows.

  • Choose i=3, and decrease A_i by 1.
  • Choose i=2, and increase A_i by 1.

Since it is impossible to achieve the goal with less than 2 operation, the answer is 2.


Sample Input 2

5 2
1 4 2 3 5

Sample Output 2

0

You may already have achieved the goal from the start.


Sample Input 3

8 5
15 59 64 96 31 17 88 9

Sample Output 3

35
C - Harmonic Mean

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

以下の条件を全て満たす長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が存在するか判定し、存在するならば一つ構築してください。

  • \sum_{i=1}^{N} \frac{1}{A_i} = 1
  • A の要素は全て相異なる。
  • 1 \le A_i \le 10^9(1 \le i \le N)

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

制約

  • 1 \le T \le 500
  • 1 \le N \le 500

入力

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

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

ここで、\mathrm{case}_i とは i 個目のテストケースである。各テストケースは以下の形式で与えられる。

N

出力

それぞれのケースについて、条件を満たす正整数列 A=(A_1,A_2,\dots,A_N) が存在しないならば No を出力せよ。存在するならば、以下の形式で出力せよ。

Yes
A_1 A_2 \dots A_N

条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

2
3
5

出力例 1

Yes
2 3 6 
Yes
3 4 5 6 20 

1 個目のテストケースでは、N=3 です。

A=(2,3,6) は、\frac{1}{2} + \frac{1}{3} + \frac{1}{6} = 1 かつ他の条件も全て満たすため正当です。

2 個目のテストケースでは、N=5 です。

A=(3,4,5,6,20) は、\frac{1}{3} + \frac{1}{4} + \frac{1}{5} + \frac{1}{6} + \frac{1}{20} = 1 かつ他の条件も全て満たすため正当です。

例えば、A=(5,5,5,5,5) は、1,3 個目の条件を満たしていますが同じ要素が存在するため不適であることに注意してください。

Score : 600 points

Problem Statement

Determine whether there is a length-N sequence of positive integers A=(A_1,A_2,\dots,A_N) that satisfies all of the following conditions, and if it exists, construct one.

  • \sum_{i=1}^{N} \frac{1}{A_i} = 1
  • All elements of A are distinct.
  • 1 \le A_i \le 10^9(1 \le i \le N)

You are given T test cases. Find the answer for each of them.

Constraints

  • 1 \le T \le 500
  • 1 \le N \le 500

Input

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

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

Here, \mathrm{case}_i is the i-th test case. Each test case is given in the following format:

N

Output

For each case, if there is not a sequence of positive integers A=(A_1,A_2,\dots,A_N) that satisfies the conditions, print No. If there is, print one in the following format:

Yes
A_1 A_2 \dots A_N

If there are multiple valid solutions, any of them will be accepted.


Sample Input 1

2
3
5

Sample Output 1

Yes
2 3 6 
Yes
3 4 5 6 20 

In the first test case, N=3.

A=(2,3,6) is valid because \frac{1}{2} + \frac{1}{3} + \frac{1}{6} = 1 and it satisfies all other conditions.

In the second test case, N=5.

A=(3,4,5,6,20) is valid because \frac{1}{3} + \frac{1}{4} + \frac{1}{5} + \frac{1}{6} + \frac{1}{20} = 1 and it satisfies all other conditions.

Note that, for example, A=(5,5,5,5,5) satisfies the first and third conditions, but it is invalid because it contains duplicated elements.

D - Sum of SCC

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700

問題文

以下の条件を全て満たす頂点に 1 から N までの番号がついた N 頂点の有向グラフ G を考えます。

  • G はトーナメントである。すなわち、G に多重辺や自己ループはなく、G のどの 2 頂点 u,v に対しても、u \rightarrow v 辺または v \rightarrow u 辺のうちちょうど片方が存在する。

  • G の辺のうち、頂点番号が小さい方から大きい方へ向けられた辺はちょうど M 本存在する。

そのような有向グラフ G 全てに対する強連結成分の個数の総和を 998244353 で割ったあまりを求めてください。

制約

  • 1 \le N \le 30
  • 0 \le M \le \frac{N(N-1)}{2}

入力

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

N M

出力

答えを出力せよ。


入力例 1

3 1

出力例 1

7

条件を満たす有向グラフ G は以下の 3 個です。それぞれ強連結成分の個数は 3,1,3 であるため答えは 7 です。


入力例 2

6 2

出力例 2

300

入力例 3

25 156

出力例 3

902739687

Score : 700 points

Problem Statement

Consider a directed graph G with N vertices numbered 1 to N that satisfies all of the following conditions.

  • G is a tournament. In other words, G has no multi-edges or self-loops, and for any two vertices u,v of G, exactly one of the edges u \rightarrow v and v \rightarrow u exists.

  • Among the edges of G, exactly M are directed from a vertex with a smaller number to a vertex with a larger number.

Find the total number of strongly connected components over all such directed graphs G, modulo 998244353.

Constraints

  • 1 \le N \le 30
  • 0 \le M \le \frac{N(N-1)}{2}

Input

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

N M

Output

Print the answer.


Sample Input 1

3 1

Sample Output 1

7

Below are the three directed graphs G that satisfy the conditions. The numbers of their strongly connected components are 3,1,3 from left to right, so the answer is 7.


Sample Input 2

6 2

Sample Output 2

300

Sample Input 3

25 156

Sample Output 3

902739687
E - Chmin XOR Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 900

問題文

長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) を用いて Alice と Bob はゲームをします。

Alice から以下の操作を交互に行います。先に操作が出来なくなった方が負けです。

  • A_i > A_i \oplus X を満たす整数 i が存在するような非負整数 X を選ぶ。
  • 1 \le i \le N に対して A_i\min(A_i,A_i \oplus X) で置き換える。

両者が勝つために最善な行動をしたとき、勝つのがどちらか判定してください。

ただしここで、\oplus はビットごとの排他的論理和を表します。

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

制約

  • 1 \le T \le 100
  • 1 \le N \le 100
  • 0 \le A_i \le 10^9

入力

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

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

ここで、\mathrm{case}_i とは i 個目のテストケースである。各テストケースは以下の形式で与えられる。

N
A_1 A_2 \dots A_N

出力

T 行出力せよ。

i(1 \le i \le T) 行目には、i 個目のテストケースにおいて Alice が勝つならば Alice、Bob が勝つならば Bob を出力せよ。


入力例 1

5
2
3 1
5
1 1 1 1 1
4
0 0 0 0
4
8 1 6 4
5
3 8 7 12 15

出力例 1

Bob
Alice
Bob
Bob
Alice

1 個目のテストケースは、あり得るゲームの進行として以下のようなものが考えられます。

  • Alice が X=3 を選ぶ。i=1 において 3 > 3 \oplus 3(=0) であるためこの選択は有効である。
  • A=(3,1) から A=(0,1) となる。
  • Bob が X=1 を選ぶ。i=2 において 1 > 1 \oplus 1(=0) であるためこの選択は有効である。
  • A=(0,1) から A=(0,0) となる。
  • Alice が選べる X が存在しないため、ゲームを終了する。

この場合 Bob が勝ちます。

2 個目のテストケースは、あり得るゲームの進行として以下のようなものが考えられます。

  • Alice が X=1 を選ぶ。i=1 において 1 > 1 \oplus 1(=0) であるためこの選択は有効である。
  • A=(1,1,1,1,1) から A=(0,0,0,0,0) となる。
  • Bob が選べる X が存在しないため、ゲームを終了する。

この場合 Alice が勝ちます。

Score : 900 points

Problem Statement

Alice and Bob are playing a game using a length-N sequence of non-negative integers A=(A_1,A_2,\dots,A_N).

Starting with Alice, they take turns performing the following operation. The player who cannot make a move first loses.

  • Choose a non-negative integer X such that there is an integer i satisfying A_i > A_i \oplus X.
  • For each 1 \le i \le N, replace A_i with \min(A_i,A_i \oplus X).

Determine who wins when both players play optimally.

Here, \oplus represents the bitwise XOR.

You are given T test cases. Find the answer for each of them.

Constraints

  • 1 \le T \le 100
  • 1 \le N \le 100
  • 0 \le A_i \le 10^9

Input

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

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

Here, \mathrm{case}_i is the i-th test case. Each test case is given in the following format:

N
A_1 A_2 \dots A_N

Output

Print T lines.

The i-th line (1 \le i \le T) should contain Alice if Alice wins, and Bob if Bob wins in the i-th test case.


Sample Input 1

5
2
3 1
5
1 1 1 1 1
4
0 0 0 0
4
8 1 6 4
5
3 8 7 12 15

Sample Output 1

Bob
Alice
Bob
Bob
Alice

In the first test case, one possible game progression could be as follows.

  • Alice chooses X=3. This choice is valid because 3 > 3 \oplus 3(=0) for i=1.
  • A=(3,1) becomes A=(0,1).
  • Bob chooses X=1. This choice is valid because 1 > 1 \oplus 1(=0) for i=2.
  • A=(0,1) becomes A=(0,0).
  • Since Alice cannot choose any X, the game ends.

In this case, Bob wins.

In the second test case, one possible game progression could be as follows.

  • Alice chooses X=1. This choice is valid because 1 > 1 \oplus 1(=0) for i=1.
  • A=(1,1,1,1,1) becomes A=(0,0,0,0,0).
  • Since Bob cannot choose any X, the game ends.

In this case, Alice wins.

F - Many Increasing Problems

Time Limit: 5 sec / Memory Limit: 1024 MiB

配点 : 1100

問題文

PCT 君は以下の問題を作りました。

Increasing Problem

長さ N の非負整数列 A_1,A_2,\dots,A_N が与えられます。あなたは以下の操作を好きな回数(0 回でもよい)行うことが出来ます。

  • 1 \le i \le N を満たす整数 i1 個選び、A_i1 増やすか 1 減らす。

あなたの目標は A を広義単調増加にすることです。目標を達成するために必要な最小の操作回数を求めてください。

この問題がコンテストの最後に置くには簡単だと考えた PCT 君は、以下のように改題しました。

Many Increasing Problems

長さ N かつ全ての要素が 1 以上 M 以下であるような整数列 AM^N 個ありますが、その全てに対する Increasing Problem の答えの総和を 998244353 で割ったあまりを求めてください。

Many Increasing Problems を解いてください。

制約

  • 1 \le N,M \le 10^5

入力

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

N M

出力

Many Increasing Problems の答えを出力せよ。


入力例 1

2 2

出力例 1

1

長さが 2 かつ全ての要素が 1 以上 2 以下である数列全てに対して Increasing Problem を解きます。

  • A=(1,1) の時の解は 0
  • A=(1,2) の時の解は 0
  • A=(2,1) の時の解は 1
  • A=(2,2) の時の解は 0

よって、答えは 0+0+1+0=1 です。


入力例 2

6 4

出力例 2

14668

入力例 3

163 702

出力例 3

20728656

入力例 4

98765 99887

出力例 4

103564942

Score : 1100 points

Problem Statement

PCT-kun created the following problem.

Increasing Problem

You are given a length-N sequence of non-negative integers A_1,A_2,\dots,A_N. You can perform the following operation any number of times (possibly zero).

  • Choose an integer i such that 1 \le i \le N, and increase or decrease A_i by 1.

Your goal is to make A non-decreasing. Find the minimum number of operations required to achieve this goal.

Thinking that this problem is too easy to be placed at the end of the contest, PCT-kun has revised it as follows.

Many Increasing Problems

There are M^N integer sequences A of length N where all elements are between 1 and M, inclusive. Find the sum of the answers to Increasing Problem for all those sequences, modulo 998244353.

Solve Many Increasing Problems.

Constraints

  • 1 \le N,M \le 10^5

Input

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

N M

Output

Print the answer to Many Increasing Problems.


Sample Input 1

2 2

Sample Output 1

1

Let us solve Increasing Problem for all sequences of length 2 where all elements are between 1 and 2, inclusive.

  • For A=(1,1), the answer is 0.
  • For A=(1,2), the answer is 0.
  • For A=(2,1), the answer is 1.
  • For A=(2,2), the answer is 0.

Therefore, the final answer is 0+0+1+0=1.


Sample Input 2

6 4

Sample Output 2

14668

Sample Input 3

163 702

Sample Output 3

20728656

Sample Input 4

98765 99887

Sample Output 4

103564942