A - Erasing Vertices

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1 から N までの番号のついた N 頂点からなる有向グラフがあります. このグラフの辺は,N 個の長さ N の文字列 S_1,S_2,\ldots,S_N によって表されます. 具体的には,頂点 i から頂点 j に向かう辺が存在する場合は S_{i,j}=1 で, そうでない場合は S_{i,j}=0 です. このグラフに自己ループや多重辺はありません.

クマの AtCobear くんが,以下の操作をグラフが空になるまで繰り返します.

  • (まだ削除されていない) グラフの頂点を一様ランダムに 1 つ選ぶ(このランダムな選択は,今までの選択とは独立である). そして,その頂点,およびその頂点からいくつかの辺をたどることで到達可能なすべての頂点を,削除する. 削除された頂点に接続する辺も,すべて削除される.

操作回数の期待値を求めてください.

制約

  • 1 \leq N \leq 100
  • S_i0,1 からなる長さ N の文字列.
  • S_{i,i}=0

入力

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

N
S_1
S_2
\vdots
S_N

出力

操作回数の期待値を出力せよ. 絶対誤差または相対誤差が 10^{-9} 以下ならば,正解と判定される.


入力例 1

3
010
001
010

出力例 1

1.66666666666666666667

以下の 3 つのシナリオが等確率で起こります.

  • 最初の操作で頂点 1 を選び,頂点 1,2,3 が削除される. グラフが空になったので操作を終了する.

  • 最初の操作で頂点 2 を選び,頂点 2,3 が削除される. 次の操作で,頂点 1 を選び,頂点 1 が削除される. グラフが空になったので操作を終了する.

  • 最初の操作で頂点 3 を選び,頂点 2,3 が削除される. 次の操作で,頂点 1 を選び,頂点 1 が削除される. グラフが空になったので操作を終了する.

よって操作回数の期待値は,(1+2+2)/3=5/3 になります.


入力例 2

3
000
000
000

出力例 2

3.00000000000000000000

必ず 3 回の操作を行います.


入力例 3

3
011
101
110

出力例 3

1.00000000000000000000

必ず 1 回の操作を行います.

Score : 400 points

Problem Statement

We have a directed graph with N vertices numbered 1 to N. N strings of length N each, S_1,S_2,\ldots,S_N, represent the edges in the graph. Specifically, S_{i,j} is 1 if there is an edge from Vertex i to Vertex j, and 0 otherwise. The graph has no self-loops and no multi-edges.

Until the graph becomes empty, AtCobear will repeat the following operation:

  • Choose one (unerased) vertex uniformly at random (independently from the previous choices). Then, erase that vertex and all vertices that are reachable from the chosen vertex by traversing some edges. Erasing a vertex will also erase the edges incident to it.

Find the expected value of the number of times the operation is done.

Constraints

  • 1 \leq N \leq 100
  • S_i is a string of length N consisting of 0 and 1.
  • S_{i,i}=0

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print the expected value of the number of times the operation is done. Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-9}.


Sample Input 1

3
010
001
010

Sample Output 1

1.66666666666666666667

We have the following three scenarios happening with equal probability:

  • Choose Vertex 1 in the first operation, erasing Vertex 1, 2, and 3. The graph is now empty, so we are done.

  • Choose Vertex 2 in the first operation, erasing Vertex 2 and 3. Then, choose Vertex 1 in the second operation, erasing Vertex 1. The graph is now empty, so we are done.

  • Choose Vertex 3 in the first operation, erasing Vertex 2 and 3. Then, choose Vertex 1 in the second operation, erasing Vertex 1. The graph is now empty, so we are done.

Thus, the expected value of the number of times the operation is done is (1+2+2)/3=5/3.


Sample Input 2

3
000
000
000

Sample Output 2

3.00000000000000000000

There will always be three operations.


Sample Input 3

3
011
101
110

Sample Output 3

1.00000000000000000000

There will always be one operation.

B - Flip Digits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

01 からなる長さ N の文字列 S 及び T が与えられます. あなたは,S に以下の操作を好きな回数行うことができます.

  • S_i=1 となる i (2 \leq i \leq N) を選ぶ. そして,S_i0 で置き換える. さらに,S_{i-1} を今と異なる文字へ変更する.つまり,操作の直前で S_{i-1}0 であれば 1 に,1 であれば 0 に変更する.

