A - Continuous 1

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

0, 1, ? のみからなる長さ N の文字列 S=S_1S_2\dots S_N が与えられます。

これから S に含まれるすべての ?0, 1 のいずれかに置き換えることで、以下の条件がすべて満たされるようにしたいです。

  • S1 をちょうど K 個含む。
  • K 個の 1 は連続している。すなわち、ある i\ (1 \leq i \le N-K+1) があって、S_i=S_{i+1}=\dots=S_{i+K-1}= 1 が成り立つ。

条件を満たすような ? の置き換え方がちょうど 1 通りであるか判定してください。

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

制約

  • 1 \leq T \leq 10^5
  • 1 \leq K < N \leq 3 \times 10^5
  • S0, 1, ? のみからなる長さ N の文字列
  • 全テストケースに対する N の総和は 3 \times 10^5 以下

入力

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

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

各ケースは以下の形式で与えられます。

N K
S

出力

T 行出力してください。i 行目には i 番目のテストケースについて、条件を満たすような ? の置き換え方がちょうど 1 通りである場合は Yes を、そうでない場合は No を出力してください。


入力例 1

4
3 2
1??
4 2
?1?0
6 3
011?1?
10 5
00?1???10?

出力例 1

Yes
No
No
Yes

1 個目のテストケースについて、例えば S101 に変えることができますが、この場合は 1 が連続していないため、条件を満たしません。 S が条件を満たすようにするには S110 に変えるしかありません。

2 個目のテストケースについて、 S が条件を満たすようにするには S1100, または 0110 に変えればよく、条件を満たすような ? の置き換え方は 2 通りあります。

3 個目のテストケースについて、S が条件を満たすよう ? を置き換える方法は存在しません。

Score : 300 points

Problem Statement

You are given a string of length N, S=S_1S_2\dots S_N, consisting of 0, 1, and ?.

We like to replace every ? with 0 or 1 so that all of the following conditions are satisfied.

  • S contains exactly K occurrences of 1.
  • These K occurrences of 1 are consecutive. That is, there is an i\ (1 \leq i \le N-K+1) such that S_i=S_{i+1}=\dots=S_{i+K-1}= 1.

Determine whether there is exactly one way to replace the characters to satisfy the conditions.

You have T test cases to solve.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq K < N \leq 3 \times 10^5
  • S is a string of length N consisting of 0, 1, and ?.
  • The sum of N across the test cases is at most 3 \times 10^5.

Input

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

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

Each case is in the following format:

N K
S

Output

Print T lines. The i-th line should contain Yes if, for the i-th test case, there is exactly one way to replace the characters to satisfy the conditions, and No otherwise.


Sample Input 1

4
3 2
1??
4 2
?1?0
6 3
011?1?
10 5
00?1???10?

Sample Output 1

Yes
No
No
Yes

For the first test case, turning S into 101, for instance, does not satisfy the conditions since the 1s will not be consecutive. The only way to satisfy the conditions is to turn S into 110.

For the second test case, we may turn S into 1100 or 0110 to satisfy the conditions, so there are two ways to satisfy them.

For the third test case, there is no way to replace the characters to satisfy the conditions.

B - Make Divisible

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

正整数 A,\ B が与えられます。

非負整数 X,\ Y であって、 B+YA+X の倍数となるようなものに対する X+Y の最小値を求めてください。

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

制約

  • 1 \leq T \leq 100
  • 1 \leq A,\ B \leq 10^9
  • 入力される値はすべて整数

入力

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

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

各ケースは以下の形式で与えられます。

A B

出力

T 行出力してください。 i 行目には i 番目のテストケースに対する答えを出力してください。


入力例 1

5
11 23
8 16
4394 993298361
95392025 569922442
8399283 10293

出力例 1

2
0
65
2429708
8388990

1 個目のテストケースについて、X=1,\ Y=1 とすると B+Y=24A+X=12 の倍数になります。このとき X+Y=2 であり、X+Y はこれより小さくすることはできないので答えは 2 です。

