message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
def solution (a, b, n):
i = 0
while i * a <= n:
if (n - (i * a)) % b == 0:
return (i, int((n - (i * a)) / b))
i = i + 1
return -1
n,a,b = map(int,input().split())
a,b = max(a,b), min(a,b)
x = solution(a,b,n)
if(x == -1):
print(x)
else:
ans = []
r,s = x[0],x[1]
curr = 1
while(r):
last = a + curr - 1
ans.append(last)
for i in range(curr, curr + a - 1):
ans.append(i)
curr += a
r-=1
while(s):
last = b + curr - 1
ans.append(last)
for i in range(curr, curr + b - 1):
ans.append(i)
curr += b
s-=1
print(*ans)
``` | instruction | 0 | 4,778 | 12 | 9,556 |
Yes | output | 1 | 4,778 | 12 | 9,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
from sys import stdin, stdout
from random import randrange
n, a, b = map(int, stdin.readline().split())
first, second = -1, -1
for i in range(n):
if n >= a * i and not (n - a * i) % b:
first, second = i, (n - a * i) // b
break
if min(first, second) == -1:
stdout.write('-1')
else:
ans = [0 for i in range(n + 1)]
current = 1
for i in range(first):
ans[current] = current + a - 1
current += 1
for j in range(1, a):
ans[current] = current - 1
current += 1
current -= 1
for i in range(second):
ans[current] = current + b - 1
current += 1
for j in range(1, b):
ans[current] = current - 1
current += 1
stdout.write(' '.join(list(map(str, ans[1:]))))
``` | instruction | 0 | 4,779 | 12 | 9,558 |
No | output | 1 | 4,779 | 12 | 9,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
#This code sucks, you know it and I know it.
#Move on and call me an idiot later.
n, a, b = map(int, input().split())
if n%a!=0 and n%b!=0 and (n%a)%b!=0 and (n%b)%a!=0:
print(-1)
else:
if b > a:
a, b = b, a
aa = n//a
bb = (n-aa*a)//b
l = []
if n%a==0 or n%b==0:
c = a
if n%b==0:
c = b
for i in range(1, (n//c)+1):
x = c*(i-1) + 1
y = c*i
l.append(y)
l += [j for j in range(x, y)]
print(" ".join(map(str, l)))
exit(0)
for i in range(1,aa+1):
x = a*(i-1) + 1
y = a*i
l.append(y)
l += [j for j in range(x, y)]
for i in range(1,bb+1):
x = a*aa + b*(i-1) + 1
y = a*aa + b*i
l.append(y)
l += [j for j in range(x, y)]
print(" ".join(map(str, l)))
``` | instruction | 0 | 4,780 | 12 | 9,560 |
No | output | 1 | 4,780 | 12 | 9,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
from sys import stdin, stdout
from random import randrange
n, a, b = map(int, stdin.readline().split())
first, second = -1, -1
for i in range(n):
if n > a * i and not (n - a * i) % b:
first, second = i, (n - a * i) // b
break
if min(first, second) == -1:
stdout.write('-1')
else:
ans = [0 for i in range(n + 1)]
current = 1
for i in range(first):
ans[current] = current + a - 1
current += 1
for j in range(1, a):
ans[current] = current - 1
current += 1
current -= 1
for i in range(second):
ans[current] = current + b - 1
current += 1
for j in range(1, b):
ans[current] = current - 1
current += 1
stdout.write(' '.join(list(map(str, ans[1:]))))
``` | instruction | 0 | 4,781 | 12 | 9,562 |
No | output | 1 | 4,781 | 12 | 9,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
# ========= /\ /| |====/|
# | / \ | | / |
# | /____\ | | / |
# | / \ | | / |
# ========= / \ ===== |/====|
# code
def main():
n , a , b = map(int , input().split())
if a < b:
a , b = b , a
s = []
k = 1
ok = False
x = 0
y = 0
for j in range(n):
k = n - a * j
if k >= 0:
if k % b == 0:
ok = True
x = j
y = k // b
break
for j in range(x):
for i in range(a - 1):
s.append(k + i + 1)
s.append(k)
k += a
n -= a
for j in range(y):
for i in range(b - 1):
s.append(k + i + 1)
s.append(k)
k += b
n -= b
print(*s)
return
if __name__ == "__main__":
main()
``` | instruction | 0 | 4,782 | 12 | 9,564 |
No | output | 1 | 4,782 | 12 | 9,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,792 | 12 | 9,584 |
Tags: combinatorics, math
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
if sum(a[i]==i+1 for i in range(n))>=n//1000:print("Petr")
else:print("Um_nik")
``` | output | 1 | 4,792 | 12 | 9,585 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,793 | 12 | 9,586 |
Tags: combinatorics, math
Correct Solution:
```
input()
a=list(map(int,input().split()))
n=len(a)
u=n
for i in range(n):
j=i
k=0
while a[j]>0:
k+=1
t=j
j=a[j]-1
a[t]=0
if k>0:
u+=1-k%2
s='Petr'
if u%2>0:
s='Um_nik'
print(s)
``` | output | 1 | 4,793 | 12 | 9,587 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,794 | 12 | 9,588 |
Tags: combinatorics, math
Correct Solution:
```
#!/usr/bin/env python3
n = int(input().strip())
ais = list(map(int, input().strip().split()))
visited = [False for _ in range(n)]
parity = 0
for i in range(n):
if not visited[i]:
parity += 1
j = i
while not visited[j]:
visited[j] = True
j = ais[j] - 1
if parity % 2 == 0:
print ('Petr')
else:
print ('Um_nik')
``` | output | 1 | 4,794 | 12 | 9,589 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,795 | 12 | 9,590 |
Tags: combinatorics, math
Correct Solution:
```
n = int( input() )
a = list( map( lambda x: int( x )-1, input().split( ' ' ) ) )
ret = True
for i in range( n ):
if a[i]==-1: continue
x, ret = i, not ret
while a[x]!=i:
a[x], x = -1, a[x]
a[x] = -1
if ret: print( "Petr" )
else: print( "Um_nik" )
``` | output | 1 | 4,795 | 12 | 9,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,796 | 12 | 9,592 |
Tags: combinatorics, math
Correct Solution:
```
n = int(input())
u = list(map(int, input().split()))
for i in range(n):
u[i] -= 1
ans = 0
for i in range(n):
if u[i] == -1:
continue
ans = 1 - ans
x = i
while x >= 0:
y = u[x]
u[x] = -1
x = y
if ans:
print('Um_nik')
else:
print('Petr')
``` | output | 1 | 4,796 | 12 | 9,593 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,797 | 12 | 9,594 |
Tags: combinatorics, math
Correct Solution:
```
n=int(input())
a=[0] + list(map(int,input().split()))
d={}
for i in range(1,n+1):
d[a[i]]=i
ans=0
for i in range(1,n+1):
if a[i]!=i:
ind1=d[a[i]]
ind2=d[i]
va1=a[i]
val2=i
a[ind1],a[ind2]=a[ind2],a[ind1]
d[i]=i
d[va1]=ind2
ans+=1
# print(a,ans,d)
# print(ans)
if (3*n - ans)%2==0:
print("Petr")
else:
print("Um_nik")
``` | output | 1 | 4,797 | 12 | 9,595 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,798 | 12 | 9,596 |
Tags: combinatorics, math
Correct Solution:
```
import sys
n = int(sys.stdin.readline().rstrip())
nums = list(map(int, sys.stdin.readline().split()))
swaps = 0
visited = set()
for index in range(n):
if index in visited:
continue
else:
visited.add(index)
length = 0
value = nums[index]
while (value != index + 1):
visited.add(value - 1)
value = nums[value - 1]
length += 1
swaps += length
if ((3 * n - swaps) % 2):
print("Um_nik")
else:
print("Petr")
``` | output | 1 | 4,798 | 12 | 9,597 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,799 | 12 | 9,598 |
Tags: combinatorics, math
Correct Solution:
```
class BIT:
__all__ = ['add', 'sumrange', 'lower_left']
def __init__(self, maxsize=10**7):
assert (maxsize > 0)
self._n = maxsize+1
self._bitdata = [0]*(maxsize+1)
def add(self, i, x):
'''Add x to A[i] (A[i] += x) '''
assert(0 <= i < self._n)
pos = i+1
while pos < self._n:
self._bitdata[pos] += x
pos += pos&(-pos)
def running_total(self, i):
''' Return sum of (A[0] ... A[i]) '''
assert (-1<= i < self._n)
if i == -1:
return 0
returnval = 0
pos = i+1
while pos:
returnval += self._bitdata[pos]
pos -= pos & (-pos)
return returnval
def sumrange(self, lo=0, hi=None):
''' Return sum of (A[lo] ... A[hi]) '''
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = self._n
return self.running_total(hi) - self.running_total(lo-1)
def lower_left(self, total):
''' Return min-index satisfying {sum(A0 ~ Ai) >= total}
only if Ai >= 0 (for all i)
'''
if total < 0:
return -1
pos = 0
k = 1<<(self._n.bit_length()-1)
while k > 0:
if pos+k < self._n and self._bitdata[pos+k] < total:
total -= self._bitdata[pos+k]
pos += k
k //= 2
return pos
def tentousu(lis):
bit = BIT()
ans = 0
for i in range(len(lis)):
bit.add(lis[i], 1)
ans += i + 1 - bit.running_total(lis[i])
return ans
N=int(input())
L=list(map(int,input().split()))
a=tentousu(L)
a%=2
if N%2==0 and a%2==0:
print("Petr")
if N%2==0 and a%2==1:
print("Um_nik")
if N%2==1 and a%2==0:
print("Um_nik")
if N%2==1 and a%2==1:
print("Petr")
``` | output | 1 | 4,799 | 12 | 9,599 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden. | instruction | 0 | 4,800 | 12 | 9,600 |
Tags: combinatorics, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
n=in_num()
l=in_arr()
c=0
for i in range(n):
if not l[i]:
continue
temp=i+1
c+=1
while temp:
#print i,temp,l
x=l[temp-1]
l[temp-1]=0
temp=x
#temp,l[temp-1]=l[temp-1],0
if c%2:
pr('Um_nik')
else:
pr('Petr')
``` | output | 1 | 4,800 | 12 | 9,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
def getIntList():
return list(map(int, input().split()));
n= int(input());
s=getIntList();
s=[0]+s;
seen=[False]*(n+1);
sign=1;
for i in range(1, n+1):
length=1;
if seen[i]:
continue;
seen[i]=True;
j=s[i];
while j!=i:
seen[j]=True;
j=s[j];
length+=1;
if length%2==0:
sign*=-1;
if n%2==0:
signPetr=1;
else:
signPetr=-1;
if signPetr==sign:
print("Petr");
else:
print("Um_nik");
``` | instruction | 0 | 4,801 | 12 | 9,602 |
Yes | output | 1 | 4,801 | 12 | 9,603 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
def read():
return int(input())
def readmap():
return map(int, input().split())
def readlist():
return list(map(int, input().split()))
N = read()
P = readlist()
def is_even(P):
checked = [False] * (N+1)
# numset = {}
# for n in range(1, N+1):
# numset[n] = n
num_of_even_cycles = 0
for n in range(1, N+1):
if checked[n]:
continue
checked[n] = True
len_cyc = 1
nxt = P[n-1]
while not checked[nxt]:
checked[nxt] = True
nxt = P[nxt-1]
len_cyc += 1
if len_cyc % 2 == 0:
num_of_even_cycles += 1
# while numset:
# flag = False
# for n in numset.keys():
# if flag:
# break
# flag = True
# length_of_cycle = 1
# while P[n-1] in numset:
# length_of_cycle += 1
# del numset[n]
# n = P[n-1]
#
# if length_of_cycle % 2 == 0:
# num_of_even_cycles += 1
if num_of_even_cycles % 2 == 0:
return True
else:
return False
if N % 2 == 0:
if is_even(P):
print("Petr")
else:
print("Um_nik")
else:
if not is_even(P):
print("Petr")
else:
print("Um_nik")
``` | instruction | 0 | 4,802 | 12 | 9,604 |
Yes | output | 1 | 4,802 | 12 | 9,605 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
pos={}
for i in range(n):
pos[a[i]]=i
ct=0
curr=1
for i in range(n):
if pos[curr]==i:
curr+=1
continue
ct+=1
j=pos[curr]
pos[curr]=i
pos[a[i]]=j
curr+=1
a[i],a[j]=a[j],a[i]
if (3*n<ct) or (3*n-ct)&1:
print("Um_nik")
else:
print("Petr")
``` | instruction | 0 | 4,803 | 12 | 9,606 |
Yes | output | 1 | 4,803 | 12 | 9,607 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
import random
astr=input()
N=int(astr)
s=input()
sk=s.split(' ')
ml=[int(i) for i in sk]
kl=[0 for i in ml]
#for i in range(0, 7*N+2):
# A=random.randint(0,N-1)
# B=A
# while(A==B):
# B=random.randint(0,N-1)
# swap=ml[A]
# ml[A]=ml[B]
# ml[B]=swap
#print(ml)
k=0
for i in range(0,N):
if kl[i]==0:
kl[i]=1
j=ml[i]
k=k+1
while(kl[j-1]==0):
kl[j-1]=1
j=ml[j-1]
if k%2==0: print("Petr")
else: print("Um_nik")
``` | instruction | 0 | 4,804 | 12 | 9,608 |
Yes | output | 1 | 4,804 | 12 | 9,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
from random import randint
a = randint(1, 2)
if a == 1:
print("Petr")
else:
print("Um_nik")
ps = 0
``` | instruction | 0 | 4,805 | 12 | 9,610 |
No | output | 1 | 4,805 | 12 | 9,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
import random
n=int(input())
l = input().split()
if(random.random() > 0.52):
print('Petr')
else:
print('Um_nik')
``` | instruction | 0 | 4,806 | 12 | 9,612 |
No | output | 1 | 4,806 | 12 | 9,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
n = int(input())
num = list(map(int, input().split()))
res = 0
for i in range(len(num)):
res += (num[i] != (i - 1))
if not (res < 2):
print('Petr')
else:
print('Um_nik')
``` | instruction | 0 | 4,807 | 12 | 9,614 |
No | output | 1 | 4,807 | 12 | 9,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 1 to n and then 3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+1 times instead of 3n times. Because it is more random, OK?!
You somehow get a test from one of these problems and now you want to know from which one.
Input
In the first line of input there is one integer n (10^{3} β€ n β€ 10^{6}).
In the second line there are n distinct integers between 1 and n β the permutation of size n from the test.
It is guaranteed that all tests except for sample are generated this way: First we choose n β the size of the permutation. Then we randomly choose a method to generate a permutation β the one of Petr or the one of Alex. Then we generate a permutation using chosen method.
Output
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
Example
Input
5
2 4 5 1 3
Output
Petr
Note
Please note that the sample is not a valid test (because of limitations for n) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.
Due to randomness of input hacks in this problem are forbidden.
Submitted Solution:
```
from random import randint
a = randint(1, 2)
if a == 1:
print("Petr")
else:
print("Um_nik")
q = 0
``` | instruction | 0 | 4,808 | 12 | 9,616 |
No | output | 1 | 4,808 | 12 | 9,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0. | instruction | 0 | 5,096 | 12 | 10,192 |
Tags: combinatorics, dp, matrices, probabilities
Correct Solution:
```
N, T = map(int, input().split())
A = [int(a) for a in input().split()]
if sum(A) > N//2:
A = [1-a for a in A][::-1]
K = sum(A)
S = sum(A[-K:])
M = K + 1
P = 10**9+7
inv = pow(N*(N-1)//2, P-2, P)
X = [[0]*M for _ in range(M)]
for i in range(M):
if i > 0: X[i-1][i] = ((K-i+1)**2*inv)%P
if i < M-1: X[i+1][i] = (N-2*K+i+1)*(i+1)*inv%P
X[i][i] = (1-((K-i)**2*inv)-(N-2*K+i)*(i)*inv)%P
def ddd(n):
for i in range(1, 100):
if (n*i%P) < 100:
return (n*i%P), i
return -1, -1
def poww(MM, n):
if n == 1:
return MM
if n % 2:
return mult(poww(MM, n-1), MM)
return poww(mult(MM,MM), n//2)
def mult(M1, M2):
Y = [[0] * M for _ in range(M)]
for i in range(M):
for j in range(M):
for k in range(M):
Y[i][j] += M1[i][k] * M2[k][j]
Y[i][j] %= P
return Y
X = poww(X, T)
print(X[S][K])
``` | output | 1 | 5,096 | 12 | 10,193 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0. | instruction | 0 | 5,097 | 12 | 10,194 |
Tags: combinatorics, dp, matrices, probabilities
Correct Solution:
```
import sys; input=sys.stdin.readline
# print(input())
N, T = map(int, input().split())
A = [int(a) for a in input().split()]
if sum(A) > N//2:
A = [1-a for a in A][::-1]
K = sum(A)
S = sum(A[-K:])
M = K + 1
P = 10**9+7
inv = pow(N*(N-1)//2, P-2, P)
X = [[0]*M for _ in range(M)]
for i in range(M):
if i > 0: X[i-1][i] = ((K-i+1)**2*inv)%P
if i < M-1: X[i+1][i] = (N-2*K+i+1)*(i+1)*inv%P
X[i][i] = (1-((K-i)**2*inv)-(N-2*K+i)*(i)*inv)%P
# def ddd(n):
# for i in range(1, 100):
# if (n*i%P) < 100:
# return (n*i%P), i
# return -1, -1
def poww(MM, n):
if n == 1:
return MM
if n % 2:
return mult(poww(MM, n-1), MM)
return poww(mult(MM,MM), n//2)
def mult(M1, M2):
Y = [[0] * M for _ in range(M)]
for i in range(M):
for j in range(M):
for k in range(M):
Y[i][j] += M1[i][k] * M2[k][j]
Y[i][j] %= P
return Y
X = poww(X, T)
print(X[S][K])
``` | output | 1 | 5,097 | 12 | 10,195 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0. | instruction | 0 | 5,098 | 12 | 10,196 |
Tags: combinatorics, dp, matrices, probabilities
Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Codeforces Round #553 (Div. 2)
Problem F. Sonya and Informatics
:author: Kitchen Tong
:mail: kctong529@gmail.com
Please feel free to contact me if you have any question
regarding the implementation below.
"""
__version__ = '1.8'
__date__ = '2019-04-21'
import sys
def binom_dp():
dp = [[-1 for j in range(110)] for i in range(110)]
def calculate(n, k):
if n < k:
return 0
if n == k or k == 0:
return 1
if dp[n][k] > 0:
return dp[n][k]
else:
dp[n][k] = calculate(n-1, k-1) + calculate(n-1, k)
return dp[n][k]
return calculate
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def multiply(A, B, mod):
if not hasattr(B[0], '__len__'):
C = [sum(aij * B[j] % mod for j, aij in enumerate(ai)) for ai in A]
else:
C = [[0 for col in range(len(B[0]))] for row in range(len(A))]
len_A = len(A)
len_B = len(B)
for row in range(len_A):
if sum(A[row]) == 0:
continue
for col in range(len_B):
C[row][col] = sum(A[row][k] * B[k][col]
for k in range(len_B)) % mod
return C
def memoize(func):
memo = {}
def wrapper(*args):
M, n, mod = args
if n not in memo:
memo[n] = func(M, n, mod)
return memo[n]
return wrapper
@memoize
def matrix_pow(M, n, mod):
# print(f'n is {n}')
if n == 2:
return multiply(M, M, mod)
if n == 1:
return M
sub_M = matrix_pow(M, n//2, mod)
if n % 2 == 0:
return multiply(sub_M, sub_M, mod)
return multiply(sub_M, matrix_pow(M, n - n//2, mod), mod)
def solve(n, k, a, binom, mod):
ones = sum(a)
zeros = n - ones
M = [[0 for col in range(zeros+1)] for row in range(zeros+1)]
for row in range(max(0, zeros-ones), zeros+1):
pre_zeros = row
pre_ones = zeros - pre_zeros
post_zeros = pre_ones
post_ones = ones - pre_ones
M[row][row] = (pre_ones * post_ones + pre_zeros * post_zeros
+ binom(zeros, 2) + binom(ones, 2))
if row > max(0, zeros-ones):
M[row-1][row] = pre_zeros * post_ones
if row < zeros:
M[row+1][row] = post_zeros * pre_ones
M = [matrix_pow(M, k, mod)[-1]]
b = [0] * (zeros + 1)
b[zeros - sum(a[:zeros])] = 1
C = multiply(M, b, mod)
return C[-1]
def main(argv=None):
mod = int(1e9) + 7
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
binom = binom_dp()
P = solve(n, k, a, binom, mod)
if P == 0:
print(0)
else:
Q = pow(binom(n, 2), k, mod)
print(P * modinv(Q, mod) % mod)
return 0
if __name__ == "__main__":
STATUS = main()
sys.exit(STATUS)
``` | output | 1 | 5,098 | 12 | 10,197 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0. | instruction | 0 | 5,099 | 12 | 10,198 |
Tags: combinatorics, dp, matrices, probabilities
Correct Solution:
```
M = 10 ** 9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
z, o = a.count(0), a.count(1)
d = pow(n * (n - 1) // 2, M - 2, M)
if z > o:
o, z = z, o
a = [1 - x for x in a][::-1]
res = [[0] * (z + 1) for i in range(z + 1)]
tf = [[0] * (z + 1) for i in range(z + 1)]
for i in range(z + 1):
res[i][i] = 1
tf[i][i] = (z * (z - 1) // 2 + o * (o - 1) // 2 + i * (z - i) + (z - i) * (o - z + i)) * d % M
if i < z: tf[i + 1][i] = (z - i) * (z - i) * d % M
if i: tf[i - 1][i] = i * (o - z + i) * d % M
def mul(a, b):
t = [[0] * (z + 1) for i in range(z + 1)]
for i in range(z + 1):
for k in range(z + 1):
for j in range(z + 1):
t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % M
return t
while k:
if k & 1:
res = mul(res, tf)
tf = mul(tf, tf)
k >>= 1
print(res[-1][a[:z].count(0)])
``` | output | 1 | 5,099 | 12 | 10,199 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
Submitted Solution:
```
n,m=[int(x)for x in input().split()]
ns=[int(x)for x in input().split()]
print(1)
# k=n-sum(ns)
# c=k-sum(ns[:k])
# # print(k,c)
#
# def r(num):
# return num, k - num, k - num, n - k - (k - num)
# def p(num):
# a,b,c,d=r(num)
# o,z=a+c,b+d
# return a*d,o*(o-1)//2+z*(z-1)//2+a*b+c*d,b*c
#
# import numpy as np
#
# bs=np.zeros((k+1,k+1),'int')
# md=10**9+7
# st=np.zeros((1,k+1),'int')
# st[0,c]=1
# deno=n*(n-1)//2
#
# def get_inv(aa,pp):
# pp-=2
# l=len(bin(pp))-2
# ls=[aa]
# for i in range(l):
# ls.append(ls[-1]*ls[-1]%(pp+2))
# # print(ls)
# aa=1
# for i in range(l):
# if (pp>>i)&1:
# aa=aa*ls[i]%(pp+2)
# return aa%(pp+2)
# inv=get_inv(deno,md)
# # print('inv of {} : {}'.format(deno,inv))
#
#
# for i in range(k+1):
# x=p(i)
# for j in range(i-1,i+2):
# if 0<=j<=k:
# bs[i][j]=x[j-i+1]*inv%md
# ls=[bs]
# i=0
# m=bin(m)[2:][::-1]
# # print('m',m)
# for i in range(len(m)):
# ls.append(np.matmul(ls[-1],ls[-1])%md)
# # print(ls)
# for i in range(len(m)):
# c=m[i]
# if c=='1':
# st=np.matmul(st,ls[i])%md
# print(st[0,k])
#
#
#
#
# # print(bs)
# # print(np.matmul(bs,bs))
``` | instruction | 0 | 5,100 | 12 | 10,200 |
No | output | 1 | 5,100 | 12 | 10,201 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
Submitted Solution:
```
n,m=[int(x)for x in input().split()]
ns=[int(x)for x in input().split()]
k=n-sum(ns)
c=k-sum(ns[:k])
# print(k,c)
def r(num):
return num, k - num, k - num, n - k - (k - num)
def p(num):
a,b,c,d=r(num)
o,z=a+c,b+d
return a*d,o*(o-1)//2+z*(z-1)//2+a*b+c*d,b*c
bs=[[0]*(k+1) for i in range(k+1)]
md=10**9+7
st=[[0]*(k+1)]
st[0][c]=1
deno=n*(n-1)//2
def get_inv(aa,pp):
pp-=2
l=len(bin(pp))-2
ls=[aa]
for i in range(l):
ls.append(ls[-1]*ls[-1]%(pp+2))
# print(ls)
aa=1
for i in range(l):
if (pp>>i)&1:
aa=aa*ls[i]%(pp+2)
return aa%(pp+2)
inv=get_inv(deno,md)
# print('inv of {} : {}'.format(deno,inv))
def matmul(x,y):
ans=[[0]*(k+1) for i in range(len(x))]
for i in range(len(x)):
for j in range(k+1):
t=0
for kk in range(k+1):
t+=x[i][kk]*y[kk][j]%md
ans[i][j]=t%md
return ans
for i in range(k+1):
x=p(i)
for j in range(i-1,i+2):
if 0<=j<=k:
bs[i][j]=x[j-i+1]*inv%md
ls=[bs]
i=0
m=bin(m)[2:][::-1]
# print('m',m)
for i in range(len(m)):
ls.append(matmul(ls[-1],ls[-1]))
# print(ls)
for i in range(len(m)):
c=m[i]
if c=='1':
st=matmul(st,ls[i])
print(st[0][k])
# print(bs)
# print(np.matmul(bs,bs))
``` | instruction | 0 | 5,101 | 12 | 10,202 |
No | output | 1 | 5,101 | 12 | 10,203 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her.
Given an array a of length n, consisting only of the numbers 0 and 1, and the number k. Exactly k times the following happens:
* Two numbers i and j are chosen equiprobable such that (1 β€ i < j β€ n).
* The numbers in the i and j positions are swapped.
Sonya's task is to find the probability that after all the operations are completed, the a array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem.
It can be shown that the desired probability is either 0 or it can be represented as P/Q, where P and Q are coprime integers and Q notβ‘ 0~\pmod {10^9+7}.
Input
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 10^9) β the length of the array a and the number of operations.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 1) β the description of the array a.
Output
If the desired probability is 0, print 0, otherwise print the value P β
Q^{-1} \pmod {10^9+7}, where P and Q are defined above.
Examples
Input
3 2
0 1 0
Output
333333336
Input
5 1
1 1 1 0 0
Output
0
Input
6 4
1 0 0 1 1 0
Output
968493834
Note
In the first example, all possible variants of the final array a, after applying exactly two operations: (0, 1, 0), (0, 0, 1), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (1, 0, 0), (0, 1, 0). Therefore, the answer is 3/9=1/3.
In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is 0.
Submitted Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Codeforces Round #553 (Div. 2)
Problem F. Sonya and Informatics
:author: Kitchen Tong
:mail: kctong529@gmail.com
Please feel free to contact me if you have any question
regarding the implementation below.
"""
__version__ = '1.5'
__date__ = '2019-04-21'
import sys
def binom_dp():
dp = [[-1 for j in range(110)] for i in range(110)]
def calculate(n, k):
if n < k:
return 0
if n == k or k == 0:
return 1
if dp[n][k] > 0:
return dp[n][k]
else:
dp[n][k] = calculate(n-1, k-1) + calculate(n-1, k)
return dp[n][k]
return calculate
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def multiply(A, B, mod):
if not hasattr(B[0], '__len__'):
C = [sum(aij * B[j] % mod for j, aij in enumerate(ai)) for ai in A]
else:
B = list(zip(*B))
C = [multiply(A, B[i], mod) for i in range(len(B[0]))]
return C
def memoize(func):
memo = {}
def wrapper(*args):
M, n, mod = args
if n not in memo:
memo[n] = func(M, n, mod)
return memo[n]
return wrapper
@memoize
def matrix_pow(M, n, mod):
# print(f'n is {n}')
if n == 2:
return multiply(M, M, mod)
if n == 1:
return M
sub_M = matrix_pow(M, n//2, mod)
if n % 2 == 0:
return multiply(sub_M, sub_M, mod)
return multiply(sub_M, matrix_pow(M, n - n//2, mod), mod)
def solve(n, k, a, binom, mod):
ones = sum(a)
zeros = n - ones
M = [[0 for col in range(zeros+1)] for row in range(zeros+1)]
for row in range(max(0, zeros-ones), zeros+1):
pre_zeros = row
pre_ones = zeros - pre_zeros
post_zeros = pre_ones
post_ones = ones - pre_ones
M[row][row] = (pre_ones * post_ones + pre_zeros * post_zeros
+ binom(zeros, 2) + binom(ones, 2))
if row > max(0, zeros-ones):
M[row-1][row] = pre_zeros * post_ones
if row < zeros:
M[row+1][row] = post_zeros * pre_ones
M = [matrix_pow(M, k, mod)[-1]]
b = [0] * (zeros + 1)
b[zeros - sum(a[:zeros])] = 1
C = multiply(M, b, mod)
return C[-1]
def main(argv=None):
mod = int(1e9) + 7
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
binom = binom_dp()
P = solve(n, k, a, binom, mod)
if P == 0:
print(0)
else:
Q = pow(binom(n, 2), k, mod)
print(P * modinv(Q, mod) % mod)
return 0
if __name__ == "__main__":
STATUS = main()
sys.exit(STATUS)
``` | instruction | 0 | 5,102 | 12 | 10,204 |
No | output | 1 | 5,102 | 12 | 10,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,177 | 12 | 10,354 |
Tags: greedy
Correct Solution:
```
from sys import stdin, gettrace
if not gettrace():
def input():
return next(stdin)[:-1]
def main():
def solve():
n = int(input())
bb = [int(a) for a in input().split()]
avail = [False] + [True] * 2 * n
for b in bb:
avail[b] = False
res = []
for b in bb:
if b == n*2:
print(-1)
return
res.append(b)
c = b+1
while not avail[c]:
c+=1
if c == 2*n+1:
print(-1)
return
res.append(c)
avail[c] = False
print(' '.join(map(str, res)))
q = int(input())
for _ in range(q):
solve()
if __name__ == "__main__":
main()
``` | output | 1 | 5,177 | 12 | 10,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,178 | 12 | 10,356 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()));d={}
for i in range(n):
d[a[i]]=1
ans=[];f=0
for i in range(n):
if a[i]<2*n:
for j in range(a[i],(2*n)+2):
if not d.get(j) and j<=2*n:
ans.append(a[i])
ans.append(j)
d[j]=1
break
if j>2*n:
print(-1)
f=1
break
else:
print(-1)
f=1
break
if f==1:
break
if f==0:
for i in ans:
print(i,end=' ')
print()
``` | output | 1 | 5,178 | 12 | 10,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,179 | 12 | 10,358 |
Tags: greedy
Correct Solution:
```
t=int(input())
for i in range(0,t):
n=int(input())
b=[]
c=[]
a=list(map(int,input().split()))
k1=1
k2=n*2
if(k1 in a and k2 not in a):
for j in range(1,2*n+1):
if(j not in a):
b.append(j)
for j in range(0,n):
k=a[j]+1
f=1
while(k not in b):
k=k+1
if(k>2*n):
f=0
break
if(f==0):
print(-1)
break
c.append(a[j])
c.append(k)
b.pop(b.index(k))
if(f==0):
continue
for j in range(0,2*n):
print(c[j],end=" ")
print("")
else:
print(-1)
``` | output | 1 | 5,179 | 12 | 10,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,180 | 12 | 10,360 |
Tags: greedy
Correct Solution:
```
from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def arr():return [int(i) for i in input().split()]
def sarr():return [int(i) for i in input()]
def inn():return int(input())
mo=1000000007
#----------------------------CODE------------------------------#
for _ in range(int(input())):
n=inn()
a=arr()
d=defaultdict(int)
ans=[]
for i in a:
d[i]+=1
for i in range(n):
res=a[i]
for j in range(a[i]+1,2*n+1):
if(j not in d):
ans+=[(res,j)]
d[j]+=1
break
flag=0
for i in range(1,2*n+1):
if(i not in d):
flag=1
break
if(flag==1):
print(-1)
else:
for i in range(n):
print(ans[i][0],ans[i][1],end=" ")
print()
``` | output | 1 | 5,180 | 12 | 10,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,181 | 12 | 10,362 |
Tags: greedy
Correct Solution:
```
from collections import defaultdict as dfd
for _ in range(int(input())):
N = int(input())
A = list(map(int,input().split()))
C = dfd(int)
for i in range(1,(2*N)+1):
C[i] = 0
for i in range(len(A)):
C[A[i]] = 1
B = []
# print(C)
for i in range(len(A)):
B.append(A[i])
z = 0
temp = A[i]+1
while(z!=1):
if(C[temp]==0):
C[temp]=1
B.append(temp)
z = 1
else:
temp+=1
# print("Hello")
res = 0
for i in range(len(B)):
if(B[i]>(2*N)):
print(-1)
res = 1
break
if(res==0):
print(*B)
``` | output | 1 | 5,181 | 12 | 10,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,182 | 12 | 10,364 |
Tags: greedy
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
b = list(map(int, input().split()))
if max(b) >= n*2 or 1 not in b:
print(-1)
else:
a = [0]*(2*n)
ok = True
for i in range(n):
a[i*2] = b[i]
while 0 in a:
moved = False
for i in range(1, 1+2*n):
ind = a.index(0)
if i not in a and i > a[ind-1]:
a[ind] = i
moved = True
break
if not moved:
ok = False
break
if not ok:
print(-1)
else:
print(*a)
``` | output | 1 | 5,182 | 12 | 10,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,183 | 12 | 10,366 |
Tags: greedy
Correct Solution:
```
for T in range(int(input())):
n = int(input())
x = list(map(int, input().split(" ")))
b = [0]
for i in x:
b.append(i)
vis = [False for cnt_used in range(2*n + 1)]
a = [0 for cnt_a in range(2*n + 1)]
set_elem_b = True
for i in range(1, n+1):
num = int(b[i])
if not vis[num]:
vis[num] = True
else:
set_elem_b = False
break
if not set_elem_b:
print(-1)
continue
find = False
for i in range(1, n+1):
find = False
for j in range(1, 2*n+1):
if not vis[j] and j > b[i]:
vis[j] = True
a[i*2-1] = b[i]
a[i*2] = j
find = True
break
if not find:
break
if not find:
print(-1)
continue
for i in range(1, 2*n+1):
print(a[i], end = ' ')
print()
``` | output | 1 | 5,183 | 12 | 10,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,184 | 12 | 10,368 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
b=list(map(int,input().split()))
d={}
for i in range(1,2*n+1):
d[i]=1
for i in b:
d[i]=0
a=[]
for i in b:
a.append(i)
z=i
for j in range(z+1,2*n+1):
if d[j]:
z=j
d[j]=0
break
if z!=i:
a.append(z)
else:
break
if len(a)==2*n:
print(' '.join(map(str,a)))
else:
print(-1)
``` | output | 1 | 5,184 | 12 | 10,369 |
Provide tags and a correct Python 2 solution for this coding contest problem.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | instruction | 0 | 5,185 | 12 | 10,370 |
Tags: greedy
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
for t in range(ni()):
n=ni()
l=li()
d=Counter(l)
arr=[]
for i in range(1,2*n+1):
if not d[i]:
arr.append(i)
f=0
ans=[]
for i in range(n):
f1=0
for j in arr:
if j>l[i]:
f1=1
break
if not f1:
f=1
break
ans.append(l[i])
ans.append(j)
arr.remove(j)
if f:
pn(-1)
else:
pa(ans)
``` | output | 1 | 5,185 | 12 | 10,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
from bisect import bisect
def solution(arr, n):
all_set = [i for i in range(1, (2 * n) + 1)]
for i, x in enumerate(arr):
all_set.remove(arr[i])
res = []
for i in range(n):
idx = bisect(all_set, arr[i])
if idx == len(all_set):
print(-1)
return
res.append(arr[i])
res.append(all_set[idx])
all_set.pop(idx)
for x in res:
print(x, end=' ')
print()
def main():
t = int(input())
for _ in range(t):
n = int(input())
arr = [int(x) for x in input().split()]
solution(arr, n)
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)
def input():
return sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | instruction | 0 | 5,186 | 12 | 10,372 |
Yes | output | 1 | 5,186 | 12 | 10,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
y = [*map(int, input().split())]
x = {*range(1, 2*n+1)}.difference(y)
res = []
for i in y:
a = [j for j in x if i < j]
if a:
b = min(a)
x.remove(b)
res.extend([i, b])
else:
print(-1)
break
if not x:
print(*res)
``` | instruction | 0 | 5,187 | 12 | 10,374 |
Yes | output | 1 | 5,187 | 12 | 10,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
I=input
for _ in[0]*int(I()):
n=2*int(I());a=[0]*n;b=a[::2]=*map(int,I().split()),;c={*range(n+1)}-{*b};i=1
try:
for x in b:y=a[i]=min(c-{*range(x)});c-={y};i+=2
except:a=-1,
print(*a)
``` | instruction | 0 | 5,188 | 12 | 10,376 |
Yes | output | 1 | 5,188 | 12 | 10,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
b = list(map(int, input().split()))
for i in range(n):
b[i] -= 1
# print("b: ", b)
used = [False for _ in range(2*n)]
for i in range(n):
used[b[i]] = True
ans = [0 for i in range(2*n)]
for i in range(n):
ans[2*i] = b[i]
result = True
for i in range(n):
found_cand = False
for cand in range(ans[2*i]+1, 2*n):
if not used[cand]:
used[cand] = True
ans[2*i+1] = cand
found_cand = True
break
if not found_cand:
result = False
if not result:
print(-1)
else:
print(" ".join([str(item+1) for item in ans]))
# print("ans: ", ans)
# print("used: ", used)
# print()
``` | instruction | 0 | 5,189 | 12 | 10,378 |
Yes | output | 1 | 5,189 | 12 | 10,379 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr('\n'.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
for t in range(ni()):
n=ni()
l=li()
d=Counter(l)
arr=[]
for i in range(1,2*n+1):
if not d[i]:
arr.append(i)
pos=0
d1=Counter()
f=0
for i in sorted(d):
if arr[pos]<i:
f=1
break
d1[i]=arr[pos]
pos+=1
if f:
print -1
continue
for i in range(n):
print l[i],d1[l[i]],
print
``` | instruction | 0 | 5,190 | 12 | 10,380 |
No | output | 1 | 5,190 | 12 | 10,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
'''
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
'''
class NotFoundError(Exception):
def __str__(self):
return "Lexicograohic sequence nnot found"
def returnNumber(bFinal, a, index):
for i in range(len(a)):
# print(a[i], bFinal[index])
if a[i] > bFinal[index] and a[i] not in bFinal:
return a[i]
raise NotFoundError()
testCases = int(input())
for t in range(testCases):
n = int(input())
b = list(map(int, input().rstrip().split()))
a = [i for i in range(1, 2*n + 1)]
bFinal = [None]*2*n
for i in range(len(b)):
bFinal[2*i] = b[i]
for i in b:
if i in a:
a.pop(a.index(i))
# print(b)
# print(a)
# print(bFinal)
for i in range(n):
try:
bFinal[2*i + 1] = returnNumber(bFinal, a, 2*i)
# print("bFinal[2*i + 1]= ", bFinal[2*i + 1])
except NotFoundError as e:
# print(e)
bFinal = -1
break
# print(bFinal)
if type(bFinal) is list:
print(bFinal)
else:
print(-1)
``` | instruction | 0 | 5,191 | 12 | 10,382 |
No | output | 1 | 5,191 | 12 | 10,383 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
from sys import stdin
from collections import defaultdict
input=stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
dict=defaultdict(int)
arr=[]
for i in range(n):
arr.append([a[i],i])
dict[a[i]]=1
arr.sort()
#print(arr)
tmp=[]
for i in range(1,2*n+1):
if dict[i]==0:
tmp.append(i)
#print(tmp)
fl=0
farr={}
for i in range(len(tmp)):
if arr[i][0]>tmp[i]:
fl=1
break
else:
farr[arr[i][0]]=tmp[i]
if fl==1:
print(-1)
else:
for x in a:
print(x,farr[x],end=" ")
print()
``` | instruction | 0 | 5,192 | 12 | 10,384 |
No | output | 1 | 5,192 | 12 | 10,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
import heapq as pq
def solve():
n = int(input())
A = list(map(int,input().split()))
d = dict()
arr = [0 for _ in range(2*n)]
for i in range(n):
d[A[i]] = i
arr[2*i] = A[i]
A[i] = (A[i],i)
remaining = []
pq.heapify(remaining)
for i in range(1,(2*n)+1):
if d.get(i,-1)==-1:
pq.heappush(remaining,i)
# print("sds")
# print(A,arr,remaining)
A.sort(key = lambda x: x[0]+x[1])
for i in range(n):
curr1 = pq.heappop(remaining)
if A[i][0]>curr1:
print(-1)
return
arr[(2*d.get(A[i][0]))+1] = curr1
for i in range(2*n):
if i==(2*n)-1:
print(arr[i])
else:
print(arr[i],end=" ")
return
if __name__ == '__main__':
t = int(input())
for _ in range(t):
solve()
``` | instruction | 0 | 5,193 | 12 | 10,386 |
No | output | 1 | 5,193 | 12 | 10,387 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
ar1 = list(map(int, input().split()))
ar2 = []
kek = set()
for i in range(n):
kek.add(ar1[i])
ar2.append([ar1[i], 0, i])
ar2.sort()
j = 0
flag = 0
for i in range(1, 2 * n + 1):
if i not in kek:
if i < ar2[j][0]:
flag = 1
break
ar2[j][1] = i
j += 1
if flag == 1:
print(-1)
else:
ar2.sort(key=lambda x:x[2])
ans = []
for a, b, c in ar2:
ans.append(a)
ans.append(b)
print(*ans)
``` | instruction | 0 | 5,194 | 12 | 10,388 |
No | output | 1 | 5,194 | 12 | 10,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains three integers n, k, and m (3 β€ n β€ 2 β
10^5; 3 β€ k β€ n; k is odd; 1 β€ m < n) β the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 β€ b_1 < b_2 < ... < b_m β€ n) β the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements β it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | instruction | 0 | 5,236 | 12 | 10,472 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
for _ in range(int(sys.stdin.readline().strip())):
n,k,m=tuple(map(int,sys.stdin.readline().strip().split(" ")))
ml=set(map(int,sys.stdin.readline().strip().split(" ")))
havitada=[]
for i in range(1,n+1):
if i not in ml:
havitada.append(i)
saab=False
if len(havitada)%(k-1)!=0:
print("no")
continue
for i in range(k//2-1,len(havitada)-k//2):
if havitada[i+1]-havitada[i]!=1:
print("yes")
break
else:
print("no")
``` | output | 1 | 5,236 | 12 | 10,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains three integers n, k, and m (3 β€ n β€ 2 β
10^5; 3 β€ k β€ n; k is odd; 1 β€ m < n) β the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 β€ b_1 < b_2 < ... < b_m β€ n) β the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements β it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | instruction | 0 | 5,237 | 12 | 10,474 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
def load_sys():
return sys.stdin.readlines()
def load_local():
with open('input.txt','r') as f:
input = f.readlines()
return input
def km(n,k,m,B):
if (n-m)%(k-1) != 0:
return 'NO'
R = [0]*m
L = [0]*m
for i in range(m):
R[i] = n-B[i]-(m-i-1)
L[i] = n-m-R[i]
if R[i] >= k//2 and L[i] >= k//2:
return 'YES'
return 'NO'
#input = load_local()
input = load_sys()
idx = 1
N = len(input)
while idx < len(input):
n,k,m = [int(x) for x in input[idx].split()]
B = [int(x) for x in input[idx+1].split()]
idx += 2
print(km(n,k,m,B))
``` | output | 1 | 5,237 | 12 | 10,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains three integers n, k, and m (3 β€ n β€ 2 β
10^5; 3 β€ k β€ n; k is odd; 1 β€ m < n) β the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 β€ b_1 < b_2 < ... < b_m β€ n) β the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements β it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | instruction | 0 | 5,238 | 12 | 10,476 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
T = int(input())
for _ in range(T):
n, k, m = map(int, input().split())
b = [int(i) for i in input().split()]
ff = False
for p, i in enumerate(b):
if i - p - 1 >= k>>1 and n - i - len(b) + p + 1 >= k>>1:
ff = True
if b==[int(i)+1 for i in range(n)]:
print('YES')
elif k>1 and (n-m)%(k-1)==0:
if ff:
print('YES')
else:
print('NO')
else:
print('NO')
``` | output | 1 | 5,238 | 12 | 10,477 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.