A - Airplane

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

空港 A, B, C があり、それぞれの空港の間では、双方向に飛行機が運航しています。

空港 A, B 間の飛行時間は片道 P 時間、空港 B, C 間の飛行時間は片道 Q 時間、空港 C, A 間の飛行時間は片道 R 時間です。

いずれかの空港からスタートして他の空港に飛行機で移動し、さらにそのどちらでもない空港に飛行機で移動するような経路を考えます。

飛行時間の和は最短で何時間になるでしょうか。

制約

  • 1 \leq P,Q,R \leq 100
  • 入力は全て整数である

入力

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

P Q R

出力

飛行時間の和の最小値を出力せよ。


入力例 1

1 3 4

出力例 1

4

A, B, C の順に移動する経路の飛行時間の和は、1 + 3 = 4 時間

A, C, B の順に移動する経路の飛行時間の和は、4 + 3 = 7 時間

B, A, C の順に移動する経路の飛行時間の和は、1 + 4 = 5 時間

B, C, A の順に移動する経路の飛行時間の和は、3 + 4 = 7 時間

C, A, B の順に移動する経路の飛行時間の和は、4 + 1 = 5 時間

C, B, A の順に移動する経路の飛行時間の和は、3 + 1 = 4 時間

であるので、これらの最小値は 4 時間です。


入力例 2

3 2 3

出力例 2

5

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

  • 1 \leq P,Q,R \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.


Sample Input 1

1 3 4

Sample Output 1

4
  • The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours
  • The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours
  • The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours
  • The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours
  • The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours
  • The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.


Sample Input 2

3 2 3

Sample Output 2

5
B - Balance

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1 から N の番号がついた N 個の重りがあり、番号 i の重りの重さは W_i です。

ある整数 1 \leq T < N に対してこれらの重りを、番号が T 以下の重り と 番号が T より大きい重りの 2 グループに分けることを考え、それぞれのグループの重さの和を S_1, S_2 とします。

このような分け方全てを考えた時、S_1S_2 の差の絶対値の最小値を求めてください。

制約

  • 2 \leq N \leq 100
  • 1 \leq W_i \leq 100
  • 入力は全て整数である

入力

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

N
W_1 W_2 ... W_{N-1} W_N

出力

S_1S_2 の差の絶対値の最小値を出力せよ。


入力例 1

3
1 2 3

出力例 1

0

T = 2 としたとき、S_1 = 1 + 2 = 3, S_2 = 3 となり、差の絶対値は 0 となります。


入力例 2

4
1 3 1 1

出力例 2

2

T = 2 としたとき、S_1 = 1 + 3 = 4, S_2 = 1 + 1 = 2 となり、差の絶対値は 2 です。これより差の絶対値を小さくすることは出来ません。


入力例 3

8
27 23 76 2 3 5 62 52

出力例 3

2

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq W_i \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.


Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.


Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.


Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2
C - Typical Stairs

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 段の階段があります。高橋君は現在、上り口(0 段目)にいます。 高橋君は一歩で 1 段か 2 段上ることができます。

ただし、a_1,a_2,a_3,....a_M 段目の床は壊れており、その段に足を踏み入れることは危険です。

壊れている床を踏まないようにしながら、最上段(N 段目)にたどりつくまでの移動方法は何通りあるでしょうか? 総数を 1,000,000,007 で割った余りを求めてください。

制約

  • 1 \leqq N \leqq 10^5
  • 0 \leqq M \leqq N-1
  • 1 \leqq a_1 < a_2 < ... < a_M \leqq N-1

入力

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

N M
a_1
a_2
 .
 .
 .
a_M

出力

条件を満たすような移動方法の総数を、1,000,000,007 で割った余りを出力してください。


入力例 1

6 1
3

出力例 1

4

移動方法は以下の 4 通りです。

  • 0 \to 1 \to 2 \to 4 \to 5 \to 6
  • 0 \to 1 \to 2 \to 4 \to 6
  • 0 \to 2 \to 4 \to 5 \to 6
  • 0 \to 2 \to 4 \to 6

入力例 2

10 2
4
5

出力例 2

0

壊れている床を踏まないような移動方法がない場合もあります。


入力例 3

100 5
1
23
45
67
89

出力例 3

608200469

総数を 1,000,000,007 で割った余りを出力することに注意して下さい。

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step. He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps? Find the count modulo 1\ 000\ 000\ 007.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq N-1
  • 1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
 .
 .
 .
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.


Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

  • 0 \to 1 \to 2 \to 4 \to 5 \to 6
  • 0 \to 1 \to 2 \to 4 \to 6
  • 0 \to 2 \to 4 \to 5 \to 6
  • 0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.


Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.

D - Lamp

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

H 行横 W 列のグリッドが与えられます。このグリッドのうち、いくつかのマスには障害物が存在します。

すぬけ君は、障害物のないマスのうち一つを選び、そのマスに明かりを設置しようとしています。 設置されたマスから、上下左右の四方向にまっすぐに光線が伸びます。それぞれの方向について、最初に障害物が存在するマスにぶつかる、もしくはグリッドの端にぶつかる手前のマスまで照らされます。明かりを設置したマスも照らされますが、障害物が存在するマスは照らされません。

