A - Horizon

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

地上 x メートルの高さから見える水平線は \sqrt{x(12800000+x)} メートル先にあるとするとき、 地上 H メートルの高さから見える水平線が何メートル先にあるか求めてください。

制約

  • 1 \leq H \leq 10^5
  • H は整数である

入力

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

H

出力

答えを出力せよ。
なお、想定解との絶対誤差または相対誤差が 10^{-6} 以下であれば、正解として扱われる。


入力例 1

333

出力例 1

65287.907678222

\sqrt{333(12800000+333)} = 65287.9076782\ldots です。65287.91 などの出力でも正解となります。


入力例 2

634

出力例 2

90086.635834623

\sqrt{634(12800000+634)} = 90086.6358346\ldots です。

Score : 100 points

Problem Statement

Assuming that the horizon seen from a place x meters above the ground is \sqrt{x(12800000+x)} meters away, find how many meters away the horizon seen from a place H meters above the ground is.

Constraints

  • 1 \leq H \leq 10^5
  • H is an integer.

Input

Input is given from Standard Input in the following format:

H

Output

Print the answer.
Your answer will be considered correct when the absolute or relative error from the judge's answer is at most 10^{-6}.


Sample Input 1

333

Sample Output 1

65287.907678222

We have \sqrt{333(12800000+333)} = 65287.9076782\ldots. Outputs such as 65287.91 would also be accepted.


Sample Input 2

634

Sample Output 2

90086.635834623

We have \sqrt{634(12800000+634)} = 90086.6358346\ldots.

B - Integer Division

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

-10^{18} 以上 10^{18} 以下の整数 X が与えられるので、\left\lfloor \dfrac{X}{10} \right\rfloor を出力してください。

注記

実数 x に対して、「x 以下の整数の中で最大の整数」を \left\lfloor x \right\rfloor と表します。たとえば \left\lfloor 4.7 \right\rfloor = 4, \left\lfloor -2.4 \right\rfloor = -3, \left\lfloor 5 \right\rfloor = 5 のようになります。(詳しくは入出力例にある説明を参考にしてください。)

制約

  • -10^{18} \leq X \leq 10^{18}
  • 入力はすべて整数である。

入力

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

X

出力

\left\lfloor \frac{X}{10} \right\rfloor を出力せよ。整数として出力する必要があることに注意せよ。


入力例 1

47

出力例 1

4

\frac{47}{10} = 4.7 以下の整数は、すべての負の整数および 0, 1, 2, 3, 4 です。この中で一番大きい整数は 4 なので、\left\lfloor \frac{47}{10} \right\rfloor = 4 となります。


入力例 2

-24

出力例 2

-3

\frac{-24}{10} = -2.4 以下の整数の中で一番大きい整数は -3 なので、 \left\lfloor \frac{-24}{10} \right\rfloor = -3 となります。
-2-2.4 よりも大きいので、条件を満たさないことに注意してください。


入力例 3

50

出力例 3

5

\frac{50}{10} = 5 以下の整数の中で一番大きい整数は 5 自身です。よって \left\lfloor \frac{50}{10} \right\rfloor = 5 となります。


入力例 4

-30

出力例 4

-3

上の例と同様に \left\lfloor \frac{-30}{10} \right\rfloor = -3 となります。


入力例 5

987654321987654321

出力例 5

98765432198765432

答えは 98765432198765432 となります。すべての桁が正しく合っているか確認しましょう。

なお、もしも自分で書いたプログラムが想定通りの挙動をしない場合は、使用しているプログラミング言語の仕様を調べてみることを推奨します。
また、自分の書いたコードがどのように動作するかを確認したい場合は問題文上部の「コードテスト」をご利用ください。

Score : 200 points

Problem Statement

Given an integer X between -10^{18} and 10^{18} (inclusive), print \left\lfloor \dfrac{X}{10} \right\rfloor.

Notes

For a real number x, \left\lfloor x \right\rfloor denotes "the maximum integer not exceeding x". For example, we have \left\lfloor 4.7 \right\rfloor = 4, \left\lfloor -2.4 \right\rfloor = -3, and \left\lfloor 5 \right\rfloor = 5. (For more details, please refer to the description in the Sample Input and Output.)

Constraints

  • -10^{18} \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print \left\lfloor \frac{X}{10} \right\rfloor. Note that it should be output as an integer.


Sample Input 1

47

Sample Output 1

4

