Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
正整数 N,K 及び長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A に含まれる要素のうち、K の倍数であるもののみを全て取り出し、それらを K で割って出力してください。
制約
- 1\leq N,K\leq 100
- 1\leq A_1 < A_2 < \ldots < A_N \leq 100
- A には K の倍数が 1 個以上含まれる
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
A に含まれる要素のうち、K の倍数であるもの全てを K で割った値を、空白区切りで昇順に出力せよ。
入力例 1
5 2 2 5 6 7 10
出力例 1
1 3 5
A に含まれる要素のうち、2 の倍数は 2,6,10 です。それらを 2 で割って得られる 1,3,5 を空白区切りで昇順に出力してください。
入力例 2
3 1 3 4 7
出力例 2
3 4 7
入力例 3
5 10 50 51 54 60 65
出力例 3
5 6
Score: 100 points
Problem Statement
You are given positive integers N and K, and a sequence of length N, A=(A_1,A_2,\ldots,A_N).
Extract all elements of A that are multiples of K, divide them by K, and print the quotients.
Constraints
- 1\leq N,K\leq 100
- 1\leq A_1 < A_2 < \ldots < A_N \leq 100
- A has at least one multiple of K.
- All given numbers are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Divide all elements of A that are multiples of K and print the quotients in ascending order with spaces in between.
Sample Input 1
5 2 2 5 6 7 10
Sample Output 1
1 3 5
The multiples of 2 among the elements in A are 2, 6, and 10. Divide them by 2 to get 1, 3, and 5, and print them in ascending order with spaces in between.
Sample Input 2
3 1 3 4 7
Sample Output 2
3 4 7
Sample Input 3
5 10 50 51 54 60 65
Sample Output 3
5 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英大文字からなる長さ 3 の文字列 S が与えられます。S が ACE
、BDF
、CEG
、DFA
、EGB
、FAC
、GBD
のいずれかと等しいとき Yes
を、そうでないとき No
を出力してください。
制約
- S は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が ACE
、BDF
、CEG
、DFA
、EGB
、FAC
、GBD
のいずれかと等しいとき Yes
を、そうでないとき No
を出力せよ。
入力例 1
ABC
出力例 1
No
S = ABC
のとき、S は ACE
、BDF
、CEG
、DFA
、EGB
、FAC
、GBD
のいずれとも等しくないので No
を出力します。
入力例 2
FAC
出力例 2
Yes
入力例 3
XYX
出力例 3
No
Score : 100 points
Problem Statement
Given a length-3 string S consisting of uppercase English letters, print Yes
if S equals one of ACE
, BDF
, CEG
, DFA
, EGB
, FAC
, and GBD
; print No
otherwise.
Constraints
- S is a length-3 string consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes
if S equals one of ACE
, BDF
, CEG
, DFA
, EGB
, FAC
, and GBD
; print No
otherwise.
Sample Input 1
ABC
Sample Output 1
No
When S = ABC
, S does not equal any of ACE
, BDF
, CEG
, DFA
, EGB
, FAC
, and GBD
, so No
should be printed.
Sample Input 2
FAC
Sample Output 2
Yes
Sample Input 3
XYX
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字および英大文字のみからなる文字列 S, T が与えられます。
文字列 S が以下の条件を満たしているか判定してください。
- S の先頭でない英大文字の直前の文字はすべて T に含まれる。より形式的には、2 \leq i \leq |S| なる整数 i について S の i 番目の文字が英大文字ならば、S の i-1 番目の文字は T に含まれる。
制約
- S, T は長さ 1 以上 100 以下の英小文字および英大文字のみからなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S が問題文中の条件を満たしているとき Yes
と出力せよ。そうでないとき、No
と出力せよ。
入力例 1
AtCoder Total
出力例 1
Yes
S の先頭でない英大文字は 3 番目の文字の C
のみです。この直前の文字である t
は T に含まれているため、Yes
と出力すればよいです。
入力例 2
aBCdE abcdcba
出力例 2
No
S の 3 番目の文字は英大文字 C
であり、その直前の文字は B
ですが、B
は T に含まれていません。
入力例 3
abcde XYZ
出力例 3
Yes
Score : 200 points
Problem Statement
You are given strings S and T consisting of lowercase and uppercase English letters.
Determine whether the string S satisfies the following condition:
- Every uppercase letter in S that is not at the beginning is immediately preceded by a character contained in T. More formally, for all integers i such that 2 \leq i \leq |S|, if the i-th character of S is uppercase, then the (i-1)-th character of S is contained in T.
Constraints
- Each of S and T is a string consisting of lowercase and uppercase English letters with length between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S T
Output
If S satisfies the condition in the problem statement, output Yes
. Otherwise, output No
.
Sample Input 1
AtCoder Total
Sample Output 1
Yes
The only uppercase letter in S that is not at the beginning is the 3rd character C
. The immediately preceding character t
is contained in T, so output Yes
.
Sample Input 2
aBCdE abcdcba
Sample Output 2
No
The 3rd character of S is the uppercase letter C
, and its immediately preceding character is B
, but B
is not contained in T.
Sample Input 3
abcde XYZ
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 8 マス、横 8 マスの 64 マスからなるマス目があります。 上から i 行目 (1\leq i\leq8) 、左から j 列目 (1\leq j\leq8) のマスをマス (i,j) と呼ぶことにします。
それぞれのマスは、空マスであるかコマが置かれているかのどちらかです。
マスの状態は長さ 8 の文字列からなる長さ 8 の列 (S _ 1,S _ 2,S _ 3,\ldots,S _ 8) で表されます。
マス (i,j) (1\leq i\leq8,1\leq j\leq8) は、S _ i の j 文字目が .
のとき空マスで、#
のときコマが置かれています。
あなたは、すでに置かれているどのコマにも取られないように、いずれかの空マスに自分のコマを置きたいです。
マス (i,j) に置かれているコマは、次のどちらかの条件を満たすコマを取ることができます。
- i 行目のマスに置かれている
- j 列目のマスに置かれている
たとえば、マス (4,4) に置かれているコマは、以下の図で青く示されたマスに置かれているコマを取ることができます。
あなたがコマを置くことができるマスがいくつあるか求めてください。
制約
- S _ i は
.
,#
からなる長さ 8 の文字列 (1\leq i\leq 8)
入力
入力は以下の形式で標準入力から与えられる。
S _ 1 S _ 2 S _ 3 S _ 4 S _ 5 S _ 6 S _ 7 S _ 8
出力
すでに置かれているコマに取られずに自分のコマを置くことができる空マスの個数を出力せよ。
入力例 1
...#.... #....... .......# ....#... .#...... ........ ........ ..#.....
出力例 1
4
すでに置かれているコマは、以下の図で青く示されたマスに置かれたコマを取ることができます。
よって、あなたがすでに置かれているコマに取られないように自分のコマを置くことができるマスはマス (6,6), マス (6,7), マス (7,6), マス (7,7) の 4 マスです。
入力例 2
........ ........ ........ ........ ........ ........ ........ ........
出力例 2
64
コマがひとつも置かれていないこともあります。
入力例 3
.#...... ..#..#.. ....#... ........ ..#....# ........ ...#.... ....#...
出力例 3
4
Score : 200 points
Problem Statement
There is a grid of 64 squares with 8 rows and 8 columns. Let (i,j) denote the square at the i-th row from the top (1\leq i\leq8) and j-th column from the left (1\leq j\leq8).
Each square is either empty or has a piece placed on it.
The state of the squares is represented by a sequence (S_1,S_2,S_3,\ldots,S_8) of 8 strings of length 8.
Square (i,j) (1\leq i\leq8,1\leq j\leq8) is empty if the j-th character of S_i is .
, and has a piece if it is #
.
You want to place your piece on an empty square in such a way that it cannot be captured by any of the existing pieces.
A piece placed on square (i,j) can capture pieces that satisfy either of the following conditions:
- Placed on a square in row i
- Placed on a square in column j
For example, a piece placed on square (4,4) can capture pieces placed on the squares shown in blue in the following figure:
How many squares can you place your piece on?
Constraints
- Each S_i is a string of length 8 consisting of
.
and#
(1\leq i\leq 8).
Input
The input is given from Standard Input in the following format:
S_1 S_2 S_3 S_4 S_5 S_6 S_7 S_8
Output
Print the number of empty squares where you can place your piece without it being captured by any existing pieces.
Sample Input 1
...#.... #....... .......# ....#... .#...... ........ ........ ..#.....
Sample Output 1
4
The existing pieces can capture pieces placed on the squares shown in blue in the following figure:
Therefore, you can place your piece without it being captured on 4 squares: square (6,6), square (6,7), square (7,6), and square (7,7).
Sample Input 2
........ ........ ........ ........ ........ ........ ........ ........
Sample Output 2
64
There may be no pieces on the grid.
Sample Input 3
.#...... ..#..#.. ....#... ........ ..#....# ........ ...#.... ....#...
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 次元座標平面があります。x 軸正方向を右向き、y 軸正方向を上向きとします。
この平面上に自己交差のない四角形があります。
4 つの頂点の座標は反時計回りに (A_x,A_y),(B_x,B_y),(C_x,C_y),(D_x,D_y) です。
この四角形が凸であるか判定してください。
なお、四角形の 4 つの内角が全て 180 度未満であるとき、かつ、その時に限り、その四角形は凸であるといいます。
制約
- -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
- 入力に含まれる値は全て整数である
- 与えられる 4 点は四角形の 4 頂点を反時計回りに並べたものである
- 与えられる 4 点のなす四角形は自己交差がなく退化していない。すなわち
- どの 2 頂点も同じ座標にない
- どの 3 頂点も同一直線上にない
- 隣接しない 2 辺は共有点を持たない
入力
入力は以下の形式で標準入力から与えられる。
A_x A_y B_x B_y C_x C_y D_x D_y
出力
与えられる四角形が凸なら Yes
、凸でないなら No
を出力せよ。
入力例 1
0 0 1 0 1 1 0 1
出力例 1
Yes
与えられた四角形は正方形であり、4 つの内角は全て 90 度です。したがって、この四角形は凸です。
入力例 2
0 0 1 1 -1 0 1 -1
出力例 2
No
角 A が 270 度です。したがって、この四角形は凸ではありません。
Score : 300 points
Problem Statement
Consider a two-dimensional coordinate plane, where the x-axis is oriented to the right, and the y-axis is oriented upward.
In this plane, there is a quadrilateral without self-intersection.
The coordinates of the four vertices are (A_x,A_y), (B_x,B_y), (C_x,C_y), and (D_x,D_y), in counter-clockwise order.
Determine whether this quadrilateral is convex.
Here, a quadrilateral is convex if and only if all four interior angles are less than 180 degrees.
Constraints
- -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
- All values in input are integers.
- The given four points are the four vertices of a quadrilateral in counter-clockwise order.
- The quadrilateral formed by the given four points has no self-intersection and is non-degenerate. That is,
- no two vertices are at the same coordinates;
- no three vertices are colinear; and
- no two edges that are not adjacent have a common point.
Input
Input is given from Standard Input in the following format:
A_x A_y B_x B_y C_x C_y D_x D_y
Output
If the given quadrilateral is convex, print Yes
; otherwise, print No
.
Sample Input 1
0 0 1 0 1 1 0 1
Sample Output 1
Yes
The given quadrilateral is a square, whose four interior angles are all 90 degrees. Thus, this quadrilateral is convex.
Sample Input 2
0 0 1 1 -1 0 1 -1
Sample Output 2
No
The angle A is 270 degrees. Thus, this quadrilateral is not convex.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英大文字からなる長さ 3 の文字列 T が、英小文字からなる文字列 S の 空港コード であるとは、 T が S から次のいずれかの方法により得られることとします。
- S の長さ 3 の(連続とは限らない)部分列をとり、それを英大文字に変換したものを T とする
- S の長さ 2 の(連続とは限らない)部分列をとり、それを英大文字に変換したものの末尾に
X
を追加したものを T とする
文字列 S, T が与えられるので、 T が S の空港コードであるか判定してください。
制約
- S は英小文字からなる長さ 3 以上 10^5 以下の文字列
- T は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
T が S の空港コードであるならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
narita NRT
出力例 1
Yes
narita
の部分列 nrt
を英大文字に変換した文字列 NRT
は、 narita
の空港コードです。
入力例 2
losangeles LAX
出力例 2
Yes
losangeles
の部分列 la
を英大文字に変換した文字列 LA
の末尾に X
を追加したもの LAX
は、 losangeles
の空港コードです。
入力例 3
snuke RNG
出力例 3
No
Score: 300 points
Problem Statement
A string T of length 3 consisting of uppercase English letters is an airport code for a string S of lowercase English letters if and only if T can be derived from S by one of the following methods:
- Take a subsequence of length 3 from S (not necessarily contiguous) and convert it to uppercase letters to form T.
- Take a subsequence of length 2 from S (not necessarily contiguous), convert it to uppercase letters, and append
X
to the end to form T.
Given strings S and T, determine if T is an airport code for S.
Constraints
- S is a string of lowercase English letters with a length between 3 and 10^5, inclusive.
- T is a string of uppercase English letters with a length of 3.
Input
The input is given from Standard Input in the following format:
S T
Output
Print Yes
if T is an airport code for S, and No
otherwise.
Sample Input 1
narita NRT
Sample Output 1
Yes
The subsequence nrt
of narita
, when converted to uppercase, forms the string NRT
, which is an airport code for narita
.
Sample Input 2
losangeles LAX
Sample Output 2
Yes
The subsequence la
of losangeles
, when converted to uppercase and appended with X
, forms the string LAX
, which is an airport code for losangeles
.
Sample Input 3
snuke RNG
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
数直線上に N 個の村があります。i 番目の村は座標 X_i にあり、P_i 人の村人がいます。
Q 個のクエリに答えてください。i 番目のクエリは以下の形式です。
- 整数 L_i,R_i が与えられる。座標が L_i 以上 R_i 以下の村に住んでいる村人の人数の総数を求めよ。
制約
- 1\leq N,Q\leq 2\times 10^5
- -10^9\leq X_1 < X_2 < \ldots < X_N \leq 10^9
- 1\leq P_i\leq 10^9
- -10^9\leq L_i \leq R_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 \ldots X_N P_1 \ldots P_N Q L_1 R_1 \vdots L_Q R_Q
出力
Q 行出力せよ。
i\ (1\leq i \leq Q) 行目には、i 番目のクエリに対する答えを出力せよ。
入力例 1
4 1 3 5 7 1 2 3 4 4 1 1 2 6 0 10 2 2
出力例 1
1 5 10 0
1 番目のクエリについて考えます。座標が 1 以上 1 以下の村は、座標 1 にある村で、村人は 1 人います。よって答えは 1 です。
2 番目のクエリについて考えます。座標が 2 以上 6 以下の村は、座標 3 にある村と座標 5 にある村で、村人はそれぞれ 2 人と 3 人います。よって答えは 2+3=5 です。
入力例 2
7 -10 -5 -3 -1 0 1 4 2 5 6 5 2 1 7 8 -7 7 -1 5 -10 -4 -8 10 -5 0 -10 5 -8 7 -8 -3
出力例 2
26 15 7 26 18 28 26 11
Score : 350 points
Problem Statement
There are N villages on a number line. The i-th village is located at coordinate X_i, and has P_i villagers.
Answer Q queries. The i-th query is in the following format:
- Given integers L_i and R_i, find the total number of villagers living in villages located between coordinates L_i and R_i, inclusive.
Constraints
- 1\leq N,Q\leq 2\times 10^5
- -10^9\leq X_1 < X_2 < \ldots < X_N \leq 10^9
- 1\leq P_i\leq 10^9
- -10^9\leq L_i \leq R_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 \ldots X_N P_1 \ldots P_N Q L_1 R_1 \vdots L_Q R_Q
Output
Print Q lines.
The i-th line(1\leq i \leq Q) should contain the answer to the i-th query.
Sample Input 1
4 1 3 5 7 1 2 3 4 4 1 1 2 6 0 10 2 2
Sample Output 1
1 5 10 0
Consider the first query. The villages between coordinates 1 and 1 are the village at coordinate 1, with 1 villager. Hence, the answer is 1.
Consider the second query. The villages between coordinates 2 and 6 are the villages at coordinates 3 and 5, with 2 and 3 villagers, respectively. Hence, the answer is 2+3=5.
Sample Input 2
7 -10 -5 -3 -1 0 1 4 2 5 6 5 2 1 7 8 -7 7 -1 5 -10 -4 -8 10 -5 0 -10 5 -8 7 -8 -3
Sample Output 2
26 15 7 26 18 28 26 11
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
長さ 2N の非負整数列 A = (A_1, \dots, A_{2N}) が与えられます。
長さ 2N の括弧列 s のスコアを、以下で得られる値として定義します。
- s の i 文字目が
)
であるようなすべての整数 i について A_i の値を 0 に変えた場合の、A の要素の総和。
長さ 2N の正しい括弧列のスコアとしてありうる最大値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
正しい括弧列とは
正しい括弧列とは、()
である部分文字列を削除することを 0 回以上繰り返して空文字列にできる文字列を指します。
制約
- 1 \leq T \leq 500
- 1 \leq N \leq 2 \times 10^5
- 各入力ファイルについて、すべてのテストケースの N の総和は 2 \times 10^5 以下である。
- 0 \leq A_i \leq 10^9 (1 \leq i \leq 2N)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
T \textrm{case}_1 \textrm{case}_2 \vdots \textrm{case}_T
\textrm{case}_i は i 番目のテストケースを表す。各テストケースは以下の形式で与えられる。
N A_1 A_2 \vdots A_{2N}
出力
T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
2 3 400 500 200 100 300 600 6 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 1
1200 6000000000
1 番目のテストケースにおいて、正しい括弧列として (())()
を選ぶと、そのスコアは 400+500+0+0+300+0=1200 となります。
1200 よりも高いスコアを得るような正しい括弧列は存在しないので、1 番目のテストケースの答えとして 1200 を出力します。
この入出力例における 2 番目のテストケースのように、答えが 32 bit 整数におさまらないことがあることに注意してください。
Score : 450 points
Problem Statement
You are given a sequence of non-negative integers A = (A_1,\dots,A_{2N}) of length 2N.
Define the score of a parenthesis sequence s of length 2N as follows:
- For every position i where the i-th character of s is
)
, set A_i to 0, then take the sum of all elements of A.
Find the maximum possible score of a correct parenthesis sequence of length 2N.
You are given T test cases; solve each.
What is a correct parenthesis sequence?
A correct parenthesis sequence is a string that can be reduced to the empty string by repeatedly removing substrings equal to()
.
Constraints
- 1 \le T \le 500
- 1 \le N \le 2 \times 10^{5}
- For each input file, the sum of N over all test cases is at most 2 \times 10^{5}.
- 0 \le A_i \le 10^{9} (1 \le i \le 2N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T \textrm{case}_1 \textrm{case}_2 \vdots \textrm{case}_T
\textrm{case}_i represents the i-th test case. Each test case is given in the following format:
N A_1 A_2 \vdots A_{2N}
Output
Output T lines. The i-th line (1 \le i \le T) should contain the answer for the i-th test case.
Sample Input 1
2 3 400 500 200 100 300 600 6 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 1
1200 6000000000
In the first test case, choosing the correct parenthesis string (())()
gives a score of 400+500+0+0+300+0=1200.
No correct parenthesis string yields a higher score, so the answer is 1200.
Note that, as in the second test case of this sample, the answer may exceed the 32-bit integer range.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
座標平面上にタイルが敷き詰められています。 1\times1 の大きさの小タイルと K\times K の大きさの大タイルの 2 種類があり、次の規則に従って敷き詰められています。
- 整数の組 (i,j) に対し、正方形 \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace は 1 つの小タイルもしくは 1 つの大タイルに含まれる。
- \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor が偶数のとき、小タイルに含まれる。
- そうでないとき、大タイルに含まれる。
ただし、タイルは境界を含むものとし、共通部分が正の面積をもつような 2 つの異なるタイルは存在しないとします。
例えば、K=3 のとき、タイルは以下のようになります。
高橋君は、はじめ座標平面上の点 (S _ x+0.5,S _ y+0.5) にいます。
高橋君は、次の移動を好きなだけ繰り返します。
- 上下左右の方向と正の整数 n を選ぶ。その方向に n だけ進む。
高橋君が異なるタイルを通るたび、高橋君は通行料を 1 だけ支払います。
高橋君が点 (T _ x+0.5,T _ y+0.5) にたどり着くために支払わなければならない通行料の最小値を求めてください。
制約
- 1\leq K\leq10 ^ {16}
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
K S _ x S _ y T _ x T _ y
出力
高橋君が支払わなければならない通行料の最小値を出力せよ。
入力例 1
3 7 2 1 6
出力例 1
5
例えば、以下のように移動することで支払う通行料を 5 にすることができます。
- 上に 3 進む。通行料を 1 支払う。
- 左に 2 進む。通行料を 1 支払う。
- 上に 1 進む。通行料を 1 支払う。
- 左に 4 進む。通行料を 2 支払う。
支払う通行料を 4 以下にすることはできないので、5
を出力してください。
入力例 2
1 41 42 13 56
出力例 2
42
高橋君が最短距離で移動するとき、どのように移動しても通行料を 42 だけ支払います。
支払う通行料を 41 以下にすることはできないので、42
を出力してください。
入力例 3
100 100 99 199 1
出力例 3
0
通行料を支払わなくてよい場合もあります。
入力例 4
96929423 5105216413055191 10822465733465225 1543712011036057 14412421458305526
出力例 4
79154049
Score: 550 points
Problem Statement
Tiles are laid out on a coordinate plane. There are two types of tiles: small tiles of size 1\times1 and large tiles of size K\times K, laid out according to the following rules:
- For each pair of integers (i,j), the square \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained within either one small tile or one large tile.
- If \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor is even, it is contained within a small tile.
- Otherwise, it is contained within a large tile.
Tiles include their boundaries, and no two different tiles have a positive area of intersection.
For example, when K=3, tiles are laid out as follows:
Takahashi starts at the point (S_x+0.5,S_y+0.5) on the coordinate plane.
He can repeat the following movement any number of times:
- Choose a direction (up, down, left, or right) and a positive integer n. Move n units in that direction.
Each time he crosses from one tile to another, he must pay a toll of 1.
Determine the minimum toll Takahashi must pay to reach the point (T_x+0.5,T_y+0.5).
Constraints
- 1\leq K\leq10^{16}
- 0\leq S_x\leq2\times10^{16}
- 0\leq S_y\leq2\times10^{16}
- 0\leq T_x\leq2\times10^{16}
- 0\leq T_y\leq2\times10^{16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
K S_x S_y T_x T_y
Output
Print the minimum toll Takahashi must pay.
Sample Input 1
3 7 2 1 6
Sample Output 1
5
For example, he can move as follows, paying a toll of 5.
- Move up by 3. Pay a toll of 1.
- Move left by 2. Pay a toll of 1.
- Move up by 1. Pay a toll of 1.
- Move left by 4. Pay a toll of 2.
The toll paid cannot be 4 or less, so print 5
.
Sample Input 2
1 41 42 13 56
Sample Output 2
42
When he moves the shortest distance, he will always pay a toll of 42.
The toll paid cannot be 41 or less, so print 42
.
Sample Input 3
100 100 99 199 1
Sample Output 3
0
There are cases where no toll needs to be paid.
Sample Input 4
96929423 5105216413055191 10822465733465225 1543712011036057 14412421458305526
Sample Output 4
79154049