Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個の実数 A_1, A_2, \ldots, A_N が与えられます。添字のペア (i, j) (i < j) であって、積 A_i \cdot A_j が整数であるようなものの個数を求めてください。
制約
- 2 \leq N \leq 200\,000
- 0 < A_i < 10^4
- A_i は小数部の桁数が 9 以下であるような数として与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \vdots A_N
出力
積 A_i \cdot A_j が整数であるような添字のペア (i, j) (i < j) の個数を出力せよ。
入力例 1
5 7.5 2.4 17.000000001 17 16.000000000
出力例 1
3
積が整数であるようなペアは以下の 3 個です。
- 7.5 \cdot 2.4 = 18
- 7.5 \cdot 16 = 120
- 17 \cdot 16 = 272
入力例 2
11 0.9 1 1 1.25 2.30000 5 70 0.000000001 9999.999999999 0.999999999 1.000000001
出力例 2
8
Score : 300 points
Problem Statement
You are given N real values A_1, A_2, \ldots, A_N. Compute the number of pairs of indices (i, j) such that i < j and the product A_i \cdot A_j is integer.
Constraints
- 2 \leq N \leq 200\,000
- 0 < A_i < 10^4
- A_i is given with at most 9 digits after the decimal.
Input
Input is given from Standard Input in the following format.
N A_1 A_2 \vdots A_N
Output
Print the number of pairs with integer product A_i \cdot A_j (and i < j).
Sample Input 1
5 7.5 2.4 17.000000001 17 16.000000000
Sample Output 1
3
There are 3 pairs with integer product:
- 7.5 \cdot 2.4 = 18
- 7.5 \cdot 16 = 120
- 17 \cdot 16 = 272
Sample Input 2
11 0.9 1 1 1.25 2.30000 5 70 0.000000001 9999.999999999 0.999999999 1.000000001
Sample Output 2
8
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
リマクは、文字列の先頭 2 文字のうち片方を取り除くことを繰り返し行えます。例えば、abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx とすることができます。
N 個の相異なる文字列 S_1, S_2, \ldots, S_N が与えられます。N \cdot (N-1) / 2 個のペア (S_i, S_j) のうち何個において、リマクは一方からもう一方を得ることができるでしょうか。
制約
- 2 \leq N \leq 200\,000
- S_i は英小文字
a
-z
からなる。 - S_i \neq S_j
- 1 \leq |S_i|
- |S_1| + |S_2| + \ldots + |S_N| \leq 10^6
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
リマクが一方の文字列からもう一方を得られるような非順序対 (S_i, S_j) (i \neq j) の個数を出力せよ。
入力例 1
3 abcxyx cyx abc
出力例 1
1
条件を満たすペアは (abcxyx, cyx) のみです。
入力例 2
6 b a abc c d ab
出力例 2
5
条件を満たすペアは (b, abc), (a, abc), (abc, c), (b, ab), (a, ab) の 5 個です。
Score : 700 points
Problem Statement
Limak can repeatedly remove one of the first two characters of a string, for example abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx.
You are given N different strings S_1, S_2, \ldots, S_N. Among N \cdot (N-1) / 2 pairs (S_i, S_j), in how many pairs could Limak obtain one string from the other?
Constraints
- 2 \leq N \leq 200\,000
- S_i consists of lowercase English letters
a
-z
. - S_i \neq S_j
- 1 \leq |S_i|
- |S_1| + |S_2| + \ldots + |S_N| \leq 10^6
Input
Input is given from Standard Input in the following format.
N S_1 S_2 \vdots S_N
Output
Print the number of unordered pairs (S_i, S_j) where i \neq j and Limak can obtain one string from the other.
Sample Input 1
3 abcxyx cyx abc
Sample Output 1
1
The only good pair is (abcxyx, cyx).
Sample Input 2
6 b a abc c d ab
Sample Output 2
5
There are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
P を素数 200\,003 とします。N 個の整数 A_1, A_2, \ldots, A_N が与えられるので、N \cdot (N-1) / 2 個すべての非順序対 (A_i, A_j) (i < j) に対する ((A_i \cdot A_j) \bmod P) の和を求めてください。
和を P で割った余りを求めるのではないことに注意してください。
制約
- 2 \leq N \leq 200\,000
- 0 \leq A_i < P = 200\,003
- 入力中のすべての値は整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \cdots A_N
出力
一つの整数、すなわち ((A_i \cdot A_j) \bmod P) の和を出力せよ。
入力例 1
4 2019 0 2020 200002
出力例 1
474287
0 でない積は以下の通りです。
- 2019 \cdot 2020 \bmod P = 78320
- 2019 \cdot 200002 \bmod P = 197984
- 2020 \cdot 200002 \bmod P = 197983
よって、答えは 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287 となります。
入力例 2
5 1 1 2 2 100000
出力例 2
600013
Score : 800 points
Problem Statement
Let’s take a prime P = 200\,003. You are given N integers A_1, A_2, \ldots, A_N. Find the sum of ((A_i \cdot A_j) \bmod P) over all N \cdot (N-1) / 2 unordered pairs of elements (i < j).
Please note that the sum isn't computed modulo P.
Constraints
- 2 \leq N \leq 200\,000
- 0 \leq A_i < P = 200\,003
- All values in input are integers.
Input
Input is given from Standard Input in the following format.
N A_1 A_2 \cdots A_N
Output
Print one integer — the sum over ((A_i \cdot A_j) \bmod P).
Sample Input 1
4 2019 0 2020 200002
Sample Output 1
474287
The non-zero products are:
- 2019 \cdot 2020 \bmod P = 78320
- 2019 \cdot 200002 \bmod P = 197984
- 2020 \cdot 200002 \bmod P = 197983
So the answer is 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287.
Sample Input 2
5 1 1 2 2 100000
Sample Output 2
600013
Time Limit: 2.5 sec / Memory Limit: 1024 MiB
配点 : 1000 点
問題文
テレビドラマシリーズ『ストレンジャー・シングス』に触発されたクマのリマクは、現実世界と鏡像世界を歩いて行き来することにしました。
二つの高さ H の完全二分木があり、それぞれの頂点は 1 から 2^H-1 までの標準的な番号付けがなされています。 すなわち、根の番号は 1 で、番号 x の子の番号は 2 \cdot x と 2 \cdot x + 1 です。
一つの木が持つ葉の数を L とします。すなわち、L = 2^{H-1} です。
数列 1, \ldots, L の順列 P_1, P_2, \ldots, P_L が与えられます。これは、二つの木を結ぶ L 本の 特殊辺 を表します。 すなわち、第一の木の番号 L+i-1 の頂点と第二の木の番号 L+P_i-1 の頂点が一本の特殊辺で結ばれています。
入力例 1 の図示。P = (2, 3, 1, 4) であり、緑の辺が特殊辺
サイクルの 積 を、それに含まれる頂点の番号の積と定義します。ちょうど二本の特殊辺を持つような すべての単純サイクルの積の和を (10^9+7) で割った余りを求めてください。
なお、単純サイクルとは、長さが 3 以上であって頂点や辺の重複がないようなサイクルです。
制約
- 2 \leq H \leq 18
- 1 \leq P_i \leq L (ただし L = 2^{H-1})
- P_i \neq P_j (すなわち、この数列は 1, \ldots, L の順列である)
入力
入力は以下の形式で標準入力から与えられる (ここで、L = 2^{H-1} である)。
H P_1 P_2 \cdots P_L
出力
ちょうど二本の特殊辺を持つようなすべての単純サイクルの積の和を求め、それを (10^9+7) で割った余りを出力せよ。
入力例 1
3 2 3 1 4
出力例 1
121788
下図に考慮すべきサイクルを二つ示します (他にも存在します)。一つめのサイクルの積は 2 \cdot 5 \cdot 6 \cdot 3 \cdot 1 \cdot 2 \cdot 5 \cdot 4 = 7200、二つめのサイクルの積は 1 \cdot 3 \cdot 7 \cdot 7 \cdot 3 \cdot 1 \cdot 2 \cdot 5 \cdot 4 \cdot 2 = 35280 です。三つめのサイクルは、特殊辺の数が二本でないため、考慮すべきサイクルではありません。
入力例 2
2 1 2
出力例 2
36
グラフ中の唯一のサイクルは確かに特殊辺を二本持ち、その頂点番号の積は 1 \cdot 3 \cdot 3 \cdot 1 \cdot 2 \cdot 2 = 36 です。
入力例 3
5 6 14 15 7 12 16 5 4 11 9 3 10 8 2 13 1
出力例 3
10199246
Score : 1000 points
Problem Statement
Inspired by the tv series Stranger Things, bear Limak is going for a walk between two mirror worlds.
There are two perfect binary trees of height H, each with the standard numeration of vertices from 1 to 2^H-1. The root is 1 and the children of x are 2 \cdot x and 2 \cdot x + 1.
Let L denote the number of leaves in a single tree, L = 2^{H-1}.
You are given a permutation P_1, P_2, \ldots, P_L of numbers 1 through L. It describes L special edges that connect leaves of the two trees. There is a special edge between vertex L+i-1 in the first tree and vertex L+P_i-1 in the second tree.
drawing for the first sample test, permutation P = (2, 3, 1, 4), special edges in green
Let's define product of a cycle as the product of numbers in its vertices. Compute the sum of products of all simple cycles that have exactly two special edges, modulo (10^9+7).
A simple cycle is a cycle of length at least 3, without repeated vertices or edges.
Constraints
- 2 \leq H \leq 18
- 1 \leq P_i \leq L where L = 2^{H-1}
- P_i \neq P_j (so this is a permutation)
Input
Input is given from Standard Input in the following format (where L = 2^{H-1}).
H P_1 P_2 \cdots P_L
Output
Compute the sum of products of simple cycles that have exactly two special edges. Print the answer modulo (10^9+7).
Sample Input 1
3 2 3 1 4
Sample Output 1
121788
The following drawings show two valid cycles (but there are more!) with products 2 \cdot 5 \cdot 6 \cdot 3 \cdot 1 \cdot 2 \cdot 5 \cdot 4 = 7200 and 1 \cdot 3 \cdot 7 \cdot 7 \cdot 3 \cdot 1 \cdot 2 \cdot 5 \cdot 4 \cdot 2 = 35280. The third cycle is invalid because it doesn't have exactly two special edges.
Sample Input 2
2 1 2
Sample Output 2
36
The only simple cycle in the graph indeed has two special edges, and the product of vertices is 1 \cdot 3 \cdot 3 \cdot 1 \cdot 2 \cdot 2 = 36.
Sample Input 3
5 6 14 15 7 12 16 5 4 11 9 3 10 8 2 13 1
Sample Output 3
10199246
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 1800 点
問題文
これは output-only 問題です。入力は与えられません。
簡潔に述べると、整数の掛け算を、比較 (x < y) と加算 (x + y) のみを用いてシミュレートする問題です。 入力はありません。操作列の出力のみを行ってください。
長さ N の長大な配列 a[0], a[1], ..., a[N-1] があるとします。 先頭の二要素の初期値は非負整数 A, B であり (あなたには知らされません)、残りの要素の初期値は 0 です。 あなたの目的は、最終的に a[2] を積 A \cdot B とすることです。
あなたが行えるのは、以下の形式の二種類の操作です (ここで、0 \leq i, j, k < N です)。
+ i j k
: a[k] = a[i] + a[j] とする。< i j k
: a[k] = a[i] < a[j] とする。すなわち、a[i] < a[j] であれば a[k] は 1 となり、そうでなければ a[k] は 0 となる。
操作を行える回数は最大で Q 回であり、操作中に a の要素が V より大きくなってはなりません。 ただし、一回の操作で指定する添字 (i, j, k) に重複があっても構わず、(先頭二要素を含む) 配列のどの要素も書き換えて構いません。 なお、この問題の正誤判定機は、単一テストケースにおいて実際には複数のペア (A, B) について操作列を実行します。 各回について、判定機は値 A, B を選んで配列 a = [A, B, 0, 0, \ldots, 0] を生成し、提出された操作列を適用して最終的に a[2] = A \cdot B となるかを検証します。
制約
- 0 \leq A, B \leq 10^9
- N = Q = 200\,000
- V = 10^{19} = 10\,000\,000\,000\,000\,000\,000
部分点
- A, B \leq 10 を満たすテストケースを通過すると、800 点が与えられる。
- すべてのテストケースを通過すると、さらに 1000 点が与えられる。
入力
標準入力から与えられる入力は空である。
出力
1 行目に、操作を行う回数を出力せよ。
続けて、行う操作をそれぞれ + i j k
または < i j k
という形式で一行に出力せよ。
Sample Input 1
Sample Output 1
4 < 0 1 8 + 0 1 2 + 2 8 2 + 0 0 0
入力例 1 では、正誤判定機はペア (A, B) = (2, 3) についてのみ提出された操作列を検証します。 上記の出力は、このテストケースを通過します。その過程は次の通りです。
- はじめ、a[0] = 2, a[1] = 3, a[2] = a[3] = \ldots = a[N-1] = 0 である。
< 0 1 8
により、a[0] < a[1] であるため a[8] = 1 となる。+ 0 1 2
により、a[2] = a[0] + a[1] = 5 となる。+ 2 8 2
により、a[2] = a[2] + a[8] = 6 となる。+ 0 0 0
により、a[0] = a[0] + a[0] = 4 となる。- 要求された通り、最終的に a[2] = 6 = A \cdot B となっている。
Score : 1800 points
Problem Statement
This is an output-only problem. You shouldn't read anything from the input.
In short, your task is to simulate multiplication by using only comparison (x < y) and addition (x + y). There is no input in this problem, you just print a sequence of operations.
Imagine that there is a big array a[0], a[1], ..., a[N-1] of length N. The first two values are initially two non-negative integers A and B (which are unknown to you), the other elements are zeros. Your goal is to get the product A \cdot B in a[2] at the end.
You are allowed operations of two types, with the following format (where 0 \leq i, j, k < N):
+ i j k
— applies operation a[k] = a[i] + a[j].< i j k
— applies operation a[k] = a[i] < a[j]. That is, if a[i] < a[j] then a[k] becomes 1, otherwise it becomes 0.
You can use at most Q operations. Elements of a can't exceed V. Indices (i, j, k) don't have to be distinct. It's allowed to modify any element of the array (including the first two). The actual checker simulates the process for multiple pairs (A, B) within a single test. Each time, the checker chooses values A and B, creates the array a = [A, B, 0, 0, \ldots, 0], applies all your operations and ensures that a[2] = A \cdot B.
Constraints
- 0 \leq A, B \leq 10^9
- N = Q = 200\,000
- V = 10^{19} = 10\,000\,000\,000\,000\,000\,000
Partial Score
- 800 points will be awarded for passing tests that satisfy A, B \leq 10.
- Another 1000 points will be awarded for passing all tests.
Input
The Standard Input is empty.
Output
In the first line, print the number of operations.
Each operation should then be printed in a single line of format + i j k
or < i j k
.
Sample Input 1
Sample Output 1
4 < 0 1 8 + 0 1 2 + 2 8 2 + 0 0 0
In the first sample test, the checker checks your sequence only for a pair (A, B) = (2, 3). The provided output is correct for this test:
- Initially, a[0] = 2, a[1] = 3, a[2] = a[3] = \ldots = a[N-1] = 0.
< 0 1 8
applies a[8] = 1 because a[0] < a[1].+ 0 1 2
applies a[2] = a[0] + a[1] = 5.+ 2 8 2
applies a[2] = a[2] + a[8] = 6.+ 0 0 0
applies a[0] = a[0] + a[0] = 4.- As required, at the end we have a[2] = 6 = A \cdot B.
Time Limit: 1.25 sec / Memory Limit: 1024 MiB
配点 : 2200 点
問題文
無限に広がるチェス盤上の N 個の敵ルークの位置 (X_i, Y_i) が与えられます。[訳注: ルークは将棋の飛車と似た動きをする駒です。] どの二つのルークも、お互いを攻められる位置にはありません (すなわち、一行あたり、または一列あたりのルークの数は一個以下です)。
あなたは、ルークのうち一個をキングに置き換え、キングを繰り返し動かしてできるだけ多くのルークを取ろうとしています。[訳注: キングは将棋の王将と似た動きをする駒です。]
ルークに攻められているマスに入ることはできません。 また、斜めに移動することで空きマスに移ることもできません (しかし、斜めに移動することでルークを取ることはできます)。
(つまり、このキングの動きは、斜め四方向に動いて駒を取ることと縦横四方向に動くことができる強化版ポーンのようなものです。)
各ルークについて、そのルークをキングで置き換えた際に取ることのできる最大数のルークを取るために必要な最小手数を求めてください。
制約
- 2 \leq N \leq 200\,000
- 1 \leq X_i, Y_i \leq 10^6
- X_i \neq X_j
- Y_i \neq Y_j
- 入力中の値はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
N 行出力せよ。 i 行目は、(X_i, Y_i) に置かれたルークをキングに置き換えた場合に対応する。 この行には、一つの整数、すなわち M_i 個のルークを取るために必要な最小手数を出力せよ。 ここで、M_i は (何手かけてもよいとして) この場合に取ることのできるルークの最大数である。
入力例 1
6 1 8 6 10 2 7 4 4 9 3 5 1
出力例 1
5 0 7 5 0 0
下図を見てください。 ルーク 3 をキングに置き換えた場合、他のルークを最大で二個取ることができます。 図の赤い経路がこの場合の最適な手順の一つ - ルーク 1 を取り、右下方向に進み続けてルーク 4 を取る - です。 ここでの手数は 7 手であり、これが出力例の三つ目の数です。
x 軸正方向: 右、y 軸正方向: 上
ルーク 2, 5, 6 のいずれかをキングに置き換えた場合には、他のルークを一個も取ることができません。このとき、最小手数は 0 です。
入力例 2
5 5 5 100 100 70 20 81 70 800 1
出力例 2
985 985 1065 1034 0
入力例 3
10 2 5 4 4 13 12 12 13 14 17 17 19 22 22 16 18 19 27 25 26
出力例 3
2 2 9 9 3 3 24 5 0 25
Score : 2200 points
Problem Statement
You are given positions (X_i, Y_i) of N enemy rooks on an infinite chessboard. No two rooks attack each other (at most one rook per row or column).
You're going to replace one rook with a king and then move the king repeatedly to beat as many rooks as possible.
You can't enter a cell that is being attacked by a rook. Additionally, you can't move diagonally to an empty cell (but you can beat a rook diagonally).
(So this king moves like a superpawn that beats diagonally in 4 directions and moves horizontally/vertically in 4 directions.)
For each rook, consider replacing it with a king, and find the minimum possible number of moves needed to beat the maximum possible number of rooks.
Constraints
- 2 \leq N \leq 200\,000
- 1 \leq X_i, Y_i \leq 10^6
- X_i \neq X_j
- Y_i \neq Y_j
- All values in input are integers.
Input
Input is given from Standard Input in the following format.
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print N lines. The i-th line is for scenario of replacing the rook at (X_i, Y_i) with your king. This line should contain one integer: the minimum number of moves to beat M_i rooks where M_i denotes the maximum possible number of beaten rooks in this scenario (in infinite time).
Sample Input 1
6 1 8 6 10 2 7 4 4 9 3 5 1
Sample Output 1
5 0 7 5 0 0
See the drawing below. If we replace rook 3 with a king, we can beat at most two other rooks. The red path is one of optimal sequences of moves: beat rook 1, then keep going down and right until you can beat rook 4. There are 7 steps and that's the third number in the output.
x-coordinate increases from left to right, while y increases bottom to top.
Starting from rook 2, 5 or 6, we can't beat any other rook. The optimal number of moves is 0.
Sample Input 2
5 5 5 100 100 70 20 81 70 800 1
Sample Output 2
985 985 1065 1034 0
Sample Input 3
10 2 5 4 4 13 12 12 13 14 17 17 19 22 22 16 18 19 27 25 26
Sample Output 3
2 2 9 9 3 3 24 5 0 25