2 個目のテストケースについて、X=0,\ Y=0 とすると B+Y=16A+X=8 の倍数になります。

Score : 500 points

Problem Statement

You are given positive integers A and B.

Find the minimum value of X+Y for non-negative integers X and Y such that B+Y is a multiple of A+X.

You have T test cases to solve.

Constraints

  • 1 \leq T \leq 100
  • 1 \leq A,\ B \leq 10^9
  • All values in the input are integers.

Input

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

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

Each case is in the following format:

A B

Output

Print T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
11 23
8 16
4394 993298361
95392025 569922442
8399283 10293

Sample Output 1

2
0
65
2429708
8388990

For the first test case, if we let X=1 and Y=1, then B+Y=24 will be a multiple of A+X=12. Here, we have X+Y=2, and there is no way to make X+Y smaller, so the answer is 2.

For the second test case, if we let X=0 and Y=0, then B+Y=16 will be a multiple of A+X=8.

C - Path and Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の連結無向グラフ G があります。頂点には 1 から N の番号がついています。i 番目の辺は頂点 U_i,\ V_i を結びます。

また、長さ N の整数列 A=(A_1,\ A_2, \dots,\ A_N) 、および長さ K の整数列 B=(B_1,\ B_2,\ \dots,\ B_K) が与えられます。

G,\ A,\ B が以下の条件を満たすか判定してください。

  • G における頂点 1 から N への任意の単純パス v=(v_1,\ v_2, \dots,\ v_k)\ (v_1=1,\ v_k=N) に対し、B(A_{v_1},\ A_{v_2},\ \dots,\ A_{v_k}) の(連続とは限らない)部分列になる。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq K \leq N
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_i < V_i \leq N
  • i \neq j ならば (U_i,\ V_i) \neq (U_j,\ V_j)
  • 1 \leq A_i,\ B_i \leq N
  • 入力される値はすべて整数
  • 与えられるグラフ G は連結

入力

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

N M K
U_1 V_1
U_2 V_2
\vdots
U_M V_M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_K

出力

条件を満たす場合 Yes と、満たさない場合 No と出力してください。


入力例 1

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

出力例 1

Yes

頂点 1 から頂点 6 への単純パスは (1,\ 2,\ 4,\ 6),\ (1,\ 3,\ 5,\ 6)2 通りであり、これらに対する (A_{v_1},\ A_{v_2},\ \dots,\ A_{v_k}) はそれぞれ (1,\ 2,\ 5,\ 6),\ (1,\ 4,\ 2,\ 6) です。 これらはいずれも B=(1,\ 2,\ 6) を部分列に持つので答えは Yes です。


入力例 2

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

出力例 2

No

頂点 1 から頂点 5 への単純パスである (1,\ 2,\ 5) に対する (A_{v_1},\ A_{v_2},\ \dots,\ A_{v_k})(1,\ 2,\ 2) であり、これは B=(1,\ 3,\ 2) を部分列に持ちません。


入力例 3

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

出力例 3

Yes

Score : 500 points

Problem Statement

We have a connected undirected graph G with N vertices and M edges. The vertices are numbered 1 to N. The i-th edge connects vertices U_i and V_i.

Additionally, we are given an integer sequence of length N, A=(A_1,\ A_2, \dots,\ A_N), and an integer sequence of length K, B=(B_1,\ B_2,\ \dots,\ B_K).

Determine whether G, A, and B satisfy the following condition.

  • For every simple path from vertex 1 to N in G, v=(v_1,\ v_2, \dots,\ v_k)\ (v_1=1,\ v_k=N), B is a (not necessarily contiguous) subsequence of (A_{v_1},\ A_{v_2},\ \dots,\ A_{v_k}).

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq K \leq N
  • N-1 \leq M \leq 2 \times 10^5
  • 1 \leq U_i < V_i \leq N
  • (U_i,\ V_i) \neq (U_j,\ V_j) if i \neq j.
  • 1 \leq A_i,\ B_i \leq N
  • All values in the input are integers.
  • The given graph G is connected.

