A - Test Score Difference

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233

問題文

高橋君のクラスには高橋君以外に N 人の生徒がいます。先日行われたテストで、高橋君は X 点を取りました。i 番目 (1 \leq i \leq N) の生徒の点数は A_i 点です。

高橋君は自分より点数が高い生徒を見ると悔しく感じます。各生徒 i について、高橋君が感じる悔しさは A_i > X のとき A_i - X、そうでないとき 0 です。

高橋君が N 人の生徒全員に対して感じる悔しさの合計を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 0 \leq X \leq 100
  • 0 \leq A_i \leq 100
  • 入力はすべて整数

入力

N X
A_1 A_2 \cdots A_N
  • 1 行目には、高橋君以外の生徒の人数 N と高橋君の点数 X が、スペース区切りで与えられる。
  • 2 行目には、N 人の生徒の点数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

高橋君が感じる悔しさの合計値を整数で 1 行に出力してください。


入力例 1

5 60
55 60 61 80 40

出力例 1

21

入力例 2

4 100
99 100 80 0

出力例 2

0

入力例 3

12 48
30 48 49 50 10 100 47 48 51 52 0 75

出力例 3

89

入力例 4

30 73
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

出力例 4

351

入力例 5

1 0
100

出力例 5

100

Score : 233 pts

Problem Statement

There are N students in Takahashi's class besides Takahashi himself. In a recent test, Takahashi scored X points. The i-th student (1 \leq i \leq N) scored A_i points.

Takahashi feels frustrated when he sees a student who scored higher than him. For each student i, the frustration Takahashi feels is A_i - X if A_i > X, and 0 otherwise.

Find the total frustration Takahashi feels toward all N students.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 0 \leq X \leq 100
  • 0 \leq A_i \leq 100
  • All inputs are integers

Input

N X
A_1 A_2 \cdots A_N
  • The first line contains the number of students besides Takahashi N and Takahashi's score X, separated by a space.
  • The second line contains the scores of the N students A_1, A_2, \ldots, A_N, separated by spaces.

Output

Print the total frustration Takahashi feels as an integer on a single line.


Sample Input 1

5 60
55 60 61 80 40

Sample Output 1

21

Sample Input 2

4 100
99 100 80 0

Sample Output 2

0

Sample Input 3

12 48
30 48 49 50 10 100 47 48 51 52 0 75

Sample Output 3

89

Sample Input 4

30 73
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

Sample Output 4

351

Sample Input 5

1 0
100

Sample Output 5

100
B - Missing Survey and Team Division

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は N 人の生徒を赤チームと白チームの2つに分けようとしています。事前にアンケートを取り、各生徒に希望するチームを紙に書いてもらいました。

しかし、アンケート用紙の一部にコーヒーがこぼれてしまい、何人かの回答が読めなくなってしまいました。読めなくなった回答は ? として記録されており、元々「赤」だったか「白」だったかが判別できません。

各生徒の回答は、R(赤チーム希望)、W(白チーム希望)、?(判読不能)のいずれか1文字で表されます。

高橋君は、判読不能な回答(?)のそれぞれを R(赤チーム希望)または W(白チーム希望)に独立に置き換えることで、最終的な赤チーム希望者数と白チーム希望者数の差の絶対値を最小にしたいと考えています。

? を最適に置き換えたとき、赤チーム希望者数と白チーム希望者数の差の絶対値の最小値を求めてください。なお、? が1つも含まれない場合は、そのままの回答に基づいて差の絶対値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • S_iRW? のいずれかである (1 \leq i \leq N)

入力

N
S_1
S_2
\vdots
S_N
  • 1 行目には、生徒の人数を表す整数 N が与えられる。
  • i = 1, 2, \dots, N について、1 + i 行目には i 番目の生徒の回答を表す1文字 S_i が与えられる。S_iRW? のいずれかである。

出力

赤チーム希望者数と白チーム希望者数の差の絶対値の最小値を1行で出力せよ。


入力例 1

5
R
W
?
R
?

出力例 1

1

入力例 2

8
R
W
?
R
?
?
R
?

出力例 2

0

入力例 3

10
R
R
W
?
R
?
R
R
?
W

出力例 3

0

Score : 300 pts

Problem Statement

