Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は通信工学の実験で、N 個の中継器を通して信号を伝送するシステムを設計しています。
信号は高橋君の送信機から発信され、1 番目の中継器、2 番目の中継器、...、N 番目の中継器へと順番に伝送されていきます。
各中継器 i( 1 \leq i \leq N )には「伝送率」 A_i がパーセント単位の整数値で設定されています。中継器 i に入力される信号の強度を C とすると、中継器 i を通過した後の信号の強度は \left\lfloor \dfrac{C \times A_i}{100} \right\rfloor になります。ここで \lfloor x \rfloor は x 以下の最大の整数(床関数)を表します。
高橋君が送信機から発信する最初の信号の強度は M です。
N 個すべての中継器を順に通過した後の信号の強度を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^{16}
- 1 \leq A_i \leq 100( 1 \leq i \leq N )
- 入力はすべて整数
入力
N M A_1 A_2 \ldots A_N
- 1 行目には、中継器の個数を表す整数 N と、最初の信号の強度を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各中継器の伝送率を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
N 番目の中継器を通過した後の信号の強度を 1 行で出力せよ。
入力例 1
3 100 80 50 60
出力例 1
24
入力例 2
4 7 50 50 50 50
出力例 2
0
入力例 3
8 12345 99 87 73 100 64 58 91 77
出力例 3
2017
入力例 4
20 10000000000000000 97 88 76 100 65 54 43 92 81 70 59 48 37 100 99 83 67 52 41 30
出力例 4
1884542668173
入力例 5
1 1 1
出力例 5
0
Score : 233 pts
Problem Statement
Takahashi is designing a system that transmits a signal through N repeaters as part of a communications engineering experiment.
The signal is emitted from Takahashi's transmitter and is transmitted sequentially to the 1-st repeater, the 2-nd repeater, ..., and the N-th repeater.
Each repeater i (1 \leq i \leq N) has a "transmission rate" A_i set as an integer value in percent. If the strength of the signal input to repeater i is C, then the strength of the signal after passing through repeater i becomes \left\lfloor \dfrac{C \times A_i}{100} \right\rfloor. Here, \lfloor x \rfloor denotes the largest integer not exceeding x (the floor function).
The initial strength of the signal emitted from Takahashi's transmitter is M.
Find the strength of the signal after it has passed through all N repeaters in order.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^{16}
- 1 \leq A_i \leq 100 (1 \leq i \leq N)
- All inputs are integers.
Input
N M A_1 A_2 \ldots A_N
- The first line contains an integer N representing the number of repeaters and an integer M representing the initial signal strength, separated by a space.
- The second line contains integers A_1, A_2, \ldots, A_N representing the transmission rate of each repeater, separated by spaces.
Output
Print the strength of the signal after passing through the N-th repeater on a single line.
Sample Input 1
3 100 80 50 60
Sample Output 1
24
Sample Input 2
4 7 50 50 50 50
Sample Output 2
0
Sample Input 3
8 12345 99 87 73 100 64 58 91 77
Sample Output 3
2017
Sample Input 4
20 10000000000000000 97 88 76 100 65 54 43 92 81 70 59 48 37 100 99 83 67 52 41 30
Sample Output 4
1884542668173
Sample Input 5
1 1 1
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は学校のダンスパーティーの運営を任されました。参加者は N 人おり、それぞれ 1 から N までの番号が付けられています。
ダンスパーティーでは、参加者同士がペアを組んで踊ります。ただし、この学校には独特な伝統があり、各参加者 i には「優先度」と呼ばれる正の整数値 R_i が定められています。優先度が高い(値が大きい)人ほど先にペア相手を選ぶ権利があります。
青木君は参加者番号 1 の人物であり、自分がどの相手とペアになるかを気にしています。
参加者の中から M 組の「ペア候補」が事前に指定されています。 j 番目のペア候補は参加者 U_j と参加者 V_j の組であり、これは参加者 U_j と参加者 V_j が互いにペアを組む相手の候補であることを意味します。ペアは、この M 組の候補の中からのみ組むことができます。
ペア決めのルールは以下の通りです:
- N 人全員のペア相手を未定とする。
- ペア相手が未定の参加者のうち、優先度 R_i が最も大きい参加者を 1 人選ぶ。この人を参加者 x とする。
- 参加者 x とペア候補の関係にあり、かつペア相手が未定である参加者のうち、参加者番号が最も小さい人を 1 人選ぶ。この人を参加者 y とする。参加者 x と参加者 y をペアにする。
- ペア相手が未定の参加者がいなくなるまで、手順 2〜3 を繰り返す。
このルールにより、全体で N / 2 組のペアが作られます( N は偶数であることが保証されます)。
優先度の値 R_i は全員異なるため、手順 2 で選ばれる参加者は一意に定まります。また、手順 3 で参加者 x がペア候補の中から選べる相手が 0 人になることはないことが保証されます(すなわち、すべての参加者がちょうど 1 人の相手とペアを組めるような入力のみが与えられます)。
このルールに従ってペアの組み合わせを決めたとき、青木君(参加者番号 1 )のペア相手の参加者番号を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- N は偶数
- \frac{N}{2} \leq M \leq \min\left(\frac{N(N-1)}{2},\ 2 \times 10^5\right)
- 1 \leq R_i \leq 10^9 (1 \leq i \leq N)
- R_i はすべて異なる
- 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
- (U_j, V_j) はすべて異なる
- すべての参加者がちょうど 1 人の相手とペアを組めることが保証される
- 入力はすべて整数
入力
N M R_1 R_2 \ldots R_N U_1 V_1 U_2 V_2 : U_M V_M
- 1 行目には、参加者の人数を表す N と、ペア候補の数を表す M が、スペース区切りで与えられる。
- 2 行目には、各参加者の優先度を表す R_1, R_2, \ldots, R_N がスペース区切りで与えられる。
- 続く M 行にわたって、ペア候補の情報が与えられる。
- そのうち j 行目(入力全体では 2 + j 行目)では、 j 番目のペア候補である参加者 U_j と参加者 V_j が、スペース区切りで与えられる。
出力
青木君(参加者番号 1 )のペア相手の参加者番号を 1 行で出力せよ。
入力例 1
4 3 10 20 30 40 1 2 1 3 3 4
出力例 1
2
入力例 2
6 4 100 50 80 70 60 90 1 2 1 3 3 4 5 6
出力例 2
2
入力例 3
10 7 50 15 25 35 45 55 65 75 85 95 1 2 1 4 2 3 3 4 5 6 7 8 9 10
出力例 3
2
入力例 4
20 12 50 40 100 90 80 70 60 55 45 35 30 25 20 15 10 5 95 85 75 65 1 2 1 3 2 4 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
出力例 4
3
入力例 5
2 1 1 2 1 2
出力例 5
2
Score : 333 pts
Problem Statement
Takahashi has been put in charge of organizing the school's dance party. There are N participants, each numbered from 1 to N.
At the dance party, participants form pairs to dance together. However, this school has a unique tradition: each participant i has a positive integer value called "priority" R_i. A person with higher priority (larger value) has the right to choose their partner first.
Aoki is participant number 1, and he is concerned about who his partner will be.
M "pair candidates" have been designated in advance from among the participants. The j-th pair candidate is the pair of participant U_j and participant V_j, meaning that participant U_j and participant V_j are candidates to be paired with each other. Pairs can only be formed from these M candidates.
The pairing rules are as follows:
- Set all N participants' partners as undetermined.
- Among the participants whose partner is undetermined, select the one participant with the highest priority R_i. Call this participant x.
- Among the participants who are pair candidates with participant x and whose partner is undetermined, select the one with the smallest participant number. Call this participant y. Pair participant x with participant y.
- Repeat steps 2–3 until there are no participants with undetermined partners.
By this rule, a total of N / 2 pairs are formed (N is guaranteed to be even).
Since all priority values R_i are distinct, the participant selected in step 2 is uniquely determined. It is also guaranteed that in step 3, participant x will never have 0 available candidates to choose from (that is, the input is guaranteed to allow every participant to be paired with exactly one partner).
Determine the participant number of Aoki's (participant number 1) partner when the pairs are decided according to this rule.
Constraints
- 2 \leq N \leq 2 \times 10^5
- N is even
- \frac{N}{2} \leq M \leq \min\left(\frac{N(N-1)}{2},\ 2 \times 10^5\right)
- 1 \leq R_i \leq 10^9 (1 \leq i \leq N)
- All R_i are distinct
- 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
- All (U_j, V_j) are distinct
- It is guaranteed that every participant can be paired with exactly one partner
- All input values are integers
Input
N M R_1 R_2 \ldots R_N U_1 V_1 U_2 V_2 : U_M V_M
- The first line contains N, the number of participants, and M, the number of pair candidates, separated by a space.
- The second line contains the priorities R_1, R_2, \ldots, R_N of each participant, separated by spaces.
- The following M lines contain the pair candidate information.
- The j-th of these lines (the (2 + j)-th line overall) contains participant U_j and participant V_j of the j-th pair candidate, separated by a space.
Output
Output the participant number of Aoki's (participant number 1) partner on a single line.
Sample Input 1
4 3 10 20 30 40 1 2 1 3 3 4
Sample Output 1
2
Sample Input 2
6 4 100 50 80 70 60 90 1 2 1 3 3 4 5 6
Sample Output 2
2
Sample Input 3
10 7 50 15 25 35 45 55 65 75 85 95 1 2 1 4 2 3 3 4 5 6 7 8 9 10
Sample Output 3
2
Sample Input 4
20 12 50 40 100 90 80 70 60 55 45 35 30 25 20 15 10 5 95 85 75 65 1 2 1 3 2 4 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sample Output 4
3
Sample Input 5
2 1 1 2 1 2
Sample Output 5
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、ある会社の組織図を分析しています。
この会社の組織は N 人の社員からなり、組織図は根付き木で表されます。社員には 1 から N までの番号が付けられており、社員 1 が社長(根)です。各社員 i(2 \leq i \leq N)には直属の上司 P_i がおり、社員 i は社員 P_i の直属の部下です。
各社員 i には担当する仕事量を表す非負整数 W_i が割り当てられています。直属の部下を 1 人以上持つ社員(管理職)については W_i = 0 であることが保証されています。直属の部下を持たない社員(一般社員、すなわち木の葉に対応する社員)については W_i \geq 0 です(W_i = 0 の場合もあります)。
まず、各社員 u に対して、社員 u の チーム仕事量 S_u を、社員 u を根とする部分木に含まれるすべての社員(u 自身を含む)の仕事量の総和と定義します。すなわち、S_u = \displaystyle\sum_{v} W_v(和は社員 u を根とする部分木に含まれるすべての社員 v について取る)です。
次に、各社員 v の アンバランス度 を以下のように定義します。
- 社員 v の直属の部下が 2 人以上いるとき:直属の部下たちのチーム仕事量の最大値と最小値の差。すなわち、社員 v の直属の部下の集合を \{c_1, c_2, \ldots, c_k\}(k \geq 2)としたとき、\max(S_{c_1}, \ldots, S_{c_k}) - \min(S_{c_1}, \ldots, S_{c_k})。
- 社員 v の直属の部下が 1 人以下のとき(直属の部下がちょうど 1 人の管理職、および直属の部下を持たない一般社員のいずれも含む):0。
組織全体の アンバランス度 を、すべての社員のアンバランス度の最大値と定義します。
組織全体のアンバランス度を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq i - 1(2 \leq i \leq N)
- 0 \leq W_i \leq 10^9(1 \leq i \leq N)
- 社員 i が直属の部下を持つ(葉でない)とき、W_i = 0
- 入力はすべて整数である
入力
N P_2 P_3 \cdots P_N W_1 W_2 \cdots W_N
- 1 行目には、社員の数を表す整数 N が与えられる。
- 2 行目には、社員 2 から社員 N までの直属の上司の番号 P_2, P_3, \ldots, P_N が N - 1 個、空白区切りで与えられる。N = 1 のとき、この行は空行となる。
- 3 行目には、各社員の仕事量 W_1, W_2, \ldots, W_N が N 個、空白区切りで与えられる。
出力
組織全体のアンバランス度(すべての社員のアンバランス度の最大値)を 1 行で出力せよ。
入力例 1
3 1 1 0 10 3
出力例 1
7
入力例 2
7 1 1 2 2 3 3 0 0 0 8 2 9 1
出力例 2
8
入力例 3
10 1 1 1 2 2 3 4 4 4 0 0 0 0 20 5 12 100 3 1
出力例 3
99
Score : 366 pts
Problem Statement
Takahashi is analyzing the organizational chart of a company.
The organization consists of N employees, and the organizational chart is represented as a rooted tree. The employees are numbered from 1 to N, and employee 1 is the president (root). Each employee i (2 \leq i \leq N) has a direct supervisor P_i, meaning employee i is a direct subordinate of employee P_i.
Each employee i is assigned a non-negative integer W_i representing their workload. It is guaranteed that for employees who have one or more direct subordinates (managers), W_i = 0. For employees who have no direct subordinates (regular employees, i.e., employees corresponding to leaves of the tree), W_i \geq 0 (including the case W_i = 0).
First, for each employee u, define the team workload S_u of employee u as the sum of the workloads of all employees (including u themselves) in the subtree rooted at employee u. That is, S_u = \displaystyle\sum_{v} W_v (where the sum is taken over all employees v in the subtree rooted at employee u).
Next, define the imbalance of each employee v as follows:
- When employee v has 2 or more direct subordinates: the difference between the maximum and minimum team workloads among the direct subordinates. That is, if the set of direct subordinates of employee v is \{c_1, c_2, \ldots, c_k\} (k \geq 2), then \max(S_{c_1}, \ldots, S_{c_k}) - \min(S_{c_1}, \ldots, S_{c_k}).
- When employee v has 1 or fewer direct subordinates (this includes both managers with exactly 1 direct subordinate and regular employees with no direct subordinates): 0.
Define the imbalance of the entire organization as the maximum imbalance among all employees.
Find the imbalance of the entire organization.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq i - 1 (2 \leq i \leq N)
- 0 \leq W_i \leq 10^9 (1 \leq i \leq N)
- When employee i has direct subordinates (is not a leaf), W_i = 0
- All input values are integers
Input
N P_2 P_3 \cdots P_N W_1 W_2 \cdots W_N
- The first line contains an integer N representing the number of employees.
- The second line contains N - 1 integers P_2, P_3, \ldots, P_N, the direct supervisor numbers for employees 2 through N, separated by spaces. When N = 1, this line is an empty line.
- The third line contains N integers W_1, W_2, \ldots, W_N, the workloads of each employee, separated by spaces.
Output
Output the imbalance of the entire organization (the maximum imbalance among all employees) on a single line.
Sample Input 1
3 1 1 0 10 3
Sample Output 1
7
Sample Input 2
7 1 1 2 2 3 3 0 0 0 8 2 9 1
Sample Output 2
8
Sample Input 3
10 1 1 1 2 2 3 4 4 4 0 0 0 0 20 5 12 100 3 1
Sample Output 3
99
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君のクラスでは、体育の授業で背の順に並ぶことになりました。先生は生徒同士の身長がどれくらい散らばっているかを把握するため、すべての生徒のペアについて身長差の総和を調べることにしました。
クラスには N 人の生徒がおり、i 番目の生徒の身長は A_i センチメートルです。
先生は、N 人の中から異なる 2 人を選ぶすべての組み合わせについて、身長の差の絶対値を求め、その総和を計算したいと考えています。
すなわち、1 \leq i < j \leq N を満たすすべてのペア (i, j) について |A_i - A_j| を計算し、その総和を求めてください。身長が同じ生徒がいる場合でも、異なる人物の組であればそれぞれ 1 つのペアとして数えます。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^8
- 入力はすべて整数
- 答えは 2^{63} - 1 以下であることが保証される(64ビット符号付き整数型に収まる)
入力
N A_1 A_2 \ldots A_N
- 1 行目には、生徒の人数を表す整数 N が与えられる。
- 2 行目には、各生徒の身長を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
- A_i は i 番目の生徒の身長(センチメートル)を表す。
出力
1 \leq i < j \leq N を満たすすべてのペア (i, j) についての |A_i - A_j| の総和を 1 行で出力せよ。
入力例 1
4 150 160 170 180
出力例 1
100
入力例 2
5 160 160 150 170 150
出力例 2
100
入力例 3
10 172 165 180 158 165 190 175 168 182 160
出力例 3
551
入力例 4
20 145 172 168 190 155 160 177 182 149 171 166 158 193 174 169 161 185 152 178 164
出力例 4
2975
入力例 5
2 1 100000000
出力例 5
99999999
Score : 400 pts
Problem Statement
In Takahashi's class, students are to line up in order of height for their physical education class. The teacher wants to understand how spread out the students' heights are, so they decided to calculate the sum of height differences for all pairs of students.
There are N students in the class, and the height of the i-th student is A_i centimeters.
The teacher wants to find the absolute difference in height for every combination of 2 distinct students chosen from the N students, and calculate the total sum.
Specifically, for all pairs (i, j) satisfying 1 \leq i < j \leq N, compute |A_i - A_j| and find their total sum. Even if two students have the same height, as long as they are different individuals, they are counted as one pair.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^8
- All inputs are integers.
- It is guaranteed that the answer is at most 2^{63} - 1 (fits in a 64-bit signed integer).
Input
N A_1 A_2 \ldots A_N
- The first line contains an integer N, representing the number of students.
- The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the heights of the students.
- A_i represents the height (in centimeters) of the i-th student.
Output
Print in one line the sum of |A_i - A_j| over all pairs (i, j) satisfying 1 \leq i < j \leq N.
Sample Input 1
4 150 160 170 180
Sample Output 1
100
Sample Input 2
5 160 160 150 170 150
Sample Output 2
100
Sample Input 3
10 172 165 180 158 165 190 175 168 182 160
Sample Output 3
551
Sample Input 4
20 145 172 168 190 155 160 177 182 149 171 166 158 193 174 169 161 185 152 178 164
Sample Output 4
2975
Sample Input 5
2 1 100000000
Sample Output 5
99999999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君はハイキングコースの評価システムを開発しています。このシステムでは、1 から N までの番号が付けられた N 個のコースそれぞれについて「ハイキングスコア」を計算します。
各コースの番号を、先頭にゼロを付けない通常の十進表記で書いたとき、その桁数を k、各桁の数字を左(最上位桁)から順に d_1, d_2, \ldots, d_k とします。コース番号は 1 以上なので、d_1 \geq 1 です。このとき、d_1, d_2, \ldots, d_k をそれぞれコースの第 1 区間、第 2 区間、…、第 k 区間の標高(0 以上 9 以下の整数)とみなします。各コースは山道を模しており、桁の数字の並びが標高の変化を表しています。
ハイキングスコアは次のように定義されます。
まず、隣接する区間の標高差の絶対値をすべて足し合わせた値を 基本スコア B とします。すなわち、
B = \sum_{i=1}^{k-1} |d_i - d_{i+1}|
です。コース番号が 1 桁(k = 1)のときは隣接する区間の組が存在しないため、B = 0 とします。
次に、d_1, d_2, \ldots, d_k の中に標高 0 の区間が 1 つでも含まれている場合、そのコースには「休憩スポット」があるとみなされ、基本スコアが 2 倍されます。標高 0 の区間が 1 つも含まれていない場合、基本スコアはそのままです。
すなわち、倍率 m を
m = \begin{cases} 2 & (d_1, d_2, \ldots, d_k \text{ の中に } 0 \text{ が含まれる場合}) \\ 1 & (\text{それ以外の場合}) \end{cases}
と定めたとき、そのコースのハイキングスコアは m \times B です。
コースの番号の上限を表す整数 N が与えられたとき、コース 1 からコース N までの N 個のコースすべてのハイキングスコアの総和を 10^9 + 7 で割った余りを求めてください。
制約
- 1 \leq N \leq 10^{18}
- N は整数である
入力
N
- 1 行目には、コース番号の上限を表す整数 N が与えられる。
出力
コース 1 からコース N までのハイキングスコアの総和を 10^9 + 7 で割った余りを 1 行で出力せよ。
入力例 1
15
出力例 1
12
入力例 2
20
出力例 2
42
入力例 3
1000
出力例 3
7652
入力例 4
999999999999999999
出力例 4
243567135
入力例 5
1
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi is developing an evaluation system for hiking courses. This system calculates a "hiking score" for each of the N courses numbered from 1 to N.
When the number of each course is written in standard decimal notation without leading zeros, let k be the number of digits, and let d_1, d_2, \ldots, d_k be the digits from left (most significant digit) to right. Since the course number is at least 1, we have d_1 \geq 1. Here, d_1, d_2, \ldots, d_k are regarded as the elevations (integers between 0 and 9, inclusive) of the 1st section, 2nd section, …, k-th section of the course, respectively. Each course simulates a mountain trail, and the sequence of digits represents the changes in elevation.
The hiking score is defined as follows.
First, the base score B is defined as the sum of the absolute differences in elevation between all pairs of adjacent sections. That is,
B = \sum_{i=1}^{k-1} |d_i - d_{i+1}|
When the course number has only 1 digit (k = 1), there are no pairs of adjacent sections, so B = 0.
Next, if at least one section among d_1, d_2, \ldots, d_k has elevation 0, the course is considered to have a "rest spot," and the base score is doubled. If no section has elevation 0, the base score remains as is.
That is, defining the multiplier m as
m = \begin{cases} 2 & (\text{if } 0 \text{ is contained among } d_1, d_2, \ldots, d_k) \\ 1 & (\text{otherwise}) \end{cases}
the hiking score of that course is m \times B.
Given an integer N representing the upper limit of course numbers, find the sum of hiking scores of all N courses from course 1 to course N, modulo 10^9 + 7.
Constraints
- 1 \leq N \leq 10^{18}
- N is an integer
Input
N
- The first line contains an integer N representing the upper limit of course numbers.
Output
Print in one line the sum of hiking scores from course 1 to course N, modulo 10^9 + 7.
Sample Input 1
15
Sample Output 1
12
Sample Input 2
20
Sample Output 2
42
Sample Input 3
1000
Sample Output 3
7652
Sample Input 4
999999999999999999
Sample Output 4
243567135
Sample Input 5
1
Sample Output 5
0