Codeforces Round 929 (Div. 3) A B C D E

A. Turtle Puzzle: Rearrange and Negate

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are given an array a a a of n n n integers. You must perform the following two operations on the array (the first, then the second):

  1. Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged.
  2. Choose at most one contiguous segment of elements and replace the signs of all elements in this segment with their opposites. Formally, you can choose a pair of indices l , r l, r l,r such that 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn and assign a i = − a i a_i = -a_i ai=ai for all l ≤ i ≤ r l \le i \le r lir (negate elements). Note that you may choose not to select a pair of indices and leave all the signs of the elements unchanged.

What is the maximum sum of the array elements after performing these two operations (the first, then the second)?

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 50 1 \le n \le 50 1n50) — the number of elements in array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( − 100 ≤ a i ≤ 100 -100 \le a_i \le 100 100ai100) — elements of the array.

Output

For each test case, output the maximum sum of the array elements after sequentially performing the two given operations.

Example

input

8
3
-2 3 -3
1
0
2
0 1
1
-99
4
10 -2 -3 7
5
-1 -2 -3 -4 -5
6
-41 22 -69 73 -15 -50
12
1 2 3 4 5 6 7 8 9 10 11 12

output

8
0
1
99
22
15
270
78

Tutorial

输出所有元素的绝对值之和,即 ∑ i = 0 i = n ∣ a i ∣ \sum_{i = 0}^{i = n}\vert a_i \vert i=0i=nai

Solution

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    print(sum(abs(ai) for ai in a))

B. Turtle Math: Fast Three Task

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are given an array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an.

In one move, you can perform either of the following two operations:

  • Choose an element from the array and remove it from the array. As a result, the length of the array decreases by 1 1 1;
  • Choose an element from the array and increase its value by 1 1 1.

You can perform any number of moves. If the current array becomes empty, then no more moves can be made.

Your task is to find the minimum number of moves required to make the sum of the elements of the array a a a divisible by 3 3 3. It is possible that you may need 0 0 0 moves.

Note that the sum of the elements of an empty array (an array of length 0 0 0) is equal to 0 0 0.

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105).

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 4 1 \le a_i \le 10^4 1ai104).

The sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer: the minimum number of moves.

Example

input

8
4
2 2 5 4
3
1 3 2
4
3 7 6 8
1
1
4
2 2 4 2
2
5 5
7
2 4 8 1 9 3 4
2
4 10

output

1
0
0
1
1
2
1
1

Note

In the first test case, initially the array a = [ 2 , 2 , 5 , 4 ] a = [2, 2, 5, 4] a=[2,2,5,4]. One of the optimal ways to make moves is:

  • remove the current 4 4 4th element and get a = [ 2 , 2 , 5 ] a = [2, 2, 5] a=[2,2,5];

As a result, the sum of the elements of the array a a a will be divisible by 3 3 3 (indeed, a 1 + a 2 + a 3 = 2 + 2 + 5 = 9 a_1 + a_2 + a_3 = 2 + 2 + 5 = 9 a1+a2+a3=2+2+5=9).

In the second test case, initially, the sum of the array is 1 + 3 + 2 = 6 1+3+2 = 6 1+3+2=6, which is divisible by 3 3 3. Therefore, no moves are required. Hence, the answer is 0 0 0.

In the fourth test case, initially, the sum of the array is 1 1 1, which is not divisible by 3 3 3. By removing its only element, you will get an empty array, so its sum is 0 0 0. Hence, the answer is 1 1 1.

Tutorial

  • 如果对 3 取余等于 0,则需要 0 次操作
  • 如果对 3 取余等于 2,则需要 1 次添加操作即可
  • 如果对 3 取余等于 1
    • 若数组中有一个元素对 3 取余等于 1,删除这个元素即可
    • 否则做两次添加操作

Solution

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    ans = (3 - sum(a) % 3) % 3
    for ai in a:
        if ai % 3 == 3 - ans:
            ans = min(ans, 1)
    print(ans)

C. Turtle Fingers: Count the Values of k

time limit per test: 5 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are given three positive integers a a a, b b b and l l l ( a , b , l > 0 a,b,l>0 a,b,l>0).

It can be shown that there always exists a way to choose non-negative (i.e. ≥ 0 \ge 0 0) integers k k k, x x x, and y y y such that l = k ⋅ a x ⋅ b y l = k \cdot a^x \cdot b^y l=kaxby.

