A - Keyboard

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

YN のいずれかである文字 S と、 abc のいずれかである文字 T が与えられます。

SY ならば T の文字を大文字にしたものを出力してください。

SN ならば T の文字をそのまま出力してください。

制約

  • SYN のいずれか
  • Tabc のいずれか

入力

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

S
T

出力

問題文の通りに文字を出力せよ。


入力例 1

Y
a

出力例 1

A

SY なので、T の文字 a を大文字にした A を出力します。


入力例 2

N
b

出力例 2

b

SN なので、T の文字 b をそのまま出力します。

Score : 100 points

Problem Statement

Given are a character S, which is Y or N, and another character T, which is A, B, or C.

If S is Y, print T uppercased.

If S is N, print T as it is.

Constraints

  • S is Y or N.
  • T is a, b, or c.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print a character as specified in the problem statement.


Sample Input 1

Y
a

Sample Output 1

A

Since S is Y, we should print T - which is a - uppercased, that is, A.


Sample Input 2

N
b

Sample Output 2

b

Since S is Y, we should print T - which is b - as it is.

B - Futon

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

H 行、横 W 列からなるマス目があり、それぞれのマスは散らかっているか散らかっていないかのどちらかです。

今からあなたはこのマス目に 1 つ布団を敷きます。

縦または横に隣接するマス目の内部の 2 マスであって、いずれのマスも散らかっていない場所に布団を敷くことができます。

整数 H, WH 個の長さ W の文字列 S_i が与えられます。 S_ij 文字目が . のとき、上から i 行目、左から j 列目のマスは散らかっていません。S_ij 文字目が # のとき、上から i 行目、左から j 列目のマスは散らかっています。

布団を敷く場所の選び方は全部で何通りあるか求めてください。

制約

  • 2 \leq H \leq 100
  • 2 \leq W \leq 100
  • S_i.# のみからなる長さ W の文字列

入力

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

H W
S_1
:
S_H

出力

布団を敷く場所の選び方の総数を出力せよ。


入力例 1

2 3
..#
#..

出力例 1

3

次の 3 通りの選び方があります。

  • 上から 1 行目の左から 1 列目と 2 列目
  • 上から 2 行目の左から 2 列目と 3 列目
  • 左から 2 列目の上から 1 行目と 2 行目

入力例 2

2 2
.#
#.

出力例 2

0

Score : 200 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns of squares, where each square is tidy or untidy.

You will now place a mattress on this grid.

The mattress can be placed on two horizontally or vertically adjacent tidy squares in the grid.

Given are integers H, W, and H strings S_i of length W each. If the j-th character of S_i is ., the square at the i-th row from the top and the j-th column from the left is tidy; if the j-th character of S_i is #, the square at the i-th row from the top and the j-th column from the left is untidy.

Find the number of ways to choose the position for the mattress.

Constraints

  • 2 \leq H \leq 100
  • 2 \leq W \leq 100
  • S_i is a string of length W consisting of . and #.

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the number of ways to choose the position for the mattress.


Sample Input 1

2 3
..#
#..

Sample Output 1

3

We have the following three choices:

  • the 1-st and 2-nd squares from the left in the 1-st row from the top;
  • the 2-nd and 3-rd squares from the left in the 2-nd row from the top; and
  • the 1-st and 2-nd squares from the top in the 2-nd column the left.

Sample Input 2

2 2
.#
#.

Sample Output 2

0
C - Neq Min

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の数列 p_1, ... , p_N が与えられます。

i=1, 2, ..., N について、 0 以上の整数で p_1,...,p_i のいずれとも等しくない値のうち最小値を求めてください。

制約

  • 1 \leq N \leq 200,000
  • 0 \leq p_i \leq 200,000
  • 入力は全て整数

入力

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

N
p_1 ... p_N

出力

合計 N 行出力せよ。

i 行目 (1 \leq i \leq N) には 0 以上の整数で p_1,...,p_i のいずれとも等しくない値のうち最小値を出力せよ。


入力例 1

4
1 1 0 2

出力例 1

