A - Wi-Fiの届く範囲

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 266

問題文

高橋君は、大学のキャンパス内に設置された Wi-Fi ルーターの電波がどの建物まで届くかを調査しています。

キャンパスには N 棟の建物があり、i 番目の建物 (1 \leq i \leq N) は座標 (X_i, Y_i) に位置しています。同じ座標に複数の建物が存在する場合もありますが、それぞれ別の建物として数えます。

Wi-Fi ルーターは座標 (0, 0) にある管理棟の屋上に設置されています。管理棟は調査対象の N 棟には含まれません。

Wi-Fi の電波は、ルーターからのユークリッド距離が D 以下の建物に届きます。すなわち、i 番目の建物に Wi-Fi の電波が届くのは、

\sqrt{X_i^2 + Y_i^2} \leq D

を満たすとき、つまり

X_i^2 + Y_i^2 \leq D^2

を満たすときです。

Wi-Fi の電波が届かない建物の数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq D \leq 10^6
  • -10^6 \leq X_i \leq 10^6
  • -10^6 \leq Y_i \leq 10^6
  • 入力はすべて整数

入力

N D
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
  • 1 行目には、建物の数を表す整数 N と、電波が届く最大距離を表す整数 D が、スペース区切りで与えられる。
  • 2 行目から N+1 行目では、各建物の座標が与えられる。
  • i+1 行目には、i 番目の建物の x 座標 X_iy 座標 Y_i が、スペース区切りで与えられる。

出力

Wi-Fi の電波が届かない建物の数を 1 行で出力してください。


入力例 1

5 10
3 4
8 6
12 5
-5 0
7 -7

出力例 1

1

入力例 2

8 100
50 50
-30 40
80 60
0 100
100 1
-70 -70
25 -25
0 -150

出力例 2

2

入力例 3

12 1000
100 200
-500 500
700 700
0 1000
1000 0
-800 -600
300 -400
999 1
-1 999
600 800
-707 -708
450 -900

出力例 3

2

Score : 266 pts

Problem Statement

Takahashi is investigating which buildings on a university campus can be reached by the signal from a Wi-Fi router installed on campus.

There are N buildings on the campus, and the i-th building (1 \leq i \leq N) is located at coordinates (X_i, Y_i). Multiple buildings may exist at the same coordinates, but each is counted as a separate building.

The Wi-Fi router is installed on the rooftop of the administration building located at coordinates (0, 0). The administration building is not included in the N buildings being investigated.

The Wi-Fi signal reaches buildings whose Euclidean distance from the router is at most D. That is, the Wi-Fi signal reaches the i-th building when

\sqrt{X_i^2 + Y_i^2} \leq D

is satisfied, or equivalently when

X_i^2 + Y_i^2 \leq D^2

is satisfied.

Find the number of buildings that the Wi-Fi signal does not reach.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq D \leq 10^6
  • -10^6 \leq X_i \leq 10^6
  • -10^6 \leq Y_i \leq 10^6
  • All inputs are integers

Input

N D
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
  • The first line contains an integer N representing the number of buildings and an integer D representing the maximum distance the signal can reach, separated by a space.
  • From the 2nd line to the (N+1)-th line, the coordinates of each building are given.
  • The (i+1)-th line contains the x-coordinate X_i and y-coordinate Y_i of the i-th building, separated by a space.

Output

Output the number of buildings that the Wi-Fi signal does not reach, on a single line.


Sample Input 1

5 10
3 4
8 6
12 5
-5 0
7 -7

Sample Output 1

1

Sample Input 2

8 100
50 50
-30 40
80 60
0 100
100 1
-70 -70
25 -25
0 -150

Sample Output 2

2

Sample Input 3

12 1000
100 200
-500 500
700 700
0 1000
1000 0
-800 -600
300 -400
999 1
-1 999
600 800
-707 -708
450 -900

Sample Output 3

2
B - 連番チケットの当選

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 333

問題文

高橋君は宝くじ売り場で働いています。この宝くじでは、1 から N までの番号がそれぞれ印刷されたチケットが 1 枚ずつ、合計 N 枚販売されています。当選条件はチケット番号の桁和によって決まります。ここで、正の整数の桁和とは、その 10 進表記における各桁の数字の合計のことです。例えば、123 の桁和は 1 + 2 + 3 = 6 です。

具体的には、チケット番号の桁和を K で割った余りが R に等しいとき、そのチケットは「当選チケット」となります。

高橋君は、連続する 3 つの番号 x-1, x, x+1 のチケットがすべて当選チケットになるようなケースに興味を持ちました。

