A - Determinant

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

2 \times 2 行列 A = \begin{bmatrix} a & b \\ c & d \end{bmatrix} が与えられます。
A の行列式は ad-bc で求められます。
A の行列式を求めてください。

制約

  • 入力は全て整数
  • -100 \le a, b, c, d \le 100

入力

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

a b
c d

出力

答えを整数で出力せよ。


入力例 1

1 2
3 4

出力例 1

-2

\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} の行列式は 1 \times 4 - 2 \times 3 = -2 です。


入力例 2

0 -1
1 0

出力例 2

1

入力例 3

100 100
100 100

出力例 3

0

Score : 100 points

Problem Statement

Given is a 2 \times 2 matrix A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}.
The determinant of A can be found as ad-bc.
Find it.

Constraints

  • All values in input are integers.
  • -100 \le a, b, c, d \le 100

Input

Input is given from Standard Input in the following format:

a b
c d

Output

Print the answer as an integer.


Sample Input 1

1 2
3 4

Sample Output 1

-2

The determinant of \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} is 1 \times 4 - 2 \times 3 = -2.


Sample Input 2

0 -1
1 0

Sample Output 2

1

Sample Input 3

100 100
100 100

Sample Output 3

0
B - Quizzes

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋くんは、 N 問のクイズに答えます。
高橋くんの持っている点数ははじめ X 点で、クイズに正解すると 1 点増え、不正解だと 1 点減ります。
ただし、持っている点数が 0 点のときに不正解となった場合は点数は減りません。

高橋くんのクイズの結果が文字列 S で与えられます。
S の左から i 番目の文字が o のとき、 i 問目が正解だったことを、 x のとき、 i 問目が不正解だったことを表します。
高橋くんの最終的な点数はいくつでしょうか?

制約

  • 1 \le N \le 2 \times 10^5
  • 0 \le X \le 2 \times 10^5
  • Sox からなる長さ N の文字列

入力

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

N X
S

出力

高橋くんの最終的な点数を出力せよ。


入力例 1

3 0
xox

出力例 1

0

はじめ、高橋くんの点数は 0 です。
1 問目は不正解ですが、点数が 0 なので減りません。
2 問目は正解なので、点数が増えて 1 になります。
3 問目は不正解なので、点数が減って 0 になります。 高橋くんの最終的な点数は 0 なので、0 を出力します。


入力例 2

20 199999
oooooooooxoooooooooo

出力例 2

200017

入力例 3

20 10
xxxxxxxxxxxxxxxxxxxx

出力例 3

0

Score : 200 points

Problem Statement

Takahashi will answer N quiz questions.
Initially, he has X points. Each time he answers a question, he gains 1 point if his answer is correct and loses 1 point if it is incorrect.
However, there is an exception: when he has 0 points, he loses nothing for answering a question incorrectly.

You will be given a string S representing whether Takahashi's answers are correct.
If the i-th character of S from the left is o, it means his answer for the i-th question is correct; if that character is x, it means his answer for the i-th question is incorrect.
How many points will he have in the end?

Constraints

  • 1 \le N \le 2 \times 10^5
  • 0 \le X \le 2 \times 10^5
  • S is a string of length N consisting of o and x.

Input

Input is given from Standard Input in the following format:

N X
S

Output

Print the number of points Takahashi will have in the end.


Sample Input 1

3 0
xox

Sample Output 1

0

Initially, he has 0 points.
He answers the first question incorrectly but loses nothing because he has no point.
Then, he answers the second question correctly, gains 1 point, and now has 1 point.
Finally, he answers the third question incorrectly, loses 1 point, and now has 0 points.
Thus, he has 0 points in the end. We should print 0.


Sample Input 2

20 199999
oooooooooxoooooooooo

Sample Output 2

200017

Sample Input 3

20 10
xxxxxxxxxxxxxxxxxxxx

Sample Output 3

0
C - Super Ryuma

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

無限に広がる 2 次元グリッドがあり、マス (r_1, c_1) に駒「超竜馬」が置かれています。
この駒は、 1 手で次のような動きができます。