Your task is to find the number of distinct possible values of k k k across all such ways.

Input

The first line contains the integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The following t t t lines contain three integers, a a a, b b b and l l l ( 2 ≤ a , b ≤ 100 2 \le a, b \le 100 2a,b100, 1 ≤ l ≤ 1 0 6 1 \le l \le 10^6 1l106) — description of a test case.

Output

Output t t t lines, with the i i i-th ( 1 ≤ i ≤ t 1 \le i \le t 1it) line containing an integer, the answer to the i i i-th test case.

Example

input

11
2 5 20
2 5 21
4 6 48
2 3 72
3 5 75
2 2 1024
3 7 83349
100 100 1000000
7 3 2
2 6 6
17 3 632043

output

6
1
5
12
6
11
24
4
1
3
24

Note

In the first test case, a = 2 , b = 5 , l = 20 a=2, b=5, l=20 a=2,b=5,l=20. The possible values of k k k (and corresponding x , y x,y x,y) are as follows:

  • Choose k = 1 , x = 2 , y = 1 k = 1, x = 2, y = 1 k=1,x=2,y=1. Then k ⋅ a x ⋅ b y = 1 ⋅ 2 2 ⋅ 5 1 = 20 = l k \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = l kaxby=12251=20=l.
  • Choose k = 2 , x = 1 , y = 1 k = 2, x = 1, y = 1 k=2,x=1,y=1. Then k ⋅ a x ⋅ b y = 2 ⋅ 2 1 ⋅ 5 1 = 20 = l k \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = l kaxby=22151=20=l.
  • Choose k = 4 , x = 0 , y = 1 k = 4, x = 0, y = 1 k=4,x=0,y=1. Then k ⋅ a x ⋅ b y = 4 ⋅ 2 0 ⋅ 5 1 = 20 = l k \cdot a^x \cdot b^y = 4 \cdot 2^0 \cdot 5^1 = 20 = l kaxby=42051=20=l.
  • Choose k = 5 , x = 2 , y = 0 k = 5, x = 2, y = 0 k=5,x=2,y=0. Then k ⋅ a x ⋅ b y = 5 ⋅ 2 2 ⋅ 5 0 = 20 = l k \cdot a^x \cdot b^y = 5 \cdot 2^2 \cdot 5^0 = 20 = l kaxby=52250=20=l.
  • Choose k = 10 , x = 1 , y = 0 k = 10, x = 1, y = 0 k=10,x=1,y=0. Then k ⋅ a x ⋅ b y = 10 ⋅ 2 1 ⋅ 5 0 = 20 = l k \cdot a^x \cdot b^y = 10 \cdot 2^1 \cdot 5^0 = 20 = l kaxby=102150=20=l.
  • Choose k = 20 , x = 0 , y = 0 k = 20, x = 0, y = 0 k=20,x=0,y=0. Then k ⋅ a x ⋅ b y = 20 ⋅ 2 0 ⋅ 5 0 = 20 = l k \cdot a^x \cdot b^y = 20 \cdot 2^0 \cdot 5^0 = 20 = l kaxby=202050=20=l.

In the second test case, a = 2 , b = 5 , l = 21 a=2, b=5, l=21 a=2,b=5,l=21. Note that l = 21 l = 21 l=21 is not divisible by either a = 2 a = 2 a=2 or b = 5 b = 5 b=5. Therefore, we can only set x = 0 , y = 0 x = 0, y = 0 x=0,y=0, which corresponds to k = 21 k = 21 k=21.

