A - Multiples in the String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

この問題は output-only です。入力は与えられません。

正整数 X と文字列 S の組 (X, S) であって以下の条件を全て満たすものを 1 つ挙げてください。

  • X10^{50} 以上 10^{5000} 未満の正整数である。
  • S0 から 9 までの数字からなる長さ 5000 以下の文字列である。
  • 1 \leq i \leq 1000 を満たす整数 i 全てに対して次の条件が成り立つ。
    • Xi 倍した整数を 10 進表記して出来る文字列は S の(連続な)部分文字列である。

入力

この問題では入力は与えられない。

出力

問題文の条件を満たす (X, S) を以下の形式で出力せよ。(条件を満たす (X, S) は少なくとも 1 組存在する。)
なお、この問題では X を leading-zeros を含んだ表現で出力してもよいが、その場合は文字列としての長さが 5000 文字以下となるように出力せよ。

X
S

出力例

問題文の条件を満たさない出力例を以下に挙げます。

5
105

この出力例は問題文の条件を満たしません。理由は次の通りです。

  • X の値の範囲は問題文の 1 番目の条件を満たさない。
  • i = 1, 2 において 5, 10S の部分文字列である。しかし、i=3 において 15S の部分文字列でない。よって (X, S) は問題文の 3 番目の条件を満たさない。

Score : 600 points

Problem Statement

This problem is output-only. No input is given.

Present one pair (X, S) of a positive integer X and a string S that satisfies all of the following conditions.

  • X is a positive integer at least 10^{50} and less than 10^{5000}.
  • S is a string of length at most 5000, consisting of digits from 0 to 9.
  • For every integer i such that 1 \leq i \leq 1000, the following condition is satisfied:
    • The decimal representation of X multiplied by i is a (contiguous) substring of S.

Input

No input is given for this problem.

Output

Print a pair (X, S) satisfying the conditions stated in the problem in the following format (there exists at least one such pair (X, S)).
You may include leading zeros when outputting X, but in that case, make sure that the string representation of X has length at most 5000.

X
S

Sample Output

Below is an example of output that does not satisfy the conditions in the problem statement.

5
105

This sample output does not satisfy the problem statement for the following reasons:

  • The value of X does not meet the first condition of the problem statement regarding its range.
  • For i = 1 and 2, 5 and 10 are substrings of S. However, for i = 3, 15 is not a substring of S. Thus, (X, S) fails to meet the third condition of the problem statement.
B - Odd Namori

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 1000

問題文

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

頂点に 1 から N までの番号がついた N 頂点の有向グラフ G のうち、次の条件を全て満たすものを 良いグラフ と呼びます。(ここで、頂点 u から頂点 v へ向かう辺を辺 u \to v と表します。)

  • G に含まれる全ての頂点の出次数は 1 である。
  • G は偶数長の閉路を持たない。
  • 2 \leq i \leq N を満たす i 全てに対して、G は辺 i \to p_i を含まない。

良いグラフである G 全てに対する 2^{(G に含まれる閉路の個数)} の総和を 998244353 で割った余りを求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq p_i \lt i
  • 入力で与えられる値は全て整数

入力

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

N
p_2 p_3 \dots p_N

出力

良いグラフである G 全てに対する 2^{(G に含まれる閉路の個数)} の総和を 998244353 で割った余りを出力せよ。


入力例 1

2
1

出力例 1

6

良いグラフとしてあり得るものは次の 2 個です。

  • 1 \to 1, 辺 2 \to 2 を辺集合に持つグラフ
  • 1 \to 2, 辺 2 \to 2 を辺集合に持つグラフ

グラフに含まれる閉路の個数はそれぞれ 2 個, 1 個です。よって答えは (2^2 + 2^1) \bmod{998244353} = 6 になります。


入力例 2

3
1 2

出力例 2

34

例えば 辺 1 \to 2, 辺 2 \to 3, 辺 3 \to 1 を辺集合に持つグラフは良いグラフで、このグラフに含まれる閉路の個数は 1 個です。


入力例 3

5
1 2 1 1

出力例 3

3104

入力例 4

20
1 2 2 2 5 3 5 1 7 9 4 6 4 12 8 2 5 16 6

出力例 4

784973196