0
0
2
3
  • 0 以上の整数で p_1=1 と等しくない最小値は 0 です。
  • 0 以上の整数で p_1=1, p_2=1 のいずれとも等しくない最小値は 0 です。
  • 0 以上の整数で p_1=1, p_2=1, p_3=0 のいずれとも等しくない最小値は 2 です。
  • 0 以上の整数で p_1=1, p_2=1, p_3=0, p_4=2 のいずれとも等しくない最小値は 3 です。

入力例 2

10
5 4 3 2 1 0 7 7 6 6

出力例 2

0
0
0
0
0
6
6
6
8
8

Score : 300 points

Problem Statement

Given is a number sequence of length N: p_1, ..., p_N.

For each i=1, 2, ..., N, find the minimum non-negative integer that is not equal to any of the numbers p_1, ..., p_i.

Constraints

  • 1 \leq N \leq 200,000
  • 0 \leq p_i \leq 200,000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_1 ... p_N

Output

Print N lines in total.

The i-th line (1 \leq i \leq N) should contain the minimum non-negative integer that is not equal to any of the numbers p_1, ..., p_i.


Sample Input 1

4
1 1 0 2

Sample Output 1

0
0
2
3
  • The minimum non-negative integer that is not equal to p_1=1 is 0.
  • The minimum non-negative integer that is not equal to any of p_1=1, p_2=1 is 0.
  • The minimum non-negative integer that is not equal to any of p_1=1, p_2=1, p_3=0 is 2.
  • The minimum non-negative integer that is not equal to any of p_1=1, p_2=1, p_3=0, p_4=2 is 3.

Sample Input 2

10
5 4 3 2 1 0 7 7 6 6

Sample Output 2

0
0
0
0
0
6
6
6
8
8
D - Squares

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

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

辺の長さが N の白い正方形を座標平面の (0,0), (N,0), (0,N), (N,N)4 頂点が重なるように置きます。

次に、この白い正方形の内部または周上に収まるように、辺の長さが A の青い正方形と辺の長さが B の赤い正方形を 1 つずつ置きます。

ただし、正方形のどの辺も x 軸または y 軸と平行に置かれている必要があります。

また、青い正方形と赤い正方形の各頂点は格子点上に置かれている必要があります。

赤い正方形の内部と青い正方形の内部が重ならないように置く方法の数を 1,000,000,007 で割ったあまりを求めてください。

1 つの入力につき、T 個のテストケースに答えてください。

制約

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^9
  • 1 \leq A \leq N
  • 1 \leq B \leq N
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。入力の 1 行目は以下のとおりである。

T

そして、 T 個のテストケースが続く。これらはそれぞれ以下の形式で与えられる。

N A B

出力

各テストケースについて、赤い正方形と青い正方形の内部が重ならないように置く方法の数を 1,000,000,007 で割ったあまりを出力せよ。 各テストケースごとに改行せよ。


入力例 1

3
3 1 2
4 2 2
331895368 154715807 13941326

出力例 1

20
32
409369707

例えば最初のテストケースでは、重なりを気にせず青い正方形を置く方法が 9 通り、赤い正方形を置く方法が 4 通りあります。

赤い正方形をどこに置いても、赤い正方形の内部に重なるような青い正方形の置き方は 4 通りずつあります。

よって、赤い正方形の内部と青い正方形の内部が重ならないように置く方法の数は、 9 \times 4 - 4 \times 4 = 20 通りです。

赤い正方形と青い正方形が周上のみを共有するとき、例えば青い正方形の左下の頂点が (0,0)、赤い正方形の左下の頂点が (0,1) のときは赤い正方形と青い正方形の内部が重なっていないことに注意してください。

Score : 400 points

Problem Statement

Given are integers N, A, and B.

We will place a white square whose side is of length N on the coordinate plane so that the vertices are at (0, 0), (N, 0), (0, N), and (N, N).

Then, we will place a blue square whose side is of length A and a red square whose side is of length B so that these squares are inside the white square (including its boundary).

Here, each side of these squares must be parallel to the x- or y-axis.

Additionally, each vertex of red and blue squares must be at an integer point.