In the third test case, a = 4 , b = 6 , l = 48 a=4, b=6, l=48 a=4,b=6,l=48. The possible values of k k k (and corresponding x , y x,y x,y) are as follows:

  • Choose k = 2 , x = 1 , y = 1 k = 2, x = 1, y = 1 k=2,x=1,y=1. Then k ⋅ a x ⋅ b y = 2 ⋅ 4 1 ⋅ 6 1 = 48 = l k \cdot a^x \cdot b^y = 2 \cdot 4^1 \cdot 6^1 = 48 = l kaxby=24161=48=l.
  • Choose k = 3 , x = 2 , y = 0 k = 3, x = 2, y = 0 k=3,x=2,y=0. Then k ⋅ a x ⋅ b y = 3 ⋅ 4 2 ⋅ 6 0 = 48 = l k \cdot a^x \cdot b^y = 3 \cdot 4^2 \cdot 6^0 = 48 = l kaxby=34260=48=l.
  • Choose k = 8 , x = 0 , y = 1 k = 8, x = 0, y = 1 k=8,x=0,y=1. Then k ⋅ a x ⋅ b y = 8 ⋅ 4 0 ⋅ 6 1 = 48 = l k \cdot a^x \cdot b^y = 8 \cdot 4^0 \cdot 6^1 = 48 = l kaxby=84061=48=l.
  • Choose k = 12 , x = 1 , y = 0 k = 12, x = 1, y = 0 k=12,x=1,y=0. Then k ⋅ a x ⋅ b y = 12 ⋅ 4 1 ⋅ 6 0 = 48 = l k \cdot a^x \cdot b^y = 12 \cdot 4^1 \cdot 6^0 = 48 = l kaxby=124160=48=l.
  • Choose k = 48 , x = 0 , y = 0 k = 48, x = 0, y = 0 k=48,x=0,y=0. Then k ⋅ a x ⋅ b y = 48 ⋅ 4 0 ⋅ 6 0 = 48 = l k \cdot a^x \cdot b^y = 48 \cdot 4^0 \cdot 6^0 = 48 = l kaxby=484060=48=l.

Tutorial

暴力枚举 x x x y y y,如果满足题意,则将 l a x × b y l \over {a^x \times b^y} ax×byl 添加到 s e t set set 中,最后输出 s e t set set 的容器大小

Solution

for _ in range(int(input())):
    a, b, l = map(int, input().split())
    s = set()
    cnt1 = 1
    for x in range(l):
        if cnt1 > l:
            break
        if not l % cnt1:
            mid, cnt2 = l // cnt1, 1
            for y in range(l):
                if cnt2 > mid:
                    break
                if not mid % cnt2:
                    s.add(mid // cnt2)
                cnt2 *= b
        cnt1 *= a
    print(len(s))

D. Turtle Tenacity: Continual Mods

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Given an array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an, determine whether it is possible to rearrange its elements into b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn, such that b 1   m o d   b 2   m o d   …   m o d   b n ≠ 0 b_1 \bmod b_2 \bmod \ldots \bmod b_n \neq 0 b1modb2modmodbn=0.

Here x   m o d   y x \bmod y xmody denotes the remainder from dividing x x x by y y y. Also, the modulo operations are calculated from left to right. That is, x   m o d   y   m o d   z = ( x   m o d   y )   m o d   z x \bmod y \bmod z = (x \bmod y) \bmod z xmodymodz=(xmody)modz. For example, 2024   m o d   1000   m o d   8 = ( 2024   m o d   1000 )   m o d   8 = 24   m o d   8 = 0 2024 \bmod 1000 \bmod 8 = (2024 \bmod 1000) \bmod 8 = 24 \bmod 8 = 0 2024mod1000mod8=(2024mod1000)mod8=24mod8=0.

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105).

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109).

The sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output “YES” if it is possible, “NO” otherwise.

You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive responses.

Example

input

8
6
1 2 3 4 5 6
5
3 3 3 3 3
3
2 2 3
5
1 1 2 3 7
3
1 2 2
3
1 1 2
6
5 2 10 10 10 2
4
3 6 9 3

output

YES
NO
YES
NO
YES
NO
YES
NO

Note

In the first test case, rearranging the array into b = [ 1 , 2 , 3 , 4 , 5 , 6 ] b = [1, 2, 3, 4, 5, 6] b=[1,2,3,4,5,6] (doing nothing) would result in 1   m o d   2   m o d   3   m o d   4   m o d   5   m o d   6 = 1 1 \bmod 2 \bmod 3 \bmod 4 \bmod 5 \bmod 6 = 1 1mod2mod3mod4mod5mod6=1. Hence it is possible to achieve the goal.

In the second test case, the array b b b must be equal to [ 3 , 3 , 3 , 3 , 3 ] [3, 3, 3, 3, 3] [3,3,3,3,3], which would result in 3   m o d   3   m o d   3   m o d   3   m o d   3 = 0 3 \bmod 3 \bmod 3 \bmod 3 \bmod 3 = 0 3mod3mod3mod3mod3=0. Hence it is impossible to achieve the goal.

