実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
正整数 N が与えられます。
N 以下の正整数であって回文立方数であるものの最大値を求めてください。
ただし、正整数 K は以下の 2 つの条件を満たすとき、またそのときに限り回文立方数であると定義します。
- ある正整数 x が存在し、x^3 = K を満たす。
- K を先頭に 0 をつけずに 10 進表記した文字列が回文となる。より厳密には、0 以上 9 以下の整数 A_0, A_1, \ldots, A_{L-2} および 1 以上 9 以下の整数 A_{L-1} を用いて K = \sum_{i = 0}^{L-1} A_i10^i と表記したときに i = 0, 1, \ldots, L-1 に対して A_i = A_{L-1-i} を満たす。
制約
- N は 10^{18} 以下の正整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
345
出力例 1
343
343 は回文立方数であり、344, 345 は回文立方数ではありません。したがって、343 が答えとなります。
入力例 2
6
出力例 2
1
入力例 3
123456789012345
出力例 3
1334996994331
Score: 250 points
Problem Statement
You are given a positive integer N.
Find the maximum value of a palindromic cube number not greater than N.
Here, a positive integer K is defined to be a palindromic cube number if and only if it satisfies the following two conditions:
- There is a positive integer x such that x^3 = K.
- The decimal representation of K without leading zeros is a palindrome. More precisely, if K is represented as K = \sum_{i = 0}^{L-1} A_i10^i using integers A_0, A_1, \ldots, A_{L-2} between 0 and 9, inclusive, and an integer A_{L-1} between 1 and 9, inclusive, then A_i = A_{L-1-i} for all i = 0, 1, \ldots, L-1.
Constraints
- N is a positive integer not greater than 10^{18}.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
345
Sample Output 1
343
343 is a palindromic cube number, while 344 and 345 are not. Thus, the answer is 343.
Sample Input 2
6
Sample Output 2
1
Sample Input 3
123456789012345
Sample Output 3
1334996994331
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
以下の条件を満たす正整数 x を 321-like Number と呼びます。 この定義は A 問題と同様です。
- x の各桁を上から見ると狭義単調減少になっている。
- すなわち、x が d 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
- ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )
なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。
例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。
K 番目に小さい 321-like Number を求めてください。
制約
- 入力は全て整数
- 1 \le K
- 321-like Number は K 個以上存在する
入力
入力は以下の形式で標準入力から与えられる。
K
出力
K 番目に小さい 321-like Number を整数として出力せよ。
入力例 1
15
出力例 1
32
321-like Number は小さいものから順に (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) です。
このうち 15 番目に小さいものは 32 です。
入力例 2
321
出力例 2
9610
入力例 3
777
出力例 3
983210
Score : 300 points
Problem Statement
A positive integer x is called a 321-like Number when it satisfies the following condition. This definition is the same as the one in Problem A.
- The digits of x are strictly decreasing from top to bottom.
- In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
- (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).
Note that all one-digit positive integers are 321-like Numbers.
For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.
Find the K-th smallest 321-like Number.
Constraints
- All input values are integers.
- 1 \le K
- At least K 321-like Numbers exist.
Input
The input is given from Standard Input in the following format:
K
Output
Print the K-th smallest 321-like Number as an integer.
Sample Input 1
15
Sample Output 1
32
The 321-like Numbers are (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) from smallest to largest.
The 15-th smallest of them is 32.
Sample Input 2
321
Sample Output 2
9610
Sample Input 3
777
Sample Output 3
983210
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君が住んでいる二次元平面上の街には N 個のジャンプ台があります。i 番目のジャンプ台は点 (x_i, y_i) にあり、ジャンプ台のパワーは P_i です。また高橋君のジャンプ力は S で表され、はじめ S=0 です。高橋君が訓練を 1 回行う度に S は 1 増えます。
高橋君は以下の条件を満たす場合に限り、i 番目のジャンプ台から j 番目のジャンプ台にジャンプすることができます。
- P_iS\geq |x_i - x_j| +|y_i - y_j|
高橋君の目的は、適切に始点とするジャンプ台を決めることで、そのジャンプ台からどのジャンプ台にも何回かのジャンプで移動できるようにすることです。
目的を達成するためには高橋君は最低で何回訓練を行う必要があるでしょうか?
制約
- 2 \leq N \leq 200
- -10^9 \leq x_i,y_i \leq 10^9
- 1 \leq P_i \leq 10^9
- (x_i, y_i) \neq (x_j,y_j)\ (i\neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 P_1 \vdots x_N y_N P_N
出力
答えを出力せよ。
入力例 1
4 -10 0 1 0 0 5 10 0 1 11 0 1
出力例 1
2
高橋君が 2 回訓練したとすると、 S=2 です。 このとき、2 番目のジャンプ台から全てのジャンプ台に移動することができます。
例えば、4 番目のジャンプ台へは以下の方法で移動ができます。
-
2 番目のジャンプ台から 3 番目のジャンプ台へジャンプする。( P_2 S = 10, |x_2-x_3| + |y_2-y_3| = 10 であり、 P_2 S \geq |x_2-x_3| + |y_2-y_3| を満たす。)
-
3 番目のジャンプ台から 4 番目のジャンプ台へジャンプする。( P_3 S = 2, |x_3-x_4| + |y_3-y_4| = 1 であり、 P_3 S \geq |x_3-x_4| + |y_3-y_4| を満たす。)
入力例 2
7 20 31 1 13 4 3 -10 -15 2 34 26 5 -2 39 4 0 -50 1 5 -20 2
出力例 2
18
Score : 400 points
Problem Statement
There are N trampolines on a two-dimensional planar town where Takahashi lives. The i-th trampoline is located at the point (x_i, y_i) and has a power of P_i. Takahashi's jumping ability is denoted by S. Initially, S=0. Every time Takahashi trains, S increases by 1.
Takahashi can jump from the i-th to the j-th trampoline if and only if:
- P_iS\geq |x_i - x_j| +|y_i - y_j|.
Takahashi's objective is to become able to choose a starting trampoline such that he can reach any trampoline from the chosen one with some jumps.
At least how many times does he need to train to achieve his objective?
Constraints
- 2 \leq N \leq 200
- -10^9 \leq x_i,y_i \leq 10^9
- 1 \leq P_i \leq 10^9
- (x_i, y_i) \neq (x_j,y_j)\ (i\neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N x_1 y_1 P_1 \vdots x_N y_N P_N
Output
Print the answer.
Sample Input 1
4 -10 0 1 0 0 5 10 0 1 11 0 1
Sample Output 1
2
If he trains twice, S=2, in which case he can reach any trampoline from the 2-nd one.
For example, he can reach the 4-th trampoline as follows.
-
Jump from the 2-nd to the 3-rd trampoline. (Since P_2 S = 10 and |x_2-x_3| + |y_2-y_3| = 10, it holds that P_2 S \geq |x_2-x_3| + |y_2-y_3|.)
-
Jump from the 3-rd to the 4-th trampoline. (Since P_3 S = 2 and |x_3-x_4| + |y_3-y_4| = 1, it holds that P_3 S \geq |x_3-x_4| + |y_3-y_4|.)
Sample Input 2
7 20 31 1 13 4 3 -10 -15 2 34 26 5 -2 39 4 0 -50 1 5 -20 2
Sample Output 2
18
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
AtCoder 食堂では主菜と副菜からなる定食が販売されています。
主菜は N 種類あり、順に主菜 1, 主菜 2, \dots, 主菜 N と呼びます。主菜 i の価格は a_i 円です。
副菜は M 種類あり、順に副菜 1, 副菜 2, \dots, 副菜 M と呼びます。副菜 i の価格は b_i 円です。
定食は主菜と副菜を 1 種類ずつ選んで構成されます。定食の価格は選んだ主菜の価格と副菜の価格の和です。
ただし、L 個の相異なる組 (c_1, d_1), \dots, (c_L, d_L) について、主菜 c_i と副菜 d_i からなる定食は食べ合わせが悪いため提供されていません。
つまり、提供されている定食は NM - L 種類あることになります。(提供されている定食が少なくとも 1 種類存在することが制約によって保証されています。)
提供されている定食のうち、最も価格の高い定食の価格を求めてください。
制約
- 1 \leq N, M \leq 10^5
- 0 \leq L \leq \min(10^5, N M - 1)
- 1 \leq a_i, b_i \leq 10^9
- 1 \leq c_i \leq N
- 1 \leq d_j \leq M
- i \neq j ならば (c_i, d_i) \neq (c_j, d_j)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M L a_1 a_2 \dots a_N b_1 b_2 \dots b_M c_1 d_1 c_2 d_2 \vdots c_L d_L
出力
提供されている定食のうち、最も価格の高い定食が何円であるかを出力せよ。
入力例 1
2 3 3 2 1 10 30 20 1 2 2 1 2 3
出力例 1
31
提供されている定食、及びその価格は次の 3 種類です。
- 主菜 1 と副菜 1 からなる定食。価格は 2 + 10 = 12 円である。
- 主菜 1 と副菜 3 からなる定食。価格は 2 + 20 = 22 円である。
- 主菜 2 と副菜 2 からなる定食。価格は 1 + 30 = 31 円である。
この中で最も高い定食は 3 番目の定食です。よって 31 を出力してください。
入力例 2
2 1 0 1000000000 1 1000000000
出力例 2
2000000000
入力例 3
10 10 10 47718 21994 74148 76721 98917 73766 29598 59035 69293 29127 7017 46004 16086 62644 74928 57404 32168 45794 19493 71590 1 3 2 6 4 5 5 4 5 5 5 6 5 7 5 8 5 10 7 3
出力例 3
149076
Score : 450 points
Problem Statement
AtCoder cafeteria sells meals consisting of a main dish and a side dish.
There are N types of main dishes, called main dish 1, main dish 2, \dots, main dish N. Main dish i costs a_i yen.
There are M types of side dishes, called side dish 1, side dish 2, \dots, side dish M. Side dish i costs b_i yen.
A set meal is composed by choosing one main dish and one side dish. The price of a set meal is the sum of the prices of the chosen main dish and side dish.
However, for L distinct pairs (c_1, d_1), \dots, (c_L, d_L), the set meal consisting of main dish c_i and side dish d_i is not offered because they do not go well together.
That is, NM - L set meals are offered. (The constraints guarantee that at least one set meal is offered.)
Find the price of the most expensive set meal offered.
Constraints
- 1 \leq N, M \leq 10^5
- 0 \leq L \leq \min(10^5, NM - 1)
- 1 \leq a_i, b_i \leq 10^9
- 1 \leq c_i \leq N
- 1 \leq d_j \leq M
- (c_i, d_i) \neq (c_j, d_j) if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M L a_1 a_2 \dots a_N b_1 b_2 \dots b_M c_1 d_1 c_2 d_2 \vdots c_L d_L
Output
Print the price, in yen, of the most expensive set meal offered.
Sample Input 1
2 3 3 2 1 10 30 20 1 2 2 1 2 3
Sample Output 1
31
They offer three set meals, listed below, along with their prices:
- A set meal consisting of main dish 1 and side dish 1, at a price of 2 + 10 = 12 yen.
- A set meal consisting of main dish 1 and side dish 3, at a price of 2 + 20 = 22 yen.
- A set meal consisting of main dish 2 and side dish 2, at a price of 1 + 30 = 31 yen.
Among them, the most expensive is the third one. Thus, print 31.
Sample Input 2
2 1 0 1000000000 1 1000000000
Sample Output 2
2000000000
Sample Input 3
10 10 10 47718 21994 74148 76721 98917 73766 29598 59035 69293 29127 7017 46004 16086 62644 74928 57404 32168 45794 19493 71590 1 3 2 6 4 5 5 4 5 5 5 6 5 7 5 8 5 10 7 3
Sample Output 3
149076
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
N 個の頂点と M 本の辺からなる単純な無向グラフが与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。 また、i = 1, 2, \ldots, N について、頂点 i には正整数 W_i が割り当てられており、A_i 個のコマが置かれています。
グラフ上にコマが存在する限り、下記の操作を繰り返します。
- まず、グラフ上のコマを 1 個選んで取り除き、そのコマが置かれていた頂点を x とおく。
- x に隣接するいくつかの頂点からなる(空でも良い)集合 S であって、\sum_{y \in S} W_y \lt W_x であるものを選び、S に含まれる頂点それぞれに 1 個ずつコマを置く。
操作を行う回数としてあり得る最大値を出力してください。
なお、どのように操作を行っても、有限回の操作の後にはグラフ上にコマが存在しない状態に至ることが証明出来ます。
制約
- 入力される値はすべて整数
- 2 \leq N \leq 5000
- 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
- 1 \leq W_i \leq 5000
- 0 \leq A_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M W_1 W_2 \ldots W_N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
6 6 1 2 2 3 3 1 3 4 1 5 5 6 9 2 3 1 4 4 1 0 0 0 0 1
出力例 1
5
以下の説明では、各頂点にあるコマの個数を、数列 A = (A_1, A_2, \ldots, A_N) として表します。 はじめ、A = (1, 0, 0, 0, 0, 1) です。
下記の手順で操作を行うことを考えます。
- 頂点 1 にあるコマを 1 個取り除き、頂点 2, 3 にコマを 1 個ずつ置く。その結果、A = (0, 1, 1, 0, 0, 1) となる。
- 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 1) となる。
- 頂点 6 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 0) となる。
- 頂点 3 にあるコマを 1 個取り除き、頂点 2 にコマを 1 個置く。その結果、A = (0, 1, 0, 0, 0, 0) となる。
- 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 0, 0, 0, 0) となる。
上記の手順で操作を行う回数は 5 回であり、これが操作を行う回数としてあり得る最大値です。
入力例 2
2 1 1 2 1 2 0 0
出力例 2
0
この入力例では、はじめからグラフ上にコマが存在しません。
入力例 3
10 20 4 8 1 10 1 7 5 9 9 10 8 10 7 5 1 4 7 3 8 7 2 8 5 8 4 2 5 1 7 2 8 3 3 4 8 9 7 10 2 3 25 5 1 1 16 5 98 3 21 1 35 39 32 11 35 37 14 29 36 1
出力例 3
1380
Score: 475 points
Problem Statement
You are given a simple undirected graph consisting of N vertices and M edges. For i = 1, 2, \ldots, M, the i-th edge connects vertices u_i and v_i. Also, for i = 1, 2, \ldots, N, vertex i is assigned a positive integer W_i, and there are A_i pieces placed on it.
As long as there are pieces on the graph, repeat the following operation:
- First, choose and remove one piece from the graph, and let x be the vertex on which the piece was placed.
- Choose a (possibly empty) set S of vertices adjacent to x such that \sum_{y \in S} W_y \lt W_x, and place one piece on each vertex in S.
Print the maximum number of times the operation can be performed.
It can be proved that, regardless of how the operation is performed, there will be no pieces on the graph after a finite number of iterations.
Constraints
- All input values are integers.
- 2 \leq N \leq 5000
- 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
- 1 \leq W_i \leq 5000
- 0 \leq A_i \leq 10^9
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M W_1 W_2 \ldots W_N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
6 6 1 2 2 3 3 1 3 4 1 5 5 6 9 2 3 1 4 4 1 0 0 0 0 1
Sample Output 1
5
In the following explanation, let A = (A_1, A_2, \ldots, A_N) represent the numbers of pieces on the vertices. Initially, A = (1, 0, 0, 0, 0, 1).
Consider performing the operation as follows:
- Remove one piece from vertex 1 and place one piece each on vertices 2 and 3. Now, A = (0, 1, 1, 0, 0, 1).
- Remove one piece from vertex 2. Now, A = (0, 0, 1, 0, 0, 1).
- Remove one piece from vertex 6. Now, A = (0, 0, 1, 0, 0, 0).
- Remove one piece from vertex 3 and place one piece on vertex 2. Now, A = (0, 1, 0, 0, 0, 0).
- Remove one piece from vertex 2. Now, A = (0, 0, 0, 0, 0, 0).
In this procedure, the operation is performed five times, which is the maximum possible number of times.
Sample Input 2
2 1 1 2 1 2 0 0
Sample Output 2
0
In this sample input, there are no pieces on the graph from the beginning.
Sample Input 3
10 20 4 8 1 10 1 7 5 9 9 10 8 10 7 5 1 4 7 3 8 7 2 8 5 8 4 2 5 1 7 2 8 3 3 4 8 9 7 10 2 3 25 5 1 1 16 5 98 3 21 1 35 39 32 11 35 37 14 29 36 1
Sample Output 3
1380