ST に一致させることは可能でしょうか? また可能な場合は,そのために必要な最小の操作回数はいくらでしょうか?

制約

  • 1 \leq N \leq 5 \times 10^5
  • S0,1 からなる長さ N の文字列.
  • T0,1 からなる長さ N の文字列.

入力

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

N
S
T

出力

ST に一致させることが可能な場合,必要な最小の操作回数を出力せよ. 不可能な場合,-1 を出力せよ.


入力例 1

3
001
100

出力例 1

2

001 → (i=3 で操作) → 010 → (i=2 で操作) → 100 とすればよいです.


入力例 2

3
001
110

出力例 2

-1

入力例 3

5
10111
01010

出力例 3

5

Score : 600 points

Problem Statement

Given are length-N strings S and T consisting of 0 and 1. You can do the following operation on S any number of times:

  • Choose i (2 \leq i \leq N) such that S_i=1, and replace S_i with 0. Additionally, invert S_{i-1}, that is, change it to 1 if it is 0 now and vice versa.

Is it possible to make S exactly match T? If it is possible, how many operations does it take at least?

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • T is a string of length N consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

N
S
T

Output

If it is possible to make S exactly match T, print the minimum number of operations it takes. Otherwise, print -1.


Sample Input 1

3
001
100

Sample Output 1

2

We can do as follows: 001 → (choose i=3) → 010 → (choose i=2) → 100.


Sample Input 2

3
001
110

Sample Output 2

-1

Sample Input 3

5
10111
01010

Sample Output 3

5
C - Robots

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

数直線上にロボットがいます. 具体的には,各 i=0,1,2,\cdots,10^{100} について,座標 i1 台のロボットがおり,ロボット i と呼ばれています.

たくさんのボールがあります. それぞれのボールには,正整数が 1 つ書いてあります. これらのボールの情報は,長さ N の整数列 AB で表されます. 具体的には,各 i (1 \leq i \leq N) について,A_i の書かれたボールが B_i 個あります.

今からすぬけくんは,次の操作を行います.

  • Step 1: 0 個以上のボールを選び,そこに書かれている整数を,1 以上 10^{100} 以下の好きな正整数に書き換える.(ボールごとに書き換える整数を選択できる)

  • Step 2: ボールを 1 つずつ食べる.ボールを食べる順番は自由に選べる.ボールを食べるたびに,以下の操作を行う.

    • 今食べたボールに書かれた整数を v とする.ロボット v が存在するなら,それを,現在の座標より 1 小さい座標へ移動させる.もし移動先に別のロボットがいるなら,そのロボットは破壊される.(ロボット v は無事である)

すぬけくんは,ロボット 0 が破壊されないように,すべてのボールを食べきりたいです. すぬけくんが目標を達成するために Step 1 で書き換える必要のあるボールの個数の最小値を求めてください.

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_1 < A_2 < \ldots < A_N \leq 10^9
  • 1 \leq B_i \leq 10^9

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

答えを出力せよ.


入力例 1

3
1 2 3
1 2 1

出力例 1

1

2 の書かれたボールを 1 つ選び,3 に書き換えればよいです. その後,以下の順序でボールを食べればよいです.

  • 2 の書かれたボールを食べる.ロボット 2 を座標 2 から座標 1 へ移動させる. ロボット 1 が破壊される.

  • 3 の書かれたボールを食べる.ロボット 3 を座標 3 から座標 2 へ移動させる.

  • 3 の書かれたボールを食べる.ロボット 3 を座標 2 から座標 1 へ移動させる. ロボット 2 が破壊される.

  • 1 の書かれたボールを食べる.ロボット 1 はすでに破壊されているので,何もしない.


入力例 2

4
1 3 5 7
3 1 4 1

出力例 2

0

Score : 800 points

Problem Statement

There are robots on a number line. Specifically, for each i=0,1,2,\cdots,10^{100}, there is exactly one robot called Robot i at coordinate i.

We have many balls. Each of the balls has a positive integer written on it. Integer sequences A and B, each of length N, describe these balls. Specifically, for each i (1 \leq i \leq N), there are B_i balls with A_i written on them.

