A - A+...+B Problem

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

すぬけ君は、整数を N 個持っています。このうち最小のものは A、最大のものは B です。すぬけ君が持っている整数の総和としてありうる値は何通りあるでしょうか。

制約

  • 1 ≦ N,A,B ≦ 10^9
  • A,B は整数である

入力

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

N A B

出力

総和としてありうる値の個数を出力せよ。


入力例 1

4 4 6

出力例 1

5

18=4+4+4+6,19=4+4+5+6,20=4+5+5+6,21=4+5+6+6,22=4+6+6+65 つの値が総和として考えられます。


入力例 2

5 4 3

出力例 2

0

入力例 3

1 7 10

出力例 3

0

入力例 4

1 3 3

出力例 4

1

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B. We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

  • 1 ≤ N,A,B ≤ 10^9
  • A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.


Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.


Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1
B - Evilator

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

すけぬ君は、N 階建てのビルを建てました。ビルにはエレベーターが 1 基あり、すべての階に止まります。

すけぬ君は、各階に上下の方向ボタンを設置しましたが、うっかりしていたため、どの階にも上向きか下向きの片方のボタンしかありません。 そのため、どの階からも上か下のどちらかにしか進むことができません。 S_iU のとき i 階には上向きのボタンしかなく、上にしか進めないことを、D のとき下向きのボタンしかなく、 下にしか進めないことを表します。

ある階から目的の階へと移動したい住民は、仕方がないので必要があれば他の階を経由して目的の階へと向かうことにしました。 全ての階の順序対 (i,j) についての、i 階から j 階へ行くときのエレベーターに乗る回数の最小値の合計を求めてください。

制約

  • 2 ≦ |S| ≦ 10^5
  • S_iU または D である
  • S_1U である
  • S_{|S|}D である

入力

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

S_1S_2...S_{|S|}

出力

全ての階の順序対 (i,j) についての、i 階から j 階へ行くときのエレベーターに乗る回数の最小値の合計を出力せよ。


入力例 1

UUD

出力例 1

7

1 階から 2 階までは、1 回エレベーターに乗れば行くことができます。

1 階から 3 階までは、1 回エレベーターに乗れば行くことができます。

2 階から 1 階までは、2 回エレベーターに乗れば行くことができます。

2 階から 3 階までは、1 回エレベーターに乗れば行くことができます。

3 階から 1 階までは、1 回エレベーターに乗れば行くことができます。

3 階から 2 階までは、1 回エレベーターに乗れば行くことができます。

これらの合計は 7 なので、7 を出力します。


入力例 2

UUDUUDUD

出力例 2

77

Score : 400 points

Problem Statement

Skenu constructed a building that has N floors. The building has an elevator that stops at every floor.

There are buttons to control the elevator, but Skenu thoughtlessly installed only one button on each floor - up or down. This means that, from each floor, one can only go in one direction. If S_i is U, only "up" button is installed on the i-th floor and one can only go up; if S_i is D, only "down" button is installed on the i-th floor and one can only go down.

The residents have no choice but to go to their destination floors via other floors if necessary. Find the sum of the following numbers over all ordered pairs of two floors (i,j): the minimum number of times one needs to take the elevator to get to the j-th floor from the i-th floor.

Constraints

  • 2 ≤ |S| ≤ 10^5
  • S_i is either U or D.
  • S_1 is U.
  • S_{|S|} is D.

Input

The input is given from Standard Input in the following format:

S_1S_2...S_{|S|}

Output

Print the sum of the following numbers over all ordered pairs of two floors (i,j): the minimum number of times one needs to take the elevator to get to the j-th floor from the i-th floor.


Sample Input 1

UUD

Sample Output 1

7

From the 1-st floor, one can get to the 2-nd floor by taking the elevator once.

From the 1-st floor, one can get to the 3-rd floor by taking the elevator once.

From the 2-nd floor, one can get to the 1-st floor by taking the elevator twice.

From the 2-nd floor, one can get to the 3-rd floor by taking the elevator once.

From the 3-rd floor, one can get to the 1-st floor by taking the elevator once.

From the 3-rd floor, one can get to the 2-nd floor by taking the elevator once.

The sum of these numbers of times, 7, should be printed.


Sample Input 2

UUDUUDUD

Sample Output 2

