A - Three Cells per Row and Column

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

NN 列からなる盤面があります.

以下の条件をすべて満たすように,すべてのマスを白か黒で塗ってください.

  • 各行について,その行のマスのうちちょうど 3 個が黒く塗られている.
  • 各列について,その列のマスのうちちょうど 3 個が黒く塗られている.
  • 黒いマスからなる連結成分の個数がちょうど N 個である. ここで,ある 2 つの黒いマス x,y が連結であるとは,x からスタートし,上下左右の黒いマスに移動することを繰り返し,y に到達できることを意味する.

なお,問題の制約より,必ず解が存在することが証明できます.

制約

  • 6 \leq N \leq 500
  • 入力される値はすべて整数である

入力

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

N

出力

答えを以下の形式で出力せよ.

s_{1,1}s_{1,2}\cdots s_{1,N}
s_{2,1}s_{2,2}\cdots s_{2,N}
\vdots
s_{N,1}s_{N,2}\cdots s_{N,N}

ここで,s_{i,j} は,上から i 行目,左から j 列目のマスを塗る色を表す文字であり, s_{i,j}=# のときはそのマスを黒く,s_{i,j}=. のときはそのマスを白く塗ることを意味する. 答えが複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

6

出力例 1

##..#.
##..#.
..##.#
..##.#
##...#
..###.

各行,各列にある # の個数はちょうど 3 です. また,# からなる連結成分の個数はちょうど 6 です.

Score : 300 points

Problem Statement

We have a grid board with N rows and N columns.

Paint every square black or white to satisfy all of the conditions below.

  • For each row, exactly three of the squares in that row are painted black.
  • For each column, exactly three of the squares in that column are painted black.
  • There are exactly N connected components of black squares. Here, two black squares x and y are considered connected when it is possible to start at x and reach y by repeatedly moving up, down, left, or right to a black square.

It can be proved that the Constraints of the problem guarantee the existence of a solution.

Constraints

  • 6 \leq N \leq 500
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print your answer in the following format:

s_{1,1}s_{1,2}\cdots s_{1,N}
s_{2,1}s_{2,2}\cdots s_{2,N}
\vdots
s_{N,1}s_{N,2}\cdots s_{N,N}

Here, s_{i,j} should be a character representing the color of the square at the i-th row from the top and j-th column from the left: s_{i,j}=# means the square is painted black, and s_{i,j}=. means it is painted white. If multiple solutions exist, printing any of them is accepted.


Sample Input 1

6

Sample Output 1

##..#.
##..#.
..##.#
..##.#
##...#
..###.

Here, there are exactly 3 #s in each row and column. Additionally, there are exactly 6 connected components of #s.

B - Range Argmax

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

整数 N と整数の組が M 個与えられます. i 番目の組は (L_i,R_i) です.

以下の手順で生成されうる整数列 x=(x_1,x_2,\cdots,x_M) の個数を 998244353 で割った余りを求めて下さい.

  • (1,2,\cdots,N) の順列 p=(p_1,p_2,\cdots,p_N) を用意する.
  • i (1 \leq i \leq M) について,p_{L_i},p_{L_i+1},\cdots,p_{R_i} の中で最も大きい値の index を x_i とする. つまり,\max(p_{L_i},p_{L_i+1},\cdots,p_{R_i})=p_{x_i} である.

制約

  • 2 \leq N \leq 300
  • 1 \leq M \leq N(N-1)/2
  • 1 \leq L_i < R_i \leq N
  • (L_i,R_i) \neq (L_j,R_j) (i \neq j)
  • 入力される値はすべて整数である

入力

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

出力

答えを出力せよ.


入力例 1

3 2
1 2
1 3

出力例 1

4

例えば,p=(2,1,3) とした場合は x=(1,3) となります.

条件を満たす数列は,x=(1,1),(1,3),(2,2),(2,3)4 通りです.


入力例 2

6 3
1 2
3 4
5 6