In the third test case, rearranging the array into b = [ 3 , 2 , 2 ] b = [3, 2, 2] b=[3,2,2] would result in 3   m o d   2   m o d   2 = 1 3 \bmod 2 \bmod 2 = 1 3mod2mod2=1. Hence it is possible to achieve the goal.

Tutorial

需要构建出唯一的一个最小值,如果数组的最小值已经唯一,则拿这个最小值去对其他所有数取余,否则用其他数对当前最小值取余,如果能找到比当前最小值小的数,则输出 YES,否则输出 NO

Solution

for _ in range(int(input())):
    n = int(input())
    a = sorted(list(map(int, input().split())))
    ans = "NO"
    if a[0] < a[1]:
        ans = "YES"
    for ai in a[2:]:
        if ai % a[0] and ai % a[0] < a[0]:
            ans = "YES"
            break
    print(ans)

E. Turtle vs. Rabbit Race: Optimal Trainings

time limit per test: 5 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Isaac begins his training. There are n n n running tracks available, and the i i i-th track ( 1 ≤ i ≤ n 1 \le i \le n 1in) consists of a i a_i ai equal-length sections.

Given an integer u u u ( 1 ≤ u ≤ 1 0 9 1 \le u \le 10^9 1u109), finishing each section can increase Isaac’s ability by a certain value, described as follows:

  • Finishing the 1 1 1-st section increases Isaac’s performance by u u u.
  • Finishing the 2 2 2-nd section increases Isaac’s performance by u − 1 u-1 u1.
  • Finishing the 3 3 3-rd section increases Isaac’s performance by u − 2 u-2 u2.
  • … \ldots
  • Finishing the k k k-th section ( k ≥ 1 k \ge 1 k1) increases Isaac’s performance by u + 1 − k u+1-k u+1k. (The value u + 1 − k u+1-k u+1k can be negative, which means finishing an extra section decreases Isaac’s performance.)

You are also given an integer l l l. You must choose an integer r r r such that l ≤ r ≤ n l \le r \le n lrn and Isaac will finish each section of each track l , l + 1 , … , r l, l + 1, \dots, r l,l+1,,r (that is, a total of ∑ i = l r a i = a l + a l + 1 + … + a r \sum_{i=l}^r a_i = a_l + a_{l+1} + \ldots + a_r i=lrai=al+al+1++ar sections).

Answer the following question: what is the optimal r r r you can choose that the increase in Isaac’s performance is maximum possible?

If there are multiple r r r that maximize the increase in Isaac’s performance, output the smallest r r r.

To increase the difficulty, you need to answer the question for q q q different values of l l l and u u u.

Input

The first line of input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The descriptions of the test cases follow.

The first line contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105).

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 4 1 \le a_i \le 10^4 1ai104).

The third line contains a single integer q q q ( 1 ≤ q ≤ 1 0 5 1 \le q \le 10^5 1q105).

The next q q q lines each contain two integers l l l and u u u ( 1 ≤ l ≤ n , 1 ≤ u ≤ 1 0 9 1 \le l \le n, 1 \le u \le 10^9 1ln,1u109) — the descriptions to each query.

The sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105. The sum of q q q over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output q q q integers: the i i i-th integer contains the optimal r r r for the i i i-th query. If there are multiple solutions, output the smallest one.

Example

input

5
6
3 1 4 1 5 9
3
1 8
2 7
5 9
1
10
1
1 1
9
5 10 9 6 8 3 10 7 3
5
8 56
1 12
9 3
1 27
5 45
5
7 9 2 5 2
10
1 37
2 9
3 33
4 32
4 15
2 2
4 2
2 19
3 7
2 7
10
9 1 6 7 6 3 10 7 3 10
5
10 43
3 23
9 3
6 8
5 14

output

3 4 5 
1 
9 2 9 4 9 
5 2 5 5 5 2 4 5 4 2 
10 6 9 7 7 

Note