より正確には、超竜馬がマス (a, b) にあるとき、以下のいずれかの条件を満たすマス (c, d) に動かすことができます。

  • a + b = c + d
  • a - b = c - d
  • |a - c| + |b - d| \le 3

超竜馬を (r_1, c_1) から (r_2, c_2) に動かすのに必要な最小手数を求めてください。

制約

  • 入力は全て整数
  • 1 \le r_1, c_1, r_2, c_2 \le 10^9

入力

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

r_1 c_1
r_2 c_2

出力

超竜馬を (r_1, c_1) から (r_2, c_2) に動かすのに必要な最小手数を出力せよ。


入力例 1

1 1
5 6

出力例 1

2

例えば、 (1, 1) \rightarrow (5, 5) \rightarrow (5, 6) と動かすと 2 手になります。


入力例 2

1 1
1 200001

出力例 2

2

例えば、 (1, 1) \rightarrow (100001, 100001) \rightarrow (1, 200001) と動かすと 2 手になります。


入力例 3

2 3
998244353 998244853

出力例 3

3

例えば、 (2, 3) \rightarrow (3, 3) \rightarrow (-247, 253) \rightarrow (998244353, 998244853) と動かすと 3 手になります。


入力例 4

1 1
1 1

出力例 4

0

Score : 300 points

Problem Statement

There is an infinite two-dimensional grid, and we have a piece called Super Ryuma at square (r_1, c_1). (Ryu means dragon and Ma means horse.) In one move, the piece can go to one of the squares shown below:

More formally, when Super Ryuma is at square (a, b), it can go to square (c, d) such that at least one of the following holds:

  • a + b = c + d
  • a - b = c - d
  • |a - c| + |b - d| \le 3

Find the minimum number of moves needed for the piece to reach (r_2, c_2) from (r_1, c_1).

Constraints

  • All values in input are integers.
  • 1 \le r_1, c_1, r_2, c_2 \le 10^9

Input

Input is given from Standard Input in the following format:

r_1 c_1
r_2 c_2

Output

Print the minimum number of moves needed for Super Ryuma to reach (r_2, c_2) from (r_1, c_1).


Sample Input 1

1 1
5 6

Sample Output 1

2

We need two moves - for example, (1, 1) \rightarrow (5, 5) \rightarrow (5, 6).


Sample Input 2

1 1
1 200001

Sample Output 2

2

We need two moves - for example, (1, 1) \rightarrow (100001, 100001) \rightarrow (1, 200001).


Sample Input 3

2 3
998244353 998244853

Sample Output 3

3

We need three moves - for example, (2, 3) \rightarrow (3, 3) \rightarrow (-247, 253) \rightarrow (998244353, 998244853).


Sample Input 4

1 1
1 1

Sample Output 4

0
D - increment of coins

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

袋の中に金貨が A 枚、銀貨が B 枚、銅貨が C 枚入っています。

袋の中にあるいずれかの種類の硬貨が 100 枚になるまで以下の操作を繰り返します。

操作:袋の中から硬貨をランダムに 1 枚取り出す。(どの硬貨も等確率で選ばれる。) 取り出した硬貨と同じ種類の硬貨を 2 枚袋に戻す。

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

制約

  • 0 \leq A,B,C \leq 99
  • A+B+C \geq 1

入力

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

A B C

出力

操作回数の期待値を出力せよ。正しい値との絶対誤差または相対誤差が 10^{-6} 以下であれば正解とみなされる。


入力例 1

99 99 99

出力例 1

1.000000000

1 回目の操作でどの硬貨を取り出しても、取り出した種類の硬貨が 100 枚になります。


入力例 2

98 99 99

出力例 2

1.331081081

1 回目の操作で金貨を取り出したときのみ 2 回の操作を行います。 よって期待値は 2\times \frac{98}{98+99+99}+1\times \frac{99}{98+99+99}+1\times \frac{99}{98+99+99}=1.331081081\ldots となります。


入力例 3

0 0 1

出力例 3

99.000000000

操作を行うごとに銅貨が 1 枚ずつ増えていきます。


入力例 4

31 41 59

出力例 4

91.835008202

Score : 400 points

Problem Statement