Takahashi is trying to divide N students into two teams: the Red team and the White team. He conducted a survey in advance, asking each student to write their preferred team on a piece of paper.

However, coffee was spilled on some of the survey papers, making several responses unreadable. The unreadable responses are recorded as ?, and it is impossible to determine whether they originally said "Red" or "White".

Each student's response is represented by a single character: R (prefers Red team), W (prefers White team), or ? (unreadable).

Takahashi wants to independently replace each unreadable response (?) with either R (prefers Red team) or W (prefers White team) so as to minimize the absolute difference between the final number of Red team supporters and White team supporters.

Find the minimum possible absolute difference between the number of Red team supporters and the number of White team supporters when the ?s are replaced optimally. If there are no ?s, simply compute the absolute difference based on the responses as they are.

Constraints

  • 1 \leq N \leq 10^5
  • S_i is one of R, W, or ? (1 \leq i \leq N)

Input

N
S_1
S_2
\vdots
S_N
  • The first line contains an integer N representing the number of students.
  • For i = 1, 2, \dots, N, the (1 + i)-th line contains a single character S_i representing the response of the i-th student. S_i is one of R, W, or ?.

Output

Print the minimum possible absolute difference between the number of Red team supporters and the number of White team supporters on a single line.


Sample Input 1

5
R
W
?
R
?

Sample Output 1

1

Sample Input 2

8
R
W
?
R
?
?
R
?

Sample Output 2

0

Sample Input 3

10
R
R
W
?
R
?
R
R
?
W

Sample Output 3

0
C - Simultaneous Firework Launch

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は花火大会の演出を担当しています。会場には N 台の花火発射装置が設置されており、それぞれ一定の間隔で花火を打ち上げます。

各発射装置 i1 \leq i \leq N)の打ち上げ間隔は T_i 秒です。ただし、T_i は整数とは限らず、正の有理数として与えられます。具体的には、各 T_i は既約分数 \frac{P_i}{Q_i}P_i, Q_i は正の整数、\gcd(P_i, Q_i) = 1)の形で入力されます。

すべての発射装置は時刻 0 に同時にスタートし、時刻 0 にも花火を打ち上げます。つまり、発射装置 i は時刻 0, T_i, 2T_i, 3T_i, \ldots に花火を打ち上げます。

高橋君はフィナーレの演出として、時刻 0 より後ですべての発射装置が同時に花火を打ち上げる最も早い時刻を知りたいと考えています。

ここで、正の有理数の組 T_1, T_2, \ldots, T_N に対する最小公倍数を次のように定義します。正の有理数 L が、すべての i1 \leq i \leq N)に対して \frac{L}{T_i} が正の整数となるとき、LT_1, T_2, \ldots, T_N公倍数といいます。公倍数は必ず存在し(例えば T_1 \times T_2 \times \cdots \times T_N の適切な正整数倍は公倍数です)、公倍数のうち最小のものを最小公倍数といいます。これは整数における最小公倍数の自然な拡張です。

求める時刻は T_1, T_2, \ldots, T_N の最小公倍数 L に等しくなります。L を既約分数として求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^9
  • 1 \leq Q_i \leq 10^9
  • \gcd(P_i, Q_i) = 1
  • 入力で与えられる値はすべて整数である。
  • 答えを既約分数 \frac{A}{B}A, B は正の整数、\gcd(A, B) = 1)で表したとき、A \leq 10^{18} かつ B \leq 10^{18} であることが保証される。

入力

N
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

1 行目には、発射装置の台数を表す整数 N が与えられる。続く N 行のうち i 行目には、発射装置 i の打ち上げ間隔 T_i = \frac{P_i}{Q_i} を表す分子 P_i と分母 Q_i が、スペース区切りで与えられる。

出力

最小公倍数 L を既約分数 \frac{A}{B}A, B は正の整数、\gcd(A, B) = 1)として、AB をスペース区切りで 1 行に出力せよ。L が整数となる場合は B = 1 として出力せよ。


入力例 1

2
1 2
1 3

出力例 1

1 1

入力例 2

3
2 3
4 9
8 27

出力例 2

8 3

入力例 3

5
3 4
5 6
7 3
2 9
1 2

出力例 3

210 1

入力例 4

8
12 5
18 7
15 4
20 3
14 9
25 6
21 8
10 11