総和を 998244353 で割った余りを求める点に注意してください。

Score : 1000 points

Problem Statement

You are given a rooted tree T with N vertices labeled from 1 to N. The root is the vertex 1, and the parent of vertex i (2 \leq i \leq N) is p_i (p_i \lt i).

Consider a directed graph G with N vertices labeled from 1 to N. We call G good if it satisfies all of the following conditions (here, let u \to v denote an edge from vertex u to vertex v):

  • Every vertex in G has outdegree 1.
  • G has no cycle of even length.
  • For every i (2 \leq i \leq N), G does not contain the edge i \to p_i.

Find the sum, modulo 998244353, of 2^{(\text{number of cycles in }G)} over all good graphs G.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq p_i \lt i
  • All values given 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 sum, modulo 998244353, of 2^{(\text{number of cycles in }G)} over all good graphs G.


Sample Input 1

2
1

Sample Output 1

6

Here are the two possible good graphs:

  • The graph with edges 1 \to 1 and 2 \to 2.
  • The graph with edges 1 \to 2 and 2 \to 2.

The number of cycles in these graphs are 2 and 1, respectively. Thus, the answer is (2^2 + 2^1) \bmod{998244353} = 6.


Sample Input 2

3
1 2

Sample Output 2

34

For example, the graph with edges 1 \to 2, 2 \to 3, and 3 \to 1 is a good graph, whose number of cycle is 1.


Sample Input 3

5
1 2 1 1

Sample Output 3

3104

Sample Input 4

20
1 2 2 2 5 3 5 1 7 9 4 6 4 12 8 2 5 16 6

Sample Output 4

784973196

Remember to find the sum modulo 998244353.

C - No Streak

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 1000

問題文

Alice と Bob はじゃんけんを N 回行いました。 1 回のじゃんけんの結果は「Alice の勝ち」「Bob の勝ち」「引き分け」のいずれかです。
以下の条件を満たす N 回のじゃんけんの結果は何通りありますか?答えを \bf{1000000007 = 10^9 + 7} で割った余りを求めてください。

  • N 回のうち Alice が勝ったのは A 回、Bob が勝ったのは B 回だった。
  • Alice が引き分けを挟まずに 2 連続で勝つことはなかった。
  • Bob が引き分けを挟まずに 2 連続で勝つことはなかった。
  • どの時点においても Alice の勝利数が Bob の勝利数を下回ることはなかった。形式的に説明すると、1 \leq i \leq N を満たす i 全てに対して、「じゃんけんが i 回終了した時点での Alice の勝利数」は「じゃんけんが i 回終了した時点での Bob の勝利数」以上だった。

制約

  • 2 \leq N \leq 2 \times 10^7
  • 1 \leq B \leq A
  • A + B \leq N
  • N, A, B は整数

入力

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

N A B

出力

条件を満たす N 回のじゃんけんの結果の個数を 10^9 + 7 で割った余りを求めよ。


入力例 1

5 2 2

出力例 1

5

例えば、次の N 回のじゃんけんの結果は問題文の条件を満たします。

  • 1 回目のじゃんけんで Alice が勝つ。
  • 2 回目のじゃんけんで Bob が勝つ。
  • 3 回目のじゃんけんで Alice が勝つ。
  • 4 回目のじゃんけんで両者が引き分ける。
  • 5 回目のじゃんけんで Bob が勝つ。

一方、次の N 回のじゃんけんの結果は 問題文の条件を満たしません。なぜならば、4 回目のじゃんけんが終了した時点で Alice の勝利数 (= 1) が Bob の勝利数 (= 2) を下回っているからです。

  • 1 回目のじゃんけんで Alice が勝つ。
  • 2 回目のじゃんけんで Bob が勝つ。
  • 3 回目のじゃんけんで両者が引き分ける。
  • 4 回目のじゃんけんで Bob が勝つ。
  • 5 回目のじゃんけんで Alice が勝つ。

問題文の条件を満たす結果は全部で 5 通りあります。


入力例 2

70 29 12

出力例 2

693209192

個数を 10^9 + 7 で割った余りを求める点に注意してください。


入力例 3

20000000 1234567 890123

出力例 3

566226457

Score : 1000 points

Problem Statement