We have a bag containing A gold coins, B silver coins, and C bronze coins.

Until the bag contains 100 coins of the same color, we will repeat the following operation:

Operation: Randomly take out one coin from the bag. (Every coin has an equal probability of being chosen.) Then, put back into the bag two coins of the same kind as the removed coin.

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

Constraints

  • 0 \leq A,B,C \leq 99
  • A+B+C \geq 1

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the expected value of the number of times the operation is done. Your output will be accepted if its absolute or relative error from the correct value is at most 10^{-6}.


Sample Input 1

99 99 99

Sample Output 1

1.000000000

No matter what coin we take out in the first operation, the bag will contain 100 coins of that kind.


Sample Input 2

98 99 99

Sample Output 2

1.331081081

We will do the second operation only if we take out a gold coin in the first operation. Thus, the expected number of operations is 2\times \frac{98}{98+99+99}+1\times \frac{99}{98+99+99}+1\times \frac{99}{98+99+99}=1.331081081\ldots


Sample Input 3

0 0 1

Sample Output 3

99.000000000

Each operation adds a bronze coin.


Sample Input 4

31 41 59

Sample Output 4

91.835008202
E - Third Avenue

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

H マス、横 W マスの 2 次元グリッドで表された街があります。
上から i 行目、左から j 列目のマスの情報が文字 a_{i,j} により与えられます。 a_{i,j}S , G , . , # , a ~ z のいずれかです。
# は入ることができないマスを、a ~ z はテレポーターのあるマスを表します。

高橋くんははじめ S のマスにおり、 1 秒ごとに以下のいずれかの移動を行います。

  • 現在いるマスと上下左右に隣り合う、# でないマスに移動する。
  • 今いるマスと同じ文字が書かれているマスを 1 つ選び、そこにテレポートする。この移動は現在いるマスが a ~ z のいずれかであるとき使える。

高橋くんが S のマスから G のマスに移動するのに必要な最短の時間を求めてください。
ただし、どうしても G のマスにたどり着けない場合は、代わりに -1 を出力してください。

制約

  • 1 \le H, W \le 2000
  • a_{i,j}S , G , . , # , 英小文字のいずれか
  • S のマスと G のマスはちょうど 1 つ存在する

入力

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

H W
a_{1,1}\dots a_{1,W}
\vdots
a_{H,1}\dots a_{H,W}

出力

S のマスから G のマスに移動するのに必要な最短時間を出力せよ。
S のマスから G のマスに移動する方法が存在しない場合は、代わりに -1 を出力せよ。


入力例 1

2 5
S.b.b
a.a.G

出力例 1

4

上から i 行目、左から j 列目のマスを (i, j) で表すこととします。
はじめ、高橋くんは (1, 1) にいます。 例えば、以下のような手順で 4 秒で (2, 5) に移動することができます。

  • (1, 1) から (2, 1) に移動する
  • (2, 1) と同じ a のマスである (2, 3) にテレポートする
  • (2, 3) から (2, 4) に移動する
  • (2, 4) から (2, 5) に移動する

入力例 2

11 11
S##...#c...
...#d.#.#..
..........#
.#....#...#
#.....bc...
#.##......#
.......c..#
..#........
a..........
d..#...a...
.#........G

出力例 2

14

入力例 3

11 11
.#.#.e#a...
.b..##..#..
#....#.#..#
.#dd..#..#.
....#...#e.
c#.#a....#.
.....#..#.e
.#....#b.#.
.#...#..#..
......#c#G.
#..S...#...

出力例 3

-1

Score : 500 points

Problem Statement

There is a town represented as a two-dimensional grid with H horizontal rows and W vertical columns.
A character a_{i,j} describes the square at the i-th row from the top and j-th column from the left. Here, a_{i,j} is one of the following: S , G , . , # , a, ..., and z.
# represents a square that cannot be entered, and a, ..., z represent squares with teleporters.

Takahashi is initially at the square represented as S. In each second, he will make one of the following moves:

  • Go to a non-# square that is horizontally or vertically adjacent to his current position.
  • Choose a square with the same character as that of his current position, and teleport to that square. He can only use this move when he is at a square represented as a, ..., or z.