Input

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

N M K
U_1 V_1
U_2 V_2
\vdots
U_M V_M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_K

Output

If the condition is satisfied, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

There are two simple paths from vertex 1 to vertex 6: (1,\ 2,\ 4,\ 6) and (1,\ 3,\ 5,\ 6). The (A_{v_1},\ A_{v_2},\ \dots,\ A_{v_k}) corresponding to these paths are (1,\ 2,\ 5,\ 6) and (1,\ 4,\ 2,\ 6). Both of them have B=(1,\ 2,\ 6) as a subsequence, so the answer is Yes.


Sample Input 2

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

Sample Output 2

No

For a simple path (1,\ 2,\ 5) from vertex 1 to vertex 5, the (A_{v_1},\ A_{v_2},\ \dots,\ A_{v_k}) is (1,\ 2,\ 2), which does not have B=(1,\ 3,\ 2) as a subsequence.


Sample Input 3

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

Sample Output 3

Yes
D - Removing Gacha

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

頂点に 1 から N の番号がついた N 頂点の根付き木があります。頂点 1 はこの木の根であり、頂点 i\ (2\leq i) の親頂点は頂点 p_i です。

各頂点は白、黒の色を持っています。はじめすべて頂点の色は白です。

根付き木において、頂点 1,\ i を結ぶ唯一の単純パス上の頂点 (頂点 1,\ i 含む) の色がすべて黒であるとき、頂点 i を「よい頂点」といいます。また、「よい頂点」ではない頂点を「わるい頂点」といいます。

すべての頂点の色が黒になるまで「『わるい頂点』から一様ランダムに頂点を 1 つ選び、その頂点を黒色で上塗りする」という操作を行います。

操作を行う回数の期待値を \bmod\ 998244353 で求めてください。

期待値 \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 を答えてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq p_i < i
  • 入力される値はすべて整数

入力

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

N
p_2 p_3 \dots p_{N}

出力

答えを出力してください。


入力例 1

4
1 1 3

出力例 1

831870300

例えば 1,\ 2,\ 3 回目の操作で順に頂点 1,\ 2,\ 4 が選ばれた場合を考えます。このとき、頂点 1,\ 2 は「よい頂点」ですが、頂点 4 は祖先である頂点 3 が白色であるため「わるい頂点」です。よって 4 回目の操作で頂点を選ぶ際は頂点 3,\ 4 の中から一様ランダムに選びます。

操作を行う回数の期待値は \displaystyle \frac{35}{6} になります。


入力例 2

15
1 2 1 1 4 5 3 3 5 10 3 6 3 13

出力例 2

515759610

Score : 700 points

Problem Statement

We have a rooted tree with N vertices numbered 1 to N. Vertex 1 is the root of the tree, and the parent of vertex i\ (2\leq i) is vertex p_i.

Each vertex has a color: black or white. Initially, all vertices are white.

In this rooted tree, vertex i is said to be good when all vertices on the simple path connecting vertices 1 and i (including vertices 1 and i) are black, and bad otherwise.

Until all vertices are black, let us repeat the following operation: choose one vertex from the bad vertices uniformly at random, and repaint the chosen vertex black.

Find the expected value of the number of times we perform the operation, modulo 998244353.

The definition of the expected value modulo 998244353

It can be proved that the sought expected value is always a rational number. Additionally, when that value is represented as an irreducible fraction \frac{P}{Q}, it can also be proved that Q \not \equiv 0 \pmod{998244353}. Therefore, there is a unique integer R such that R \times Q \equiv P \pmod{998244353} and 0 \leq R < 998244353. Find this R.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq p_i < i
  • All values in the input are integers.

Input

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

N
p_2 p_3 \dots p_{N}

Output

Print the answer.


Sample Input 1

4
1 1 3

