text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
R = range(100)
c = [[] for _ in [0]*100]
for i in range(n*2):
c[a[i]].append(i)
d = [0]*200
heap = 1
z = [0, 0, 0]
for i in R:
if len(c[i]) == 1:
z[heap]+=1
d[c[i][0]] = heap
heap = 3 - heap;
for i in R:
if len(c[i]) > 1:
z[1]+=1
z[2]+=1
while len(c[i]):
d[c[i].pop()] = heap
heap = 3 - heap
print(z[1]*z[2])
print(' '.join(map(str, d[:n*2])))
# Made By Mostafa_Khaled
```
| 87,900 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
from collections import defaultdict as defdict
n = int(input())
d = defdict(int)
a = list(map(int, input().split()))
for i in a:
d[i] += 1
b = set(i for i in a if d[i] > 1)
c = [i for i in a if d[i] == 1]
##print('b =', b)
mx = len(b) + (len(c) // 2)
d = defdict(int)
answ = [None] * (n * 2)
cnt_diff_1 = 0
cnt_diff_2 = 0
for i, x in enumerate(a):
if cnt_diff_1 < mx:
if x in b:
if d[x] == 0:
answ[i] = '1'
d[x] += 1
cnt_diff_1 += 1
for i, x in enumerate(a):
if cnt_diff_1 < mx:
if x not in b:
answ[i] = '1'
cnt_diff_1 += 1
##print(d)
cnt1 = cnt_diff_1
cnt2 = 0
##print(cnt_diff_1)
##print(answ)
##print(mx)
for i, x in enumerate(a):
if answ[i] == '1': continue
if x in b:
if d[x] == 0:
answ[i] = '2'
d[x] = 2
cnt_diff_2 += 1
cnt2 += 1
elif d[x] == 1:
answ[i] = '2'
d[x] += 1
cnt_diff_2 += 1
cnt2 += 1
else:
if cnt1 < n:
answ[i] = '1'
cnt1 += 1
else:
answ[i] = '2'
cnt2 += 1
else:
if cnt2 < n:
answ[i] = '2'
cnt2 += 1
cnt_diff_2 += 1
else:
answ[i] = '1'
cnt1 += 1
## print('cnt2 =', cnt2)
## print(cnt_diff_2)
## print('cnt1 =', cnt1)
cnt_diff_1 += 1
##print(cnt_diff_1, cnt_diff_2)
##print()
##cnt1 = len(set(a[i] for i in range(2 * n) if answ[i] == '1'))
##cnt2 = len(set(a[i] for i in range(2 * n) if answ[i] == '2'))
##print(cnt1, cnt2)
print(cnt_diff_1 * cnt_diff_2)
print(' '.join(answ))
```
| 87,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
g=[[] for i in range(100)]
for i in range(2*n):
g[arr[i]].append(i)
x=0
y=0
curr=1
r=[]
for i in range(10,100):
if len(g[i])==1:
arr[g[i][0]]=curr
if curr==1:
x+=1
else:
y+=1
curr=3-curr
if len(g[i])>1:
arr[g[i][0]]=1
arr[g[i][1]]=2
x+=1
y+=1
for j in range(2,len(g[i])):
r.append(g[i][j])
for i in range(len(r)):
arr[r[i]]=2-(i%2)
print(x*y)
print(*arr)
```
| 87,902 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
from math import*
from random import*
n = int(input()) * 2
A = list(map(int, input().split()))
amount = [0] * 101
B = []
for i in range(n):
if amount[A[i]] < 2:
amount[A[i]] += 1
B += [(A[i], i)]
B.sort()
x, y = [], []
for i in range(len(B)):
if(i % 2 == 0):
x.append(B[i][1])
else:
y.append(B[i][1])
lolka = 0
aaa = 0
# print(x)
# print(y)
print(len(x) * len(y))
for i in range(n):
if i in x:
lolka += 1
aaa += 1
print(1, end = ' ')
elif i in y:
print(2, end = ' ')
else:
if len(x) - lolka + aaa < n // 2:
print(1, end = ' ')
aaa += 1
else:
print(2, end = ' ')
print()
# B, C = [], []
# for i in range(n):
# S = list(set(A))
# where = [0] * 101
# am1, am2 = 0, 0
# for i in range(len(S)):
# if(i % 2 == 0):
# where[S[i]] = 1
# am1 += 1
# else:
# where[S[i]] = 2
# am2 += 1
# used = [0] * 201
# for i in range(n):
# if not used[A[i]]:
# print(where[A[i]])
# used[A[i]] = True
# else:
# print(3 - where[A[i]])
```
| 87,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
c = [0] * 100
for i in a:
c[i] += 1
x = [0] * 100
y = [0] * 100
j = 0
for i in range(100):
if c[i] == 1:
[x, y][j][i] += 1
j = 1 - j
for i in range(100):
if c[i] != 1:
x[i] += c[i] // 2
y[i] += c[i] // 2
if c[i] % 2 == 1:
[x, y][j][i] += 1
j = 1 - j
xk = len(list(filter(lambda it: it, x)))
yk = len(list(filter(lambda it: it, y)))
print(xk * yk)
zs = [None] * (2*n)
for i in range(2*n):
if x[a[i]] > 0:
x[a[i]] -= 1
zs[i] = 1
else:
assert y[a[i]] > 0
y[a[i]] -= 1
zs[i] = 2
print(*zs)
```
| 87,904 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
from collections import *
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
for i in range(2*n):
a[i] = [a[i],i]
a.sort()
ans = [0 for i in range(2*n)]
da, db = {}, {}
f = 0
for i in range(2*n):
if(c[a[i][0]] == 1): continue
ans[a[i][1]] = f + 1
if(f&1):
da[a[i][0]] = 1
else:
db[a[i][0]] = 1
f ^= 1
for i in range(2*n):
if(ans[a[i][1]] != 0): continue
ans[a[i][1]] = f+1
if(f&1):
da[a[i][0]] = 1
else:
db[a[i][0]] = 1
f ^= 1
print(len(da)*len(db))
print(*ans)
```
| 87,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
n, t = 2 * int(input()), map(int, input().split())
d = [[] for i in range(100)]
for i, j in enumerate(t):
d[j].append(i)
y, x = [], False
p, q = [], ['1'] * n
for i in d[10: 100]:
if i:
if len(i) == 1:
if x: y.append(i[0])
x = not x
else:
y.append(i[0])
p += i[2: ]
k, l = len(p), len(y)
print(l * (n - k - l))
for i in y:
q[i] = '2'
for i in (p[k // 2: ] if x else p[: k // 2]):
q[i] = '2'
print(' '.join(q))
```
| 87,906 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
N = int(input())
Nums = list(map(int, input().split()))
Good = [1] * (N * 2)
Amounts = [0] * 100
Mono, Duo = 0, 0
for Num in Nums:
if Amounts[Num] == 0:
Mono += 1
elif Amounts[Num] == 1:
Duo += 1
Mono -= 1
Amounts[Num] += 1
Flag = Mono % 2
Duo_Flag = False
Counts = [0] * 100
for i in range(2 * N):
Num = Nums[i]
if Amounts[Num] == 1:
if Flag:
Good[i] = 1
else:
Good[i] = 2
Flag = not Flag
else:
if Counts[Num] == 0:
Good[i] = 1
elif Counts[Num] == 1:
Good[i] = 2
else:
if Duo_Flag:
Good[i] = 1
else:
Good[i] = 2
Duo_Flag = not Duo_Flag
Counts[Num] += 1
print((Duo + (Mono // 2)) * (Duo + ((Mono + 1) // 2)))
print(*Good)
```
Yes
| 87,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = 2 * int(input())
t = list(map(int, input().split()))
q, p, c = [0] * n, [0] * 100, [0, 0]
for i in t:
p[i] += 1
for i in p:
if i: c[i == 1] += 1
k = c[1] // 2
print((c[0] + k) * (c[0] + c[1] - k))
r = sorted([(p[j], j, i) for i, j in enumerate(t)])
for i in r[0: n: 2]:
q[i[2]] = '1'
for i in r[1: n: 2]:
q[i[2]] = '2'
print(' '.join(q))
```
Yes
| 87,908 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
#!/usr/bin/python3
def readln(): return tuple(map(int, input().split()))
n, = readln()
b = readln()
cnt = [0] * 101
for v in b:
cnt[v] += 1
f = []
s = []
x = []
y = []
for i in range(10, 101):
if cnt[i] > 1:
f.extend([i] * (cnt[i] // 2))
s.extend([i] * (cnt[i] // 2))
if cnt[i] % 2:
y.append(i)
cnt[i] = 0
elif cnt[i] == 1:
x.append(i)
f.extend(x[:len(x)//2])
f.extend(y[:n - len(f)])
s.extend(x[len(x)//2:])
if len(s) < n:
s.extend(y[-n + len(s):])
print(len(set(f)) * len(set(s)));
ans = [1] * 2 * n
for v in s:
for i in range(2 * n):
if b[i] == v and ans[i] == 1:
ans[i] = 2
break
print(*ans)
```
Yes
| 87,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
def s():
input()
l = [[]for _ in range(100)]
a = list(map(int,input().split()))
for i,v in enumerate(a):
l[v].append(i)
c = 0
cc = 0
fs = [0,0]
for i in l:
if len(i) == 0:
continue
if len(i) == 1:
a[i[0]] = c+1
fs[c]+=1
c = 1 - c
continue
fs[c] += 1
fs[c-1] += 1
for e in i[:len(i)//2]:
a[e] = 1 + cc
for e in i[len(i)//2:]:
a[e] = 2 - cc
if len(i) % 2 == 1:
cc = 1 - cc
print(fs[0]*fs[1])
print(*a)
s()
```
Yes
| 87,910 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = int(input())
set1 = set()
set2 = set()
res = [0] * (2 * n)
S = list(map(int, input().split()))
#A = [[S[i], i] for i in range(2 * n)]
c = 0
for i in sorted([[S[i], i] for i in range(2 * n)]):
if c == 0:
set1.add(i[0])
res[i[1]] = 1
c = 1
else:
set2.add(i[0])
res[i[1]] = 2
c = 0
print(len(set1) * len(set2))
print(' '.join(map(str, res)))
```
No
| 87,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
flip2=0
la=0
lb=0
a={}
b={}
for i in range(2*n):
if arr[i] in a and arr[i] in b:
if flip2==0 and la<n:
a[arr[i]]+=1
arr[i]="1"
flip2=1
la+=1
else:
b[arr[i]]+=1
arr[i]="2"
flip2=0
lb+=1
elif la<=lb:
if arr[i] in b:
a[arr[i]]=1
arr[i]="1"
la+=1
else:
if arr[i] in a:
if lb<n:
b[arr[i]]=1
arr[i]="2"
lb+=1
else:
a[arr[i]]+=1
arr[i]="1"
la+=1
else:
a[arr[i]]=1
arr[i]="1"
la+=1
else:
if arr[i] in a:
b[arr[i]]=1
arr[i]="2"
lb+=1
else:
if arr[i] in b:
if la<n:
a[arr[i]]=1
arr[i]="1"
la+=1
else:
b[arr[i]]+=1
arr[i]="2"
lb+=1
else:
b[arr[i]]=1
arr[i]="2"
lb+=1
print(len(a)*len(b))
print(" ".join(arr))
```
No
| 87,912 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
first = []
second = []
ans = []
d = {}
for i in list(set(a)):
d[i] = i
for i in a:
if d[i] % 2 == 0:
first.append(i)
ans.append(1)
else:
second.append(i)
ans.append(2)
d[i] += 1
#if len(first) != len(second):
# exit(1)
print(len(list(set(first))) * len(list(set(second))))
print(*ans)
```
No
| 87,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
Output
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = int(input())
heap_1 = set()
heap_2 = set()
arr = list(map(int, input().split()))
ans = [0] * (2 * n)
for i in range(2 * n):
new = arr[i]
if new not in heap_1 and new not in heap_2:
if len(heap_1) > len(heap_2):
heap_2.add(new)
ans[i] = 2
else:
heap_1.add(new)
ans[i] = 1
elif new not in heap_1 and new in heap_2:
heap_1.add(new)
ans[i] = 1
elif new not in heap_2 and new in heap_1:
heap_2.add(new)
ans[i] = 2
print(len(heap_1) * len(heap_2))
print(*ans)
```
No
| 87,914 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kostya is a progamer specializing in the discipline of Dota 2. Valve Corporation, the developer of this game, has recently released a new patch which turned the balance of the game upside down. Kostya, as the captain of the team, realizes that the greatest responsibility lies on him, so he wants to resort to the analysis of innovations patch from the mathematical point of view to choose the best heroes for his team in every game.
A Dota 2 match involves two teams, each of them must choose some heroes that the players of the team are going to play for, and it is forbidden to choose the same hero several times, even in different teams. In large electronic sports competitions where Kostya's team is going to participate, the matches are held in the Captains Mode. In this mode the captains select the heroes by making one of two possible actions in a certain, predetermined order: pick or ban.
* To pick a hero for the team. After the captain picks, the picked hero goes to his team (later one of a team members will play it) and can no longer be selected by any of the teams.
* To ban a hero. After the ban the hero is not sent to any of the teams, but it still can no longer be selected by any of the teams.
The team captain may miss a pick or a ban. If he misses a pick, a random hero is added to his team from those that were available at that moment, and if he misses a ban, no hero is banned, as if there was no ban.
Kostya has already identified the strength of all the heroes based on the new patch fixes. Of course, Kostya knows the order of picks and bans. The strength of a team is the sum of the strengths of the team's heroes and both teams that participate in the match seek to maximize the difference in strengths in their favor. Help Kostya determine what team, the first one or the second one, has advantage in the match, and how large the advantage is.
Input
The first line contains a single integer n (2 ≤ n ≤ 100) — the number of heroes in Dota 2.
The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 106) — the strengths of all the heroes.
The third line contains a single integer m (2 ≤ m ≤ min(n, 20)) — the number of actions the captains of the team must perform.
Next m lines look like "action team", where action is the needed action: a pick (represented as a "p") or a ban (represented as a "b"), and team is the number of the team that needs to perform the action (number 1 or 2).
It is guaranteed that each team makes at least one pick. Besides, each team has the same number of picks and the same number of bans.
Output
Print a single integer — the difference between the strength of the first team and the strength of the second team if the captains of both teams will act optimally well.
Examples
Input
2
2 1
2
p 1
p 2
Output
1
Input
6
6 4 5 4 5 5
4
b 2
p 1
b 1
p 2
Output
0
Input
4
1 2 3 4
4
p 2
b 2
p 1
b 1
Output
-2
Submitted Solution:
```
n = int(input().strip())
arr = [int(x) for x in input().strip().split(' ')]
m = int(input().strip())
actions = []
for i in range(m):
actions.append(input().strip().split(' '))
for i in range(m):
actions[i][1] = int(actions[i][1])
i =0
while i < m-1:
j = 0
while i+j < m-1 and actions[i][1] == actions[i+j+1][1] and actions[i][0] == 'b' and actions[i+j+1][0] == 'p':
j += 1
if j > 0:
actions[i][0] = 'p'
actions[i+j][0] = 'b'
i = i+j+1
else:
i += 1
arr.sort()
arr.reverse()
j = 0
d = {1:0, 2:0}
for i in actions:
if i[0] == 'p':
d[i[1]]+=arr[j]
j += 1
print(d[1]-d[2])
```
No
| 87,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
import sys
from functools import reduce
from collections import Counter
import time
import datetime
from math import sqrt,gcd
# def time_t():
# print("Current date and time: " , datetime.datetime.now())
# print("Current year: ", datetime.date.today().strftime("%Y"))
# print("Month of year: ", datetime.date.today().strftime("%B"))
# print("Week number of the year: ", datetime.date.today().strftime("%W"))
# print("Weekday of the week: ", datetime.date.today().strftime("%w"))
# print("Day of year: ", datetime.date.today().strftime("%j"))
# print("Day of the month : ", datetime.date.today().strftime("%d"))
# print("Day of week: ", datetime.date.today().strftime("%A"))
def ip(): return int(sys.stdin.readline())
def sip(): return sys.stdin.readline()
def mip(): return map(int,sys.stdin.readline().split())
def mips(): return map(str,sys.stdin.readline().split())
def lip(): return list(map(int,sys.stdin.readline().split()))
def matip(n,m):
lst=[]
for i in range(n):
arr = lip()
lst.insert(i,arr)
return lst
def factors(n): # find the factors of a number
return list(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def minJumps(arr, n): #to reach from 0 to n-1 in the array in minimum steps
jumps = [0 for i in range(n)]
if (n == 0) or (arr[0] == 0):
return float('inf')
jumps[0] = 0
for i in range(1, n):
jumps[i] = float('inf')
for j in range(i):
if (i <= j + arr[j]) and (jumps[j] != float('inf')):
jumps[i] = min(jumps[i], jumps[j] + 1)
break
return jumps[n-1]
def dic(arr): # converting list into dict of count
return Counter(arr)
def check_prime(n):
if n<2:
return False
for i in range(2,int(n**(0.5))+1,2):
if n%i==0:
return False
return True
# --------------------------------------------------------- #
# sys.stdin = open('input.txt','r')
# sys.stdout = open('output.txt','w')
# --------------------------------------------------------- #
n = ip()
s = sip()
up,down = 0,0
lst= []
for i in range(n):
lst.append(s[i])
if s[i]=='X':
up+=1
else:
down+=1
if up==down:
print(0)
print(s)
elif up>down:
recq = n//2-down
print(recq)
for i in range(n):
if lst[i]=='X':
lst[i]='x'
recq-=1
if recq==0:
break
print(''.join(lst))
else:
recq = n//2-up
print(recq)
for i in range(n):
if lst[i]=='x':
lst[i]='X'
recq-=1
if recq==0:
break
print(''.join(lst))
```
| 87,916 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
n=input()
n=int(n)
y=input()
x_s=0
x_c=0
b=""
for i in range(n):
if y[i]=='x':
x_s+=1
else:
x_c+=1
if x_s==x_c:
print(0)
for i in y:
print(i ,end="")
if x_s>x_c:
u=n/2-x_c
o=n/2-x_c
while u>0:
for i in range(n):
if y[i]=='x' and not u<1:
b+='X'
u -= 1
else:
b+=y[i]
print(int(o))
for i in b:
print(i, end="")
if x_s<x_c:
u=n/2-x_s
o = n / 2 - x_s
while u>0:
for i in range(n):
if y[i]=='X' and not u<1:
b+='x'
u-=1
else:
b+=y[i]
print(int(o))
for i in b:
print(i, end="")
```
| 87,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
n = int(input())
s = list(input())
count = s.count('X')
if count < n//2:
t = count
for i in range(n):
if t < n//2 and s[i] == 'x':
s[i] = 'X'
t += 1
print(t-count)
print(''.join(s))
elif count > n//2:
t = count
for i in range(n):
if t > n//2 and s[i] == 'X':
s[i] = 'x'
t -= 1
print(count-t)
print(''.join(s))
else:
print(0)
print(''.join(s))
```
| 87,918 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
n = int(input())
s = input()
diff = (s.count('x') - s.count('X')) // 2
print(abs(diff))
s = list(s)
for i in range(len(s)):
if s[i] == 'X' and diff < 0:
s[i] = 'x'
diff += 1
elif s[i] == 'x' and diff > 0:
s[i] = 'X'
diff -= 1
print(''.join(s))
```
| 87,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
n=int(input());s=list(input());x=s.count('x');b=n//2-min(x,n-x)
if (n-x)==x:t=''
elif (n-x)>x:t='X'
else:t='x'
print(b)
for i in range(n):
if s[i]==t:b-=1;s[i]='x' if t=='X' else 'X'
if b==0:break
print(''.join(s))
```
| 87,920 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
n = int(input());a = input()
i = (max(a.count('x'), a.count('X')) - n // 2)
print(i, a.replace(['x', 'X'][a.count('x') < a.count('X')], ['x', 'X'][a.count('x') > a.count('X')], i))
```
| 87,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
n = int(input())
s = input()
print(abs(s.count('X') - n//2))
if s.count('X') > n // 2:
print(s.replace('X', 'x', s.count('X') - n // 2))
elif s.count('X') == n//2:
print(s)
else:
print(s.replace('x', 'X', s.count('x') - n // 2))
```
| 87,922 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Tags: implementation
Correct Solution:
```
n = int(input())
s = input()
a = 0
b = 0
s = list(s)
for i in range(n):
if s[i] == 'x':
a += 1
else:
b += 1
if a > n // 2:
counter = 0
for i in range(len(s)):
if s[i] == 'x' and counter + a > n // 2:
s[i] = 'X'
counter -= 1
else:
counter = 0
for i in range(len(s)):
if s[i] == 'X' and counter + b > n // 2:
s[i] = 'x'
counter -= 1
print(abs(a-b)//2)
yay = ""
for i in range(len(s)):
yay += s[i]
print(yay)
```
| 87,923 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
# Made By Mostafa_Khaled
bot = True
input()
s = input()
c1 = s.count('x')
c2 = s.count('X')
s = s.replace('x', 'X', max(0, (c1-c2)//2))
s = s.replace('X', 'x', max(0, (c2-c1)//2))
print(abs(c2-c1)//2)
print(s)
# Made By Mostafa_Khaled
```
Yes
| 87,924 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
n = int(input())
s = input()
numX = s.count("X");
if numX < n // 2:
print(n // 2 - numX)
print(str.replace(s, "x", "X", n // 2 - numX))
else:
print(numX - n // 2)
print(str.replace(s, "X", "x", numX - n // 2))
```
Yes
| 87,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
n = int(input())
line = list(input())
x = line.count('x')
X = line.count('X')
res = max(x, X) - n//2
if x < n//2:
for i in range(n):
if line[i] == 'X':
line[i] = 'x'
x+=1
if x==n//2:
break
elif x>n//2:
for i in range(n):
if line[i]=='x':
line[i] = 'X'
X+=1
if n//2==X:
break
print(res)
print(''.join(line))
```
Yes
| 87,926 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
n = int(input())
ls = input()
val = abs(ls.count('X')-ls.count('x'))/2
print(int(val))
if val!=0:
ch = 'X' if ls.count('X')<ls.count('x') else 'x'
nstr = ''
for i in ls:
if val>0:
if i!=ch:
nstr+=ch
val-=1
else:
nstr+=ch
else:
nstr+=i
print(nstr)
else:
print(ls)
```
Yes
| 87,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return (int(input()))
def inlt():
return (list(map(int, input().split())))
def insr():
s = input()
return list(s[:len(s) - 1])
def invr():
return (map(int, input().split()))
n = inp()
s = insr()
i = 0
j = 0
for a in range(len(s)):
if s[a] == 'x':
i = i+1
elif s[a] == 'X':
j = j+1
if i == j:
print(0)
elif i > j:
n = n - 2*j
print(int(n/2))
elif j > i:
n = n - 2*i
print(int(n/2))
```
No
| 87,928 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
# Codeforces A. Squats
# Created by Abdulrahman Elsayed on 13/01/2021
n = int(input())
pos1 = list(input())
down = 0
up = 0
for c in pos1:
if c == 'x':
down +=1
else:
up +=1
t = 1
i = 0
if (up > down):
r = up - down - 1
print(r)
while(t):
if (pos1[i] == 'X'):
pos1[i] = 'x'
r -= 1
i += 1
if (r == 0):
t = 0
elif (up < down):
r = down - up - 1
print(r)
while(t):
if (pos1[i] == 'x'):
pos1[i] = 'X'
r -= 1
i += 1
if (r == 0):
t = 0
else:
print(0)
pos2 = str()
for c in pos1:
pos2 += c
print(pos2)
```
No
| 87,929 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
#! /usr/bin/env python
n = int(input())
s = list(input())
cnt = s.count('x')
ans = cnt - n // 2;
if ans > 0:
tmp = ans;
for i, x in enumerate(s):
if x == 'x':
s[i] = 'X'
tmp -= 1
if tmp == 0:
break
elif ans < 0:
tmp = -ans;
for i, x in enumerate(s):
if x == 'X':
s[i] = 'x'
tmp -= 1
if tmp == 0:
break
print(ans)
print(''.join(s))
```
No
| 87,930 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly <image> hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Examples
Input
4
xxXx
Output
1
XxXx
Input
2
XX
Output
1
xX
Input
6
xXXxXx
Output
0
xXXxXx
Submitted Solution:
```
n = int(input(""))
hams = input()
x = [0 if char == "x" else 1 for char in hams]
counter = 0
sit = x.count(0)
stand = x.count(1)
diff = abs(sit-stand)
if diff == 0:
print(0, hams, sep='\n')
else:
for i in range(n):
if stand == sit:
break
if x[i] == 0 and stand <= diff//2:
counter += 1
x[i] = 1
sit -= 1
stand += 1
elif x[i] == 1 and stand >= diff//2:
counter += 1
x[i] = 0
sit += 1
stand -= 1
print(counter, "x"*sit+"X"*stand, sep='\n')
```
No
| 87,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Tags: binary search, brute force
Correct Solution:
```
import math
def good(x):
# Returns true if there are at least k table members <= x
c = 0
for i in range(1, n + 1):
c += min(m, math.floor(x / i))
if c >= k:
return True
return False
n, m, k = [int(x) for x in input().split(' ')]
if n * m == k:
print(k)
else:
l = 0
r = k
while r - l > 1 * ((10 ** 1) ** (int(n == 499997))):
h = (l + r) // 2
if good(h):
r = h
else:
l = h
print(r)
```
| 87,932 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Tags: binary search, brute force
Correct Solution:
```
def works(X,N,M,K):
#in each row, how many numbers are < X
res = 0
n = 1
div = X/M
while n < div:
res += M
n += 1
while n < N+1:
res += (X-1)//n
n += 1
return res
def solve():
N, M, K = [int(s) for s in input().split()]
left = 1
right = K+1
#we want the smallest smallest such that there are AT LEAST K-1 smaller numbers
while right - left > 1:
middle = (left+right)//2
if works(middle,N,M,K) < K:
left = middle
else:
right = middle
#if there are exactly K-1 elements less than right, then this is our answer
return left
#for _ in range(getInt()):
print(solve())
```
| 87,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Tags: binary search, brute force
Correct Solution:
```
# import sys
# from functools import lru_cache, cmp_to_key
# from heapq import merge, heapify, heappop, heappush
# from math import *
# from collections import defaultdict as dd, deque, Counter as C
# from itertools import combinations as comb, permutations as perm
# from bisect import bisect_left as bl, bisect_right as br, bisect
# from time import perf_counter
# from fractions import Fraction
# import copy
# import time
# starttime = time.time()
# mod = int(pow(10, 9) + 7)
# mod2 = 998244353
# def data(): return sys.stdin.readline().strip()
# def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
# def L(): return list(sp())
# def sl(): return list(ssp())
# def sp(): return map(int, data().split())
# def ssp(): return map(str, data().split())
# def l1d(n, val=0): return [val for i in range(n)]
# def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
# try:
# # sys.setrecursionlimit(int(pow(10,4)))
# sys.stdin = open("input.txt", "r")
# # sys.stdout = open("../output.txt", "w")
# except:
# pass
# def pmat(A):
# for ele in A:
# print(*ele,end="\n")
# def seive():
# prime=[1 for i in range(10**6+1)]
# prime[0]=0
# prime[1]=0
# for i in range(10**6+1):
# if(prime[i]):
# for j in range(2*i,10**6+1,i):
# prime[j]=0
# return prime
import math
def good(x):
# Returns true if there are at least k table members <= x
c = 0
for i in range(1, n + 1):
c += min(m, math.floor(x / i))
if c >= k:
return True
return False
n, m, k = [int(x) for x in input().split(' ')]
if n * m == k:
print(k)
else:
l = 0
r = k
while r - l > 1 * ((10 ** 1) ** (int(n == 499997))):
h = (l + r) // 2
if good(h):
r = h
else:
l = h
print(r)
# endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")
```
| 87,934 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Tags: binary search, brute force
Correct Solution:
```
def main():
from math import sqrt
m, n, k = map(int, input().split())
if n < m:
n, m = m, n
lo, hi = 1, k + 1
while lo + 1 < hi:
mid = (lo + hi) // 2
t = mid - 1
v = min(int(sqrt(t)), m)
tn, tm = (t - 1) // m, t // n
vv = [t // i for i in range(tm + 1, v + 1)]
if t // n * (n + m) + sum(vv) * 2 + max(min((tn - tm), len(vv)) * m, 0) - v * v - sum(
vv[:max(min(tn - tm, len(vv)), 0)]) < k:
lo = mid
else:
hi = mid
print(lo)
if __name__ == '__main__':
main()
```
| 87,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Submitted Solution:
```
n, m, k = map(int, input().split())
idx = 1
while idx * (m + n) - idx * idx < k:
idx += 1
a = []
for i in range(idx, n + 1):
a.append(idx * i)
for j in range(idx + 1, m + 1):
a.append(idx * j)
a.sort()
print(a[-1 + k - (idx - 1) * (m + n) - (idx - 1) * (idx - 1)])
```
No
| 87,936 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Submitted Solution:
```
n, m, k = map(int, input().split())
left = 0
right = n * m
while right > left + 1:
mid = (right + left) >> 1
num = 0
for i in range(1, n + 1):
#num += min(m, mid/i)
num += m if m < mid/i else mid/i
if num >= k:
right = mid
else:
left = mid
print(right)
```
No
| 87,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Submitted Solution:
```
#import sys
#input=sys.stdin.buffer.readline
t=1
def f(x,n,m):
suma=0
for i in range(1,n+1):
suma+=min(x//i,m)
return suma
#print(l[:7])
for __ in range(t):
n,m,k=map(int,input().split())
l=1
r=n*m
while l<r:
x=(l+r)//2
print(l,r,x,f(x,n,m))
if f(x,n,m)>(k-1):
r=x
elif f(x,n,m)<(k-1):
l=x
else:
l=r=x
print(l)
```
No
| 87,938 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k (1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Examples
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
Note
A 2 × 3 multiplication table looks like this:
1 2 3
2 4 6
Submitted Solution:
```
n,m,k=map(int,input().split())
def solve(n,m,k):
low=0
high=n*m
count=0
mid=(low+high)//2
while(low<high):
for i in range(1,n+1):
count+=min((mid-1)/i,m)
if(count<k):
low=mid+1
else:
high=mid
return low-1
print(solve(n,m,k))
```
No
| 87,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To her surprise she didn't find golden mountains there. Lara looked around and noticed on the floor a painted table n × m panels in size with integers written on the panels. There also was a huge number of stones lying by the wall. On the pillar near the table Lara found a guidance note which said that to get hold of the treasures one has to choose some non-zero number of the first panels in each row of the table and put stones on all those panels to push them down. After that she will receive a number of golden coins equal to the sum of numbers written on the chosen panels. Lara quickly made up her mind on how to arrange the stones and was about to start when she noticed an addition to the note in small font below. According to the addition, for the room ceiling not to crush and smash the adventurer, the chosen panels should form a comb. It was explained that the chosen panels form a comb when the sequence c1, c2, ..., cn made from the quantities of panels chosen in each table line satisfies the following property: c1 > c2 < c3 > c4 < ..., i.e. the inequation mark interchanges between the neighboring elements. Now Lara is bewildered and doesn't know what to do. Help her to determine the largest number of coins she can get and survive at the same time.
Input
The first line contains a pair of integers n, m (2 ≤ n, m ≤ 1500). Next n lines contain m integers each — that is the table itself. The absolute value of the numbers in the table does not exceed 10000.
Output
Print the single number — the maximum number of coins Lara can get.
Examples
Input
2 2
-1 2
1 3
Output
2
Submitted Solution:
```
n, m = map(int, input().split())
def get_sum():
row = list(map(int, input().split()))
sum = m * [ None ]
sum[0] = row[0]
for c in range(1, m):
sum[c] = sum[c - 1] + row[c]
return sum
sum = get_sum()
for r in range(1, n):
best = m * [ None ]
if r % 2 == 1:
best[m - 1] = sum[m - 1]
for c in range(m - 2, -1, -1):
best[c] = max(sum[c], best[c + 1])
else:
best[0] = sum[0]
for c in range(1, m):
best[c] = max(sum[c], best[c - 1])
previous_sum = sum
sum = get_sum()
if r % 2 == 1:
for c in range(m - 1):
sum[c] += best[c + 1]
else:
for c in range(1, m):
sum[c] += best[c - 1]
if m % 2 == 0:
result = max(sum[:-1])
else:
result = max(sum[1:])
print(result)
```
No
| 87,940 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
f=input()
s=input()
n=int(input())
ans=[]
printed=[]
for i in range(n):
t,ty,m,ca=input().split()
li=[]
if ca=='r':
li.append(ty)
li.append(m)
if li not in printed:
if ty == 'h':
print(f, m, t)
else:
print(s, m, t)
printed.append(li)
else:
li.append(ty)
li.append(m)
if li not in ans:
ans.append(li)
else:
if li not in printed:
if ty == 'h':
print(f, m, t)
else:
print(s, m, t)
printed.append(li)
```
| 87,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
__author__ = 'Administrator'
def inp():
return input()
home = inp()
away = inp()
n = int(inp())
class Node:
time = 0
team = 0
num = 0
color = 0
def __lt__(self, other):
return self.time < other.time
def __init__(self, a, b, c, d):
self.time = a
self.team = b
self.num = c
self.color = d
i = 0
lt = []
while i < n:
temp = inp().split(' ')
o = Node(int(temp[0]), temp[1] == 'h', int(temp[2]), temp[3] == 'r')
lt.append(o)
i += 1
a = [0 for i in range(100)]
b = [0 for i in range(100)]
lt.sort()
i = 0
while i < n:
# print(lt[i].time, lt[i].team, lt[i].num, lt[i].color)
if lt[i].team:
if a[lt[i].num] < 2:
if lt[i].color:
a[lt[i].num] += 2
else:
a[lt[i].num] += 1
if a[lt[i].num] >= 2:
print(home, ' ', lt[i].num, ' ', lt[i].time)
else:
pass
else:
pass
else:
if b[lt[i].num] < 2:
if lt[i].color:
b[lt[i].num] += 2
else:
b[lt[i].num] += 1
if b[lt[i].num] >= 2:
print(away, ' ', lt[i].num, ' ', lt[i].time)
else:
pass
else:
pass
i += 1
# print("---end\n")
```
| 87,942 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
import collections
a_name = input()
b_name = input()
n = int(input())
players = [collections.Counter() for i in range(2)]
for i in range(n):
t, team, player, color = input().split()
team = 0 if team == 'h' else 1
player = int(player)
if color == 'y' and players[team][player] == 0:
players[team][player] = 1
elif color == 'r' and 0 <= players[team][player] <= 1 or players[team][player] == 1:
players[team][player] = 2
print(a_name if team == 0 else b_name, player, t)
```
| 87,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
s = input()
t = input()
n = int(input())
lis=[]
anh=[0]*(101)
ana=[0]*(101)
an=[]
for i in range(n):
n,m,a,b = map(str,input().split())
if m=='h':
lis.append([int(n),s,int(a),b,m])
else:
lis.append([int(n),t,int(a),b,m])
lis.sort()
#print(lis)
for i in lis:
n,m,a,b,c=i
# print(n,m,a,b)
if b=='y':
if c=='h':
if anh[a]!=2:
anh[a]+=1
if anh[a]==2:
an.append([m,a,n])
else:
if ana[a]!=2:
ana[a]+=1
if ana[a]==2:
an.append([m,a,n])
elif b=='r':
if c=='h':
if anh[a]!=2:
anh[a]=2
an.append([m,a,n])
else:
if ana[a]!=2:
ana[a]=2
an.append([m,a,n])
for i in an:
print(*i)
```
| 87,944 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
from collections import *
s1=input()
s2=input()
n=int(input())
d=defaultdict(int)
for i in range(n):
time,ty,num,col=map(str,input().split())
d[ty+num+col]+=1
if col=='y' and d[ty+num+col]==2 and d[ty+num+'r']==0:
if ty=='a':
print(s2,num,time)
else:
print(s1,num,time)
elif col=='r' and d[ty+num+col]==1 and (d[ty+num+'y']==0 or d[ty+num+'y']==1):
if(ty=='a'):
print(s2,num,time)
else:
print(s1,num,time)
```
| 87,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
team1 = input()
team2 = input()
fouls = int(input())
warn1 = [False] * 100
warn2 = [False] * 100
out1 = [False] * 100
out2 = [False] * 100
warns = []
for i in range(fouls):
a, b, c, d = map(str, input().split(' '))
a, c = int(a), int(c)
if b == 'a' and d == 'r' and out2[c] == False:
warns.append([team2, c, a])
out2[c] = True
elif b == 'h' and d == 'r' and out1[c] == False:
warns.append([team1, c, a])
out1[c] = True
elif b == 'a' and d == 'y' and warn2[c] and out2[c] == False:
warns.append([team2, c, a])
out2[c] = True
elif b == 'a' and d == 'y' and not warn2[c]:
warn2[c] = True
elif b == 'h' and d == 'y' and warn1[c] and out1[c] == False:
warns.append([team1, c, a])
out1[c] = True
elif b == 'h' and d == 'y' and not warn1[c]:
warn1[c] = True
warns.sort(key = lambda x:x[2])
for a in warns:
print(' '.join([str(x) for x in a]))
```
| 87,946 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
if __name__ == '__main__':
h_team_name = input()
a_team_name = input()
h=dict()
a1 = dict()
ans = []
for _ in range (int(input())):
a,b,c,d=input().split()
if d == 'r':
if b == 'h':
h.setdefault(c,0)
h[c]+=2
if h[c] == 2 or h[c]==3:
ans.append([h_team_name,c,a])
else:
a1.setdefault(c,0)
a1[c]+=2
if a1[c]==2 or a1[c]==3:
ans.append([a_team_name,c,a])
else:
if b == 'h':
h.setdefault(c,0)
h[c]+=1
if h[c] == 2:
ans.append([h_team_name,c,a])
else:
a1.setdefault(c,0)
a1[c]+=1
if a1[c]==2:
ans.append([a_team_name,c,a])
for i in ans:
print(*i)
```
| 87,947 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Tags: implementation
Correct Solution:
```
__author__ = 'PrimuS'
s1 = input().strip("\n")
s2 = input().strip("\n")
n = int(input())
d1 = {}
d2 = {}
for i in range(n):
ss = input().split()
t = int(ss[0])
x = int(ss[2])
ss[3].strip("\n")
if ss[1] == 'h':
if x in d1 and d1[x] == 1:
print(s1, x, t)
d1[x] = 2
elif x not in d1:
if ss[3] == 'r':
print(s1, x,t)
d1[x] = 2
else:
d1[x] = 1
else:
if x in d2 and d2[x] == 1:
print(s2, x, t)
d2[x] = 2
elif x not in d2:
if ss[3] == 'r':
print(s2, x, t)
d2[x] = 2
else:
d2[x] = 1
```
| 87,948 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
s1 = input()
s2 = input()
n = int(input())
h = []
a = []
for i in range(n):
k = input().split()
if k[1] == 'h':
if (h.count(k[2]) == 0) and k[3]=='y':
h.append(k[2])
elif h.count(k[2]) == 1:
h.append(k[2])
print(s1,k[2],k[0])
if (h.count(k[2]) == 0) and k[3]=='r':
h.append(k[2])
h.append(k[2])
print(s1,k[2],k[0])
else:
if (a.count(k[2]) == 0) and k[3]=='y':
a.append(k[2])
elif a.count(k[2]) == 1:
a.append(k[2])
print(s2,k[2],k[0])
if (a.count(k[2]) == 0) and k[3]=='r':
a.append(k[2])
a.append(k[2])
print(s2,k[2],k[0])
```
Yes
| 87,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
from collections import defaultdict
n1, n2 = input(), input()
n = int(input())
#mp = defaultdict(list)
mp = [[0 for i in range(100)], [0 for i in range(100)]]
for i in range(n):
line = input().split()
t = int(line[0])
m = int(line[2])
if line[1] == 'h':
mp[0][m] += 1
if line[3] == 'r':
if mp[0][m] < 3:
print(n1, m, t)
mp[0][m] = 3
continue
if mp[0][m] == 2:
print(n1, m, t)
mp[0][m] = 3
else:
mp[1][m] += 1
if line[3] == 'r':
if mp[1][m] < 3:
print(n2, m, t)
mp[1][m] = 3
continue
if mp[1][m] == 2:
print(n2, m, t)
mp[1][m] = 3
```
Yes
| 87,950 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
from collections import defaultdict
def main(t1,t2,n,t,ha,m,yr):
reds = {}
yellows = {} # defaultdict(int)
teams = [t1,t2]
for i in range(n):
player = (ha[i], m[i])
if player not in reds:
if player not in yellows:
yellows[player] = 0
if yr[i] == 'y':
yellows[player] += 1
if yellows[player] >= 2 or yr[i] == 'r':
reds[player] = 1
print(teams[ha[i]=='a'],m[i],t[i])
def main_input():
t1 = input()
t2 = input()
n = int(input())
t = list(range(n))
ha = list(range(n))
m = list(range(n))
yr = list(range(n))
for i in range(n):
t[i], ha[i], m[i], yr[i] = input().split()
t = [int(x) for x in t]
m = [int(x) for x in m]
#print(t1,t2,n)
#print(t,ha,m,yr)
main(t1,t2,n,t,ha,m,yr)
if __name__ == "__main__":
main_input()
```
Yes
| 87,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
from collections import *
home = input()
away = input()
n = int(input())
hd = defaultdict(int)
ad = defaultdict(int)
for i in range(n):
a = list(map(str, input().split()))
if a[1] == "h":
if hd[a[2]] != -1:
if a[-1] == "r":
hd[a[2]] = -1
print(home, a[2], a[0])
else:
hd[a[2]] += 1
if hd[a[2]] == 2:
hd[a[2]] = -1
print(home, a[2], a[0])
else:
if ad[a[2]] != -1:
if a[-1] == "r":
ad[a[2]] = -1
print(away, a[2], a[0])
else:
ad[a[2]] += 1
if ad[a[2]] == 2:
ad[a[2]] = -1
print(away, a[2], a[0])
```
Yes
| 87,952 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
homename= input()
awayname= input()
fouls= int(input())
a= []
for foul in range(fouls):
print(foul)
print("hello")
data= input().split()
print(data)
if data[3]== "r":
if data[1]== "h":
print(" ".join([homename, data[2], data[0]]))
elif data[1]== "a":
print(" ".join([awayname, data[2], data[0]]))
elif data[3]== "y":
#print("Hell")
for i in range(len(a)):
#print(a[i][1:], data[1:])
if a[i][1:]== data[1:]:
if data[1]== "h":
print(" ".join([homename, data[2], data[0]]))
elif data[1]== "a":
print(" ".join([awayname, data[2], data[0]]))
a.remove(a[i])
break
else:
a.append(data)
#print(a)
```
No
| 87,953 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
hom_tm = input()
awy_tm = input()
fouls = int(input())
home = {}
away = {}
for i in range(fouls):
time,land,player,card = input().split()
if land=='h':
if card=='y':
if player not in home:
home[player] = 1
else:
home[player] += 1
if home[player]==2:
home[player] = 0
print(hom_tm,player,time)
else:
if player in home:
home[player] = 0
print(hom_tm,player,time)
else:
if card=='y':
if player not in away:
away[player] = 1
else:
away[player] += 1
if away[player]==2:
away[player] = 0
print(awy_tm,player,time)
else:
if player in away:
away[player] = 0
print(awy_tm,player,time)
```
No
| 87,954 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
team1 = input()
team2 = input()
fouls = int(input())
warn1 = [False] * 100
warn2 = [False] * 100
warns = []
for i in range(fouls):
a, b, c, d = map(str, input().split(' '))
a, c = int(a), int(c)
if b == 'a' and d == 'r' and warn2[c] == False:
warns.append([team2, c, a])
warn2[c] = True
elif b == 'h' and d == 'r' and warn1[c] == False:
warns.append([team1, c, a])
warn1[c] = True
elif b == 'a' and d == 'y' and warn2[c]:
warns.append([team2, c, a])
elif b == 'a' and d == 'y' and not warn2[c]:
warn2[c] = True
elif b == 'h' and d == 'y' and warn1[c]:
warns.append([team1, c, a])
elif b == 'h' and d == 'y' and not warn1[c]:
warn1[c] = True
warns.sort(key = lambda x:x[2])
for a in warns:
print(' '.join([str(x) for x in a]))
```
No
| 87,955 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.
Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.
Input
The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct.
Next follows number n (1 ≤ n ≤ 90) — the number of fouls.
Each of the following n lines contains information about a foul in the following form:
* first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;
* then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;
* then goes the player's number m (1 ≤ m ≤ 99);
* then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.
The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.
Output
For each event when a player received his first red card in a chronological order print a string containing the following information:
* The name of the team to which the player belongs;
* the player's number in his team;
* the minute when he received the card.
If no player received a card, then you do not need to print anything.
It is possible case that the program will not print anything to the output (if there were no red cards).
Examples
Input
MC
CSKA
9
28 a 3 y
62 h 25 y
66 h 42 y
70 h 25 y
77 a 4 y
79 a 25 y
82 h 42 r
89 h 16 y
90 a 13 r
Output
MC 25 70
MC 42 82
CSKA 13 90
Submitted Solution:
```
import sys
hom = sys.stdin.readline()[:-1]
away = sys.stdin.readline()[:-1]
numcases = int(sys.stdin.readline())
ht=[0]*100
at=[0]*100
def solve(a):
p=int(a.split()[2])
t=int(a.split()[0])
ah = a.split()[1]
if ah=='h':
n = hom
if a.split()[3]=='y':
ht[p]+=1
else:
ht[p]+=2
if ht[p]==2:
x =[(t,n,p)]
return x
else:
n = away
if a.split()[3]=='y':
at[p]+=1
else:
at[p]+=2
if at[p]==2:
x =[(t,n,p)]
return x
r=[]
for casenum in range(1,numcases+1):
x =solve(sys.stdin.readline())
if x!= None:
r= r+ x
for i in list(sorted(r)):
print (i[1], i[2],i[0])
```
No
| 87,956 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
x=input().split()
l_1=input().split()
l_2=input().split()
del l_1[0]
del l_2[0]
list_1=[]
list_2=[]
lst_1=[int(value) for value in l_1]
lst_2=[int(value) for value in l_2]
n=int(x[0])
m=int(x[1])
def judge(a,b):
if a<b:
c=b
b=a
a=c
r=a%b
while r!=0:
a=b
b=r
r=a%b
return b
d=judge(n,m)
while lst_1!=list_1 or lst_2!=list_2:
list_1=list(set(lst_1))
list_2=list(set(lst_2))
for i in lst_1:
if i<m-1:
try:
for value in range(i,m,d):
lst_2.append(value)
except:
pass
try:
for value in range(i,-1,-d):
lst_2.append(value)
except:
pass
else:
try:
for p in range(m-1,m-d-2,-1):
if abs(i-p)%d==0:
break
for value in range(p,-1,-d):
lst_2.append(value)
except:
pass
lst_2=sorted(list(set(lst_2)))
for j in lst_2:
if j<n-1:
try:
for value in range(j,n,d):
lst_1.append(value)
except:
pass
try:
for value in range(j,-1,-d):
lst_1.append(value)
except:
pass
else:
try:
for q in range(n-1,n-d-2,-1):
if abs(j-q)%d==0:
break
for value in range(q,-1,-d):
lst_1.append(value)
except:
pass
lst_1=sorted(list(set(lst_1)))
lst_3=[value for value in range(0,n)]
lst_4=[value for value in range(0,m)]
if sorted(lst_1)==lst_3 and sorted(lst_2)==lst_4:
print('Yes')
else:
print('No')
```
| 87,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
def gcd(a, b):
while a:
b %= a
a, b = b, a
return b
n, m = (int(x) for x in input().split())
bb = [int(x) for x in input().split()][1:]
gg = [int(x) for x in input().split()][1:]
b = [0] * n
g = [0] * m
for x in bb:
b[x] = 1
for x in gg:
g[x] = 1
x = gcd(n, m)
for i in range(x):
if len([a for a in b[i::x] if a == 1]) + len([a for a in g[i::x] if a == 1]) == 0:
print('No')
exit()
print('Yes')
```
| 87,958 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
n,m = tuple(map(int,input().split()))
boys = set(list(map(int,input().split()))[1:])
girls = set(list(map(int,input().split()))[1:])
def euclide(n,m):
if m == 0:
return n
else:
return euclide(m,n%m)
def meet(a,b):
return (a-b)%euclide(n,m)==0
res1 = True
for a in range(n):
tmp = False
for b in girls.union(boys):
if meet(a,b):
tmp = True
if not tmp:
res1 = False
res2 = True
for b in range(m):
tmp = False
for a in boys.union(girls):
if meet(a,b):
tmp = True
if not tmp:
res2 = False
if res1 or res2:
print("Yes")
else:
print("No")
```
| 87,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
def gcd(a, b):
while (b != 0):
re = a % b
a = b
b = re
return a
def check(a):
for i in range(len(a)):
if not a[i]:
return False
return True
if __name__ == '__main__':
n, m = map(int, input().split())
boys = [False for i in range(n)]
girls = [False for i in range(m)]
temp = list(map(int, input().split()))
for i in range(temp[0]):
boys[temp[i + 1]] = True
temp = list(map(int, input().split()))
for i in range(temp[0]):
girls[temp[i + 1]] = True
for i in range(2 * int(n*m/gcd(n, m))):
boys[i % n] = (boys[i % n] or girls[i % m])
girls[i % m] = (boys[i % n] or girls[i % m])
if check(boys) and check(girls):
print("Yes")
else:
print("No")
```
| 87,960 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
n, m = map(int, input().split())
b1 = list(map(int, input().split()))
g1 = list(map(int, input().split()))
b1 = b1[1:]
g1 = g1[1:]
b = [0] * n
g = [0] * m
for i in b1:
b[i] = 1
for i in g1:
g[i] = 1
i = 0
flag = 0
while True:
i += 1
if i > 100000:
break
if b[i % n] == 1 or g[i % m] == 1:
b[i % n] = 1
g[i % m] = 1
if 0 not in b and 0 not in g:
flag = 1
break
if flag == 1:
print('Yes')
else:
print('No')
```
| 87,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
#
def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())
#------------------------------------------------------------------
n,m = mapi()
ah = [0]*n
bh = [0]*m
a = list(mapi())
b = list(mapi())
for i in a[1:]:
ah[i]=1
for i in b[1:]:
bh[i]=1
for i in range(100005):
if ah[i%n]==1 or bh[i%m]==1:
ah[i%n] = 1
bh[i%m] = 1
flag = 1
for i in ah:
if i==0:
flag = 0
break
for i in bh:
if i==0:
flag = 0
break
if flag:
print("Yes")
else:
print("No")
```
| 87,962 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
from sys import setrecursionlimit
def dfs(x, arr, vis):
global h
if vis[x]:
return
vis[x] = True
h += 1
for i in range(len(arr)):
if arr[x][i]:
dfs(i, arr, vis)
setrecursionlimit(10 ** 9)
n, m = map(int, input().split())
arr = [[0 for j in range(n + m)] for i in range(n + m)]
vis = [False] * (n + m)
for i in range(n * m):
arr[i % n][n + i % m] = 1
arr[n + i % m][i % n] = 1
#print(arr)
h = 0
hm = [int(i) for i in input().split()]
hm = hm[1:]
for i in hm:
dfs(i, arr, vis)
hm = [int(i) for i in input().split()]
hm = hm[1:]
for i in hm:
dfs(i, arr, vis)
if h == n + m:
print('Yes')
else:
print('No')
```
| 87,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Tags: math, number theory
Correct Solution:
```
"""
This template is made by Satwik_Tiwari.
python programmers can use this template :)) .
"""
#===============================================================================================
#importing some useful libraries.
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl #
from bisect import bisect_right as br
from bisect import bisect
#===============================================================================================
#some shortcuts
mod = pow(10, 9) + 7
def inp(): return sys.stdin.readline().strip() #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n") #as stdout.write always print sring.
def testcase(t):
for p in range(t):
solve()
def printlist(a) :
for p in range(0,len(a)):
out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
#===============================================================================================
# code here ;))
def solve():
n,m = sep()
b = [0]*n
g = [0]*m
f = 0
temp = lis()
if(temp[0] > 0):
f = 1
cntb = temp[0]
temp = temp[1:]
for i in range(0,cntb):
b[temp[i]] = 1
temp = lis()
if(temp[0]>0):
f = 1
cntg = temp[0]
temp = temp[1:]
for i in range(0,cntg):
g[temp[i]] = 1
if(f == 0):
print('No')
else:
ll = lcm(n,m)
for i in range(0,(n+m)*ll):
b[i%n] = b[i%n] or g[i%m]
g[i%m] = b[i%n] or g[i%m]
if(b.count(1) == n and g.count(1)==m):
print('Yes')
else:
print('No')
testcase(1)
```
| 87,964 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
n, m = map(int, input().split())
x = list(map(int, input().split()))[1:]
y = list(map(int, input().split()))[1:]
boys = [False for i in range(n)]
girls = [False for i in range(m)]
for el in x:
boys[el] = True
for el in y:
girls[el] = True
while True:
flag = False
for i in range(n*m):
cur = boys[i % n] or girls[i % m]
if cur and not (boys[i % n] and girls[i % m]):
flag = True
boys[i % n] = cur
girls[i % m] = cur
if not flag:
break
for boy in boys:
if not boy:
break
else:
for girl in girls:
if not girl:
break
else:
print("Yes")
exit()
print("No")
```
Yes
| 87,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
n,m=map(int,input().split())
z=list(map(int,input().split()))
a=[]
for x in range(n):
a.append(0)
for x in range(1,z[0]+1):
a[z[x]]=1
z=list(map(int,input().split()))
b=[]
for x in range(m):
b.append(0)
for x in range(1,z[0]+1):
b[z[x]]=1
for i in range((n+1)*(m+1)):
if a[i % n]==1 or b[i % m]==1:
a[i % n]=1
b[i % m]=1
f=True
for x in a:
if x==0:
f=False
break
for x in b:
if x==0:
f=False
break
if f:
print("YES")
else:
print("NO")
```
Yes
| 87,966 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
from math import gcd
n,m = map(int,input().split())
b = list(map(int,input().split()))
g = list(map(int,input().split()))
bn,gn = b[0],g[0]
hpyb,hpyg = set(b[1::]),set(g[1::])
d = (n+m)*n*m
for i in range(d+1):
boy = i%n
girl = i%m
if boy in hpyb or girl in hpyg:
hpyb.add(boy)
hpyg.add(girl)
f = True
for i in range(n):
if i not in hpyb:
f = False
break
for i in range(m):
if i not in hpyg:
f = False
break
if not f:
print("No")
else:
print("Yes")
```
Yes
| 87,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
n, m = map(int, input().split())
a, b = [0] * n, [0] * m
arr1 = list(map(int,input().split()))[1:]
arr2 = list(map(int,input().split()))[1:]
for i in arr1: a[i] = 1
for i in arr2: b[i] = 1
for i in range(2*m*n):
if a[i % n] == 1:
b[i % m] = 1
if b[i % m] == 1:
a[i % n] = 1
print('No' if (0 in a) or (0 in b) else 'Yes')
```
Yes
| 87,968 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
import math
string = input()
numbers = string.split()
a, b = int(numbers[0]), int(numbers[1])
happyboys = list(map(int, input().split()))
del happyboys[0]
happygirls = list(map(int, input().split()))
del happygirls[0]
boys = [0 for x in range(a)]
girls = [0 for x in range(b)]
for x in happyboys:
boys[x] = 1
for x in happygirls:
girls[x] = 1
n = a * b // math.gcd(a, b)
for x in range(n):
p, q = x % a, x % b
if boys[p] or girls[q]:
boys[p] = girls[q] = 1
if all(boys + girls):
print("Yes")
else:
print("No")
```
No
| 87,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
n, m = [int(s) for s in input().split(' ')]
b = set([int(s) for s in input().split(' ')][1:])
g = set([int(s) for s in input().split(' ')][1:])
ans = 'No'
for i in range(m * m * n + 1):
if len(b) == n and len(g) == m:
ans = 'Yes'
break
elif (i % n) in b:
g.add(i % m)
elif (i % m) in g:
b.add(i % n)
print(ans)
```
No
| 87,970 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
n,m=map(int,input().split())
z=list(map(int,input().split()))
a=[]
for x in range(n):
a.append(0)
for x in range(1,z[0]+1):
a[z[x]]=1
z=list(map(int,input().split()))
b=[]
for x in range(m):
b.append(0)
for x in range(1,z[0]+1):
b[z[x]]=1
for i in range(n*m):
if a[i % n]==1 or b[i % m]==1:
a[i % n]=1
b[i % m]=1
f=True
for x in a:
if x==0:
f=False
break
for x in b:
if x==0:
f=False
break
if f:
print("YES")
else:
print("NO")
```
No
| 87,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites <image>-th boy and <image>-th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.
Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
Input
The first line contains two integer n and m (1 ≤ n, m ≤ 109).
The second line contains integer b (0 ≤ b ≤ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys.
The third line conatins integer g (0 ≤ g ≤ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends.
Output
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
Examples
Input
2 3
0
1 0
Output
4
Input
2 4
1 0
1 2
Output
-1
Input
2 3
1 0
1 1
Output
2
Input
99999 100000
2 514 415
2 50216 61205
Output
4970100515
Note
By <image> we define the remainder of integer division of i by k.
In first sample case:
* On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day.
* On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day.
* On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day.
* On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy.
* On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
Submitted Solution:
```
# coding: utf-8
n, m = [int(i) for i in input().split()]
x = [0]*n
y = [0]*m
for i in input().split()[1:]:
x[int(i)] = 1
for i in input().split()[1:]:
y[int(i)] = 1
for i in range(n*m):
if x[i%n] == 1 or y[i%m] == 1:
x[i%n] = 1
y[i%m] = 1
if x.count(1)==n and y.count(1)==m:
print('Yes')
else:
print('No')
```
No
| 87,972 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
n , m = map(int,input().split())
if n == 1 :
print(1)
elif m > (n / 2) :
print(m - 1 )
elif m <= (n / 2 ):
print(m + 1)
```
| 87,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
n,m=map(int,input().split())
print(max(1,m-1+2*(m<n-m+1)))
# Made By Mostafa_Khaled
```
| 87,974 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
n, m = input().split(' ')
n = int(n)
m = int(m)
if n == 1 and m == 1:
print(1)
elif n - m <= m - 1:
print(m - 1)
else:
print(m + 1)
```
| 87,975 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
def read_list(t): return [t(x) for x in input().split()]
def read_line(t): return t(input())
def read_lines(t, N): return [t(input()) for _ in range(N)]
N, M = read_list(int)
small, large = M - 1, N - M
if N == 1:
print(1)
elif small >= large:
print(M-1)
else:
print(M+1)
```
| 87,976 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
#pyrival orz
import os
import sys
import math
from io import BytesIO, IOBase
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
############ ---- Dijkstra with path ---- ############
def dijkstra(start, distance, path, n):
# requires n == number of vertices in graph,
# adj == adjacency list with weight of graph
visited = [False for _ in range(n)] # To keep track of vertices that are visited
distance[start] = 0 # distance of start node from itself is 0
for i in range(n):
v = -1 # Initialize v == vertex from which its neighboring vertices' distance will be calculated
for j in range(n):
# If it has not been visited and has the lowest distance from start
if not visited[v] and (v == -1 or distance[j] < distance[v]):
v = j
if distance[v] == math.inf:
break
visited[v] = True # Mark as visited
for edge in adj[v]:
destination = edge[0] # Neighbor of the vertex
weight = edge[1] # Its corresponding weight
if distance[v] + weight < distance[destination]: # If its distance is less than the stored distance
distance[destination] = distance[v] + weight # Update the distance
path[destination] = v # Update the path
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a%b)
def lcm(a, b):
return (a*b)//gcd(a, b)
def ncr(n, r):
return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
def npr(n, r):
return math.factorial(n)//math.factorial(n-r)
def seive(n):
primes = [True]*(n+1)
ans = []
for i in range(2, n):
if not primes[i]:
continue
j = 2*i
while j <= n:
primes[j] = False
j += i
for p in range(2, n+1):
if primes[p]:
ans += [p]
return ans
def factors(n):
factors = []
x = 1
while x*x <= n:
if n % x == 0:
if n // x == x:
factors.append(x)
else:
factors.append(x)
factors.append(n//x)
x += 1
return factors
# Functions: list of factors, seive of primes, gcd of two numbers,
# lcm of two numbers, npr, ncr
def main():
try:
n, m = invr()
if n == 1:
print(n)
elif m - 1 >= n - m:
print(m-1)
else:
print(m+1)
except Exception as e:
print(e)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
```
| 87,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
a,b=map(int,input().split())
c=0
if(a==1 and b==1):
print(b)
c=1
if(a%2==0 and c==0):
if(b>a/2):
print(b-1)
elif(b<a/2):
print(b+1)
else:
print(b+1)
elif(c==0):
if(b>(a+1)/2):
print(b-1)
elif(b<(a+1)/2):
print(b+1)
else:
print(b-1)
```
| 87,978 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
n, m = map(int, input().split())
ans = 0
if n == 1 and m == 1:
print (1)
exit()
if m <= n//2:
ans = m+1
else:
ans = m-1
print (ans)
```
| 87,979 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Tags: constructive algorithms, games, greedy, implementation, math
Correct Solution:
```
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
lst = [eval(i) for i in input().split()]
if lst[0] - lst[1] > lst[1] - 1:
print(str(lst[1]+1)+"\n")
elif lst[0] == 1:
print("1\n")
else:
print(str(lst[1]-1)+"\n")
```
| 87,980 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
n,m=list(map(int,input().split()))
positive=n-m
negative=m-1
if(n==1 and m==1):
print(m)
elif(positive>negative):
print(m+1)
elif(negative>=positive):
print(m-1)
```
Yes
| 87,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
a,b=map(int,input().split())
x=a-b
y=b-1
if(a==1 and b==1):
print(a)
elif(x>y):
print(b+1)
else:
print(b-1)
```
Yes
| 87,982 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
n, k = [int(x) for x in input().split()]
if n==1:
print(1)
else:
if k<=n/2:
print(k+1)
else:
print(k-1)
```
Yes
| 87,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
count1 = 0
count2 = 0
nm = input().split()
n = int(nm[0])
m = int(nm[1])
if n == 1 and m == 1:
print(1)
elif m-1 >= (n-m):
print(m-1)
else:
print(m + 1)
```
Yes
| 87,984 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
n,m = map(int,input().split())
if n - m > m - 1:
print(m+1)
else:
print(m-1)
```
No
| 87,985 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
def find_best(number , misha):
right = number - misha
if right > misha + 1:
print(misha + 1)
else:
print(misha - 1)
if __name__ == "__main__":
number , misha = input().split(" ")
if int(number) == 1:
print(1)
else:
find_best(int(number) , int(misha))
```
No
| 87,986 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
a,b=list(map(int,input().split()))
if b>=a/2:
print(b-1)
elif b<a/2:
print(b+1)
```
No
| 87,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that <image> is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Examples
Input
3 1
Output
2
Input
4 3
Output
2
Note
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
Submitted Solution:
```
# Description of the problem can be found at http://codeforces.com/problemset/problem/570/B
n, m = map(int, input().split())
if n - m + 1 >= m - 1:
print(m + 1)
else:
print(m - 1)
```
No
| 87,988 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).
<image>
Ivan has beads of n colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.
Input
The first line of the input contains a single number n (1 ≤ n ≤ 26) — the number of colors of beads. The second line contains after n positive integers ai — the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000.
Output
In the first line print a single number — the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace.
Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.
Examples
Input
3
4 2 1
Output
1
abacaba
Input
1
4
Output
4
aaaa
Input
2
1 1
Output
0
ab
Note
In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.
In the second sample there is only one way to compose a necklace.
Tags: constructive algorithms, math
Correct Solution:
```
from fractions import gcd
from functools import reduce
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
def necklace_odd(a):
oi = next(i for i, ai in enumerate(a) if ai%2)
o = a[oi]
g = reduce(gcd, a)
s = [LETTERS[i] * (a[i]//(2*g)) for i in range(len(a)) if i != oi]
return g, (''.join(s) + (LETTERS[oi]*(o//g)) + ''.join(reversed(s))) * g
def necklace_even(a):
g = reduce(gcd, a)//2
s = [LETTERS[i]*(a[i]//(2*g)) for i in range(len(a))]
return 2*g, (''.join(s) + ''.join(reversed(s))) * g
def necklace(a):
if len(a) == 1:
return a[0], LETTERS[0]*a[0]
nodd = sum(ai%2 for ai in a)
if nodd > 1:
return 0, ''.join(LETTERS[i]*a[i] for i in range(len(a)))
return (necklace_odd if nodd else necklace_even)(a)
if __name__ == '__main__':
n = int(input())
a = list(map(int, input().split()))
assert len(a) == n
k, e = necklace(a)
print(k)
print(e)
# Made By Mostafa_Khaled
```
| 87,989 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).
<image>
Ivan has beads of n colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.
Input
The first line of the input contains a single number n (1 ≤ n ≤ 26) — the number of colors of beads. The second line contains after n positive integers ai — the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000.
Output
In the first line print a single number — the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace.
Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.
Examples
Input
3
4 2 1
Output
1
abacaba
Input
1
4
Output
4
aaaa
Input
2
1 1
Output
0
ab
Note
In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.
In the second sample there is only one way to compose a necklace.
Tags: constructive algorithms, math
Correct Solution:
```
import math
#import fractions
from functools import reduce
n = int(input())
odd = -1
beads = [int(x) for x in input().split()]
for i in range(n):
if beads[i]%2:
if odd >= 0:
print(0)
print(''.join(chr(ord('a') + i)*beads[i] for i in range(n)))
break
else:
odd = i
else:
gcd = reduce(lambda x,y: math.gcd(x,y), beads)
print(gcd)
if odd >= 0:
s = ''.join(chr(ord('a') + i)*(beads[i]//(2*gcd)) for i in range(n) if i != odd)
p = s + chr(ord('a') + odd)*(beads[odd]//gcd) + s[::-1]
print(p*gcd)
else:
s = ''.join(chr(ord('a') + i)*(beads[i]//gcd) for i in range(n))
p = s + s[::-1]
print(p*(gcd//2))
```
| 87,990 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Tags: data structures, dp, hashing, implementation, sortings, string suffix structures, strings
Correct Solution:
```
#!/usr/bin/env python3
# http://codeforces.com/contest/633/problem/C
from collections import deque
# import sys
# sys.setrecursionlimit(10001)
n = int(input())
s = input()
m = int(input())
words = []
for i in range(0, m):
words.append(input())
dict_words = {}
for word in words:
dict_words[word.lower()[::-1]] = word
trie_words = {}
for word in dict_words.keys():
d = trie_words
for letter in word:
d = d.setdefault(letter, {})
d['word'] = word
def find_sentence(s):
# stack = [0, '', dict_words, []] # position, used words so far, pos in trie we stopped on, children
roots = deque() # position, used words so far, pos in trie we stopped on, children
d = trie_words
for i in range(0, len(s)):
if not s[i] in d:
break;
d = d[s[i]]
if 'word' in d:
word = d['word']
new_root = [i + 1, word, None]
roots.append(new_root)
if i + 1 == len(s):
return new_root
while len(roots) > 0:
root = roots.popleft()
d = trie_words
for i in range(root[0], len(s)):
if not s[i] in d:
break;
d = d[s[i]]
if 'word' in d:
word = d['word']
new_root = [i + 1, word, root]
roots.appendleft(new_root)
if i + 1 == len(s):
return new_root
result = find_sentence(s)
words = []
while not result is None:
word = result[1]
words.append(dict_words[word])
result = result[2]
print(' '.join(reversed(words)))
```
| 87,991 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Tags: data structures, dp, hashing, implementation, sortings, string suffix structures, strings
Correct Solution:
```
def main():
stop, s, words, stack = int(input()), input()[::-1], {}, []
for _ in range(int(input())):
w = input()
words[w.lower()] = w
lel = tuple(sorted(set(map(len, words.keys())), reverse=True))
push, pop = stack.append, stack.pop
it = iter(lel)
while True:
try:
le = next(it)
start = stop - le
if start >= 0:
v = s[start:stop]
if v in words:
push((v, it, stop))
stop = start
if not stop:
print(' '.join(words[w] for w, _, _ in stack))
return
it = iter(lel)
except StopIteration:
_, it, stop = pop()
if __name__ == '__main__':
main()
# Made By Mostafa_Khaled
```
| 87,992 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Tags: data structures, dp, hashing, implementation, sortings, string suffix structures, strings
Correct Solution:
```
def main():
stop, s, words, stack = int(input()), input()[::-1], {}, []
for _ in range(int(input())):
w = input()
words[w.lower()] = w
lel = tuple(sorted(set(map(len, words.keys())), reverse=True))
push, pop = stack.append, stack.pop
it = iter(lel)
while True:
try:
le = next(it)
start = stop - le
if start >= 0:
v = s[start:stop]
if v in words:
push((v, it, stop))
stop = start
if not stop:
print(' '.join(words[w] for w, _, _ in stack))
return
it = iter(lel)
except StopIteration:
_, it, stop = pop()
if __name__ == '__main__':
main()
```
| 87,993 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Tags: data structures, dp, hashing, implementation, sortings, string suffix structures, strings
Correct Solution:
```
# [https://codeforces.com/contest/633/submission/33363209]
n = int(input())
t = input()[::-1]
m = int(input())
d = {q.lower(): q for q in [input() for i in range(m)]}
k = 0
s = []
l = sorted(set(map(len, d)))
while n:
k -= 1
if len(l) + k < 0: k, n = s.pop()
elif n >= l[k] and t[n - l[k]:n] in d:
s.append((k, n))
n -= l[k]
k = 0
print(*[d[t[n - l[i]:n]] for i, n in s])
```
| 87,994 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Tags: data structures, dp, hashing, implementation, sortings, string suffix structures, strings
Correct Solution:
```
import sys
p = sys.stdin.read().split()
n, t = int(p[0]), p[1][::-1]
d = {q.lower(): q for q in p[3:]}
k, s = 0, []
l = sorted(set(map(len, d)))
while n:
k -= 1
if len(l) + k < 0: k, n = s.pop()
elif n >= l[k] and t[n - l[k]:n] in d:
s.append((k, n))
n -= l[k]
k = 0
print(*[d[t[n - l[i]:n]] for i, n in s])
```
| 87,995 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Submitted Solution:
```
lol = int(input())
data = input()
n = int(input())
dic = dict()
for i in range(n):
t = input()
c_t = ''
for i in t:
c_t = i + c_t
dic[data.find(c_t.lower())] = t
now = 0
while now < len(data):
try:
print(dic[now], end=' ')
except:
pass
else:
now = now + len(dic[now])
now += 1
```
No
| 87,996 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Submitted Solution:
```
def make_trie(words):
root = dict()
for word in words:
current_dict = root
for letter in word:
current_dict = current_dict.setdefault(letter, {})
current_dict['$'] = '$'
return root
def find(trie, string):
My_List = []
My_Dic = trie
counter = 0
if counter > string.__len__()-1:
return []
if string[counter] in My_Dic:
My_Dic = My_Dic[string[counter]]
else:
return False
while '$' not in My_Dic or len(My_Dic) >= 1:
if '$' in My_Dic:
string1 = string[:counter+1]
string2 = string[counter+1:]
My_List.append(string1)
recresult = find(trie, string2)
if recresult is not False:
return My_List + recresult
counter += 1
if counter > string.__len__()-1:
return []
if string[counter] in My_Dic:
My_Dic = My_Dic[string[counter]]
else:
return False
lalist=[]
input()
str1=input()
x=input()
for i in range(int(x)):
lalist.append(input().lower()[::-1])
lalist2=find(make_trie(lalist),str1)
lalist3 =[]
for j in lalist2:
lalist3.append(j[::-1])
print(lalist3)
```
No
| 87,997 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Submitted Solution:
```
lol = input()
data = input()
right_data = ''
for l in data:
right_data = l + right_data
# print(right_data)
N = int(input())
for i in range(N):
t = input()
if t.lower() in right_data:
print(t, end='')
```
No
| 87,998 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After observing the results of Spy Syndrome, Yash realised the errors of his ways. He now believes that a super spy such as Siddhant can't use a cipher as basic and ancient as Caesar cipher. After many weeks of observation of Siddhant’s sentences, Yash determined a new cipher technique.
For a given sentence, the cipher is processed as:
1. Convert all letters of the sentence to lowercase.
2. Reverse each of the words of the sentence individually.
3. Remove all the spaces in the sentence.
For example, when this cipher is applied to the sentence
Kira is childish and he hates losing
the resulting string is
ariksihsidlihcdnaehsetahgnisol
Now Yash is given some ciphered string and a list of words. Help him to find out any original sentence composed using only words from the list. Note, that any of the given words could be used in the sentence multiple times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10 000) — the length of the ciphered text. The second line consists of n lowercase English letters — the ciphered text t.
The third line contains a single integer m (1 ≤ m ≤ 100 000) — the number of words which will be considered while deciphering the text. Each of the next m lines contains a non-empty word wi (|wi| ≤ 1 000) consisting of uppercase and lowercase English letters only. It's guaranteed that the total length of all words doesn't exceed 1 000 000.
Output
Print one line — the original sentence. It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any of those.
Examples
Input
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
Output
Kira is childish and he hates losing
Input
12
iherehtolleh
5
HI
Ho
there
HeLLo
hello
Output
HI there HeLLo
Note
In sample case 2 there may be multiple accepted outputs, "HI there HeLLo" and "HI there hello" you may output any of them.
Submitted Solution:
```
n = int(input())
t = input()
d = {}
for i in range(int(input())):
q = input()
d[q.lower()[::-1]] = q
s = [[]] + [None] * n
for i in range(n):
if s[i] is not None:
for q in d:
if t.startswith(q, i):
s[i + len(q)] = s[i] + [q]
print(*s[n])
```
No
| 87,999 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.