Now, Snuke will do the following sequence of operations:

  • Step 1: Choose zero or more balls and replace the integers written on those balls with positive integers of his choice between 1 and 10^{100} (inclusive). (The new integers can be chosen independently from each other.)

  • Step 2: Eat the balls one by one in any order of his choice. Each time he eats a ball, he should do the following:

    • Let v be the integer written on the ball he has just eaten. If Robot v exists, move it so that its coordinate decreases by 1. Here, if there is another robot at the destination, that robot (not Robot v) will be destroyed.

Snuke wants to eat all the balls without destroying Robot 0. Find the minimum number of balls Snuke must choose in Step 1 to achieve his objective.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_1 < A_2 < \ldots < A_N \leq 10^9
  • 1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print the answer.


Sample Input 1

3
1 2 3
1 2 1

Sample Output 1

1

We can achieve the objective by choosing one ball with 2 written on it, rewriting 3 on it, and then eating the balls in the following order:

  • Eat the ball with 2 written on it, moving Robot 2 from coordinate 2 to coordinate 1 and destroying Robot 1.

  • Eat the ball with 3 written on it, moving Robot 3 from coordinate 3 to coordinate 2.

  • Eat the ball with 3 written on it, moving Robot 3 from coordinate 2 to coordinate 1 and destroying Robot 2.

  • Eat the ball with 1 written on it. Robot 1 is already destroyed, so nothing happens.


Sample Input 2

4
1 3 5 7
3 1 4 1

Sample Output 2

0
D - Convex Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

整数 NM が与えられます. 長さ N の非負整数列 (A_1,A_2,\ldots,A_N) であって,次の条件を満たすものの個数を\bmod (10^9+7) で求めてください.

  • A_1+A_2+\ldots +A_N = M
  • すべての i (2 \leq i \leq N-1) について,2 A_i \leq A_{i-1} + A_{i+1}

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 入力はすべて整数である.

入力

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

N M

出力

条件を満たす数列の個数を\bmod (10^9+7) で出力せよ.


入力例 1

3 3

出力例 1

7

以下の 7 個の数列が条件を満たします.

  • 0,0,3
  • 0,1,2
  • 1,0,2
  • 1,1,1
  • 2,0,1
  • 2,1,0
  • 3,0,0

入力例 2

10 100

出力例 2

10804516

入力例 3

10000 100000

出力例 3

694681734

Score : 1000 points

Problem Statement

Given are integers N and M. Find the number, modulo (10^9+7), of length-N sequences (A_1,A_2,\ldots,A_N) that consist of non-negative integers and satisfy the following conditions:

  • A_1+A_2+\ldots +A_N = M;
  • For every i (2 \leq i \leq N-1), 2 A_i \leq A_{i-1} + A_{i+1}.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

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


Sample Input 1

3 3

Sample Output 1

7

The following seven sequences satisfy the conditions.

  • 0,0,3
  • 0,1,2
  • 1,0,2
  • 1,1,1
  • 2,0,1
  • 2,1,0
  • 3,0,0

Sample Input 2

10 100

Sample Output 2

10804516

Sample Input 3

10000 100000

Sample Output 3

694681734
E - Increment Decrement

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1600

問題文

maroon くんは以下のような問題を考えました.


すぬけ君は長さ N の整数列 a を持っています. 最初,すべての i (1 \leq i \leq N) について,a_i=0 です.

すぬけ君は,次の 2 つの操作を好きな順序で好きな回数繰り返します.

  • 操作 1: 好きな整数 i (1 \leq i \leq N) と x (x=1 or x=-1) を選び,a_ia_i+x で置き換える. この操作は,1 回につき 1 のコストがかかる.

  • 操作 2: 好きな整数 l,r (1 \leq l \leq r \leq N) と x (x=1 or x=-1) を選び,すべての i (l \leq i \leq r) について,a_ia_i+x で置き換える. この操作は,1 回につき C のコストがかかる.

長さ N の整数列 A が与えられます. すぬけくんの目標は,すべての i について,a_i=A_i とすることです. 目標を達成するために必要なコストの総和の最小値を求めてください.


しかし,この問題を準備していたところ,想定していない解法がたくさん見つかってしまいました. そこで,以下のように問題を変形しました.


すぬけくんは今,N 個の整数列 B_1,B_2,\cdots,B_N と,整数 C,K を持っています. B_i (1 \leq i \leq N) はすべて長さ K の整数列です.