正の整数 N、正の整数 K、非負整数 R が与えられます。整数 n の桁和を \mathrm{digitsum}(n) と書くとき、2 \leq x \leq N - 1 を満たす整数 x であって、

(\mathrm{digitsum}(x-1)) \bmod K = R, \quad (\mathrm{digitsum}(x)) \bmod K = R, \quad (\mathrm{digitsum}(x+1)) \bmod K = R

3 条件をすべて満たす整数 x の個数を 10^9 + 7 で割った余りを求めてください。

制約

  • N3 以上の整数
  • N10 進表記の文字列として与えられ、先頭に不要な 0 を含まない
  • N10 進表記の長さは 5 \times 10^6 以下
  • 1 \leq K \leq 100
  • 0 \leq R \leq K - 1

入力

N K R

入力は 1 行からなり、チケット番号の上限 N、除数 K、余り R がスペース区切りで与えられる。N は非常に大きい場合があるため、10 進表記の文字列として与えられる。KR は整数として与えられる。

出力

条件を満たす整数 x の個数を 10^9 + 7 で割った余りを 1 行で出力せよ。


入力例 1

25 3 1

出力例 1

0

入力例 2

50 7 0

出力例 2

0

入力例 3

123456789 9 6

出力例 3

0

入力例 4

100000000000000000000000000000000000000000000000000 97 42

出力例 4

0

入力例 5

3 1 0

出力例 5

1

Score : 333 pts

Problem Statement

Takahashi works at a lottery ticket booth. In this lottery, a total of N tickets are sold, each printed with a unique number from 1 to N. The winning condition is determined by the digit sum of the ticket number. Here, the digit sum of a positive integer is the sum of its digits in its decimal representation. For example, the digit sum of 123 is 1 + 2 + 3 = 6.

Specifically, a ticket is a "winning ticket" if the digit sum of its ticket number modulo K is equal to R.

Takahashi became interested in cases where three consecutive ticket numbers x-1, x, x+1 are all winning tickets.

You are given a positive integer N, a positive integer K, and a non-negative integer R. Let \mathrm{digitsum}(n) denote the digit sum of an integer n. Find the number of integers x satisfying 2 \leq x \leq N - 1 such that all of the following three conditions are met, modulo 10^9 + 7:

(\mathrm{digitsum}(x-1)) \bmod K = R, \quad (\mathrm{digitsum}(x)) \bmod K = R, \quad (\mathrm{digitsum}(x+1)) \bmod K = R

Constraints

  • N is an integer of at least 3
  • N is given as a string in decimal representation, without leading zeros
  • The length of the decimal representation of N is at most 5 \times 10^6
  • 1 \leq K \leq 100
  • 0 \leq R \leq K - 1

Input

N K R

The input consists of a single line containing the upper bound of the ticket numbers N, the divisor K, and the remainder R, separated by spaces. Since N can be extremely large, it is given as a string in decimal representation. K and R are given as integers.

Output

Print the number of integers x satisfying the conditions, modulo 10^9 + 7, in a single line.


Sample Input 1

25 3 1

Sample Output 1

0

Sample Input 2

50 7 0

Sample Output 2

0

Sample Input 3

123456789 9 6

Sample Output 3

0

Sample Input 4

100000000000000000000000000000000000000000000000000 97 42

Sample Output 4

0

Sample Input 5

3 1 0

Sample Output 5

1
C - 通行止めの迂回路

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君は街 1 から街 N へ移動しようとしています。

N 個の街と M 本の道路があり、i 番目の道路は街 U_i と街 V_i を双方向に結んでいます。

現在、いくつかの道路は工事のため通行止めとなっています。具体的には、S_i = 1 のとき i 番目の道路は通行可能であり、S_i = 0 のとき通行止めで通ることができません。

高橋君は通行可能な道路のみを使って街 1 から街 N へ移動します。移動の際、同じ街や同じ道路を複数回通ってもかまいません。このとき、通る道路の本数(同じ道路を複数回通った場合はその回数分だけ数える)の最小値を求めてください。

ただし、通行可能な道路のみを使ってどのように移動しても街 N にたどり着けない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M < 2 \times 10^5
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 同じ組 (U_i, V_i) に対して複数の道路が存在することがある(多重辺がありうる。それぞれの道路について S_i の値は独立に定まる)
  • S_i \in \{0, 1\}
  • 入力はすべて整数である

入力