Alice and Bob played rock-paper-scissors N times. Each round of rock-paper-scissors results in “Alice’s win,” “Bob’s win,” or “Draw.”
Among all possible sequences of results of N rounds, how many satisfy the following conditions? Find the count modulo \bf{1000000007 = 10^9 + 7}.

  • Among the N rounds, Alice won exactly A times and Bob won exactly B times.
  • Alice never won twice in a row without intervening draws.
  • Bob never won twice in a row without intervening draws.
  • At no point did Bob’s number of wins exceed Alice’s number of wins. Formally, for all 1 \leq i \leq N, “the number of times Alice has won up through round i” is at least “the number of times Bob has won up through round i.”

Constraints

  • 2 \leq N \leq 2 \times 10^7
  • 1 \leq B \leq A
  • A + B \leq N
  • N, A, and B are integers.

Input

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

N A B

Output

Print the number, modulo (10^9 + 7), of sequences of results of N rounds that satisfy the conditions.


Sample Input 1

5 2 2

Sample Output 1

5

For example, the following sequence of N rounds satisfies the conditions:

  • Round 1: Alice wins.
  • Round 2: Bob wins.
  • Round 3: Alice wins.
  • Round 4: Draw.
  • Round 5: Bob wins.

On the other hand, the following sequence of N rounds does not satisfy the conditions, because after round 4, Alice’s number of wins (=1) is less than Bob’s number of wins (=2):

  • Round 1: Alice wins.
  • Round 2: Bob wins.
  • Round 3: Draw.
  • Round 4: Bob wins.
  • Round 5: Alice wins.

There are five sequences in total that satisfy the conditions.


Sample Input 2

70 29 12

Sample Output 2

693209192

Remember to find the count modulo (10^9 + 7).


Sample Input 3

20000000 1234567 890123

Sample Output 3

566226457
D - 2D Solitaire

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 1800

問題文

正整数 N, K および (1, 2, \dots, N+K) の順列 A=(A_1,A_2,\dots,A_{N+K}),B=(B_1,B_2,\dots,B_{N+K}) が与えられます。
Q 個のクエリが与えられるので処理してください。

各クエリでは整数の組 (X, Y) が与えられるので、次の問題を解いてください。(全てのクエリは独立です。)

1 から N + K + X までの番号がついた N + K + X 枚のカードがあります。
各カードには整数の組が書かれていて、カード i には

  • 1 \leq i \leq N+K の時、(A_i, B_i)
  • N + K + 1 \leq i \leq N + K + X の時、(i, Y)

が書かれています。

今、あなたはカード 1 からカード N までの N 枚のカードを持っています。また、場にカード N+1 からカード N+K+X までの K+X 枚のカードが場に置かれています。
あなたは次の一連の操作を 0 回以上自由な回数行うことが出来ます。

  • まず、持っているカードと場に置かれているカードを 1 枚ずつ選び、それぞれカード i、カード j とする。ただし、選んだカードは次の条件を満たす必要がある。
    • カード i に書かれている整数の組を (a_i, b_i)、カード j に書かれている整数の組を (a_j, b_j) とした時、a_i \lt a_j かつ b_i \lt b_j が成り立つ必要がある。
  • そして、カード j を場から取り除き、かわりにカード i を場に置く。(取り除かれたカードはその後選択できない。)

操作によって持っているカードの枚数を最小で何枚にすることができますか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq N + K
  • 1 \leq B_i \leq N + K
  • A, B(1, 2, \dots, N+K) の順列
  • 1 \leq X \leq 10
  • 1 \leq Y \leq N + K
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_ii 番目のクエリを意味する。

N K Q
A_1 A_2 \dots A_{N+K}
B_1 B_2 \dots B_{N+K}
\mathrm{query}_1 
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下の形式で与えられる。

X Y

出力

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


入力例 1

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

出力例 1

4
3
3
3
2
1

2 番目のクエリ、すなわち (X, Y) = (2, 2) の場合を例に挙げて説明します。
はじめ、持っているカードと場に出ているカードを列挙すると次のようになります。

  • 持っているカード : (1, 4), (2, 2), (3, 5), (4, 1), (6, 6)
  • 場に出ているカード : (5, 3), (7, 2), (8, 2)

