Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 100 点
問題文
非負整数 x に対し定義される関数 f(x) は以下の条件を満たします。
- f(0) = 1
- 任意の正整数 k に対し f(k) = k \times f(k-1)
このとき、 f(N) を求めてください。
制約
- N は 0 \le N \le 10 を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
2
出力例 1
2
f(2) = 2 \times f(1) = 2 \times 1 \times f(0) = 2 \times 1 \times 1 = 2 です。
入力例 2
3
出力例 2
6
f(3) = 3 \times f(2) = 3 \times 2 = 6 です。
入力例 3
0
出力例 3
1
入力例 4
10
出力例 4
3628800
Score : 100 points
Problem Statement
A function f(x) defined for non-negative integer x satisfies the following conditions:
- f(0) = 1;
- f(k) = k \times f(k-1) for all positive integers k.
Find f(N).
Constraints
- N is an integer such that 0 \le N \le 10.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
2
Sample Output 1
2
We have f(2) = 2 \times f(1) = 2 \times 1 \times f(0) = 2 \times 1 \times 1 = 2.
Sample Input 2
3
Sample Output 2
6
We have f(3) = 3 \times f(2) = 3 \times 2 = 6.
Sample Input 3
0
Sample Output 3
1
Sample Input 4
10
Sample Output 4
3628800
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 200 点
問題文
非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。
- X の 10^i の位以下を四捨五入する。
- 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
- 具体例を挙げる。
- 273 の 10^1 の位以下を四捨五入すれば 300 となる。
- 999 の 10^2 の位以下を四捨五入すれば 1000 となる。
- 100 の 10^9 の位以下を四捨五入すれば 0 となる。
- 1015 の 10^0 の位以下を四捨五入すれば 1020 となる。
制約
- X,K は整数
- 0 \le X < 10^{15}
- 1 \le K \le 15
入力
入力は以下の形式で標準入力から与えられる。
X K
出力
答えを整数として出力せよ。
入力例 1
2048 2
出力例 1
2100
操作の過程で、 X は 2048 \rightarrow 2050 \rightarrow 2100 と変化します。
入力例 2
1 15
出力例 2
0
入力例 3
999 3
出力例 3
1000
入力例 4
314159265358979 12
出力例 4
314000000000000
X は 32bit 整数型に収まらない可能性があります。
Score : 200 points
Problem Statement
Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.
- Round X off to the nearest 10^i.
- Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
- Here are some examples:
- Rounding 273 off to the nearest 10^2 yields 300.
- Rounding 999 off to the nearest 10^3 yields 1000.
- Rounding 100 off to the nearest 10^{10} yields 0.
- Rounding 1015 off to the nearest 10^1 yields 1020.
Constraints
- X and K are integers.
- 0 \le X < 10^{15}
- 1 \le K \le 15
Input
The input is given from Standard Input in the following format:
X K
Output
Print the answer as an integer.
Sample Input 1
2048 2
Sample Output 1
2100
X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.
Sample Input 2
1 15
Sample Output 2
0
Sample Input 3
999 3
Sample Output 3
1000
Sample Input 4
314159265358979 12
Sample Output 4
314000000000000
X may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 300 点
問題文
長さ N の数列 A = (A_1, A_2, \ldots, A_N) が与えられます。 K = 0, 1, 2, \ldots, N-1 のそれぞれについて、下記の問題を解いてください。
1 以上 N 以下の整数 i であって、次の条件を満たすものの個数を求めよ。
- A に含まれる整数のうち A_i より大きいものはちょうど K 種類である。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には K = i-1 の場合の問題の答えを出力せよ。
入力例 1
6 2 7 1 8 2 8
出力例 1
2 1 2 1 0 0
例として、K = 2 の場合の問題の答えを以下で求めます。
- A_1 = 2 に関して、A に含まれる整数のうち A_1 より大きいものは、7, 8 の 2 種類です。
- A_2 = 7 に関して、A に含まれる整数のうち A_2 より大きいものは、8 の 1 種類です。
- A_3 = 1 に関して、A に含まれる整数のうち A_3 より大きいものは、2, 7, 8 の 3 種類です。
- A_4 = 8 に関して、A に含まれる整数のうち A_4 より大きいものは、0 種類です(存在しません)。
- A_5 = 2 に関して、A に含まれる整数のうち A_5 より大きいものは、7, 8 の 2 種類です。
- A_6 = 8 に関して、A に含まれる整数のうち A_6 より大きいものは、0 種類です(存在しません)。
よって、A に含まれる整数のうちA_i より大きいものがちょうど K = 2 種類であるような i は、i = 1 と i = 5 の 2 つです。よって、K = 2 の場合の答えは 2 です。
入力例 2
1 1
出力例 2
1
入力例 3
10 979861204 57882493 979861204 447672230 644706927 710511029 763027379 710511029 447672230 136397527
出力例 3
2 1 2 1 2 1 1 0 0 0
Score : 300 points
Problem Statement
You are given a sequence A = (A_1, A_2, \ldots, A_N) of length N. For each K = 0, 1, 2, \ldots, N-1, solve the following problem.
Find the number of integers i between 1 and N (inclusive) such that:
- A contains exactly K distinct integers greater than A_i.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain the answer for K = i-1.
Sample Input 1
6 2 7 1 8 2 8
Sample Output 1
2 1 2 1 0 0
For example, we will find the answer for K=2.
- Regarding A_1 = 2, A contains 2 distinct integers greater than A_1: 7 and 8.
- Regarding A_2 = 7, A contains 1 distinct integer greater than A_2: 8.
- Regarding A_3 = 1, A contains 3 distinct integers greater than A_3: 2, 7, and 8.
- Regarding A_4 = 8, A contains 0 distinct integers greater than A_4 (there is no such integer).
- Regarding A_5 = 2, A contains 2 distinct integers greater than A_5: 7 and 8.
- Regarding A_6 = 8, A contains 0 distinct integers greater than A_6 (there is no such integer).
Thus, there are two i's, i = 1 and i = 5, such that A contains exactly K = 2 distinct integers greater than A_i. Therefore, the answer for K = 2 is 2.
Sample Input 2
1 1
Sample Output 2
1
Sample Input 3
10 979861204 57882493 979861204 447672230 644706927 710511029 763027379 710511029 447672230 136397527
Sample Output 3
2 1 2 1 2 1 1 0 0 0
Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。上から i 行目、左から j 列目にあるマスをマス (i, j) で表します。
N 個のマス (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) は壁になっています。
はじめ、高橋君はマス (r_\mathrm{s}, c_\mathrm{s}) にいます。
高橋君に Q 個の指示が与えられます。
i = 1, 2, \ldots, Q について、i 番目の指示は文字 d_i と正整数 l_i の組で表されます。d_i は L
、R
、U
、D
のいずれかの文字であり、それぞれ左、右、上、下の方向を表します。
i 番目の指示に対して高橋君は下記の行動を l_i 回繰り返します。
現在いるマスに対して、d_i が表す向きに壁のないマスが隣接しているなら、そのマスに移動する。 そのようなマスが存在しない場合は、何もしない。
i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマスを出力してください。
制約
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- すべての i = 1, 2, \ldots, Nについて、(r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i)
- 1 \leq Q \leq 2 \times 10^5
- d_i は
L
、R
、U
、D
のいずれかの文字 - 1 \leq l_i \leq 10^9
- d_i 以外の入力値は整数
入力
入力は以下の形式で標準入力から与えられる。
H W r_\mathrm{s} c_\mathrm{s} N r_1 c_1 r_2 c_2 \vdots r_N c_N Q d_1 l_1 d_2 l_2 \vdots d_Q l_Q
出力
Q 行出力せよ。 下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までの指示を実行した直後に高橋君がいるマス (R_i, C_i) を i 行目に出力せよ。
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
入力例 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
出力例 1
4 2 3 2 3 1 3 5
与えられるグリッドと高橋君の初期位置は下記の通りです。
ここで、#
は壁のマスを、T
は高橋君がいるマスを表し、.
がその他のマスを表します。
...#. .#... ..... ...T. ..#..
1 つ目の指示に対して高橋君は、左に 2 マス移動し、高橋君の位置は下記の通り、マス (4, 2) になります。
...#. .#... ..... .T... ..#..
2 つ目の指示に対して高橋君は、上に 1 マスに移動した後、次の移動先が壁であるために「何もしない」を 2 回行います。その結果、高橋君の位置は下記の通り、マス (3, 2) になります。
...#. .#... .T... ..... ..#..
3 つ目の指示に対して高橋君は、左に 1 マス移動した後、次の移動先となるマスが存在しないために「何もしない」を 1 回行います。その結果、高橋君の位置は下記の通り、マス (3, 1) になります。
...#. .#... T.... ..... ..#..
4 つ目の指示に対して高橋君は、右に 4 マス移動し、高橋君の位置は下記の通り、マス (3, 5) になります。
...#. .#... ....T ..... ..#..
入力例 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
出力例 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Score : 400 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
N squares, (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N), have walls.
Takahashi is initially at square (r_\mathrm{s}, c_\mathrm{s}).
Q instructions are given to Takahashi.
For i = 1, 2, \ldots, Q, the i-th instruction is represented by a pair of a character d_i and a positive integer l_i. d_i is one of L
, R
, U
, and D
, representing the directions of left, right, up, and down, respectively.
Given the i-th direction, Takahashi repeats the following action l_i times:
If a square without a wall is adjacent to the current square in the direction represented by d_i, move to that square; otherwise, do nothing.
For i = 1, 2, \ldots, Q, print the square where Takahashi will be after he follows the first i instructions.
Constraints
- 2 \leq H, W \leq 10^9
- 1 \leq r_\mathrm{s} \leq H
- 1 \leq c_\mathrm{s} \leq W
- 0 \leq N \leq 2 \times 10^5
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- (r_\mathrm{s}, c_\mathrm{s}) \neq (r_i, c_i) for all i = 1, 2, \ldots, N.
- 1 \leq Q \leq 2 \times 10^5
- d_i is one of the characters
L
,R
,U
, andD
. - 1 \leq l_i \leq 10^9
- All values in the input other than d_i are integers.
Input
The input is given from Standard Input in the following format:
H W r_\mathrm{s} c_\mathrm{s} N r_1 c_1 r_2 c_2 \vdots r_N c_N Q d_1 l_1 d_2 l_2 \vdots d_Q l_Q
Output
Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the square (R_i, C_i) where Takahashi will be after he follows the first i instructions, in the following format:
R_1 C_1 R_2 C_2 \vdots R_Q C_Q
Sample Input 1
5 5 4 4 3 5 3 2 2 1 4 4 L 2 U 3 L 2 R 4
Sample Output 1
4 2 3 2 3 1 3 5
The given grid and the initial position of Takahashi are as follows, where #
denotes a square with a wall, T
a square where Takahashi is, and .
the other squares:
...#. .#... ..... ...T. ..#..
Given the 1-st instruction, Takahashi moves 2 squares to the left, ending up in square (4, 2) as follows:
...#. .#... ..... .T... ..#..
Given the 2-nd instruction, Takahashi first moves 1 square upward, then he "does nothing" twice because the adjacent square in his direction has a wall. As a result, he ends up in square (3, 2) as follows:
...#. .#... .T... ..... ..#..
Given the 3-rd instruction, Takahashi first moves 1 square to the left, then he "does nothing" once because there is no square in his direction. As a result, he ends up in square (3, 1) as follows:
...#. .#... T.... ..... ..#..
Given the 4-th instruction, Takahashi moves 4 squares to the right, ending up in square (3, 5) as follows:
...#. .#... ....T ..... ..#..
Sample Input 2
6 6 6 3 7 3 1 4 3 2 6 3 4 5 5 1 1 3 2 10 D 3 U 3 L 2 D 2 U 3 D 3 U 3 R 3 L 3 D 1
Sample Output 2
6 3 5 3 5 1 6 1 4 1 6 1 4 1 4 2 4 1 5 1
Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
整数列 A とノートがあります。ノートには 10^9 枚のページがあります。
Q 個のクエリが与えられます。各クエリは下記の 4 種類のいずれかです。
ADD x : 整数 x を A の末尾に追加する。
DELETE : A の末尾の要素を削除する。ただし、A が空である場合は何もしない。
SAVE y : ノートの y ページ目に書かれている数列を消し、代わりに現在の A を y ページ目に書き込む。
LOAD z : A をノートの z ページ目に書かれている数列で置き換える。
はじめ、A は空列であり、ノートのすべてのページには空列の情報が書かれています。 その初期状態から、Q 個のクエリを与えられる順に実行し、各クエリの実行直後における A の末尾の要素を出力してください。
なお、入出力の量が多くなる場合があるので、高速な方法で入出力を行うことを推奨します。
制約
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, z は整数
- 与えられるクエリは問題文中の 4 種類のいずれか
入力
入力は以下の形式で標準入力から与えられる。
Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
出力
下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までのクエリを実行した直後の A の末尾の要素 X_i を( A が空の場合は X_i := -1 とする)出力せよ。
X_1 X_2 \ldots X_Q
入力例 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
出力例 1
3 3 4 4 3 -1 -1 4 4 -1 4
はじめ、A は空列、すなわち A = () であり、ノートのすべてのページには空列の情報が書かれています。
- 1 番目のクエリによって、 A の末尾に 3 が追加され、A = (3) となります。
- 2 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3) になります。A は変わらず A = (3) です。
- 3 番目のクエリによって、 A の末尾に 4 が追加され、A = (3, 4) となります。
- 4 番目のクエリによって、ノートの 2 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 5 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3) で置き換えられ、A = (3) となります。
- 6 番目のクエリによって、 A の末尾の要素が削除され、A = () となります。
- 7 番目のクエリでは、A がすでに空であるので何もしません。A は変わらず A = () です。
- 8 番目のクエリによって、 A がノートの 2 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
- 9 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 10 番目のクエリによって、 A がノートの 3 ページ目に書かれた数列 () で置き換えられ、A = () となります。
- 11 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
入力例 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
出力例 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
Score : 500 points
Problem Statement
We have an integer sequence A and a notebook. The notebook has 10^9 pages.
You are given Q queries. Each query is of one of the following four kinds:
ADD x: append an integer x to the tail of A.
DELETE: remove the last term of A if A is not empty; do nothing otherwise.
SAVE y: erase the sequence recorded on the y-th page of the notebook, and record the current A onto the y-th page.
LOAD z: replace A with the sequence recorded on the z-th page of the notebook.
Initially, A is an empty sequence, and an empty sequence is recorded on each page of the notebook. Process Q queries successively in the given order and print the last term of A after processing each query.
The use of fast input and output methods is recommended because of potentially large input and output.
Constraints
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, and z are integers.
- Each of the given queries is of one of the four kinds in the Problem Statement.
Input
The input is given from Standard Input in the following format:
Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Output
For each i = 1, 2, \ldots, Q, let X_i be the last element of A after processing up to the i-th query, or let X_i := -1 if A is empty, and print them in the following format:
X_1 X_2 \ldots X_Q
Sample Input 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
Sample Output 1
3 3 4 4 3 -1 -1 4 4 -1 4
Initially, A is an empty sequence, so A = (), and an empty sequence is recorded on each page of the notebook.
- By the 1-st query, 3 is appended to the tail of A, resulting in A = (3).
- By the 2-nd query, the sequence recorded on the 1-st page of the notebook becomes (3). It remains that A = (3).
- By the 3-rd query, 4 is appended to the tail of A, resulting in A = (3, 4).
- By the 4-th query, the sequence recorded on the 2-nd page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 5-th query, A is replaced by (3), which is recorded on the 1-st page of the notebook, resulting in A = (3).
- By the 6-th query, the last term of A is removed, resulting in A = ().
- By the 7-th query, nothing happens because A is already empty. It remains that A = ().
- By the 8-th query, A is replaced by (3,4), which is recorded on the 2-nd page of the notebook, resulting in A = (3, 4).
- By the 9-th query, the sequence recorded on the 1-st page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 10-th query, A is replaced by (), which is recorded on the 3-rd page of the notebook, resulting in A = ().
- By the 11-th query, A is replaced by (3, 4), which is recorded on the 1-st page of the notebook, resulting in A = (3, 4).
Sample Input 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
Sample Output 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。
また、数直線上に N 枚の壁と N 本のハンマーがあります。
- 座標 Y_1,Y_2,\dots,Y_N にはそれぞれタイプ 1,2,\dots,N の壁があります。
- 最初、高橋君は壁を超えて移動することができません。
- 座標 Z_1,Z_2,\dots,Z_N にはそれぞれタイプ 1,2,\dots,N のハンマーがあります。
- 高橋君はハンマーのある座標に着くとそこにあるハンマーを手に入れます。
- タイプ i のハンマーはタイプ i の壁を破壊するための専用のもので、タイプ i のハンマーを手に入れた後でなら、タイプ i の壁を破壊して通過できるようになります。
高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。
制約
- 入力は全て整数
- 1 \le N \le 1500
- 1 \le |X|,|Y_i|,|Z_i| \le 10^9
- 合計 2 \times N + 1 個の座標 X,Y_i,Z_i は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N X Y_1 Y_2 \dots Y_N Z_1 Z_2 \dots Z_N
出力
高橋君がゴールに到達することが可能であれば移動距離の最小値を整数として出力せよ。
不可能であれば -1
と出力せよ。
入力例 1
3 10 -2 8 -5 5 -10 3
出力例 1
40
以下の手順により、移動距離 40 で高橋くんがゴールに到達でき、これが移動距離の最小です。
- 座標 0 から高橋君が行動を開始する。
- 座標 3 に行く。タイプ 3 のハンマーを手に入れる。
- 座標 5 に行く。タイプ 1 のハンマーを手に入れる。
- 座標 -2 に行く。タイプ 1 の壁を破壊する。
- 座標 -5 に行く。タイプ 3 の壁を破壊する。
- 座標 -10 に行く。タイプ 2 のハンマーを手に入れる。
- 座標 8 に行く。タイプ 2 の壁を破壊する。
- 座標 10 に行く。ここがゴールである。
入力例 2
5 -1 10 -20 30 -40 50 -10 20 -30 40 -50
出力例 2
1
ゴールに移動するために、ハンマーを手に入れる必要も壁を破壊する必要もない場合もあります。
入力例 3
1 100 30 60
出力例 3
-1
高橋君がタイプ 1 のハンマーを手に入れることは不可能であり、ゴールに辿り着くこともできません。
入力例 4
4 865942261 703164879 -531670946 -874856231 -700164975 -941120316 599462305 -649785130 665402307
出力例 4
4078987507
Score : 500 points
Problem Statement
Takahashi is at the origin of a number line. Takahashi wants to reach the goal at coordinate X.
Also, there are N walls and N hammers on the number line.
- At coordinates Y_1,Y_2,\dots,Y_N are walls of types 1,2,\dots,N, respectively.
- Initially, Takahashi cannot get over the walls.
- At coordinates Z_1,Z_2,\dots,Z_N are hammers of types 1,2,\dots,N, respectively.
- When he arrives at a coordinate with a hammer, he obtains the hammer.
- The hammer of type i is dedicated to destroying the wall of type i. After he obtains the hammer of type i, he can destroy the wall of type i and get over it.
Determine if he can reach the goal. If he can, find the minimum distance he travels.
Constraints
- All values in the input are integers.
- 1 \le N \le 1500
- 1 \le |X|,|Y_i|,|Z_i| \le 10^9
- The (2 \times N + 1) coordinates X,Y_i and Z_i are distinct.
Input
The input is given from Standard Input in the following format:
N X Y_1 Y_2 \dots Y_N Z_1 Z_2 \dots Z_N
Output
If Takahashi can reach the goal, print the minimum possible distance he travels as an integer.
Otherwise, print -1
.
Sample Input 1
3 10 -2 8 -5 5 -10 3
Sample Output 1
40
Takahashi can reach the goal by traveling a distance of 40 as follows, which is the minimum possible:
- He starts at coordinate 0.
- He moves to coordinate 3 to obtain the hammer of type 3.
- He moves to coordinate 5 to obtain the hammer of type 1.
- He moves to coordinate -2 to destroy the wall of type 1.
- He moves to coordinate -5 to destroy the wall of type 3.
- He moves to coordinate -10 to obtain the hammer of type 2.
- He moves to coordinate 8 to destroy the wall of type 2.
- He moves to coordinate 10, which is the goal.
Sample Input 2
5 -1 10 -20 30 -40 50 -10 20 -30 40 -50
Sample Output 2
1
It may not be required that he obtains a hammer or destroys a wall to reach the goal.
Sample Input 3
1 100 30 60
Sample Output 3
-1
Takahashi cannot obtain the hammer of type 1, and neither can he reach the goal.
Sample Input 4
4 865942261 703164879 -531670946 -874856231 -700164975 -941120316 599462305 -649785130 665402307
Sample Output 4
4078987507
Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 600 点
問題文
非負整数を要素とする N 次正方行列であって、下記の 2 つの条件をともに満たすものの個数を 998244353 で割ったあまりを出力してください。
- すべての i = 1, 2, \ldots, N について、i 行目の要素の和は R_i である。
- すべての i = 1, 2, \ldots, N について、i 列目の要素の和は C_i である。
入力で与えられる R_i および C_i は 0 以上 2 以下の整数であることに注意してください(制約参照)。
制約
- 1 \leq N \leq 5000
- 0 \leq R_i \leq 2
- 0 \leq C_i \leq 2
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N R_1 R_2 \ldots R_N C_1 C_2 \ldots C_N
出力
答えを出力せよ。
入力例 1
3 1 1 1 0 1 2
出力例 1
3
条件を満たす行列は下記の 3 つです。
0 1 0 0 0 1 0 0 1
0 0 1 0 1 0 0 0 1
0 0 1 0 0 1 0 1 0
入力例 2
3 1 1 1 2 2 2
出力例 2
0
入力例 3
18 2 0 1 2 0 1 1 2 1 1 2 0 1 2 2 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 2 1 1 0 2 2
出力例 3
968235177
998244353 で割ったあまりを出力することに注意してください。
Score : 600 points
Problem Statement
Find the number, modulo 998244353, of square matrices of size N whose elements are non-negative integers, that satisfy both of the following two conditions:
- for all i = 1, 2, \ldots, N, the sum of the elements in the i-th row is R_i;
- for all i = 1, 2, \ldots, N, the sum of the elements in the i-th column is C_i.
Note that R_i and C_i given in the input are integers between 0 and 2 (see Constraints).
Constraints
- 1 \leq N \leq 5000
- 0 \leq R_i \leq 2
- 0 \leq C_i \leq 2
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N R_1 R_2 \ldots R_N C_1 C_2 \ldots C_N
Output
Print the answer.
Sample Input 1
3 1 1 1 0 1 2
Sample Output 1
3
The following 3 matrices satisfy the conditions:
0 1 0 0 0 1 0 0 1
0 0 1 0 1 0 0 0 1
0 0 1 0 0 1 0 1 0
Sample Input 2
3 1 1 1 2 2 2
Sample Output 2
0
Sample Input 3
18 2 0 1 2 0 1 1 2 1 1 2 0 1 2 2 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 2 1 1 0 2 2
Sample Output 3
968235177
Be sure to print the count modulo 998244353.
Time Limit: 6 sec / Memory Limit: 1024 MB
配点 : 600 点
問題文
整数組からなる列 A があります。はじめ A = ( (0, 1), (1, 0) ) です。
あなたは A に対して次の操作を 0 回以上何度でも行うことができます。
- 隣り合う 2 つの整数組 (a, b), (c, d) を自由に選び、その間に (a + c, b + d) を挿入する。
整数組の列 T について、 f(T) を以下のように定義します。
- f(T) = ( T の要素がすべて A に含まれる状態になるまでに必要な最小の操作回数 ) とする。
- 「 T の要素がすべて A に含まれる状態」とは、全ての T に含まれる要素 x について、 x が ( A に含まれる要素の集合 ) に含まれることを指す。
- ただし、そのような操作が存在しない場合 f(T) = 0 とする。
N 個の整数組からなる列 S = ((a_1, b_1), (a_2, b_2), \dots, (a_N, b_N)) があります。また、 S の要素は相異なります。
S の連続部分列 S_{l,r}=((a_l,b_l),(a_{l+1},b_{l+1}),\dots,(a_r,b_r)) として考えられるものは \frac{N \times (N+1)}{2} 通りありますが、それらに対する f(S_{l,r}) の総和を 998244353 で割った余りを求めてください。
より正確には、 \displaystyle \sum^{N} _ {l=1} \sum^{N} _ {r=l} f(S_{l,r}) を 998244353 で割った余りを求めてください。
制約
- 1 \le N \le 10^5
- 0 \le a_i,b_i \le 10^9
- i \neq j ならば、 a_i \neq a_j または b_i \neq b_j
入力
入力は以下の形式で標準入力から与えられる。
N a_1 b_1 a_2 b_2 \dots a_N b_N
出力
答えを整数として出力せよ。
入力例 1
7 1 2 3 7 3 5 0 0 1000000000 1 0 1 6 3
出力例 1
3511324
- f(S_{1,1})=2 です。
- ((0,1),(1,0)) \rightarrow ((0,1),(1,1),(1,0)) \rightarrow ((0,1),(1,2),(1,1),(1,0)) と操作をすればよいです。
- f(S_{1,2})=5 です。
- f(S_{1,3})=7 です。
- f(S_{2,2})=5 です。
- f(S_{2,3})=7 です。
- f(S_{3,3})=4 です。
- f(S_{5,5})=1000000000 = 10^9 です。
- f(S_{5,6})=1000000000 = 10^9 です。
- f(S_{6,6})=0 です。
- (0,1) は元から A に含まれています。
- 上述されていない S_{l,r} について、 f(S_{l,r})=0 です。
- A にどのように操作を行っても、 (0,0) や (6,3) が含まれる状態にはできないことが証明できます。
以上より、 f(S_{l,r}) の総和は 2000000030 = 2 \times 10^9 + 30 であり、それを 998244353 で割った余りは 3511324 です。
Score : 600 points
Problem Statement
We have a sequence A consisting of integer pairs. Initially, A = ( (0, 1), (1, 0) ).
You may perform the following operation on A as many (possibly zero) times as you want:
- choose adjacent two integer pairs (a, b) and (c, d), and insert (a + c, b + d) between them.
For a sequence T consisting of integer pairs, let us define f(T) as follows:
- let f(T) = (the minimum number of operations required to make every element of T contained in A).
- We say that "every element of T is contained in A" if, for all elements x contained in T, x is contained in (the set consisting of the elements contained in A).
- Here, if there are no such operations, let f(T) = 0.
There is a sequence S = ((a_1, b_1), (a_2, b_2), \dots, (a_N, b_N)) consisting of N integer pairs. Here, all elements of S are pairwise distinct.
There are \frac{N \times (N+1)}{2} possible consecutive subarrays S_{l,r}=((a_l,b_l),(a_{l+1},b_{l+1}),\dots,(a_r,b_r)) of S. Find the sum, modulo 998244353, of f(S_{l,r}) over those subarrays.
Formally, find \displaystyle \sum^{N} _ {l=1} \sum^{N} _ {r=l} f(S_{l,r}), modulo 998244353.
Constraints
- 1 \le N \le 10^5
- 0 \le a_i,b_i \le 10^9
- a_i \neq a_j or b_i \neq b_j, if i \neq j.
Input
The input is given from Standard Input in the following format:
N a_1 b_1 a_2 b_2 \dots a_N b_N
Output
Print the answer as an integer.
Sample Input 1
7 1 2 3 7 3 5 0 0 1000000000 1 0 1 6 3
Sample Output 1
3511324
- f(S_{1,1})=2.
- We can make ((0,1),(1,0)) \rightarrow ((0,1),(1,1),(1,0)) \rightarrow ((0,1),(1,2),(1,1),(1,0)).
- f(S_{1,2})=5.
- f(S_{1,3})=7.
- f(S_{2,2})=5.
- f(S_{2,3})=7.
- f(S_{3,3})=4.
- f(S_{5,5})=1000000000 = 10^9.
- f(S_{5,6})=1000000000 = 10^9.
- f(S_{6,6})=0.
- (0, 1) is originally contained in A.
- f(S_{l,r})=0 for all S_{l,r} not mentioned above.
- We can prove that A can never contain (0,0) or (6,3) regardless of operations.
Therefore, the sum of f(S_{l,r}) is 2000000030 = 2 \times 10^9 + 30, whose remainder when divided by 998244353 is 3511324.