77
C - Nuske vs Phantom Thnook

Time Limit: 4 sec / Memory Limit: 256 MB

配点 : 700

問題文

ぬすけ君は、N × M のグリッドを持っています。行には上から順に 1 から N、列には左から順に 1 から M の番号がついています。 グリッドの各マスは白か青かに塗られており、S_{i,j}1 のとき ij 列のマスは青マス、0 のとき白マスです。 青く塗られた任意の二つのマス a,b について、a からはじめて、現在いるマスから辺を共有する青いマスに進むことを繰り返して b に至るような経路のうち、同じマスを 2 度以上通らないようなものは、高々 1 通りです。

ぬすけ君の永遠のライバルである怪盗スヌークは、ぬすけ君に Q 個の質問をしました。i 個目の質問は、4 つの整数 x_{i,1},y_{i,1},x_{i,2},y_{i,2} からなり、 グリッドの x_{i,1} 行目から x_{i,2} 行目まで、y_{i,1} 列目から y_{i,2} 列目までの範囲の長方形領域を切り出したときに、 その範囲の青マスからなる連結成分がいくつあるかを答える質問です。

怪盗スヌークの質問すべてに答えてください。

制約

  • 1 ≦ N,M ≦ 2000
  • 1 ≦ Q ≦ 200000
  • S_{i,j}0 または 1 である
  • S_{i,j} は問題文中の条件を満たす
  • 1 ≦ x_{i,1} ≦ x_{i,2} ≦ N(1 ≦ i ≦ Q)
  • 1 ≦ y_{i,1} ≦ y_{i,2} ≦ M(1 ≦ i ≦ Q)

入力

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

N M Q
S_{1,1}..S_{1,M}
:
S_{N,1}..S_{N,M}
x_{1,1} y_{i,1} x_{i,2} y_{i,2}
:
x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2}

出力

質問ごとに、その範囲の青マスからなる連結成分がいくつあるかを出力せよ。


入力例 1

3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4

出力例 1

3
2
2
2

1 つ目の質問では、グリッド全体が指定されます。青マスからなる連結成分は 3 つあるので、3 を出力します。

2 つめの質問では、赤枠の範囲が指定されます。青マスからなる連結成分は 2 つあるので、2 を出力します。 もとのグリッドで同じ成分に属するマスが、異なる成分に属するかもしれないことに注意してください。


入力例 2

5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4

出力例 2

3
2
1
1
3
2

Score : 700 points

Problem Statement

Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once.

Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region?

Process all the queries.

Constraints

  • 1 ≤ N,M ≤ 2000
  • 1 ≤ Q ≤ 200000
  • S_{i,j} is either 0 or 1.
  • S_{i,j} satisfies the condition explained in the statement.
  • 1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q)
  • 1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q)

Input

The input is given from Standard Input in the following format:

N M Q
S_{1,1}..S_{1,M}
:
S_{N,1}..S_{N,M}
x_{1,1} y_{i,1} x_{i,2} y_{i,2}
:
x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2}

Output

For each query, print the number of the connected components consisting of blue squares in the region.


Sample Input 1

3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4

Sample Output 1

3
2
2
2

In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus 3 should be printed.

In the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus 2 should be printed. Note that squares that belong to the same component in the original grid may belong to different components.


Sample Input 2

5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4

Sample Output 2

3
2
1
1
3
2
D - A or...or B Problem

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 900

問題文

ぬけす君は、A 以上 B 以下の整数から 1 個以上選んで、それらの整数の bitwise or を取ってできる整数を持っています。 ぬけす君が持っている整数としてありうるものは何通りあるでしょうか。

制約

  • 1 ≦ A ≦ B < 2^{60}
  • A,B は整数である

入力

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

A
B

出力

ぬけす君が持っている整数としてありうるものの個数を出力せよ。


入力例 1

7
9

出力例 1

4

7,8,9 のうちの 1 個以上の整数の bitwise or で書ける整数は、7,8,9,154 つです。


入力例 2

65
98

出力例 2

63

入力例 3

271828182845904523
314159265358979323

出力例 3

68833183630578410

Score : 900 points

Problem Statement

Nukes has an integer that can be represented as the bitwise OR of one or more integers between A and B (inclusive). How many possible candidates of the value of Nukes's integer there are?

