実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
非負整数 n が次の条件を満たすとき、n を 良い整数 と呼びます。
- n を 10 進法で表したときに、偶数の数字 (0, 2, 4, 6, 8) のみが登場する。
例えば 0、68 および 2024 は良い整数です。
整数 N が与えられます。良い整数のうち小さい方から N 番目の整数を求めてください。
制約
- 1 \leq N \leq 10^{12}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
小さい方から N 番目の良い整数を出力せよ。
入力例 1
8
出力例 1
24
良い整数を小さい方から順に並べると 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots となります。
小さい方から 8 番目の良い整数は 24 なので、これを出力します。
入力例 2
133
出力例 2
2024
入力例 3
31415926535
出力例 3
2006628868244228
Score: 300 points
Problem Statement
A non-negative integer n is called a good integer when it satisfies the following condition:
- All digits in the decimal notation of n are even numbers (0, 2, 4, 6, and 8).
For example, 0, 68, and 2024 are good integers.
You are given an integer N. Find the N-th smallest good integer.
Constraints
- 1 \leq N \leq 10^{12}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the N-th smallest good integer.
Sample Input 1
8
Sample Output 1
24
The good integers in ascending order are 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots.
The eighth smallest is 24, which should be printed.
Sample Input 2
133
Sample Output 2
2024
Sample Input 3
31415926535
Sample Output 3
2006628868244228
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。
以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。
1 x: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。2 x: S の x 番目の文字を出力する。
制約
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S は英小文字からなる。
2 xの形式のクエリが 1 個以上与えられる。- N,Q,x はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
それぞれのクエリは以下の形式で与えられる。ここで、t は 1 または 2 である。
t x
出力
2 x の形式の各クエリについて、答えを一行に出力せよ。
入力例 1
3 3 abc 2 2 1 1 2 2
出力例 1
b a
1 個目のクエリのとき、S は abc なので 2 文字目の b を出力します。
2 個目のクエリのとき、S は abc から cab に変わります。
3 個目のクエリのとき、S は cab なので 2 文字目の a を出力します。
入力例 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
出力例 2
c u c u
Score : 300 points
Problem Statement
You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.
Process Q queries. Each query is of one of the following two types.
1 x: Perform the following x times in a row: delete the last character of S and append it to the beginning.2 x: Print the x-th character of S.
Constraints
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S consists of lowercase English letters.
- At least one query in the format
2 x. - N, Q, x are all integers.
Input
Input is given from Standard Input in the following format:
N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in the following format, where t is 1 or 2:
t x
Output
For each query in the format 2 x, print the answer in a single line.
Sample Input 1
3 3 abc 2 2 1 1 2 2
Sample Output 1
b a
In the 1-st query, S is abc, so the 2-nd character b should be printed.
In the 2-nd query, S is changed from abc to cab.
In the 3-rd query, S is cab, so the 2-nd character a should be printed.
Sample Input 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
Sample Output 2
c u c u
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
(1,2,\dots,N) を並び替えて得られる数列 P=(P_1,P_2,\dots,P_N) が与えられます。
長さ K の正整数列 (i_1,i_2,\dots,i_K) であって、以下の条件を共に満たすものを良い添字列と呼びます。
- 1\leq i_1 < i_2 < \dots < i_K \leq N
- (P_{i_1},P_{i_2},\dots,P_{i_K}) はある連続する K 個の整数を並び替えることで得られる。
厳密には、ある整数 a が存在して、\lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace。
全ての良い添字列における i_K-i_1 の最小値を求めてください。 なお、本問題の制約下では良い添字列が必ず 1 つ以上存在することが示せます。
制約
- 1\leq K \leq N \leq 2\times 10^5
- 1\leq P_i\leq N
- i\neq j ならば P_i\neq P_j
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K P_1 P_2 \dots P_N
出力
全ての良い添字列における i_K-i_1 の最小値を出力せよ。
入力例 1
4 2 2 3 1 4
出力例 1
1
良い添字列は (1,2),(1,3),(2,4) の 3 つです。 例えば (i_1,i_2)=(1,3) は、 1\leq i_1 < i_2 \leq N かつ (P_{i_1},P_{i_2})=(2,1) が連続する 2 つの整数 1,2 の並び替えなので良い添字列です。
これらの良い添字列のうち i_K-i_1 の値が最小となるのは (1,2) で、そのときの値は 2-1=1 です。
入力例 2
4 1 2 3 1 4
出力例 2
0
どの良い添字列においても i_K-i_1=i_1-i_1=0 です。
入力例 3
10 5 10 1 6 8 7 2 5 9 3 4
出力例 3
5
Score: 425 points
Problem Statement
You are given a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N).
A length-K sequence of indices (i_1, i_2, \dots, i_K) is called a good index sequence if it satisfies both of the following conditions:
- 1 \leq i_1 < i_2 < \dots < i_K \leq N.
- The subsequence (P_{i_1}, P_{i_2}, \dots, P_{i_K}) can be obtained by rearranging some consecutive K integers.
Formally, there exists an integer a such that \lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace.
Find the minimum value of i_K - i_1 among all good index sequences. It can be shown that at least one good index sequence exists under the constraints of this problem.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq N
- P_i \neq P_j if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K P_1 P_2 \dots P_N
Output
Print the minimum value of i_K - i_1 among all good index sequences.
Sample Input 1
4 2 2 3 1 4
Sample Output 1
1
The good index sequences are (1,2),(1,3),(2,4). For example, (i_1, i_2) = (1,3) is a good index sequence because 1 \leq i_1 < i_2 \leq N and (P_{i_1}, P_{i_2}) = (2,1) is a rearrangement of two consecutive integers 1, 2.
Among these good index sequences, the smallest value of i_K - i_1 is for (1,2), which is 2-1=1.
Sample Input 2
4 1 2 3 1 4
Sample Output 2
0
i_K - i_1 = i_1 - i_1 = 0 in all good index sequences.
Sample Input 3
10 5 10 1 6 8 7 2 5 9 3 4
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
正整数 X, Y に対し、2 次元平面上において以下の条件を満たす長方形を良い長方形と呼びます。
- 全ての辺は x 軸または y 軸に平行である。
- 全ての頂点に対し、その x 座標は 0 以上 X 以下の整数であり、その y 座標は 0 以上 Y 以下の整数である。
面積がそれぞれ A 以上、B 以上、C 以上であるような 3 つの良い長方形を重ならないように配置することができるか判定してください。
ただし、3 つの長方形が重ならないとは、どの 2 つの長方形についても、それらの共通部分の面積が 0 であることを指します。
制約
- 1 \leq X, Y \leq 10^9
- 1 \leq A, B, C \leq 10^{18}
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
X Y A B C
出力
問題文で与えられた条件を満たすように長方形を配置することができるならば Yes、できないならば No と出力せよ。
入力例 1
3 3 2 2 3
出力例 1
Yes
下の図のように配置すればよいです。長方形内の数値は面積を表します。
2 \geq A, 3 \geq B, 3 \geq C であるので、問題文で与えられた条件を満たします。