N M
U_1 V_1 S_1
U_2 V_2 S_2
\vdots
U_M V_M S_M
  • 1 行目には、街の数 N と道路の数 M がスペース区切りで与えられる。
  • 続く M 行にわたって、各道路の情報が与えられる。
  • (i+1) 行目には、i 番目の道路が結ぶ 2 つの街 U_i, V_i と、その道路が通行可能かを表す S_i がスペース区切りで与えられる。

出力

通行可能な道路のみを使って街 1 から街 N へ移動するために通る道路の本数の最小値を 1 行で出力せよ。ただし、移動できない場合は -1 を出力せよ。


入力例 1

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

出力例 1

2

入力例 2

5 6
1 2 1
2 3 0
3 5 1
1 4 0
4 5 1
2 4 0

出力例 2

-1

入力例 3

8 12
1 2 1
1 3 0
2 4 1
3 4 1
4 5 0
4 6 1
6 8 1
5 8 1
2 3 1
2 5 1
1 2 0
7 8 1

出力例 3

3

入力例 4

12 20
1 2 1
2 3 1
3 12 0
1 4 1
4 5 1
5 6 1
6 12 1
2 5 0
3 6 1
1 7 0
7 8 1
8 9 1
9 12 1
4 8 1
5 9 0
2 10 1
10 11 1
11 12 1
6 10 0
3 4 1

出力例 4

4

入力例 5

2 1
1 2 1

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is trying to travel from town 1 to town N.

There are N towns and M roads, and the i-th road bidirectionally connects town U_i and town V_i.

Currently, some roads are closed due to construction. Specifically, when S_i = 1, the i-th road is passable, and when S_i = 0, it is closed and cannot be used.

Takahashi travels from town 1 to town N using only passable roads. During the trip, he may pass through the same town or the same road multiple times. Find the minimum number of roads traversed (if the same road is used multiple times, each traversal is counted separately).

However, if it is impossible to reach town N using only passable roads regardless of the route taken, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M < 2 \times 10^5
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • There may be multiple roads for the same pair (U_i, V_i) (i.e., multiple edges are possible; the value of S_i is determined independently for each road)
  • S_i \in \{0, 1\}
  • All input values are integers

Input

N M
U_1 V_1 S_1
U_2 V_2 S_2
\vdots
U_M V_M S_M
  • The first line contains the number of towns N and the number of roads M, separated by a space.
  • The following M lines provide information about each road.
  • The (i+1)-th line contains the two towns U_i, V_i connected by the i-th road, and S_i indicating whether the road is passable, separated by spaces.

Output

Output in a single line the minimum number of roads traversed to travel from town 1 to town N using only passable roads. If it is impossible to make the trip, output -1.


Sample Input 1

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

Sample Output 1

2

Sample Input 2

5 6
1 2 1
2 3 0
3 5 1
1 4 0
4 5 1
2 4 0

Sample Output 2

-1

Sample Input 3

8 12
1 2 1
1 3 0
2 4 1
3 4 1
4 5 0
4 6 1
6 8 1
5 8 1
2 3 1
2 5 1
1 2 0
7 8 1

Sample Output 3

3

Sample Input 4

12 20
1 2 1
2 3 1
3 12 0
1 4 1
4 5 1
5 6 1
6 12 1
2 5 0
3 6 1
1 7 0
7 8 1
8 9 1
9 12 1
4 8 1
5 9 0
2 10 1
10 11 1
11 12 1
6 10 0
3 4 1

Sample Output 4

4

Sample Input 5

2 1
1 2 1

Sample Output 5

1
D - ビーズ列

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は、AB2 種類の文字からなる長さ N の文字列で表されるビーズ列を持っています。ビーズ列は先頭と末尾が区別される線形の列です。ビーズ列の初期状態は文字列 S で表されます。

高橋君は、ビーズ列に対して次の 3 種類の操作を、好きな順序で 0 回以上、合計何回でも行うことができます。各操作はその時点でのビーズ列の状態に対して適用されます。

  • 左に回転:ビーズ列を巡回左シフトする。すなわち、先頭の 1 文字を取り除き、末尾に付加する。
  • 右に回転:ビーズ列を巡回右シフトする。すなわち、末尾の 1 文字を取り除き、先頭に付加する。
  • パターン複製N の正の約数 dd < N)を 1 つ選ぶ。d の値は操作ごとに自由に選ぶことができる。現在のビーズ列の先頭 d 文字をパターン P とし、ビーズ列全体を PN / d 回繰り返して連結した文字列で置き換える。

高橋君は、ビーズ列を目標の文字列 T と一致させたいです。