Find the number of ways to place red and blue squares so that they do not strictly overlap (they may share boundary with each other), modulo 1,000,000,007.

Solve this problem for T test cases in each input file.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^9
  • 1 \leq A \leq N
  • 1 \leq B \leq N
  • All values in input are integers.

Input

Input is given from Standard Input as follows. The first line of input is in the format below:

T

Then, T test cases follow, each of which is in the format below:

N A B

Output

For each test case, print the number of ways to place red and blue squares so that they do not strictly overlap, modulo 1,000,000,007. Use one line for each test case.


Sample Input 1

3
3 1 2
4 2 2
331895368 154715807 13941326

Sample Output 1

20
32
409369707

For example, in the first test case, there are 9 ways to place the blue squares and 4 ways to place the red squares, ignoring overlap.

Wherever we place the red square, there are 4 ways to place the blue square so that it strictly overlaps with the red square.

Thus, there are 9 \times 4 - 4 \times 4 = 20 ways to place red and blue squares so that they do not strictly overlap.

Note that the squares may share boundary with each other. For example, when the bottom-left vertices of blue and red squares are (0, 0) and (0, 1), respectively, the squares do not strictly overlap.

E - Lamps

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

H 行、横 W 列からなるマス目があり、それぞれのマスは散らかっているか散らかっていないかのどちらかです。

今からあなたはこのマス目のうち 0 個以上の散らかっていないマスに照明を置きます。

照明は置かれたマスの上下左右の 4 方向に、マス目の端もしくは最初に散らかっているマスにぶつかる直前まで照らします (散らかっているマスは照らされません)。照明は、置かれたマス自身も照らします。

整数 H, WH 個の長さ W の文字列 S_i が与えられます。 S_ij 文字目が . のとき、上から i 行目、左から j 列目のマスは散らかっていません。S_ij 文字目が # のとき、上から i 行目、左から j 列目のマスは散らかっています。

散らかっていないマスの個数を K 個だとすると、照明の置き方は全部で 2^K 通りあります。 この 2^K 通りそれぞれについて、1 個以上の照明によって照らされるマスの個数を計算したときの総和を 1,000,000,007 で割ったあまりを求めてください。

制約

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • S_i.# のみからなる長さ W の文字列

入力

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

H W
S_1
:
S_H

出力

2^K 通りそれぞれについて、1 個以上の照明によって照らされるマスの個数を計算したときの総和を 1,000,000,007 で割ったあまりを出力せよ。


入力例 1

1 5
..#..

出力例 1

48

全部で照明の置き方は 16 通りあります。

  • このうち 9 通り (左から 1 番目か 2 番目の少なくとも一方に照明が置かれている、かつ左から 4 番目か 5 番目の少なくとも一方に照明が置かれている) では、4 マスが照らされます。
  • このうち 3 通り (左から 1 番目か 2 番目の少なくとも一方に照明が置かれている、かつ左から 4 番目と 5 番目のどちらにも照明が置かれていない) では、2 マスが照らされます。
  • このうち 3 通り (左から 4 番目か 5 番目の少なくとも一方に照明が置かれている、かつ左から 1 番目と 2 番目のどちらにも照明が置かれていない) では、2 マスが照らされます。
  • このうち 1 通り (照明が 1 つも置かれていない) では、0 マスが照らされます。

求める総和は 4 \times 9 + 2 \times 3 + 2 \times 3 + 0 \times 1 = 48 となります。


入力例 2

2 3
..#
#..

出力例 2

52

Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns of squares, where each square is tidy or untidy.

You will now place lamps on zero or more tidy squares in this grid.

A lamp will lighten the squares in each of the four directions - up, down, left, and right - to the point just before an edge of the grid or an untidy square is reached for the first time (the untidy square will not be lighted). The lamp will also lighten the square on which it is placed.

Given are integers H, W, and H strings S_i of length W each. If the j-th character of S_i is ., the square at the i-th row from the top and the j-th column from the left is tidy; if the j-th character of S_i is #, the square at the i-th row from the top and the j-th column from the left is untidy.

