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 |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,659 | 12 | 81,318 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
# n * (n-1)
# (k-2)!
mod = 1000000007
for i in range(int(input())):
n = int(input())
d = list(map(int, input().split()))
s = d[0]
for j in range(1, len(d)):
s &= d[j]
c = 0
for j in d:
c += (j == s)
if c < 2:
print(0)
else:
fc = 1
for j in range(2, n-1):
fc *= j
fc %= mod
print(c*(c-1)*fc%mod)
``` | output | 1 | 40,659 | 12 | 81,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,660 | 12 | 81,320 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
from functools import reduce
from sys import stdin
input = stdin.readline
def main():
MOD = int(1e9 + 7)
test = int(input())
for _ in range(test):
n = int(input())
ara = [int(x) for x in input().split()]
all_and = reduce((lambda x, y : x & y), ara)
count = 0
for num in ara:
if num == all_and:
count += 1
ans = (count * (count - 1)) % MOD
for i in range(n - 2):
ans = (ans * (i + 1)) % MOD
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 40,660 | 12 | 81,321 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,661 | 12 | 81,322 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
import sys,os,io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def ncr(n, r, p):
# initialize numerator
# and denominator
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
for _ in range (int(input())):
n = int(input())
a = [int(i) for i in input().split()]
an = a[0]
for i in a:
an &= i
cnt = a.count(an)
if cnt<=1:
print(0)
continue
ans = 2
mod = 10**9 + 7
for i in range (1,n-1):
ans *= i
ans %= mod
ans *= ncr(cnt,2,mod)
ans %= mod
print(ans)
``` | output | 1 | 40,661 | 12 | 81,323 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,662 | 12 | 81,324 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
import sys
input=sys.stdin.readline
rem=1000000007
for _ in range(int(input())):
n=int(input())
ar=list(map(int,input().split()))
ma=max(ar)
req=[]
st=1
while(st<=ma):
count=0
for i in range(n):
if(ar[i]&st):
count+=1
if(count!=n and count!=0):
req.append(st)
st<<=1
mai=0
for i in range(n):
flag=True
for j in req:
if(ar[i]&j):
flag=False
if(flag):
mai+=1
fac=n-2
ans=(mai*(mai-1))//2
ans=ans%rem
ans=(ans*2)%rem
for i in range(1,fac+1):
ans=(ans*i)%rem
print(ans)
``` | output | 1 | 40,662 | 12 | 81,325 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,663 | 12 | 81,326 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
t= int(input())
for test in range(t):
n = int(input())
a = list(map(int,input().split()))
k=a[0]
for x in a:
k&=x
r=a.count(k)
r*=r-1
for i in range(2,len(a)-1):
r=r*i%(10**9+7)
print(r)
``` | output | 1 | 40,663 | 12 | 81,327 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,664 | 12 | 81,328 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
import math, sys
input = sys.stdin.readline
for _ in range(int(input())):
n, values = int(input()), [int(i) for i in input().split()]
low, cnt, ans = min(values), values.count(min(values)), float("inf")
for i in range(n):
if low & values[i] != low:
ans = 0
ans = min(ans, (cnt * (cnt - 1) * math.factorial(n - 2)) % (10 ** 9 + 7))
print(ans)
``` | output | 1 | 40,664 | 12 | 81,329 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,665 | 12 | 81,330 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
MOD=(7+10**9)
def nc2(a):
return int((a * (a - 1) / 2)) % MOD
def fac(n):
if n <= 0:
return 1
ans = 1
for i in range(1, n + 1):
ans *= i
ans %= MOD
return ans
def solution():
n=int(input())
arr=list(map(int,input().split()))
mn=min(arr)
mc=arr.count(mn)
tp=1
for i in range(n):
if arr[i]&mn!=mn:
print(0)
return
if (i+1)<=n-2:
tp*=(i+1)
tp%=MOD
if mc<2:
print(0)
return
ans=2*(nc2(mc))*(tp)%MOD
print(ans)
t=int(input())
while t:
t-=1
solution()
``` | output | 1 | 40,665 | 12 | 81,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0. | instruction | 0 | 40,666 | 12 | 81,332 |
Tags: bitmasks, combinatorics, constructive algorithms, math
Correct Solution:
```
inf=int(1e9+7)
for s in[*open(0)][2::2]:
l=[*map(int,s.split())]
n=len(l)
a=max(l)
for i in l:
a&=i
c=l.count(a)
if c>1:
v=1
for i in range(2,n-1):
v=(v*i)%inf
print((c*(c-1)*v)%inf)
else:print(0)
``` | output | 1 | 40,666 | 12 | 81,333 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
t = int(input())
def main(a):
a.sort()
cnt = 1
for e in a[1:]:
if e == a[0]:
cnt += 1
else:
if a[0] & e != a[0]:
return 0
if cnt < 2:
return 0
res = 1
for i in range(2, len(a) - 1):
res = (res * i) % 1000000007
res = (res * (cnt-1)) % 1000000007
res = (res * cnt) % 1000000007
return res
for _ in range(t):
n = int(input())
a = [int(s) for s in input().split()]
print(main(a))
``` | instruction | 0 | 40,667 | 12 | 81,334 |
Yes | output | 1 | 40,667 | 12 | 81,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
from math import sqrt
from functools import reduce
mod = 10 ** 9 + 7
def cal(x):
ans = set()
for i in range(29, -1, -1):
if x >> i & 1:
ans.add(i)
return ans
def perm(n, m):
ans = 1
for i in range(n, n - m, -1):
ans = (ans * i) % mod
return ans
def fac(x):
ans = 1
for i in range(1, x + 1):
ans = (ans * i) % mod
return ans
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
cnt = A.count(0)
if cnt == 1:
print(0)
continue
if cnt >= 2:
ans = perm(cnt, 2) * fac(n - 2) % mod
print(ans)
continue
S = [cal(a) for a in A]
x = reduce(lambda x, y: x & y, S)
# print(S)
# print(x)
cnt1 = 0
for s in S:
if s & x == s:
cnt1 += 1
ans = perm(cnt1, 2) * fac(n - 2) % mod
print(ans)
``` | instruction | 0 | 40,668 | 12 | 81,336 |
Yes | output | 1 | 40,668 | 12 | 81,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
mod=1000000007
def A(n):
res=1
for i in range(2,n+1):
res=res*i%mod
return res
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
andres=a[0]
for i in a:
andres=andres & i
cnt=0
for i in a:
if i==andres:
cnt+=1
if cnt<2:
print(0)
continue
cnt=cnt%mod
print(cnt*(cnt-1)*A(n-2)%mod)
``` | instruction | 0 | 40,669 | 12 | 81,338 |
Yes | output | 1 | 40,669 | 12 | 81,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
import sys
input=sys.stdin.readline
mod = 10**9+7
def factorial(n):
ans = 1
for i in range(2,n+1):
ans = (ans*i)%mod
return ans
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
minn = min(a)
cnt,flag = 0,1
for i in a:
if i==minn:
cnt += 1;
if (i&minn)!=minn:
flag = 0;
ans = 0;
if flag:
ans = (cnt*(cnt-1)*factorial(n-2))%mod
print(ans)
``` | instruction | 0 | 40,670 | 12 | 81,340 |
Yes | output | 1 | 40,670 | 12 | 81,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
MOD = 1e9 + 7
def fac(n):
fact = 1
for i in range(1, n+1):
fact = (fact * i) % MOD
return fact
def comb(n, k):
num = fac(n)
den = fac(k)
sub = fac(n-k)
result = num/den
result = result/sub
return result
t = int(input())
for _ in range(t):
n = int(input())
v = [int(x) for x in input().split()]
ends = v[0]
for i in range(1, n):
ends &= v[i]
count = 0
for i in v:
if (ends & i) == i:
count += 1
if count < 2:
print(0)
else:
f = fac(n - 2)
outer = comb(count, 2)
result = (f * outer * 2) % MOD
print(int(result))
``` | instruction | 0 | 40,671 | 12 | 81,342 |
No | output | 1 | 40,671 | 12 | 81,343 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
import math
from collections import Counter
fact = [1]
big = 10**9 + 7
for i in range(1,2*(10**5) + 1):
fact.append((i*fact[-1])%big)
for _ in range(int(input())):
n = int(input())
l = list(map(int,input().split()))
d = Counter(l)
zero = -1
odd = -1
even = -1
for i in range(n):
if l[i] == 0:
zero = 1
continue
if l[i]%2 == 0:
even = 1
else:
odd = 1
if zero == -1 and even == 1 and odd == 1:
print(0)
else:
if d[0]<=1 and even == 1 and odd == 1:
print(0)
else:
ans = 0
c = d[0]
if c>=2:
ans+=c*(c-1)*fact[n-2]
ans = ans%big
if d[1]>=2:
ans+=(d[1])*(d[1]-1)*fact[n-2]
ans = ans%big
print(ans)
``` | instruction | 0 | 40,672 | 12 | 81,344 |
No | output | 1 | 40,672 | 12 | 81,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
def factorial(n):
if n==1 or n==0:
return 1
return n*factorial(n - 1)
for i in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
mini = min(arr)
if arr[0] != mini or arr[-1] != mini:
print(0)
else:
flag = True
for j in range(1, n - 1):
if arr[j] & arr[0] != mini:
print(0)
flag = False
break
if flag:
c = arr.count(mini)
ans = c * (c - 1)
ans *= factorial(n - 2)
print((ans) % (10**9 + 7))
``` | instruction | 0 | 40,673 | 12 | 81,346 |
No | output | 1 | 40,673 | 12 | 81,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A sequence of n non-negative integers (n ≥ 2) a_1, a_2, ..., a_n is called good if for all i from 1 to n-1 the following condition holds true: $$$a_1 \: \& \: a_2 \: \& \: ... \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: ... \: \& \: a_n, where \&$$$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
You are given an array a of size n (n ≥ 2). Find the number of permutations p of numbers ranging from 1 to n, for which the sequence a_{p_1}, a_{p_2}, ... ,a_{p_n} is good. Since this number can be large, output it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4), denoting the number of test cases.
The first line of each test case contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
Output t lines, where the i-th line contains the number of good permutations in the i-th test case modulo 10^9 + 7.
Example
Input
4
3
1 1 1
5
1 2 3 4 5
5
0 2 0 3 0
4
1 3 5 1
Output
6
0
36
4
Note
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of 6 permutations possible with numbers from 1 to 3: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
In the second test case, it can be proved that no permutation exists for which the sequence is good.
In the third test case, there are a total of 36 permutations for which the sequence is good. One of them is the permutation [1,5,4,2,3] which results in the sequence s=[0,0,3,2,0]. This is a good sequence because
* s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0,
* s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0.
Submitted Solution:
```
import math
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
an = 1
for x in a:
an = an & x
y = a.count(an)
ans = math.factorial(n-2)*y*(y-1)
print(ans%1000000007)
``` | instruction | 0 | 40,674 | 12 | 81,348 |
No | output | 1 | 40,674 | 12 | 81,349 |
Provide a correct Python 3 solution for this coding contest problem.
G --Derangement / Derangement
Story
Person D is doing research on permutations. At one point, D found a permutation class called "Derangement". "It's cool to have a perfect permutation !!! And it starts with D !!!", the person of D who had a crush on Chunibyo decided to rewrite all the permutations given to him.
Problem
Given a permutation of length n, p = (p_1 p_2 ... p_n). The operation of swapping the i-th element and the j-th element requires the cost of (p_i + p_j) \ times | i-j |. At this time, find the minimum cost required to make p into a perfect permutation using only the above swap operation.
However, the fact that the permutation q is a perfect permutation means that q_i \ neq i holds for 1 \ leq i \ leq n. For example, (5 3 1 2 4) is a perfect permutation, but (3 2 5 4 1) is not a perfect permutation because the fourth number is 4.
Input
The input consists of the following format.
n
p_1 p_2 ... p_n
The first row consists of one integer representing the length n of the permutation. The second line consists of n integers, and the i-th integer represents the i-th element of the permutation p. Each is separated by a single blank character.
Constraints:
* 2 \ leq n \ leq 100
* 1 \ leq p_i \ leq n, i \ neq j ⇔ p_i \ neq p_j
Output
Print the minimum cost to sort p into a complete permutation on one line. Be sure to start a new line at the end of the line.
Sample Input 1
Five
1 2 3 5 4
Sample Output 1
7
After swapping 1 and 2, swapping 1 and 3 yields (2 3 1 5 4), which is a complete permutation.
Sample Input 2
Five
5 3 1 2 4
Sample Output 2
0
It is a perfect permutation from the beginning.
Sample Input 3
Ten
1 3 4 5 6 2 7 8 9 10
Sample Output 3
38
Example
Input
5
1 2 3 5 4
Output
7 | instruction | 0 | 41,165 | 12 | 82,330 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
class MinCostFlow:
""" 最小費用流(ダイクストラ版):O(F*E*logV) """
INF = 10 ** 18
def __init__(self, N):
self.N = N
self.G = [[] for i in range(N)]
def add_edge(self, fr, to, cap, cost):
G = self.G
G[fr].append([to, cap, cost, len(G[to])])
G[to].append([fr, 0, -cost, len(G[fr])-1])
def flow(self, s, t, f):
from heapq import heappush, heappop
N = self.N; G = self.G
INF = MinCostFlow.INF
res = 0
H = [0] * N
prv_v = [0] * N
prv_e = [0] * N
while f:
dist = [INF] * N
dist[s] = 0
que = [(0, s)]
while que:
c, v = heappop(que)
if dist[v] < c:
continue
for i, (w, cap, cost, _) in enumerate(G[v]):
if cap > 0 and dist[w] > dist[v] + cost + H[v] - H[w]:
dist[w] = r = dist[v] + cost + H[v] - H[w]
prv_v[w] = v; prv_e[w] = i
heappush(que, (r, w))
if dist[t] == INF:
return INF
for i in range(N):
H[i] += dist[i]
d = f; v = t
while v != s:
d = min(d, G[prv_v[v]][prv_e[v]][1])
v = prv_v[v]
f -= d
res += d * H[t]
v = t
while v != s:
e = G[prv_v[v]][prv_e[v]]
e[1] -= d
G[v][e[3]][1] += d
v = prv_v[v]
return res
N = INT()
A = LIST()
mcf = MinCostFlow(N*2+2)
s = N * 2
t = N * 2 + 1
for i, a in enumerate(A):
mcf.add_edge(s, i, 1, 0)
mcf.add_edge(N+i, t, 1, 0)
for j in range(N):
# 完全順列なので、A[i] == i のペアには辺を張れない
if a == j+1:
continue
cost = a * abs(i - j)
mcf.add_edge(i, N+j, 1, cost)
res = mcf.flow(s, t, N)
print(res)
``` | output | 1 | 41,165 | 12 | 82,331 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,170 | 12 | 82,340 |
"Correct Solution:
```
N = int(input())
*A, = map(int, input().split())
cnt = 0
for i in range(N-1, -1, -1):
for j in range(i):
if A[j] > A[j+1]:
A[j], A[j+1] = A[j+1], A[j]
cnt += 1
print(*A)
print(cnt)
``` | output | 1 | 41,170 | 12 | 82,341 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,171 | 12 | 82,342 |
"Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
flag = True
ret = 0
while flag:
flag=False
for j in range(1,n)[::-1]:
if a[j-1] > a[j]:
a[j],a[j-1]=a[j-1],a[j]
flag=True
ret += 1
print(" ".join(map(str,a)))
print(ret)
``` | output | 1 | 41,171 | 12 | 82,343 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,172 | 12 | 82,344 |
"Correct Solution:
```
n = int(input())
A = list(map(int, input().split()))
cnt = 0
flg = 1
while flg:
flg = 0
for i in range(n - 1, 0, -1):
if A[i] < A[i - 1]:
A[i], A[i - 1] = A[i - 1], A[i]
cnt += 1
flg = 1
print(' '.join(map(str, A)))
print(cnt)
``` | output | 1 | 41,172 | 12 | 82,345 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,173 | 12 | 82,346 |
"Correct Solution:
```
N=int(input())
A=list(map(int,input().split()))
count=0
i=N-1
while i>0:
j=0
while j<i:
if A[N-1-j]<A[N-2-j]:
(A[N-1-j],A[N-2-j])=(A[N-2-j],A[N-1-j])
count+=1
#print(' '.join(map(str,A)))
j+=1
i-=1
print(' '.join(map(str,A)))
print(count)
``` | output | 1 | 41,173 | 12 | 82,347 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,174 | 12 | 82,348 |
"Correct Solution:
```
n, data = int(input()), list(map(int, input().split()))
i, t = 0, 0
while i != n-1:
for j in range(n-1, i, -1):
if data[j-1] > data[j]:
data[j-1], data[j] = data[j], data[j-1]
t += 1
i += 1
print(" ".join(map(str, data)))
print(t)
``` | output | 1 | 41,174 | 12 | 82,349 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,175 | 12 | 82,350 |
"Correct Solution:
```
N=int(input())
A=list(map(int,input().split()))[::-1]
flag=1
count=0
while flag:
flag=0
for i in range(N-1):
if A[i]<A[i+1]:
A[i],A[i+1]=A[i+1],A[i]
flag=1
count+=1
print(*A[::-1])
print(count)
``` | output | 1 | 41,175 | 12 | 82,351 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,176 | 12 | 82,352 |
"Correct Solution:
```
n = int(input())
m = list(map(int, input().split()))
count = 0
flag = 1
while flag:
flag = 0
for j in range(n-1, 0, -1):
if m[j] < m[j-1]:
m[j], m[j-1] = m[j-1], m[j]
count += 1
flag = 1
print(" ".join(str(x) for x in m))
print(count)
``` | output | 1 | 41,176 | 12 | 82,353 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9 | instruction | 0 | 41,177 | 12 | 82,354 |
"Correct Solution:
```
N=int(input())
A=[int(i) for i in input().split()]
ans=0
flag=1
while flag:
flag=0
for j in range(1,N)[::-1]:
if A[j]<A[j-1]:
A[j],A[j-1]=A[j-1],A[j]
flag=1
ans+=1
print(" ".join([str(i) for i in A]))
print(ans)
``` | output | 1 | 41,177 | 12 | 82,355 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
N = int(input())
A = list(map(int,input().split()))
flag = 1 #逆の隣接要素が存在する
cnt = 0
while flag :
flag = 0
for j in reversed(range(1,N)):
if A[j]<A[j-1]:
v = A[j]
A[j] = A[j-1]
A[j-1] = v
flag = 1
cnt += 1
print(" ".join(list(map(str,A))
))
print(cnt)
``` | instruction | 0 | 41,178 | 12 | 82,356 |
Yes | output | 1 | 41,178 | 12 | 82,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
N = int(input())
lst = list(map(int,input().split()))
cnt = 0
for i in range(N):
for j in range(N-1, i, -1):
if (lst[j] < lst[j-1]):
lst[j],lst[j-1] = lst[j-1],lst[j]
cnt += 1
print(" ".join(list(map(str, lst))))
print(cnt)
``` | instruction | 0 | 41,179 | 12 | 82,358 |
Yes | output | 1 | 41,179 | 12 | 82,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
n=int(input())
nums=list(map(int,input().split()))
k=0
for i in range(len(nums)-1):
for j in range(len(nums)-1,i,-1):
if nums[j]<nums[j-1]:
nums[j],nums[j-1]=nums[j-1],nums[j]
k+=1
print(*nums)
print(k)
``` | instruction | 0 | 41,180 | 12 | 82,360 |
Yes | output | 1 | 41,180 | 12 | 82,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
# ALDS1_2_A
N = int(input())
A = [int(i) for i in input().split()]
c = 0
flag = 1
while flag:
flag = 0
for j in range(N-1, 0, -1):
if A[j] < A[j-1]:
A[j], A[j-1] = A[j-1], A[j]
flag = 1
c += 1
print(" ".join(map(str, A)))
print(c)
``` | instruction | 0 | 41,181 | 12 | 82,362 |
Yes | output | 1 | 41,181 | 12 | 82,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
def bubbleSort(A, N):
flag = True
count = 0
while flag:
flag = 0
for j in range(N-1, 0, -1):
if A[j] < A[j-1]:
tmp = A[j]
A[j] = A[j-1]
A[j-1] = tmp
flag = 1
count += 1
print(A)
print(count)
arr_length = int(input())
arr_num = [int(i) for i in input().split(" ")]
bubbleSort(arr_num, arr_length)
``` | instruction | 0 | 41,182 | 12 | 82,364 |
No | output | 1 | 41,182 | 12 | 82,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
n = int(input())
lst = list(map(int,input().split()))
cnt = 0
for i in range(n):
for j in range(n-1,i,-1):
if lst[j] < lst[j-1]:
count += 1
lst[j],lst[j-1] = lst[j-1],lst[j]
print(' '.join(map(str,lst)))
print(cnt)
``` | instruction | 0 | 41,183 | 12 | 82,366 |
No | output | 1 | 41,183 | 12 | 82,367 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
n = input()
A = [int(i) for i in input().split(' ')]
def trace(A):
for index, v in enumerate(A):
print(v, end='')
if index != len(A) - 1:
print(' ', end='')
print()
def bubble_sort(A, n):
i = n - 1
counter = 0
while i >= 0:
j = i - 1
while j >= 0:
if A[i] < A[j]:
A[i], A[j] = A[j], A[i]
counter += 1
j -= 1
i -= 1
trace(A)
print(counter)
bubble_sort(A, 20)
``` | instruction | 0 | 41,184 | 12 | 82,368 |
No | output | 1 | 41,184 | 12 | 82,369 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
Submitted Solution:
```
n=int(input())
nums=list(map(int,input().split()))
k=0
for i in range(len(nums)-1):
for j in range(len(nums)-1,i,-1):
print(i,j)
if nums[j]<nums[j-1]:
nums[j],nums[j-1]=nums[j-1],nums[j]
k+=1
print(*nums)
print(k)
``` | instruction | 0 | 41,185 | 12 | 82,370 |
No | output | 1 | 41,185 | 12 | 82,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,349 | 12 | 82,698 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
t=int(input())
from math import *
while t>0:
t-=1
n=int(input())
a=[int(x) for x in input().split()]
p=list(bin(sum(a)))[2:]
s=sum(a)
x=0
for i in range(n):
x=x^a[i]
if sum(a)==2*x:
print(0)
print()
continue
h=0
#print(x,s)
if x!=0:
while 2**(int(ceil(log(x,2)))+h)+x+s>2*(2**(int(ceil(log(x,2))+h))):
h+=1
#print("h=",h,int(2**(int(ceil(log(x,2)))+h)))
f=2*(2**(int(ceil(log(x,2)))+h))-(2**(int(ceil(log(x,2)))+h)+x+s )
f=int(f)
if f==0:
print(1)
print(int(2**(int(ceil(log(x,2)))+h))+x)
else:
print(3)
print(2**(int(ceil(log(x,2)))+h)+x,f//2,f//2)
else:
while 2**h+s>2*(2**h):
h+=1
f=2*(2**h)-(2**h+s)
f=int(f)
#print("h=",h)
if f==0:
print(1)
print(int(2**(h))+x)
else:
print(3)
print(int(2**(h))+x,f//2,f//2)
``` | output | 1 | 41,349 | 12 | 82,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,350 | 12 | 82,700 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
arr = [int(i) for i in input().split()]
sum_arr = sum(arr)
xor_arr = 0
for i in range(n):
xor_arr = xor_arr ^ arr[i]
ans = []
ans.append(xor_arr)
y = sum_arr + xor_arr
ans.append(y)
print(len(ans))
print(*ans)
``` | output | 1 | 41,350 | 12 | 82,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,351 | 12 | 82,702 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
from collections import Counter,defaultdict,deque
import heapq as hq
from itertools import count, islice
#alph = 'abcdefghijklmnopqrstuvwxyz'
#from math import factorial as fact
#a,b = [int(x) for x in input().split()]
import math
import sys
input=sys.stdin.readline
tt = int(input())
for test in range(tt):
n = int(input())
arr = [int(x) for x in input().split()]
xor = 0
s = 0
for el in arr:
s+=el
xor^= el
print(2)
print(xor,xor+s)
``` | output | 1 | 41,351 | 12 | 82,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,352 | 12 | 82,704 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
t=int(input())
while t>0:
n=int(input())
a=list(map(int,input().split()))
xor=0
s=0
for each in a:
xor=xor^each
s=s+each
if(s==2*xor):
print(0)
else:
print(2)
a.append(xor)
s=s+xor
print(xor,s)
t=t-1
``` | output | 1 | 41,352 | 12 | 82,705 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,353 | 12 | 82,706 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
k=sum(a)
d=[0]*40
for j in range(n):
s='';l=a[j]
while l!=0:
s+=str(l%2)
l=l//2
for u in range(len(s)):
if s[u]=='1':
d[u]+=1
p=0
for j in range(40):
d[j]=d[j]%2
if d[j]==1:
p+=2**j
k+=p
print(2)
print(p,k)
'''else:
print(3)
print(1,p,k-1)'''
``` | output | 1 | 41,353 | 12 | 82,707 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,354 | 12 | 82,708 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
for u in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
x=0
s=sum(l)
for i in l:
x^=i
s=x+s
print(2)
print(x,s)
``` | output | 1 | 41,354 | 12 | 82,709 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,355 | 12 | 82,710 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
def proc_case(a):
sum = 0
xor = 0
for i in a:
sum += i
xor ^= i
if sum == xor * 2:
return 0, []
if xor == 0:
return 1, [sum]
return 2, [xor, xor+sum]
cases_num = int(input())
for _ in range(cases_num):
_ = input()
a = [int(i) for i in input().split(" ")]
c, nums = proc_case(a)
print(f"{c}\n{' '.join([str(i) for i in nums])}")
``` | output | 1 | 41,355 | 12 | 82,711 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8. | instruction | 0 | 41,356 | 12 | 82,712 |
Tags: bitmasks, constructive algorithms, math
Correct Solution:
```
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int,input().split()))
res = 0
sa = 0
for i in a:
res ^= i
sa += i
if sa % 2:
mnum = (1 << 59) - 1
else:
mnum = (1<<59)
res = (mnum ^ res) * 2
snum = (res - sa - mnum) // 2
print(3)
print(mnum, snum,snum)
``` | output | 1 | 41,356 | 12 | 82,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
def solve(xr,sm):
k = []
if(2*xr==sm):
return []
else:
k.append(xr)
k.append(sm+xr)
return k
for _ in range(int(input())):
n = int(input())
l = list(map(int,input().split()))
xr = 0;
sm = 0
for i in l:
xr = xr^i
sm += i
z = solve(xr,sm)
print (len(z))
print(*z)
``` | instruction | 0 | 41,357 | 12 | 82,714 |
Yes | output | 1 | 41,357 | 12 | 82,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
x = 0
s = 0
for i in a :
x ^= i
s += i
print(2)
print(x,x + s)
``` | instruction | 0 | 41,358 | 12 | 82,716 |
Yes | output | 1 | 41,358 | 12 | 82,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
import math
from decimal import Decimal
import heapq
from collections import deque
def na():
n = int(input())
b = [int(x) for x in input().split()]
return n,b
def nab():
n = int(input())
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
return n,b,c
def dv():
n, m = map(int, input().split())
return n,m
def da():
n, m = map(int, input().split())
a = list(map(int, input().split()))
return n,m, a
def dva():
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
return n,m,b
def eratosthenes(n):
sieve = list(range(n + 1))
for i in sieve:
if i > 1:
for j in range(i + i, len(sieve), i):
sieve[j] = 0
return sorted(set(sieve))
def lol(lst,k):
k=k%len(lst)
ret=[0]*len(lst)
for i in range(len(lst)):
if i+k<len(lst) and i+k>=0:
ret[i]=lst[i+k]
if i+k>=len(lst):
ret[i]=lst[i+k-len(lst)]
if i+k<0:
ret[i]=lst[i+k+len(lst)]
return(ret)
def nm():
n = int(input())
b = [int(x) for x in input().split()]
m = int(input())
c = [int(x) for x in input().split()]
return n,b,m,c
def dvs():
n = int(input())
m = int(input())
return n, m
def fact(a, b):
c = []
ans = 0
f = int(math.sqrt(a))
for i in range(1, f + 1):
if a % i == 0:
c.append(i)
l = len(c)
for i in range(l):
c.append(a // c[i])
for i in range(len(c)):
if c[i] <= b:
ans += 1
if a / f == f and b >= f:
return ans - 1
return ans
import math
from decimal import Decimal
import heapq
from collections import deque
def na():
n = int(input())
b = [int(x) for x in input().split()]
return n,b
def nab():
n = int(input())
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
return n,b,c
def dv():
n, m = map(int, input().split())
return n,m
def da():
n, m = map(int, input().split())
a = list(map(int, input().split()))
return n,m, a
def dva():
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
return n,m,b
def eratosthenes(n):
sieve = list(range(n + 1))
for i in sieve:
if i > 1:
for j in range(i + i, len(sieve), i):
sieve[j] = 0
return sorted(set(sieve))
def lol(lst,k):
k=k%len(lst)
ret=[0]*len(lst)
for i in range(len(lst)):
if i+k<len(lst) and i+k>=0:
ret[i]=lst[i+k]
if i+k>=len(lst):
ret[i]=lst[i+k-len(lst)]
if i+k<0:
ret[i]=lst[i+k+len(lst)]
return(ret)
def nm():
n = int(input())
b = [int(x) for x in input().split()]
m = int(input())
c = [int(x) for x in input().split()]
return n,b,m,c
def dvs():
n = int(input())
m = int(input())
return n, m
def fact(a, b):
c = []
ans = 0
f = int(math.sqrt(a))
for i in range(1, f + 1):
if a % i == 0:
c.append(i)
l = len(c)
for i in range(l):
c.append(a // c[i])
for i in range(len(c)):
if c[i] <= b:
ans += 1
if a / f == f and b >= f:
return ans - 1
return ans
for _ in range(int(input())):
n, a = na()
d = sum(a)
x = 0
for i in a:
x ^= i
print(2)
print(d + x, x)
``` | instruction | 0 | 41,359 | 12 | 82,718 |
Yes | output | 1 | 41,359 | 12 | 82,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
'''input
3
4
1 2 3 6
1
8
2
1 1
'''
# A coding delight
from sys import stdin
from collections import defaultdict
def give(num):
return 0, num
# main starts
t = int(stdin.readline().strip())
for _ in range(t):
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
xor = 0
for i in arr:
xor ^= i
if xor == 0:
s = sum(arr)
a, b = give(s)
print(2)
print(a, b)
else:
s = sum(arr) + xor
a = xor
b, c = give(s)
print(3)
print(a, b, c)
``` | instruction | 0 | 41,360 | 12 | 82,720 |
Yes | output | 1 | 41,360 | 12 | 82,721 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
'''input
3
4
1 2 3 6
1
8
2
1 1
'''
# A coding delight
from sys import stdin
from collections import defaultdict
def give(num):
return 0, num
# main starts
t = int(stdin.readline().strip())
for _ in range(t):
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
xor = 1
for i in arr:
xor ^= i
if xor == 0:
s = sum(arr)
a, b = give(s)
print(2)
print(a, b)
else:
s = sum(arr) + xor
a = xor
b, c = give(s)
print(3)
print(a, b, c)
``` | instruction | 0 | 41,361 | 12 | 82,722 |
No | output | 1 | 41,361 | 12 | 82,723 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
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")
# ------------------- fast io --------------------
testcases=int(input())
for j in range(testcases):
n=int(input())
vals=list(map(int,input().split()))
#find the sum
summy=sum(vals)
#just append the sum
outy=[0,summy]
print(2)
print(*outy)
``` | instruction | 0 | 41,362 | 12 | 82,724 |
No | output | 1 | 41,362 | 12 | 82,725 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
import sys
def main():
tests = int(sys.stdin.readline()[:-1])
for i in range(tests):
n = int(sys.stdin.readline()[:-1])
a = [int(x) for x in sys.stdin.readline()[:-1].split()]
s = 0
x = 0
for j in range(n):
s += a[j]
x ^= a[j]
if s == 2 * x:
print(0)
print()
else:
b = []
if s % 2 == 1:
s += 1
x ^= 1
b.append(1)
b.append(x)
b.append(s // 2)
print(len(b))
for k in b:
print(k, end=' ')
print()
if __name__ == '__main__':
main()
``` | instruction | 0 | 41,363 | 12 | 82,726 |
No | output | 1 | 41,363 | 12 | 82,727 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2⋅(a_1 ⊕ a_2 ⊕ ... ⊕ a_m), where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2⋅ 6 = 2⋅ (1⊕ 2 ⊕ 3 ⊕ 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 ≠ 2⋅ 1 = 2⋅(1⊕ 2 ⊕ 1 ⊕ 3).
You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤ n ≤ 10^5) — the size of the array.
The second line of each test case contains n integers a_1, a_2, ..., a_n (0≤ a_i ≤ 10^9) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output two lines.
In the first line, output a single integer s (0≤ s≤ 3) — the number of elements you want to append.
In the second line, output s integers b_1, ..., b_s (0≤ b_i ≤ 10^{18}) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
Example
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
Note
In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.
In the second test case of the example, after adding 4, 4, the array becomes [8, 4, 4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.
Submitted Solution:
```
t = int(input())
for q in range(t):
n = int(input())
a = [int(i) for i in input().split()]
s = 0
x = a[0]
for i in range(n):
if i == 0:
s += a[i]
else:
s += a[i]
x ^= a[i]
if x * 2 == s:
print(0)
continue
elif x == 0:
if s % 2 == 0:
print(1)
print(s)
else:
print(2)
print(1, s)
continue
if x % 2 != 0 and s % 2 != 0:
x ^= 1
s += 1
if 2 * x > s:
dif = 2 * x - s
dif //= 2
print(3)
print(1, dif, dif)
else:
var1 = x ^ 1
var2 = s
s += x
print(2)
print(var1, s)
continue
elif x % 2 == 0 and s % 2 == 0:
if 2 * s > x:
dif = 2 * s - x
print(2)
print(dif // 2, dif // 2)
else:
var1 = x
s += x
print(2)
print(var1, s)
continue
'''
else:
if x * 2 > s:
dif = x * 2 - s
if dif % 2 == 0:
x1 = dif // 2
print(2)
print(x1, x1)
continue
else:
if x % 2 != 0:
x ^= 1
dif = x * 2 - s
x1 = dif // 2
print(3)
print(1, x1, x1)
continue
else:
dif = s - x * 2
if x * 2 == 0:
print(1)
print(dif)
else:
if dif % 2 != 0:
if s % 2 != 0 and x % 2 != 0:
s += x
print(2)
print(x, s)
continue
else:
'''
``` | instruction | 0 | 41,364 | 12 | 82,728 |
No | output | 1 | 41,364 | 12 | 82,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is constraints.
You are given a sequence a consisting of n positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not.
Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome.
You have to answer t independent test cases.
Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2].
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2000) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 ≤ n ≤ 2000) — the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26.
It is guaranteed that the sum of n over all test cases does not exceed 2000 (∑ n ≤ 2000).
Output
For each test case, print the answer — the maximum possible length of some subsequence of a that is a three blocks palindrome.
Example
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3 | instruction | 0 | 41,397 | 12 | 82,794 |
Tags: binary search, brute force, data structures, dp, two pointers
Correct Solution:
```
import sys
# import bisect
# from collections import deque
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 998244353
for _ in range(int(ri())):
n = int(ri())
a = Ri()
# lis = []
# cnt = []
# c = 0
# for i in range(1,len(a)):
# if a[i] ==a[i-1]:
# c+=1
# else:
# lis.append(a[i-1])
# cnt.append(c+1)
# c = 0
# lis.append(a[-1])
# cnt.append(c+1)
count = list2d(27,len(a)+1,0)
for i in range(len(a)):
for j in range(1,27):
count[j][i+1] = count[j][i]
count[a[i]][i+1]+=1
ans = -1
for i in range(len(a)):
for j in range(i+1,len(a)):
if a[i] == a[j]:
left = count[a[i]][i+1]
right = count[a[i]][len(a)] - count[a[i]][j]
maxx = 0
for k in range(1,27):
if k != a[i]:
cnt = count[k][j+1]-count[k][i]
maxx = max(maxx,cnt)
ans = max(ans,min(left,right)*2+maxx)
for i in range(1,27):
ans = max(ans,count[i][n])
print(ans)
``` | output | 1 | 41,397 | 12 | 82,795 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.