The integers that do not exceed \frac{47}{10} = 4.7 are all the negative integers, 0, 1, 2, 3, and 4. The maximum integer among them is 4, so we have \left\lfloor \frac{47}{10} \right\rfloor = 4.


Sample Input 2

-24

Sample Output 2

-3

Since the maximum integer not exceeding \frac{-24}{10} = -2.4 is -3, we have \left\lfloor \frac{-24}{10} \right\rfloor = -3.
Note that -2 does not satisfy the condition, as -2 exceeds -2.4.


Sample Input 3

50

Sample Output 3

5

The maximum integer that does not exceed \frac{50}{10} = 5 is 5 itself. Thus, we have \left\lfloor \frac{50}{10} \right\rfloor = 5.


Sample Input 4

-30

Sample Output 4

-3

Just like the previous example, \left\lfloor \frac{-30}{10} \right\rfloor = -3.


Sample Input 5

987654321987654321

Sample Output 5

98765432198765432

The answer is 98765432198765432. Make sure that all the digits match.

If your program does not behave as intended, we recommend you checking the specification of the programming language you use.
If you want to check how your code works, you may use "Custom Test" above the Problem Statement.

C - Knight Fork

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

xy 座標平面上の 2 つの格子点 (x_1, y_1), (x_2, y_2) からの距離がともに \sqrt{5} である格子点は存在しますか?

注記

xy 座標平面上にある点のうち、x 座標と y 座標がともに整数である点を格子点と呼びます。
また、xy 平面上の 2(a, b), (c, d) の距離をユークリッド距離 \sqrt{(a - c)^2 + (b-d)^2} として定義します。

参考として、xy 平面の (0, 0) の上に黒丸を、(0, 0) からの距離が \sqrt{5} となる格子点の上に白丸を書いた図は以下のようになります。(x,y いずれかが整数となる部分に目盛り線を引いています。)

image

制約

  • -10^9 \leq x_1 \leq 10^9
  • -10^9 \leq y_1 \leq 10^9
  • -10^9 \leq x_2 \leq 10^9
  • -10^9 \leq y_2 \leq 10^9
  • (x_1, y_1) \neq (x_2, y_2)
  • 入力はすべて整数である。

入力

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

x_1 y_1 x_2 y_2

出力

条件を満たす格子点が存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

0 0 3 3

出力例 1

Yes
  • (2,1)(x_1, y_1) の距離は \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5}
  • (2,1)(x_2, y_2) の距離は \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5}
  • (2, 1) は格子点

なので点 (2, 1) は条件を満たします。よって Yes を出力します。
なお、点 (1, 2) も条件を満たすことが同様にして確認できます。


入力例 2

0 1 2 3

出力例 2

No

条件を満たす格子点は存在しません。よって No を出力します。


入力例 3

1000000000 1000000000 999999999 999999999

出力例 3

Yes

(10^9 + 1, 10^9 - 2) および点 (10^9 - 2, 10^9 + 1)が条件を満たします。

Score : 300 points

Problem Statement

On an xy-coordinate plane, is there a lattice point whose distances from two lattice points (x_1, y_1) and (x_2, y_2) are both \sqrt{5}?

Notes

A point on an xy-coordinate plane whose x and y coordinates are both integers is called a lattice point.
The distance between two points (a, b) and (c, d) is defined to be the Euclidean distance between them, \sqrt{(a - c)^2 + (b-d)^2}.

The following figure illustrates an xy-plane with a black circle at (0, 0) and white circles at the lattice points whose distances from (0, 0) are \sqrt{5}. (The grid shows where either x or y is an integer.)

image

Constraints

  • -10^9 \leq x_1 \leq 10^9
  • -10^9 \leq y_1 \leq 10^9
  • -10^9 \leq x_2 \leq 10^9
  • -10^9 \leq y_2 \leq 10^9
  • (x_1, y_1) \neq (x_2, y_2)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

If there is a lattice point satisfying the condition, print Yes; otherwise, print No.


Sample Input 1

0 0 3 3

Sample Output 1

Yes
  • The distance between points (2,1) and (x_1, y_1) is \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5};
  • the distance between points (2,1) and (x_2, y_2) is \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5};
  • point (2, 1) is a lattice point,

so point (2, 1) satisfies the condition. Thus, Yes should be printed.
One can also assert in the same way that (1, 2) also satisfies the condition.


Sample Input 2

0 1 2 3

Sample Output 2

No

No lattice point satisfies the condition, so No should be printed.


Sample Input 3