Let K be the number of tidy squares. There are 2^K ways to place the lamps in total. Assume that, for each of these 2^K ways, the number of squares lightened by one or more lamps is computed. Find the sum of those numbers modulo 1,000,000,007.

Constraints

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • S_i is a string of length W consisting of . and #.

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the sum, modulo 1,000,000,007, of the numbers of squares lightened by one or more lamps over the 2^K ways to place the lamps.


Sample Input 1

1 5
..#..

Sample Output 1

48

There are 16 ways to place the lamps in total.

  • In 9 of the ways (where at least one of the 1-st and 2-nd squares from the left contains a lamp and at least one of the 4-th and 5-th squares from the left contains a lamp), 4 squares will be lighted.
  • In 3 of the ways (where at least one of the 1-st and 2-nd squares from the left contains a lamp but none of the 4-th and 5-th squares from the left contains a lamp), 2 squares will be lighted.
  • In another 3 of the ways (where none of the 1-st and 2-nd squares from the left contains a lamp but at least one of the 4-th and 5-th squares from the left contains a lamp), 2 squares will be lighted.
  • In 1 of the ways (where no square contains a lamp), 0 squares will be lighted.

The sum in question is 4 \times 9 + 2 \times 3 + 2 \times 3 + 0 \times 1 = 48.


Sample Input 2

2 3
..#
#..

Sample Output 2

52
F - Random Max

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個の連続型確率変数 x_i (1 ≤ i ≤ N) があり、それぞれ [L_i, R_i] の範囲をとる連続一様分布にしたがいます。 (すなわち、x_iL_i 以上 R_i 以下の実数を等確率でとりうるランダムな変数です)

本問題の制約下では、これらの N 個の確率変数の最大値の期待値を E とすると、E \times (N+1)! \times \prod_{i=1}^N (R_i - L_i) は正整数であることが示せます。この値を 1,000,000,007 で割ったあまりを求めてください。

制約

  • 1 ≤ N ≤ 1000
  • 0 ≤ L_i < R_i ≤ 10^9
  • 入力は全て整数

入力

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

N
L_1 R_1
:
L_N R_N

出力

E \times (N+1)! \times \prod_{i=1}^N (R_i - L_i)1,000,000,007 で割ったあまりを整数で出力せよ。


入力例 1

1
1 2

出力例 1

3

この確率変数の最大値の期待値は、とりうる範囲の中央値、すなわち E = \frac{3}{2} に等しいです。

よって、 E \times (N+1)! \times (R_1 - L_1) = E \times 2 = 3 が正解となります。


入力例 2

2
1 2
1 2

出力例 2

10

求める期待値は E = \frac{5}{3} です。


入力例 3

2
1 2
2 4

出力例 3

36

入力例 4

5
40 96
81 92
16 384
32 768
65 536

出力例 4

52776507

Score : 600 points

Problem Statement

There are N continuous random variables x_i (1 ≤ i ≤ N), each of which follows the continuous uniform distribution on the interval [L_i, R_i]. (That is, x_i is a random variable that can take any real value between L_i and R_i (inclusive) with equal probability.)

Let E be the expected value of the maximum of these N random variables. Under the constraints in this problem, it can be proved that E \times (N+1)! \times \prod_{i=1}^N (R_i - L_i) is a positive integer. Find this value modulo 1,000,000,007.

Constraints

  • 1 ≤ N ≤ 1000
  • 0 ≤ L_i < R_i ≤ 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
:
L_N R_N

Output

Print E \times (N+1)! \times \prod_{i=1}^N (R_i - L_i) modulo 1,000,000,007, as an integer.


Sample Input 1

1
1 2

Sample Output 1

3

The expected value of the maximum of these random variables - there is actually just one in this case - is equal to the median of the range of the values the variable can take, that is, E = \frac{3}{2}.

Thus, the correct output is E \times (N+1)! \times (R_1 - L_1) = E \times 2 = 3.


Sample Input 2

2
1 2
1 2

Sample Output 2

10

The expected value in question is E = \frac{5}{3}.


Sample Input 3

2
1 2
2 4

Sample Output 3

36

Sample Input 4

5
40 96
81 92
16 384
32 768
65 536

Sample Output 4

52776507