実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1 以上 10 以下の整数 X が与えられます。
HelloWorld という文字列から X 文字目だけを削除した文字列を出力してください。
制約
- X は 1 以上 10 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを出力せよ。
入力例 1
5
出力例 1
HellWorld
HelloWorld の 5 文字目を削除すると HellWorld になります。したがって、HellWorld を出力してください。
入力例 2
9
出力例 2
HelloWord
入力例 3
1
出力例 3
elloWorld
Score : 100 points
Problem Statement
You are given an integer X between 1 and 10, inclusive.
Output the string obtained by deleting only the X-th character from the string HelloWorld.
Constraints
- X is an integer between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
X
Output
Output the answer.
Sample Input 1
5
Sample Output 1
HellWorld
Deleting the 5-th character of HelloWorld gives HellWorld. Thus, output HellWorld.
Sample Input 2
9
Sample Output 2
HelloWord
Sample Input 3
1
Sample Output 3
elloWorld
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字のみからなる N 個の文字列 S_1,S_2,\ldots,S_N が与えられます。
N 個の数字 C_1,C_2,\ldots,C_N を以下のように定義します。
- S_i の先頭の文字が
a,b,cのいずれかならば C_i=2 - S_i の先頭の文字が
d,e,fのいずれかならば C_i=3 - S_i の先頭の文字が
g,h,iのいずれかならば C_i=4 - S_i の先頭の文字が
j,k,lのいずれかならば C_i=5 - S_i の先頭の文字が
m,n,oのいずれかならば C_i=6 - S_i の先頭の文字が
p,q,r,sのいずれかならば C_i=7 - S_i の先頭の文字が
t,u,vのいずれかならば C_i=8 - S_i の先頭の文字が
w,x,y,zのいずれかならば C_i=9
C_1,C_2,\ldots,C_N をこの順に連結した文字列を出力してください。
制約
- 1\leq N\leq 10
- N は整数
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \ldots S_N
出力
C_1,C_2,\ldots,C_N をこの順に連結した文字列を出力せよ。
入力例 1
2 algorithm heuristic
出力例 1
24
- S_1=
algorithmの先頭の文字はaであるため、C_1=2となります。 - S_2=
heuristicの先頭の文字はhであるため、C_2=4となります。
よって、これらを連結した 24 を出力します。
入力例 2
3 i love you
出力例 2
459
Score : 200 points
Problem Statement
You are given N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters.
Define N digits C_1, C_2, \ldots, C_N as follows:
- If the first character of S_i is one of
a,b,c, then C_i=2 - If the first character of S_i is one of
d,e,f, then C_i=3 - If the first character of S_i is one of
g,h,i, then C_i=4 - If the first character of S_i is one of
j,k,l, then C_i=5 - If the first character of S_i is one of
m,n,o, then C_i=6 - If the first character of S_i is one of
p,q,r,s, then C_i=7 - If the first character of S_i is one of
t,u,v, then C_i=8 - If the first character of S_i is one of
w,x,y,z, then C_i=9
Output the string obtained by concatenating C_1, C_2, \ldots, C_N in this order.
Constraints
- 1 \leq N \leq 10
- N is an integer.
- S_i is a string of length between 1 and 10, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \ldots S_N
Output
Output the string obtained by concatenating C_1, C_2, \ldots, C_N in this order.
Sample Input 1
2 algorithm heuristic
Sample Output 1
24
- The first character of S_1=
algorithmisa, so C_1=2. - The first character of S_2=
heuristicish, so C_2=4.
Thus, output 24, which is their concatenation.
Sample Input 2
3 i love you
Sample Output 2
459
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 個のマスが左右一列に並んでいます。 はじめ、すべてのマスには何も置かれていません。
Q 個のクエリが与えられるので、順に処理してください。 各クエリは次の 2 種類のいずれかです。
1 x: 左から x 番目のマスにブロックを 1 個積む。その後、すべてのマスに 1 個以上のブロックが積まれているならば、すべてのマスからブロックを 1 個ずつ取り除く。2 y: y 個以上のブロックが積まれているマスの個数を出力する。
制約
- 1\leq N\leq 3\times 10^5
- 1\leq Q\leq 3\times 10^5
- 1\leq x\leq N
- 1\leq y\leq 3\times 10^5
- 入力はすべて整数
- 2 種類目のクエリが少なくとも 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリ \mathrm{query}_i (1\leq i\leq Q) は次の形式のいずれかで与えられる。
1 x
2 y
出力
2 種類目のクエリの個数を K 個として、K 行出力せよ。i 行目 (1\leq i\leq K) には i 番目の 2 種類目のクエリに対する答えを出力せよ。
入力例 1
3 7 1 1 1 3 1 3 2 1 2 2 1 2 2 1
出力例 1
2 1 1
N=3 であり、はじめ、左から 1,2,3 番目に積まれているブロックの数は (0,0,0) です。
クエリは順に次のように処理されます。
- 左から 1 番目のマスに 1 個ブロックを積みます。ブロックの積まれていないマスが存在するため、それ以上の操作は行いません。左から 1,2,3 番目に積まれているブロックの数は (1,0,0) となります。
- 左から 3 番目のマスに 1 個ブロックを積みます。左から 1,2,3 番目に積まれているブロックの数は (1,0,1) となります。
- 左から 3 番目のマスに 1 個ブロックを積みます。左から 1,2,3 番目に積まれているブロックの数は (1,0,2) となります。
- 1 個以上のブロックが積まれているマスは左から 1 番目と 3 番目のマスの 2 つです。よって、2 を出力します。
- 2 個以上のブロックが積まれているマスは左から 3 番目のマスの 1 つだけです。よって、1 を出力します。
- 左から 2 番目のマスに 1 個ブロックを積みます。この時点ですべてのマスにブロックが 1 個以上積まれているため、すべてのマスからブロックを 1 個取り除きます。左から 1,2,3 番目に積まれているブロックの数は (0,0,1) となります。
- 1 個以上のブロックが積まれているマスは左から 3 番目のマスの 1 つだけです。よって、1 を出力します。
よって、2,1,1 をこの順に各行に出力します。
Score : 300 points
Problem Statement
There are N cells arranged in a row from left to right. Initially, no blocks are placed in any cell.
You are given Q queries, so process them in order. Each query is one of the following two types:
1 x: Place 1 block in the x-th cell from the left. Then, if every cell has at least 1 block, remove 1 block from every cell.2 y: Output the number of cells that have at least y blocks.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq Q \leq 3 \times 10^5
- 1 \leq x \leq N
- 1 \leq y \leq 3 \times 10^5
- All input values are integers.
- At least one query of the second type exists.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query \mathrm{query}_i (1 \leq i \leq Q) is given in one of the following formats:
1 x
2 y
Output
Let K be the number of queries of the second type. Output K lines. The i-th line (1 \leq i \leq K) should contain the answer to the i-th query of the second type.
Sample Input 1
3 7 1 1 1 3 1 3 2 1 2 2 1 2 2 1
Sample Output 1
2 1 1
N=3, and initially the numbers of blocks placed in the 1-st, 2-nd, 3-rd cells from the left are (0, 0, 0). The queries are processed in order as follows:
- Place 1 block in the 1-st cell from the left. There exist cells with no blocks, so no further action is taken. The numbers of blocks in the 1-st, 2-nd, 3-rd cells become (1, 0, 0).
- Place 1 block in the 3-rd cell from the left. The numbers of blocks in the 1-st, 2-nd, 3-rd cells become (1, 0, 1).
- Place 1 block in the 3-rd cell from the left. The numbers of blocks in the 1-st, 2-nd, 3-rd cells become (1, 0, 2).
- The cells with at least 1 block are the 1-st and 3-rd cells from the left, so there are 2 such cells. Thus, output 2.
- The cells with at least 2 blocks is only the 3-rd cell from the left, so there is 1 such cell. Thus, output 1.
- Place 1 block in the 2-nd cell from the left. At this point, every cell has at least 1 block, so 1 block is removed from every cell. The numbers of blocks in the 1-st, 2-nd, 3-rd cells become (0, 0, 1).
- The cells with at least 1 block is only the 3-rd cell from the left, so there is 1 such cell. Thus, output 1.
Thus, output 2, 1, 1 in this order, one per line.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
英小文字からなる文字列 S が与えられます。
S の各文字を並び替えることでどの隣り合う 2 文字も異なるようにすることが可能か判定し、可能な場合はそのような並び替えを一つ求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 3\times 10^5
- S は英小文字からなる長さ 1 以上 10^6 以下の文字列
- 全てのテストケースにおける S の長さの総和は 10^6 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
i 番目 (1\le i\le T) のテストケース \text{case}_i は以下の形式で与えられる。
S
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、どの隣り合う 2 文字も異なるように S を並び替えることが不可能な場合は No を出力せよ。
可能な場合はそのような並び替えを S' として以下の形式で出力せよ。
Yes S'
条件を満たす S の並び替え方が複数存在する場合、どれを出力しても正答となる。
入力例 1
3 aiiw doodoo aabbababcacababaaba
出力例 1
Yes iwai No Yes ababacabababacababa
1 番目のテストケースについて考えます。
iwai は aiiw を並び替えてできる文字列で、どの隣接する 2 文字も異なります。したがって、iwai を出力すると正答となります。
この他にも、wiai や iawi などを出力しても正答となります。
Score : 400 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Determine whether it is possible to rearrange the characters of S so that no two adjacent characters are the same, and if so, find one such rearrangement.
You are given T test cases; solve each.
Constraints
- 1 \le T \le 3 \times 10^5
- S is a string of length between 1 and 10^6, inclusive, consisting of lowercase English letters.
- The total length of S across all test cases is at most 10^6.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
The i-th (1 \le i \le T) test case \text{case}_i is given in the following format:
S
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, if it is impossible to rearrange S so that no two adjacent characters are the same, output No.
If it is possible, let S' be such a rearrangement and output in the following format:
Yes S'
If there are multiple valid rearrangements of S, any of them will be accepted.
Sample Input 1
3 aiiw doodoo aabbababcacababaaba
Sample Output 1
Yes iwai No Yes ababacabababacababa
Consider the first test case.
iwai is a rearrangement of aiiw in which no two adjacent characters are the same. Thus, outputting iwai is correct.
Other correct outputs include wiai and iawi.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
N 頂点の根付き木 T があり、各頂点は頂点 1, 頂点 2, \ldots, 頂点 N と番号づけられています。
頂点 1 は T の根であり、頂点 i (2\leq i\leq N) の(直接の)親は P_i です。
また、頂点 i (1\leq i\leq N) には C_i 個のアメがあります。 ここで、(C_1+C_2+\cdots+C_N) 個のアメはすべて区別されます。
高橋君は N 匹のリスに命令を出しました。 具体的には、i 匹目 (1\leq i\leq N) のリスに次の命令を出しました。
- 頂点 i を根とした部分木から D_i 個のアメを選んで取ってくる。
異なるリスが同じアメを取ることはできません。
アメの選ばれ方としてあり得るものの個数を 998244353 で割った余りを出力してください。
ここで、最終的に選ばれたアメの集合が同じであっても、選んだリスが異なっていれば、異なる選ばれ方として数えるものとします。
また、すべてのリスが命令通りにアメを持ってくることが不可能な場合は 0 を出力してください。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq P_i\leq N
- 1\leq C_i\leq 10^9
- 1\leq D_i
- D_1+D_2+\cdots+D_N\leq 10^6
- 入力はすべて整数
- T は頂点 1 を根とした根付き木
入力
入力は以下の形式で標準入力から与えられる。
N P_2 P_3 \ldots P_N C_1 C_2 \ldots C_N D_1 D_2 \ldots D_N
出力
アメの選ばれ方としてあり得るものの個数を 998244353 で割った余りを出力せよ。
入力例 1
5 1 1 3 3 1 2 1 2 3 1 1 3 1 1
出力例 1
144
頂点 1,2,3,4,5 にあるアメをそれぞれアメ 1, アメ 2\cdot 3, アメ 4, アメ 5\cdot 6, アメ 7\cdot 8\cdot 9 とします。
アメの選ばれ方としては例えば次のようなものが考えられます。
- リス 1 が頂点 1 を根とした(頂点 1,2,3,4,5 からなる)木からアメ 3 を持ってくる。
- リス 2 が頂点 2 を根とした(頂点 2 からなる)木からアメ 2 を持ってくる。
- リス 3 が頂点 3 を根とした(頂点 3,4,5 からなる)木からアメ 4,7,9 を持ってくる。
- リス 4 が頂点 4 を根とした(頂点 4 のみからなる)木からアメ 6 を持ってくる。
- リス 5 が頂点 5 を根とした(頂点 5 のみからなる)木からアメ 8 を持ってくる。
なお、これは以下のような選ばれ方とは区別されます。
- リス 1 が頂点 1 を根とした(頂点 1,2,3,4,5 からなる)木からアメ \mathbf{2} を持ってくる。
- リス 2 が頂点 2 を根とした(頂点 2 からなる)木からアメ \mathbf{3} を持ってくる。
- リス 3 が頂点 3 を根とした(頂点 3,4,5 からなる)木からアメ 4,7,9 を持ってくる。
- リス 4 が頂点 4 を根とした(頂点 4 のみからなる)木からアメ 6 を持ってくる。
- リス 5 が頂点 5 を根とした(頂点 5 のみからなる)木からアメ 8 を持ってくる。
条件をみたす選び方は全部で 144 通りあるため、144 を 998244353 で割った余りである 144 を出力します。
入力例 2
2 1 1 1 2 1
出力例 2
0
木には頂点 1 と頂点 2 あわせて 2 個しかアメがないため、両方のリスが命令通りにアメを選ぶことはできません。
よって、0 を出力します。
入力例 3
3 3 1 1000000000 1 1 1 1 1
出力例 3
1755647
998244353 で割った余りを出力することに注意してください。
Score : 450 points
Problem Statement
There is a rooted tree T with N vertices, where the vertices are numbered as vertex 1, vertex 2, \ldots, vertex N.
Vertex 1 is the root of T, and the (direct) parent of vertex i (2 \leq i \leq N) is P_i.
Additionally, vertex i (1 \leq i \leq N) has C_i candies. All (C_1 + C_2 + \cdots + C_N) candies are distinguishable from each other.
Takahashi gave instructions to N squirrels. Specifically, he gave the following instruction to the i-th squirrel (1 \leq i \leq N):
- Choose and collect D_i candies from the subtree rooted at vertex i.
Different squirrels cannot take the same candy.
Output the number, modulo 998244353, of possible ways to choose the candies.
Here, even if the set of candies ultimately chosen is the same, if the squirrels that chose them are different, they are counted as different ways.
If it is impossible for all squirrels to bring back candies as instructed, output 0.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq N
- 1 \leq C_i \leq 10^9
- 1 \leq D_i
- D_1 + D_2 + \cdots + D_N \leq 10^6
- All input values are integers.
- T is a rooted tree with vertex 1 as the root.
Input
The input is given from Standard Input in the following format:
N P_2 P_3 \ldots P_N C_1 C_2 \ldots C_N D_1 D_2 \ldots D_N
Output
Output the number, modulo 998244353, of possible ways to choose the candies.
Sample Input 1
5 1 1 3 3 1 2 1 2 3 1 1 3 1 1
Sample Output 1
144
We call the candies so that we have candy 1 at vertex 1, candies 2, 3 at vertex 2, candy 4 at vertex 3, candies 5, 6 at vertex 4, and candies 7, 8, 9 at vertex 5.
One possible way to choose the candies is as follows:
- Squirrel 1 collects candy 3 from the tree rooted at vertex 1 (consisting of vertices 1, 2, 3, 4, 5).
- Squirrel 2 collects candy 2 from the tree rooted at vertex 2 (consisting of vertex 2).
- Squirrel 3 collects candies 4, 7, 9 from the tree rooted at vertex 3 (consisting of vertices 3, 4, 5).
- Squirrel 4 collects candy 6 from the tree rooted at vertex 4 (consisting of vertex 4 only).
- Squirrel 5 collects candy 8 from the tree rooted at vertex 5 (consisting of vertex 5 only).
Note that this is counted as a different way from the following:
- Squirrel 1 collects candy \mathbf{2} from the tree rooted at vertex 1 (consisting of vertices 1, 2, 3, 4, 5).
- Squirrel 2 collects candy \mathbf{3} from the tree rooted at vertex 2 (consisting of vertex 2).
- Squirrel 3 collects candies 4, 7, 9 from the tree rooted at vertex 3 (consisting of vertices 3, 4, 5).
- Squirrel 4 collects candy 6 from the tree rooted at vertex 4 (consisting of vertex 4 only).
- Squirrel 5 collects candy 8 from the tree rooted at vertex 5 (consisting of vertex 5 only).
There are 144 valid ways in total, so output 144 modulo 998244353, that is, 144.
Sample Input 2
2 1 1 1 2 1
Sample Output 2
0
The tree has only two candies across vertices 1 and 2, so it is impossible for both squirrels to collect candies as instructed.
Thus, output 0.
Sample Input 3
3 3 1 1000000000 1 1 1 1 1
Sample Output 3
1755647
Remember to output the count modulo 998244353.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
あなたは以下の操作を A に対して 0 回以上好きな回数行うことができます:
- 1\le i \le N - 1 を満たす整数 i を選び、A_i を 1 減らし、A_{i+1} を 1 増やす。
A を狭義単調増加列にするために必要な操作回数の最小値を求めてください。
ただし、答えは 2^{63} 未満になることが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 3\times 10^5
- 1\le N\le 2\times 10^5
- 0\le A_i\le 10^9
- 全てのテストケースにおける N の総和は 6\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
i 番目 (1\le i\le T) のテストケース \text{case}_i は以下の形式で与えられる。
N A_1 A_2 \ldots A_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
4 3 0 1 0 4 4 6 3 5 7 1 2 3 4 5 6 7 10 11 9 1 3 17 19 10 19 17 3
出力例 1
3 5 0 78
1 番目のテストケースについて考えます。
以下のように操作することで 3 回の操作で A を狭義単調増加列にすることができます:
- i=1 を選ぶ。A=(-1,2,0) となる。
- i=2 を選ぶ。A=(-1,1,1) となる。
- i=2 を選ぶ。A=(-1,0,2) となる。
3 回未満の操作で A を狭義単調増加列にすることはできないので、3 を出力してください。
Score : 525 points
Problem Statement
You are given a sequence of non-negative integers A = (A_1, A_2, \ldots, A_N) of length N.
You can perform the following operation on A zero or more times:
- Choose an integer i with 1 \le i \le N - 1, decrease A_i by 1, and increase A_{i+1} by 1.
Find the minimum number of operations required to make A strictly increasing.
It can be proved that the answer is less than 2^{63}.
You are given T test cases; solve each.
Constraints
- 1 \le T \le 3 \times 10^5
- 1 \le N \le 2 \times 10^5
- 0 \le A_i \le 10^9
- The sum of N across all test cases is at most 6 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
The i-th (1 \le i \le T) test case \text{case}_i is given in the following format:
N A_1 A_2 \ldots A_N
Output
Output the answers for the test cases in order, separated by newlines.
Sample Input 1
4 3 0 1 0 4 4 6 3 5 7 1 2 3 4 5 6 7 10 11 9 1 3 17 19 10 19 17 3
Sample Output 1
3 5 0 78
Consider the first test case.
By performing the following operations, A can be made strictly increasing in three operations:
- Choose i=1. A becomes (-1, 2, 0).
- Choose i=2. A becomes (-1, 1, 1).
- Choose i=2. A becomes (-1, 0, 2).
It is impossible to make A strictly increasing in fewer than three operations, so output 3.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 625 点
問題文
整数 A,B,X,Y が与えられます。
二次元平面上にコマが置かれています。はじめコマは座標 (0,0) にあります。
あなたは以下の操作を 0 回以上何回でも行うことができます:
- 今コマがある座標を (x,y) として、以下の条件のいずれかを満たす座標 (x',y') にコマを移動させる。
- |x-x'|=A かつ |y-y'|=B
- |x-x'|=B かつ |y-y'|=A
コマを座標 (X,Y) に移動させることが可能か判定し、可能であれば必要な操作回数の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 2\times 10^5
- 1\le A < B \le 10^6
- 0\le X,Y\le 10^6
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
i 番目 (1\le i\le T) のテストケース \text{case}_i は以下の形式で与えられる。
A B X Y
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、コマを座標 (X,Y) に移動させることが可能な場合は操作回数の最小値を、不可能な場合は -1 を出力せよ。
入力例 1
4 1 2 1 0 4 6 3 5 459 2026 0 0 19 2026 523 459459
出力例 1
3 -1 0 2670
1 番目のテストケースについて考えます。
コマを座標 (0,0) から順に (1,2),(-1,1),(1,0) へと動かすことで 3 回の操作でコマを座標 (1,0) に移動させることができます。
3 回未満の操作でコマを座標 (1,0) に移動させることはできないので、1 行目には 3 を出力してください。
Score : 625 points
Problem Statement
You are given integers A, B, X, Y.
A piece is placed on a two-dimensional plane. Initially, the piece is at coordinates (0, 0).
You can perform the following operation zero or more times:
- Let (x, y) be the current coordinates of the piece. Move the piece to coordinates (x', y') satisfying one of the following conditions:
- |x - x'| = A and |y - y'| = B
- |x - x'| = B and |y - y'| = A
Determine whether it is possible to move the piece to coordinates (X, Y), and if so, find the minimum number of operations required.
You are given T test cases; solve each.
Constraints
- 1 \le T \le 2 \times 10^5
- 1 \le A < B \le 10^6
- 0 \le X, Y \le 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
The i-th (1 \le i \le T) test case \text{case}_i is given in the following format:
A B X Y
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, if it is possible to move the piece to coordinates (X, Y), output the minimum number of operations required; otherwise, output -1.
Sample Input 1
4 1 2 1 0 4 6 3 5 459 2026 0 0 19 2026 523 459459
Sample Output 1
3 -1 0 2670
Consider the first test case.
By moving the piece from (0, 0) to (1, 2), then to (-1, 1), then to (1, 0) in order, the piece can be moved to coordinates (1, 0) in three operations.
It is impossible to move the piece to coordinates (1, 0) in fewer than three operations, so output 3 on the first line.