出力例 4

6300 1

入力例 5

1
999999937 1

出力例 5

999999937 1

Score : 366 pts

Problem Statement

Takahashi is in charge of directing a fireworks festival. At the venue, N firework launchers are installed, each launching fireworks at regular intervals.

The launch interval of each launcher i (1 \leq i \leq N) is T_i seconds. However, T_i is not necessarily an integer and is given as a positive rational number. Specifically, each T_i is given as an irreducible fraction \frac{P_i}{Q_i} (P_i, Q_i are positive integers, \gcd(P_i, Q_i) = 1).

All launchers start simultaneously at time 0 and also launch fireworks at time 0. That is, launcher i launches fireworks at times 0, T_i, 2T_i, 3T_i, \ldots.

For the finale, Takahashi wants to know the earliest time after time 0 at which all launchers simultaneously launch fireworks.

Here, the least common multiple of a set of positive rational numbers T_1, T_2, \ldots, T_N is defined as follows. A positive rational number L is called a common multiple of T_1, T_2, \ldots, T_N if \frac{L}{T_i} is a positive integer for all i (1 \leq i \leq N). Common multiples always exist (for example, an appropriate positive integer multiple of T_1 \times T_2 \times \cdots \times T_N is a common multiple), and the smallest among all common multiples is called the least common multiple. This is a natural extension of the least common multiple for integers.

The desired time equals the least common multiple L of T_1, T_2, \ldots, T_N. Find L as an irreducible fraction.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^9
  • 1 \leq Q_i \leq 10^9
  • \gcd(P_i, Q_i) = 1
  • All values given in the input are integers.
  • It is guaranteed that when the answer is expressed as an irreducible fraction \frac{A}{B} (A, B are positive integers, \gcd(A, B) = 1), A \leq 10^{18} and B \leq 10^{18}.

Input

N
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

The first line contains an integer N representing the number of launchers. Each of the following N lines contains the numerator P_i and denominator Q_i representing the launch interval T_i = \frac{P_i}{Q_i} of launcher i, separated by a space.

Output

Express the least common multiple L as an irreducible fraction \frac{A}{B} (A, B are positive integers, \gcd(A, B) = 1), and output A and B separated by a space on a single line. If L is an integer, output with B = 1.


Sample Input 1

2
1 2
1 3

Sample Output 1

1 1

Sample Input 2

3
2 3
4 9
8 27

Sample Output 2

8 3

Sample Input 3

5
3 4
5 6
7 3
2 9
1 2

Sample Output 3

210 1

Sample Input 4

8
12 5
18 7
15 4
20 3
14 9
25 6
21 8
10 11

Sample Output 4

6300 1

Sample Input 5

1
999999937 1

Sample Output 5

999999937 1
D - Card Stacking

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は N 枚のカードを持っています。i 番目(1 \leq i \leq N)のカードの表面には正の整数 P_i が、裏面には正の整数 Q_i が書かれています。

高橋君はこれらのカードの中から 2 枚以上を選びたいと考えています。選んだカードの番号の集合を SS \subseteq \{1, 2, \ldots, N\}|S| \geq 2)とします。このとき、選んだカードの表面に書かれた整数の積と裏面に書かれた整数の積が等しい、すなわち

\prod_{i \in S} P_i = \prod_{i \in S} Q_i

が成り立つような S が存在するかどうかを判定してください。

存在する場合は、条件を満たす S における |S| の最小値を出力してください。存在しない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 22
  • 1 \leq P_i \leq 100
  • 1 \leq Q_i \leq 100
  • 入力はすべて整数である。

入力

N
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

1 行目には、カードの枚数を表す整数 N が与えられる。続く N 行の i 行目(1 \leq i \leq N)には、i 番目のカードの表面に書かれた整数 P_i と裏面に書かれた整数 Q_i がスペース区切りで与えられる。

出力

条件を満たす集合 S が存在する場合、|S| の最小値を 1 行で出力せよ。存在しない場合は -1 を出力せよ。


入力例 1

3
2 3
3 2
5 5

出力例 1

2

入力例 2

3
2 1
4 1
8 1

出力例 2

-1

入力例 3

8
2 3
3 5
5 2
7 1
11 1
13 1
17 1
19 1