For the 1 1 1-st query in the first test case:

  • By choosing r = 3 r = 3 r=3, Isaac finishes a 1 + a 2 + a 3 = 3 + 1 + 4 = 8 a_1 + a_2 + a_3 = 3 + 1 + 4 = 8 a1+a2+a3=3+1+4=8 sections in total, hence his increase in performance is u + ( u − 1 ) + … + ( u − 7 ) = 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 36 u+(u-1)+\ldots+(u-7)=8+7+6+5+4+3+2+1 = 36 u+(u1)++(u7)=8+7+6+5+4+3+2+1=36.
  • By choosing r = 4 r = 4 r=4, Isaac finishes a 1 + a 2 + a 3 + a 4 = 3 + 1 + 4 + 1 = 9 a_1 + a_2 + a_3 + a_4 = 3 + 1 + 4 + 1 = 9 a1+a2+a3+a4=3+1+4+1=9 sections in total, hence his increase in performance is u + ( u − 1 ) + … + ( u − 8 ) = 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 = 36 u+(u-1)+\ldots+(u-8)=8+7+6+5+4+3+2+1+0 = 36 u+(u1)++(u8)=8+7+6+5+4+3+2+1+0=36.

Both choices yield the optimal increase in performance, however we want to choose the smallest r r r. So we choose r = 3 r = 3 r=3.

For the 2 2 2-nd query in the first test case, by choosing r = 4 r = 4 r=4, Isaac finishes a 2 + a 3 + a 4 = 1 + 4 + 1 = 6 a_2 + a_3 + a_4 = 1 + 4 + 1 = 6 a2+a3+a4=1+4+1=6 sections in total, hence his increase in performance is u + ( u − 1 ) + … + ( u − 5 ) = 7 + 6 + 5 + 4 + 3 + 2 = 27 u+(u-1)+\ldots+(u-5)=7+6+5+4+3+2 = 27 u+(u1)++(u5)=7+6+5+4+3+2=27. This is the optimal increase in performance.

For the 3 3 3-rd query in the first test case:

  • By choosing r = 5 r = 5 r=5, Isaac finishes a 5 = 5 a_5 = 5 a5=5 sections in total, hence his increase in performance is u + ( u − 1 ) + … + ( u − 4 ) = 9 + 8 + 7 + 6 + 5 = 35 u+(u-1)+\ldots+(u-4)=9+8+7+6+5 = 35 u+(u1)++(u4)=9+8+7+6+5=35.
  • By choosing r = 6 r = 6 r=6, Isaac finishes a 5 + a 6 = 5 + 9 = 14 a_5 + a_6 = 5 + 9 = 14 a5+a6=5+9=14 sections in total, hence his increase in performance is u + ( u − 1 ) + … + ( u − 13 ) = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 + ( − 1 ) + ( − 2 ) + ( − 3 ) + ( − 4 ) = 35 u+(u-1)+\ldots+(u-13)=9+8+7+6+5+4+3+2+1+0+(-1)+(-2)+(-3)+(-4) = 35 u+(u1)++(u13)=9+8+7+6+5+4+3+2+1+0+(1)+(2)+(3)+(4)=35.

Both choices yield the optimal increase in performance, however we want to choose the smallest r r r. So we choose r = 5 r = 5 r=5.

Hence the output for the first test case is [ 3 , 4 , 5 ] [3, 4, 5] [3,4,5].

Tutorial

二分答案到最接近 u u u 的区间即可,此时右边界的左右两个值都有可能成为答案,所以需要对右边界两边求出可以提高的能力,然后比较得到可以获得能力最大的位置

Solution

def check(sum1, sum2, u):
    return sum1 * u - (sum1 - 1) * sum1 // 2 > sum2 * u - (sum2 - 1) * sum2 // 2

for _ in range(int(input())):
    n = int(input())
    pre = [0] + list(map(int, input().split()))
    for i in range(1, n + 1):
        pre[i] += pre[i - 1]

    ans = []
    for __ in range(int(input())):
        l, u = map(int, input().split())
        left, right = l, n + 1
        while left + 1 < right:
            mid = (left + right) >> 1
            if check(pre[mid] - pre[l - 1], pre[mid - 1] - pre[l - 1], u):
                left = mid
            else:
                right = mid
        ans.append(left)
    print(*ans)

F. Turtle Mission: Robot and the Earthquake

time limit per test: 3 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

The world is a grid with n n n rows and m m m columns. The rows are numbered 0 , 1 , … , n − 1 0, 1, \ldots, n-1 0,1,,n1, while the columns are numbered 0 , 1 , … , m − 1 0, 1, \ldots, m-1 0,1,,m1. In this world, the columns are cyclic (i.e. the top and the bottom cells in each column are adjacent). The cell on the i i i-th row and the j j j-th column ( 0 ≤ i < n , 0 ≤ j < m 0 \le i < n, 0 \le j < m 0i<n,0j<m) is denoted as ( i , j ) (i,j) (i,j).

