Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
100 以上の整数 N が与えられます。N の下 2 桁を出力してください。
ただし、N の下 2 桁とは十の位と一の位をこの順に並べたものを言います。
制約
- 100 \le N \le 999
- N は整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
254
出力例 1
54
254 の下 2 桁は 54 であるため、54 を出力します。
入力例 2
101
出力例 2
01
101 の下 2 桁は 01 であるため、01 を出力します。
Score : 100 points
Problem Statement
You are given an integer N at least 100. Print the last two digits of N.
Strictly speaking, print the tens and ones digits of N in this order.
Constraints
- 100 \le N \le 999
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
254
Sample Output 1
54
The last two digits of 254 are 54, which should be printed.
Sample Input 2
101
Sample Output 2
01
The last two digits of 101 are 01, which should be printed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
体力が A の敵がいます。あなたは、1 回攻撃をすることで敵の体力を B 減らすことが出来ます。
敵の体力を 0 以下にするためには、最小で何回攻撃をする必要があるでしょうか?
制約
- 1 \le A,B \le 10^{18}
- A, B は整数である。
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
答えを出力せよ。
入力例 1
7 3
出力例 1
3
3 回攻撃すると敵の体力が -2 となります。
2 回攻撃しただけでは敵の体力は 1 であるため、3 回攻撃する必要があります。
入力例 2
123456789123456789 987654321
出力例 2
124999999
入力例 3
999999999999999998 2
出力例 3
499999999999999999
Score : 100 points
Problem Statement
There is an enemy with stamina A. Every time you attack the enemy, its stamina reduces by B.
At least how many times do you need to attack the enemy to make its stamina 0 or less?
Constraints
- 1 \le A,B \le 10^{18}
- A and B are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print the answer.
Sample Input 1
7 3
Sample Output 1
3
Attacking three times make the enemy's stamina -2.
Attacking only twice makes the stamina 1, so you need to attack it three times.
Sample Input 2
123456789123456789 987654321
Sample Output 2
124999999
Sample Input 3
999999999999999998 2
Sample Output 3
499999999999999999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
あるプログラミングコンテストの予選に N 人が参加し、参加者全員が異なる順位を得ました。
長さ N の文字列 S が与えられ、この文字列は決勝への参加希望の有無を表現します。具体的には下記の通りです。
- S の i 文字目が
o
なら、予選 i 位の参加者が決勝への参加を希望した。 - S の i 文字目が
x
なら、予選 i 位の参加者が決勝への参加を希望しなかった。
決勝への参加を希望した参加者のうち順位の小さい方から K 人が予選を通過します。
以下の条件を満たす長さ N の文字列 T を出力してください。
- 予選 i 位の参加者が予選を通過する場合、 T の i 文字目は
o
- 予選 i 位の参加者が予選を通過しない場合、 T の i 文字目は
x
制約
- N,K は整数
- 1 \le K \le N \le 100
- S は
o
とx
からなる長さ N の文字列 - S には少なくとも K 個の
o
が含まれる
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを出力せよ。
入力例 1
10 3 oxxoxooxox
出力例 1
oxxoxoxxxx
この入力の場合、予選の参加者は N=10 人であり、予選を通過する人数は K=3 人です。
- 予選 1 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 1 人です。
- 予選 2,3 位の参加者は決勝への参加を希望していないため、予選を通過しません。
- 予選 4 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 2 人です。
- 予選 5 位の参加者は決勝への参加を希望していないため、予選を通過しません。
- 予選 6 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 3 人です。
- ここで、予選を通過した人数が 3 人となりました。なので、予選 7 位以下の参加者は予選を通過しません。
Score : 200 points
Problem Statement
There were N contestants in the qualification round of a programming contest. All contestants got distinct ranks.
You are given a length-N string S, which represents whether the contestants want to participate in the final round or not. Specifically,
- if the i-th character of S is
o
, the contestant ranked i-th in the qualification wants to participate in the final; - if the i-th character of S is
x
, the contestant ranked i-th in the qualification does not want to participate in the final.
Among those who want to participate in the final, K contestants with the smallest ranks advance to the final.
Print a string T of length N that satisfies the following conditions:
- if the contestant ranked i-th in the qualification advances to the final, the i-th character of T is
o
; - if the contestant ranked i-th in the qualification does not advance to the final, the i-th character of T is
x
.
Constraints
- N and K are integers.
- 1 \le K \le N \le 100
- S is a string of length N consisting of
o
andx
. - S has at least K
o
's.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer.
Sample Input 1
10 3 oxxoxooxox
Sample Output 1
oxxoxoxxxx
In this input, N=10 people took part in the qualification round, and K=3 of them advance to the final.
- The participant who ranked 1-st in the qualification wants to participate in the final, so the participant advances to the final. 1 participant has advanced so far.
- The participants who ranked 2-nd and 3-rd in the qualification do not want to participate in the final, so the participants do not advance to the final.
- The participant who ranked 4-th in the qualification wants to participate in the final, so the participant advances to the final. 2 participants have advanced so far.
- The participants who ranked 5-th in the qualification does not want to participate in the final, so the participant does not advance to the final.
- The participant who ranked 6-th in the qualification wants to participate in the final, so the participant advances to the final. 3 participants have advanced so far.
- Now that 3 people have advanced to the final, no participants ranked 7-th or lower advance to the final.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は英小文字からなる文字列 S を持っています。
高橋君は文字列 S に対して、下記の操作をちょうど 1 回行います。
- まず、非負整数 K を選ぶ。
- その後、S の各文字を K 個後ろの英小文字に変更する。
ただし、
a
の 1 個後ろの英小文字はb
であり、b
の 1 個後ろの英小文字はc
であり、c
の 1 個後ろの英小文字はd
であり、- \cdots
y
の 1 個後ろの英小文字はz
であり、z
の 1 個後ろの英小文字はa
です。
例えば、b
の 4 個後ろの英小文字は f
であり、y
の 3 個後ろの英小文字は b
です。
文字列 T が与えられます。 高橋君が上記の操作によって S を T に一致させることができるかを判定してください。
制約
- S と T はそれぞれ英小文字からなる長さ 1 以上 10^5 以下の文字列
- S の長さと T の長さは等しい
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
高橋君が S を T に一致させることができる場合は Yes
と出力し、
できない場合は No
と出力せよ。
入力例 1
abc ijk
出力例 1
Yes
高橋君が K=8 を選ぶと、
a
は 8 個後ろのi
にb
は 8 個後ろのj
にc
は 8 個後ろのk
に
それぞれ変更され、S と T が一致します。
高橋君が S を T に一致させることができるため Yes
と出力します。
入力例 2
z a
出力例 2
Yes
高橋君が K=1 を選ぶと S と T が一致します。
z
の 1 個後ろの英小文字は a
であることに注意してください。
入力例 3
ppq qqp
出力例 3
No
高橋君は非負整数 K をどのように選んでも S を T に一致させることができません。
よって、No
と出力します。
入力例 4
atcoder atcoder
出力例 4
Yes
高橋君が K=0 を選ぶと S と T が一致します。
Score : 200 points
Problem Statement
Takahashi has a string S consisting of lowercase English letters.
On this string, he will do the operation below just once.
- First, choose a non-negative integer K.
- Then, shift each character of S to the right by K (see below).
Here,
a
shifted to the right by 1 isb
;b
shifted to the right by 1 isc
;c
shifted to the right by 1 isd
;- \cdots
y
shifted to the right by 1 isz
;z
shifted to the right by 1 isa
.
For example, b
shifted to the right by 4 is f
, and y
shifted to the right by 3 is b
.
You are given a string T. Determine whether Takahashi can make S equal T by the operation above.
Constraints
- Each of S and T is a string of length between 1 and 10^5 (inclusive) consisting of lowercase English letters.
- The lengths of S and T are equal.
Input
Input is given from Standard Input in the following format:
S T
Output
If Takahashi can make S equal T, print Yes
; if not, print No
.
Sample Input 1
abc ijk
Sample Output 1
Yes
When Takahashi chooses K=8,
a
is shifted to the right by 8 and becomesi
,b
is shifted to the right by 8 and becomesj
,c
is shifted to the right by 8 and becomesk
,
and now S and T are equal.
Therefore, he can make S equal T, so Yes
should be printed.
Sample Input 2
z a
Sample Output 2
Yes
Choosing K=1 makes S and T equal.
Note that the letter on the right of z
is a
.
Sample Input 3
ppq qqp
Sample Output 3
No
There is no non-negative integer K that he can choose to make S equal T, so No
should be printed.
Sample Input 4
atcoder atcoder
Sample Output 4
Yes
Choosing K=0 makes S and T equal.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。 S の各文字は色 1 、色 2 、\ldots 、色 M の M 色のうちのいずれかで塗られており、 i = 1, 2, \ldots, N について、S の i 文字目は色 C_i で塗られています。
各 i = 1, 2, \ldots, M について、この順番に下記の操作を行います。
- S の色 i で塗られた文字からなる部分を、右に 1 つ巡回シフトする。 すなわち、S の 色 i で塗られた文字の位置が先頭のものから順に p_1, p_2, p_3, \ldots, p_k 文字目であるとき、 S の p_1, p_2, p_3, \ldots, p_k 文字目を、それぞれ、S の p_k, p_1,p_2, \ldots, p_{k-1} 文字目で同時に置き換える。
上記の操作をすべて行った後の、最終的な S を出力してください。
なお、M 色あるどの色についても、その色で塗られた S の文字が必ず 1 つ以上存在することが、制約として保証されます。
制約
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq M
- N, M, C_i はすべて整数
- S は英小文字からなる長さ N の文字列
- 任意の整数 1 \leq i \leq M に対して、ある整数 1 \leq j \leq N が存在して C_j = i が成り立つ
入力
入力は以下の形式で標準入力から与えられる。
N M S C_1 C_2 \ldots C_N
出力
答えを出力せよ。
入力例 1
8 3 apzbqrcs 1 2 3 1 2 2 1 2
出力例 1
cszapqbr
はじめ、S = apzbqrcs
です。
- i = 1 に対する操作では、S の 1, 4, 7 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S =
cpzaqrbs
となります。 - i = 2 に対する操作では、S の 2, 5, 6, 8 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S =
cszapqbr
となります。 - i = 3 に対する操作では、S の 3 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S =
cszapqbr
となります(操作の前後で S は変わりません)。
よって、最終的な S である cszapqbr
を出力します。
入力例 2
2 1 aa 1 1
出力例 2
aa
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters. Each character of S is painted in one of the M colors: color 1, color 2, ..., color M; for each i = 1, 2, \ldots, N, the i-th character of S is painted in color C_i.
For each i = 1, 2, \ldots, M in this order, let us perform the following operation.
- Perform a right circular shift by 1 on the part of S painted in color i. That is, if the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters are painted in color i from left to right, then simultaneously replace the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters of S with the p_k-th, p_1-th, p_2-th, \ldots, p_{k-1}-th characters of S, respectively.
Print the final S after the above operations.
The constraints guarantee that at least one character of S is painted in each of the M colors.
Constraints
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq M
- N, M, and C_i are all integers.
- S is a string of length N consisting of lowercase English letters.
- For each integer 1 \leq i \leq M, there is an integer 1 \leq j \leq N such that C_j = i.
Input
The input is given from Standard Input in the following format:
N M S C_1 C_2 \ldots C_N
Output
Print the answer.
Sample Input 1
8 3 apzbqrcs 1 2 3 1 2 2 1 2
Sample Output 1
cszapqbr
Initially, S = apzbqrcs
.
- For i = 1, perform a right circular shift by 1 on the part of S formed by the 1-st, 4-th, 7-th characters, resulting in S =
cpzaqrbs
. - For i = 2, perform a right circular shift by 1 on the part of S formed by the 2-nd, 5-th, 6-th, 8-th characters, resulting in S =
cszapqbr
. - For i = 3, perform a right circular shift by 1 on the part of S formed by the 3-rd character, resulting in S =
cszapqbr
(here, S is not changed).
Thus, you should print cszapqbr
, the final S.
Sample Input 2
2 1 aa 1 1
Sample Output 2
aa
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正の整数 N が与えられます。
A\leq B\leq C かつ ABC\leq N であるような正の整数の組 (A,B,C) の個数を求めてください。
なお、制約の条件下で答えは 2^{63} 未満であることが保証されます。
制約
- 1 \leq N \leq 10^{11}
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
4
出力例 1
5
条件を満たす組は (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2) の 5 つです。
入力例 2
100
出力例 2
323
入力例 3
100000000000
出力例 3
5745290566750
Score : 300 points
Problem Statement
You are given a positive integer N.
Find the number of triples of positive integers (A, B, C) such that A\leq B\leq C and ABC\leq N.
The Constraints guarantee that the answer is less than 2^{63}.
Constraints
- 1 \leq N \leq 10^{11}
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
4
Sample Output 1
5
There are five such triples: (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2).
Sample Input 2
100
Sample Output 2
323
Sample Input 3
100000000000
Sample Output 3
5745290566750
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
20230322
は並べ替えると 02320232
となり、これは 0232
を 2 度繰り返しています。
このように、数字のみからなる文字列であって、適切に文字を並び替える (そのままでもよい) ことによって同じ列を 2 度繰り返すようにできるものを 嬉しい列 と呼びます。
数字のみからなる文字列 S が与えられるので、以下の条件を全て満たす整数の組 (l,r) はいくつあるか求めてください。
- 1 \le l \le r \le |S| ( |S| は S の長さ)
- S の l 文字目から r 文字目までの (連続する) 部分文字列は嬉しい列である。
制約
- S は数字のみからなる長さ 1 以上 5 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
20230322
出力例 1
4
S= 20230322
です。
条件を満たす整数組 (l,r) は (1,6),(1,8),(2,7),(7,8) の 4 つです。
入力例 2
0112223333444445555556666666777777778888888889999999999
出力例 2
185
S の先頭が 0
である場合もあります。
入力例 3
3141592653589793238462643383279502884197169399375105820974944
出力例 3
9
Score : 400 points
Problem Statement
The string 20230322
can be rearranged into 02320232
, which is a repetition of 0232
twice.
Similarly, a string consisting of digits is said to be happy when it can be rearranged into (or already is) a repetition of some string twice.
You are given a string S consisting of digits. Find the number of pairs of integers (l,r) satisfying all of the following conditions.
- 1 \le l \le r \le |S|. (|S| is the length of S.)
- The (contiguous) substring formed of the l-th through r-th characters of S is happy.
Constraints
- S is a string consisting of digits whose length is between 1 and 5 \times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print an integer representing the answer.
Sample Input 1
20230322
Sample Output 1
4
We have S= 20230322
.
Here are the four pairs of integers (l,r) that satisfy the condition: (1,6), (1,8), (2,7), and (7,8).
Sample Input 2
0112223333444445555556666666777777778888888889999999999
Sample Output 2
185
S may begin with 0
.
Sample Input 3
3141592653589793238462643383279502884197169399375105820974944
Sample Output 3
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
xy -平面上のいくつかの点に村があります。
高橋君はこれらの村を民兵や魔女などの外敵から守るため、これらのすべての村を囲むようなお堀を建設します。
0 と 1 からなる 4 \times 4 行列 A = (A_{i, j}) が与えられます。
A_{i, j} = 1 を満たす整数の組 (i, j) (1 \leq i, j \leq 4) ごとに、座標 (i-0.5, j-0.5) に村があります。
お堀は平面上の多角形です。 高橋君は以下の条件をすべて満たすお堀を建設します(入力例1・出力例1の説明も参考にして下さい)。
- 自己交差がない
- 内部にすべての村を含む
- すべての頂点の x 座標と y 座標は 0 以上 4 以下の整数
- すべての辺は x 軸と y 軸のどちらかに平行
- それぞれの内角の大きさは 90 度または 270 度
高橋君が建設するお堀として考えられるものが何通りあるかを出力して下さい。
制約
- A_{i, j} \in \lbrace 0, 1\rbrace
- A_{i, j} = 1 となる (i, j) が少なくとも 1 つ存在する
入力
入力は以下の形式で標準入力から与えられる。
A_{1, 1} A_{1, 2} A_{1, 3} A_{1, 4} A_{2, 1} A_{2, 2} A_{2, 3} A_{2, 4} A_{3, 1} A_{3, 2} A_{3, 3} A_{3, 4} A_{4, 1} A_{4, 2} A_{4, 3} A_{4, 4}
出力
高橋君が建設するお堀として考えられるものが何通りあるかを出力せよ。
入力例 1
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0
出力例 1
1272
下記の 2 つの画像の例は、高橋君が建設するお堀の条件を満たします。
下記の 4 つの画像の例は、高橋君が建設するお堀の条件を満たしません。
上記の 4 つの例が高橋君の建設するお堀の条件を満たさない理由は、以下の通りです。
- 1 つ目の画像の例は、「自己交差がない」という条件を満たしません。
- 2 つ目の画像の例は、「内部にすべての村を含む」という条件を満たしません。
- 3 つ目の画像の例は、「すべての頂点の x 座標と y 座標は 0 以上 4 以下の整数」という条件を満たしません。(座標が整数でない頂点があります。)
- 4 つ目の画像の例は、「すべての辺は x 軸と y 軸のどちらかに平行」という条件を満たしません。
入力例 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 2
1
Score : 500 points
Problem Statement
There are villages at some number of points in the xy-plane.
Takahashi will construct a moat to protect these villages from enemies such as civil armies and witches.
You are given a 4 \times 4 matrix A = (A_{i, j}) consisting of 0 and 1.
For each pair of integers (i, j) (1 \leq i, j \leq 4) such that A_{i, j} = 1, there is a village at the coordinates (i-0.5, j-0.5).
The moat will be a polygon in the plane. Takahashi will construct it so that the following conditions will be satisfied. (See also the annotation at Sample Input/Output 1.)
- There is no self-intersection.
- All villages are contained in the interior of the polygon.
- The x- and y-coordinates of every vertex are integers between 0 and 4 (inclusive).
- Every edge is parallel to the x- or y-axis.
- Every inner angle is 90 or 270 degrees.
Print the number of ways in which Takahashi can construct the moat.
Constraints
- A_{i, j} \in \lbrace 0, 1\rbrace
- There is at least one pair (i, j) such that A_{i, j} = 1.
Input
Input is given from Standard Input in the following format:
A_{1, 1} A_{1, 2} A_{1, 3} A_{1, 4} A_{2, 1} A_{2, 2} A_{2, 3} A_{2, 4} A_{3, 1} A_{3, 2} A_{3, 3} A_{3, 4} A_{4, 1} A_{4, 2} A_{4, 3} A_{4, 4}
Output
Print the number of ways in which Takahashi can construct the moat.
Sample Input 1
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0
Sample Output 1
1272
The two ways to construct the moat shown in the images below are valid.
The four ways to construct the moat shown in the images below are invalid.
Here are the reasons the above ways are invalid.
- The first way violates the condition: "There is no self-intersection."
- The second way violates the condition: "All villages are contained in the interior of the polygon."
- The third way violates the condition: "The x- and y-coordinates of every vertex are integers between 0 and 4." (Some vertices have non-integer coordinates.)
- The fourth way violates the condition: "Every edge is parallel to the x- or y-axis."
Sample Input 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 2
1
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
xy 平面上にある AtCoder 王国の道路は、全ての整数 n に対する直線 x=n および直線 y=n からなります。 そのうち、全ての整数 n に対する直線 x=Bn および直線 y=Bn は大通りです。
高橋君は (x,y) にいるときに、(x,y-1),(x,y+1),(x+1,y),(x-1,y) のいずれかに移動することができます。 また、1 回の移動につき、大通りに沿って移動する場合は 1 秒、それ以外の場合は K 秒かかります。
(S_x,S_y) にいる高橋君が (G_x,G_y) に移動するのに最短で何秒かかるかを求めてください。
この問題は T ケース与えられます。
制約
- 1 \le T \le 2 \times 10^5
- 1 \le B,K \le 10^9
- 0 \le S_x,S_y,G_x,G_y \le 10^9
- 入力はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
T \mathrm{testcase}_1 \mathrm{testcase}_2 \vdots \mathrm{testcase}_T
それぞれのテストケースは以下の形式で与えられる。
B K S_x S_y G_x G_y
出力
T 行出力せよ。i 行目には i 個目のテストケースの解を出力せよ。
入力例 1
4 3 4 2 2 4 4 5 6 2 3 2 3 1 1000000000 0 0 1000000000 1000000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000
出力例 1
10 0 2000000000 500000000500000000
1 個目のテストケースについて、(2,2) から (2,3) に 4 秒かけて移動し、(2,3) から (4,3) に 2 秒かけて移動し、(4,3) から (4,4) に 4 秒かけて移動することで 10 秒で (2,2) から (4,4) に移動することができます。10 秒より早く移動することはできないため、解は 10 です。
2 個目のテストケースについて、初めから (G_x,G_y) にいるため解は 0 です。
入力例 2
10 928184439 674654465 203937094 186855052 851783856 805293696 55480262 448852233 823161539 786348805 550018803 322680316 891870741 235679524 32164572 497841190 620600021 96487871 321502816 428964257 499656016 521484999 717623189 824784374 144040837 680268887 76238777 371138006 350230937 78690135 768922620 799628518 403830696 60449731 218880692 88319939 482031503 121412614 472330444 284479575 949635609 427232765 389524418 132987043 656496997 678732442 23028233 488463974 857778764 629964237 714551548 739330018 579247790 874251485 461612428 535402609 555160129 833592114 44418273 287363785
出力例 2
177606591118701316 6205925075792263 30320747646118343 84136273267803188 83764071874751489 118960470930399064 2929499649126153 16403238161749820 84995699148879437 71771264361119335
Score : 500 points
Problem Statement
The roads in the Kingdom of AtCoder, which lies on the xy-plane, are the lines x=n and y=n for all integers n. Among them, the lines x=Bn and y=Bn for all integers n are main roads.
When Takahashi is at (x,y), he can move to (x,y-1), (x,y+1), (x+1,y), or (x-1,y). Each move takes 1 second along a main road and K seconds otherwise.
Find the minimum number of seconds Takahashi needs to get from (S_x, S_y) to (G_x, G_y).
You will have T test cases to solve.
Constraints
- 1 \le T \le 2 \times 10^5
- 1 \le B,K \le 10^9
- 0 \le S_x,S_y,G_x,G_y \le 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
T \mathrm{testcase}_1 \mathrm{testcase}_2 \vdots \mathrm{testcase}_T
Each test case is in the following format:
B K S_x S_y G_x G_y
Output
Print T lines. The i-th line should contain the answer to the i-th test case.
Sample Input 1
4 3 4 2 2 4 4 5 6 2 3 2 3 1 1000000000 0 0 1000000000 1000000000 1000000000 1000000000 500000000 500000000 1000000000 1000000000
Sample Output 1
10 0 2000000000 500000000500000000
For the 1-st test case, he can go from (2,2) to (2,3) in 4 seconds, from (2,3) to (4,3) in 2 seconds, and from (4,3) to (4,4) in 4 seconds to get from (2,2) to (4,4) in 10 seconds. It is impossible to get there in less than 10 seconds, so the answer is 10.
For the 2-nd test case, he is already at (G_x, G_y) in the beginning, so the answer is 0.
Sample Input 2
10 928184439 674654465 203937094 186855052 851783856 805293696 55480262 448852233 823161539 786348805 550018803 322680316 891870741 235679524 32164572 497841190 620600021 96487871 321502816 428964257 499656016 521484999 717623189 824784374 144040837 680268887 76238777 371138006 350230937 78690135 768922620 799628518 403830696 60449731 218880692 88319939 482031503 121412614 472330444 284479575 949635609 427232765 389524418 132987043 656496997 678732442 23028233 488463974 857778764 629964237 714551548 739330018 579247790 874251485 461612428 535402609 555160129 833592114 44418273 287363785
Sample Output 2
177606591118701316 6205925075792263 30320747646118343 84136273267803188 83764071874751489 118960470930399064 2929499649126153 16403238161749820 84995699148879437 71771264361119335