出力例 3

3

入力例 4

15
2 3
3 5
5 7
7 2
11 1
13 1
17 1
19 1
23 1
29 1
31 1
37 1
41 1
43 1
47 1

出力例 4

4

入力例 5

2
5 5
3 3

出力例 5

2

Score : 400 pts

Problem Statement

Takahashi has N cards. The i-th card (1 \leq i \leq N) has a positive integer P_i written on its front side and a positive integer Q_i written on its back side.

Takahashi wants to select 2 or more cards from among these cards. Let S (S \subseteq \{1, 2, \ldots, N\}, |S| \geq 2) be the set of indices of the selected cards. Determine whether there exists such an S where the product of the integers written on the front sides of the selected cards equals the product of the integers written on the back sides, that is,

\prod_{i \in S} P_i = \prod_{i \in S} Q_i

If such an S exists, output the minimum value of |S| among all S satisfying the condition. If no such S exists, output -1.

Constraints

  • 2 \leq N \leq 22
  • 1 \leq P_i \leq 100
  • 1 \leq Q_i \leq 100
  • All input values are integers.

Input

N
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

The first line contains an integer N representing the number of cards. Each of the following N lines, the i-th line (1 \leq i \leq N), contains the integer P_i written on the front side and the integer Q_i written on the back side of the i-th card, separated by a space.

Output

If a set S satisfying the condition exists, output the minimum value of |S| in a single line. If no such set exists, output -1.


Sample Input 1

3
2 3
3 2
5 5

Sample Output 1

2

Sample Input 2

3
2 1
4 1
8 1

Sample Output 2

-1

Sample Input 3

8
2 3
3 5
5 2
7 1
11 1
13 1
17 1
19 1

Sample Output 3

3

Sample Input 4

15
2 3
3 5
5 7
7 2
11 1
13 1
17 1
19 1
23 1
29 1
31 1
37 1
41 1
43 1
47 1

Sample Output 4

4

Sample Input 5

2
5 5
3 3

Sample Output 5

2
E - Cyclic Password

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君の会社には、1 から N までの番号が付いた N 台の端末があります。

端末 i1 \leq i \leq N)には 1 桁の数字 D_i0 以上 9 以下)が表示されており、移動先の端末番号 P_i1 \leq P_i \leq N)が設定されています。

高橋君は、指定された開始端末から出発し、次の操作をちょうど K 回繰り返します。

  1. 現在いる端末に表示された数字を記録する。
  2. 現在いる端末の番号が i であるとき、端末 P_i へ移動する。

同じ端末を複数回訪問することもあります。

K 回の操作で順に記録された数字を d_1, d_2, \ldots, d_K とすると、得られる値 V は次のように定まります。

V = \sum_{t=1}^{K} d_t \cdot 10^{K-t}

これは、数字列 d_1 d_2 \ldots d_K10 進表記の非負整数とみなした値です(先頭が 0 であってもよい)。

K = 0 のとき(操作を一度も行わないとき)は V = 0 です。

Q 個の問い合わせが与えられます。j 番目(1 \leq j \leq Q)の問い合わせでは、開始端末の番号 S_j と操作回数 K_j が指定されます。端末 S_j から出発して操作を K_j 回行ったときに得られる VM で割った余りを求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • N + Q \leq 1.5 \times 10^5
  • 1 \leq M \leq 10^9
  • 0 \leq D_i \leq 9
  • 1 \leq P_i \leq N
  • 1 \leq S_j \leq N
  • 0 \leq K_j \leq 10^{18}
  • 入力はすべて整数である。

入力

N Q M
D_1 P_1
D_2 P_2
\vdots
D_N P_N
S_1 K_1
S_2 K_2
\vdots
S_Q K_Q
  • 1 行目には、端末の台数 N、問い合わせの個数 Q、割る数 M がスペース区切りで与えられる。
  • 続く N 行のうち i 行目には、端末 i に表示された数字 D_i と移動先の端末番号 P_i がスペース区切りで与えられる。
  • 続く Q 行のうち j 行目には、開始端末の番号 S_j と操作回数 K_j がスペース区切りで与えられる。

出力

Q 行出力せよ。

j 行目には、j 番目の問い合わせに対する答えとして、VM で割った余りを出力せよ。