この時、次のように操作を行うことで持っているカードの枚数を 3 枚にすることができて、これが最小です。

  • 持っているカードとして (2, 2) が書かれたカード 2 を、場に出ているカードとして (5, 3) が書かれたカード 6 を選ぶ。カード 6 を場から取り除き、カード 2 を場に置く。取り除かれていないカードの状況は次のようになる。
    • 持っているカード : (1, 4), (3, 5), (4, 1), (6, 6)
    • 場に出ているカード : (2, 2), (7, 2), (8, 2)
  • 持っているカードとして (4, 1) が書かれたカード 4 を、場に出ているカードとして (8, 2) が書かれたカード 8 を選ぶ。カード 8 を場から取り除き、カード 4 を場に置く。取り除かれていないカードの状況は次のようになる。
    • 持っているカード : (1, 4), (3, 5), (6, 6)
    • 場に出ているカード : (2, 2), (7, 2), (4, 1)

入力例 2

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

出力例 2

4
5
5
4
2
4
1
6
3
4

入力例 3

20 3 10
18 16 5 12 21 23 14 1 19 17 3 15 13 22 7 4 2 8 9 20 6 11 10
18 1 6 20 9 13 21 17 4 16 15 22 12 14 10 23 2 5 3 19 7 11 8
1 11
1 3
3 19
1 22
2 15
4 9
3 16
1 19
5 21
2 3

出力例 3

13
15
6
11
9
13
8
12
3
15

Score : 1800 points

Problem Statement

You are given positive integers N and K, and two permutations A = (A_1, A_2, \dots, A_{N+K}) and B = (B_1, B_2, \dots, B_{N+K}) of (1, 2, \dots, N+K).
Process Q queries.

For each query, given a pair of integers (X, Y), solve the following problem. (All queries are independent.)

There are N + K + X cards labeled from 1 to N + K + X.
Each card has a pair of integers written on it. Specifically,

  • For 1 \leq i \leq N+K, card i has (A_i, B_i).
  • For N + K + 1 \leq i \leq N + K + X, card i has (i, Y).

At the beginning, you have the N cards 1 to N in your hand, and the K + X cards from N+1 to N + K + X are on the table.
You may perform the following sequence of operations any number of times, possibly zero.

  • First, choose one card from your hand and one card from the table, calling them card i and card j. Here, the following condition must hold:
    • Let (a_i, b_i) be the pair of integers written on card i, and (a_j, b_j) be the pair on card j. You must have a_i < a_j and b_i < b_j.
  • Then, remove card j from the table and place card i on the table (the removed card is no longer available).

What is the minimum number of cards you can end up holding in your hand after performing these operations?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq N + K
  • 1 \leq B_i \leq N + K
  • A and B are permutations of (1, 2, \dots, N+K).
  • 1 \leq X \leq 10
  • 1 \leq Y \leq N + K
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{query}_i denotes the i-th query:

N K Q
A_1 A_2 \dots A_{N+K}
B_1 B_2 \dots B_{N+K}
\mathrm{query}_1 
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in the following format:

X Y

Output

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


Sample Input 1

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

Sample Output 1

4
3
3
3
2
1

Taking the query (X, Y) = (2, 2) (the second query) as an example:

Initially, the cards in your hand and on the table are as follows:

  • In your hand: (1, 4), (2, 2), (3, 5), (4, 1), (6, 6)
  • On the table: (5, 3), (7, 2), (8, 2)

You can achieve holding three cards in your hand, which is the minimum, by performing the following operations:

  • Choose card 2 in your hand with (2, 2) and card 6 on the table with (5, 3). Remove card 6 from the table and place card 2 on the table. Now, the following cards are remaining.
    • In your hand: (1, 4), (3, 5), (4, 1), (6, 6)
    • On the table: (2, 2), (7, 2), (8, 2)
  • Choose card 4 in your hand with (4, 1) and card 8 on the table with (8, 2). Remove card 8 from the table and place card 4 on the table. Now, the following cards are remaining.
    • In your hand: (1, 4), (3, 5), (6, 6)
    • On the table: (2, 2), (7, 2), (4, 1)

Sample Input 2

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

Sample Output 2

4
5
5
4
2
4
1
6
3
4