これからすぬけくんは,長さ N の整数列 A を作り,上記の問題の答えを求めようとしています. A_i の値は,B_{i,1},B_{i,2},\cdots,B_{i,K} から選ぶことにします. ここで,B_i の値に重複があっても,それらの値を区別することにします. つまり,A の作り方は K^N 通り存在します.

すべての A に対する上記の問題の答えの総和を\bmod (10^9+7) で求めてください.


この問題を解いてください.

制約

  • 1 \leq N \leq 50
  • 1 \leq C \leq N
  • 1 \leq K \leq 50
  • 1 \leq B_{i,j} \leq 10^9
  • 入力はすべて整数である.

入力

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

N C K
B_{1,1} B_{1,2} \cdots B_{1,K}
B_{2,1} B_{2,2} \cdots B_{2,K}
\vdots
B_{N,1} B_{N,2} \cdots B_{N,K}

出力

答えを出力せよ.


入力例 1

5 2 1
2
3
1
2
1

出力例 1

6

A=(2,3,1,2,1) です. 最適な操作の一例を以下に示します.

  • l=1,r=5,x=1 として,操作 2 を実行する. a=(1,1,1,1,1) となる.
  • l=1,r=4,x=1 として,操作 2 を実行する. a=(2,2,2,2,1) となる.
  • i=2,x=1 として,操作 1 を実行する. a=(2,3,2,2,1) となる.
  • i=3,x=-1 として,操作 1 を実行する. a=(2,3,1,2,1) となる.

入力例 2

3 2 3
1 2 3
1 2 3
1 2 3

出力例 2

126

入力例 3

10 4 1
8
10
10
1
5
9
5
5
9
1

出力例 3

45

入力例 4

10 5 10
79 48 35 56 16 26 37 6 75 23
39 99 57 100 49 90 18 9 12 91
29 97 49 86 30 94 78 63 49 22
100 27 48 91 66 14 6 20 23 84
12 60 99 75 88 95 61 58 20 46
10 11 30 38 55 94 9 52 92 75
27 22 46 85 83 88 50 63 95 91
49 59 19 37 53 27 11 26 2 91
95 36 20 76 84 41 59 95 67 66
52 60 17 11 28 57 75 69 95 24

出力例 4

877826779

Score : 1600 points

Problem Statement

Maroon came up with the following problem:


Snuke has an integer sequence a of length N. Initially, a_i=0 for every i (1 \leq i \leq N).

Snuke will repeat the following two operations any number of times in any order:

  • Operation 1: Replace a_i with a_i+x, where i (1 \leq i \leq N) and x (x=1 or x=-1) are integers of his choice. The cost of doing this operation once is 1.

  • Operation 2: Replace a_i with a_i+x for every i (l \leq i \leq r), where l, r (1 \leq l \leq r \leq N) and x (x=1 or x=-1) are integers of his choice. The cost of doing this operation once is C.

You are given an integer sequence A of length N. Snuke wants to make a_i=A_i for every i. Find the minimum total cost needed to achieve his objective.


However, during the preparation of this problem, too many unexpected solutions were found, so Maroon changed the problem into the following:


Snuke has N integer sequences B_1,B_2,\cdots,B_N and integers C and K. The length of every sequence B_i (1 \leq i \leq N) is K.

Now, he wants to make an integer sequence A of length N and find the answer to the problem above. The value of A_i will be chosen from B_{i,1},B_{i,2},\cdots,B_{i,K}. Here, even if there are duplicated values in B_{i,1},B_{i,2},\cdots,B_{i,K}, we still distinguish those values. That is, there are K^N ways to make A.

Find the sum of the answers to the problem above over all the ways to make A, modulo (10^9+7).


Solve this problem.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq C \leq N
  • 1 \leq K \leq 50
  • 1 \leq B_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C K
B_{1,1} B_{1,2} \cdots B_{1,K}
B_{2,1} B_{2,2} \cdots B_{2,K}
\vdots
B_{N,1} B_{N,2} \cdots B_{N,K}

Output

Print the answer.


Sample Input 1

5 2 1
2
3
1
2
1

Sample Output 1

6

We have A=(2,3,1,2,1). Below is one optimal sequence of operations:

  • Let l=1,r=5,x=1 and do Operation 2, resulting in a=(1,1,1,1,1).
  • Let l=1,r=4,x=1 and do Operation 2, resulting in a=(2,2,2,2,1).
  • Let i=2,x=1 and do Operation 1, resulting in a=(2,3,2,2,1).
  • Let i=3,x=-1 and do Operation 1, resulting in a=(2,3,1,2,1).

