message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem has unusual memory constraint.
At evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared n papers with the income of some company for some time periods. Note that the income can be positive, zero or negative.
Igor and Zhenya placed the papers in a row and decided to take turns making moves. Igor will take the papers from the left side, Zhenya will take the papers from the right side. Igor goes first and takes 1 or 2 (on his choice) papers from the left. Then, on each turn a player can take k or k + 1 papers from his side if the opponent took exactly k papers in the previous turn. Players can't skip moves. The game ends when there are no papers left, or when some of the players can't make a move.
Your task is to determine the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
Input
The first line contains single positive integer n (1 ≤ n ≤ 4000) — the number of papers.
The second line contains n integers a1, a2, ..., an ( - 105 ≤ ai ≤ 105), where ai is the income on the i-th paper from the left.
Output
Print the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
Examples
Input
3
1 3 1
Output
4
Input
5
-1 -2 -1 -2 -1
Output
0
Input
4
-4 -2 4 5
Output
-13
Note
In the first example it's profitable for Igor to take two papers from the left to have the sum of the incomes equal to 4. Then Zhenya wouldn't be able to make a move since there would be only one paper, and he would be able to take only 2 or 3..
Submitted Solution:
```
from collections import deque
n = int(input())
a = list(map(int, input().split()))
t = 0
k = 1
i = 0
j = len(a)-1
igor = True
if a == [-4, -2, 4, 5]:
print(-13)
else:
while i < j and j - i + 1>= k:
if j != len(a) -1:
break
if igor:
t1 = sum(a[i:i+k])
t2 = sum(a[i:i+k+1])
if abs(t1) > abs(t2):
t += t1
i += 1
else:
k += 1
t += t2
i += 2
igor = False
else:
t1 = sum(a[j-k+1:j+1])
t2 = sum(a[j-k:j+1])
if t - t1 < t - t2:
t -= t1
j -= 1
else:
k += 1
t -= t2
j -= 2
igor = True
print(t)
``` | instruction | 0 | 10,428 | 19 | 20,856 |
No | output | 1 | 10,428 | 19 | 20,857 |
Provide a correct Python 3 solution for this coding contest problem.
We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.
You can do the following operation:
* Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
Since the balls are very easy to break, you cannot move Ball i more than C_i times in total. Within this limit, you can do the operation any number of times.
Your objective is to have Ball i in Box B_i for every i (1 \leq i \leq M). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* 1 \leq C_i \leq 10^5
* In the situation where the objective is achieved, every box contains one or more balls. That is, for every i (1 \leq i \leq N), there exists j such that B_j=i.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Output
If the objective is unachievable, print -1; if it is achievable, print the minimum number of operations required to achieve it.
Examples
Input
3 3
1 2 1
2 1 1
1 3 2
Output
3
Input
2 2
1 2 1
2 1 1
Output
-1
Input
5 5
1 2 1
2 1 1
1 3 2
4 5 1
5 4 1
Output
6
Input
1 1
1 1 1
Output
0 | instruction | 0 | 10,545 | 19 | 21,090 |
"Correct Solution:
```
import sys
from bisect import bisect_left
from collections import defaultdict
from itertools import accumulate
class UnionFind:
def __init__(self, n):
self.table = [-1] * n
def _root(self, x):
stack = []
tbl = self.table
while tbl[x] >= 0:
stack.append(x)
x = tbl[x]
for y in stack:
tbl[y] = x
return x
def find(self, x, y):
return self._root(x) == self._root(y)
def unite(self, x, y):
r1 = self._root(x)
r2 = self._root(y)
if r1 == r2:
return
d1 = self.table[r1]
d2 = self.table[r2]
if d1 <= d2:
self.table[r2] = r1
self.table[r1] += d2
else:
self.table[r1] = r2
self.table[r2] += d1
def get_size(self, x):
return -self.table[self._root(x)]
def solve():
n, m = map(int, sys.stdin.buffer.readline().split())
extra_durabilities = [0] * n
self_loop_durabilities = [[] for _ in range(n)]
outdegrees = [0] * n
base_operation_count = 0
uft = UnionFind(n)
mp = map(int, sys.stdin.buffer.read().split())
for a, b, c in zip(mp, mp, mp):
a -= 1
b -= 1
outdegrees[a] += 1
if a == b:
if c >= 2:
self_loop_durabilities[a].append(c)
continue
uft.unite(a, b)
extra_durabilities[a] += c - 1
base_operation_count += 1
# components[root] = [size, max_outdegree, durability(non-self-loop), self-loop-durability]
components = defaultdict(lambda: [0, 0, 0, []])
for i in range(n):
r = uft._root(i)
item = components[r]
item[0] += 1
item[1] = max(item[1], outdegrees[i])
item[2] += extra_durabilities[i]
item[3].extend(self_loop_durabilities[i])
exists_initial_catalyst_on_moving_path = False
exists_initial_catalyst_at_self_loop = False
supplied_catalyst = 0
demanded_catalyst = 0
self_loop_catalysts_cost1 = []
self_loop_catalysts_cost2 = []
for i, (cnt, deg, dur, sel) in components.items():
if cnt == 1:
if deg == 1:
self_loop_catalysts_cost2.extend(c - 2 for c in sel)
else:
if len(sel) >= 1:
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
exists_initial_catalyst_at_self_loop = True
continue
if deg == 1:
supplied_catalyst += dur
demanded_catalyst += 1
else:
supplied_catalyst += dur
if dur >= 1:
exists_initial_catalyst_on_moving_path = True
elif len(sel) >= 1:
exists_initial_catalyst_at_self_loop = True
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
# print(base_operation_count, supplied_catalyst, demanded_catalyst,
# exists_initial_catalyst_on_moving_path, exists_initial_catalyst_at_self_loop)
if demanded_catalyst == 0:
return base_operation_count
if not exists_initial_catalyst_on_moving_path and not exists_initial_catalyst_at_self_loop:
return -1
if supplied_catalyst >= demanded_catalyst:
if exists_initial_catalyst_on_moving_path:
return base_operation_count + demanded_catalyst
else:
return base_operation_count + demanded_catalyst + 1
self_loop_catalysts_cost1.sort(reverse=True)
self_loop_catalysts_cost2.sort(reverse=True)
# print(self_loop_catalysts_cost1)
# print(self_loop_catalysts_cost2)
acc1 = [0] + list(accumulate(self_loop_catalysts_cost1))
acc2 = [0] + list(accumulate(self_loop_catalysts_cost2))
# print(acc1)
# print(acc2)
shortage = demanded_catalyst - supplied_catalyst
if acc1[-1] + acc2[-1] < shortage:
return -1
cost = 10 ** 18
for use1 in range(0 if exists_initial_catalyst_on_moving_path else 1, len(acc1)):
cat = acc1[use1]
remaining = shortage - cat
if remaining <= 0:
cost = min(cost, use1)
break
if remaining > acc2[-1]:
continue
use2 = bisect_left(acc2, remaining)
cost = min(cost, use1 + 2 * use2)
return base_operation_count + demanded_catalyst + cost
print(solve())
``` | output | 1 | 10,545 | 19 | 21,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.
You can do the following operation:
* Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
Since the balls are very easy to break, you cannot move Ball i more than C_i times in total. Within this limit, you can do the operation any number of times.
Your objective is to have Ball i in Box B_i for every i (1 \leq i \leq M). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* 1 \leq C_i \leq 10^5
* In the situation where the objective is achieved, every box contains one or more balls. That is, for every i (1 \leq i \leq N), there exists j such that B_j=i.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Output
If the objective is unachievable, print -1; if it is achievable, print the minimum number of operations required to achieve it.
Examples
Input
3 3
1 2 1
2 1 1
1 3 2
Output
3
Input
2 2
1 2 1
2 1 1
Output
-1
Input
5 5
1 2 1
2 1 1
1 3 2
4 5 1
5 4 1
Output
6
Input
1 1
1 1 1
Output
0
Submitted Solution:
```
import sys
from bisect import bisect_left
from collections import defaultdict
from itertools import accumulate
class UnionFind:
def __init__(self, n):
self.table = [-1] * n
def _root(self, x):
stack = []
tbl = self.table
while tbl[x] >= 0:
stack.append(x)
x = tbl[x]
for y in stack:
tbl[y] = x
return x
def find(self, x, y):
return self._root(x) == self._root(y)
def unite(self, x, y):
r1 = self._root(x)
r2 = self._root(y)
if r1 == r2:
return
d1 = self.table[r1]
d2 = self.table[r2]
if d1 <= d2:
self.table[r2] = r1
self.table[r1] += d2
else:
self.table[r1] = r2
self.table[r2] += d1
def get_size(self, x):
return -self.table[self._root(x)]
def solve():
n, m = map(int, sys.stdin.buffer.readline().split())
extra_durabilities = [0] * n
self_loop_durabilities = [[] for _ in range(n)]
outdegrees = [0] * n
base_operation_count = 0
uft = UnionFind(n)
mp = map(int, sys.stdin.buffer.read().split())
for a, b, c in zip(mp, mp, mp):
a -= 1
b -= 1
outdegrees[a] += 1
if a == b:
if c >= 2:
self_loop_durabilities[a].append(c)
continue
uft.unite(a, b)
extra_durabilities[a] += c - 1
base_operation_count += 1
# components[root] = [size, max_outdegree, durability(non-self-loop), self-loop-durability]
components = defaultdict(lambda: [0, 0, 0, []])
for i in range(n):
r = uft._root(i)
item = components[r]
item[0] += 1
item[1] = max(item[1], outdegrees[i])
item[2] += extra_durabilities[i]
item[3].extend(self_loop_durabilities[i])
exists_initial_catalyst = False
supplied_catalyst = 0
demanded_catalyst = 0
self_loop_catalysts_cost1 = []
self_loop_catalysts_cost2 = []
for i, (cnt, deg, dur, sel) in components.items():
if cnt == 1:
if deg == 1:
self_loop_catalysts_cost2.extend(c - 2 for c in sel)
else:
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
exists_initial_catalyst = True
continue
if deg == 1:
supplied_catalyst += dur
demanded_catalyst += 1
else:
supplied_catalyst += dur
if dur >= 1 or sel:
exists_initial_catalyst = True
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
# print(base_operation_count, supplied_catalyst, demanded_catalyst, exists_initial_catalyst)
if demanded_catalyst == 0:
return base_operation_count
if not exists_initial_catalyst:
return -1
if supplied_catalyst >= demanded_catalyst:
return base_operation_count + demanded_catalyst
self_loop_catalysts_cost1.sort(reverse=True)
self_loop_catalysts_cost2.sort(reverse=True)
# print(self_loop_catalysts_cost1)
# print(self_loop_catalysts_cost2)
acc1 = list(accumulate(self_loop_catalysts_cost1))
acc2 = [0] + list(accumulate(self_loop_catalysts_cost2))
# print(acc1)
# print(acc2)
shortage = demanded_catalyst - supplied_catalyst
if acc1[-1] + acc2[-1] < shortage:
return -1
cost = 10 ** 18
for use1, cat in enumerate(acc1, start=1):
remaining = shortage - cat
if remaining <= 0:
cost = min(cost, use1)
break
if remaining > acc2[-1]:
continue
use2 = bisect_left(acc2, remaining)
cost = min(cost, use1 + 2 * use2)
return base_operation_count + demanded_catalyst + cost
print(solve())
``` | instruction | 0 | 10,546 | 19 | 21,092 |
No | output | 1 | 10,546 | 19 | 21,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.
You can do the following operation:
* Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
Since the balls are very easy to break, you cannot move Ball i more than C_i times in total. Within this limit, you can do the operation any number of times.
Your objective is to have Ball i in Box B_i for every i (1 \leq i \leq M). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* 1 \leq C_i \leq 10^5
* In the situation where the objective is achieved, every box contains one or more balls. That is, for every i (1 \leq i \leq N), there exists j such that B_j=i.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Output
If the objective is unachievable, print -1; if it is achievable, print the minimum number of operations required to achieve it.
Examples
Input
3 3
1 2 1
2 1 1
1 3 2
Output
3
Input
2 2
1 2 1
2 1 1
Output
-1
Input
5 5
1 2 1
2 1 1
1 3 2
4 5 1
5 4 1
Output
6
Input
1 1
1 1 1
Output
0
Submitted Solution:
```
import sys
from bisect import bisect_left
from collections import defaultdict
from itertools import accumulate
class UnionFind:
def __init__(self, n):
self.table = [-1] * n
def _root(self, x):
stack = []
tbl = self.table
while tbl[x] >= 0:
stack.append(x)
x = tbl[x]
for y in stack:
tbl[y] = x
return x
def find(self, x, y):
return self._root(x) == self._root(y)
def unite(self, x, y):
r1 = self._root(x)
r2 = self._root(y)
if r1 == r2:
return
d1 = self.table[r1]
d2 = self.table[r2]
if d1 <= d2:
self.table[r2] = r1
self.table[r1] += d2
else:
self.table[r1] = r2
self.table[r2] += d1
def get_size(self, x):
return -self.table[self._root(x)]
def solve():
n, m = map(int, sys.stdin.buffer.readline().split())
extra_durabilities = [0] * n
self_loop_durabilities = [[] for _ in range(n)]
outdegrees = [0] * n
base_operation_count = 0
uft = UnionFind(n)
mp = map(int, sys.stdin.buffer.read().split())
for a, b, c in zip(mp, mp, mp):
a -= 1
b -= 1
outdegrees[a] += 1
if a == b:
if c >= 3:
self_loop_durabilities[a].append(c)
continue
uft.unite(a, b)
extra_durabilities[a] += c - 1
base_operation_count += 1
# components[root] = [size, max_outdegree, durability(non-self-loop), self-loop-durability]
components = defaultdict(lambda: [0, 0, 0, []])
for i in range(n):
r = uft._root(i)
item = components[r]
item[0] += 1
item[1] = max(item[1], outdegrees[i])
item[2] += extra_durabilities[i]
item[3].extend(self_loop_durabilities[i])
exists_initial_catalyst = False
supplied_catalyst = 0
demanded_catalyst = 0
self_loop_catalysts_cost1 = []
self_loop_catalysts_cost2 = []
for i, (cnt, deg, dur, sel) in components.items():
if cnt == 1:
if deg == 1:
self_loop_catalysts_cost2.extend(c - 2 for c in sel)
else:
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
continue
if deg == 1:
supplied_catalyst += dur
demanded_catalyst += 1
else:
supplied_catalyst += dur
exists_initial_catalyst = True
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
if demanded_catalyst == 0:
return base_operation_count
if not exists_initial_catalyst:
return -1
if supplied_catalyst >= demanded_catalyst:
return base_operation_count + demanded_catalyst
self_loop_catalysts_cost1.sort(reverse=True)
self_loop_catalysts_cost2.sort(reverse=True)
acc1 = [0] + list(accumulate(self_loop_catalysts_cost1))
acc2 = [0] + list(accumulate(self_loop_catalysts_cost2))
shortage = demanded_catalyst - supplied_catalyst
if acc1[-1] + acc2[-1] < shortage:
return -1
cost = 10 ** 18
for use1, cat in enumerate(acc1):
remaining = shortage - cat
if remaining <= 0:
cost = min(cost, use1)
break
use2 = bisect_left(acc2, remaining)
cost = min(cost, use1 + 2 * use2)
return base_operation_count + demanded_catalyst + cost
print(solve())
``` | instruction | 0 | 10,547 | 19 | 21,094 |
No | output | 1 | 10,547 | 19 | 21,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.
You can do the following operation:
* Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
Since the balls are very easy to break, you cannot move Ball i more than C_i times in total. Within this limit, you can do the operation any number of times.
Your objective is to have Ball i in Box B_i for every i (1 \leq i \leq M). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* 1 \leq C_i \leq 10^5
* In the situation where the objective is achieved, every box contains one or more balls. That is, for every i (1 \leq i \leq N), there exists j such that B_j=i.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Output
If the objective is unachievable, print -1; if it is achievable, print the minimum number of operations required to achieve it.
Examples
Input
3 3
1 2 1
2 1 1
1 3 2
Output
3
Input
2 2
1 2 1
2 1 1
Output
-1
Input
5 5
1 2 1
2 1 1
1 3 2
4 5 1
5 4 1
Output
6
Input
1 1
1 1 1
Output
0
Submitted Solution:
```
import sys
from bisect import bisect_left
from collections import defaultdict
from itertools import accumulate
class UnionFind:
def __init__(self, n):
self.table = [-1] * n
def _root(self, x):
stack = []
tbl = self.table
while tbl[x] >= 0:
stack.append(x)
x = tbl[x]
for y in stack:
tbl[y] = x
return x
def find(self, x, y):
return self._root(x) == self._root(y)
def unite(self, x, y):
r1 = self._root(x)
r2 = self._root(y)
if r1 == r2:
return
d1 = self.table[r1]
d2 = self.table[r2]
if d1 <= d2:
self.table[r2] = r1
self.table[r1] += d2
else:
self.table[r1] = r2
self.table[r2] += d1
def get_size(self, x):
return -self.table[self._root(x)]
def solve():
n, m = map(int, sys.stdin.buffer.readline().split())
extra_durabilities = [0] * n
self_loop_durabilities = [[] for _ in range(n)]
outdegrees = [0] * n
base_operation_count = 0
uft = UnionFind(n)
mp = map(int, sys.stdin.buffer.read().split())
for a, b, c in zip(mp, mp, mp):
a -= 1
b -= 1
outdegrees[a] += 1
if a == b:
if c >= 2:
self_loop_durabilities[a].append(c)
continue
uft.unite(a, b)
extra_durabilities[a] += c - 1
base_operation_count += 1
# components[root] = [size, max_outdegree, durability(non-self-loop), self-loop-durability]
components = defaultdict(lambda: [0, 0, 0, []])
for i in range(n):
r = uft._root(i)
item = components[r]
item[0] += 1
item[1] = max(item[1], outdegrees[i])
item[2] += extra_durabilities[i]
item[3].extend(self_loop_durabilities[i])
exists_initial_catalyst_on_moving_path = False
exists_initial_catalyst_at_self_loop = False
supplied_catalyst = 0
demanded_catalyst = 0
self_loop_catalysts_cost1 = []
self_loop_catalysts_cost2 = []
for i, (cnt, deg, dur, sel) in components.items():
if cnt == 1:
if deg == 1:
self_loop_catalysts_cost2.extend(c - 2 for c in sel)
else:
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
exists_initial_catalyst_at_self_loop = True
continue
if deg == 1:
supplied_catalyst += dur
demanded_catalyst += 1
else:
supplied_catalyst += dur
if dur >= 1:
exists_initial_catalyst_on_moving_path = True
elif len(sel) >= 1:
exists_initial_catalyst_at_self_loop = True
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
# print(base_operation_count, supplied_catalyst, demanded_catalyst,
# exists_initial_catalyst_on_moving_path, exists_initial_catalyst_at_self_loop)
if demanded_catalyst == 0:
return base_operation_count
if not exists_initial_catalyst_on_moving_path and not exists_initial_catalyst_at_self_loop:
return -1
if supplied_catalyst >= demanded_catalyst:
if exists_initial_catalyst_on_moving_path:
return base_operation_count + demanded_catalyst
else:
return base_operation_count + demanded_catalyst + 1
self_loop_catalysts_cost1.sort(reverse=True)
self_loop_catalysts_cost2.sort(reverse=True)
# print(self_loop_catalysts_cost1)
# print(self_loop_catalysts_cost2)
acc1 = [0] + list(accumulate(self_loop_catalysts_cost1))
acc2 = [0] + list(accumulate(self_loop_catalysts_cost2))
# print(acc1)
# print(acc2)
shortage = demanded_catalyst - supplied_catalyst
if acc1[-1] + acc2[-1] < shortage:
return -1
cost = 10 ** 18
for use1 in range(0 if exists_initial_catalyst_on_moving_path else 1, len(acc1)):
cat = acc1[use1]
remaining = shortage - cat
if remaining <= 0:
cost = min(cost, use1)
break
if remaining > acc2[-1]:
continue
use2 = bisect_left(acc2, remaining)
cost = min(cost, use1 + 2 * use2)
return base_operation_count + demanded_catalyst + cost
print(solve())
``` | instruction | 0 | 10,548 | 19 | 21,096 |
No | output | 1 | 10,548 | 19 | 21,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.
You can do the following operation:
* Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
Since the balls are very easy to break, you cannot move Ball i more than C_i times in total. Within this limit, you can do the operation any number of times.
Your objective is to have Ball i in Box B_i for every i (1 \leq i \leq M). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.
Constraints
* 1 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N
* 1 \leq C_i \leq 10^5
* In the situation where the objective is achieved, every box contains one or more balls. That is, for every i (1 \leq i \leq N), there exists j such that B_j=i.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
Output
If the objective is unachievable, print -1; if it is achievable, print the minimum number of operations required to achieve it.
Examples
Input
3 3
1 2 1
2 1 1
1 3 2
Output
3
Input
2 2
1 2 1
2 1 1
Output
-1
Input
5 5
1 2 1
2 1 1
1 3 2
4 5 1
5 4 1
Output
6
Input
1 1
1 1 1
Output
0
Submitted Solution:
```
import sys
# 箱を頂点とし、A→Bに有向辺を張る
# ①出次数(初期でボールが入っている個数)が全て1以下の、サイズ2以上の連結成分
# ・分岐のないサイクル(入次数1以上が保証されているため)
# ・外部から触媒が来ないと動かせない。来たら全て連鎖的に流れる
# ・流れる途中で Ci>=2 のボールがあればそいつも他の触媒になれる
# ②出次数に2以上のものがある連結成分
# ・強連結成分分解+トポロジカルソートを考える(実際にやる必要は無い)
# ・トポロジカル順で最上位になる組は、必ず下位の組に辺が伸びる頂点が出次数2以上
# ・連鎖的に全てのボールを希望の箱に1回で移せる
# ・Ci>=2 のボールがあれば他の触媒になれる
# ③自己ループボール
# ・動かす必要は無いが、Ci>=2なら触媒数が足りなければ触媒になれる
# ・②に含まれるものは、全体の移動回数を1増やすことでCi-1回触媒を増やせる
# ・完全に浮いているものはそれ自身を起動するのにも触媒が必要なので、
# 全体の移動回数を2と触媒数1を犠牲にCi-1回触媒を増やせる(差し引きCi-2回)
#
# ①が存在し、かつ②が1つもなければ不可能
# ①が存在しなければ、A!=Bであるボール数
# ①が存在し、初期で②が1つでもあれば、
# A!=Bであるボール数 + ①の個数 が下限
# 触媒が足りない場合、全体の移動回数を1 or 2増やすことでCi-2回触媒を増やせる
# →それでも足りなければ不可能
# →足りるならコストと価値でナップサック問題
# A!=Bであるボール数 + ①の個数 + 自己ループを触媒に利用するためのコスト
from bisect import bisect_left
from collections import defaultdict
from itertools import accumulate
class UnionFind:
def __init__(self, n):
self.table = [-1] * n
def _root(self, x):
stack = []
tbl = self.table
while tbl[x] >= 0:
stack.append(x)
x = tbl[x]
for y in stack:
tbl[y] = x
return x
def find(self, x, y):
return self._root(x) == self._root(y)
def unite(self, x, y):
r1 = self._root(x)
r2 = self._root(y)
if r1 == r2:
return
d1 = self.table[r1]
d2 = self.table[r2]
if d1 <= d2:
self.table[r2] = r1
self.table[r1] += d2
else:
self.table[r1] = r2
self.table[r2] += d1
def get_size(self, x):
return -self.table[self._root(x)]
def solve():
n, m = map(int, sys.stdin.buffer.readline().split())
extra_durabilities = [0] * n
self_loop_durabilities = [[] for _ in range(n)]
outdegrees = [0] * n
base_operation_count = 0
uft = UnionFind(n)
mp = map(int, sys.stdin.buffer.read().split())
for a, b, c in zip(mp, mp, mp):
a -= 1
b -= 1
outdegrees[a] += 1
if a == b:
if c >= 3:
self_loop_durabilities[a].append(c)
continue
uft.unite(a, b)
extra_durabilities[a] += c - 1
base_operation_count += 1
# components[root] = [size, max_outdegree, durability(non-self-loop), self-loop-durability]
components = defaultdict(lambda: [0, 0, 0, []])
for i in range(n):
r = uft._root(i)
item = components[r]
item[0] += 1
item[1] = max(item[1], outdegrees[i])
item[2] += extra_durabilities[i]
item[3].extend(self_loop_durabilities[i])
exists_initial_catalyst = False
supplied_catalyst = 0
demanded_catalyst = 0
self_loop_catalysts_cost1 = []
self_loop_catalysts_cost2 = []
for i, (cnt, deg, dur, sel) in components.items():
if cnt == 1:
if deg == 1:
self_loop_catalysts_cost2.extend(c - 2 for c in sel)
else:
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
continue
if deg == 1:
supplied_catalyst += dur
demanded_catalyst += 1
else:
supplied_catalyst += dur
exists_initial_catalyst = True
self_loop_catalysts_cost1.extend(c - 1 for c in sel)
if demanded_catalyst == 0:
return base_operation_count
if not exists_initial_catalyst:
return -1
if supplied_catalyst >= demanded_catalyst:
return base_operation_count + demanded_catalyst
self_loop_catalysts_cost1.sort(reverse=True)
self_loop_catalysts_cost2.sort(reverse=True)
acc1 = [0] + list(accumulate(self_loop_catalysts_cost1))
acc2 = [0] + list(accumulate(self_loop_catalysts_cost2))
shortage = demanded_catalyst - supplied_catalyst
if acc1[-1] + acc2[-1] < shortage:
return -1
cost = 10 ** 18
for use1, cat in enumerate(acc1):
remaining = shortage - cat
if remaining <= 0:
cost = min(cost, use1)
break
if remaining > acc2[-1]:
continue
use2 = bisect_left(acc2, remaining)
cost = min(cost, use1 + 2 * use2)
return base_operation_count + demanded_catalyst + cost
print(solve())
``` | instruction | 0 | 10,549 | 19 | 21,098 |
No | output | 1 | 10,549 | 19 | 21,099 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,974 | 19 | 21,948 |
Tags: constructive algorithms, games, math
Correct Solution:
```
for t in range(int(input())):
x,y=map(int, input().split())
print(x-1,y,end=' ')
``` | output | 1 | 10,974 | 19 | 21,949 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,975 | 19 | 21,950 |
Tags: constructive algorithms, games, math
Correct Solution:
```
from sys import stdin
input=stdin.readline
for _ in range(int(input())):
a,b=map(int,input().split())
print(a-1,b)
``` | output | 1 | 10,975 | 19 | 21,951 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,976 | 19 | 21,952 |
Tags: constructive algorithms, games, math
Correct Solution:
```
mod = 10**9 + 7
def solve():
x, y = map(int, input().split())
print(x - 1, y)
t = 1
t = int(input())
while t > 0:
solve()
t -= 1
``` | output | 1 | 10,976 | 19 | 21,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,977 | 19 | 21,954 |
Tags: constructive algorithms, games, math
Correct Solution:
```
for i in range(int(input())):
x,y=map(int,input().split())
print(x-1,y,end=" ")
print()
``` | output | 1 | 10,977 | 19 | 21,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,978 | 19 | 21,956 |
Tags: constructive algorithms, games, math
Correct Solution:
```
t = int(input())
for i in range(t):
n, m = map(int, input().split())
print(n - 1, m)
``` | output | 1 | 10,978 | 19 | 21,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,979 | 19 | 21,958 |
Tags: constructive algorithms, games, math
Correct Solution:
```
for _ in range(int(input())):
x,y = map(int,input().split(' '))
if x==1: print(0,y)
else: print(x-1,y)
``` | output | 1 | 10,979 | 19 | 21,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,980 | 19 | 21,960 |
Tags: constructive algorithms, games, math
Correct Solution:
```
import time,math as mt,bisect as bs,sys
from sys import stdin,stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi=3.14159265358979323846264338327950
def II(): # to take integer input
return int(stdin.readline())
def IP(): # to take tuple as input
return map(int,stdin.readline().split())
def L(): # to take list as input
return list(map(int,stdin.readline().split()))
def P(x): # to print integer,list,string etc..
return stdout.write(str(x)+"\n")
def PI(x,y): # to print tuple separatedly
return stdout.write(str(x)+" "+str(y)+"\n")
def lcm(a,b): # to calculate lcm
return (a*b)//gcd(a,b)
def gcd(a,b): # to calculate gcd
if a==0:
return b
elif b==0:
return a
if a>b:
return gcd(a%b,b)
else:
return gcd(a,b%a)
def bfs(adj,v): # a schema of bfs
visited=[False]*(v+1)
q=deque()
while q:
pass
def setBit(n):
count=0
while n!=0:
n=n&(n-1)
count+=1
return count
mx=10**7
spf=[mx]*(mx+1)
def readTree(n,e): # to read tree
adj=[set() for i in range(n+1)]
for i in range(e):
u1,u2=IP()
adj[u1].add(u2)
return adj
def sieve():
li=[True]*(10**3+5)
li[0],li[1]=False,False
for i in range(2,len(li),1):
if li[i]==True:
for j in range(i*i,len(li),i):
li[j]=False
prime,cur=[0]*200,0
for i in range(10**3+5):
if li[i]==True:
prime[cur]=i
cur+=1
return prime
def SPF():
mx=(10**6+1)
spf[1]=1
for i in range(2,mx):
if spf[i]==1e9:
spf[i]=i
for j in range(i*i,mx,i):
if i<spf[j]:
spf[j]=i
return
def prime(n,d):
prm=set()
while n!=1:
prm.add(spf[n])
n=n//spf[n]
for ele in prm:
d[ele]=d.get(ele,0)+1
return
#####################################################################################
mod=998244353
def solve():
x,y=IP()
print(x-1,y)
return
t=II()
for i in range(t):
solve()
#######
#
#
####### # # # #### # # #
# # # # # # # # # # #
# #### # # #### #### # #
###### # # #### # # # # #
# ``````¶0````1¶1_```````````````````````````````````````
# ```````¶¶¶0_`_¶¶¶0011100¶¶¶¶¶¶¶001_````````````````````
# ````````¶¶¶¶¶00¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶0_````````````````
# `````1_``¶¶00¶0000000000000000000000¶¶¶¶0_`````````````
# `````_¶¶_`0¶000000000000000000000000000¶¶¶¶¶1``````````
# ```````¶¶¶00¶00000000000000000000000000000¶¶¶_`````````
# ````````_¶¶00000000000000000000¶¶00000000000¶¶`````````
# `````_0011¶¶¶¶¶000000000000¶¶00¶¶0¶¶00000000¶¶_````````
# ```````_¶¶¶¶¶¶¶00000000000¶¶¶¶0¶¶¶¶¶00000000¶¶1````````
# ``````````1¶¶¶¶¶000000¶¶0¶¶¶¶¶¶¶¶¶¶¶¶0000000¶¶¶````````
# ```````````¶¶¶0¶000¶00¶0¶¶`_____`__1¶0¶¶00¶00¶¶````````
# ```````````¶¶¶¶¶00¶00¶10¶0``_1111_`_¶¶0000¶0¶¶¶````````
# ``````````1¶¶¶¶¶00¶0¶¶_¶¶1`_¶_1_0_`1¶¶_0¶0¶¶0¶¶````````
# ````````1¶¶¶¶¶¶¶0¶¶0¶0_0¶``100111``_¶1_0¶0¶¶_1¶````````
# ```````1¶¶¶¶00¶¶¶¶¶¶¶010¶``1111111_0¶11¶¶¶¶¶_10````````
# ```````0¶¶¶¶__10¶¶¶¶¶100¶¶¶0111110¶¶¶1__¶¶¶¶`__````````
# ```````¶¶¶¶0`__0¶¶0¶¶_¶¶¶_11````_0¶¶0`_1¶¶¶¶```````````
# ```````¶¶¶00`__0¶¶_00`_0_``````````1_``¶0¶¶_```````````
# ``````1¶1``¶¶``1¶¶_11``````````````````¶`¶¶````````````
# ``````1_``¶0_¶1`0¶_`_``````````_``````1_`¶1````````````
# ``````````_`1¶00¶¶_````_````__`1`````__`_¶`````````````
# ````````````¶1`0¶¶_`````````_11_`````_``_``````````````
# `````````¶¶¶¶000¶¶_1```````_____```_1``````````````````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶0_``````_````_1111__``````````````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶01_`````_11____1111_```````````
# `````````¶¶0¶0¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶1101_______11¶_```````````
# ``````_¶¶¶0000000¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶0¶0¶¶¶1````````````
# `````0¶¶0000000¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶1`````````````
# ````0¶0000000¶¶0_````_011_10¶110¶01_1¶¶¶0````_100¶001_`
# ```1¶0000000¶0_``__`````````_`````````0¶_``_00¶¶010¶001
# ```¶¶00000¶¶1``_01``_11____``1_``_`````¶¶0100¶1```_00¶1
# ``1¶¶00000¶_``_¶_`_101_``_`__````__````_0000001100¶¶¶0`
# ``¶¶¶0000¶1_`_¶``__0_``````_1````_1_````1¶¶¶0¶¶¶¶¶¶0```
# `_¶¶¶¶00¶0___01_10¶_``__````1`````11___`1¶¶¶01_````````
# `1¶¶¶¶¶0¶0`__01¶¶¶0````1_```11``___1_1__11¶000`````````
# `1¶¶¶¶¶¶¶1_1_01__`01```_1```_1__1_11___1_``00¶1````````
# ``¶¶¶¶¶¶¶0`__10__000````1____1____1___1_```10¶0_```````
# ``0¶¶¶¶¶¶¶1___0000000```11___1__`_0111_```000¶01```````
# ```¶¶¶00000¶¶¶¶¶¶¶¶¶01___1___00_1¶¶¶`_``1¶¶10¶¶0```````
# ```1010000¶000¶¶0100_11__1011000¶¶0¶1_10¶¶¶_0¶¶00``````
# 10¶000000000¶0________0¶000000¶¶0000¶¶¶¶000_0¶0¶00`````
# ¶¶¶¶¶¶0000¶¶¶¶_`___`_0¶¶¶¶¶¶¶00000000000000_0¶00¶01````
# ¶¶¶¶¶0¶¶¶¶¶¶¶¶¶_``_1¶¶¶00000000000000000000_0¶000¶01```
# 1__```1¶¶¶¶¶¶¶¶¶00¶¶¶¶00000000000000000000¶_0¶0000¶0_``
# ```````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶00000000000000000000010¶00000¶¶_`
# ```````0¶¶¶¶¶¶¶¶¶¶¶¶¶¶00000000000000000000¶10¶¶0¶¶¶¶¶0`
# ````````¶¶¶¶¶¶¶¶¶¶0¶¶¶00000000000000000000010¶¶¶0011```
# ````````1¶¶¶¶¶¶¶¶¶¶0¶¶¶0000000000000000000¶100__1_`````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶000000000000000000¶11``_1``````
# `````````1¶¶¶¶¶¶¶¶¶¶¶0¶¶¶00000000000000000¶11___1_`````
# ``````````¶¶¶¶¶¶0¶0¶¶¶¶¶¶¶0000000000000000¶11__``1_````
# ``````````¶¶¶¶¶¶¶0¶¶¶0¶¶¶¶¶000000000000000¶1__````__```
# ``````````¶¶¶¶¶¶¶¶0¶¶¶¶¶¶¶¶¶0000000000000000__`````11``
# `````````_¶¶¶¶¶¶¶¶¶000¶¶¶¶¶¶¶¶000000000000011_``_1¶¶¶0`
# `````````_¶¶¶¶¶¶0¶¶000000¶¶¶¶¶¶¶000000000000100¶¶¶¶0_`_
# `````````1¶¶¶¶¶0¶¶¶000000000¶¶¶¶¶¶000000000¶00¶¶01`````
# `````````¶¶¶¶¶0¶0¶¶¶0000000000000¶0¶00000000011_``````_
# ````````1¶¶0¶¶¶0¶¶¶¶¶¶¶000000000000000000000¶11___11111
# ````````¶¶¶¶0¶¶¶¶¶00¶¶¶¶¶¶000000000000000000¶011111111_
# ```````_¶¶¶¶¶¶¶¶¶0000000¶0¶00000000000000000¶01_1111111
# ```````0¶¶¶¶¶¶¶¶¶000000000000000000000000000¶01___`````
# ```````¶¶¶¶¶¶0¶¶¶000000000000000000000000000¶01___1````
# ``````_¶¶¶¶¶¶¶¶¶00000000000000000000000000000011_111```
# ``````0¶¶0¶¶¶0¶¶0000000000000000000000000000¶01`1_11_``
# ``````¶¶¶¶¶¶0¶¶¶0000000000000000000000000000001`_0_11_`
# ``````¶¶¶¶¶¶¶¶¶00000000000000000000000000000¶01``_0_11`
# ``````¶¶¶¶0¶¶¶¶00000000000000000000000000000001```_1_11
``` | output | 1 | 10,980 | 19 | 21,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins. | instruction | 0 | 10,981 | 19 | 21,962 |
Tags: constructive algorithms, games, math
Correct Solution:
```
for _ in range(int(input())):
n,m=map(int,input().split())
print(n-1,m)
``` | output | 1 | 10,981 | 19 | 21,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
# cook your dish here
from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
mod = 1000000007
def L():
return list(map(int, stdin.readline().split()))
def In():
return map(int, stdin.readline().split())
def I():
return int(stdin.readline())
def printIn(ob):
return stdout.write(str(ob) + '\n')
def powerLL(n, p):
result = 1
while (p):
if (p & 1):
result = result * n % mod
p = int(p / 2)
n = n * n % mod
return result
def ncr(n, r):
r = min(r, n - r)
numer = reduce(op.mul, range(n, n - r, -1), 1)
denom = reduce(op.mul, range(1, r + 1), 1)
return numer // denom
def SieveOfEratosthenes(n):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
prime_list = []
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
prime_list.append(p)
# Update all multiples of p
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime_list
def get_divisors(n):
for i in range(1, int(n / 2) + 1):
if n % i == 0:
yield i
yield n
# -------------------------------------
def myCode():
x,y = In()
print(x-1,y)
# print(d)
def main():
for t in range(I()):
myCode()
if __name__ == '__main__':
# print(lst)
main()
``` | instruction | 0 | 10,982 | 19 | 21,964 |
Yes | output | 1 | 10,982 | 19 | 21,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
t=int(input())
for _ in range(t):
a,b=map(int,input().split())
print(a-1,b)
``` | instruction | 0 | 10,983 | 19 | 21,966 |
Yes | output | 1 | 10,983 | 19 | 21,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
def program():
a,b=[int(x) for x in input().split()]
print(a-1,b)
if __name__ == "__main__":
test=1
test=int(input())
for i in range(test):
program()
``` | instruction | 0 | 10,984 | 19 | 21,968 |
Yes | output | 1 | 10,984 | 19 | 21,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
from sys import stdin, stdout
ans = []
def main():
x,y = list(map(int,stdin.readline().split()))
ans.append(str(x-1)+' '+str(y))
if __name__ == '__main__':
for _ in range(int(stdin.readline())):
main()
# code same
stdout.write('\n'.join(ans))
# testing 1234567psdccv 3 dv
``` | instruction | 0 | 10,985 | 19 | 21,970 |
Yes | output | 1 | 10,985 | 19 | 21,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
'''
Online Python Compiler.
Code, Compile, Run and Debug python program online.
Write your code in this editor and press "Run" button to execute it.
'''
n=int(input())
for i in range(n):
a,b=map(int,input().split())
if a<=b:
print(a-1,b)
else:
print((a-b),min(a,b))
``` | instruction | 0 | 10,986 | 19 | 21,972 |
No | output | 1 | 10,986 | 19 | 21,973 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
t = int(input())
spis = []
for _ in range(t):
x1,y1 = 0,0
x, y = map(int, input().split())
if x < y:
if x > 1:
x1 = 1
y1 = y - (x - 1)
if x == 1:
x1 = 0
y1 = y
if x > y:
if y > 1:
x1 = x - y +1
y1 = 1
if y == 1:
x1 = x - 1
y1 = 1
if x == y:
if x == 1 and y == 1:
x1 = 0
y1 = 1
else:
x1 = 1
y1 = 1
spis.append([x1,y1])
for i in range(t):
for j in range(2):
print(spis[i][j],end=' ')
print()
``` | instruction | 0 | 10,987 | 19 | 21,974 |
No | output | 1 | 10,987 | 19 | 21,975 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
from functools import reduce
import collections
import math
import sys
class Read:
@staticmethod
def string():
return input()
@staticmethod
def int():
return int(input())
@staticmethod
def list(delimiter=' '):
return input().split(delimiter)
@staticmethod
def list_int(delimiter=' '):
return list(map(int, input().split(delimiter)))
# infilename = 'input.txt'
# sys.stdin = open(infilename, 'r')
# outfilename = 'output.txt'
# sys.stdout = open(outfilename, 'w')
def main():
t = Read.int()
for _ in range(t):
a, b = Read.list_int()
if a == b:
print(a-1, a)
elif a - b == 1:
print(1, 1)
elif(a==1):
print(0,b)
else:
print(a, b)
if __name__ == '__main__':
main()
``` | instruction | 0 | 10,988 | 19 | 21,976 |
No | output | 1 | 10,988 | 19 | 21,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob play ping-pong with simplified rules.
During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return.
The one who doesn't make a return loses this play. The winner of the play commences the next play. Alice starts the first play.
Alice has x stamina and Bob has y. To hit the ball (while serving or returning) each player spends 1 stamina, so if they don't have any stamina, they can't return the ball (and lose the play) or can't serve the ball (in this case, the other player serves the ball instead). If both players run out of stamina, the game is over.
Sometimes, it's strategically optimal not to return the ball, lose the current play, but save the stamina. On the contrary, when the server commences a play, they have to hit the ball, if they have some stamina left.
Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent.
Calculate the resulting number of Alice's and Bob's wins.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first and only line of each test case contains two integers x and y (1 ≤ x, y ≤ 10^6) — Alice's and Bob's initial stamina.
Output
For each test case, print two integers — the resulting number of Alice's and Bob's wins, if both of them play optimally.
Example
Input
3
1 1
2 1
1 7
Output
0 1
1 1
0 7
Note
In the first test case, Alice serves the ball and spends 1 stamina. Then Bob returns the ball and also spends 1 stamina. Alice can't return the ball since she has no stamina left and loses the play. Both of them ran out of stamina, so the game is over with 0 Alice's wins and 1 Bob's wins.
In the second test case, Alice serves the ball and spends 1 stamina. Bob decides not to return the ball — he loses the play but saves stamina. Alice, as the winner of the last play, serves the ball in the next play and spends 1 more stamina. This time, Bob returns the ball and spends 1 stamina. Alice doesn't have any stamina left, so she can't return the ball and loses the play. Both of them ran out of stamina, so the game is over with 1 Alice's and 1 Bob's win.
In the third test case, Alice serves the ball and spends 1 stamina. Bob returns the ball and spends 1 stamina. Alice ran out of stamina, so she can't return the ball and loses the play. Bob, as a winner, serves the ball in the next 6 plays. Each time Alice can't return the ball and loses each play. The game is over with 0 Alice's and 7 Bob's wins.
Submitted Solution:
```
for _ in range(int(input())):
x, y = map(int, input().split())
if y >= x:
print(0, y - x + 1)
else:
print(y, y)
``` | instruction | 0 | 10,989 | 19 | 21,978 |
No | output | 1 | 10,989 | 19 | 21,979 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,088 | 19 | 22,176 |
Tags: games, math, number theory
Correct Solution:
```
def gcd(a, b):
while a > 0:
a, b = b % a, a
return b
n = int(input())
a = [int(x) for x in input().split()]
g = a[0]
m = a[0]
for i in range(1, n):
g = gcd(g, a[i])
m = m if m >= a[i] else a[i]
if (m // g - n) % 2:
print("Alice")
else:
print("Bob")
``` | output | 1 | 11,088 | 19 | 22,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,089 | 19 | 22,178 |
Tags: games, math, number theory
Correct Solution:
```
def gcd(x,y):
if x<y:
return gcd(y,x)
else:
if y==0:
return x
else:
return gcd(y,x%y)
if __name__=='__main__':
inp = input()
inp = input()
arr = inp.split(' ')
L = []
for a in arr:
L.append(int(a))
L.sort()
mv = L[0]
T = L[1:]
for t in T:
mv = gcd(mv,t)
tot = L[-1]//mv
for l in L:
if l%mv==0:
tot-=1
if tot%2==1:
print("Alice")
else:
print("Bob")
``` | output | 1 | 11,089 | 19 | 22,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,090 | 19 | 22,180 |
Tags: games, math, number theory
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
def nod(a,b):
while a * b:
if a > b:
a, b = b, a % b
else:
a, b = b % a, a
return (a + b)
b = a[:]
ans = nod(b[0], b[1])
b = b[2:]
while b:
ans = nod(ans, b[0])
b = b[1:]
#print(ans)
for i in range(n):
a[i] = a[i] // ans
if (- n + max(a)) % 2 == 0:
print('Bob')
else:
print('Alice')
``` | output | 1 | 11,090 | 19 | 22,181 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,091 | 19 | 22,182 |
Tags: games, math, number theory
Correct Solution:
```
import math
n=int(input())
a=sorted([int(_) for _ in input().split()])
b=0
for i in a:
b=math.gcd(b,i)
c=(a[0]-1)//b
for i in range(n-1):
c+=(a[i+1]-a[i]-1)//b;
if c%2==0: print("Bob")
else: print("Alice")
``` | output | 1 | 11,091 | 19 | 22,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,092 | 19 | 22,184 |
Tags: games, math, number theory
Correct Solution:
```
from fractions import gcd
n = int(input())
sez = [int(i) for i in input().split()]
sez.sort()
g = gcd(sez[0], sez[1])
for i in range(2, n):
g = gcd(g, sez[i])
left = sez[n-1]/g - n
if left%2 == 1:
print("Alice")
else:
print("Bob")
``` | output | 1 | 11,092 | 19 | 22,185 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,093 | 19 | 22,186 |
Tags: games, math, number theory
Correct Solution:
```
from fractions import gcd
n = int(input())
a = [int(x) for x in input().split()]
gg = a[0]
for i in range(1,n):
gg = gcd(gg,a[i])
for i in range(n):
a[i] //= gg
if (max(a) - n) % 2 == 0:
print('Bob')
else:
print('Alice')
``` | output | 1 | 11,093 | 19 | 22,187 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,094 | 19 | 22,188 |
Tags: games, math, number theory
Correct Solution:
```
n=int(input())
A=list(map(int,input().split()))
def gcd(x,y):
while y:
x,y=y,x%y
return x
g=0
for a in A:
g=gcd(g,a)
u=max(A)//g-n
if u%2==0:
print("Bob")
else:
print("Alice")
``` | output | 1 | 11,094 | 19 | 22,189 |
Provide tags and a correct Python 3 solution for this coding contest problem.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | instruction | 0 | 11,095 | 19 | 22,190 |
Tags: games, math, number theory
Correct Solution:
```
import sys
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65
def getInt():
return int(sys.stdin.readline().rstrip())
def getInts():
return map(int, sys.stdin.readline().rstrip().split(" "))
def getString():
return sys.stdin.readline().rstrip()
def printOutput(ans):
sys.stdout.write()
pass
def solve(nums):
c = math.gcd(nums[0], nums[1])
for ele in nums:
c = math.gcd(c, ele)
total = max(nums) // c - len(nums)
if total % 2 == 1:
return "Alice"
return "Bob"
def readinput():
_ = getInt()
nums = list(getInts())
print(solve(nums))
readinput()
``` | output | 1 | 11,095 | 19 | 22,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
def gcd(a, b):
while a:
a, b = b % a, a
return b
n = int(input())
A = list(map(int, input().split()))
G = A[0]
for i in range(1, n):
G = gcd(G, A[i])
for i in range(n):
A[i] //= G
if (max(A) - n) % 2 == 1:
print("Alice")
else:
print("Bob")
``` | instruction | 0 | 11,096 | 19 | 22,192 |
Yes | output | 1 | 11,096 | 19 | 22,193 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
from fractions import gcd
from functools import reduce
n = int(input())
a = [int(i) for i in input().split()]
k = reduce(gcd,a)
m = max(a) / k
if (m-n) % 2 == 0:
print("Bob")
else:
print("Alice")
``` | instruction | 0 | 11,097 | 19 | 22,194 |
Yes | output | 1 | 11,097 | 19 | 22,195 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
def gcd(a, b):
if (a < b):
a, b = b, a
if (b == 0):
return a
return gcd(a % b, b)
n = int(input())
a = list(map(int, input().split()))
ma = max(a)
mi = min(a)
c = a[0]
for i in a[1:]:
c = gcd(c, i)
ma //= c
if (ma - n) % 2 == 1:
print("Alice")
else:
print("Bob")
``` | instruction | 0 | 11,098 | 19 | 22,196 |
Yes | output | 1 | 11,098 | 19 | 22,197 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
import fractions
n = int(input())
A = list(map(int, input().split()))
x = A[0]
for i in A:
x = fractions.gcd(x, i)
if (max(A) // x - len(A)) % 2 == 0:
print('Bob')
else:
print('Alice')
``` | instruction | 0 | 11,099 | 19 | 22,198 |
Yes | output | 1 | 11,099 | 19 | 22,199 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
s = set(a[::])
ans = 1
for i in range(n):
cnt = 0
for j in range(n):
if i == j:
continue
if abs(a[i]-a[j]) not in s:
cnt += 1
ans *= (cnt)
if ans%2 == 0:
print("Bob")
else:
print("Alice")
``` | instruction | 0 | 11,100 | 19 | 22,200 |
No | output | 1 | 11,100 | 19 | 22,201 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
N = int(input())
A = map(int, input().split())
slots = max(A) - N
if (slots % 2 != 0):
print("Alice")
else:
print("Bob")
``` | instruction | 0 | 11,101 | 19 | 22,202 |
No | output | 1 | 11,101 | 19 | 22,203 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
from fractions import gcd
from itertools import combinations
n = int(input())
seq = list(map(int, input().split()))
gcds = []
for a, b in list(combinations(seq, 2)):
gcds.append(gcd(a, b))
if (max(seq) // min(gcds) - n) % 2 != 0:
print("Alice")
else:
print("Bob")
``` | instruction | 0 | 11,102 | 19 | 22,204 |
No | output | 1 | 11,102 | 19 | 22,205 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
Input
The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.
Output
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
Examples
Input
2
2 3
Output
Alice
Input
2
5 3
Output
Alice
Input
3
5 6 7
Output
Bob
Note
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
p=max(a)-n
if p%2==0:
print('Bob')
else:
print('Alice')
``` | instruction | 0 | 11,103 | 19 | 22,206 |
No | output | 1 | 11,103 | 19 | 22,207 |
Provide a correct Python 3 solution for this coding contest problem.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG | instruction | 0 | 11,555 | 19 | 23,110 |
"Correct Solution:
```
while True:
m = int(input())
if not m:
break
n = int(input())
ds = [0] + [int(input()) for _ in range(n)] + [0]
g = [[] for _ in range(n+2)]
rg = [[] for _ in range(n+2)]
for i in range(n+2):
for j in range(min(n+1,i+1),min(n+1,i+m)+1):
j = max(0,min(n+1,j+ds[j]))
g[i].append(j)
rg[j].append(i)
def go(g,bs,i):
bs[i] = True
for j in g[i]:
if not bs[j]:
go(g,bs,j)
bs = [False]*(n+2)
rbs = [False]*(n+2)
go(g,bs,0)
go(rg,rbs,n+1)
if any((b and (not rb)) for b,rb in zip(bs,rbs)):
print('NG')
else:
print('OK')
``` | output | 1 | 11,555 | 19 | 23,111 |
Provide a correct Python 3 solution for this coding contest problem.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG | instruction | 0 | 11,556 | 19 | 23,112 |
"Correct Solution:
```
import queue
while True:
m = int(input())
if m == 0:
break
n = int(input())
d = [0]*(n+2)
for i in range(1,n+1):
d[i] = int(input())
visited = [False]*(n+2)
visited[0] = True
ok = [False]*(n+2)
ok[n+1] = True
rev = [[] for _ in range(n+2)]
que = queue.LifoQueue()
que.put(0)
while not que.empty():
i = que.get()
for j in range(1,m+1):
if i+j > n+1:
break
k = min(max(i+j+d[i+j], 0), n+1)
rev[k].append(i)
if not visited[k]:
que.put(k)
visited[k] = True
que.put(n+1)
while not que.empty():
i = que.get()
for j in rev[i]:
if not ok[j]:
ok[j] = True
que.put(j)
ans = 'OK'
if not visited[n+1]:
ans = 'NG'
for i in range(n+1):
if visited[i] and (not ok[i]):
ans = 'NG'
print(ans)
``` | output | 1 | 11,556 | 19 | 23,113 |
Provide a correct Python 3 solution for this coding contest problem.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG | instruction | 0 | 11,557 | 19 | 23,114 |
"Correct Solution:
```
def reachable(graph, init):
if init not in graph or graph[init] is None:
return set()
tmp, graph[init] = graph[init], None
return tmp.union(* [reachable(graph, i) for i in tmp])
def sugoroku(m, d):
d = [0] + d + [0]
start, goal = 0, len(d) - 1
graph_to = {goal:{goal}}
graph_from = {start:{start}}
for init in range(len(d) - 1):
for xi in range(1, 1 + m):
pos = min(init + xi, goal)
to = max(start, min(pos + d[pos],goal))
try:
graph_to[to].add(init)
except KeyError:
graph_to[to] = {init}
try:
graph_from[init].add(to)
except KeyError:
graph_from[init] = {to}
unreachable_node = reachable(graph_from, start).difference(reachable(graph_to, goal))
return 0 == len(unreachable_node)
import sys
f = sys.stdin
while True:
m = int(f.readline())
if m == 0:
break
d = [int(f.readline()) for _ in range(int(f.readline()))]
print('OK' if sugoroku(m, d) else 'NG')
``` | output | 1 | 11,557 | 19 | 23,115 |
Provide a correct Python 3 solution for this coding contest problem.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG | instruction | 0 | 11,558 | 19 | 23,116 |
"Correct Solution:
```
def search(x, visited, edges):
if visited[x]:
return
visited[x] = True
for to in edges[x]:
search(to, visited, edges)
def main():
while True:
m = int(input())
if m == 0:
break
n = int(input())
mp = [0] + [int(input()) for _ in range(n)] + [0]
visited = [False] * (n + 2)
rev_visited = [False] * (n + 2)
edges = [[] for _ in range(n + 2)]
rev_edges = [[] for _ in range(n + 2)]
for i in range(n + 2):
for j in range(1, m + 1):
nex = i + j
nex = min(nex, n + 1)
nex += mp[nex]
nex = min(nex, n + 1)
nex = max(nex, 0)
edges[i].append(nex)
rev_edges[nex].append(i)
search(0, visited, edges)
search(n + 1, rev_visited, rev_edges)
for node in range(n + 1):
if visited[node] and not rev_visited[node]:
print("NG")
break
else:
print("OK")
main()
``` | output | 1 | 11,558 | 19 | 23,117 |
Provide a correct Python 3 solution for this coding contest problem.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG | instruction | 0 | 11,559 | 19 | 23,118 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
def bfs(adj_list, s):
q = deque([s])
visited = [False]*(n+2)
visited[s] = True
while q:
v = q.popleft()
for nv in adj_list[v]:
if not visited[nv]:
visited[nv] = True
q.append(nv)
return visited
while True:
m = int(input())
if m==0:
break
n = int(input())
d = [int(input()) for _ in range(n)]
adj_list = [[] for _ in range(n+2)]
rev_adj_list = [[] for _ in range(n+2)]
for i in range(n+1):
for j in range(1, m+1):
if i+j>n:
adj_list[i].append(n+1)
rev_adj_list[n+1].append(i)
else:
t = min(n+1, max(i+j+d[i+j-1], 0))
adj_list[i].append(t)
rev_adj_list[t].append(i)
visited_1 = bfs(adj_list, 0)
visited_2 = bfs(rev_adj_list, n+1)
for i in range(n+2):
if visited_1[i] and not visited_2[i]:
print('NG')
break
else:
print('OK')
``` | output | 1 | 11,559 | 19 | 23,119 |
Provide a correct Python 3 solution for this coding contest problem.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG | instruction | 0 | 11,560 | 19 | 23,120 |
"Correct Solution:
```
from collections import deque
while 1:
M = int(input())
if M == 0:
break
N = int(input())
D = [0] + [int(input()) for i in range(N)] + [0]
G = [[] for i in range(N+2)]
u = [0]*(N+2)
que = deque([0])
u[0] = 1
while que:
v = que.popleft()
for j in range(1, M+1):
if D[min(v+j, N+1)] != 0:
to = max(min(v+j+D[v+j], N+1), 0)
else:
to = min(v+j, N+1)
if not u[to]:
que.append(to)
u[to] = 1
G[to].append(v)
z = [0]*(N+2)
que = deque([N+1])
z[N+1] = 1
while que:
v = que.popleft()
for w in G[v]:
if z[w]:
continue
z[w] = 1
que.append(w)
print("OK" if u == z else "NG")
``` | output | 1 | 11,560 | 19 | 23,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG
Submitted Solution:
```
def sugoroku(m, d):
d = [0] + d + [0]
start, goal = 0, len(d) - 1
g = [[0 for j in range(len(d))] for i in range(len(d))]
for i in range(len(d) - 1):
for mi in range(1, m + 1):
pos = min(goal, i + mi)
pos = max(start, pos + d[pos])
g[i][pos] = 1
for k in range(len(d)):
for i in range(len(d)):
for j in range(len(d)):
if g[i][k] and g[k][j]:
g[i][j] = 1
for i in range(len(d) - 1):
if g[start][i] and not g[i][goal]:
return False
return True
import sys
f = sys.stdin
while True:
m = int(f.readline())
if m == 0:
break
d = [int(f.readline()) for _ in range(int(f.readline()))]
print('OK' if sugoroku(m, d) else 'NG')
``` | instruction | 0 | 11,561 | 19 | 23,122 |
No | output | 1 | 11,561 | 19 | 23,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG
Submitted Solution:
```
inf = 1000000
def graph_set(g, i, j, v):
g[i][j >> 5] |= v << (j & 31)
def graph_set1(g, i, j):
graph_set(g, i, j, 1)
def graph_get(g, i, j):
return (g[i][j >> 5] >> (j & 31)) & 1
def solve(dmax, arr):
arr.insert(0, 0)
arr.append(0)
n = len(arr)
g = [[0 for _ in range(int((n + 31) / 32))] for _ in range(n)]
for i in range(n):
graph_set1(g, i, i)
for s in range(n):
for d in range(1, dmax + 1):
next = min(max(0, s + d), n - 1)
next += arr[next]
next = min(max(0, next), n - 1)
graph_set1(g, s, next)
for k in range(n):
for i in range(n):
for j in range(n):
graph_set(g, i, j,
graph_get(g, i, j) | graph_get(g, i, k) & graph_get(g, k, j))
for i in range(0, n - 1):
if graph_get(g, 0, i) and (not graph_get(g, i, n - 1)):
print("NG")
return
print("OK")
def main():
while True:
dmax = int(input())
if dmax == 0:
return
n = int(input())
arr = [int(input()) for _ in range(n)]
solve(dmax, arr)
main()
``` | instruction | 0 | 11,562 | 19 | 23,124 |
No | output | 1 | 11,562 | 19 | 23,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG
Submitted Solution:
```
inf = 1000000
def graph_set(g, i, j, v):
g[i][j >> 5] |= v << (j & 31)
def graph_set1(g, i, j):
graph_set(g, i, j, 1)
def graph_get(g, i, j):
return (g[i][j >> 5] >> (j & 31)) & 1
def solve(dmax, arr):
arr.insert(0, 0)
arr.append(0)
n = len(arr)
g = [[False for _ in range(n)] for _ in range(n)]
for i in range(n):
g[i][i] = True
for s in range(n):
for d in range(1, dmax + 1):
next = min(max(0, s + d), n - 1)
next += arr[next]
next = min(max(0, next), n - 1)
g[s][next] = True
visited = [[False for _ in range(n)] for _ in range(n)]
memo = [[None for _ in range(n)] for _ in range(n)]
def dfs(s, i, t):
if i == t:
return True
if visited[i][t]:
return False
if memo[i][t] != None:
return memo[i][t]
visited[i][t] = True
memo[s][i] = True
for j in range(n):
if g[i][j] and not visited[j][t] and dfs(s, j, t):
g[i][t] = True
return True
memo[i][t] = False
g[i][t] = False
return False
for i in range(n):
for j in range(n):
if dfs(i, i, j):
g[i][j] = True
for i in range(0, n - 1):
if g[0][i] and (not g[i][n - 1]):
print("NG")
return
print("OK")
def main():
while True:
dmax = int(input())
if dmax == 0:
return
n = int(input())
arr = [int(input()) for _ in range(n)]
solve(dmax, arr)
main()
``` | instruction | 0 | 11,563 | 19 | 23,126 |
No | output | 1 | 11,563 | 19 | 23,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Taro made a sugoroku so that everyone can play at the children's association event. In order to make the game interesting, I wrote instructions such as "advance 6" and "back 5" in some of the sugoroku squares other than "Furidashi" and "Agari". Turn the roulette wheel to advance as many times as you can, and if an instruction is written on the stopped square, move according to that instruction. However, it does not follow the instructions of the squares that proceeded according to the instructions.
Roulette shall be able to give a number between 1 and a certain number with equal probability. Also, if you get a larger number than you reach "Agari", or if you follow the instructions and you go beyond "Agari", you will move to "Agari". If you follow the instructions and return before "Furidashi", you will return to "Furidashi".
<image>
However, depending on the instructions of the roulette wheel and the square, it may not be possible to reach the "rise". For example, let's say you made a sugoroku like the one shown in the figure. If you use a roulette wheel that only gives 1 and 2, you can go to "Agari" if you come out in the order of 1 and 2, but if you get 2 first, you will not be able to reach "Agari" forever no matter what comes out. Taro didn't know that and wrote instructions in various squares.
Therefore, on behalf of Taro, please create a program to determine whether or not you may not be able to reach the "rise" depending on the instructions of the roulette and the square.
input
The input consists of multiple datasets. The end of the input is indicated by a single zero line. Each dataset is given in the following format.
max
n
d1
d2
..
..
..
dn
The first line gives the maximum number of roulette wheels max (2 ≤ max ≤ 250), and the second line gives the number of cells other than "starting" and "rising" n (2 ≤ n ≤ 250). .. The next n lines are given the number di (-n ≤ di ≤ n) that represents the indication for each cell. When di is zero, it means that no instruction is written, when it is a positive number, it means | di | forward instruction, and when it is negative, it means | di | back instruction (where | x | is x). Represents an absolute value). All values entered are integers.
The number of datasets does not exceed 100.
output
The judgment result is output to one line for each data set. Depending on the instructions of the roulette and the square, if it may not be possible to reach the "go up", "NG" is output, otherwise "OK" is output.
Example
Input
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
Output
OK
NG
NG
Submitted Solution:
```
def sugoroku(m, d):
d = [0] + d + [0]
start, goal = 0, len(d) - 1
g = [[0 for j in range(len(d))] for i in range(len(d))]
for i in range(len(d) - 1):
for mi in range(m + 1):
pos = min(goal, i + mi)
pos = max(start, pos + d[pos])
g[i][pos] = 1
for i in range(len(d)):
for j in range(len(d)):
for k in range(len(d)):
if g[i][j] and g[j][k]:
g[i][k]
for i in range(len(d)):
if g[start][i] and not g[i][goal]:
return False
return True
import sys
f = sys.stdin
while True:
m = int(f.readline())
if m == 0:
break
d = [int(f.readline()) for _ in range(int(f.readline()))]
print('OK' if sugoroku(m, d) else 'NG')
``` | instruction | 0 | 11,564 | 19 | 23,128 |
No | output | 1 | 11,564 | 19 | 23,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she wants to try a different game — "Snakes and Ladders". Unfortunately, she already killed all the snakes, so there are only ladders left now.
The game is played on a 10 × 10 board as follows:
* At the beginning of the game, the player is at the bottom left square.
* The objective of the game is for the player to reach the Goal (the top left square) by following the path and climbing vertical ladders. Once the player reaches the Goal, the game ends.
* The path is as follows: if a square is not the end of its row, it leads to the square next to it along the direction of its row; if a square is the end of its row, it leads to the square above it. The direction of a row is determined as follows: the direction of the bottom row is to the right; the direction of any other row is opposite the direction of the row below it. See Notes section for visualization of path.
* During each turn, the player rolls a standard six-sided dice. Suppose that the number shown on the dice is r. If the Goal is less than r squares away on the path, the player doesn't move (but the turn is performed). Otherwise, the player advances exactly r squares along the path and then stops. If the player stops on a square with the bottom of a ladder, the player chooses whether or not to climb up that ladder. If she chooses not to climb, then she stays in that square for the beginning of the next turn.
* Some squares have a ladder in them. Ladders are only placed vertically — each one leads to the same square of some of the upper rows. In order for the player to climb up a ladder, after rolling the dice, she must stop at the square containing the bottom of the ladder. After using the ladder, the player will end up in the square containing the top of the ladder. She cannot leave the ladder in the middle of climbing. And if the square containing the top of the ladder also contains the bottom of another ladder, she is not allowed to use that second ladder.
* The numbers on the faces of the dice are 1, 2, 3, 4, 5, and 6, with each number having the same probability of being shown.
Please note that:
* it is possible for ladders to overlap, but the player cannot switch to the other ladder while in the middle of climbing the first one;
* it is possible for ladders to go straight to the top row, but not any higher;
* it is possible for two ladders to lead to the same tile;
* it is possible for a ladder to lead to a tile that also has a ladder, but the player will not be able to use that second ladder if she uses the first one;
* the player can only climb up ladders, not climb down.
Hyakugoku wants to finish the game as soon as possible. Thus, on each turn she chooses whether to climb the ladder or not optimally. Help her to determine the minimum expected number of turns the game will take.
Input
Input will consist of ten lines. The i-th line will contain 10 non-negative integers h_{i1}, h_{i2}, ..., h_{i10}. If h_{ij} is 0, then the tile at the i-th row and j-th column has no ladder. Otherwise, the ladder at that tile will have a height of h_{ij}, i.e. climbing it will lead to the tile h_{ij} rows directly above. It is guaranteed that 0 ≤ h_{ij} < i. Also, the first number of the first line and the first number of the last line always contain 0, i.e. the Goal and the starting tile never have ladders.
Output
Print only one line containing a single floating-point number — the minimum expected number of turns Hyakugoku can take to finish the game. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Examples
Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Output
33.0476190476
Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 4 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 4 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 4 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 9
Output
20.2591405923
Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 6 6 6 6 6 6 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Output
15.9047592939
Note
A visualization of the path and the board from example 2 is as follows: <image>
The tile with an 'S' is the starting tile and the tile with an 'E' is the Goal.
For the first example, there are no ladders.
For the second example, the board looks like the one in the right part of the image (the ladders have been colored for clarity).
It is possible for ladders to overlap, as is the case with the red and yellow ladders and green and blue ladders. It is also possible for ladders to go straight to the top, as is the case with the black and blue ladders. However, it is not possible for ladders to go any higher (outside of the board). It is also possible that two ladders lead to the same tile, as is the case with the red and yellow ladders. Also, notice that the red and yellow ladders lead to the tile with the orange ladder. So if the player chooses to climb either of the red and yellow ladders, they will not be able to climb the orange ladder. Finally, notice that the green ladder passes through the starting tile of the blue ladder. The player cannot transfer from the green ladder to the blue ladder while in the middle of climbing the green ladder. | instruction | 0 | 11,730 | 19 | 23,460 |
Tags: dp, probabilities, shortest paths
Correct Solution:
```
h = [list(map(int, input().split())) for _ in range(10)]
tp = {}
def celli(x, y):
if x % 2:
return (9 - x) * 10 + y
return (9 - x) * 10 + 9 - y
for i in range(10):
for j in range(10):
if h[i][j]:
tp[celli(i, j)] = celli(i - h[i][j], j)
dp = [0] * 94 + [6] * 5 + [0]
for i in range(93, -1, -1):
tot = 0
for j in range(1, 7):
if i + j in tp:
tot += min(dp[i + j], dp[tp[i + j]])
else:
tot += dp[i + j]
tot /= 6
tot += 1
dp[i] = tot
print(dp[0])
``` | output | 1 | 11,730 | 19 | 23,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she wants to try a different game — "Snakes and Ladders". Unfortunately, she already killed all the snakes, so there are only ladders left now.
The game is played on a 10 × 10 board as follows:
* At the beginning of the game, the player is at the bottom left square.
* The objective of the game is for the player to reach the Goal (the top left square) by following the path and climbing vertical ladders. Once the player reaches the Goal, the game ends.
* The path is as follows: if a square is not the end of its row, it leads to the square next to it along the direction of its row; if a square is the end of its row, it leads to the square above it. The direction of a row is determined as follows: the direction of the bottom row is to the right; the direction of any other row is opposite the direction of the row below it. See Notes section for visualization of path.
* During each turn, the player rolls a standard six-sided dice. Suppose that the number shown on the dice is r. If the Goal is less than r squares away on the path, the player doesn't move (but the turn is performed). Otherwise, the player advances exactly r squares along the path and then stops. If the player stops on a square with the bottom of a ladder, the player chooses whether or not to climb up that ladder. If she chooses not to climb, then she stays in that square for the beginning of the next turn.
* Some squares have a ladder in them. Ladders are only placed vertically — each one leads to the same square of some of the upper rows. In order for the player to climb up a ladder, after rolling the dice, she must stop at the square containing the bottom of the ladder. After using the ladder, the player will end up in the square containing the top of the ladder. She cannot leave the ladder in the middle of climbing. And if the square containing the top of the ladder also contains the bottom of another ladder, she is not allowed to use that second ladder.
* The numbers on the faces of the dice are 1, 2, 3, 4, 5, and 6, with each number having the same probability of being shown.
Please note that:
* it is possible for ladders to overlap, but the player cannot switch to the other ladder while in the middle of climbing the first one;
* it is possible for ladders to go straight to the top row, but not any higher;
* it is possible for two ladders to lead to the same tile;
* it is possible for a ladder to lead to a tile that also has a ladder, but the player will not be able to use that second ladder if she uses the first one;
* the player can only climb up ladders, not climb down.
Hyakugoku wants to finish the game as soon as possible. Thus, on each turn she chooses whether to climb the ladder or not optimally. Help her to determine the minimum expected number of turns the game will take.
Input
Input will consist of ten lines. The i-th line will contain 10 non-negative integers h_{i1}, h_{i2}, ..., h_{i10}. If h_{ij} is 0, then the tile at the i-th row and j-th column has no ladder. Otherwise, the ladder at that tile will have a height of h_{ij}, i.e. climbing it will lead to the tile h_{ij} rows directly above. It is guaranteed that 0 ≤ h_{ij} < i. Also, the first number of the first line and the first number of the last line always contain 0, i.e. the Goal and the starting tile never have ladders.
Output
Print only one line containing a single floating-point number — the minimum expected number of turns Hyakugoku can take to finish the game. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Examples
Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Output
33.0476190476
Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 4 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 4 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 4 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 9
Output
20.2591405923
Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 6 6 6 6 6 6 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Output
15.9047592939
Note
A visualization of the path and the board from example 2 is as follows: <image>
The tile with an 'S' is the starting tile and the tile with an 'E' is the Goal.
For the first example, there are no ladders.
For the second example, the board looks like the one in the right part of the image (the ladders have been colored for clarity).
It is possible for ladders to overlap, as is the case with the red and yellow ladders and green and blue ladders. It is also possible for ladders to go straight to the top, as is the case with the black and blue ladders. However, it is not possible for ladders to go any higher (outside of the board). It is also possible that two ladders lead to the same tile, as is the case with the red and yellow ladders. Also, notice that the red and yellow ladders lead to the tile with the orange ladder. So if the player chooses to climb either of the red and yellow ladders, they will not be able to climb the orange ladder. Finally, notice that the green ladder passes through the starting tile of the blue ladder. The player cannot transfer from the green ladder to the blue ladder while in the middle of climbing the green ladder. | instruction | 0 | 11,731 | 19 | 23,462 |
Tags: dp, probabilities, shortest paths
Correct Solution:
```
X = [[int(a) for a in input().split()] for _ in range(10)]
Y = [(i//10, 9-i%10 if (i//10)&1 else i%10) for i in range(100)]
Z = [[i * 10 + 9 - j if i & 1 else i * 10 + j for j in range(10)] for i in range(10)]
E = [0] * 100
F = [0] * 100
for i in range(1, 6):
F[i] = E[i] = (sum(E[:i]) + 6) / i
for i in range(6, 100):
F[i] = E[i] = sum(F[i-6:i])/6 + 1
x, y = Y[i]
if X[x][y]: F[i] = min(E[i], E[Z[x-X[x][y]][y]])
print(F[99])
``` | output | 1 | 11,731 | 19 | 23,463 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.