1000000000 1000000000 999999999 999999999

Sample Output 3

Yes

Point (10^9 + 1, 10^9 - 2) and point (10^9 - 2, 10^9 + 1) satisfy the condition.

D - Prime Sum Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君と青木君が次のようなゲームをします。

  • まず、高橋君が A 以上 B 以下の好きな整数を選び、青木君に伝える
  • 次に、青木君が C 以上 D 以下の好きな整数を選ぶ
  • 二人の選んだ整数の和が素数なら青木君の勝ち、そうでなければ高橋君の勝ち

二人が最適な戦略を取るとき、どちらが勝ちますか?

制約

  • 1 \leq A \leq B \leq 100
  • 1 \leq C \leq D \leq 100
  • 入力に含まれる値は全て整数である

入力

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

A B C D

出力

二人が最適な戦略をとったとき、高橋君が勝つなら Takahashi、青木君が勝つなら Aoki を出力せよ。


入力例 1

2 3 3 4

出力例 1

Aoki

例えば高橋君が 2 を選んだときは、青木君は 3 を選ぶことで、和を素数である 5 にすることができます。


入力例 2

1 100 50 60

出力例 2

Takahashi

最適な戦略を取ると高橋君が必ず勝ちます。


入力例 3

3 14 1 5

出力例 3

Aoki

Score : 400 points

Problem Statement

Takahashi and Aoki are playing a game.

  • First, Takahashi chooses an integer between A and B (inclusive) and tells it to Aoki.
  • Next, Aoki chooses an integer between C and D (inclusive).
  • If the sum of these two integers is a prime, then Aoki wins; otherwise, Takahashi wins.

When the two players play optimally, which player will win?

Constraints

  • 1 \leq A \leq B \leq 100
  • 1 \leq C \leq D \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

If Takahashi wins when the two players play optimally, print Takahashi; if Aoki wins, print Aoki.


Sample Input 1

2 3 3 4

Sample Output 1

Aoki

For example, if Takahashi chooses 2, Aoki can choose 3 to make the sum 5, which is a prime.


Sample Input 2

1 100 50 60

Sample Output 2

Takahashi

If they play optimally, Takahashi always wins.


Sample Input 3

3 14 1 5

Sample Output 3

Aoki
E - Subtree K-th Max

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の根付き木があります。頂点には 1 から N の番号がついており、根は頂点 1 です。
i 番目の辺は頂点 A_iB_i を結んでいます。
頂点 i には整数 X_i が書かれています。

Q 個のクエリが与えられます。i 番目のクエリでは整数の組 (V_i,K_i) が与えられるので、次の問題に答えてください。

  • 問題:頂点 V_i の部分木に含まれる頂点に書かれた整数のうち、大きい方から K_i 番目の値を求めよ

制約

  • 2 \leq N \leq 10^5
  • 0\leq X_i\leq 10^9
  • 1\leq A_i,B_i\leq N
  • 1\leq Q \leq 10^5
  • 1\leq V_i\leq N
  • 1\leq K_i\leq 20
  • 与えられるグラフは木である
  • 頂点 V_i の部分木は頂点を K_i 個以上持つ
  • 入力に含まれる値は全て整数である

入力

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

N Q
X_1 \ldots X_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 K_1
\vdots
V_Q K_Q

出力

Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

5 2
1 2 3 4 5
1 4
2 1
2 5
3 2
1 2
2 1

出力例 1

4
5

この入力において与えられる木は下図のようなものです。

図

1 番目のクエリでは、頂点 1 の部分木に含まれる頂点 1,2,3,4,5 に書かれた数のうち大きい方から 2 番目である 4 を出力します。
2 番目のクエリでは、頂点 2 の部分木に含まれる頂点 2,3,5 に書かれた数のうち大きい方から 1 番目である 5 を出力します。


入力例 2

6 2
10 10 10 9 8 8
1 4
2 1
2 5
3 2
6 4
1 4
2 2

出力例 2

9
10

入力例 3

4 4
1 10 100 1000
1 2
2 3
3 4
1 4
2 3
3 2
4 1

出力例 3

1
10
100
1000

Score : 500 points

Problem Statement

We have a rooted tree with N vertices. The vertices are numbered 1 through N, and the root is Vertex 1.
The i-th edge connects Vertices A_i and B_i.
Vertex i has an integer X_i written on it.

You are given Q queries. For the i-th query, given a pair of integers (V_i,K_i), answer the following question.

  • Question: among the integers written on the vertices in the subtree rooted at Vertex V_i, find the K_i-th largest value.