出力例 2

8

入力例 3

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

出力例 3

1060

Score : 900 points

Problem Statement

Given are an integer N and M pairs of integers. The i-th pair is (L_i,R_i).

Find the number of integer sequences x=(x_1,x_2,\cdots,x_M) that can be generated as follows, modulo 998244353.

  • Provide a permutation p=(p_1,p_2,\cdots,p_N) of (1,2,\cdots,N).
  • For each i (1 \leq i \leq M), let x_i be the index of the largest element among p_{L_i},p_{L_i+1},\cdots,p_{R_i}. That is, \max(p_{L_i},p_{L_i+1},\cdots,p_{R_i})=p_{x_i}.

Constraints

  • 2 \leq N \leq 300
  • 1 \leq M \leq N(N-1)/2
  • 1 \leq L_i < R_i \leq N
  • (L_i,R_i) \neq (L_j,R_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the answer.


Sample Input 1

3 2
1 2
1 3

Sample Output 1

4

For example, for p=(2,1,3), we will have x=(1,3).

The four sequences that meet the requirement are x=(1,1),(1,3),(2,2),(2,3).


Sample Input 2

6 3
1 2
3 4
5 6

Sample Output 2

8

Sample Input 3

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

Sample Output 3

1060
C - 01 Balanced

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 900

問題文

0, 1 からなる長さ N の文字列 s を作ることを考えます. ここで,sM 個の条件を満たす必要があります. i 番目の条件は整数 L_i,R_i (1 \leq L_i < R_i \leq N) で表されます. これは,sL_i 文字目から R_i 文字目までを見たときに,そこに含まれる 0 の個数と 1 の個数が等しい必要があることを意味します.

すべての条件を満たす中で辞書順最小の s を求めてください. なお,問題の制約より,条件を満たす s が必ず存在することが証明できます.

制約

  • 2 \leq N \leq 10^6
  • 1 \leq M \leq 200000
  • 1 \leq L_i < R_i \leq N
  • (R_i-L_i+1) \equiv 0 \mod 2
  • (L_i,R_i) \neq (L_j,R_j) (i \neq j)
  • 入力される値はすべて整数である

入力

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

出力

答えを出力せよ.


入力例 1

4 2
1 2
3 4

出力例 1

0101

入力例 2

6 2
1 4
3 6

出力例 2

001100

入力例 3

20 10
6 17
2 3
14 19
5 14
10 15
7 20
10 19
3 20
6 9
7 12

出力例 3

00100100101101001011

Score : 900 points

Problem Statement

Consider making a string s of length N consisting of 0 and 1, where s must satisfy M conditions. The i-th condition is represented by integers L_i and R_i (1 \leq L_i < R_i \leq N). This means that there should be equal numbers of 0 and 1 between the L_i-th and R_i-th characters (inclusive) of s.

Find the lexicographically smallest string s that satisfies all conditions. It can be proved that the Constraints of the problem guarantee the existence of s that satisfies the conditions.

Constraints

  • 2 \leq N \leq 10^6
  • 1 \leq M \leq 200000
  • 1 \leq L_i < R_i \leq N
  • (R_i-L_i+1) \equiv 0 \mod 2
  • (L_i,R_i) \neq (L_j,R_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the answer.


Sample Input 1

4 2
1 2
3 4

Sample Output 1

0101

Sample Input 2

6 2
1 4
3 6

Sample Output 2

001100

Sample Input 3

20 10
6 17
2 3
14 19
5 14
10 15
7 20
10 19
3 20
6 9
7 12

Sample Output 3

00100100101101001011
D - Subset Sum Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1600

問題文

黒板に N 個の整数が書かれており,そのうち i 番目の整数は A_i です. なお,N は偶数です. また,整数 L,R が与えられます.

Alice と Bob がゲームをします. 二人は Alice からはじめて交互に手番をプレイします. 各手番では,プレイヤーは黒板に書かれている数を一つ選んで消します.

N ターン後にゲームが終了します. ここで,Alice が消した整数の総和を s とします. L \leq s \leq R であれば Alice の勝利,そうでなければ Bob の勝利です. 両者が最適に行動した時,どちらが勝つか求めてください.

制約

  • 2 \leq N \leq 5000
  • N は偶数
  • 1 \leq A_i \leq 10^9
  • 0 \leq L \leq R \leq \sum_{1 \leq i \leq N} A_i
  • 入力される値はすべて整数である

入力

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

N L R
A_1 A_2 \cdots A_N

出力

Alice が勝つ場合は Alice,Bob が勝つ場合は Bob と出力せよ.


入力例 1

4 5 6
3 1 4 5

出力例 1

Alice

このゲームでは Alice が必ず勝ちます. ゲームの進行の一例を以下に示します.

  • Alice が 1 を消す.
  • Bob が 4 を消す.
  • Alice が 5 を消す.
  • Bob が 3 を消す.

この時,Alice が消した整数の総和は 6 であり,L \leq 6 \leq R なので,Alice の勝利です.


入力例 2

2 2 3
4 1

出力例 2

Bob

入力例 3

30 655 688
42 95 9 13 91 27 99 56 64 15 3 11 5 16 85 3 62 100 64 79 1 70 8 69 70 28 78 4 33 12

出力例 3

Bob

入力例 4

30 792 826
81 60 86 57 5 20 26 13 39 64 89 58 43 98 50 79 58 21 27 68 46 47 45 85 88 5 82 90 74 57

出力例 4

Alice

Score : 1600 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i. Here, N is even. Additionally, integers L and R are given.

Alice and Bob will play a game. They alternately take turns, with Alice going first. In each turn, the player chooses a number written on the blackboard and erases it.

The game will end in N turns. Then, let s be the sum of the integers erased by Alice. If L \leq s \leq R, Alice wins; otherwise, Bob wins. Find which player will win when both players play optimally.

Constraints

  • 2 \leq N \leq 5000
  • N is even.
  • 1 \leq A_i \leq 10^9
  • 0 \leq L \leq R \leq \sum_{1 \leq i \leq N} A_i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L R
A_1 A_2 \cdots A_N

Output

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


Sample Input 1

4 5 6
3 1 4 5

Sample Output 1

Alice

In this game, Alice will always win. Below is a possible progression of the game.

  • Alice erases 1.
  • Bob erases 4.
  • Alice erases 5.
  • Bob erases 3.

Here, the sum of the integers erased by Alice is 6. Since L \leq 6 \leq R, Alice wins.


Sample Input 2

2 2 3
4 1

Sample Output 2

Bob

Sample Input 3

30 655 688
42 95 9 13 91 27 99 56 64 15 3 11 5 16 85 3 62 100 64 79 1 70 8 69 70 28 78 4 33 12

Sample Output 3

Bob

Sample Input 4

30 792 826
81 60 86 57 5 20 26 13 39 64 89 58 43 98 50 79 58 21 27 68 46 47 45 85 88 5 82 90 74 57

Sample Output 4

Alice
E - Cheese

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 1600

問題文

長さ N の円周があります. 円周上のある決まった点から距離 x だけ時計回りに進んだ点を,座標 x の点と呼ぶことにします.

各整数 i (0 \leq i \leq N-1) について,座標 i+0.5 に一匹のネズミがいます.

すぬけくんは今から,N-1 回チーズを投げます. 具体的には,以下のような操作を N-1 回繰り返します.

  • 整数 y (0 \leq y \leq N-1)をランダムに選ぶ. ただし,y=i となる確率は A_i% である. この選択は毎回独立である.
  • その後,座標 y からチーズを投げる. チーズは,円周上を時計回りに移動する. ネズミのいる位置をチーズが通る時,以下のことが起こる.
    • そのネズミが今までにチーズを食べていない場合:
      • 1/2 の確率でチーズを食べる.食べられたチーズは消失する.
      • 1/2 の確率でなにもしない.
    • そのネズミが今までにチーズを食べたことがある場合:
      • なにもしない.
  • チーズは,いずれかのネズミに食べられるまで,円周上を回り続ける.

N-1 回チーズを投げたあと,ちょうど 1 匹だけ,チーズを食べていないネズミがいます. 各 k=0,1,\cdots,N-1 について,座標 k+0.5 にいるネズミが最終的にチーズを食べていない確率を \bmod\ 998244353 で求めてください.

確率 \bmod\ 998244353 の定義

求める確率は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \neq 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 1 \leq N \leq 40
  • 0 \leq A_i
  • \sum_{0 \leq i \leq N-1} A_i = 100
  • 入力される値はすべて整数である

入力

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

N
A_0 A_1 \cdots A_{N-1}

出力

k=0,1,\cdots,N-1 に対する答えを,空白区切りで一行に出力せよ.


入力例 1

2
0 100

出力例 1

665496236 332748118

必ず y=1 が選択されます.ここからチーズを投げた時,以下のことが起こります.

  • 1/2 の確率で,座標 1.5 のネズミがチーズを食べる.
  • 1/4 の確率で,座標 0.5 のネズミがチーズを食べる.
  • 1/8 の確率で,座標 1.5 のネズミがチーズを食べる.
  • 1/16 の確率で,座標 0.5 のネズミがチーズを食べる.
  • \vdots

結局,このチーズを座標 0.5,1.5 のネズミが食べる確率は,それぞれ 1/3,2/3 です.

よって,最終的にチーズを食べていない確率は,それぞれ 2/3,1/3 になります.


入力例 2

2
50 50

出力例 2

499122177 499122177

入力例 3

10
1 2 3 4 5 6 7 8 9 55

出力例 3

333507001 91405664 419217284 757959653 974851434 806873643 112668439 378659755 979030842 137048051

Score : 1600 points

Problem Statement

There is a circumference of length N. On this circumference, a point whose distance from a certain fixed point, measured in the clockwise direction, is x has the coordinate x.

For each integer i (0 \leq i \leq N-1), there is a mouse at coordinate i+0.5.

Snuke will throw a piece of cheese N-1 times. More specifically, he repeats the following N-1 times.

  • Choose an integer y (0 \leq y \leq N-1) randomly, where y=i is chosen with probability A_i percent. This choice is made independently each time.
  • Then, throw cheese from the coordinate y. The cheese travels in the clockwise direction on the circumference. When the cheese meets a mouse, the following happens.
    • If the mouse has not eaten cheese:
      • It eats the cheese with probability 1/2. The cheese disappears.
      • It does nothing with probability 1/2.
    • If the mouse has already eaten cheese:
      • It does nothing.
  • The cheese keeps traveling on the circumference until eaten by some mouse.

After N-1 throws of cheese, there will be exactly one mouse that has never eaten cheese. For each k=0,1,\cdots,N-1, find the probability that the mouse at coordinate k+0.5 ends up not eating cheese, modulo 998244353.

How to represent a probability modulo 998244353

We can prove that each probability in question is always a rational number. Additionally, under the Constraints of this problem, when that number is represented as an irreducible fraction \frac{P}{Q}, we can also prove that Q \neq 0 \pmod{998244353}. Thus, there is a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Answer with this R.

Constraints

  • 1 \leq N \leq 40
  • 0 \leq A_i
  • \sum_{0 \leq i \leq N-1} A_i = 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 \cdots A_{N-1}

Output

Print the answers for k=0,1,\cdots,N-1 in one line, with spaces in between.


Sample Input 1

2
0 100

Sample Output 1

665496236 332748118

y=1 is always chosen. When throwing cheese from this point, the following happens.

  • With probability 1/2, the mouse at coordinate 1.5 eats it.
  • With probability 1/4, the mouse at coordinate 0.5 eats it.
  • With probability 1/8, the mouse at coordinate 1.5 eats it.
  • With probability 1/16, the mouse at coordinate 0.5 eats it.
  • \vdots

In the end, the mice at coordinates 0.5 and 1.5 eat this cheese with probabilities 1/3 and 2/3, respectively.

Therefore, they end up not eating cheese with probabilities 2/3 and 1/3, respectively.


Sample Input 2

2
50 50

Sample Output 2

499122177 499122177

Sample Input 3

10
1 2 3 4 5 6 7 8 9 55

Sample Output 3

333507001 91405664 419217284 757959653 974851434 806873643 112668439 378659755 979030842 137048051
F - Degree Sequence in DFS Order

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1600

問題文

整数 NM が与えられます. 以下の手順で生成されうる整数列 a=(a_1,a_2,\cdots,a_N) の個数を 998244353 で割った余りを求めてください.

  • N 頂点,M 辺からなる連結な無向グラフ G を用意する. ここで,G は自己ループを含んではならないが,多重辺を含んでもよい
  • G 上で DFS を行い,i (1 \leq i \leq N) 番目に訪れた頂点の次数を a_i とする. より正確には,以下の疑似コードを実行して a を得る.
a = empty array

dfs(v):
    visited[v]=True
    a.append(degree[v])
    for u in g[v]:
        if not visited[u]:
            dfs(u)

dfs(arbitrary root)

ここで,変数 g はグラフ G を隣接リストとして表したものであり,g[v] は頂点 v に隣接する頂点を任意の順番で格納したリストである.

例えば,N=4,M=5 の時,a=(2,4,1,3) を生成することは可能です. そのためには,以下のようなグラフ G を用意すればよいです.

picture

ここで,頂点にかかれている数は,その頂点を DFS で何番目に訪れたかを表しています.(1 と書かれた頂点から DFS を開始しています.) また,オレンジ色の矢印は DFS での遷移を示しており,その横の数字は辺をたどる順番を表しています.

制約

  • 2 \leq N \leq M \leq 10^6
  • 入力される値はすべて整数である

入力

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

N M

出力

答えを出力せよ.


入力例 1

2 2

出力例 1

1

あり得るのは a=(2,2) のみです. G は多重辺を持ってもよいことに注意してください.


入力例 2

3 4

出力例 2

9

入力例 3

10 20

出力例 3

186225754

入力例 4

100000 1000000

出力例 4

191021899

Score : 1600 points

Problem Statement

Given are integers N and M. Find the number of integer sequences a=(a_1,a_2,\cdots,a_N) that can be generated as follows, modulo 998244353.

  • Provide an undirected connected graph G with N vertices and M edges. Here, G may not contain self-loops, but may contain multi-edges.
  • Perform DFS on G and let a_i be the degree of the i-th vertex to be visited. More accurately, execute the pseudo-code below to get a.
a = empty array

dfs(v):
    visited[v]=True
    a.append(degree[v])
    for u in g[v]:
        if not visited[u]:
            dfs(u)

dfs(arbitrary root)

Here, the variable g represents the graph G as an adjacency list, where g[v] is the list of vertices adjacent to the vertex v in arbitrary order.

For example, when N=4,M=5, it is possible to generate a=(2,4,1,3), by providing the graph G as follows.

picture

Here, the numbers written on vertices represent the order they are visited in the DFS. (The DFS starts at the vertex labeled 1.) The orange arrows show the transitions in the DFS, and the numbers next to them represent the order the edges are traversed.

Constraints

  • 2 \leq N \leq M \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.


Sample Input 1

2 2

Sample Output 1

1

The only possible result is a=(2,2). Note that G may have multi-edges.


Sample Input 2

3 4

Sample Output 2

9

Sample Input 3

10 20

Sample Output 3

186225754

Sample Input 4

100000 1000000

Sample Output 4

191021899