Constraints

  • 1 ≤ A ≤ B < 2^{60}
  • A and B are integers.

Input

The input is given from Standard Input in the following format:

A
B

Output

Print the number of possible candidates of the value of Nukes's integer.


Sample Input 1

7
9

Sample Output 1

4

In this case, A=7 and B=9. There are four integers that can be represented as the bitwise OR of a non-empty subset of {7, 8, 9}: 7, 8, 9 and 15.


Sample Input 2

65
98

Sample Output 2

63

Sample Input 3

271828182845904523
314159265358979323

Sample Output 3

68833183630578410
E - Mr.Aoki Incubator

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1200

問題文

数直線上に N 人の高橋君が並んでおり、1 から N までの番号がついています。 i 人目の高橋君は、時刻 0 に位置 X_i にいて、速度 V_i で正の方向に歩き始めます。

けすぬ君は、時刻 0 に高橋君たちのうちの何人かを選んで青木君にすることができます。 高橋君が青木君になっても歩く速度は変化しません。 それ以降、もしある瞬間に高橋君と青木君が同じ座標にいたなら、高橋君は青木君になります。

けすぬ君が時刻 0 に高橋君を青木君にする 2^N 通りの方法のうち、 十分な時間が経過した後、高橋君が全員青木君になっているような方法の数を 10^9+7 で割ったあまりを求めてください。

制約

  • 1 ≦ N ≦ 200000
  • 1 ≦ X_i,V_i ≦ 10^9(1 ≦ i ≦ N)
  • X_i,V_i は整数である
  • X_i たちはすべて異なる
  • V_i たちはすべて異なる

入力

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

N
X_1 V_1
:
X_N V_N

出力

十分な時間が経過した後、高橋君が全員青木君になっているような方法の数を 10^9+7 で割ったあまりを出力せよ。


入力例 1

3
2 5
6 1
3 7

出力例 1

6

けすぬ君が (2),(3),(1,2),(1,3),(2,3),(1,2,3) 番目の高橋君を青木君にしたとき、十分な時間が経過した後にすべての高橋君が青木君になっています。


入力例 2

4
3 7
2 9
8 16
10 8

出力例 2

9

Score : 1200 points

Problem Statement

Takahashi is an expert of Clone Jutsu, a secret art that creates copies of his body.

On a number line, there are N copies of Takahashi, numbered 1 through N. The i-th copy is located at position X_i and starts walking with velocity V_i in the positive direction at time 0.

Kenus is a master of Transformation Jutsu, and not only can he change into a person other than himself, but he can also transform another person into someone else.

Kenus can select some of the copies of Takahashi at time 0, and transform them into copies of Aoki, another Ninja. The walking velocity of a copy does not change when it transforms. From then on, whenever a copy of Takahashi and a copy of Aoki are at the same coordinate, that copy of Takahashi transforms into a copy of Aoki.

Among the 2^N ways to transform some copies of Takahashi into copies of Aoki at time 0, in how many ways will all the copies of Takahashi become copies of Aoki after a sufficiently long time? Find the count modulo 10^9+7.

Constraints

  • 1 ≤ N ≤ 200000
  • 1 ≤ X_i,V_i ≤ 10^9(1 ≤ i ≤ N)
  • X_i and V_i are integers.
  • All X_i are distinct.
  • All V_i are distinct.

Input

The input is given from Standard Input in the following format:

N
X_1 V_1
:
X_N V_N

Output

Print the number of the ways that cause all the copies of Takahashi to turn into copies of Aoki after a sufficiently long time, modulo 10^9+7.


Sample Input 1

3
2 5
6 1
3 7

Sample Output 1

6

All copies of Takahashi will turn into copies of Aoki after a sufficiently long time if Kenus transforms one of the following sets of copies of Takahashi into copies of Aoki: (2), (3), (1,2), (1,3), (2,3) and (1,2,3).


Sample Input 2

4
3 7
2 9
8 16
10 8

Sample Output 2

9
F - Kenus the Ancient Greek

Time Limit: 5 sec / Memory Limit: 256 MB

配点 : 1700

問題文

国際ユークリッドの互除法オリンピックの主催者であるけぬすくんは、 オリンピックをより面白くするため、2 数のペアに対してユークリッドの互除法を走らせたとき、反復回数ができるだけ大きくなるようなペアを探しています。