入力例 1

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

出力例 1

123
423
234
0

入力例 2

3 5 13
0 2
5 2
9 1
1 1
1 3
3 2
2 4
3 0

出力例 2

0
3
12
4
0

入力例 3

9 8 257
6 2
2 3
0 4
7 2
5 3
1 5
9 8
3 9
4 7
1 4
1 10
6 6
5 0
7 5
8 12
9 1
3 100

出力例 3

39
58
118
0
202
176
4
110

入力例 4

20 12 999999937
8 2
1 3
4 4
1 5
5 6
9 4
2 3
6 7
0 8
3 11
7 12
8 13
2 14
4 12
9 16
5 17
0 18
1 19
3 20
6 18
1 6
1 25
9 10
10 4
10 123456789012345678
15 5
15 1000000000000000000
4 0
12 11
20 7
8 13
17 2

出力例 4

814159
175840913
624159159
3782
327335048
95013
215912785
0
482487648
6136136
591984774
1

入力例 5

1 4 1
7 1
1 0
1 1
1 10
1 1000000000000000000

出力例 5

0
0
0
0

Score : 466 pts

Problem Statement

Takahashi's company has N terminals numbered from 1 to N.

Terminal i (1 \leq i \leq N) displays a single digit D_i (between 0 and 9 inclusive) and has a destination terminal number P_i (1 \leq P_i \leq N) configured.

Takahashi starts from a specified starting terminal and repeats the following operation exactly K times.

  1. Record the digit displayed on the current terminal.
  2. If the current terminal number is i, move to terminal P_i.

The same terminal may be visited multiple times.

Let d_1, d_2, \ldots, d_K be the digits recorded in order over the K operations. The resulting value V is defined as follows:

V = \sum_{t=1}^{K} d_t \cdot 10^{K-t}

This is the value obtained by interpreting the digit string d_1 d_2 \ldots d_K as a non-negative integer in base 10 (leading zeros are allowed).

When K = 0 (no operations are performed), V = 0.

You are given Q queries. In the j-th query (1 \leq j \leq Q), a starting terminal number S_j and a number of operations K_j are specified. Find the remainder when V, obtained by starting from terminal S_j and performing K_j operations, is divided by M.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • N + Q \leq 1.5 \times 10^5
  • 1 \leq M \leq 10^9
  • 0 \leq D_i \leq 9
  • 1 \leq P_i \leq N
  • 1 \leq S_j \leq N
  • 0 \leq K_j \leq 10^{18}
  • All input values are integers.

Input

N Q M
D_1 P_1
D_2 P_2
\vdots
D_N P_N
S_1 K_1
S_2 K_2
\vdots
S_Q K_Q
  • The first line contains the number of terminals N, the number of queries Q, and the divisor M, separated by spaces.
  • In the following N lines, the i-th line contains the digit D_i displayed on terminal i and the destination terminal number P_i, separated by a space.
  • In the following Q lines, the j-th line contains the starting terminal number S_j and the number of operations K_j, separated by a space.

Output

Output Q lines.

On the j-th line, output the remainder when V is divided by M as the answer to the j-th query.


Sample Input 1

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

Sample Output 1

123
423
234
0

Sample Input 2

3 5 13
0 2
5 2
9 1
1 1
1 3
3 2
2 4
3 0

Sample Output 2

0
3
12
4
0

Sample Input 3

9 8 257
6 2
2 3
0 4
7 2
5 3
1 5
9 8
3 9
4 7
1 4
1 10
6 6
5 0
7 5
8 12
9 1
3 100

Sample Output 3

39
58
118
0
202
176
4
110

Sample Input 4

20 12 999999937
8 2
1 3
4 4
1 5
5 6
9 4
2 3
6 7
0 8
3 11
7 12
8 13
2 14
4 12
9 16
5 17
0 18
1 19
3 20
6 18
1 6
1 25
9 10
10 4
10 123456789012345678
15 5
15 1000000000000000000
4 0
12 11
20 7
8 13
17 2

Sample Output 4

814159
175840913
624159159
3782
327335048
95013
215912785
0
482487648
6136136
591984774
1

Sample Input 5

1 4 1
7 1
1 0
1 1
1 10
1 1000000000000000000

Sample Output 5

0
0
0
0