Sample Output 1

831870300

Consider a case where the first three operations have chosen vertices 1, 2, and 4 in this order. Then, vertices 1 and 2 are good, but vertex 4 is still bad since vertex 3, an ancestor of vertex 4, is white. Thus, the fourth operation will choose vertex 3 or 4 uniformly at random.

The expected value of the number of times we perform the operation is \displaystyle \frac{35}{6}.


Sample Input 2

15
1 2 1 1 4 5 3 3 5 10 3 6 3 13

Sample Output 2

515759610
E - Weathercock

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

NK 人の人が横一列に並んでいます。左から順に人 i\ (0\leq i \leq NK-1) と表記します。

各人は常に左右どちらかの方向を向いています。時刻 t=0 において各人がどちらを向いているかは、L,R のみからなる長さ N の文字列 S=S_0 S_1 \dots S_{N-1} で表されます。時刻 t=0 において人 iS_{i \bmod N}L のとき左を、 R のとき右を向いています。

これらの人は時刻 t=0.5,\ 1.5,\ 2.5 ,\ \dots において以下の規則に従って向いている方向を同時に変化させます。

  • その時点で左を向いている場合
    自分が向いている方向に人が存在し、その中で過半数の人が右を向いている場合、向いている方向を右に変える。そうでない場合向いている方向を変えない。
  • その時点で右を向いている場合
    自分が向いている方向に人が存在し、その中で過半数の人が左を向いている場合、向いている方向を左に変える。そうでない場合向いている方向を変えない。

時刻 t=0 から t=10^{100} までの間に、NK 人それぞれが向いている方向を変える回数の総和を 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • SL,R のみからなる長さ N の文字列
  • 入力される数値はすべて整数

入力

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

N K
S

出力

答えを出力してください。


入力例 1

7 1
RRLRLLL

出力例 1

9

各時刻において 7 人が向いている方向を文字列で表すと t=1 では LLRLRRLt=2 では LLRLRLLt=3 では LLLLLLL となります。

時刻 t=3 以降では 7 人が向いている方向は変化しません。よって答えは 1+1+2+1+2+2+0=9 になります。


入力例 2

4 10
LLRR

出力例 2

0

入力例 3

23 200
RLRRRLLLLLLLLRRRLLRLRRR

出力例 3

2207

Score : 800 points

Problem Statement

NK people are standing in a line from left to right. Let us denote them as person i (0\leq i \leq NK-1) from left to right.

Each person is always facing either left or right. The direction of each person at time t=0 is represented by a string of length N, S=S_0 S_1 \dots S_{N-1}, consisting of L and R. At time t=0, person i is facing left if S_{i \bmod N} is L, and right if it is R.

At time t=0.5,\ 1.5,\ 2.5 ,\ \dots, these people simultaneously change their directions according to the following rules.

  • When a person is facing left:
    If there are one or more people in the direction the person is facing, and more than half of those people are facing right, the person changes direction and faces right. Otherwise, the person does not change direction.
  • When a person is facing right:
    If there are one or more people in the direction the person is facing, and more than half of those people are facing left, the person changes direction and faces left. Otherwise, the person does not change direction.

Find the sum, modulo 998244353, of the numbers of times the NK people change direction from time t=0 to t=10^{100}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 2 \times 10^5
  • S is a string of length N consisting of L and R.
  • All numerical values in the input are integers.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

7 1
RRLRLLL

Sample Output 1

9

If we represent the directions of the seven people at each time, we have LLRLRRL at t=1, LLRLRLL at t=2, and LLLLLLL at t=3.

After t=3, the directions of the seven people do not change. Therefore, the answer is 1+1+2+1+2+2+0=9.


Sample Input 2

4 10
LLRR

Sample Output 2

0

Sample Input 3

23 200
RLRRRLLLLLLLLRRRLLRLRRR

Sample Output 3

2207
F - Constant Sum Subsequence

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