At time 0 0 0, the cell ( i , j ) (i,j) (i,j) (where 0 ≤ i < n , 0 ≤ j < m 0 \le i < n, 0 \le j < m 0i<n,0j<m) contains either a rock or nothing. The state of cell ( i , j ) (i,j) (i,j) can be described using the integer a i , j a_{i,j} ai,j:

  • If a i , j = 1 a_{i,j} = 1 ai,j=1, there is a rock at ( i , j ) (i,j) (i,j).
  • If a i , j = 0 a_{i,j} = 0 ai,j=0, there is nothing at ( i , j ) (i,j) (i,j).

As a result of aftershocks from the earthquake, the columns follow tectonic plate movements: each column moves cyclically upwards at a velocity of 1 1 1 cell per unit of time. Formally, for some 0 ≤ i < n , 0 ≤ j < m 0 \le i < n, 0 \le j < m 0i<n,0j<m, if ( i , j ) (i,j) (i,j) contains a rock at the moment, it will move from ( i , j ) (i, j) (i,j) to ( i − 1 , j ) (i - 1, j) (i1,j) (or to ( n − 1 , j ) (n - 1, j) (n1,j) if i = 0 i=0 i=0).

The robot called RT is initially positioned at ( 0 , 0 ) (0,0) (0,0). It has to go to ( n − 1 , m − 1 ) (n-1,m-1) (n1,m1) to carry out an earthquake rescue operation (to the bottom rightmost cell). The earthquake doesn’t change the position of the robot, they only change the position of rocks in the world.

Let RT’s current position be ( x , y ) (x,y) (x,y) ( 0 ≤ x < n , 0 ≤ y < m 0 \le x < n, 0 \le y < m 0x<n,0y<m), it can perform the following operations:

  • Go one cell cyclically upwards, i.e. from ( x , y ) (x,y) (x,y) to ( ( x + n − 1 )   m o d   n , y ) ((x+n-1) \bmod n, y) ((x+n1)modn,y) using 1 1 1 unit of time.
  • Go one cell cyclically downwards, i.e. ( x , y ) (x,y) (x,y) to ( ( x + 1 )   m o d   n , y ) ((x+1) \bmod n, y) ((x+1)modn,y) using 1 1 1 unit of time.
  • Go one cell to the right, i.e. ( x , y ) (x,y) (x,y) to ( x , y + 1 ) (x, y+1) (x,y+1) using 1 1 1 unit of time. (RT may perform this operation only if y < m − 1 y < m-1 y<m1.)

Note that RT cannot go left using the operations nor can he stay at a position.

Unfortunately, RT will explode upon colliding with a rock. As such, when RT is at ( x , y ) (x,y) (x,y) and there is a rock at ( ( x + 1 )   m o d   n , y ) ((x+1) \bmod n, y) ((x+1)modn,y) or ( ( x + 2 )   m o d   n , y ) ((x+2) \bmod n, y) ((x+2)modn,y), RT cannot move down or it will be hit by the rock.

[外链图片转存中…(img-t9DlryzV-1709186980888)]

Similarly, if y + 1 < m y+1 < m y+1<m and there is a rock at ( ( x + 1 )   m o d   n , y + 1 ) ((x+1) \bmod n, y+1) ((x+1)modn,y+1), RT cannot move right or it will be hit by the rock.

[外链图片转存中…(img-lf4NvjMn-1709186980889)]

However, it is worth noting that if there is a rock at ( x   m o d   n , y + 1 ) (x \bmod n, y+1) (xmodn,y+1) and ( ( x + 1 )   m o d   n , y ) ((x+1) \bmod n, y) ((x+1)modn,y), RT can still move right safely.

Find the minimum amount of time RT needs to reach ( n − 1 , m − 1 ) (n-1,m-1) (n1,m1) without colliding with any rocks. If it is impossible to do so, output − 1 -1 1.

Input

The first line of the input contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

In each test case, the first line contains two integers n n n, m m m ( 3 ≤ n , m ≤ 1 0 3 3 \le n, m \le 10^3 3n,m103) — the size of the planet’s boundaries.