ST に一致させるために必要な最小の操作回数を求めてください。S がすでに T と一致している場合は 0 を出力してください。どのように操作しても T に一致させられない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 25
  • SAB からなる長さ N の文字列
  • TAB からなる長さ N の文字列

入力

N
S
T

N はビーズ列の長さを表す整数、S は初期状態のビーズ列を表す文字列、T は目標のビーズ列を表す文字列である。

出力

ST に一致させるために必要な最小の操作回数を 1 行で出力せよ。一致させられない場合は -1 を出力せよ。


入力例 1

6
ABBAAA
ABABAB

出力例 1

1

入力例 2

5
AAAAB
AABAB

出力例 2

-1

入力例 3

12
BAABABBAAAAA
ABBAABBAABBA

出力例 3

3

入力例 4

24
BABAAABBABBBABAAAABBABBA
AABBABAABBABAABBABAABBAB

出力例 4

5

入力例 5

1
A
A

出力例 5

0

Score : 400 pts

Problem Statement

Takahashi has a bead sequence represented by a string of length N consisting of two types of characters: A and B. The bead sequence is a linear sequence where the front and back are distinguished. The initial state of the bead sequence is represented by the string S.

Takahashi can perform the following 3 types of operations on the bead sequence, in any order, zero or more times, for any total number of times. Each operation is applied to the current state of the bead sequence.

  • Rotate left: Perform a cyclic left shift on the bead sequence. That is, remove the first character and append it to the end.
  • Rotate right: Perform a cyclic right shift on the bead sequence. That is, remove the last character and prepend it to the front.
  • Pattern duplication: Choose a positive divisor d of N (d < N). The value of d can be freely chosen for each operation. Let P be the first d characters of the current bead sequence, and replace the entire bead sequence with the string obtained by repeating P exactly N / d times concatenated together.

Takahashi wants to make the bead sequence match the target string T.

Find the minimum number of operations required to make S match T. If S already matches T, output 0. If it is impossible to make S match T no matter what operations are performed, output -1.

Constraints

  • 1 \leq N \leq 25
  • S is a string of length N consisting of A and B
  • T is a string of length N consisting of A and B

Input

N
S
T

N is an integer representing the length of the bead sequence, S is a string representing the initial state of the bead sequence, and T is a string representing the target bead sequence.

Output

Output in one line the minimum number of operations required to make S match T. If it is impossible to make them match, output -1.


Sample Input 1

6
ABBAAA
ABABAB

Sample Output 1

1

Sample Input 2

5
AAAAB
AABAB

Sample Output 2

-1

Sample Input 3

12
BAABABBAAAAA
ABBAABBAABBA

Sample Output 3

3

Sample Input 4

24
BABAAABBABBBABAAAABBABBA
AABBABAABBABAABBABAABBAB

Sample Output 4

5

Sample Input 5

1
A
A

Sample Output 5

0
E - 通信ネットワークの妨害

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 433

問題文

N 個の基地局からなる通信ネットワークがあります。基地局は 1 から N まで番号が付けられています。これらの基地局は N - 1 本の通信回線で結ばれており、どの基地局からどの基地局へも回線を辿って通信できます(つまり木構造をなしています)。i 番目の回線(1 \leq i \leq N-1)は基地局 A_i と基地局 B_i を双方向に結んでいます。また、基地局 j1 \leq j \leq N)にはデータ量 V_j のデータが保管されています。

このネットワークにおいて、攻撃者の高橋君と管理者の青木君が以下の手順でゲームを行います。

  1. 青木君の行動: N - 1 本の回線のうち 1 本を選んで 遮断 するか、あるいは 1 本も遮断しないかを決定します。遮断された回線は使用不能になります。遮断できる回線は 高々 1 です。
  2. 高橋君の行動: 青木君がどの回線を遮断したか(あるいは遮断しなかったか)をすべて知った上で、N 個の基地局の中から侵入先を 1 つ選びます。遮断によってネットワークが分断されていても、高橋君は任意の基地局を侵入先として選ぶことができます。
  3. 結果: 高橋君は、侵入先の基地局から 使用可能な(遮断されていない)回線のみ を辿って距離 D 以下で到達できるすべての基地局のデータを盗み出します。侵入先の基地局自身からも必ずデータを盗み出します。

ここで、2 つの基地局間の 距離 とは、使用可能な回線のみを辿って一方から他方へ到達するために経由する回線の本数の最小値として定義します。使用可能な回線のみでは到達できない場合、距離は \infty とみなします。特に、侵入先の基地局自身への距離は 0 です。

高橋君は盗み出すデータの合計量を 最大化 するように行動し、青木君はそれを 最小化 するように行動します。両者がともに最適に行動したとき、高橋君が盗み出せるデータの合計量を求めてください。