Find the shortest time Takahashi needs to reach the square represented as G from the one represented as S.
If the destination is unreachable, report -1 instead.

Constraints

  • 1 \le H, W \le 2000
  • a_{i,j} is S, G, ., #, or a lowercase English letter.
  • There is exactly one square represented as S and one square represented as G.

Input

Input is given from Standard Input in the following format:

H W
a_{1,1}\dots a_{1,W}
\vdots
a_{H,1}\dots a_{H,W}

Output

Print the shortest time Takahashi needs to reach the square represented as G from the one represented as S.
If the destination is unreachable from the initial position, print -1 instead.


Sample Input 1

2 5
S.b.b
a.a.G

Sample Output 1

4

Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
Initially, Takahashi is at (1, 1). One way to reach (2, 5) in four seconds is:

  • go from (1, 1) to (2, 1);
  • teleport from (2, 1) to (2, 3), which is also an a square;
  • go from (2, 3) to (2, 4);
  • go from (2, 4) to (2, 5).

Sample Input 2

11 11
S##...#c...
...#d.#.#..
..........#
.#....#...#
#.....bc...
#.##......#
.......c..#
..#........
a..........
d..#...a...
.#........G

Sample Output 2

14

Sample Input 3

11 11
.#.#.e#a...
.b..##..#..
#....#.#..#
.#dd..#..#.
....#...#e.
c#.#a....#.
.....#..#.e
.#....#b.#.
.#...#..#..
......#c#G.
#..S...#...

Sample Output 3

-1
F - Programming Contest

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋くんはプログラミングコンテストに参加します。 このコンテストのコンテスト時間は T 分間で、 N 問の問題が出題されます。
高橋くんは超能力者なので、 i 番目の問題が A_i 分で解けることが分かっています。
高橋くんは N 問の中から 0 問以上を、解くのにかかる時間の総和が T 分以下になるように選び、それらの問題を解きます。
選んだ問題を解くのにかかる時間の総和の最大値を求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 40
  • 1 \le T \le 10^9
  • 1 \le A_i \le 10^9

入力

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

N T
A_1 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

5 17
2 3 5 7 11

出力例 1

17

1,2,3,4 問目を選ぶと、解くのにかかる時間の総和が 2+3+5+7=17 分となり、 T=17 分以下での最大になります。


入力例 2

6 100
1 2 7 5 8 10

出力例 2

33

全ての問題を解くのが最適です。


入力例 3

6 100
101 102 103 104 105 106

出力例 3

0

どの問題も解くことができません。


入力例 4

7 273599681
6706927 91566569 89131517 71069699 75200339 98298649 92857057

出力例 4

273555143

2,3,7 問目を選ぶと、解くのにかかる時間の総和が 273555143 分になります。

Score : 600 points

Problem Statement

Takahashi will participate in a programming contest, which lasts for T minutes and presents N problems.
With his extrasensory perception, he already knows that it will take A_i minutes to solve the i-th problem.
He will choose zero or more problems to solve from the N problems so that it takes him no longer than T minutes in total to solve them.
Find the longest possible time it takes him to solve his choice of problems.

Constraints

  • All values in input are integers.
  • 1 \le N \le 40
  • 1 \le T \le 10^9
  • 1 \le A_i \le 10^9

Input

Input is given from Standard Input in the following format:

N T
A_1 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 17
2 3 5 7 11

Sample Output 1

17

If he chooses the 1-st, 2-nd, 3-rd, and 4-th problems, it takes him 2+3+5+7=17 minutes in total to solve them, which is the longest possible time not exceeding T=17 minutes.


Sample Input 2

6 100
1 2 7 5 8 10

Sample Output 2

33

It is optimal to solve all the problems.


Sample Input 3

6 100
101 102 103 104 105 106

Sample Output 3

0

He cannot solve any of the problems.


Sample Input 4

7 273599681
6706927 91566569 89131517 71069699 75200339 98298649 92857057

Sample Output 4

273555143

If he chooses the 2-nd, 3-rd, and 7-th problems, it takes him 273555143 minutes in total to solve them.