すぬけ君は明かりによって照らされるマスの個数を最大化したいです。

H 個の長さ W の文字列 S_i (1 ≤ i ≤ H) が与えられます。S_ij 文字目 (1 ≤ j ≤ W) が # のとき、グリッドの上から i 行目で左から j 列目のマスには障害物があり、 . のときは障害物がありません。

照らされるマスの個数の最大値を求めてください。

制約

  • 1 ≤ H ≤ 2,000
  • 1 ≤ W ≤ 2,000
  • S_i#. のみからなる長さ W の文字列
  • S_i (1 ≤ i ≤ H) のうちいずれかに . は最低 1 つ存在する

入力

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

H W
S_1
:
S_H

出力

照らされるマスの個数の最大値を出力せよ。


入力例 1

4 6
#..#..
.....#
....#.
#.#...

出力例 1

8

すぬけ君が上から 2 行目、左から 2 列目のマスに明かりを設置すると、上から 2 行目のうち左から 15 列目のマス、 左から 2 列目のうち上から 14 列目のマス全てが照らされ、全部で 8 マスです。


入力例 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

出力例 2

13

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it. The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right. In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

  • 1 \leq H \leq 2,000
  • 1 \leq W \leq 2,000
  • S_i is a string of length W consisting of # and ..
  • . occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.


Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.


Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13
E - Sum Equals Xor

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

正整数 L が二進数表記で与えられます。 以下の条件を満たす非負整数 a, b の組 (a, b) がいくつ存在するか求めてください:

  • a + b \leq L
  • a + b = a \text{ XOR } b

ただし、この値は非常に大きくなることがあるので、10^9 + 7 で割った余りを出力してください。

XOR とは

整数 A, B のビットごとの排他的論理和 a \text{ XOR } b は、以下のように定義されます。

a \text{ XOR } b を二進数表記した際の 2^k (k \geq 0) の位の数は、A, B を二進数表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。 例えば、3 \text{ XOR } 5 = 6 となります (二進数表記すると: 011 \text{ XOR } 101 = 110)。

制約

  • Lは二進数表記で与えられ、先頭文字は必ず 1 である
  • 1 \leq L < 2^{100,001}

入力

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

L

出力

条件を満たす組 (a, b) の個数を 10^9 + 7 で割った余りを出力せよ。


入力例 1

10

出力例 1

5

条件を満たす (a, b) としては (0, 0), (0, 1), (1, 0), (0, 2), (2, 0)5 つが考えられます。


入力例 2

1111111111111111111

出力例 2

162261460

Score : 500 points

Problem Statement

You are given a positive integer L in base two. How many pairs of non-negative integers (a, b) satisfy the following conditions?

  • a + b \leq L
  • a + b = a \text{ XOR } b

Since there can be extremely many such pairs, print the count modulo 10^9 + 7.

What is XOR?

The XOR of integers A and B, A \text{ XOR } B, is defined as follows:

  • When A \text{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \text{ XOR } 5 = 6. (In base two: 011 \text{ XOR } 101 = 110.)

Constraints

  • L is given in base two, without leading zeros.
  • 1 \leq L < 2^{100\ 001}

Input

Input is given from Standard Input in the following format:

L

Output

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


Sample Input 1

10

Sample Output 1

5

Five pairs (a, b) satisfy the conditions: (0, 0), (0, 1), (1, 0), (0, 2) and (2, 0).


Sample Input 2

1111111111111111111

Sample Output 2

162261460
F - Takahashi's Basics in Education and Learning

Time Limit: 3 sec / Memory Limit: 1024 MB

配点: 600

問題文

長さ L の等差数列 s_0, s_1, s_2, ... , s_{L-1} があります。

この等差数列の初項は A、公差は B です。つまり、s_i = A + B \times i が成り立ちます。

この数列の各項を、先頭に 0 の無い十進法表記に直し、順につなげて読んでできる整数を考えます。たとえば、数列 3, 7, 11, 15, 19 をつなげて読んでできる整数は 37111519 となります。この整数を M で割ったあまりはいくらでしょうか。

制約

  • 入力はすべて整数である
  • 1 \leq L, A, B < 10^{18}
  • 2 \leq M \leq 10^9
  • 等差数列の要素は全て 10^{18} 未満

入力

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

L A B M

出力

数列の各項をつなげて読んだ整数を M で割ったあまりを出力してください。


入力例 1

5 3 4 10007

出力例 1

5563

考える等差数列は 3, 7, 11, 15, 19 なので,3711151910007 で割ったあまりである 5563 が答えです.


入力例 2

4 8 1 1000000

出力例 2

891011

入力例 3

107 10000000000007 1000000000000007 998244353

出力例 3

39122908

Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

  • All values in input are integers.
  • 1 \leq L, A, B < 10^{18}
  • 2 \leq M \leq 10^9
  • All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.


Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.


Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908