制約

  • 2 \leq N \leq 3000
  • 1 \leq D \leq N - 1
  • 1 \leq V_i \leq 10^91 \leq i \leq N
  • 1 \leq A_i, B_i \leq N1 \leq i \leq N - 1
  • 入力で与えられるグラフは木である
  • 入力はすべて整数である

入力

N D
V_1 V_2 \ldots V_N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
  • 1 行目には、基地局の数 N と侵入範囲を表す整数 D が、スペース区切りで与えられる。
  • 2 行目には、各基地局のデータ量 V_1, V_2, \ldots, V_N がスペース区切りで与えられる。
  • 続く N - 1 行のうち i 行目(1 \leq i \leq N - 1)には、i 番目の回線が結ぶ 2 つの基地局の番号 A_iB_i がスペース区切りで与えられる。

出力

両者が最適に行動したとき、高橋君が盗み出せるデータの合計量を 1 行に出力してください。


入力例 1

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

出力例 1

15

入力例 2

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

出力例 2

102

入力例 3

12 2
8 6 15 3 10 7 20 1 9 4 12 5
1 2
1 3
2 4
2 5
3 6
3 7
5 8
5 9
7 10
7 11
11 12

出力例 3

52

入力例 4

25 4
17 23 5 100 8 42 11 6 29 31 7 13 19 2 53 47 3 37 41 59 61 67 71 73 79
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
9 17
10 18
11 19
12 20
13 21
14 22
15 23
20 24
20 25

出力例 4

555

入力例 5

2 1
1000000000 1
1 2

出力例 5

1000000000

Score : 433 pts

Problem Statement

There is a communication network consisting of N base stations. The base stations are numbered from 1 to N. These base stations are connected by N - 1 communication lines, and any base station can communicate with any other base station by traversing the lines (that is, they form a tree structure). The i-th line (1 \leq i \leq N-1) bidirectionally connects base station A_i and base station B_i. Also, base station j (1 \leq j \leq N) stores data of volume V_j.

In this network, Takahashi (the attacker) and Aoki (the administrator) play a game according to the following procedure:

  1. Aoki's Action: Aoki chooses to disconnect one of the N - 1 lines, or decides not to disconnect any lines. Disconnected lines become unusable. Aoki can disconnect at most one line.
  2. Takahashi's Action: Knowing exactly which line Aoki disconnected (or that he did not disconnect any), Takahashi chooses one base station from the N base stations as the target of his intrusion. Even if the network is disconnected, Takahashi can choose any base station as the target.
  3. Result: Takahashi steals data from all base stations that can be reached from the target base station within a distance of D or less, using only the usable (not disconnected) lines. He always steals data from the target base station itself.

Here, the distance between two base stations is defined as the minimum number of lines traversed to reach one from the other using only usable lines. If they cannot reach each other using only usable lines, the distance is considered to be \infty. In particular, the distance from the target base station to itself is 0.

Takahashi acts to maximize the total volume of data stolen, and Aoki acts to minimize it. Find the total volume of data Takahashi can steal when both players play optimally.

Constraints

  • 2 \leq N \leq 3000
  • 1 \leq D \leq N - 1
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq A_i, B_i \leq N (1 \leq i \leq N - 1)
  • The graph given in the input is a tree.
  • All input values are integers.

Input

N D
V_1 V_2 \ldots V_N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
  • The first line contains the number of base stations N and the integer D representing the intrusion range, separated by a space.
  • The second line contains the data volumes of the base stations V_1, V_2, \ldots, V_N, separated by spaces.
  • In the following N - 1 lines, the i-th line (1 \leq i \leq N - 1) contains the numbers of the two base stations A_i and B_i connected by the i-th communication line, separated by a space.

Output

Print the total volume of data Takahashi can steal when both players play optimally in a single line.


Sample Input 1

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

Sample Output 1

15

Sample Input 2

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

Sample Output 2

102

Sample Input 3

12 2
8 6 15 3 10 7 20 1 9 4 12 5
1 2
1 3
2 4
2 5
3 6
3 7
5 8
5 9
7 10
7 11
11 12

Sample Output 3

52

Sample Input 4

25 4
17 23 5 100 8 42 11 6 29 31 7 13 19 2 53 47 3 37 41 59 61 67 71 73 79
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
9 17
10 18
11 19
12 20
13 21
14 22
15 23
20 24
20 25

Sample Output 4

555

Sample Input 5

2 1
1000000000 1
1 2

Sample Output 5

1000000000