Sample Input 3

20 3 10
18 16 5 12 21 23 14 1 19 17 3 15 13 22 7 4 2 8 9 20 6 11 10
18 1 6 20 9 13 21 17 4 16 15 22 12 14 10 23 2 5 3 19 7 11 8
1 11
1 3
3 19
1 22
2 15
4 9
3 16
1 19
5 21
2 3

Sample Output 3

13
15
6
11
9
13
8
12
3
15
E - Remove 2K+1 Edges

Time Limit: 12 sec / Memory Limit: 1024 MiB

配点 : 1800

問題文

正整数 N, K が与えられます。頂点に 1 から (2K + 1) N + 1 までの番号がついた (2K+1)N + 1 頂点の無向木 T のうち次の条件を満たすものの個数を 998244353 で割った余りを求めてください。

  • T に次の操作を繰り返し行うことで、T に含まれる辺を全て取り除くことが出来る。
    • 長さ 2K + 1 のパスを選び、パスに含まれる辺を全て取り除く。
      厳密に述べると、相異なる頂点の列 v_0, v_1, \dots, v_{2K+1} であって 0 \leq i \leq 2K を満たす全ての i について辺 (v_i, v_{i+1}) が存在するものを選ぶ。そして、0 \leq i \leq 2K を満たす全ての i について辺 (v_i, v_{i+1}) を取り除く。

制約

  • 1 \leq N \leq 1.3 \times 10^5
  • 1 \leq K \leq 50
  • 入力される値は全て整数

入力

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

N K

出力

問題文の条件を満たす (2K + 1) N + 1 頂点の無向木 T の個数を 998244353 で割った余りを出力せよ。


入力例 1

2 1

出力例 1

8820

例えば辺 (1, 2), 辺 (2, 3), 辺 (2, 4), 辺 (2, 6), 辺 (3, 7), 辺 (4, 5) が存在する無向木を考えます。この木は以下の手順で操作を行うと全ての辺を取り除くことが出来るため、問題文の条件を満たします。

  • パス (1,2,4,5) を選ぶ。辺 (1,2), 辺 (2,4), 辺 (4,5) を取り除く。
  • パス (6,2,3,7) を選ぶ。辺 (6,2), 辺 (2,3), 辺 (3,7) を取り除く。

image

条件を満たす木は全部で 8820 個あります。


入力例 2

70 1

出力例 2

273892456

個数を 998244353 で割った余りを出力する点に注意してください。


入力例 3

12 29

出力例 3

898069699

入力例 4

100000 50

出力例 4

158600909

Score : 1800 points

Problem Statement

You are given positive integers N and K. Consider an undirected tree T with (2K + 1)N + 1 vertices labeled from 1 to (2K + 1)N + 1. Among all such trees, find the number, modulo 998244353, of trees that satisfy the following condition.

  • It is possible to remove all edges from T by repeating the following operation:
    • Choose a path of length 2K + 1 and remove all edges in it.
      Formally, choose a sequence of distinct vertices v_0, v_1, \dots, v_{2K+1} such that there exists an edge (v_i, v_{i+1}) for all i such that 0 \leq i \leq 2K, and remove the edge (v_i, v_{i+1}) for all i such that 0 \leq i \leq 2K.

Constraints

  • 1 \leq N \leq 1.3 \times 10^5
  • 1 \leq K \leq 50
  • All input values are integers.

Input

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

N K

Output

Print the number, modulo 998244353, of trees with (2K + 1)N + 1 vertices satisfying the condition.


Sample Input 1

2 1

Sample Output 1

8820

For example, consider a tree with edges (1, 2), (2, 3), (2, 4), (2, 6), (3, 7), and (4, 5). By doing the following steps, one can remove all edges from the tree, so it satisfies the condition in the problem statement:

  • Choose the path (1,2,4,5) and remove edges (1,2), (2,4), (4,5).
  • Choose the path (6,2,3,7) and remove edges (6,2), (2,3), (3,7).

image

There are 8820 such trees in total.


Sample Input 2

70 1

Sample Output 2

273892456

Remember to print the count modulo 998244353.


Sample Input 3

12 29

Sample Output 3

898069699

Sample Input 4

100000 50

Sample Output 4

158600909