Constraints

  • 2 \leq N \leq 10^5
  • 0\leq X_i\leq 10^9
  • 1\leq A_i,B_i\leq N
  • 1\leq Q \leq 10^5
  • 1\leq V_i\leq N
  • 1\leq K_i\leq 20
  • The given graph is a tree.
  • The subtree rooted at Vertex V_i has K_i or more vertices.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
X_1 \ldots X_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 K_1
\vdots
V_Q K_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.


Sample Input 1

5 2
1 2 3 4 5
1 4
2 1
2 5
3 2
1 2
2 1

Sample Output 1

4
5

The tree given in this input is shown below.

figure

For the 1-st query, the vertices in the subtree rooted at Vertex 1 are Vertices 1, 2, 3, 4, and 5, so print the 2-nd largest value of the numbers written on these vertices, 4.
For the 2-nd query, the vertices in the subtree rooted at Vertex 2 are Vertices 2, 3, and 5, so print the 1-st largest value of the numbers written on these vertices, 5.


Sample Input 2

6 2
10 10 10 9 8 8
1 4
2 1
2 5
3 2
6 4
1 4
2 2

Sample Output 2

9
10

Sample Input 3

4 4
1 10 100 1000
1 2
2 3
3 4
1 4
2 3
3 2
4 1

Sample Output 3

1
10
100
1000
F - Construct Highway

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

Atcoder 国には 1 から N の番号がついた N 個の街と 1 から M の番号がついた M 個の高速道路があります。
高速道路 i は街 A_i と街 B_i を双方向に結んでいます。

国王の高橋君は、新たに N-M-1 本の高速道路を建設し、次の 2 つの条件をともに満たそうとしています。

  • すべての街同士は、高速道路をいくつか通って互いに行き来できる
  • i=1,\ldots,N について、街 i はちょうど D_i 本の高速道路と直接つながっている

条件を満たすような建設方法が存在するか判定し、存在するなら 1 つ出力してください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 0 \leq M \lt N-1
  • 1 \leq D_i \leq N-1
  • 1\leq A_i \lt B_i \leq N
  • i\neq j ならば、(A_i, B_i) \neq (A_j,B_j)
  • 入力に含まれる値は全て整数である

入力

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

N M
D_1 \ldots D_N
A_1 B_1
\vdots
A_M B_M

出力

条件を満たすような建設方法が存在しないとき -1 を出力せよ。
存在するとき、N-M-1 行出力せよ。i 行目には、i 番目に建設する高速道路が結ぶ 2 つの街の番号を空白区切りで出力せよ。


入力例 1

6 2
1 2 1 2 2 2
2 3
1 4

出力例 1

6 2
5 6
4 5

出力例のように、街 62、街 56、街 45 をそれぞれ結ぶ高速道路を建設すると条件を満たすことができます。

この他にも、例えば 街 64、街 56、街 25 を結ぶような高速道路を建設しても条件を満たすことができます。


入力例 2

5 1
1 1 1 1 4
2 3

出力例 2

-1

入力例 3

4 0
3 3 3 3

出力例 3

-1

Score : 500 points

Problem Statement

The Republic of Atcoder has N towns numbered 1 through N, and M highways numbered 1 through M.
Highway i connects Town A_i and Town B_i bidirectionally.

King Takahashi is going to construct (N-M-1) new highways so that the following two conditions are satisfied:

  • One can travel between every pair of towns using some number of highways
  • For each i=1,\ldots,N, exactly D_i highways are directly connected to Town i