長さが N^2 の正整数列 A=(A_1,\ A_2,\ \dots,\ A_{N^2}) と正整数 S があります。この正整数列については正整数 i\ (1\leq i \leq N^2-N) に対し A_i=A_{i+N} が成り立ち、A_1,\ A_2,\ \dots,\ A_N のみが入力で与えられます。

総和が S であるような任意の正整数列 B が正整数列 (A_1,\ A_2,\ \dots,\ A_L) の(連続とは限らない)部分列となるような最小の整数 L を求めてください。

この問題の制約のもとでそのような L1 つ以上存在することが示せます。

制約

  • 1 \leq N \leq 1.5 \times 10^6
  • 1 \leq S \leq \min(N,2 \times 10^5)
  • 1 \leq A_i \leq S
  • 任意の正整数 x\ (1\leq x \leq S) について、(A_1,\ A_2,\ \dots,\ A_N)x1 つ以上含む
  • 入力される値はすべて整数

入力

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

N S
A_1 A_2 \dots A_N

出力

答えを出力してください。


入力例 1

6 4
1 1 2 1 4 3

出力例 1

9

B として考えるべきなのは B=(1,\ 1,\ 1,\ 1),\ (1,\ 1,\ 2),\ (1,\ 2,\ 1),\ (1,\ 3),\ (2,\ 1,\ 1),\ (2,\ 2),\ (3,\ 1),\ (4) です。

例えば L=8 とすると B=(2,\ 2)(A_1,A_2,\ \dots,\ A_8)=(1,\ 1,\ 2,\ 1,\ 4,\ 3,\ 1,\ 1) の部分列となりません。

一方で L=9 とするとすべての B(A_1,A_2,\ \dots,\ A_9)=(1,\ 1,\ 2,\ 1,\ 4,\ 3,\ 1,\ 1,\ 2) の部分列となります。


入力例 2

14 5
1 1 1 2 3 1 2 4 5 1 1 2 3 1

出力例 2

11

入力例 3

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

出力例 3

39

Score : 1000 points

Problem Statement

We have a sequence of positive integers of length N^2, A=(A_1,\ A_2,\ \dots,\ A_{N^2}), and a positive integer S. For this sequence of positive integers, A_i=A_{i+N} holds for positive integers i\ (1\leq i \leq N^2-N), and only A_1,\ A_2,\ \dots,\ A_N are given as the input.

Find the minimum integer L such that every sequence B of positive integers totaling S is a (not necessarily contiguous) subsequence of the sequence (A_1,\ A_2,\ \dots,\ A_L) of positive integers.

It can be shown that at least one such L exists under the Constraints of this problem.

Constraints

  • 1 \leq N \leq 1.5 \times 10^6
  • 1 \leq S \leq \min(N,2 \times 10^5)
  • 1 \leq A_i \leq S
  • For every positive integer x\ (1\leq x \leq S), (A_1,\ A_2,\ \dots,\ A_N) contains at least one occurrence of x.
  • All values in the input are integers.

Input

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

N S
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

6 4
1 1 2 1 4 3

Sample Output 1

9

There are eight sequences B to consider: B=(1,\ 1,\ 1,\ 1),\ (1,\ 1,\ 2),\ (1,\ 2,\ 1),\ (1,\ 3),\ (2,\ 1,\ 1),\ (2,\ 2),\ (3,\ 1),\ (4).

For L=8, for instance, B=(2,\ 2) is not a subsequence of (A_1,A_2,\ \dots,\ A_8)=(1,\ 1,\ 2,\ 1,\ 4,\ 3,\ 1,\ 1).

For L=9, on the other hand, every B is a subsequence of (A_1,A_2,\ \dots,\ A_9)=(1,\ 1,\ 2,\ 1,\ 4,\ 3,\ 1,\ 1,\ 2).


Sample Input 2

14 5
1 1 1 2 3 1 2 4 5 1 1 2 3 1

Sample Output 2

11

Sample Input 3

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

Sample Output 3

39