Sample Input 2

3 2 3
1 2 3
1 2 3
1 2 3

Sample Output 2

126

Sample Input 3

10 4 1
8
10
10
1
5
9
5
5
9
1

Sample Output 3

45

Sample Input 4

10 5 10
79 48 35 56 16 26 37 6 75 23
39 99 57 100 49 90 18 9 12 91
29 97 49 86 30 94 78 63 49 22
100 27 48 91 66 14 6 20 23 84
12 60 99 75 88 95 61 58 20 46
10 11 30 38 55 94 9 52 92 75
27 22 46 85 83 88 50 63 95 91
49 59 19 37 53 27 11 26 2 91
95 36 20 76 84 41 59 95 67 66
52 60 17 11 28 57 75 69 95 24

Sample Output 4

877826779
F - Happy Sequence

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 2400

問題文

長さ N の整数列 A,B,C が与えられます. 次の条件が満たされている時,すぬけくんは幸せです.

  • すべての整数 x について, \sum_{1 \leq i \leq N} |A_i-x| \leq \sum_{1 \leq i \leq N} |B_i-x| が成立.

すぬけくんは幸せになるために,A の要素を 0 個以上変更することにしました. A_it に変更するには,C_i \times (A_i-t)^2 のコストがかかります. 変更後の値も整数でなければなりません.

すぬけくんが幸せになるためにかかるコストの合計の最小値を求めてください.

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 2 \times 10^5
  • 0 \leq B_i \leq 2 \times 10^5
  • 1 \leq C_i \leq 5
  • 入力はすべて整数である.

入力

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

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N
C_1 C_2 \cdots C_N

出力

答えを出力せよ.


入力例 1

3
0 1 4
1 2 3
1 3 2

出力例 1

6

次のように操作を行うと,コストの合計は 6 となります.

  • A_12 に変更する.これには 1 \times (0-2)^2=4 のコストがかかる.
  • A_33 に変更する.これには 2 \times (4-3)^2=2 のコストがかかる.

操作後,A=(2,1,3) となりますが,このときすぬけくんは幸せです. 合計コスト 6 未満で目標を達成することはできないので,6 が答えになります.


入力例 2

20
185 89 216 105 56 383 193 161 75 196 322 180 390 15 206 78 275 338 225 167
161 77 294 117 22 382 218 140 57 231 343 160 397 8 264 68 301 349 295 157
3 1 3 5 2 1 3 4 1 4 2 2 2 2 5 1 1 5 4 3

出力例 2

3758

入力例 3

1
0
0
1

出力例 3

0

Score : 2400 points

Problem Statement

Integer sequences A, B and C, each of length N, are given. Snuke is happy if and only if the following condition is met.

  • For every integer x, \sum_{1 \leq i \leq N} |A_i-x| \leq \sum_{1 \leq i \leq N} |B_i-x| holds.

He decided to change at least 0 elements of A to become happy. It costs him C_i \times (A_i-t)^2 to change A_i to t. Here, t, the value after the change, should be an integer as well.

Find the minimum possible sum of costs in order for Snuke to become happy.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 2 \times 10^5
  • 0 \leq B_i \leq 2 \times 10^5
  • 1 \leq C_i \leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N
C_1 C_2 \cdots C_N

Output

Print the answer.


Sample Input 1

3
0 1 4
1 2 3
1 3 2

Sample Output 1

6

After the following operations, the sum of costs is 6.

  • Change A_1 to 2. This change costs 1 \times (0-2)^2=4.
  • Change A_3 to 3. This change costs 2 \times (4-3)^2=2.

After the operations, A=(2,1,3) holds, which makes Snuke happy. It is impossible to achieve the goal with sum of costs less than 6, so the answer is 6.


Sample Input 2

20
185 89 216 105 56 383 193 161 75 196 322 180 390 15 206 78 275 338 225 167
161 77 294 117 22 382 218 140 57 231 343 160 397 8 264 68 301 349 295 157
3 1 3 5 2 1 3 4 1 4 2 2 2 2 5 1 1 5 4 3

Sample Output 2

3758

Sample Input 3

1
0
0
1

Sample Output 3

0