Determine if there is such a way of construction. If it exists, print one.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 0 \leq M \lt N-1
  • 1 \leq D_i \leq N-1
  • 1\leq A_i \lt B_i \leq N
  • If i\neq j, then (A_i, B_i) \neq (A_j,B_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
D_1 \ldots D_N
A_1 B_1
\vdots
A_M B_M

Output

If there isn't a way of construction that satisfies the conditions, print -1.
If there is, print (N-M-1) lines. The i-th line should contain the indices of the two towns connected by the i-th highway to be constructed.


Sample Input 1

6 2
1 2 1 2 2 2
2 3
1 4

Sample Output 1

6 2
5 6
4 5

As in the Sample Output, the conditions can be satisfied by constructing highways connecting Towns 6 and 2, Towns 5 and 6, and Towns 4 and 5, respectively.

Another example to satisfy the conditions is to construct highways connecting Towns 6 and 4, Towns 5 and 6, and Towns 2 and 5, respectively.


Sample Input 2

5 1
1 1 1 1 4
2 3

Sample Output 2

-1

Sample Input 3

4 0
3 3 3 3

Sample Output 3

-1
G - Builder Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の単純連結無向グラフがあります。
頂点には頂点 1, 頂点 2, \dots, 頂点 N と番号が振られています。
辺には 辺 1, 辺 2, \dots, 辺 M と番号が振られています。辺 i は頂点 a_i と頂点 b_i を双方向に結んでいます。また、頂点 1 と頂点 N を直接結ぶ辺は存在しません。
各頂点の状態は「何もない頂点」か「壁がある頂点」のいずれかです。はじめ、全ての頂点は何もない頂点です。

青木君は頂点 1 を出発して、グラフ上を辺に沿って移動して頂点 N へ行こうとしています。ただし、壁がある頂点への移動を行うことはできません。

高橋君は、青木君が移動を開始する前にいくつかの頂点を選んで壁を建てることで、青木君がどのように移動しても頂点 N へ行くことができないようにすることにしました。
高橋君が頂点 i に壁を建てるには c_i 円を払う必要があります。また、頂点 1 および頂点 N には壁を建てられません。

高橋君が条件を満たすように壁を建てるときに最小で何円払う必要があるでしょうか。また、その時の壁の立て方を 1 つ出力してください。

制約

  • 3 \leq N \leq 100
  • N - 1 \leq M \leq \frac{N(N-1)}{2} - 1
  • 1 \leq a_i \lt b_i \leq N (1 \leq i \leq M)
  • (a_i, b_i) \neq (1, N)
  • 与えられるグラフは単純かつ連結である。
  • 1 \leq c_{i} \leq 10^9 (2 \leq i \leq N-1)
  • c_1 = c_N = 0
  • 入力はすべて整数である。

入力

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

N M
a_1 b_1
a_2 b_2
\vdots
a_M b_M
c_1 c_2 \dots c_N

出力

以下の形式に従って出力せよ。ここで、C,k,p_i は次に定める通りとする。

  • C は高橋君が支払う金額
  • k は高橋君が壁を建てる頂点の個数
  • (p_1,p_2,\dots,p_k) は高橋君が壁を建てる頂点からなる列
C
k
p_1 p_2 \dots p_k

最小の金額で条件を満たす建て方が複数存在する場合はどれを出力してもよい。


入力例 1

5 5
1 2
2 3
3 5
2 4
4 5
0 8 3 4 0

出力例 1

7
2
3 4

高橋君が 3 + 4 = 7 円を払って頂点 3, 頂点 4 に壁を建てると青木君は頂点 1 から頂点 5 へ行くことができなくなります。
これより少ない金額で条件を満たすことはできないので 7 円が答えとなります。


入力例 2

3 2
1 2
2 3
0 1 0

出力例 2

1
1
2

入力例 3

5 9
1 2
1 3
1 4
2 3
2 4
2 5
3 4
3 5
4 5
0 1000000000 1000000000 1000000000 0

出力例 3

3000000000
3
2 3 4

Score : 600 points

Problem Statement

We have a simple connected undirected graph with N vertices and M edges.
The vertices are numbered as Vertex 1, Vertex 2, \dots, Vertex N.
The edges are numbered as Edge 1, Edge 2, \dots, Edge M. Edge i connects Vertex a_i and Vertex b_i bidirectionally. There is no edge that directly connects Vertex 1 and Vertex N.
Each vertex is either empty or occupied by a wall. Initially, every vertex is empty.

Aoki is going to travel from Vertex 1 to Vertex N along the edges on the graph. However, Aoki is not allowed to move to a vertex occupied by a wall.

Takahashi has decided to choose some of the vertices to build walls on, so that Aoki cannot travel to Vertex N no matter which route he takes.
Building a wall on Vertex i costs Takahashi c_i yen (the currency of Japan). He cannot build a wall on Vertex 1 and Vertex N.

How many yens is required for Takahashi to build walls so that the conditions is satisfied? Also, print the way of building walls to achieve the minimum cost.

Constraints

  • 3 \leq N \leq 100
  • N - 1 \leq M \leq \frac{N(N-1)}{2} - 1
  • 1 \leq a_i \lt b_i \leq N (1 \leq i \leq M)
  • (a_i, b_i) \neq (1, N)
  • The given graph is simple and connected.
  • 1 \leq c_{i} \leq 10^9 (2 \leq i \leq N-1)
  • c_1 = c_N = 0
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
\vdots
a_M b_M
c_1 c_2 \dots c_N

Output

Print in the following format. Here, C,k, and p_i are defined as follows.

  • C is the cost that Takahashi will pay
  • k is the number of vertices for Takahashi to build walls on
  • (p_1,p_2,\dots,p_k) is a sequence of vertices on which Takahashi will build walls
C
k
p_1 p_2 \dots p_k

If there are multiple ways to build walls to satisfy the conditions with the minimum cost, print any of them.


Sample Input 1

5 5
1 2
2 3
3 5
2 4
4 5
0 8 3 4 0

Sample Output 1

7
2
3 4

If Takahashi builds walls on Vertex 3 and Vertex 4, paying 3 + 4 = 7 yen, Aoki is unable to travel from Vertex 1 to Vertex 5.
There is no way to satisfy the condition with less cost, so 7 yen is the answer.


Sample Input 2

3 2
1 2
2 3
0 1 0

Sample Output 2

1
1
2

Sample Input 3

5 9
1 2
1 3
1 4
2 3
2 4
2 5
3 4
3 5
4 5
0 1000000000 1000000000 1000000000 0

Sample Output 3

3000000000
3
2 3 4
Ex - Dice Product 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

すぬけ君は 1 以上 N 以下の整数が等確率で出るサイコロと整数 1 を持っています。
すぬけ君は持っている数が M 以下である間、次の操作を繰り返します。

  • サイコロを振り、出た目を x とする。持っている数に x を掛ける。

操作を終了するまでにすぬけ君がサイコロを振った回数の期待値を \text{mod } 10^9+7 で求めてください。

期待値 \pmod{10^9+7} の定義 求める期待値は必ず有理数になることが証明できます。また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \not\equiv 0 \pmod{10^9+7} となることも証明できます。よって、R \times Q \equiv P \pmod{10^9+7}, 0 \leq R \lt 10^9+7 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 2 \leq N \leq 10^9
  • 1 \leq M \leq 10^9

入力

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

N M

出力

答えを出力せよ。


入力例 1

2 1

出力例 1

2

答えはサイコロで 2 が出るまでにサイコロを振る回数の期待値になります。よって 2 を出力します。


入力例 2

2 39

出力例 2

12

答えはサイコロで 26 回出るまでにサイコロを振る回数の期待値になります。よって 12 を出力します。


入力例 3

3 2

出力例 3

250000004

答えは \frac{9}{4} です。4 \times 250000004 \equiv 9 \pmod{10^9+7} なので 250000004 を出力します。
\bf{10^9 + 7 = 1000000007}\mathrm{mod} を取る必要があるのに注意してください。


入力例 4

2392 39239

出力例 4

984914531

入力例 5

1000000000 1000000000

出力例 5

776759630

Score : 600 points

Problem Statement

Snuke has a die (singular of dice) that shows integers from 1 through N with equal probability, and an integer 1.
He repeats the following operation while his integer is less than or equal to M.

  • He rolls the die. If the die shows an integer x, he multiplies his integer by x.

Find the expected value of the number of times he rolls the die until he stops, modulo 10^9+7.

Definition of the expected value modulo 10^9+7 We can prove that the desired expected value is always a rational number. Moreover, under the constraints of the problem, when the value is represented as an irreducible fraction \frac{P}{Q}, we can also prove that Q \not\equiv 0 \pmod{10^9+7}. Thus, an integer R such that R \times Q \equiv P \pmod{10^9+7} and 0 \leq R \lt 10^9+7 is uniquely determined. Answer such R.

Constraints

  • 2 \leq N \leq 10^9
  • 1 \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.


Sample Input 1

2 1

Sample Output 1

2

The answer is the expected value of the number of rolls until it shows 2 for the first time. Thus, 2 should be printed.


Sample Input 2

2 39

Sample Output 2

12

The answer is the expected value of the number of rolls until it shows 2 six times. Thus, 12 should be printed.


Sample Input 3

3 2

Sample Output 3

250000004

The answer is \frac{9}{4}. We have 4 \times 250000004 \equiv 9 \pmod{10^9+7}, so 250000004 should be printed.
Note that the answer should be printed modulo \bf{10^9 + 7 = 1000000007}.


Sample Input 4

2392 39239

Sample Output 4

984914531

Sample Input 5

1000000000 1000000000

Sample Output 5

776759630