Q 個のクエリが与えられます。i 個目のクエリは、2 つの整数 X_i,Y_i で表され、 1 ≦ x ≦ X_i, 1 ≦ y ≦ Y_i なるような (x,y) のペアの中での、ユークリッドの互除法の反復回数の最大値と、その最大値をとるペアの個数を 10^9+7 で割ったあまりを求めるクエリです。

全てのクエリに答えてください。ただし、ユークリッドの互除法の反復回数とは、任意の非負整数 a,b に対し、

  • (a,b)(b,a) の反復回数は等しい
  • (0,a) の反復回数は 0
  • a > 0 かつ a ≦ b なら、整数 p,q (0 ≦ q < a) を用いて bb=pa+q と一意的に表したとき、(q,a) の反復回数に 1 を加えた値が (a,b) の反復回数となる

を満たすように定義されます。

制約

  • 1 ≦ Q ≦ 3 × 10^5
  • 1 ≦ X_i,Y_i ≦ 10^{18}(1 ≦ i ≦ Q)

入力

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

Q
X_1 Y_1
:
X_Q Y_Q

出力

各クエリに対し、ユークリッドの互除法の反復回数の最大値と、最大値を取るペアの個数を 10^9+7 で割ったあまりを空白区切りで出力せよ。


入力例 1

3
4 4
6 10
12 11

出力例 1

2 4
4 1
4 7

1 つ目のクエリでは、(2,3),(3,2),(3,4),(4,3) のユークリッドの互除法の反復回数が 2 回です。3 回以上反復が必要な組はありません。

2 つ目のクエリでは、(5,8) のユークリッドの互除法の反復回数が 4 回です。

3 つ目のクエリでは、(5,8),(8,5),(7,11),(8,11),(11,7),(11,8),(12,7) のユークリッドの互除法の反復回数が 4 回です。


入力例 2

10
1 1
2 2
5 1000000000000000000
7 3
1 334334334334334334
23847657 23458792534
111111111 111111111
7 7
4 19
9 10

出力例 2

1 1
1 4
4 600000013
3 1
1 993994017
35 37447
38 2
3 6
3 9
4 2

Score : 1700 points

Problem Statement

Kenus, the organizer of International Euclidean Olympiad, is seeking a pair of two integers that requires many steps to find its greatest common divisor using the Euclidean algorithm.

You are given Q queries. The i-th query is represented as a pair of two integers X_i and Y_i, and asks you the following: among all pairs of two integers (x,y) such that 1 ≤ x ≤ X_i and 1 ≤ y ≤ Y_i, find the maximum Euclidean step count (defined below), and how many pairs have the maximum step count, modulo 10^9+7.

Process all the queries. Here, the Euclidean step count of a pair of two non-negative integers (a,b) is defined as follows:

  • (a,b) and (b,a) have the same Euclidean step count.
  • (0,a) has a Euclidean step count of 0.
  • If a > 0 and a ≤ b, let p and q be a unique pair of integers such that b=pa+q and 0 ≤ q < a. Then, the Euclidean step count of (a,b) is the Euclidean step count of (q,a) plus 1.

Constraints

  • 1 ≤ Q ≤ 3 × 10^5
  • 1 ≤ X_i,Y_i ≤ 10^{18}(1 ≤ i ≤ Q)

Input

The input is given from Standard Input in the following format:

Q
X_1 Y_1
:
X_Q Y_Q

Output

For each query, print the maximum Euclidean step count, and the number of the pairs that have the maximum step count, modulo 10^9+7, with a space in between.


Sample Input 1

3
4 4
6 10
12 11

Sample Output 1

2 4
4 1
4 7

In the first query, (2,3), (3,2), (3,4) and (4,3) have a Euclidean step count of 2. No pairs have a step count of more than 2.

In the second query, (5,8) has a Euclidean step count of 4.

In the third query, (5,8), (8,5), (7,11), (8,11), (11,7), (11,8), (12,7) have a Euclidean step count of 4.


Sample Input 2

10
1 1
2 2
5 1000000000000000000
7 3
1 334334334334334334
23847657 23458792534
111111111 111111111
7 7
4 19
9 10

Sample Output 2

1 1
1 4
4 600000013
3 1
1 993994017
35 37447
38 2
3 6
3 9
4 2