message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a matrix a, consisting of n rows and m columns. Each cell contains an integer in it. You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be s_1, s_2, ..., s_{nm}. The traversal is k-acceptable if for all i (1 ≀ i ≀ nm - 1) |s_i - s_{i + 1}| β‰₯ k. Find the maximum integer k such that there exists some order of rows of matrix a that it produces a k-acceptable traversal. Input The first line contains two integers n and m (1 ≀ n ≀ 16, 1 ≀ m ≀ 10^4, 2 ≀ nm) β€” the number of rows and the number of columns, respectively. Each of the next n lines contains m integers (1 ≀ a_{i, j} ≀ 10^9) β€” the description of the matrix. Output Print a single integer k β€” the maximum number such that there exists some order of rows of matrix a that it produces an k-acceptable traversal. Examples Input 4 2 9 9 10 8 5 3 4 3 Output 5 Input 2 4 1 2 3 4 10 3 7 3 Output 0 Input 6 1 3 6 2 5 1 4 Output 3 Note In the first example you can rearrange rows as following to get the 5-acceptable traversal: 5 3 10 8 4 3 9 9 Then the sequence s will be [5, 10, 4, 9, 3, 8, 3, 9]. Each pair of neighbouring elements have at least k = 5 difference between them. In the second example the maximum k = 0, any order is 0-acceptable. In the third example the given order is already 3-acceptable, you can leave it as it is. Submitted Solution: ``` def loop(kr, dk): dk += 1 if dk < n - 1: for key, val in enumerate(cn[kr]): if val < i: cn[kr][key] = cn[key][kr] = 0 elif key not in nr: nr.append(key) loop(key, dk) return if nr: nr.pop() return else: for key, val in enumerate(cn[n-1]): if cn1[l][key] < i: cn1[l][key] = 0 elif key not in nr: nr.append(key) return if nr: nr.pop() return new, gf = [], [] n, m = map(int, input().split()) ord1 = [0 for i in range(n)] new = [(j, tuple(map(int, input().split()))) for j in range(n)] cn = [[0 for i in range(n)] for j in range(n)] cn1 = [[0 for i in range(n)] for j in range(n)] for i in range(n): for k in range(n): cn[i][k] = min([abs(new[i][1][j] - new[k][1][j]) for j in range(m)]) if i != k and m != 1: cn1[i][k] = min([abs(new[i][1][j] - new[k][1][j + 1]) for j in range(m - 1)]) if m == 1: cn1 = cn if n == 1: min1 = min([abs(new[0][1][j] - new[0][1][j + 1]) for j in range(m - 1)]) else: print(cn1) min_l = set(cn[i][j] for i in range(n) for j in range(n)).difference({0}) nr, min1 = [], 0 kl = sorted(list(min_l)) if m == 1: cn1 = [[kl[-1] for i in range(n)] for j in range(n)] for h, i in enumerate(kl): for l in range(n): nr = [l] loop(l, 0) if len(nr) == n: min1 = i break else: nr = [] print(min1) ```
instruction
0
47,050
12
94,100
No
output
1
47,050
12
94,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a matrix a, consisting of n rows and m columns. Each cell contains an integer in it. You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be s_1, s_2, ..., s_{nm}. The traversal is k-acceptable if for all i (1 ≀ i ≀ nm - 1) |s_i - s_{i + 1}| β‰₯ k. Find the maximum integer k such that there exists some order of rows of matrix a that it produces a k-acceptable traversal. Input The first line contains two integers n and m (1 ≀ n ≀ 16, 1 ≀ m ≀ 10^4, 2 ≀ nm) β€” the number of rows and the number of columns, respectively. Each of the next n lines contains m integers (1 ≀ a_{i, j} ≀ 10^9) β€” the description of the matrix. Output Print a single integer k β€” the maximum number such that there exists some order of rows of matrix a that it produces an k-acceptable traversal. Examples Input 4 2 9 9 10 8 5 3 4 3 Output 5 Input 2 4 1 2 3 4 10 3 7 3 Output 0 Input 6 1 3 6 2 5 1 4 Output 3 Note In the first example you can rearrange rows as following to get the 5-acceptable traversal: 5 3 10 8 4 3 9 9 Then the sequence s will be [5, 10, 4, 9, 3, 8, 3, 9]. Each pair of neighbouring elements have at least k = 5 difference between them. In the second example the maximum k = 0, any order is 0-acceptable. In the third example the given order is already 3-acceptable, you can leave it as it is. Submitted Solution: ``` def loop(kr, dk): dk += 1 if dk < n - 1: for key, val in enumerate(cn[kr]): if val < i: cn[kr][key] = cn[key][kr] = 0 elif key not in nr: nr.append(key) loop(key, dk) return if nr: nr.pop() return elif m != 1: for key, val in enumerate(cn[n-1]): if cn1[l][key] < i: cn1[l][key] = 0 elif key not in nr: nr.append(key) return if nr: nr.pop() return else: for key, val in enumerate(cn[n-1]): if cn[kr][key] < i: cn[kr][key] = 0 elif key not in nr: nr.append(key) return if nr: nr.pop() return new, gf = [], [] n, m = map(int, input().split()) ord1 = [0 for i in range(n)] new = [(j, tuple(map(int, input().split()))) for j in range(n)] cn = [[0 for i in range(n)] for j in range(n)] cn1 = [[0 for i in range(n)] for j in range(n)] for i in range(n): for k in range(n): cn[i][k] = min([abs(new[i][1][j] - new[k][1][j]) for j in range(m)]) if i != k and m != 1: cn1[i][k] = min([abs(new[i][1][j] - new[k][1][j + 1]) for j in range(m - 1)]) if m == 1 and n == 1: min1 = new[0][1][0] elif n == 1: min1 = min([abs(new[0][1][j] - new[0][1][j + 1]) for j in range(m - 1)]) else: min_l = set(cn[i][j] for i in range(n) for j in range(n)).difference({0}) nr, min1 = [], 0 kl = sorted(list(min_l)) if m == 1: cn1 = cn for h, i in enumerate(kl): for l in range(n): nr = [l] loop(l, 0) if len(nr) == n: min1 = i break else: nr = [] print(min1) ```
instruction
0
47,051
12
94,102
No
output
1
47,051
12
94,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a matrix a, consisting of n rows and m columns. Each cell contains an integer in it. You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be s_1, s_2, ..., s_{nm}. The traversal is k-acceptable if for all i (1 ≀ i ≀ nm - 1) |s_i - s_{i + 1}| β‰₯ k. Find the maximum integer k such that there exists some order of rows of matrix a that it produces a k-acceptable traversal. Input The first line contains two integers n and m (1 ≀ n ≀ 16, 1 ≀ m ≀ 10^4, 2 ≀ nm) β€” the number of rows and the number of columns, respectively. Each of the next n lines contains m integers (1 ≀ a_{i, j} ≀ 10^9) β€” the description of the matrix. Output Print a single integer k β€” the maximum number such that there exists some order of rows of matrix a that it produces an k-acceptable traversal. Examples Input 4 2 9 9 10 8 5 3 4 3 Output 5 Input 2 4 1 2 3 4 10 3 7 3 Output 0 Input 6 1 3 6 2 5 1 4 Output 3 Note In the first example you can rearrange rows as following to get the 5-acceptable traversal: 5 3 10 8 4 3 9 9 Then the sequence s will be [5, 10, 4, 9, 3, 8, 3, 9]. Each pair of neighbouring elements have at least k = 5 difference between them. In the second example the maximum k = 0, any order is 0-acceptable. In the third example the given order is already 3-acceptable, you can leave it as it is. Submitted Solution: ``` def loop(kr, dk): dk += 1 if dk < n - 1: for key, val in enumerate(cn[kr]): if val < i: cn[kr][key] = cn[key][kr] = 0 elif key not in nr: nr.append(key) loop(key, dk) return if nr: nr.pop() return elif m != 1: for key, val in enumerate(cn[kr]): if cn[kr][key] < i: cn[kr][key] = 0 elif cn1[l][key] < i: cn1[l][key] = 0 elif key not in nr: nr.append(key) return if nr: nr.pop() return else: for key, val in enumerate(cn[kr]): if cn[kr][key] < i: cn[kr][key] = 0 elif key not in nr: nr.append(key) return if nr: nr.pop() return new, gf = [], [] n, m = map(int, input().split()) ord1 = [0 for i in range(n)] new = [(j, tuple(map(int, input().split()))) for j in range(n)] cn = [[0 for i in range(n)] for j in range(n)] cn1 = [[0 for i in range(n)] for j in range(n)] for i in range(n): for k in range(n): cn[i][k] = min([abs(new[i][1][j] - new[k][1][j]) for j in range(m)]) if i != k and m != 1: cn1[i][k] = min([abs(new[i][1][j] - new[k][1][j + 1]) for j in range(m - 1)]) if m == 1 and n == 1: min1 = 0 elif n == 1: min1 = min([abs(new[0][1][j] - new[0][1][j + 1]) for j in range(m - 1)]) else: min_l = set(cn[i][j] for i in range(n) for j in range(n)).difference({0}) nr, min1 = [], 0 kl = sorted(list(min_l)) if m == 1: cn1 = cn for h, i in enumerate(kl): for l in range(n): nr = [l] loop(l, 0) if len(nr) == n: min1 = i break else: nr = [] print(min1) ```
instruction
0
47,052
12
94,104
No
output
1
47,052
12
94,105
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,053
12
94,106
Tags: constructive algorithms Correct Solution: ``` k = int(input()) a = [0 for i in range(2000)] a[0] = -1 s = k + 2000 for i in range(1, 2000): a[i] = s // 1999 + (1 if i <= s % 1999 else 0) print(2000) print(*a) ```
output
1
47,053
12
94,107
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,054
12
94,108
Tags: constructive algorithms Correct Solution: ``` k = int(input()) cSum = 0 res = [-1,1] while True: if cSum + 1_000_000 < k + len(res): cSum += 1_000_000 res.append(1_000_000) else: res.append(k + len(res) - cSum) break print(len(res)) for i in res: print(i, end = ' ') ```
output
1
47,054
12
94,109
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,055
12
94,110
Tags: constructive algorithms Correct Solution: ``` k=int(input()) for y in range(1,2000): x=y-(k%y) if (k+x)//y+x<=1000000: print(y+1) arr=[0]*(y+1) arr[y-x:-1]=[-1]*x arr[-1]=(k+x)//y+x print(*arr) exit() print(-1) ```
output
1
47,055
12
94,111
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,056
12
94,112
Tags: constructive algorithms Correct Solution: ``` k=int(input()) n=2000 s=k+n x=s//(n-1) y=s-x*(n-2) print(n) print(-1,end=' ') for i in range(n-2): print(x,end=' ') print(y) ```
output
1
47,056
12
94,113
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,057
12
94,114
Tags: constructive algorithms Correct Solution: ``` def compute(): from sys import stdin [k] = list(map(int, stdin.readline().split())) MAXX = 1000000 MAXN = 2000 o = [-1] k += 1 while k>0: k += 1 if k > MAXX: tosub = MAXX else: tosub = k o.append(tosub) k -= tosub if (len(o) > MAXN): print(-1) else: print(len(o)) print(*o) if __name__ == "__main__": compute() ```
output
1
47,057
12
94,115
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,058
12
94,116
Tags: constructive algorithms Correct Solution: ``` k = int(input()) print(2000) answer = [] answer.append(-1) S = 1999 + 1 + k while S > 1000000: answer.append(1000000) S -= 1000000 answer.append(S) while len(answer) < 2000: answer.append(0) print(*answer) ```
output
1
47,058
12
94,117
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,059
12
94,118
Tags: constructive algorithms Correct Solution: ``` from collections import defaultdict as dd import math def nn(): return int(input()) def li(): return list(input()) def mi(): return map(int, input().split()) def lm(): return list(map(int, input().split())) k=nn() goodn=0 for n in range(2,2000): if k%(n-1)<10**6 and k%(n-1)>0 : neg=n-1-(k%(n-1)) an=(k+neg)//(n-1)+neg if an<=10**6: goodn=n break if goodn==0: print(-1) else: print(goodn) answers=[0]*(n-2)+[-neg]+[an] print(*answers) ```
output
1
47,059
12
94,119
Provide tags and a correct Python 3 solution for this coding contest problem. Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≀ l ≀ r ≀ n-1} βˆ‘_{l ≀ i ≀ r} (r-l+1) β‹… a_i. In this problem, 1 ≀ n ≀ 2 000 and |a_i| ≀ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows: function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return res Also, as you can see, Alice's idea is not entirely correct. For example, suppose n = 4 and a = [6, -8, 7, -42]. Then, find_answer(n, a) would return 7, but the correct answer is 3 β‹… (6-8+7) = 15. You told Alice that her solution is incorrect, but she did not believe what you said. Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1 ≀ n ≀ 2 000 and that the absolute value of each element does not exceed 10^6. If there is no such sequence, determine so. Input The first and only line contains one integer k (1 ≀ k ≀ 10^9). Output If there is no sought sequence, print "-1". Otherwise, in the first line, print one integer n (1 ≀ n ≀ 2 000), denoting the number of elements in the sequence. Then, in the second line, print n space-separated integers: a_0, a_1, …, a_{n-1} (|a_i| ≀ 10^6). Examples Input 8 Output 4 6 -8 7 -42 Input 612 Output 7 30 -12 -99 123 -2 245 -300 Note The first sample corresponds to the example given in the problem statement. In the second sample, one answer is n = 7 with a = [30, -12, -99, 123, -2, 245, -300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
instruction
0
47,060
12
94,120
Tags: constructive algorithms Correct Solution: ``` def ceil(num,den): if(num%den == 0): return num//den else: return num//den + 1 k = int(input()) #(x - 1) * ( p - 1) = k + 1 x = ceil((k + 1), (9*10**5)) + 1 p = ceil((k + 1), (x - 1 )) + 1 diff = (x - 1)*(p - 1) - k - 1 zero = ['0' for i in range(x - 2)] gap = [str(-10**6 + 1) for i in range(10)] assert((len(zero) + 2)*(p - 1) - (diff + p) == k) print(len(zero) + 3 + len(gap)) print(*zero , '-1', p, *gap , diff + p) ```
output
1
47,060
12
94,121
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,067
12
94,134
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` n,m=map(int,input().split()) l=[] xor=0 for i in range(n): l1=list(map(int,input().split())) l.append(l1) if(i==0): xor=l1[0] else: xor=xor^l1[0] if(xor!=0): print('TAK') print('1 '*n) else: l1=[1]*n f=False for i in range(n): for j in range(m): if(l[i][j]!=l[i][0]): l1[i]=j+1 f=True break if f: break if f: print('TAK') print(*l1) else: print('NIE') ```
output
1
47,067
12
94,135
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,068
12
94,136
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` n, m = map(int, input().split()) a = [[int(i) for i in input().split()] for _ in range(n)] t = a[0][0] for i in range(1, n): t ^= a[i][0] if t != 0: print("TAK") print(' '.join('1' for i in range(n))) else: for i in range(n): for j in range(1, m): if a[i][j] != a[i][0]: print('TAK') for t in range(i): print(1, end=' ') print(j + 1, end=' ') for t in range(i + 1, n): print(1, end=' ') exit(0) print('NIE') ```
output
1
47,068
12
94,137
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,069
12
94,138
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` l=input().split() n=int(l[0]) m=int(l[1]) lfi=[] for i in range(n): l=input().split() li=[int(i) for i in l] lfi.append(li) xori=0 for i in range(n): xori=xori^lfi[i][0] if(xori!=0): print("TAK") for i in range(n): print(1,end=" ") print() quit() done=0 arr=[1 for i in range(n)] for i in range(n): for j in range(m): if(lfi[i][j]!=lfi[i][0]): done=1 arr[i]=j+1 break if(done): break if(done): print("TAK") for i in range(n): print(arr[i],end=" ") quit() print("NIE") ```
output
1
47,069
12
94,139
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,070
12
94,140
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` n,m=map(int,input().split()) sr=n bl=[] for i in range(n): a=list(map(int,input().split())) bl.append(a) if sr==n and len(set(a))>1: sr=i s=set(a) if sr!=n: print("TAK") ans=[] x=0 for i in range(n): if i!=sr: ans.append(1) x^=bl[i][0] for i in s: if i==x:continue ind=bl[sr].index(i) ans=ans[:sr]+[ind+1]+ans[sr:] break print(*ans) else: x=0 for i in range(n):x^=bl[i][0] if x: print("TAK") ans=[1]*(n) print(*ans) else:print("NIE") ```
output
1
47,070
12
94,141
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,071
12
94,142
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` # def hel(s, t): # return min(26 - abs(ord(t) - ord(s)), abs(ord(t) - ord(s))) # # # n = int(input()) # str = input() # res = float('inf') # for i in range(n-3): # res = min(res, sum([hel(str[i], 'A'), hel(str[i+1], 'C'), hel(str[i+2], 'T'), hel(str[i+3], 'G')])) # # print(res) # def can(b, n): flag = [[False] * 2 for _ in range(n+1)] flag[n][0] = True for i in range(n-1, -1, -1): flag[i][0] = (flag[i+1][0] and b[i][0] != -1) or (flag[i+1][1] and b[i][1] != -1) flag[i][1] = (flag[i+1][0] and b[i][1] != -1) or (flag[i+1][1] and b[i][0] != -1) if not flag[0][1]: return False else: print('TAK') now = 1 string = "" for i in range(n): if now == 0: if flag[i+1][0] and b[i][0] != -1: now = 0 string += str(b[i][0]+1) + " " else: now = 1 string += str(b[i][1]+1) + " " else: if flag[i+1][0] and b[i][1] != -1: now = 0 string += str(b[i][1]+1) + " " else: now = 1 string += str(b[i][0]+1) + " " print(string[:-1]) return True def ma(): n, m = [int(i) for i in input().split(' ')] grid = [] for i in range(n): grid.append([int(j) for j in input().split(' ')]) for k in range(10): b = [[-1] * 2 for _ in range(n)] for i in range(n): for j in range(m): if grid[i][j] & (1 << k): b[i][1] = j else: b[i][0] = j if can(b, n): return print('NIE') return ma() ```
output
1
47,071
12
94,143
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,072
12
94,144
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` r,c = list(map(int,input().split())) arr = [] for i in range(r): lista = list(map(int,input().split())) arr.append(lista) a = 0 ans = [1 for i in range(r)] for i in range(r): a = a^arr[i][0] flag = 0 if(a > 0): print('TAK') print(*ans) elif(a == 0): for i in range(r): for j in range(1,c): if(arr[i][0] != arr[i][j]): flag = 1 ans[i] = j+1 break if(flag == 1): break if(flag == 0): print('NIE') else: print('TAK') print(*ans) ```
output
1
47,072
12
94,145
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,073
12
94,146
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` m, n = [int(x) for x in input().split()] def is_solution(solution): from functools import reduce return reduce(lambda S, a: S ^ a[1], solution, 0) > 0 solution = [] * m multiple_elements_row = None for row in range(m): elements = [int(x) for x in input().split()] elements_dict = dict((x, i) for i, x in enumerate(elements)) solution.append((0, elements[0])) if multiple_elements_row is None and len(elements_dict) > 1: multiple_elements_row = (row, elements_dict) if multiple_elements_row is not None: row, elements_dict = multiple_elements_row while not is_solution(solution) and len(elements_dict) > 0: test_element = next(iter(elements_dict.keys())) test_element_position = elements_dict.pop(test_element) solution[row] = (test_element_position, test_element) if is_solution(solution): print("TAK") print(" ".join(str(pos + 1) for pos, _ in solution)) else: print("NIE") ```
output
1
47,073
12
94,147
Provide tags and a correct Python 3 solution for this coding contest problem. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found.
instruction
0
47,074
12
94,148
Tags: bitmasks, brute force, constructive algorithms, dp Correct Solution: ``` n, m = [int(el) for el in input().split()] matrix = list() for i in range(n): matrix.append([int(el) for el in input().split()]) base = matrix[0][0] for i in range(1, len(matrix)): base = base ^ matrix[i][0] if base != 0: print('TAK') print(' '.join([str(el) for el in [1]*n])) else: rem_j = -1 for i in range(n): cur_base = matrix[i][0] for j in range(m): if cur_base ^ matrix[i][j] != 0: rem_j = j break if rem_j != -1: output_arr = [str(el) for el in [1]*n] output_arr[i] = str(rem_j+1) print('TAK') print(' '.join(output_arr)) break if rem_j == -1: print('NIE') ```
output
1
47,074
12
94,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` n,m=map(int,input().split()) a=[] for i in range(n): a.append(list(map(int,input().split()))) x=0 c=[1]*(n-1) for i in range(n-1): x=x^a[i][0] f=0 for j in range(m): if a[n-1][j]!=x: c.append(j+1) f=1 break if f==1: print("TAK") print(*c) else: r=0 for i in range(n - 1): for j in range(1,m): if a[i][j]!=a[i][0]: f=1 r=i c[i]=(j+1) break if f==1: break if f==1: x = 0 for i in range(n - 1): if i==r: x=x^a[i][c[i]-1] else: x = x ^ a[i][0] for j in range(m): if a[n - 1][j] != x: c.append(j + 1) break print("TAK") print(*c) else: print("NIE") ```
instruction
0
47,075
12
94,150
Yes
output
1
47,075
12
94,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` R=lambda:map(int,input().split()) n,m=R() a=[[*R()]for _ in[0]*n] for i,r in enumerate(a): j=next((j for j in range(1,m)if r[j]!=r[0]),0) if j:break s=0 for r in a:s^=r[0] t=s^a[i][0]^a[i][j] r=[1]*n if s==t==0:print('NIE');exit() if t:r[i]=j+1 print('TAK',*r) ```
instruction
0
47,076
12
94,152
Yes
output
1
47,076
12
94,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` n,m=map(int,input().split()) a=[] for i in range(n): a.append(list(map(int,input().split()))) fl=-1 k=0 for i in range(n): if len(set(a[i]))>1: fl=i break x=a[0][0] for i in range(n): for j in range(m): if a[i][j]!=x: k=1 break if (k==0 and n%2==0) or (k==0 and a[0][0]==0): exit(print('NIE')) else: ans=[1]*n x=0 if fl!=-1: for i in range(n): if i!=fl: x^=a[i][0] for i in range(m): if x^a[fl][i]>0: ans[fl]=i+1 break x=0 for i in range(n): x^=a[i][ans[i]-1] if x>0: print('TAK') print(*ans) else: print('NIE') ```
instruction
0
47,077
12
94,154
Yes
output
1
47,077
12
94,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): n, m = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(n)] xor = 0 for i in a: xor ^= i[0] ans = [1] * n for i in range(n): for j in range(m): if xor ^ a[i][0] ^ a[i][j]: ans[i] = j + 1 print('TAK') print(*ans) exit() print("NIE") # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
47,078
12
94,156
Yes
output
1
47,078
12
94,157
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` #! python import operator from functools import reduce n, m = map(int, input().split()) mat = [list(map(int, input().split())) for _ in range(n)] yes = any(len(set(s)) > 1 for s in mat) xor = reduce(operator.xor, [d[0] for d in mat]) if not yes and xor == 0: yes = False index = ['1'] * n if yes: for i, row in enumerate(mat): if len(set(row)) > 1: for j, n in enumerate(row): if n != row[0]: index[i] = str(j + 1) break break print('TAK' if yes else 'NIE') if yes: print(' '.join(index)) ```
instruction
0
47,079
12
94,158
No
output
1
47,079
12
94,159
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` n,m=map(int,input().split()) a=[] for i in range(n): a.append([*map(int,input().split())]) b=a[0][0] ind=[1] for i in range(1,n): for j in range(m): if a[i][j]!=b: b=b^a[i][j] ind.append(j+1) break else: pass if len(ind)==n: print("TAK") print(*ind) else: print("NIE") ''' ans=[] tmp={} f=False for i in range(n): for j in range(m): if tmp.get(a[i][j],-1)==-1: tmp[a[i][j]]=j+1 if i>0: mm=j+1 ii=i f=True break else: pass if f: break #ans.append(tmp) #del(tmp) if f==False: print("NIE") else: print("TAK") for i in range(ii): print(1,end=" ") print(mm,end=" ") for i in range(ii,n-1): print(1,end=" ") ''' ''' ans=[] for i in range(n): tmp={} for j in range(m): if tmp.get(a[i][j],-1)==-1: tmp[a[i][j]]=j+1 else: pass ans.append(tmp) del(tmp) print(ans) ''' ''' if len(tmp)==n: print("TAK") print(*ans) else: print("NIE") ''' ```
instruction
0
47,080
12
94,160
No
output
1
47,080
12
94,161
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` # for t in range(int(input())): # s = input() # i, j = 0, 0 # cnt = 0 # ans = float('inf') # dic = {} # while j < len(s): # if len(dic) < 3: # dic[s[j]] = dic.get(s[j], 0) + 1 # # print(j) # # print(dic) # while len(dic) == 3: # ans = min(ans, j - i + 1) # dic[s[i]] -= 1 # if dic[s[i]] == 0: # del dic[s[i]] # i += 1 # # j += 1 # print((0, ans)[ans < float('inf')]) # for t in range(int(input())): # n = int(input()) # s = list(map(int, input().split())) # dp = [1] * n # for i in range(n): # k = 2 # while (i + 1) * k <= n: # j = (i + 1) * k # if s[i] < s[j - 1]: # dp[j - 1] = max(dp[j - 1], dp[i] + 1) # k += 1 # print(max(dp)) # for T in range(int(input())): # t = input() # z, o = 0, 0 # for ch in t: # z = z + 1 if ch == '0' else z # o = o + 1 if ch == '1' else o # if z > 0 and o > 0: # print('01' * len(t)) # elif o > 0 and not z: # print('1' * len(t)) # else: # print('0' * len(t)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # ans = [] # while a: # ans.append(str(a.pop(len(a) // 2))) # print(' '.join(ans)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # cnt = 0 # p = set() # l, r = 0, sum(a) # left, right = {}, {} # for i in a: # right[i] = right.get(i, 0) + 1 # for i in range(n - 1): # l += a[i] # left[a[i]] = left.get(a[i], 0) + 1 # r -= a[i] # right[a[i]] = right.get(a[i], 0) - 1 # if not right[a[i]]: # del right[a[i]] # j = n - i - 1 # if (2 + i) * (i + 1) // 2 == l and (j + 1) * j // 2 == r: # if len(left) == i + 1 and len(right) == j: # cnt += 1 # p.add((i + 1, n - i - 1)) # print(cnt) # if cnt: # for el in p: # print(*el) # for t in range(int(input())): # n = int(input()) # G = [] # taken = [False] * n # girl = -1 # for i in range(n): # g = list(map(int, input().split())) # k = g[0] # single = True # for j in range(1, k + 1): # if not taken[g[j] - 1]: # taken[g[j] - 1] = True # single = False # break # if single: # girl = i # if girl == -1: # print('OPTIMAL') # else: # print('IMPROVE') # print(girl + 1, taken.index(False) + 1) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # odd, even = [], [] # for i in range(2 * n): # if a[i] % 2: # odd.append(i + 1) # else: # even.append(i + 1) # for i in range(n - 1): # if len(odd) >= len(even): # print(odd.pop(), odd.pop()) # else: # print(even.pop(), even.pop()) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # ans, i, j = 0, 0, 1 # while j < n: # if a[i] < a[j]: # ans += 1 # i += 1 # j += 1 # else: # while j < n and a[i] == a[j]: # i += 1 # j += 1 # print(ans + 1) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # got = False # # b = 1 # while not got and b < 2 * n - 1: # if b % 2: # i, j = (b - 1) // 2, (b + 1) // 2 # else: # i, j = b // 2 - 1, b // 2 + 1 # left, right = set(a[:i]), set(a[j:]) # if left & right: # got = True # b += 1 # print('YES' if got else 'NO') # n, m, k = list(map(int, input().split())) # A = list(map(int, input().split())) # B = list(map(int, input().split())) # ans = 0 # a, b = [0], [0] # for el in A: # a.append(a[-1] + el) # for el in B: # b.append(b[-1] + el) # d = [(i, k//i) for i in range(1, int(k**0.5)+1) if k % i == 0] # d += [(j, i) for i, j in d if i != j] # for i in range(n): # for j in range(m): # for q, p in d: # if i + q <= n and j + p <= m: # if a[i + q] - a[i] == q and b[j + p] - b[j] == p: # ans += 1 # print(ans) # for t in range(int(input())): # n = int(input()) # s = input() # dic, se = {s: 1}, {s} # for k in range(2, n): # p = s[k - 1:] + (s[:k - 1], s[:k - 1][::-1])[(n % 2) == (k % 2)] # # print(k, p) # if p not in dic: # # print(dic, p) # dic[p] = k # se.add(p) # if s[::-1] not in dic: # dic[s[::-1]] = n # se.add(s[::-1]) # # print(dic) # ans = min(se) # print(ans) # print(dic[ans]) # for t in range(int(input())): # a, b, p = list(map(int, input().split())) # s = input() # road = [a if s[0] == 'A' else b] # st = [0] # for i in range(1, len(s) - 1): # if s[i] != s[i - 1]: # road.append(road[-1] + (a, b)[s[i] == 'B']) # st.append(i) # # print(road) # pay = road[-1] # j = 0 # while pay > p and j < len(st): # pay = road[-1] - road[j] # j += 1 # # print(j) # print(st[j] + 1 if j < len(st) else len(s)) # for t in range(int(input())): # n, x, y = list(map(int, input().split())) # print(max(1, min(x + y - n + 1, n)), min(n, x + y - 1)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # print(' '.join(map(str, sorted(a, reverse=True)))) # s = input() # open, close = [], [] # i = 0 # for i in range(len(s)): # if s[i] == '(': # open.append(i) # else: # close.append(i) # i, j = 0, len(close) - 1 # ans = [] # while i < len(open) and j >= 0 and open[i] < close[j]: # ans += [open[i] + 1, close[j] + 1] # i += 1 # j -= 1 # ans.sort() # print('0' if not ans else '1\n{}\n{}'.format(len(ans), ' '.join([str(idx) for idx in ans]))) import collections # n, m = list(map(int, input().split())) # a = list(input() for i in range(n)) # dic = {} # for w in a: # dic[w] = dic.get(w, 0) + 1 # l, r = '', '' # for i in range(n): # for j in range(i + 1, n): # # print(i, j, a) # if a[i] == a[j][::-1] and dic[a[i]] and dic[a[j]]: # l += a[i] # r = a[j] + r # dic[a[i]] -= 1 # dic[a[j]] -= 1 # c = '' # for k, v in dic.items(): # if v and k == k[::-1]: # if c and c[-m] == k or not c: # c += k # print(f'{len(l) + len(c) + len(r)}\n{l + c + r}') # for t in range(int(input())): # n, g, b = list(map(int, input().split())) # d = n // 2 + n % 2 # full, inc = divmod(d, g) # ans = (g + b) * (full - 1, full)[inc > 0] + (g, inc)[inc > 0] # print(ans if ans >= n else n) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # print(a[n] - a[n - 1]) # for t in range(int(input())): # n, x = list(map(int, input().split())) # s = input() # cntz = s.count('0') # total = 2 * cntz - n # bal = 0 # ans = 0 # for i in range(n): # if not total: # if bal == x: # ans = -1 # elif not abs(x - bal) % abs(total): # if (x - bal) // total >= 0: # ans += 1 # bal += 1 if s[i] == '0' else -1 # print(ans) # n = int(input()) # ans = 0 # for i in range(1, n + 1): # ans += 1 / i # print(ans) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # p, s = 0, n - 1 # for i in range(n): # if a[i] < i: # break # p = i # for i in range(n - 1, -1, -1): # if a[i] < n - i - 1: # break # s = i # print('Yes' if s <= p else 'No') # n, k = list(map(int, input().split())) # a = [input() for i in range(n)] # c = set(a) # b = set() # for i in range(n): # for j in range(i + 1, n): # third = '' # for c1, c2 in zip(a[i], a[j]): # if c1 == c2: # third += c1 # else: # if c1 != 'S' and c2 != 'S': # third += 'S' # elif c1 != 'E' and c2 != 'E': # third += 'E' # else: # third += 'T' # if third in c: # b.add(frozenset([a[i], a[j], third])) # print(len(b)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # total, curr = sum(a), 0 # ans, i, start = 'YES', 0, 0 # while ans == 'YES' and i < n: # if curr > 0: # curr += a[i] # else: # curr = a[i] # start = i # # print(curr, i, start, total) # if i - start + 1 < n and curr >= total: # ans = 'NO' # i += 1 # print(ans) # for t in range(int(input())): # n, p, k = list(map(int, input().split())) # a = list(map(int, input().split())) # a.sort(reverse=True) # odd, even = 0, 0 # i, j = len(a) - 1, len(a) - 2 # curr = 0 # while curr < p and i >= 0: # curr += a[i] # if curr <= p: # odd += 1 # i -= 2 # curr = 0 # while curr < p and j >= 0: # curr += a[j] # if curr <= p: # even += 1 # j -= 2 # print(max(odd * 2 - 1, even * 2)) # for t in range(int(input())): # s, c = input().split() # s = list(ch for ch in s) # sor = sorted(s) # for i in range(len(s)): # if s[i] != sor[i]: # j = max(j for j, v in enumerate(s[i:], i) if v == sor[i]) # s = s[:i] + [s[j]] + s[i + 1:j] + [s[i]] + s[j + 1:] # break # s = ''.join(s) # print(s if s < c else '---') # for t in range(int(input())): # n, s = list(map(int, input().split())) # a = list(map(int, input().split())) # if sum(a) <= s: # print(0) # else: # curr, i, j = 0, 0, 0 # for i in range(n): # if a[i] > a[j]: # j = i # s -= a[i] # if s < 0: # break # print(j + 1) # for t in range(int(input())): # a, b = list(map(int, input().split())) # a, b = (b, a) if b > a else (a, b) # if not ((1 + 8 * (a - b))**0.5 - 1) % 2 and ((1 + 8 * (a - b))**0.5 - 1) // 2 >= 0: # ans = ((1 + 8 * (a - b))**0.5 - 1) // 2 # print(int(ans)) # else: # n1 = int(((1 + 8 * (a - b))**0.5 - 1) // 2) + 1 # while (n1 * (n1 + 1) // 2) % 2 != (a - b) % 2: # n1 += 1 # print(n1) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # ans = 0 # l = 0 # dic = {} # for i in range(n - 1, -1, -1): # if not a[i] % 2: # l, r = 0, 30 # while l < r: # m = (l + r) // 2 # # print(l, r, m, a[i] % 2**m) # if a[i] % 2**m: # r = m # else: # l = m + 1 # dic[a[i] // 2**(l - 1)] = max(dic.get(a[i] // 2**(l - 1), 0), l - 1) # print(sum(list(dic.values()))) # n = int(input()) # s = input() # b = s.count('B') # w = n - b # if b % 2 and w % 2: # print(-1) # elif not b or not w: # print(0) # else: # ans = [] # if not b % 2: # for i in range(n - 1): # if s[i] != 'W': # ans += [str(i + 1)] # s = s[:i] + 'W' + 'BW'[s[i + 1] == 'B'] + s[i + 2:] # elif not w % 2: # for i in range(n - 1): # if s[i] != 'B': # ans += [str(i + 1)] # s = s[:i] + 'B' + 'WB'[s[i + 1] == 'W'] + s[i + 2:] # print(len(ans)) # print(' '.join(ans)) # n, m = list(map(int, input().split())) # a = list(map(int, input().split())) # b = list(map(int, input().split())) # b.sort() # ans = float('inf') # for i in range(n): # x = (b[0] - a[i]) % m # ax = [] # for j in range(n): # ax.append((a[j] + x) % m) # if b == sorted(ax): # ans = min(ans, x) # print(ans) # for t in range(int(input())): # n = int(input()) # ans = [1] + [0] * (n - 1) # p = list(map(int, input().split())) # i, j, curr, m = p.index(1), 1, 1, 1 # l, r = i, i # while l >= 0 and r < n: # if l and curr + p[l - 1] == (m + 2) * (m + 1) // 2: # ans[m] = 1 # curr += p[l - 1] # l -= 1 # # elif r + 1 < n and curr + p[r + 1] == (m + 2) * (m + 1) // 2: # ans[m] = 1 # curr += p[r + 1] # r += 1 # else: # if l and r + 1 < n: # curr, l, r = ((curr + p[l - 1], l - 1, r), # (curr + p[r + 1], l, r + 1))[p[r + 1] < p[l - 1]] # elif not l and r + 1 < n: # curr, l, r = curr + p[r + 1], l, r + 1 # elif r + 1 == n and l: # curr, l, r = curr + p[l - 1], l - 1, r # else: # break # m += 1 # print(''.join([str(i) for i in ans])) # for t in range(int(input())): # n = int(input()) # p = [input() for i in range(n)] # ans = 0 # for i in range(n): # if p[i] in p[i + 1:]: # for j in range(10): # code = p[i][:3] + str(j) # if code not in p: # p[i] = code # ans += 1 # break # print(ans) # for code in p: # print(code) # for t in range(int(input())): # a, b = list(map(int, input().split())) # if (a + b) % 3 == 0 and 2 * min(a, b) >= max(a, b): # print('YES') # else: # print('NO') # for t in range(int(input())): # x, y = list(map(int, input().split())) # if (x == 1 and y > 1) or (x == 2 and y > 3) or (x == 3 and y > 3): # print('NO') # else: # print('YES') # for t in range(int(input())): # n, m = list(map(int, input().split())) # a = list(map(int, input().split())) # if m < n or n == 2: # print(-1) # elif m == n: # print(2 * sum(a)) # for i in range(n - 1): # print(i + 1, i + 2) # print(n, 1) # else: # b = [(a[i], i + 1) for i in range(n)] # b.sort() # d = m - n # ans = sum(a) + d * (b[0][0] + b[1][0]) # for i in range(d): # print(b[0][1], b[1][1]) # for i in range(n - 1): # print(i + 1, i + 2) # print(n, 1) # n = int(input()) # a = list(map(int, input().split())) # if n % 2: # print(-1) # else: # d = 0 # c = [] # curr = 0 # came = set() # day = set() # inc = False # for i in range(n): # if a[i] > 0: # if a[i] in day: # inc = True # break # else: # day.add(a[i]) # came.add(a[i]) # else: # if abs(a[i]) not in came: # inc = True # break # else: # came.remove(abs(a[i])) # if len(came) == 0: # d += 1 # c.append(i + 1) # day = set() # if len(came) > 0: # inc = True # if inc: # print(-1) # else: # print(d) # print(c[0]) # for i in range(1, len(c)): # print(c[i] - c[i - 1]) # n = int(input()) # a = list(map(int, input().split())) # a.sort() # x, y = sum(a[:n // 2])**2, sum(a[n // 2:])**2 # print(x + y) # for t in range(int(input())): # n = int(input()) # r, p, s = list(map(int, input().split())) # b = input() # S, P, R = b.count('S'), b.count('P'), b.count('R') # cnt = 0 # ans = '' # # print(r, 'rock', p, 'paper', s, 'sc') # for i in range(n): # if b[i] == 'S': # if r > 0: # ans, r, cnt = ans + 'R', r - 1, cnt + 1 # else: # if p > R: # ans, p = ans + 'P', p - 1 # if len(ans) < i + 1 and s > P: # ans, s = ans + 'S', s - 1 # S -= 1 # elif b[i] == 'P': # if s > 0: # ans, s, cnt = ans + 'S', s - 1, cnt + 1 # else: # if p > R: # ans, p = ans + 'P', p - 1 # if len(ans) < i + 1 and r > S: # ans, r = ans + 'R', r - 1 # P -= 1 # else: # if p > 0: # ans, p, cnt = ans + 'P', p - 1, cnt + 1 # else: # if s > P: # ans, s = ans + 'S', s - 1 # if len(ans) < i + 1 and r > S: # ans, r = ans + 'R', r - 1 # R -= 1 # if cnt < (n // 2 + n % 2): # print('NO') # else: # print('YES') # # print(r, p, s) # print(ans) # for t in range(int(input())): # n = int(input()) # s = input() # f, l = s.find('1'), s.rfind('1') # f, l = max(f + 1, n - f) if f != -1 else 0, max(l + 1, n - l) if l != -1 else 0 # if not f and not l: # print(n) # else: # print(f * 2) if f > l else print(l * 2) # t = int(input()) # ans = list() # for _ in [0] * t: # n, r = map(int, input().split()) # x = sorted(set(map(int, input().split())))[::-1] # ans.append(sum([x - i * r > 0 for i, x in enumerate(x)])) # print(' '.join(map(str, ans))) # n = int(input()) # dots = [] # for i in range(n): # dots.append(sum(list(map(int, input().split())))) # print(max(dots)) # n, m = map(int, input().split()) # print(pow(2**m - 1, n, 10**9 + 7)) # n, k = map(int, input().split()) # s = input() # if not k: # print(s) # elif n == 1: # print('0') # else: # s = [int(i) for i in s] # if s[0] > 1: # s[0], k = 1, k - 1 # for i in range(1, n): # if not k: # break # if s[i] > 0: # s[i], k = 0, k - 1 # print(''.join(map(str, s))) if len(s) > 1 else print('0') # m, n = map(int, input().split()) # r = list(map(int, input().split())) # c = list(map(int, input().split())) # grid = [['ok'] * (n + 1) for i in range(m + 1)] # # for i in range(m): # row = r[i] # if row: # for j in range(row): # grid[i][j] = 1 # grid[i][row] = 0 # else: # grid[i][row] = 0 # # # inv = False # for j in range(n): # col = c[j] # if col: # for i in range(col): # if grid[i][j] == 0: # inv = True # break # else: # grid[i][j] = 1 # if grid[col][j] == 1: # inv = True # break # else: # grid[col][j] = 0 # else: # if grid[col][j] == 1: # inv = True # break # else: # grid[col][j] = 0 # # if inv: # print(0) # else: # cnt = 0 # for row in grid[:m]: # cnt += row[:n].count('ok') # print(pow(2, cnt, 10**9 + 7)) # n = int(input()) # for i in range(n): # print('BW' * (n // 2) + 'B' * (n % 2) if i % 2 else 'WB' * (n // 2) + 'W' * (n % 2)) # n = int(input()) # a = list(map(int, input().split())) # curr, odd, even = 0, 0, 0 # p = 0 # for d in a: # odd, even = ((odd, even + 1), (odd + 1, even))[curr % 2] # curr += 1 if d < 0 else 0 # p += odd if curr % 2 else even # print(n * (n + 1) // 2 - p, p) # n = int(input()) # a = list(map(int, input().split())) # p, m, z = 0, [], 0 # for d in a: # if d > 0: # p += d # elif d < 0: # m.append(d) # else: # z += 1 # ans = p - (n - z - len(m)) # # if len(m) % 2: # if z: # m.append(-1) # ans += 1 # z -= 1 # else: # m.sort(reverse=True) # x = m.pop() # ans += 1 - x # # mm = len(m) # ans += abs(sum(m)) - mm # ans += z # print(ans) # n, l, r = map(int, input().split()) # a = [2**i for i in range(r)] # print(sum(a[:l]) + n - l, sum(a) + (n - r) * a[-1]) # n = int(input()) # a = list(map(int, input().split())) # a.sort() # print('YES' if not sum(a) % 2 and a[-1] <= sum(a) - a[-1] else 'NO') # for t in range(int(input())): # n, m, k = map(int, input().split()) # h = list(map(int, input().split())) # ans = 'YES' # for i in range(n - 1): # if abs(h[i] - h[i + 1]) > k: # d = h[i] - h[i + 1] # if d < 0 and m >= abs(d) - k: # m -= -k - d # elif d > 0: # m += min(d + k, h[i]) # else: # ans = 'NO' # break # else: # d = h[i] - h[i + 1] # if d >= 0: # m += min(d + k, h[i]) # else: # m += min(k + d, h[i]) # print(ans) # h, l = map(int, input().split()) # print((h**2 + l**2) / (2 * h) - h) # n = int(input()) # a = list(map(int, input().split())) # a = {i: j for i, j in enumerate(a)} # m, idx = 0, 0 # ans = 'YES' # for i in range(n): # if a[i] > m: # m = a[i] # idx = i # for i in range(1, idx): # if a[i] < a[i - 1]: # ans = 'NO' # break # for i in range(idx + 1, n): # if a[i] > a[i - 1]: # ans = 'NO' # break # print(ans) # n, k = map(int, input().split()) # l, r = 0, n # while l <= r: # m = (l + r) // 2 # t = n - m # if m * (m + 1) // 2 - t == k: # ans = t # break # elif m * (m + 1) // 2 - t < k: # l = m + 1 # else: # r = m - 1 # print(ans) # for t in range(int(input())): # n, m = map(int, input().split()) # grid = [input() for i in range(n)] # rows = [] # cols = [0] * m # for row in grid: # rows.append(0) # for i in range(m): # if row[i] == '.': # rows[-1] += 1 # cols[i] += 1 # ans = m + n - 1 # for i in range(n): # for j in range(m): # ans = min(ans, rows[i] + cols[j] - (grid[i][j] == '.')) # print(ans) # tiles = input().split() # unique = {} # m, p, s = set(), set(), set() # m_unique = 0 # for t in tiles: # unique[t] = unique.get(t, 0) + 1 # m_unique = max(m_unique, unique[t]) # if t[1] == 'm': # m.add(int(t[0])) # elif t[1] == 'p': # p.add(int(t[0])) # else: # s.add(int(t[0])) # ans = 3 - m_unique # for t in (m, p, s): # if not t: # continue # else: # m_sub = 0 # l = list(sorted(t)) # dif = [] # for i in range(1, len(t)): # dif.append(l[i] - l[i - 1]) # if dif[-1] == 1 or dif[-1] == 2: # m_sub = max(m_sub, 2) # if i > 1 and dif[-1] == dif[-2] == 1: # m_sub = 3 # # print(l, dif, m_sub) # ans = min(ans, 3 - m_sub) # print(ans) # n = int(input()) # a = list(map(int, input().split())) # a.sort() # print('NO' if a[-3] + a[-2] <= a[-1] else 'YES') # if a[-3] + a[-2] > a[-1]: # print(' '.join(str(i) for i in a[:-3] + [a[-3]] + [a[-1]] + [a[-2]])) # n = int(input()) # s = input() # m = int(input()) # dic = {} # for i in range(n): # dic.setdefault(s[i], []).append(i) # for t in range(m): # name = [ch for ch in input()] # c = {} # ans = 0 # # print(t) # for i in range(len(name)): # idx = c.get(name[i], -1) # # print(name[i], dic[name[i]], c) # c[name[i]] = idx + 1 # ans = max(ans, dic[name[i]][idx + 1]) # print(ans + 1) # for t in range(int(input())): # a = input() # b = input() # if len(b) < len(a): # print('NO') # else: # ans = 'YES' # i, j = 0, 0 # while i < len(a): # cnt1 = 1 # while i + 1 < len(a) and a[i] == a[i + 1]: # cnt1 += 1 # i += 1 # cnt2 = 0 # while j < len(b) and b[j] == a[i]: # cnt2 += 1 # j += 1 # if cnt1 > cnt2: # ans = 'NO' # break # i += 1 # print(ans if i == len(a) and j == len(b) else 'NO') # m, n = map(int, input().split()) # g = [input() for i in range(m)] # ans = 'YES' # if m < 3 and n < 3: # ans = 'NO' # if ans == 'YES': # c = (-1, -1) # for i in range(1, m - 1): # for j in range(1, n - 1): # if g[i][j] == '*': # if all(g[I][J] == '*' for I, J in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1))): # c = (i, j) # break # if c == (-1, -1): # ans = 'NO' # # if ans == 'YES': # plus = {c} # I, J = c # i, j = I - 1, I + 1 # while i >= 0 and g[i][J] == '*': # plus.add((i, J)) # i -= 1 # while j < m and g[j][J] == '*': # plus.add((j, J)) # j += 1 # i, j = J - 1, J + 1 # while i >= 0 and g[I][i] == '*': # plus.add((I, i)) # i -= 1 # while j < n and g[I][j] == '*': # plus.add((I, j)) # j += 1 # # for i in range(m): # for j in range(n): # if g[i][j] == '*' and (i, j) not in plus: # ans = 'NO' # break # # print(ans) # for t in range(int(input())): # n = int(input()) # if not n % 2: # print(f'{n // 2} {n // 2}') # else: # idx = 1 # for i in range(3, int(n**0.5) + 1): # if not n % i: # idx = i # break # print(f'{n // idx} {n - n // idx}' if idx > 1 else f'{1} {n - 1}') # l = int(input()) # n = input() # i = l // 2 # if not l % 2 and n[i] != '0': # print(int(n[:i]) + int(n[i:])) # else: # j = i + 1 # while n[i] == '0': # i -= 1 # while j < l and n[j] == '0': # j += 1 # if i > 0 and j < l - 1: # print(min(int(n[:i]) + int(n[i:]), int(n[:j]) + int(n[j:]))) # else: # if not i: # print(int(n[:j]) + int(n[j:])) # else: # print(int(n[:i]) + int(n[i:])) # n = int(input()) # a = list(map(int, input().split())) # b = a.copy() # for i in range(n): # if a[i] >= 0: # a[i] = (a[i] + 1) * -1 # if not n % 2: # print(' '.join((str(i) for i in a))) # else: # m = (float('inf'), -1) # for i in range(n): # m = (m, (a[i], i))[m[0] > a[i] and a[i] < -1] # if m[1] == -1: # b[0] = 0 # print(' '.join((str(i) for i in b))) # else: # a[m[1]] = (a[m[1]] + 1) * -1 # print(' '.join((str(i) for i in a))) # l = int(input()) # ans = 0 # c = [input() for i in range(l)] # stack = [1] # for i in range(l): # if c[i] == 'add': # ans += stack[-1] # elif c[i] == 'end': # stack.pop() # else: # stack.append(min(stack[-1] * int(c[i].split()[1]), 2**32)) # if ans >= 2**32: # ans = 'OVERFLOW!!!' # break # print(ans) # n = int(input()) # a = list(map(int, input().split())) # odd = 0 # for d in a: # if d % 2: # odd += 1 # if 0 < odd < n: # print(' '.join((str(i) for i in sorted(a)))) # else: # print(' '.join((str(i) for i in a))) # n = int(input()) # print(n // 2 + 1) # i, j = 1, 1 # for k in range(n): # print(i, j) # if i == j: # j += 1 # else: # i += 1 # n, m = map(int, input().split()) # a = [list(map(int, input().split())) for i in range(m)] # x, y = a.pop() # ans = 'NO' # for v in (x, y): # val = [0] * (n + 1) # cnt = 0 # for v1, v2 in a: # if v1 != v and v2 != v: # val[v1], val[v2], cnt = val[v1] + 1, val[v2] + 1, cnt + 1 # if max(val) == cnt: # ans = 'YES' # break # print(ans) # k = int(input()) # idx = 1 # for j in range(5, int(k**0.5) + 1): # if not k % j: # idx = j # break # # if idx < 5 or k // idx < 5: # print('-1') # else: # pattern = 'aeiou' + 'aeiou'[:max(0, idx - 5)] # ans = '' # for i in range(k // idx): # j = i % len(pattern) # ans += pattern[j:] + pattern[:j] # print(ans) # n = int(input()) # u = list(map(int, input().split())) # g, cnt = [0] * 10, 0 # ans = 0 # for i in range(n): # g[u[i] - 1] += 1 # unique = set(g) # unique.discard(0) # if len(unique) == 1: # k = unique.pop() # if k == 1: # ans = i # elif g.count(k) == 1: # ans = i # elif len(unique) == 2: # if 1 in unique and g.count(1) == 1: # ans = i # else: # a, b = unique # if a < b: # b, a = a, b # if a - b == 1 and g.count(a) == 1: # ans = i # print(ans + 1) # m, n = map(int, input().split()) # a = [list(map(int, input().split())) for i in range(m)] # b = [list(map(int, input().split())) for j in range(m)] # ans = 'Possible' # for i in range(m): # j = 0 # a[i][0], b[i][0] = ((a[i][j], b[i][j]), (b[i][j], a[i][j]))[a[i][j] > b[i][j]] # if i and (a[i][j] <= a[i - 1][j] or b[i][j] <= b[i - 1][j]): # ans = 'Impossible' # break # if ans == 'Possible': # for j in range(n): # i = 0 # a[i][j], b[i][j] = ((a[i][j], b[i][j]), (b[i][j], a[i][j]))[a[i][j] > b[i][j]] # if j and (a[i][j] <= a[i][j - 1] or b[i][j] <= b[i][j - 1]): # ans = 'Impossible' # break # # if ans == 'Possible': # for i in range(1, m): # if ans == 'Possible': # for j in range(1, n): # a[i][j], b[i][j] = ((a[i][j], b[i][j]), (b[i][j], a[i][j]))[a[i][j] > b[i][j]] # if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: # ans = 'Impossible' # break # if b[i][j] <= b[i - 1][j] or b[i][j] <= b[i][j - 1]: # ans = 'Impossible' # break # print(ans) # n = int(input()) # a = list(map(int, input().split())) # ans = float('inf') # for i in range(n): # ans = min(ans, a[i] // max(i, n - i - 1)) # print(ans) # import string # alpha = string.ascii_lowercase # for t in range(int(input())): # s = input() # a = [ord(ch) - ord('a') for ch in s] # a.sort() # odd, even = [], [] # for ch in a: # if ch % 2: # odd.append(ch) # else: # even.append(ch) # if odd and even: # b = odd + even if abs(odd[-1] - even[0]) > 1 else even + odd # else: # b = (odd, even)[not len(odd)] # ans = '' # for i in range(1, len(b)): # if abs(b[i] - b[i - 1]) == 1: # ans = 'No answer' # break # if ans == '': # for i in range(len(b)): # ans += alpha[b[i]] # print(ans) # n = int(input()) # s = input() # eight = s.count('8') # ans = 'YES' # if (n - 11) // 2 >= eight or eight == 0: # ans = 'NO' # if ans == 'YES': # cnt, e, i = 0, 0, 0 # while e <= (n - 11) // 2: # if s[i] == '8': # e += 1 # else: # cnt += 1 # i += 1 # if cnt > (n - 11) // 2: # ans = 'NO' # print(ans) # m, n, h = map(int, input().split()) # front = list(map(int, input().split())) # n # left = list(map(int, input().split())) # m # up = [list(map(int, input().split())) for i in range(m)] # for i in range(m): # for j in range(n): # if up[i][j]: # up[i][j] = min(left[i], front[j]) # for row in up: # print(' '.join((str(i) for i in row))) # x = int(input()) # c, ans = 0, [] # for i in range(40): # if x == (2**len(format(x, 'b')) - 1): # break # if i % 2: # x += 1 # else: # ans.append(len(format(x, 'b'))) # x = x ^ (2**ans[-1] - 1) # c += 1 # print(c) # if c: # print(' '.join((str(i) for i in ans))) m, n = map(int, input().split()) a = [list(map(int, input().split())) for i in range(m)] b = a[0][0] ans = [0] * m for i in range(1, m): b = b ^ a[i][0] for i in range(m): for j in range(n): if b ^ a[i][0] ^ a[i][j] != 0: ans[i] = j print('TAK') print(' '.join((str(i + 1) for i in ans))) break print('NIE') ```
instruction
0
47,081
12
94,162
No
output
1
47,081
12
94,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Student Dima from Kremland has a matrix a of size n Γ— m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …, c_n (1 ≀ c_j ≀ m) so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column. Here x βŠ• y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y. Input The first line contains two integers n and m (1 ≀ n, m ≀ 500) β€” the number of rows and the number of columns in the matrix a. Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 ≀ a_{i, j} ≀ 1023). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, … c_n (1 ≀ c_j ≀ m), so that the inequality a_{1, c_1} βŠ• a_{2, c_2} βŠ• … βŠ• a_{n, c_n} > 0 holds. If there is more than one possible answer, you may output any. Examples Input 3 2 0 0 0 0 0 0 Output NIE Input 2 3 7 7 7 7 7 10 Output TAK 1 3 Note In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 βŠ• 10 = 13, 13 is more than 0, so the answer is found. Submitted Solution: ``` if __name__ == "__main__": MAXN = 2000 n, m = map(int, input().split()) a = [] for i in range(n): row = [int(x) for x in input().split()] a.append(row) can_sum = [[0] * MAXN for _ in range(n)] for elem in a[0]: can_sum[0][elem] = 1 for i in range(1, n): for s in range(MAXN): for j in range(len(a[i])): try: can_sum[i][s] |= can_sum[i-1][s^a[i][j]] except: print(i, j, a[i][j]) print(s, s ^ a[i][j]) exit() for s in range(1, MAXN): if can_sum[n-1][s] == 1: ans = []; su = s for i in reversed(range(n)): if i == 0: for j in range(len(a[0])): if a[0][j] == su: ans.append(j) break break for j in range(len(a[i])): if can_sum[i-1][su^a[i][j]] == 1: ans.append(j) su ^= a[i][j] break print("TAK") for i in reversed(ans): print(i + 1, end=" ") exit() print("NIE") ```
instruction
0
47,082
12
94,164
No
output
1
47,082
12
94,165
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,100
12
94,200
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` import sys input = sys.stdin.readline n,q=map(int,input().split()) A=list(map(int,input().split())) if max(A)==min(A): print(0) sys.exit() L=max(A) MM=[[200005,-1,i] for i in range(L+1)] COUNT=[0]*(L+1) for i in range(n): a=A[i] MM[a][0]=min(MM[a][0],i) MM[a][1]=max(MM[a][1],i) COUNT[a]+=1 MM.sort() i,j,k=MM[0] MAX=j CC=COUNT[k] MAXC=COUNT[k] ANS=0 for i,j,k in MM[1:]: if i==200005: ANS+=CC-MAXC break if MAX<i: ANS+=CC-MAXC MAX=j CC=COUNT[k] MAXC=COUNT[k] else: CC+=COUNT[k] MAX=max(MAX,j) MAXC=max(MAXC,COUNT[k]) print(ANS) ```
output
1
47,100
12
94,201
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,101
12
94,202
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` import sys input=sys.stdin.buffer.readline def find(x): if(par[x]==x): return x par[x]=find(par[x]) return par[x] def union(a,b): xa=find(a) xb=find(b) if(size[xa]>size[xb]): xa,xb=xb,xa par[xa]=xb size[xb]+=size[xa] n,m=map(int,input().split()) a=list(map(int,input().split())) par=[i for i in range(max(a)+1)] size=[1 for i in range(max(a)+1)] counted=[0 for i in range(max(a)+1)] for i in range(n): counted[a[i]]+=1 first=[0 for i in range(max(a)+1)] last=[0 for i in range(max(a)+1)] for i in range(n): if(first[a[i]]==0): first[a[i]]=i for i in range(n-1,-1,-1): if(last[a[i]]==0): last[a[i]]=i count=0 spread=0 counted1=counted[::] for i in range(n): if(count>0): union(a[i],a[i-1]) spread=max(spread,last[a[i]]) counted1[a[i]]-=1 if(spread==last[a[i]] and counted1[a[i]]==0): count=0 else: count+=1 maxid=[0 for i in range(max(a)+1)] for i in range(1,max(a)+1): par[i]=find(par[i]) for i in range(1,max(a)+1): maxid[par[i]]=max(maxid[par[i]],counted[i]) diff=0 for i in range(max(a)+1): diff+=maxid[i] print(n-diff) ```
output
1
47,101
12
94,203
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,102
12
94,204
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` def naiveSolve(): return from collections import Counter def main(): indexes=[[] for _ in range(200001)] n,q=readIntArr() a=readIntArr() for i,x in enumerate(a): indexes[x].append(i) # break a up into groups where all elements in a group is not found in # another group groups=[] # store (left, right) left=0 currMaxIdx=0 for i,x in enumerate(a): currMaxIdx=max(currMaxIdx,indexes[x][-1]) if currMaxIdx==i: groups.append((left,i)) currMaxIdx=i+1 left=i+1 ans=0 for left,right in groups: cnt=Counter() for i in range(left,right+1): cnt[a[i]]+=1 ans+=(right-left+1)-max(cnt.values()) print(ans) return import sys input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) # input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] # def readFloatArr(): # return [float(x) for x in input().split()] def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m]) dv=defaultValFactory;da=dimensionArr if len(da)==1:return [dv() for _ in range(da[0])] else:return [makeArr(dv,da[1:]) for _ in range(da[0])] def queryInteractive(r): print('? {}'.format(r)) sys.stdout.flush() return readIntArr() def answerInteractive(adj,n): print('!') for u in range(1,n+1): for v in adj[u]: if v>u: print('{} {}'.format(u,v)) sys.stdout.flush() inf=float('inf') MOD=10**9+7 # MOD=998244353 from math import gcd,floor,ceil # from math import floor,ceil # for Python2 for _abc in range(1): main() ```
output
1
47,102
12
94,205
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,103
12
94,206
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` import itertools import sys if __name__ == '__main__': n, q = map(int, input().split()) blocks = list(map(int, input().split())) # create empty dictionary - unordered collection dif = dict() for i in range(n): if blocks[i] in dif: dif[blocks[i]][1] = i dif[blocks[i]][2] = dif[blocks[i]][2] + 1 elif blocks[i] not in dif: dif[blocks[i]] = [i, i, 1] rez = 0 end = -1 maxi = -1 for i in range(n): if dif[blocks[i]][1] >= end: end = dif[blocks[i]][1] if dif[blocks[i]][2] >= maxi: maxi = dif[blocks[i]][2] if i == end: rez = rez + maxi maxi = 0 rez = (-1) * rez for i in dif: rez = rez + dif[i][2] print(rez) ```
output
1
47,103
12
94,207
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,104
12
94,208
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict #threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase #sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=300006, func=lambda a, b: min(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b:max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] <=key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False class Trie: def __init__(self): self.root = self.getNode() def getNode(self): return TrieNode() def _charToIndex(self, ch): return ord(ch) - ord('a') def insert(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: pCrawl.children[index] = self.getNode() pCrawl = pCrawl.children[index] pCrawl.isEndOfWord = True def search(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: return False pCrawl = pCrawl.children[index] return pCrawl != None and pCrawl.isEndOfWord #-----------------------------------------trie--------------------------------- class Node: def __init__(self, data): self.data = data self.count=0 self.left = None # left node for 0 self.right = None # right node for 1 class BinaryTrie: def __init__(self): self.root = Node(0) def insert(self, pre_xor): self.temp = self.root for i in range(31, -1, -1): val = pre_xor & (1 << i) if val: if not self.temp.right: self.temp.right = Node(0) self.temp = self.temp.right self.temp.count+=1 if not val: if not self.temp.left: self.temp.left = Node(0) self.temp = self.temp.left self.temp.count += 1 self.temp.data = pre_xor def query(self, xor): self.temp = self.root for i in range(31, -1, -1): val = xor & (1 << i) if not val: if self.temp.left and self.temp.left.count>0: self.temp = self.temp.left elif self.temp.right: self.temp = self.temp.right else: if self.temp.right and self.temp.right.count>0: self.temp = self.temp.right elif self.temp.left: self.temp = self.temp.left self.temp.count-=1 return xor ^ self.temp.data #-------------------------bin trie------------------------------------------- n,k=map(int,input().split()) l=list(map(int,input().split())) la=defaultdict(int) for i in range(n): la[l[i]]=i i=0 ans=0 c=defaultdict(int) while(i<n): st=i last=la[l[st]] ma=0 while(st<=last): c[l[st]]+=1 last=max(last,la[l[st]]) st+=1 ma=max(ma,c[l[st-1]]) ans+=last-i+1-ma i=last+1 print(ans) ```
output
1
47,104
12
94,209
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,105
12
94,210
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` n, q = map(int, input().split()) A = list(map(int, input().split())) left = {} right = {} for i in range(n): if A[i] not in left: left[A[i]] = i right[A[i]] = i E = [] for elem in left: E.append([left[elem], -1]) E.append([right[elem], 1]) E.sort() u = 0 b = 0 cntr = {} ans = 0 for i in range(n): while u < len(E) and E[u][0] == i: b -= E[u][1] u += 1 if A[i] not in cntr: cntr[A[i]] = 0 cntr[A[i]] += 1 if b == 0: s = 0 m = 0 for iss in cntr: s += cntr[iss] m = max(m, cntr[iss]) ans += s - m cntr = {} print(ans) ```
output
1
47,105
12
94,211
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,106
12
94,212
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` n, q = map(int,input().split()) A = list(map(int,input().split())) sizes = dict() for j in range(n): if A[j] in sizes: sizes[A[j]][2] += 1 sizes[A[j]][1] = j else: sizes[A[j]] = [j,j,1] #print(sizes) answer = 0 end = -1 max_size = -1 for j in range(n): end = max(end, sizes[A[j]][1]) max_size = max(max_size, sizes[A[j]][2]) if j == end: answer+= max_size #print(j, max_size) max_size = 0 answer = -answer for j in sizes: answer += sizes[j][2] print(answer) ```
output
1
47,106
12
94,213
Provide tags and a correct Python 3 solution for this coding contest problem. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
instruction
0
47,107
12
94,214
Tags: data structures, dsu, greedy, implementation, two pointers Correct Solution: ``` import io, os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline ii=lambda:int(input()) kk=lambda:map(int,input().split()) ll=lambda:list(kk()) n,_=kk() ls = ll() cnt, maxi = {}, {} for i,l in enumerate(ls): if l not in cnt: cnt[l]=0 cnt[l]+=1 maxi[l]=i nxt = su = 0 while nxt < n: l = nxt r = maxi[ls[l]] maxc = cnt[ls[l]] while l < r: l+=1 if maxi[ls[l]] > r: r = maxi[ls[l]] if cnt[ls[l]]>maxc:maxc=cnt[ls[l]] su+=r-nxt-maxc+1 nxt=r+1 print(su) ```
output
1
47,107
12
94,215
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` from sys import stdin n,m=map(int,stdin.readline().strip().split()) s=list(map(int,stdin.readline().strip().split())) mx=200000 arr=[-1 for i in range(mx+1)] visited=[False for i in range(mx+1)] cnt=[0 for i in range(mx+1)] for i in range(n): arr[s[i]]=i x=0 ans=0 inf=mx+30 while x<n: ind=arr[s[x]] v=[] l=x while x<=ind and x<n: ind=max(arr[s[x]],ind) v.append(s[x]) cnt[s[x]]+=1 x+=1 aux=0 for i in v: aux=max(aux,cnt[i]) ans+=(x-l-aux) print(ans) ```
instruction
0
47,108
12
94,216
Yes
output
1
47,108
12
94,217
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): n = I() a = LI() a.sort() f = [1]*n p = 0 ans = 0 while p < n: while p < n and not f[p]: p += 1 if p == n: break ans += 1 for i in range(n): if a[i]%a[p] == 0: f[i] = 0 print(ans) return #B def B(): n = I() s = list(map(int, input())) g = LIR(n) ans = sum(s) for t in range(30000): for i in range(n): ai,bi = g[i] if t < bi: continue if (t-bi)%ai == 0: s[i] ^= 1 su = sum(s) if ans < su: ans = su print(ans) return #C def C(): t = I() for _ in range(t): n = I() s = list(map(int, input())) mi = [s[-1]] for i in range(n-1)[::-1]: mi.append(min(mi[-1],s[i])) mi = mi[::-1] ans = [None]*n for i in range(n): if mi[i] == s[i]: ans[i] = 1 else: ans[i] = 2 q = [s[i] for i in range(n) if ans[i] > 1] p = [q[i] for i in range(len(q))] p.sort() if p == q: for i in ans: print(i,end = "") print() else: print("-") return #D def D(): def root(x): if x == par[x]: return x par[x] = root(par[x]) return par[x] def unite(x,y): x = root(x) y = root(y) if rank[x] < rank[y]: par[x] = y else: par[y] = x if rank[x] == rank[y]: rank[x] += 1 n,k = LI() par = [i for i in range(n)] rank = [0]*n for i in range(k): x,y = LI() x -= 1 y -= 1 if root(x) != root(y): unite(x,y) size = [0]*n for i in range(n): size[root(i)] += 1 ans = 0 for i in size: if i > 0: ans += i-1 print(k-ans) return #E def E(): t = I() for _ in range(t): n,m = LI() s = LIR(n) s = [[s[i][j] for i in range(n)] for j in range(m)] if n <= m: ma = [max(s[i]) for i in range(m)] ma.sort(reverse = True) print(sum(ma[:n])) else: ans = 0 k = [[]] for _ in range(m): k_ = [] for i in range(n): k_ += [x+[i] for x in k] k = [x for x in k_] for l in k: s_ = [[s[i][(j+l[i])%n] for j in range(n)] for i in range(m)] print(l) p = sum([max([s_[i][j] for i in range(m)]) for j in range(n)]) print(s_,p) if ans < p: ans = p print(ans) return #F def F(): return #G def G(): def root(x): if x == par[x]: return x par[x] = root(par[x]) return par[x] def unite(x,y): x = root(x) y = root(y) if rank[x] < rank[y]: par[x] = y else: par[y] = x if rank[x] == rank[y]: rank[x] += 1 m = 200000 par = [i for i in range(m)] rank = [0]*m n,q = LI() a = LI() for i in range(n): a[i] -= 1 count = defaultdict(lambda : 0) l = defaultdict(lambda : 0) lis = [] for i in range(n): ai = a[i] if count[ai] == 0: l[ai] = i lis.append(ai) count[ai] += 1 f = defaultdict(lambda : 0) r = defaultdict(lambda : 0) for i in range(n)[::-1]: ai = a[i] if not f[ai]: r[ai] = i f[ai] = 1 f = [0]*n for i in lis: li,ri = l[i],r[i] f[li] += 1 f[ri] -= 1 for i in range(n-1): if f[i] > 0: x,y = a[i], a[i+1] if root(x) != root(y): unite(x,y) f[i+1] += f[i] size = defaultdict(lambda : []) for i in l: size[root(i)].append(count[i]) ans = 0 for i in size.values(): ans += sum(i)-max(i) print(ans) return #H def H(): return #Solve if __name__ == "__main__": G() ```
instruction
0
47,109
12
94,218
Yes
output
1
47,109
12
94,219
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` n, q = map(int, input().split()) a = list(map(int, input().split())) d = {} def max_frequent(s, e, a): d = {} for x in a[s: e+1]: if x not in d: d[x] = 0 d[x] += 1 return e - s + 1 - max(list(d.values())) for i, x in enumerate(a): if x not in d: d[x] = [] d[x].append(i) segment = [[v[0], v[-1]] for v in d.values()] end = -1 start = -1 block = [] for s, e in segment: if s > end: if end != -1: block.append([start, end]) start = s end = e if e > end: end = e block.append([start, end]) cnt = 0 for s, e in block: cnt += max_frequent(s, e, a) print(cnt) ```
instruction
0
47,110
12
94,220
Yes
output
1
47,110
12
94,221
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` MAXN = 200100 n, q = map(int, input().split()) a = list(map(int, input().split())) lpos = [-1]*MAXN for i in range(n): lpos[a[i]] = i need = 0 i = 0 while i < n: start = i r = lpos[a[i]] j = i + 1 while j < r: r = max(r, lpos[a[j]]) j += 1 cnts = {} while i <= r: if a[i] in cnts: cnts[a[i]] += 1 else: cnts[a[i]] = 1 i += 1 best = 0 for k, v in cnts.items(): best = max(best, v) need += i - start - best print(need) ```
instruction
0
47,111
12
94,222
Yes
output
1
47,111
12
94,223
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` import collections import itertools n, _ = map(int, input().split()) a = list(map(int, input().split())) index = [set() for i in range(200005)] for i in range(n): index[a[i]].add(i) ans = 0 for i in a: if len(index[i]) == 0: continue if max(index[i]) - min(index[i]) == len(index[i]) - 1: continue else: lack = set([i for i in range(min(index[i]), max(index[i])+1)]) - set(index[i]) use = set() for j in lack: if j not in use: use |= set(index[a[j]]) index[a[j]] = [] if len(use) > 0: maxuse = max(use) else: maxuse = 0 index[i] |= use ans += len(use) print(ans) ```
instruction
0
47,112
12
94,224
No
output
1
47,112
12
94,225
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): n = I() a = LI() a.sort() f = [1]*n p = 0 ans = 0 while p < n: while p < n and not f[p]: p += 1 if p == n: break ans += 1 for i in range(n): if a[i]%a[p] == 0: f[i] = 0 print(ans) return #B def B(): n = I() s = list(map(int, input())) g = LIR(n) ans = sum(s) for t in range(30000): for i in range(n): ai,bi = g[i] if t < bi: continue if (t-bi)%ai == 0: s[i] ^= 1 su = sum(s) if ans < su: ans = su print(ans) return #C def C(): t = I() for _ in range(t): n = I() s = list(map(int, input())) mi = [s[-1]] for i in range(n-1)[::-1]: mi.append(min(mi[-1],s[i])) mi = mi[::-1] ans = [None]*n for i in range(n): if mi[i] == s[i]: ans[i] = 1 else: ans[i] = 2 q = [s[i] for i in range(n) if ans[i] > 1] p = [q[i] for i in range(len(q))] p.sort() if p == q: for i in ans: print(i,end = "") print() else: print("-") return #D def D(): def root(x): if x == par[x]: return x par[x] = root(par[x]) return par[x] def unite(x,y): x = root(x) y = root(y) if rank[x] < rank[y]: par[x] = y else: par[y] = x if rank[x] == rank[y]: rank[x] += 1 n,k = LI() par = [i for i in range(n)] rank = [0]*n for i in range(k): x,y = LI() x -= 1 y -= 1 if root(x) != root(y): unite(x,y) size = [0]*n for i in range(n): size[root(i)] += 1 ans = 0 for i in size: if i > 0: ans += i-1 print(k-ans) return #E def E(): t = I() for _ in range(t): n,m = LI() s = LIR(n) s = [[s[i][j] for i in range(n)] for j in range(m)] if n <= m: ma = [max(s[i]) for i in range(m)] ma.sort(reverse = True) print(sum(ma[:n])) else: ans = 0 k = [[]] for _ in range(m): k_ = [] for i in range(n): k_ += [x+[i] for x in k] k = [x for x in k_] for l in k: s_ = [[s[i][(j+l[i])%n] for j in range(n)] for i in range(m)] print(l) p = sum([max([s_[i][j] for i in range(m)]) for j in range(n)]) print(s_,p) if ans < p: ans = p print(ans) return #F def F(): return #G def G(): def root(x): if x == par[x]: return x par[x] = root(par[x]) return par[x] def unite(x,y): x = root(x) y = root(y) if rank[x] < rank[y]: par[x] = y else: par[y] = x if rank[x] == rank[y]: rank[x] += 1 m = 200000 par = [i for i in range(m)] rank = [0]*m n,q = LI() a = LI() q = deque() count = defaultdict(lambda : 0) l = [] for i in range(n): ai = a[i]-1 if count[ai] == 0: l.append(ai) count[ai] += 1 q.append(ai) continue count[ai] += 1 while q and q[-1] != ai: x = q.pop() if root(x) != root(ai): unite(x,ai) q.append(ai) size = defaultdict(lambda : []) for i in l: size[root(i)].append(count[i]) ans = 0 for i in size.values(): ans += sum(i)-max(i) print(ans) return #H def H(): return #Solve if __name__ == "__main__": G() ```
instruction
0
47,113
12
94,226
No
output
1
47,113
12
94,227
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` import io, os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline ii=lambda:int(input()) kk=lambda:map(int,input().split()) ll=lambda:list(kk()) n,_=kk() ls = ll() cnt, maxi = {}, {} for i,l in enumerate(ls): if l not in cnt: cnt[l]=0 cnt[l]+=1 maxi[l]=i nxt = su = 0 while nxt < n: l = nxt r = maxi[ls[l]] maxc = cnt[ls[i]] while l < r: l+=1 if maxi[ls[l]] > r: r = maxi[ls[l]] if cnt[ls[l]]>maxc:maxc=cnt[ls[l]] su+=r-nxt-maxc+1 nxt=r+1 print(su) exit() ```
instruction
0
47,114
12
94,228
No
output
1
47,114
12
94,229
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value. You are given a sequence of integers a_1, a_2, …, a_n and q updates. Each update is of form "i x" β€” change a_i to x. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. Input The first line contains integers n and q (1 ≀ n ≀ 200 000, q = 0), the length of the sequence and the number of the updates. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 200 000), the initial sequence. Each of the following q lines contains integers i_t and x_t (1 ≀ i_t ≀ n, 1 ≀ x_t ≀ 200 000), the position and the new value for this position. Output Print q+1 integers, the answer for the initial sequence and the answer after every update. Examples Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4 Submitted Solution: ``` """ NTC here """ from sys import setcheckinterval, stdin, setrecursionlimit setcheckinterval(1000) setrecursionlimit(10**7) # print("Case #{}: {} {}".format(i, n + m, n * m)) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) n,q=lin() a=lin() dc={} for i in a: try: dc[i]+=1 except: dc[i]=1 changed={i:i for i in dc} curr={i:0 for i in dc} curr[a[0]]+=1 ans=0 for i in range(1,n): if changed[a[i]]!=changed[a[i-1]] and dc[changed[a[i-1]]]-curr[changed[a[i-1]]]>0 : if dc[changed[a[i]]]>dc[changed[a[i-1]]]: ans+=dc[changed[a[i-1]]] dc[changed[a[i]]]+=dc[changed[a[i-1]]] changed[changed[a[i-1]]]=changed[a[i]] else: ans+=dc[changed[a[i]]] dc[changed[a[i-1]]]+=dc[changed[a[i]]] changed[changed[a[i]]]=changed[a[i-1]] curr[a[i]]+=1 print(ans) ```
instruction
0
47,115
12
94,230
No
output
1
47,115
12
94,231
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n integers a_1, a_2, ..., a_n, such that for each 1≀ i ≀ n holds i-n≀ a_i≀ i-1. Find some nonempty subset of these integers, whose sum is equal to 0. It can be shown that such a subset exists under given constraints. If there are several possible subsets with zero-sum, you can find any of them. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^6). The description of the test cases follows. The first line of each test case contains a single integer n (1≀ n ≀ 10^6). The second line of each test case contains n integers a_1, a_2, ..., a_n (i-n ≀ a_i ≀ i-1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, output two lines. In the first line, output s (1≀ s ≀ n) β€” the number of elements in your subset. In the second line, output s integers i_1, i_2, ..., i_s (1≀ i_k ≀ n). All integers have to be pairwise different, and a_{i_1} + a_{i_2} + ... + a_{i_s} has to be equal to 0. If there are several possible subsets with zero-sum, you can find any of them. Example Input 2 5 0 1 2 3 4 4 -3 1 1 1 Output 1 1 4 1 4 3 2 Note In the first example, we get sum is a_1 = 0. In the second example, we get sum is a_1 + a_4 + a_3 + a_2 = 0.
instruction
0
47,132
12
94,264
Tags: constructive algorithms, dfs and similar, graphs, math Correct Solution: ``` import sys input = sys.stdin.readline t=int(input()) for test in range(t): n=int(input()) A=list(map(int,input().split())) ANS=[] SET=set() NOW=1 while not (NOW in SET): ANS.append(NOW) SET.add(NOW) NOW=NOW-A[NOW-1] x=ANS.index(NOW) print(len(ANS[x:])) print(*ANS[x:]) ```
output
1
47,132
12
94,265
Provide tags and a correct Python 3 solution for this coding contest problem. We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≀ t_i ≀ n-i+1), remove a_{t_i} from the array, and append one of the numbers a_{t_i-1} or a_{t_i+1} (if t_i-1 or t_i+1 are within the array bounds) to the right end of the array b. Then we move elements a_{t_i+1}, …, a_n to the left in order to fill in the empty space. You are given the initial permutation a_1, a_2, …, a_n and the resulting array b_1, b_2, …, b_k. All elements of an array b are distinct. Calculate the number of possible sequences of indices t_1, t_2, …, t_k modulo 998 244 353. Input Each test contains multiple test cases. The first line contains an integer t (1 ≀ t ≀ 100 000), denoting the number of test cases, followed by a description of the test cases. The first line of each test case contains two integers n, k (1 ≀ k < n ≀ 200 000): sizes of arrays a and b. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n): elements of a. All elements of a are distinct. The third line of each test case contains k integers b_1, b_2, …, b_k (1 ≀ b_i ≀ n): elements of b. All elements of b are distinct. The sum of all n among all test cases is guaranteed to not exceed 200 000. Output For each test case print one integer: the number of possible sequences modulo 998 244 353. Example Input 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 Output 2 0 4 Note \require{cancel} Let's denote as a_1 a_2 … \cancel{a_i} \underline{a_{i+1}} … a_n β†’ a_1 a_2 … a_{i-1} a_{i+1} … a_{n-1} an operation over an element with index i: removal of element a_i from array a and appending element a_{i+1} to array b. In the first example test, the following two options can be used to produce the given array b: * 1 2 \underline{3} \cancel{4} 5 β†’ 1 \underline{2} \cancel{3} 5 β†’ 1 \cancel{2} \underline{5} β†’ 1 2; (t_1, t_2, t_3) = (4, 3, 2); * 1 2 \underline{3} \cancel{4} 5 β†’ \cancel{1} \underline{2} 3 5 β†’ 2 \cancel{3} \underline{5} β†’ 1 5; (t_1, t_2, t_3) = (4, 1, 2). In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 4, namely number 3, which means that it couldn't be added to array b on the second step. In the third example test, there are four options to achieve the given array b: * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ \cancel{1} \underline{4} 3 2 5 β†’ 4 3 \cancel{2} \underline{5} β†’ 4 3 5; * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ 1 \underline{4} \cancel{3} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ \cancel{1} \underline{4} 7 2 5 β†’ 4 7 \cancel{2} \underline{5} β†’ 4 7 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ 1 \underline{4} \cancel{7} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5;
instruction
0
47,205
12
94,410
Tags: combinatorics, data structures, dsu, greedy, implementation Correct Solution: ``` import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) r = [0] * n for i in range(n): r[a[i] - 1] = i b = list(map(int, input().split())) d = [0] * n for i in range(k): d[b[i] - 1] = 1 cnt = 0 for i in range(k): idx = r[b[i] - 1] t = 0 if idx > 0 and d[a[idx - 1] - 1] == 0: t += 1 if idx < n - 1 and d[a[idx + 1] - 1] == 0: t += 1 if t == 0: print(0) break elif t == 2: cnt += 1 d[b[i] - 1] = 0 else: print(pow(2, cnt, 998244353)) ```
output
1
47,205
12
94,411
Provide tags and a correct Python 3 solution for this coding contest problem. We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≀ t_i ≀ n-i+1), remove a_{t_i} from the array, and append one of the numbers a_{t_i-1} or a_{t_i+1} (if t_i-1 or t_i+1 are within the array bounds) to the right end of the array b. Then we move elements a_{t_i+1}, …, a_n to the left in order to fill in the empty space. You are given the initial permutation a_1, a_2, …, a_n and the resulting array b_1, b_2, …, b_k. All elements of an array b are distinct. Calculate the number of possible sequences of indices t_1, t_2, …, t_k modulo 998 244 353. Input Each test contains multiple test cases. The first line contains an integer t (1 ≀ t ≀ 100 000), denoting the number of test cases, followed by a description of the test cases. The first line of each test case contains two integers n, k (1 ≀ k < n ≀ 200 000): sizes of arrays a and b. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n): elements of a. All elements of a are distinct. The third line of each test case contains k integers b_1, b_2, …, b_k (1 ≀ b_i ≀ n): elements of b. All elements of b are distinct. The sum of all n among all test cases is guaranteed to not exceed 200 000. Output For each test case print one integer: the number of possible sequences modulo 998 244 353. Example Input 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 Output 2 0 4 Note \require{cancel} Let's denote as a_1 a_2 … \cancel{a_i} \underline{a_{i+1}} … a_n β†’ a_1 a_2 … a_{i-1} a_{i+1} … a_{n-1} an operation over an element with index i: removal of element a_i from array a and appending element a_{i+1} to array b. In the first example test, the following two options can be used to produce the given array b: * 1 2 \underline{3} \cancel{4} 5 β†’ 1 \underline{2} \cancel{3} 5 β†’ 1 \cancel{2} \underline{5} β†’ 1 2; (t_1, t_2, t_3) = (4, 3, 2); * 1 2 \underline{3} \cancel{4} 5 β†’ \cancel{1} \underline{2} 3 5 β†’ 2 \cancel{3} \underline{5} β†’ 1 5; (t_1, t_2, t_3) = (4, 1, 2). In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 4, namely number 3, which means that it couldn't be added to array b on the second step. In the third example test, there are four options to achieve the given array b: * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ \cancel{1} \underline{4} 3 2 5 β†’ 4 3 \cancel{2} \underline{5} β†’ 4 3 5; * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ 1 \underline{4} \cancel{3} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ \cancel{1} \underline{4} 7 2 5 β†’ 4 7 \cancel{2} \underline{5} β†’ 4 7 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ 1 \underline{4} \cancel{7} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5;
instruction
0
47,206
12
94,412
Tags: combinatorics, data structures, dsu, greedy, implementation Correct Solution: ``` import sys input = sys.stdin.readline MOD = 998244353 def main(): n, k = map(int, input().split()) alst = list(map(int, input().split())) blst = list(map(int, input().split())) ans = 1 dic = {} for i, a in enumerate(alst, 1): dic[a] = i for i in range(k): blst[i] = dic[blst[i]] - 1 tf = [True for _ in range(n)] ans = 1 for b in blst[::-1]: if b == 0: if not tf[1]: print(0) return elif b == n - 1: if not tf[n - 2]: print(0) return else: cnt = int(tf[b + 1]) + int(tf[b - 1]) if cnt == 2: ans *= 2 ans %= MOD elif cnt == 0: print(0) return tf[b] = False print(ans) for _ in range(int(input())): main() ```
output
1
47,206
12
94,413
Provide tags and a correct Python 3 solution for this coding contest problem. We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≀ t_i ≀ n-i+1), remove a_{t_i} from the array, and append one of the numbers a_{t_i-1} or a_{t_i+1} (if t_i-1 or t_i+1 are within the array bounds) to the right end of the array b. Then we move elements a_{t_i+1}, …, a_n to the left in order to fill in the empty space. You are given the initial permutation a_1, a_2, …, a_n and the resulting array b_1, b_2, …, b_k. All elements of an array b are distinct. Calculate the number of possible sequences of indices t_1, t_2, …, t_k modulo 998 244 353. Input Each test contains multiple test cases. The first line contains an integer t (1 ≀ t ≀ 100 000), denoting the number of test cases, followed by a description of the test cases. The first line of each test case contains two integers n, k (1 ≀ k < n ≀ 200 000): sizes of arrays a and b. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n): elements of a. All elements of a are distinct. The third line of each test case contains k integers b_1, b_2, …, b_k (1 ≀ b_i ≀ n): elements of b. All elements of b are distinct. The sum of all n among all test cases is guaranteed to not exceed 200 000. Output For each test case print one integer: the number of possible sequences modulo 998 244 353. Example Input 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 Output 2 0 4 Note \require{cancel} Let's denote as a_1 a_2 … \cancel{a_i} \underline{a_{i+1}} … a_n β†’ a_1 a_2 … a_{i-1} a_{i+1} … a_{n-1} an operation over an element with index i: removal of element a_i from array a and appending element a_{i+1} to array b. In the first example test, the following two options can be used to produce the given array b: * 1 2 \underline{3} \cancel{4} 5 β†’ 1 \underline{2} \cancel{3} 5 β†’ 1 \cancel{2} \underline{5} β†’ 1 2; (t_1, t_2, t_3) = (4, 3, 2); * 1 2 \underline{3} \cancel{4} 5 β†’ \cancel{1} \underline{2} 3 5 β†’ 2 \cancel{3} \underline{5} β†’ 1 5; (t_1, t_2, t_3) = (4, 1, 2). In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 4, namely number 3, which means that it couldn't be added to array b on the second step. In the third example test, there are four options to achieve the given array b: * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ \cancel{1} \underline{4} 3 2 5 β†’ 4 3 \cancel{2} \underline{5} β†’ 4 3 5; * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ 1 \underline{4} \cancel{3} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ \cancel{1} \underline{4} 7 2 5 β†’ 4 7 \cancel{2} \underline{5} β†’ 4 7 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ 1 \underline{4} \cancel{7} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5;
instruction
0
47,207
12
94,414
Tags: combinatorics, data structures, dsu, greedy, implementation Correct Solution: ``` import sys,io,os;Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline Y=lambda:[*map(int,Z().split())] M=998244353;N=200001 A=[0]*(2*N);B=[0]*N o=[] for _ in range(int(Z())): n,k=Y();a=Y();b=Y();c=1 for i in range(n):B[i+1]=0 for i in b:B[i]=1 for i in range(n): A[2*a[i]]=a[i-1] if i else -1 A[2*a[i]+1]=a[i+1] if i<n-1 else -1 for i in b: l=2;r=0 if A[2*i]<0 or B[A[2*i]]:l-=1;r=1 if A[2*i+1]<0 or B[A[2*i+1]]:l-=1 if l:c=(c*l)%M else:c=0;break B[i]=0 v=A[2*A[2*i+r]+r] if v>0:A[2*v+1-r]=i A[2*i+r]=v o.append(str(c)) print('\n'.join(o)) ```
output
1
47,207
12
94,415
Provide tags and a correct Python 3 solution for this coding contest problem. We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≀ t_i ≀ n-i+1), remove a_{t_i} from the array, and append one of the numbers a_{t_i-1} or a_{t_i+1} (if t_i-1 or t_i+1 are within the array bounds) to the right end of the array b. Then we move elements a_{t_i+1}, …, a_n to the left in order to fill in the empty space. You are given the initial permutation a_1, a_2, …, a_n and the resulting array b_1, b_2, …, b_k. All elements of an array b are distinct. Calculate the number of possible sequences of indices t_1, t_2, …, t_k modulo 998 244 353. Input Each test contains multiple test cases. The first line contains an integer t (1 ≀ t ≀ 100 000), denoting the number of test cases, followed by a description of the test cases. The first line of each test case contains two integers n, k (1 ≀ k < n ≀ 200 000): sizes of arrays a and b. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n): elements of a. All elements of a are distinct. The third line of each test case contains k integers b_1, b_2, …, b_k (1 ≀ b_i ≀ n): elements of b. All elements of b are distinct. The sum of all n among all test cases is guaranteed to not exceed 200 000. Output For each test case print one integer: the number of possible sequences modulo 998 244 353. Example Input 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 Output 2 0 4 Note \require{cancel} Let's denote as a_1 a_2 … \cancel{a_i} \underline{a_{i+1}} … a_n β†’ a_1 a_2 … a_{i-1} a_{i+1} … a_{n-1} an operation over an element with index i: removal of element a_i from array a and appending element a_{i+1} to array b. In the first example test, the following two options can be used to produce the given array b: * 1 2 \underline{3} \cancel{4} 5 β†’ 1 \underline{2} \cancel{3} 5 β†’ 1 \cancel{2} \underline{5} β†’ 1 2; (t_1, t_2, t_3) = (4, 3, 2); * 1 2 \underline{3} \cancel{4} 5 β†’ \cancel{1} \underline{2} 3 5 β†’ 2 \cancel{3} \underline{5} β†’ 1 5; (t_1, t_2, t_3) = (4, 1, 2). In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 4, namely number 3, which means that it couldn't be added to array b on the second step. In the third example test, there are four options to achieve the given array b: * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ \cancel{1} \underline{4} 3 2 5 β†’ 4 3 \cancel{2} \underline{5} β†’ 4 3 5; * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ 1 \underline{4} \cancel{3} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ \cancel{1} \underline{4} 7 2 5 β†’ 4 7 \cancel{2} \underline{5} β†’ 4 7 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ 1 \underline{4} \cancel{7} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5;
instruction
0
47,208
12
94,416
Tags: combinatorics, data structures, dsu, greedy, implementation Correct Solution: ``` from sys import stdin, stdout def identify_the_operations(n, k, a_a, b_a): MOD = 998244353 idx_a = [0] * (n+1) bit_a = [0] * (n+1) res = 1 for i in range(n): idx_a[a_a[i]] = i bit_update(bit_a, i, 1) b_s = set(b_a) for b in b_a: b_s.remove(b) cv = bit_query(bit_a, idx_a[b]) h = right_bs(idx_a[b] + 1, n - 1, cv, bit_a) l = left_bs(0, idx_a[b] - 1, cv, bit_a) if l != -1 and a_a[l] in b_s: l = -1 if h != -1 and a_a[h] in b_s: h = -1 mul = 0 if h == -1 and l == -1: return 0 elif h == -1: mul = 1 bit_update(bit_a, l, -1) elif l == -1: mul = 1 bit_update(bit_a, h, -1) else: mul = 2 bit_update(bit_a, h, -1) res *= mul res %= MOD return res def right_bs(l, h, cv, bit_a): if l > h or cv == bit_query(bit_a, h): return -1 while l < h: m = (l + h) // 2 if bit_query(bit_a, m) != cv: h = m else: l = m + 1 return h def left_bs(l, h, cv, bit_a): if l > h or cv == bit_query(bit_a, l): return -1 while l < h: m = (l + h + 1) // 2 if bit_query(bit_a, m) != cv: l = m else: h = m - 1 return l def bit_update(bit_a, i, delta): i += 1 while i < len(bit_a): bit_a[i] += delta i += (i & -i) def bit_query(bit_a, i): i += 1 r = 0 while i > 0: r += bit_a[i] i -= (i & -i) return r t = int(stdin.readline()) for _ in range(t): n, k = map(int, stdin.readline().split()) a_a = list(map(int, stdin.readline().split())) b_a = list(map(int, stdin.readline().split())) res = identify_the_operations(n, k, a_a, b_a) stdout.write(str(res) + '\n') ```
output
1
47,208
12
94,417
Provide tags and a correct Python 3 solution for this coding contest problem. We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≀ t_i ≀ n-i+1), remove a_{t_i} from the array, and append one of the numbers a_{t_i-1} or a_{t_i+1} (if t_i-1 or t_i+1 are within the array bounds) to the right end of the array b. Then we move elements a_{t_i+1}, …, a_n to the left in order to fill in the empty space. You are given the initial permutation a_1, a_2, …, a_n and the resulting array b_1, b_2, …, b_k. All elements of an array b are distinct. Calculate the number of possible sequences of indices t_1, t_2, …, t_k modulo 998 244 353. Input Each test contains multiple test cases. The first line contains an integer t (1 ≀ t ≀ 100 000), denoting the number of test cases, followed by a description of the test cases. The first line of each test case contains two integers n, k (1 ≀ k < n ≀ 200 000): sizes of arrays a and b. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n): elements of a. All elements of a are distinct. The third line of each test case contains k integers b_1, b_2, …, b_k (1 ≀ b_i ≀ n): elements of b. All elements of b are distinct. The sum of all n among all test cases is guaranteed to not exceed 200 000. Output For each test case print one integer: the number of possible sequences modulo 998 244 353. Example Input 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 Output 2 0 4 Note \require{cancel} Let's denote as a_1 a_2 … \cancel{a_i} \underline{a_{i+1}} … a_n β†’ a_1 a_2 … a_{i-1} a_{i+1} … a_{n-1} an operation over an element with index i: removal of element a_i from array a and appending element a_{i+1} to array b. In the first example test, the following two options can be used to produce the given array b: * 1 2 \underline{3} \cancel{4} 5 β†’ 1 \underline{2} \cancel{3} 5 β†’ 1 \cancel{2} \underline{5} β†’ 1 2; (t_1, t_2, t_3) = (4, 3, 2); * 1 2 \underline{3} \cancel{4} 5 β†’ \cancel{1} \underline{2} 3 5 β†’ 2 \cancel{3} \underline{5} β†’ 1 5; (t_1, t_2, t_3) = (4, 1, 2). In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 4, namely number 3, which means that it couldn't be added to array b on the second step. In the third example test, there are four options to achieve the given array b: * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ \cancel{1} \underline{4} 3 2 5 β†’ 4 3 \cancel{2} \underline{5} β†’ 4 3 5; * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ 1 \underline{4} \cancel{3} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ \cancel{1} \underline{4} 7 2 5 β†’ 4 7 \cancel{2} \underline{5} β†’ 4 7 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ 1 \underline{4} \cancel{7} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5;
instruction
0
47,209
12
94,418
Tags: combinatorics, data structures, dsu, greedy, implementation Correct Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/12/20 9:10 # @Author : zx # @File : 1442B.py # @Email : zx081325@163.com T = int(input()) for _ in range(T): res = 1 n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) m = {} for i in range(len(b)): m[b[i]] = i + 1 for i in range(len(a)): if a[i] in m.keys(): temp = 0 if i > 0 and ((a[i - 1] in m.keys() and m[a[i]] > m[a[i - 1]]) or a[i - 1] not in m.keys()): temp += 1 if i < n - 1 and ((a[i + 1] in m.keys() and m[a[i]] > m[a[i + 1]]) or a[i + 1] not in m.keys()): temp += 1 res *= temp print(res % 998244353) ```
output
1
47,209
12
94,419
Provide tags and a correct Python 3 solution for this coding contest problem. We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≀ t_i ≀ n-i+1), remove a_{t_i} from the array, and append one of the numbers a_{t_i-1} or a_{t_i+1} (if t_i-1 or t_i+1 are within the array bounds) to the right end of the array b. Then we move elements a_{t_i+1}, …, a_n to the left in order to fill in the empty space. You are given the initial permutation a_1, a_2, …, a_n and the resulting array b_1, b_2, …, b_k. All elements of an array b are distinct. Calculate the number of possible sequences of indices t_1, t_2, …, t_k modulo 998 244 353. Input Each test contains multiple test cases. The first line contains an integer t (1 ≀ t ≀ 100 000), denoting the number of test cases, followed by a description of the test cases. The first line of each test case contains two integers n, k (1 ≀ k < n ≀ 200 000): sizes of arrays a and b. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n): elements of a. All elements of a are distinct. The third line of each test case contains k integers b_1, b_2, …, b_k (1 ≀ b_i ≀ n): elements of b. All elements of b are distinct. The sum of all n among all test cases is guaranteed to not exceed 200 000. Output For each test case print one integer: the number of possible sequences modulo 998 244 353. Example Input 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 Output 2 0 4 Note \require{cancel} Let's denote as a_1 a_2 … \cancel{a_i} \underline{a_{i+1}} … a_n β†’ a_1 a_2 … a_{i-1} a_{i+1} … a_{n-1} an operation over an element with index i: removal of element a_i from array a and appending element a_{i+1} to array b. In the first example test, the following two options can be used to produce the given array b: * 1 2 \underline{3} \cancel{4} 5 β†’ 1 \underline{2} \cancel{3} 5 β†’ 1 \cancel{2} \underline{5} β†’ 1 2; (t_1, t_2, t_3) = (4, 3, 2); * 1 2 \underline{3} \cancel{4} 5 β†’ \cancel{1} \underline{2} 3 5 β†’ 2 \cancel{3} \underline{5} β†’ 1 5; (t_1, t_2, t_3) = (4, 1, 2). In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 4, namely number 3, which means that it couldn't be added to array b on the second step. In the third example test, there are four options to achieve the given array b: * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ \cancel{1} \underline{4} 3 2 5 β†’ 4 3 \cancel{2} \underline{5} β†’ 4 3 5; * 1 4 \cancel{7} \underline{3} 6 2 5 β†’ 1 4 3 \cancel{6} \underline{2} 5 β†’ 1 \underline{4} \cancel{3} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ \cancel{1} \underline{4} 7 2 5 β†’ 4 7 \cancel{2} \underline{5} β†’ 4 7 5; * 1 4 7 \underline{3} \cancel{6} 2 5 β†’ 1 4 7 \cancel{3} \underline{2} 5 β†’ 1 \underline{4} \cancel{7} 2 5 β†’ 1 4 \cancel{2} \underline{5} β†’ 1 4 5;
instruction
0
47,210
12
94,420
Tags: combinatorics, data structures, dsu, greedy, implementation Correct Solution: ``` import sys input=sys.stdin.readline mod=998244353 t=int(input()) for _ in range(t): n,k=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) pos={} used=set() tmp=set(b) for i in range(n): if a[i] not in tmp: used.add(a[i]) pos[a[i]]=i ans=1 for val in b: i=pos[val] if i==0: if a[i+1] not in used: print(0) break elif i==n-1: if a[i-1] not in used: print(0) break else: if a[i-1] in used and a[i+1] in used: ans*=2 ans%=mod elif a[i-1] not in used and a[i+1] not in used: print(0) break used.add(a[i]) else: print(ans) ```
output
1
47,210
12
94,421