入力例 2
3 3 4 4 1
出力例 2
No
条件を満たすように配置することはできません。
入力例 3
1000000000 1000000000 1000000000000000000 1000000000000000000 1000000000000000000
出力例 3
No
Score : 500 points
Problem Statement
For positive integers X and Y, a rectangle in a two-dimensional plane that satisfies the conditions below is said to be good.
- Every edge is parallel to the x- or y-axis.
- For every vertex, its x-coordinate is an integer between 0 and X (inclusive), and y-coordinate is an integer between 0 and Y (inclusive).
Determine whether it is possible to place the following three good rectangles without overlapping: a good rectangle of an area at least A, another of an area at least B, and another of an area at least C.
Here, three rectangles are considered to be non-overlapping when the intersection of any two of them has an area of 0.
Constraints
- 1 \leq X, Y \leq 10^9
- 1 \leq A, B, C \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
X Y A B C
Output
If it is possible to place three rectangles under the conditions specified in the Problem Statement, print Yes; otherwise, print No.
Sample Input 1
3 3 2 2 3
Sample Output 1
Yes
The figure below shows a possible placement, where the number in a rectangle represents its area.
We can see that 2 \geq A, 3 \geq B, 3 \geq C, satisfying the conditions.

Sample Input 2
3 3 4 4 1
Sample Output 2
No
There is no possible placement under the conditions.
Sample Input 3
1000000000 1000000000 1000000000000000000 1000000000000000000 1000000000000000000
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点の木が与えられます。頂点には 1, \dots, N の番号が付けられており、i \, (1 \leq i \leq N - 1) 番目の辺は頂点 A_i, B_i を結びます。
この木における頂点 u, v の距離を、頂点 u から頂点 v までの最短パス上にある辺の本数と定義します。
Q 個のクエリが与えられます。i \, (1 \leq i \leq Q) 番目のクエリでは、整数 U_i, K_i が与えられるので、頂点 U_i からの距離がちょうど K_i であるような頂点の番号を任意に一つ出力してください。そのような頂点が存在しない場合は、-1 を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq N - 1)
- 与えられるグラフは木
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq U_i, K_i \leq N \, (1 \leq i \leq Q)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
Q
U_1 K_1
\vdots
U_Q K_Q
出力
Q 行出力せよ。i \, (1 \leq i \leq Q) 行目には、頂点 U_i からの距離がちょうど K_i である頂点が存在するならその一例を、存在しないなら -1 を出力せよ。そのような頂点が複数存在する場合、どれを出力しても正解となる。
入力例 1
5 1 2 2 3 3 4 3 5 3 2 2 5 3 3 3
出力例 1
4 1 -1
- 頂点 2 からの距離がちょうど 2 であるのは頂点 4, 5 の二つです。
- 頂点 5 からの距離がちょうど 3 であるのは頂点 1 のみです。
- 頂点 3 からの距離がちょうど 3 である頂点は存在しません。
入力例 2
10 1 2 2 3 3 5 2 8 3 4 4 6 4 9 5 7 9 10 5 1 1 2 2 3 3 4 4 5 5
出力例 2
2 4 10 -1 -1
Score : 500 points
Problem Statement
You are given a tree with N vertices. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq N - 1) edge connects Vertices A_i and B_i.
We define the distance between Vertices u and v on this tree by the number of edges in the shortest path from Vertex u to Vertex v.
You are given Q queries. In the i-th (1 \leq i \leq Q) query, given integers U_i and K_i, print the index of any vertex whose distance from Vertex U_i is exactly K_i. If there is no such vertex, print -1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq N - 1)
- The given graph is a tree.
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq U_i, K_i \leq N \, (1 \leq i \leq Q)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
Q
U_1 K_1
\vdots
U_Q K_Q
Output
Print Q lines. The i-th (1 \leq i \leq Q) line should contain the index of any vertex whose distance from Vertex U_i is exactly K_i if such a vertex exists; if not, it should contain -1. If there are multiple such vertices, you may print any of them.
Sample Input 1
5 1 2 2 3 3 4 3 5 3 2 2 5 3 3 3
Sample Output 1
4 1 -1
- Two vertices, Vertices 4 and 5, have a distance exactly 2 from Vertex 2.
- Only Vertex 1 has a distance exactly 3 from Vertex 5.
- No vertex has a distance exactly 3 from Vertex 3.
Sample Input 2
10 1 2 2 3 3 5 2 8 3 4 4 6 4 9 5 7 9 10 5 1 1 2 2 3 3 4 4 5 5
Sample Output 2
2 4 10 -1 -1