Each of the next n n n lines contains m m m integers. The ( j + 1 ) (j+1) (j+1)-th integer on the ( i + 1 ) (i+1) (i+1)-th line ( 0 ≤ i < n , 0 ≤ j < m 0 \le i < n, 0 \le j < m 0i<n,0j<m) is a i , j a_{i,j} ai,j ( 0 ≤ a i , j ≤ 1 0 \le a_{i,j} \le 1 0ai,j1), which denotes whether or not there is a rock at ( i , j ) (i,j) (i,j) at time 0 0 0.

Additionally, it is guaranteed that a 0 , 0 = 0 a_{0,0} = 0 a0,0=0, and a i , m − 1 = 0 a_{i, m-1} = 0 ai,m1=0 for 0 ≤ i < n 0 \le i < n 0i<n. In other words, there is no rock at RT’s initial position as well as column m − 1 m-1 m1.

The sum of n ⋅ m n \cdot m nm over all test cases does not exceed 1 0 6 10^6 106.

Output

For each test case:

  • If the destination can be reached without colliding with any rocks, output a single integer — the minimum amount of time RT needs to reach ( n − 1 , m − 1 ) (n-1,m-1) (n1,m1).
  • Otherwise, output − 1 -1 1.

Example

input

12
4 5
0 1 0 0 0
0 0 1 0 0
1 0 1 1 0
0 0 0 0 0
3 3
0 0 0
1 0 0
0 0 0
5 3
0 0 0
0 0 0
1 0 0
0 0 0
1 0 0
3 7
0 0 1 0 0 1 0
1 0 1 0 1 0 0
0 1 0 0 0 0 0
3 4
0 1 0 0
1 0 0 0
0 1 1 0
5 5
0 0 0 0 0
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
3 3
0 0 0
0 0 0
0 0 0
4 3
0 1 0
1 0 0
0 1 0
1 0 0
4 3
0 1 0
0 1 0
0 1 0
0 1 0
3 3
0 0 0
1 1 0
0 0 0
3 3
0 1 0
0 0 0
0 1 0
5 5
0 0 0 0 0
0 1 1 0 0
0 1 1 0 0
0 0 0 0 0
0 0 1 0 0

output

7
3
3
8
-1
12
3
3
-1
-1
3
8

Note

Visual explanation of the first test case in the example:

Tutorial

本题可以用 b f s bfs bfs 解决

如果把机器人相对于终点进行 b f s bfs bfs,由于一个图的石头有很多,每次计算石头在哪较为复杂,所以可以把机器人相对于石头进行 b f s bfs bfs​,此时机器人向上移动变成了静止不动,向下移动变成了移动两格,向右移动变成了向右移动一格,向左移动一格

由于最后一列和起点都是没有石头的,所以可以直接跑到终点列,再跑到终点,所以需要找到跑到相对于 E N D END END 的相对位置需要多少步,再看需要多少步跑到 E N D END END

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
#define PII pair<int, int>

const int INF = 0x3f3f3f3f;

void solve() {
    int n, m, ans = INF;
    cin >> n >> m;
    // dist[i][j] 为相对棋盘初始状态 [i][j] 的位置所要的步数
    vector g(n, vector<int>(m)), dist(n, vector<int>(m, INF));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> g[i][j];
        }
    }

    queue<PII> q;
    q.emplace(0, 0);
    dist[0][0] = 0;
    while (q.size()) {
        auto [x, y] = q.front();
        q.pop();
        // 向上移动没有意义
        // 向下移动一格相当于相对向下移动两格
        if (not g[(x + 1) % n][y] and not g[(x + 2) % n][y] and dist[(x + 2) % n][y] > dist[x][y] + 1) {
            q.emplace((x + 2) % n, y);
            dist[(x + 2) % n][y] = dist[x][y] + 1;
        }
        // 不能向左移动
        // 向右移动一格相当于相对向下移动一格、向右移动一格
        if (y + 1 < m and not g[(x + 1) % n][y + 1] and dist[(x + 1) % n][y + 1] > dist[x][y] + 1) {
            q.emplace((x + 1) % n, y + 1);
            dist[(x + 1) % n][y + 1] = dist[x][y] + 1;
        }
    }

    // 最后一列都没有石头,所以只需要走到最后一列,必定可以走到终点
    for (int i = 0; i < n; ++i) {
        int idx = (dist[i][m - 1] + n - 1) % n; // 相对于 END 的列位置
        ans = min(ans, dist[i][m - 1] + min(abs(idx - i), n - abs(idx - i)));
    }
    cout << (ans == INF ? -1 : ans) << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}

  • 25
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值