text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide a correct Python 3 solution for this coding contest problem.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
"Correct Solution:
```
print('YNEOS'[int(''.join(input().split()))%4!=0::2])
```
| 89,100 |
Provide a correct Python 3 solution for this coding contest problem.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
"Correct Solution:
```
X, A, B = map(int, input().split())
print(["NO", "YES"][(A*10+B)%4==0])
```
| 89,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
a = int("".join(input().split()))
print("YES" if a % 4 == 0 else "NO")
```
Yes
| 89,102 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
N = input().split()
print(["YES", "NO"][not(int("".join(N))%4==0)])
```
Yes
| 89,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
print('YES' if int(''.join(input().split()[1:]))%4==0 else 'NO')
```
Yes
| 89,104 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
s = int("".join(input().split()))
print(["NO", "YES"][s % 4 == 0])
```
Yes
| 89,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
r,g,b = input().split()
if int((g*10+b)%4==0):
print('YES')
else:
print('No')
```
No
| 89,106 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
r,g,b= map(int(input().split()))
num = 100*r + 10*g + b
if num %4 == 0:
print('YES')
else:
print('NO')
```
No
| 89,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
a, b, c = map(int, input().split())
num = a*100 + b*10 + c
if num % 4 == 0:
print("YES)
else:
@rint("NO")
```
No
| 89,108 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer has three cards, one red, one green and one blue.
An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
Is this integer a multiple of 4?
Constraints
* 1 ≤ r, g, b ≤ 9
Input
Input is given from Standard Input in the following format:
r g b
Output
If the three-digit integer is a multiple of 4, print `YES` (case-sensitive); otherwise, print `NO`.
Examples
Input
4 3 2
Output
YES
Input
2 3 4
Output
NO
Submitted Solution:
```
def calc():
ans = r*100 + g*10 + b
if ans%4 == 0:
return "YES"
else:
return "NO"
r, g, b = map(int, input().split())
print(calc)
```
No
| 89,109 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
n = int(input())
q = [int(x) for x in input().split(' ')]
q_set = set(q)
if (n%2 == 1):
count = 2**((n-1)//2)
else:
count = 2**(n//2)
if (sum(q) == 2*sum(q_set)):
print(count%(10**9+7))
else:
print(0)
```
| 89,110 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
NUM = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
length = len(A)
if not length % 2 == 0:
length -= 1
for i in range(0, length, 2):
if not A[i] == A[i + 1]:
print(0)
exit()
print((2 ** (length // 2)) % NUM)
```
| 89,111 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
N = int(input())
A = sorted(list(map(int, input().split())))
cr_A = []
if N%2:
cr_A.append(0)
for i in range(1, N//2 + 1):
cr_A.extend([2*i]*2)
else:
for i in range(1, N//2 + 1):
cr_A.extend([2*i - 1]*2)
if A == cr_A:
if N > 1:
print((2**(N//2))%(10**9 + 7))
else:
print(1)
else:
print(0)
```
| 89,112 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
from collections import defaultdict
MOD = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)
for num in A:
d[num] += 1
d[0] += 1
if all(d[i] == 2 for i in range((N + 1) % 2, N, 2)):
print(2 ** (N // 2) % MOD)
else:
print(0)
```
| 89,113 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
iN = int(input())
aA = [int(_) for _ in input().split()]
aA.sort()
iD = 10**9 + 7
iRet = 0
if iN % 2 == 0:
aR = [x if x % 2 == 1 else x + 1 for x in range(iN)]
else:
aR = [x if x % 2 == 0 else x + 1 for x in range(iN)]
if aA == aR:
print(pow(2,iN//2,iD))
else:
print(0)
```
| 89,114 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
n = N-1
for i in range(N//2):
if A[2*i]!=n or A[2*i+1]!=n:
print(0)
exit()
n -= 2
if N&1 and A[-1]!=0:
print(0)
exit()
print((1<<N//2)%(10**9+7))
```
| 89,115 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
N = int(input())
A = list(map(int, input().split()))
L = [abs((N-1) - 2*i) for i in range(N)]
L.sort(); A.sort()
print(pow(2, (N-N%2)//2, 10**9+7) if L == A else 0)
```
| 89,116 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
"Correct Solution:
```
from collections import Counter
n = int(input())
A = list(map(int,input().split()))
mod = 10**9 + 7
c = Counter(A)
flg = True
for k,v in c.items():
if k==0:
if v!=1:
flg = False
else:
if v!=2:
flg = False
if flg is False:
print(0)
else:
print(pow(2, n//2, mod))
```
| 89,117 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
flag = True
if n%2 == 0:
if 0 in A or len(set(A))!=n//2:
flag = False
else:
if A.count(0)!=1 or len(set(A))!=n//2+1:
flag = False
if flag:
print(2**(n//2) % mod)
else:
print(0)
```
Yes
| 89,118 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
n=int(input())
A=sorted((map(int,input().split())))
G=sorted([2*(i+1) for i in range(n//2)]*2+[0])
K=sorted([(2*i+1) for i in range(n//2)]*2)
if n%2:
if A!=G:
print(0)
exit()
else:
if A!=K:
print(0)
exit()
print(2**(n//2)%(10**9+7))
```
Yes
| 89,119 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
N = int(input())
A = [int(x) for x in input().split()]
from collections import Counter
def f():
cnt = Counter(A)
m = N - 1
for k, v in cnt.items():
if k == 0 and v == 1: continue
if v != 2 or k > m:
return 0
return pow(2, N//2, 10**9 + 7)
print(f())
```
Yes
| 89,120 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
from collections import Counter
N, *A = map(int, open(0).read().split())
l = [v for _, v in sorted(Counter(A).items())]
if all(v == 2 for v in l[N % 2:]) and (N % 2 == 0 or l[0] == 1):
print(2 ** (N // 2) % (10 ** 9 + 7))
else:
print(0)
```
Yes
| 89,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque
sys.setrecursionlimit(10000000)
ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]
iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]
lcm = lambda x, y: (x * y) // math.gcd(x, y)
MOD = 10 ** 9 + 7
MAX = float('inf')
def main():
if os.getenv("LOCAL"):
sys.stdin = open("input.txt", "r")
N = ii()
A = il()
cnt = collections.Counter(A)
start = 1 if N % 2 == 0 else 0
m = 0
for n in range(start, N, 2):
if n == 0 and cnt[n] == 1:
continue
elif cnt[n] == 2:
m += 1
continue
else:
print(0)
exit()
print((m**2)%MOD)
if __name__ == '__main__':
main()
```
No
| 89,122 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
n=int(input())
a=list(map(int, input().split()))
from collections import Counter
c=Counter(a)
cnt_lst=[0]*n
for key in c:
cnt_lst[key]+=c[key]
flag=True
if n%2==1:
if cnt_lst[0]==1:
cnt_lst.pop(0)
for i in range(n-1):
if (i%2==1 and cnt_lst[i]!=2) or (i%2==0 and cnt_lst[i]!=0):
flag=False
else:
for i in range(n):
if (i%2==1 and cnt_lst[i]!=2) or (i%2==0 and cnt_lst[i]!=0):
flag=False
print(2**(n//2) if flag else 0)
```
No
| 89,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
t = int(input())
m = list(map(int,input().split()))
q={}
z=False
cnt0=0
for i in range(t):
d=m[i]
a=(t-1-d)//2
if d==0:
cnt0+=1
if (a,a+d) not in q:
q[(a,a+d)]=0
q[(a,a+d)]+=1
if q[(a,a+d)]>2:
z=True
break
if z or cnt0>1:
print(0)
elif cnt0==1 and t%2==0:
print(0)
else:
res=1
for i in q:
if i[0]==i[1]:
res=res*1
else:
res=res*2
print(res)
```
No
| 89,124 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.
Constraints
* 1≦N≦10^5
* 0≦A_i≦N-1
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of the possible orders in which they were standing, modulo 10^9+7.
Examples
Input
5
2 4 4 0 2
Output
4
Input
7
6 4 0 2 4 0 2
Output
0
Input
8
7 5 1 1 7 3 5 3
Output
16
Submitted Solution:
```
N = int(input())
A = list(map(int,input().split()))
d = N // 2
# ans = []
# def linking_up():
ans_1 = [ i for i in range(N) if A.count(i) == 2]
# for i in range(N):
# if i % 2 == 0 and i != 0:
# if A.count(i) == 2 and A.count(0) == 1:
# ans.append('c')
# ans_2 = [ i for i in range(N) if i % 2 and A.count(i) == 2]
# else:
# if A.count(i) == 2:
# ans.append('a')
# linking_up()
if len(ans_1) == d:
print(2 ** d)
else:
print(0)
```
No
| 89,125 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
import math
n = int(input())
s = int(input())
if s == n:
print(n + 1)
exit()
elif s > n:
print(-1)
exit()
for i in range(2, math.floor(math.sqrt(n)) + 1):
new_n = n
remain = 0
while new_n > 0:
remain += new_n % i
new_n //= i
if remain == s:
print(i)
exit()
for i in range(math.floor(math.sqrt(n)) + 1, 0, -1):
b = (n - s) // i + 1
st = n % b + n // b
if n // b != b and st == s:
print(b)
exit()
print(-1)
```
| 89,126 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
#!/usr/bin/env python3
import sys
def f(b: int, n: int) -> int:
if n < b:
return n
return f(b, n // b) + n % b
def sqrt(n: int):
lb = 0
hb = n + 1
while hb - lb > 1:
m = (lb + hb) // 2
if m * m <= n:
lb = m
else:
hb = m
return lb
def solve(n: int, s: int):
if s == n:
print(n + 1)
return
sn = sqrt(n)
for b in range(2, sn + 1):
if f(b, n) == s:
print(b)
return
for p in reversed(range(1, sn + 1)):
q = s - p
if q < 0:
continue
b, r = divmod(n - q, p)
if r != 0 or b <= p:
continue
if f(b, n) == s:
print(b)
return
print(-1)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
n = int(next(tokens)) # type: int
s = int(next(tokens)) # type: int
solve(n, s)
if __name__ == '__main__':
main()
```
| 89,127 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
n = int(input())
s = int(input())
rtn = -1* int( -1 * (n**0.5))
def f(b,n):
if n<b:
return n
return f(b,n//b) + n % b
if n < s or (n // 2)+1 < s < n :
print("-1")
elif s == n:
print(n+1)
elif s == n//2 +1:
if n % 2 == 1:
print(n//2 + 1)
else:
print("-1")
elif (n//3) + 2 < s <= n//2:
print(n-s+1)
else:
for b in range(2,rtn+1):
if f(b,n) == s:
print(b)
exit()
for p in range(rtn,0,-1):
if (n-s) % p == 0:
b = (n-s)//p + 1
if f(b,n) == s:
print(b)
exit()
print(n-s+1)
```
| 89,128 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
import math
n = int(input())
s = int(input())
if n == s:
print(n+1)
exit()
ru = math.ceil(math.sqrt(n))+1
for i in range(2,ru+1):
b = n+0
a = 0
while b:
a +=b%i
b//=i
if s == a:
print(i)
exit()
for i in range(ru,0,-1):
q = s-i
if q <0:
continue
c = (n-q)// i
if c <= ru-1:
continue
b = n+0
a = 0
while b:
a+=b%c
b//=c
if a == s:
print(c)
exit()
print(-1)
```
| 89,129 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
def d_DigitSum(N, S):
if S == N:
# f(b,N)=Nを満たすbはN+1が最小
return N + 1
if S > N:
return -1
from math import sqrt, floor, ceil
def f(b, n):
if n < b:
return n
else:
return f(b, n // b) + (n % b)
sn = ceil(sqrt(N))
# 2<=b<=√Nまで探索
for b in range(2, sn + 1):
if f(b, N) == S:
return b
# √N<=b<=Nまで探索
for p in range(sn, 0, -1):
if (N - S) % p == 0:
b = (N - S) // p + 1
if f(b, N) == S:
return b
return -1
N = int(input())
S = int(input())
print(d_DigitSum(N, S))
```
| 89,130 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
from math import sqrt
def n_based(n:int, base:int) -> int:
if n == 0:
return 0
ans = 0
res = n
while abs(res) > 0:
q = res%base
if q>=0:
ans = q + ans
res //= base
else:
q += abs(base)
ans = q + ans
res //= base
res += 1
return ans
n = int(input())
s = int(input())
def judge(n:int, s:int) -> int:
if n < s:
return -1
elif n==1 and s==1:
return 2
elif n == s:
return n+1
else:
for i in range(2,int(sqrt(n))+2):
#print(i)
if n_based(n,i) == s:
return i
for p in range(int(sqrt(n)), 0, -1):
if (n-s+p) % p == 0:
b = (n-s+p) // p
#print(p,b)
if n_based(n,b) == s:
return b
return -1
print(int(judge(n,s)))
```
| 89,131 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
import math
n = int(input())
s = int(input())
f = None
for i in range(2, math.ceil(math.sqrt(n))):
c = 0
k = n
while k > 0:
c += k % i
k //= i
if c == s:
f = i
break
if f is None:
for i in range(int(math.sqrt(n)), 1, -1):
b = n // i
k = n
c = 0
while k > 0:
c += k % b
k //= b
if s >= c and (s - c) % i == 0 and b - (s - c) // i > n // (i + 1):
f = b - (s - c) // i
break
if f is None:
if s == n: f = n + 1
elif s <= (n + 1) // 2: f = n - s + 1
elif s == 1: f = n
if f is None: print(-1)
else: print(f)
```
| 89,132 |
Provide a correct Python 3 solution for this coding contest problem.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
"Correct Solution:
```
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def f(b,n):
if(n<b): return n
return f(b,n//b)+n%b
def resolve():
n=int(input())
s=int(input())
if(s>n):
print(-1)
return
if(s==n):
print(n+1)
return
# 2 <= b <= sqrt(n) を全探索
k=int(n**.5)
for b in range(2,k+1):
if(f(b,n)==s):
print(b)
return
# sqrt(n) < b <= n を考える
B=[]
for p in range(1,k+1):
if((n-s)%p): continue
b=(n-s)//p+1
if(b<2): continue
if(f(b,n)==s):
B.append(b)
print(min(B) if(B) else -1)
resolve()
```
| 89,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
import math
import sys
import functools
sys.setrecursionlimit(10 ** 9)
@functools.lru_cache(None)
def f(b, n):
if n < b:
return n
else:
return f(b, n // b) + (n % b)
def main(n, s):
if s == n:
return n + 1
if s > n:
return -1
for b in range(2, math.ceil(math.sqrt(n)) + 1):
if f(b, n) == s:
return b
ans = float('inf')
for p in range(1, math.ceil(math.sqrt(n)) + 1):
if (n - s) % p == 0:
b = (n - s) // p + 1
if f(b, n) == s:
ans = min(ans, b)
return -1 if ans == float('inf') else ans
if __name__ == '__main__':
n = int(input())
s = int(input())
print(main(n, s))
```
Yes
| 89,134 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
#
# ⋀_⋀
# (・ω・)
# ./ U ∽ U\
# │* 合 *│
# │* 格 *│
# │* 祈 *│
# │* 願 *│
# │* *│
#  ̄
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないyp
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A):
for a in A: print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
N = I()
S = I()
if N==S:
print(N+1)
return
def check(N,b):
res = 0
while N:
res += N%b
N//=b
return res == S
for b in range(2,int(sqrt(N)+1)):
if check(N,b):
print(b)
return
for p in range(1,int(sqrt(N)+1))[::-1]:
b = (N-S+p)//p
if b>=2 and check(N,b):
print(b)
return
print(-1)
if __name__ == '__main__':
main()
```
Yes
| 89,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
n=int(input())
s=int(input())
if n==s:
print(n+1)
exit()
if n<s:
print(-1)
exit()
for b in range(2,int(n**(1/2))+1):
m=n
wa=0
while m>0:
wa+=m%b
m=m//b
if wa==s:
print(b)
exit()
for p in range(int((n-1)**(1/2)),0,-1):
if (n-s)%p==0:
b=(n-s)//p+1
if p+n%b==s:
print(b)
exit()
print(-1)
```
Yes
| 89,136 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
n = int(input())
s = int(input())
import math
def f(b, n):
# print(b, n)
if n<b:
return n
else:
x = f(b, n//b) + n%b
return x
ans = 0
if n<s:
print(-1)
exit()
if n==s:
print(n+1)
exit()
for b in range(2,int(math.sqrt(n))+500):
if f(b, n) == s:
ans = b
print(ans)
exit()
for p in range(int(math.sqrt(n)), 0, -1):
if (n+p-s) %p == 0:
# print(p)
if f((n+p-s)//p, n) == s:
print((n+p-s)//p)
exit()
print(-1)
```
Yes
| 89,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
n = int(input())
s = int(input())
def f(b,n) :
r = 0
i = 1
while n:
j = n % b
r += j
n = (n-j)//b
return r
if n < s or (n // 2)+1 < s < n :
print("-1")
elif s == n//2 +1:
if n % 2 == 1:
print(n//2 + 1)
else:
print("-1")
elif (n//3) + 2 < s <= n//2:
print(n-s+1)
elif s == n:
print(n+1)
elif s == 1:
def check(n,b):
while 1 < n :
if n % b != 0:
break
n //= b
if n == 1:
return b
return 0
for b in range(2,int(n**0.5)+2):
r = check(n,b)
if r:
print(r)
exit()
print(n)
else:
x = n // s
if s - (n //(x+1)) <= x:
x = x + 1
if 3 < n/(x -1) - n/x :
for b in range(n//x, 2*n//x - n//(x+1)):
if f(b,n) == s:
print(b)
exit()
for i in range(x-1,2,-1):
for b in range(n//i + (n//x)//(n//i), n//i + (n//x)//(n//i) + (2*n//x - n//(x+1))//x-i):
if f(b,n) == s:
print(b)
exit()
else:
for b in range(2,(n//2)+2):
if f(b,n) == s:
print(b)
exit()
print(n-s+1)
```
No
| 89,138 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
n = int(input())
s = int(input())
for i in range(2, int((n+1)**.5)):
c = 0
m = n
while m > 0:
c += m%i
m //= i
if c > s:
break
if c == s:
print(i)
exit()
else:
pass
if n == s:
print(n+1)
elif n <= (n+1)//2:
print(n-s+1)
elif s == 1:
print(n)
else:
print(-1)
```
No
| 89,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
def read():
return int(input())
def reads(sep=None):
return list(map(int, input().split(sep)))
def f(b, n):
c = 0
while n:
if n < b:
c += n
break
else:
c += n % b
n //= b
return c
def main():
n = read()
s = read()
if n < s:
print(-1)
elif n == s:
print(n + 1)
else:
b = 2
while b*b <= n:
if f(b, n) == s:
print(b)
return
b += 1
i = 1
bs = []
while i*i <= n:
if (n-s)%i != 0:
bs.append((n-s)%i + 1)
i += 1
for b in sorted(bs):
if f(b, n) == s:
print(b)
return
print(-1)
main()
```
No
| 89,140 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq 2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
* 1 \leq n \leq 10^{11}
* 1 \leq s \leq 10^{11}
* n,\,s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print `-1` instead.
Examples
Input
87654
30
Output
10
Input
87654
138
Output
100
Input
87654
45678
Output
-1
Input
31415926535
1
Output
31415926535
Input
1
31415926535
Output
-1
Submitted Solution:
```
n=int(input())
s=int(input())
rootn=0
while rootn**2<=n:
rootn+=1
def fun(b,n):
if n<b:return n
elif n>=b:
return fun(b,n//b)+n%b
def main(n,s):
if n==s:return n+1
for b in range(2,rootn):
if fun(b,n)==s:return int(b)
for p in reversed(range(1,rootn)):
b=1+(n-s)//p
if fun(b,n)==s:return int(b)
b=2+(n-s)//p
if fun(b,n)==s:return int(b)
return -1
x=main(n,s)
print(x)
#http://arc060.contest.atcoder.jp/data/arc/060/editorial.pdf
```
No
| 89,141 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
import sys
def f():
w=m=0;n,q=map(int,input().split());s=[0]*-~n
for e in sys.stdin:
a,v=map(int,e.split());s[a]+=v
if v<0 and a==w:m=max(s);w=s.index(m)
elif s[a]>m:w,m=a,s[a]
elif s[a]==m:w=min(w,a)
print(w,m)
if'__main__'==__name__:f()
```
| 89,142 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
def solve():
import heapq
from sys import stdin
lines = stdin
n, q = map(int, lines.readline().split())
# hq element: [number of caught fish, participant number]
hq = [(0, i) for i in range(1, n + 1)]
heapq.heapify(hq)
fish = [0] * (n + 1) # number of caught fish
ans = ""
for i in range(q):
a, v = map(int, lines.readline().split())
# Because of Python heapq specification, negative values are used for fishing.
v *= -1
fish[a] += v
heapq.heappush(hq, (fish[a], a))
while hq[0][0] != fish[hq[0][1]]:
heapq.heappop(hq)
ans += f"{hq[0][1]} {-hq[0][0]}\n"
print(ans, end="")
solve()
```
| 89,143 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
"""
セグメント木
"""
INF = 10 ** 20
n, q = map(int, input().split())
size = 1
while size < n:
size *= 2
size = size * 2 - 1
seg_tree = [(INF, 0) for _ in range(size)]
def update(a, v):
ind = (size - 1) // 2 + a
prea, prev = seg_tree[ind]
seg_tree[ind] = (a + 1, prev + v)
while ind > 0:
ind = (ind - 1) // 2
ca1, cv1 = seg_tree[ind * 2 + 1]
ca2, cv2 = seg_tree[ind * 2 + 2]
if cv1 >= cv2:
seg_tree[ind] = (ca1, cv1)
else:
seg_tree[ind] = (ca2, cv2)
for i in range(q):
a, v = map(int, input().split())
a -= 1
update(a, v)
print(seg_tree[0][0], seg_tree[0][1])
```
| 89,144 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
n,q=map(int,input().split())
s=[0]*-~n
w=m=0
for _ in[0]*q:
a,v=map(int,input().split())
s[a]+=v
if v<0 and a==w:m=max(s);w=s.index(m)
elif s[a]>m:w,m=a,s[a]
elif s[a]==m and a<w:w=a
print(w,m)
```
| 89,145 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
[n,q] = map(int,input().split())
t = [ 0 for _ in range(n) ]
max_idx = 0
max_val = 0
for _ in range(q):
[a,v] = map(int,input().split())
t[a-1] += v
if max_idx == a-1:
if v > 0:
max_val = t[a-1]
else:
max_val = max(t)
max_idx = t.index(max_val)
elif t[a-1] > max_val:
max_val = t[a-1]
max_idx = a-1
elif t[a-1] == max_val:
if a-1 < max_idx:
max_val = t[a-1]
max_idx = a-1
print("{} {}".format(max_idx+1,max_val))
```
| 89,146 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
import sys
def f():
w=m=0;n,q=map(int,input().split());s=[0]*-~n
for e in sys.stdin:
a,v=map(int,e.split());s[a]+=v
if v<0 and a==w:m=max(s);w=s.index(m)
elif s[a]>m:w,m=a,s[a]
elif s[a]==m and w>a:w=a
print(w,m)
f()
```
| 89,147 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
import sys
def f():
w=m=0;n,q=map(int,input().split());s=[0]*-~n
for e in sys.stdin:
a,v=map(int,e.split());s[a]+=v
if v<0 and a==w:m=max(s);w=s.index(m)
elif s[a]>m:w,m=a,s[a]
elif s[a]==m:w=min(w,a)
print(w,m)
f()
```
| 89,148 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
"Correct Solution:
```
"""
セグメント木
"""
INF = 10 ** 20
def main():
n, q = map(int, input().split())
size = 1
while size < n:
size *= 2
size = size * 2 - 1
seg_tree = [(INF, 0) for _ in range(size)]
def update(a, v):
ind = (size - 1) // 2 + a
prea, prev = seg_tree[ind]
seg_tree[ind] = (a + 1, prev + v)
while ind > 0:
ind = (ind - 1) // 2
ca1, cv1 = seg_tree[ind * 2 + 1]
ca2, cv2 = seg_tree[ind * 2 + 2]
if cv1 >= cv2:
seg_tree[ind] = (ca1, cv1)
else:
seg_tree[ind] = (ca2, cv2)
for i in range(q):
a, v = map(int, input().split())
update(a - 1, v)
print(seg_tree[0][0], seg_tree[0][1])
main()
```
| 89,149 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
n, q = map(int, input().split())
n = [0]*(n+1)
maxv = winner = 0
for _ in range(q):
a, v = map(int, input().split())
n[a] += v
if v < 0 and a == winner:
maxv = max(n)
winner = n.index(maxv)
elif n[a] > maxv:
maxv, winner = n[a], a
elif n[a] == maxv:
winner = min(winner, a)
print(winner, maxv)
```
Yes
| 89,150 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
import queue
n,q=map(int,input().split())
p=queue.PriorityQueue()
s=[0]*-~n
for _ in[0]*q:
a,v=map(int,input().split())
s[a]+=v;p.put((-s[a],a))
while 1:
t=p.get()
if -t[0]==s[t[1]]:print(t[1],-t[0]);p.put(t);break
```
Yes
| 89,151 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
n, q = map(int, input().split())
result = [0 for i in range(n + 1)]
max_a = -1
max_v = 0
for i in range(q):
a, v = map(int, input().split())
result[a] += v
if result[a] > max_v:
max_a = a
max_v = result[a]
elif result[a] == max_v:
if a < max_a:
max_a = a
elif a == max_a and result[a] <= max_v:
max_a = result.index(max(result))
max_v = result[max_a]
print(max_a, max_v)
```
Yes
| 89,152 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
n, q = [int(el) for el in input().split(' ')]
data = [0] * n
result, index = 0, 1
for _ in range(q):
a, v = [int(el) for el in input().split(' ')]
data[a-1] += v
if v > 0:
if result < data[a-1]:
result, index = data[a-1], a
elif result == data[a-1]:
index = min(index, a)
else:
if index == a:
result = max(data)
index = data.index(result) + 1
print(index, result)
```
Yes
| 89,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
from sys import stdin
n, q = [int(_) for _ in input().split()]
book = [0] * (n+1)
max_per, max_val = q, 0
for _ in stdin.readlines() :
a, v = [int(_2) for _2 in _.split()]
book[a] += v
if v < 0 :
max_val = max(book)
max_per = book.index(max_val)
elif max_val < book[a] : max_per, max_val = a, book[a]
elif max_val == book[a] and a < max_per : max_per = a
print(max_per, max_val)
```
No
| 89,154 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
def liquidation(A):
A.sort()#??????????????????????????????
for i in range(len(A)):
for j in range(i+1,len(A)):
if A[i][1]==A[j][1]:
A[i][0]+=A[j][0]
del A[j]
break
return A
def rank(A):
Max=A[0][1]
champ=A[0][0]
for i in A:
if Max<i[1]:
champ=i[0]
Max=i[1]
print(champ,Max)
def Rank(A):
Max=A[0][0]
champ=A[0][1]
for i in range(1,len(A)):
if A[i][0]==Max:
champ=A[i][1]
else:
break
print(champ,Max)
n,q=map(int,input().split())
A=[]
for i in range(q):
a,v=map(int,input().split())
A.append([v,a])#a??????????????????, v?????£??£???????????°
A=liquidation(A)
A.sort(reverse=True)
#print(A)
Rank(A)
```
No
| 89,155 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
from collections import deque
n, q = [int(_) for _ in input().split()]
book = deque()
for _ in range(n+1) : book.append(0)
for _ in range(q) :
a, v = [int(_2) for _2 in input().split()]
book[a] += v
print(book.index(max(book)), max(book))
```
No
| 89,156 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time.
Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number.
input
The input is given in the following format.
n q
a1 v1
a2 v2
::
aq vq
n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given.
output
For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank.
Example
Input
3 5
1 4
2 5
1 3
3 6
2 7
Output
1 4
2 5
1 7
1 7
2 12
Submitted Solution:
```
n,q=map(int,input().split())
s=[0]*-~n
w=m=0
for _ in[0]*q:
a,v=map(int,input().split())
s[a]+=v
if v<0 and a==w:m=max(s);w=s.index(m)
elif s[a]>maxv:w,m=a,s[a]
elif s[a]==maxv:w=min(w,a)
print(w,m)
```
No
| 89,157 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
from operator import itemgetter
while 1:
n = int(input())
if n == 0:
break
t_w = []
for _ in range(n):
m, a, b = map(int, input().split())
t_w.append((a, m, 5))
t_w.append((b, -m, 3))
t_w.sort(key=itemgetter(0, 2))
ans, cur = 'OK', 0
for t, w, pri in t_w:
cur += w
# print('t', t, 'w', w, 'cur', cur)
if cur > 150:
ans = 'NG'
break
print(ans)
```
| 89,158 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
import operator
while 1:
n = int(input())
if n == 0:break
s = [list(map(int,input().split())) for i in range(n)]
e = s[:]
s.sort(key = operator.itemgetter(1))
e.sort(key = operator.itemgetter(2))
s_i = e_i = 0
w = 0
for i in range(n*2):
if e_i < len(e) and s[s_i][1] >= e[e_i][2]:
w -= e[e_i][0]
e_i += 1
else:
w += s[s_i][0]
s_i += 1
if w > 150:
print('NG')
break
if i == n*2-1 or s_i >= len(s):
print('OK')
break
```
| 89,159 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
while 1:
N = int(input())
if N == 0:
break
q = []
M = {}
for i in range(N):
m, a, b = map(int, input().split())
M[a] = M.get(a, 0) + m
M[b] = M.get(b, 0) - m
*q, = M.items()
q.sort()
ok = 1
cur = 0
for t, v in q:
cur += v
if cur > 150:
ok = 0
print("OK" if ok else "NG")
```
| 89,160 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
while True:
N = int(input())
if not N:
break
e = []
for i in range(N):
m, a, b = map(int, input().split())
e.append((a, m))
e.append((b, -m))
M = 0
ot = 0
for t, m in sorted(e):
if M > 150 and ot != t:
print('NG')
break
M += m
ot = t
else:
print('OK')
```
| 89,161 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
while True:
n = int(input())
if n == 0:
break
tlst = []
qlst = []
for _ in range(n):
m, a, b = map(int, input().split())
qlst.append((m, a, b))
tlst.append(a)
tlst.append(b)
tlst.append(b - 1)
tlst = sorted(list(set(tlst)))
tlst.sort()
tdic = {}
for i, t in enumerate(tlst):
tdic[t] = i
lent = len(tlst)
mp = [0] * lent
for m, a, b in qlst:
a, b = tdic[a], tdic[b]
mp[a] += m
mp[b] -= m
acc = 0
for i in range(lent):
acc += mp[i]
mp[i] = acc
if acc > 150:
print("NG")
break
else:
print("OK")
```
| 89,162 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Dangerous Bridge
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0231
"""
import sys
def solve(n):
events = []
for _ in range(n):
m, a, b = map(int, input().split())
events.append([a, m])
events.append([b, -m])
weight = 0
for t, m in sorted(events):
weight += m
if weight > 150:
return 'NG'
return 'OK'
def main(args):
while True:
n = int(input())
if n == 0:
break
ans = solve(n)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
```
| 89,163 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
# coding: utf-8
# Your code here!
class Sch:
def __init__(self, m, t):
self.m = m
self.t = t
def __lt__(self,other):
if self.t == other.t:
return self.m < other.m
else:
return self.t < other.t
while True:
N = int(input())
if N == 0:
break
schedule = []
for l in range(N):
m,a,b = [int(i) for i in input().split()]
schedule.append(Sch(m,a))
schedule.append(Sch(-m,b))
schedule.sort()
M = 0
ans = "OK"
for i in range(len(schedule)):
M = M + schedule[i].m
if M > 150:
ans = "NG"
break
print(ans)
```
| 89,164 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
"Correct Solution:
```
while True:
n = int(input())
if n == 0:
break
tlst = []
qlst = []
for _ in range(n):
m, a, b = map(int, input().split())
qlst.append((m, a, b))
tlst.append(a)
tlst.append(b)
tlst = sorted(list(set(tlst)))
tlst.sort()
tdic = {}
for i, t in enumerate(tlst):
tdic[t] = i
lent = len(tlst)
mp = [0] * lent
for m, a, b in qlst:
a, b = tdic[a], tdic[b]
mp[a] += m
mp[b] -= m
acc = 0
for i in range(lent):
acc += mp[i]
if acc > 150:
print("NG")
break
else:
print("OK")
```
| 89,165 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
# AOJ 0231: Dangerous Bridge
# Python3 2018.6.25 bal4u
# 時間昇順ソート
while True:
n = int(input())
if n == 0: break
tbl = []
for i in range(n):
m, a, b = map(int, input().split())
tbl.append((a, m))
tbl.append((b, -m))
tbl.sort()
sum, t, s = 0, -1, 0
for i in range(len(tbl)):
if t != tbl[i][0]:
if (sum + s) > 150: break
sum += s
t, s = tbl[i][0], tbl[i][1]
else: s += tbl[i][1]
print("OK" if sum+s <= 150 else "NG")
```
Yes
| 89,166 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
num = int(input())
if num == 0:
break
L = []
start = set()
for _ in range(num):
w, a, b = [int(x) for x in input().split()]
L.append([w,a,b])
start.add(a)
f = 0
for t in sorted(start):
wt = 0
for l in L:
if t >= l[1] and t < l[2]:
wt += l[0]
if wt > 150:
f += 1
if f > 0:
print("NG")
else:
print("OK")
```
Yes
| 89,167 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
n = int(input())
if n == 0:
break
tlst = []
qlst = []
for _ in range(n):
m, a, b = map(int, input().split())
qlst.append((m, a, b))
tlst.append(a)
tlst.append(b)
tlst.append(b - 1)
tlst = sorted(list(set(tlst)))
tlst.sort()
tdic = {}
for i, t in enumerate(tlst):
tdic[t] = i
lent = len(tlst)
mp = [0] * lent
for m, a, b in qlst:
a, b = tdic[a], tdic[b]
mp[a] += m
mp[b] -= m
acc = 0
for i in range(lent):
acc += mp[i]
if acc > 150:
print("NG")
break
else:
print("OK")
```
Yes
| 89,168 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
n = int(input())
if n == 0:
break
v = []
for _ in range(n):
m, a, b = map(int, input().split())
v.append((a,m))
v.append((b,-m))
v.sort()
s = 0
ans = 'OK'
for x in v:
s += x[1]
if s > 150:
ans = 'NG'
print(ans)
```
Yes
| 89,169 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
N = int(input())
if not N:
break
e = []
for i in range(N):
m, a, b = map(int, input().split())
e.append((a, m))
e.append((b, -m))
M = 0
ot = -1
for t, m in sorted(e):
M += m
if M >= 150 and ot != t:
print('NG')
break
ot = t
else:
print('OK')
```
No
| 89,170 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
N = int(input())
if not N:
break
e = []
for i in range(N):
m, a, b = map(int, input().split())
e.append((a, m))
e.append((b, -m))
M = 0
ot = -1
for t, m in sorted(e):
M += m
if M > 150 and ot != t:
print('NG')
break
ot = t
else:
print('OK')
```
No
| 89,171 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
from operator import itemgetter
while 1:
n = int(input())
if n == 0:
break
t_w = []
for _ in range(n):
m, a, b = map(int, input().split())
t_w.append((a, m, 5))
t_w.append((b, -m, 3))
t_w.sort(key=itemgetter(0, 2))
ans, cur = 'OK', 0
for t, w, pri in t_w:
cur += w
# print('t', t, 'w', w, 'cur', cur)
if cur > 150:
ans = 'NG'
break
print(ans)
```
No
| 89,172 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 ≤ n ≤ 100), weight of each passer mi (1 ≤ mi ≤ 100), time to start crossing the bridge ai, time to finish crossing bi (0 ≤ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an bn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while 1:
n = int(input())
if n == 0:
break
t_w = []
for _ in range(n):
m, a, b = map(int, input().split())
t_w.append((b, -m))
t_w.append((a, m))
t_w.sort(key=lambda x: x[0])
ans, cur = 'OK', 0
for t, w in t_w:
cur += w
# print('t', t, 'w', w, 'cur', cur)
if cur > 150:
ans = 'NG'
break
print(ans)
```
No
| 89,173 |
Provide a correct Python 3 solution for this coding contest problem.
Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of computers, she wants to create a beautiful sequence using only 0 and 1. She defines a "Beautiful" sequence of length $N$ that consists only of 0 and 1 if it includes $M$ successive array of 1s as its sub-sequence.
Using his skills in programming, Alice decided to calculate how many "Beautiful sequences" she can generate and compile a report on it.
Make a program to evaluate the possible number of "Beautiful sequences" given the sequence length $N$ and sub-sequence length $M$ that consists solely of 1. As the answer can be extremely large, divide it by $1,000,000,007 (= 10^9 + 7)$ and output the remainder.
Input
The input is given in the following format.
$N$ $M$
The input line provides the length of sequence $N$ ($1 \leq N \leq 10^5$) and the length $M$ ($1 \leq M \leq N$) of the array that solely consists of 1s.
Output
Output the number of Beautiful sequences in a line.
Examples
Input
4 3
Output
3
Input
4 2
Output
8
"Correct Solution:
```
MOD = 1000000007
N, M = (int(x) for x in input().split())
pow = [0] * (N + 1)
dp = [0] * (N + 1)
pow[0] = 1
for i in range(1, N + 1):
pow[i] = pow[i - 1] * 2
pow[i] %= MOD
dp[0] = 1
for i in range(1, M + 1):
dp[i] = pow[i]
dp[M] -= 1
for i in range(M + 1, N + 1):
dp[i] = dp[i - 1] + (dp[i - 1] - dp[i - 1 - M])
dp[i] %= MOD
print((pow[N] - dp[N] + MOD) % MOD)
```
| 89,174 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In 2215 A.D., a war between two planets, ACM and ICPC, is being more and more intense.
ACM introduced new combat planes. These planes have a special system that is called Graze, and fighting power of a plane increases when it is close to energy bullets that ICPC combat planes shoot.
Both combat planes and energy bullets have a shape of a sphere with a radius of R. Precisely, fighting power of a plane is equivalent to the number of energy bullet where distance from the plane is less than or equals to 2R.
You, helper of captain of intelligence units, are asked to analyze a war situation. The information given to you is coordinates of AN combat planes and BN energy bullets. Additionally, you know following things:
* All combat planes and energy bullet has same z-coordinates. In other word, z-coordinate can be ignored.
* No two combat planes, no two energy bullets, and no pair of combat plane and energy bullet collide (i.e. have positive common volume) each other.
Your task is to write a program that outputs total fighting power of all combat planes.
Constraints
* Jude data includes at most 20 data sets.
* 1 ≤ AN, BN ≤ 100000
* 0 < R ≤ 10
* 0 ≤ (coordinate values) < 10000
Input
Input file consists of a number of data sets. One data set is given in following format:
AN BN R
XA1 YA1
XA2 YA2
:
XAAN YAAN
XB1 YB1
XB2 YB2
:
XBBN YBBN
AN, BN, R are integers that describe the number of combat planes, energy bullets, and their radius respectively.
Following AN lines indicate coordinates of the center of combat planes. Each line has two integers that describe x-coordinate and y-coordinate.
Following BN lines indicate coordinates of the center of energy bullets, in the same format as that of combat planes.
Input ends when AN = BN = 0. You should output nothing for this case.
Output
For each data set, output the total fighting power.
Example
Input
2 2 1
0 0
0 4
2 2
2 8
0 0 0
Output
2
Submitted Solution:
```
# AOJ 1023: Amazing Graze
# Python3 2018.7.5 bal4u
import sys
from sys import stdin
input = stdin.readline
from bisect import bisect_left
while True:
AN, BN, R = map(int, input().split())
if AN == 0: break
e = [0]*AN
a = sorted([list(map(int, input().split())) for i in range(AN)])
aa = a[0:-1][0]
r = R << 2
r2 = r*r;
for i in range(BN):
x, y = map(int, input().split())
j = bisect_left(aa, x-r-1)
while j < AN and a[j][0] <= x+r:
if (a[j][0]-x)**2 + (a[j][1]-y)**2 <= r2: e[j] += 1
j += 1
print(sum(e))
```
No
| 89,175 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
'''
hardest 2.
Aizu Online Judge 1165: Pablo Squarson's Headache
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1165
## ?§£??¬
??£?????¢?????????????????£?????§???????????¢?????\????????????????????¢???????°????????????¨???????????????????????????????????§???
??£?????¢???????????¢???????????????????????????????????¨???????????¶???????????????
?´°????????¬?????????????????????????????§?????????????????\???????????????????????§?§£?????????
1. ?°´????????????x???,?????´?????????y?????¨?????????
2. ???????????£?????¢??????????????§?¨????(0, 0)??¨????????????????????£?????¢?????§?¨?????±???????
3. ????????¢???????????? (?????£?????¢???x??§?¨?????????§???) - (?????£?????¢???x??§?¨????????°????) + 1
4. ????????¢???????????????(?????£?????¢???y??§?¨?????????§???) - (?????£?????¢???y??§?¨????????°????) + 1
?????±???????????????????????°?????????????????????(AOJ?????????????????????????????????????????????????????¨????????????????????????)
'''
adjacent_vectors = [
(-1, 0), # left
(0, -1), # down
(1, 0), # right
(0, 1) # up
]
while True:
num_squares = int(input())
if num_squares == 0:
break
x_coordinates = [0] # x??§?¨?????????????
y_coordinates = [0] # y??§?¨?????????????
for _ in range(num_squares-1):
# adjacent_square: ??£??\????????£?????¢?????????
adjacent_square, direction = map(int, input().split())
x = x_coordinates[adjacent_square] # ??£??\????????£?????¢???x??§?¨?
y = y_coordinates[adjacent_square] # ??£??\????????£?????¢???y??§?¨?
(dx, dy) = adjacent_vectors[direction]
x_coordinates.append(x + dx)
y_coordinates.append(y + dy)
max_x = max(x_coordinates)
min_x = min(x_coordinates)
max_y = max(y_coordinates)
min_y = min(y_coordinates)
print(max_x - min_x + 1, max_y - min_y + 1)
```
| 89,176 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
def main():
while True:
sq_sum = int(input())
if sq_sum == 0: break
abs_list = [[0, 0]]
for i in range(sq_sum - 1):
n, d = map(int, input().split())
abs_list.append(abs_p(abs_list, n, d))
xlist = []
ylist = []
for i in range(len(abs_list)):
xlist.append(abs_list[i][0])
ylist.append(abs_list[i][1])
print(str(max(xlist) - min(xlist) + 1) + " " + str(max(ylist) - min(ylist) + 1))
return 0
def abs_p(abs_list, fromm, wch):
if wch == 0:return[abs_list[fromm][0] - 1, abs_list[fromm][1]]
if wch == 1:return[abs_list[fromm][0], abs_list[fromm][1] + 1]
if wch == 2:return[abs_list[fromm][0] + 1, abs_list[fromm][1]]
if wch == 3:return[abs_list[fromm][0], abs_list[fromm][1] - 1]
return 0
main()
```
| 89,177 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
def solve(N, nd):
dire = [(0,-1), (-1,0), (0,1), (1,0)]
table = [[0]*(2*N+1) for i in range(2*N+1)]
table[N][N] = 1
position = [None]*N
position[0] = (N, N)
for i,ndi in enumerate(nd,start=1):
n, d = ndi
y, x = position[n]
ny, nx = y+dire[d][0], x+dire[d][1]
table[ny][nx] = 1
position[i] = (ny, nx)
left, right, top, bottom = 2*N, 0, 2*N, 0
for i in range(2*N):
for j in range(2*N):
if (table[i][j]):
left = min(left, j)
right = max(right, j)
top = min(top, i)
bottom = max(bottom, i)
return (right-left+1, bottom-top+1)
def main():
ans = []
while True:
N = int(input())
if (not N):
break
nd = [list(map(int, input().split())) for i in range(N-1)]
ans.append(solve(N, nd))
for i in ans:
print(*i)
main()
```
| 89,178 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
D = ((-1,0),(0,1),(1,0),(0,-1))
while 1:
N = int(input())
if not N:break
min_x = min_y = max_x = max_y = 0
a = [(0,0)]
for i in range(N-1):
n1, d1 = list(map(int, input().split()))
spam = (lambda x: (x[0] + D[d1][0], x[1] + D[d1][1]))(a[n1])
min_x = min(spam[0],min_x)
min_y = min(spam[1],min_y)
max_x = max(spam[0],max_x)
max_y = max(spam[1],max_y)
a.append(spam)
print('%d %d' % (max_x - min_x + 1, max_y - min_y + 1))
```
| 89,179 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
while True:
n = int(input())
if n == 0: break
xs = [(0, 0)]
dic = [(-1, 0), (0, 1), (1, 0), (0, -1)]
maxX, minX, maxY, minY = 0, 0, 0, 0
for i in range(n - 1):
n, d = map(int, input().split())
xs.append((xs[n][0] + dic[d][0], xs[n][1] + dic[d][1]))
if xs[-1][0] > maxX: maxX = xs[-1][0]
elif xs[-1][0] < minX: minX = xs[-1][0]
if xs[-1][1] > maxY: maxY = xs[-1][1]
elif xs[-1][1] < minY: minY = xs[-1][1]
print(maxX - minX + 1, maxY - minY + 1)
```
| 89,180 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
d = [[-1,0],[0,1],[1,0],[0,-1]]
while True:
n = int(input())
if not(n): break
# l = [[1,1]]
w,h = [1],[1]
for i in range(n-1):
l = list(map(int,input().split()))
w.append(w[l[0]]+d[l[1]][0])
h.append(h[l[0]]+d[l[1]][1])
print(max(w)-min(w)+1,max(h)-min(h)+1)
```
| 89,181 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
def main():
D = ((-1,0), (0,-1), (1,0), (0,1))
while True:
n = int(input())
if n==0:
exit()
sx = [0]
sy = [0]
for i in range(n-1):
ni, di = [int(x) for x in input().split()]
sx.append(sx[ni]+D[di][0])
sy.append(sy[ni]+D[di][1])
w = max(sx)-min(sx)+1
h = max(sy)-min(sy)+1
print(w,h)
if __name__ == '__main__':
main()
```
| 89,182 |
Provide a correct Python 3 solution for this coding contest problem.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
"Correct Solution:
```
while True:
N=int(input())
if N==0:break
t=[0]
y=[0]
lst=list(range(N))
for k in lst[1:]:
[x,d]=input().split()
[x,d]=[int(x),int(d)]
if d==0:
t=t+[t[x]]
y=y+[y[x]-1]
if d==1:
t=t+[t[x]-1]
y=y+[y[x]]
if d==2:
t=t+[t[x]]
y=y+[y[x]+1]
if d==3:
t=t+[t[x]+1]
y=y+[y[x]]
ht=0
lt=0
hy=0
ly=0
for f in range(N):
if ht<t[f]:
ht+=1
if lt>t[f]:
lt-=1
if hy<y[f]:
hy+=1
if ly>y[f]:
ly-=1
print(hy-ly+1,ht-lt+1)
```
| 89,183 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
Submitted Solution:
```
while True:
N = int(input())
if N==0:
break
squa = [[0,0]]
xmax, xmin, ymax, ymin = 0, 0, 0, 0
for i in range(N-1):
n, d = map(int, input().split())
if d==0:
squa.append([squa[n][0]-1, squa[n][1]])
xmin = min(xmin, squa[n][0]-1)
elif d==1:
squa.append([squa[n][0], squa[n][1]-1])
ymin = min(ymin, squa[n][1]-1)
elif d==2:
squa.append([squa[n][0]+1, squa[n][1]])
xmax = max(xmax, squa[n][0]+1)
elif d==3:
squa.append([squa[n][0], squa[n][1]+1])
ymax = max(ymax, squa[n][1]+1)
print(xmax-xmin+1, ymax-ymin+1)
```
Yes
| 89,184 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
Submitted Solution:
```
if __name__ == "__main__":
while True:
N = int(input())
if N == 0:
break
# 左, 下, 右, 上
wak = {0: [0, 0]}
ml = float("inf")
md = float("inf")
mr = -float("inf")
mu = -float("inf")
for i in range(1, N):
n, d = map(int, input().split())
if d == 0:
wak[i] = [wak[n][0] - 1, wak[n][1]]
elif d == 1:
wak[i] = [wak[n][0], wak[n][1] - 1]
elif d == 2:
wak[i] = [wak[n][0] + 1, wak[n][1]]
elif d == 3:
wak[i] = [wak[n][0], wak[n][1] + 1]
for k, v in wak.items():
ml = min(v[0], ml)
md = min(v[1], md)
mr = max(v[0], mr)
mu = max(v[1], mu)
print(abs(mr - ml) + 1, abs(mu - md) + 1)
# print(wak)
# print("--------------------")
```
Yes
| 89,185 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
Submitted Solution:
```
while 1:
n=int(input())
if not n:break
p=[(0,0)]
b=[0,0]
for _ in range(n-1):
idx,d=map(int,input().split())
b=list(p[idx])
if d==0:
b[0]-=1
elif d==1:
b[1]-=1
elif d==2:
b[0]+=1
elif d==3:
b[1]+=1
p.append(tuple(b))
w=len({i[0] for i in p})
h=len({i[1] for i in p})
print(w,h)
```
Yes
| 89,186 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
Submitted Solution:
```
ans_list = []
while True:
N = int(input())
if N == 0:
break
square = [[0, 0]]
for i in range(N - 1):
n, d = map(int, input().split())
#print(n, d)
if d == 0:
square.append([square[n][0], square[n][1] - 1])
elif d == 2:
square.append([square[n][0], square[n][1] + 1])
elif d == 1:
square.append([square[n][0] - 1, square[n][1]])
elif d == 3:
#print(100, square, square[n - 1])
square.append([square[n][0] + 1, square[n][1]])
#print(square)
mini = 10 ** 10
minj = 10 ** 10
maxi = -10 ** 9
maxj = -10 ** 9
if N == 1:
ans_list.append([1, 1])
else:
for i in range(len(square)):
mini = min(square[i][0], mini)
minj = min(square[i][1], minj)
maxi = max(square[i][0], maxi)
maxj = max(square[i][1], maxj)
#print(maxi, maxj, mini, minj)
ans_list.append([maxj - minj + 1, maxi - mini + 1])
for i in ans_list:
print(i[0], i[1])
```
Yes
| 89,187 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
Submitted Solution:
```
while True:
n = int(input())
if n == 0 : break
w, h = 1, 1
edges = [[0], [0], [0], [0]] #[left, bottom, right, top]
for i in range(1, n):
p, d = map(int, input().split())
if d == 0 or d == 2:
if p in edges[d]:
w += 1
edges[d] = [i]
if p in edges[1]:
edges[1].append(i)
if p in edges[3]:
edges[3].append(i)
elif d == 1 or d == 3:
if p in edges[d]:
h += 1
edges[d] = [i]
if p in edges[0]:
edges[0].append(i)
if p in edges[2]:
edges[2].append(i)
#print(edges)
print(w,h)
```
No
| 89,188 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth.
At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision.
Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face.
Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him!
Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another.
I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates".
<image>
Input
The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows.
> N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares (= N) used to make the piece of artwork. The number is a positive integer and is smaller than 200.
The remaining (N-1) lines in the dataset are square placement instructions. The line "ni di" indicates placement of the square numbered i (≤ N-1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., (N-1). Note that the input does not give any placement instruction to the first square, which is numbered zero.
A square placement instruction for the square numbered i, namely "ni di", directs it to be placed next to the one that is numbered ni, towards the direction given by di, which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3).
For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers.
<image>
The end of the input is indicated by a line that contains a single zero.
Output
For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters.
Sample Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output for the Sample Input
1 1
3 3
4 4
5 6
Example
Input
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Output
1 1
3 3
4 4
5 6
Submitted Solution:
```
while True:
N=int(input())
if N==0:break
t=[0]
y=[0]
lst=list(range(N))
for k in lst[1:]:
[x,d]=input().split()
[x,d]=[int(x),int(d)]
if d==0:
t=t+[t[x]]
y=y+[y[x]-1]
if d==1:
t=t+[t[x]-1]
y=y+[y[x]]
if d==2:
t=t+[t[x]]
y=y+[y[x]+1]
if d==3:
t=t+[t[x]+1]
y=y+[y[x]]
ht=0
lt=0
hy=0
ly=0
for f in range(N):
if ht<t[f]:
ht+=1
if lt>t[f]:
lt-=1
if hy<y[f]:
hy+=1
if ly>y[f]:
ly-=1
print(ht-lt+1,hy-ly+1)
```
No
| 89,189 |
Provide a correct Python 3 solution for this coding contest problem.
"Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one after another, and the player manipulates a robot vehicle on the ground to capture the balloons. The player can control the vehicle to move left or right, or simply stay. When one of the balloons reaches the ground, the vehicle and the balloon must reside at the same position, otherwise the balloon will burst and the game ends.
<image>
Figure B.1: Robot vehicle and falling balloons
The goal of the game is to store all the balloons into the house at the left end on the game field. The vehicle can carry at most three balloons at a time, but its speed changes according to the number of the carrying balloons. When the vehicle carries k balloons (k = 0, 1, 2, 3), it takes k+1 units of time to move one unit distance. The player will get higher score when the total moving distance of the vehicle is shorter.
Your mission is to help the game designer check game data consisting of a set of balloons. Given a landing position (as the distance from the house) and a landing time of each balloon, you must judge whether a player can capture all the balloons, and answer the minimum moving distance needed to capture and store all the balloons. The vehicle starts from the house. If the player cannot capture all the balloons, you must identify the first balloon that the player cannot capture.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
n
p1 t1
.
.
.
pn tn
The first line contains an integer n, which represents the number of balloons (0 < n ≤ 40). Each of the following n lines contains two integers pi and ti (1 ≤ i ≤ n) separated by a space. pi and ti represent the position and the time when the i-th balloon reaches the ground (0 < pi ≤ 100, 0 < ti ≤ 50000). You can assume ti < tj for i < j. The position of the house is 0, and the game starts from the time 0.
The sizes of the vehicle, the house, and the balloons are small enough, and should be ignored. The vehicle needs 0 time for catching the balloons or storing them into the house. The vehicle can start moving immediately after these operations.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output one word and one integer in a line separated by a space. No extra characters should occur in the output.
* If the player can capture all the balloons, output "OK" and an integer that represents the minimum moving distance of the vehicle to capture and store all the balloons.
* If it is impossible for the player to capture all the balloons, output "NG" and an integer k such that the k-th balloon in the dataset is the first balloon that the player cannot capture.
Example
Input
2
10 100
100 270
2
10 100
100 280
3
100 150
10 360
40 450
3
100 150
10 360
40 440
2
100 10
50 200
2
100 100
50 110
1
15 10
4
1 10
2 20
3 100
90 200
0
Output
OK 220
OK 200
OK 260
OK 280
NG 1
NG 2
NG 1
OK 188
"Correct Solution:
```
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007
def solve(n):
b = LIR(n)
if b[0][0] > b[0][1]:
print("NG 1")
return
dp = [[float("inf") for j in range(4)] for i in range(n)]
dp[0][1] = b[0][0]
for i in range(n-1):
ni = i+1
x,t = b[i]
for a in range(4):
nx,nt = b[ni]
if a < 3:
na = a+1
T = t+na*abs(nx-x)
if T <= nt:
nd = dp[i][a]+abs(nx-x)
if nd < dp[ni][na]:
dp[ni][na] = nd
na = 1
T = t+(a+1)*x+nx
if T > nt:
continue
nd = dp[i][a]+nx+x
if nd < dp[ni][na]:
dp[ni][na] = nd
ans = float("inf")
for i in range(4):
ans = min(ans, dp[-1][i]+b[-1][0])
if ans == float("inf"):
for i in range(n):
if min(dp[i]) == float("inf"):
print("NG",i+1)
return
print("OK",ans)
return
#Solve
if __name__ == "__main__":
while 1:
n = I()
if n == 0:
break
solve(n)
```
| 89,190 |
Provide a correct Python 3 solution for this coding contest problem.
"Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one after another, and the player manipulates a robot vehicle on the ground to capture the balloons. The player can control the vehicle to move left or right, or simply stay. When one of the balloons reaches the ground, the vehicle and the balloon must reside at the same position, otherwise the balloon will burst and the game ends.
<image>
Figure B.1: Robot vehicle and falling balloons
The goal of the game is to store all the balloons into the house at the left end on the game field. The vehicle can carry at most three balloons at a time, but its speed changes according to the number of the carrying balloons. When the vehicle carries k balloons (k = 0, 1, 2, 3), it takes k+1 units of time to move one unit distance. The player will get higher score when the total moving distance of the vehicle is shorter.
Your mission is to help the game designer check game data consisting of a set of balloons. Given a landing position (as the distance from the house) and a landing time of each balloon, you must judge whether a player can capture all the balloons, and answer the minimum moving distance needed to capture and store all the balloons. The vehicle starts from the house. If the player cannot capture all the balloons, you must identify the first balloon that the player cannot capture.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
n
p1 t1
.
.
.
pn tn
The first line contains an integer n, which represents the number of balloons (0 < n ≤ 40). Each of the following n lines contains two integers pi and ti (1 ≤ i ≤ n) separated by a space. pi and ti represent the position and the time when the i-th balloon reaches the ground (0 < pi ≤ 100, 0 < ti ≤ 50000). You can assume ti < tj for i < j. The position of the house is 0, and the game starts from the time 0.
The sizes of the vehicle, the house, and the balloons are small enough, and should be ignored. The vehicle needs 0 time for catching the balloons or storing them into the house. The vehicle can start moving immediately after these operations.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output one word and one integer in a line separated by a space. No extra characters should occur in the output.
* If the player can capture all the balloons, output "OK" and an integer that represents the minimum moving distance of the vehicle to capture and store all the balloons.
* If it is impossible for the player to capture all the balloons, output "NG" and an integer k such that the k-th balloon in the dataset is the first balloon that the player cannot capture.
Example
Input
2
10 100
100 270
2
10 100
100 280
3
100 150
10 360
40 450
3
100 150
10 360
40 440
2
100 10
50 200
2
100 100
50 110
1
15 10
4
1 10
2 20
3 100
90 200
0
Output
OK 220
OK 200
OK 260
OK 280
NG 1
NG 2
NG 1
OK 188
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
rr = []
while True:
n = I()
if n == 0:
break
a = [LI() for _ in range(n)]
d = collections.defaultdict(lambda: inf)
d[0] = 0
cp = 0
ct = 0
r = inf
for i in range(n):
p,t = a[i]
e = collections.defaultdict(lambda: inf)
for k,ck in list(d.items()):
if k < 3 and abs(cp-p) * (k+1) + ct <= t:
if e[k+1] > ck + abs(cp-p):
e[k+1] = ck + abs(cp-p)
if cp * (k+1) + p + ct <= t:
if e[1] > ck + cp + p:
e[1] = ck + cp + p
d = e
if len(e) == 0:
r = i + 1
break
cp = p
ct = t
if r < inf:
rr.append('NG {}'.format(r))
else:
for k,ck in d.items():
if r > ck + cp:
r = ck + cp
rr.append('OK {}'.format(r))
return '\n'.join(map(str,rr))
print(main())
```
| 89,191 |
Provide a correct Python 3 solution for this coding contest problem.
"Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one after another, and the player manipulates a robot vehicle on the ground to capture the balloons. The player can control the vehicle to move left or right, or simply stay. When one of the balloons reaches the ground, the vehicle and the balloon must reside at the same position, otherwise the balloon will burst and the game ends.
<image>
Figure B.1: Robot vehicle and falling balloons
The goal of the game is to store all the balloons into the house at the left end on the game field. The vehicle can carry at most three balloons at a time, but its speed changes according to the number of the carrying balloons. When the vehicle carries k balloons (k = 0, 1, 2, 3), it takes k+1 units of time to move one unit distance. The player will get higher score when the total moving distance of the vehicle is shorter.
Your mission is to help the game designer check game data consisting of a set of balloons. Given a landing position (as the distance from the house) and a landing time of each balloon, you must judge whether a player can capture all the balloons, and answer the minimum moving distance needed to capture and store all the balloons. The vehicle starts from the house. If the player cannot capture all the balloons, you must identify the first balloon that the player cannot capture.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
n
p1 t1
.
.
.
pn tn
The first line contains an integer n, which represents the number of balloons (0 < n ≤ 40). Each of the following n lines contains two integers pi and ti (1 ≤ i ≤ n) separated by a space. pi and ti represent the position and the time when the i-th balloon reaches the ground (0 < pi ≤ 100, 0 < ti ≤ 50000). You can assume ti < tj for i < j. The position of the house is 0, and the game starts from the time 0.
The sizes of the vehicle, the house, and the balloons are small enough, and should be ignored. The vehicle needs 0 time for catching the balloons or storing them into the house. The vehicle can start moving immediately after these operations.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output one word and one integer in a line separated by a space. No extra characters should occur in the output.
* If the player can capture all the balloons, output "OK" and an integer that represents the minimum moving distance of the vehicle to capture and store all the balloons.
* If it is impossible for the player to capture all the balloons, output "NG" and an integer k such that the k-th balloon in the dataset is the first balloon that the player cannot capture.
Example
Input
2
10 100
100 270
2
10 100
100 280
3
100 150
10 360
40 450
3
100 150
10 360
40 440
2
100 10
50 200
2
100 100
50 110
1
15 10
4
1 10
2 20
3 100
90 200
0
Output
OK 220
OK 200
OK 260
OK 280
NG 1
NG 2
NG 1
OK 188
"Correct Solution:
```
inf = 10**9
while True:
n = int(input())
if n == 0:
break
b = [(0,0)]
b.extend([tuple(map(int,input().split())) for i in range(n)])
dp = [[inf]*4 for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
update = False
for j in range(4):
if dp[i][j] is inf:
continue
now = b[i][0]
nxt = b[i+1][0]
if j<=2 and b[i][1]+abs(nxt-now)*(j+1) <= b[i+1][1] and dp[i+1][j+1] > dp[i][j]+abs(nxt-now):
dp[i+1][j+1] = dp[i][j]+abs(nxt-now)
update = True
if b[i][1]+now*(j+1)+nxt <= b[i+1][1] and dp[i+1][1] > dp[i][j]+nxt+now:
dp[i+1][1] = dp[i][j]+nxt+now
update = True
if not update:
print('NG',i+1)
break
if update:
print('OK',min(dp[n])+b[n][0])
```
| 89,192 |
Provide a correct Python 3 solution for this coding contest problem.
"Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one after another, and the player manipulates a robot vehicle on the ground to capture the balloons. The player can control the vehicle to move left or right, or simply stay. When one of the balloons reaches the ground, the vehicle and the balloon must reside at the same position, otherwise the balloon will burst and the game ends.
<image>
Figure B.1: Robot vehicle and falling balloons
The goal of the game is to store all the balloons into the house at the left end on the game field. The vehicle can carry at most three balloons at a time, but its speed changes according to the number of the carrying balloons. When the vehicle carries k balloons (k = 0, 1, 2, 3), it takes k+1 units of time to move one unit distance. The player will get higher score when the total moving distance of the vehicle is shorter.
Your mission is to help the game designer check game data consisting of a set of balloons. Given a landing position (as the distance from the house) and a landing time of each balloon, you must judge whether a player can capture all the balloons, and answer the minimum moving distance needed to capture and store all the balloons. The vehicle starts from the house. If the player cannot capture all the balloons, you must identify the first balloon that the player cannot capture.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
n
p1 t1
.
.
.
pn tn
The first line contains an integer n, which represents the number of balloons (0 < n ≤ 40). Each of the following n lines contains two integers pi and ti (1 ≤ i ≤ n) separated by a space. pi and ti represent the position and the time when the i-th balloon reaches the ground (0 < pi ≤ 100, 0 < ti ≤ 50000). You can assume ti < tj for i < j. The position of the house is 0, and the game starts from the time 0.
The sizes of the vehicle, the house, and the balloons are small enough, and should be ignored. The vehicle needs 0 time for catching the balloons or storing them into the house. The vehicle can start moving immediately after these operations.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output one word and one integer in a line separated by a space. No extra characters should occur in the output.
* If the player can capture all the balloons, output "OK" and an integer that represents the minimum moving distance of the vehicle to capture and store all the balloons.
* If it is impossible for the player to capture all the balloons, output "NG" and an integer k such that the k-th balloon in the dataset is the first balloon that the player cannot capture.
Example
Input
2
10 100
100 270
2
10 100
100 280
3
100 150
10 360
40 450
3
100 150
10 360
40 440
2
100 10
50 200
2
100 100
50 110
1
15 10
4
1 10
2 20
3 100
90 200
0
Output
OK 220
OK 200
OK 260
OK 280
NG 1
NG 2
NG 1
OK 188
"Correct Solution:
```
# coding: utf-8
def do(b_num,step,now_p,item,t):
global ans,p,memo
if (b_num,step,now_p,item,t) in memo:
return
memo.add((b_num,step,now_p,item,t))
if item==0 and b_num>=n:
ans=min(ans,step)
return
if b_num<n and t+(item+1)*abs(balloons[b_num][0]-now_p)<=balloons[b_num][1] and item<=2:
do(b_num+1,step+abs(balloons[b_num][0]-now_p),balloons[b_num][0],item+1,balloons[b_num][1])
else:
p=max(p,b_num)
if now_p!=0:
do(b_num,step+now_p,0,0,t+(item+1)*now_p)
while True:
ans=99999999
p=-1
n=int(input())
if n==0:
break
balloons=[]
memo=set()
for i in range(n):
balloons.append(tuple(map(int,input().split())))
do(0,0,0,0,0)
if ans!=99999999:
print('OK',ans)
else:
print('NG',p+1)
```
| 89,193 |
Provide a correct Python 3 solution for this coding contest problem.
Taro had his own personal computer and set a password for login. However, Taro inadvertently forgot the password. Then, remembering that there was a piece of paper with the password written down, Taro found the paper and was surprised to see it. The paper was cut and there were only fragments, and there were some stains that made it unreadable. Taro decided to guess the password by referring to the memo.
Constraints
* The length of the character strings A and B is 1 to 1000 characters.
* The length of the B string does not exceed the length of the A string.
Input
String A
String B
Output
Output "Yes" or "No" on one line.
Examples
Input
ABCDE
ABC
Output
Yes
Input
KUSATSU
KSATSU
Output
No
Input
ABCABC
ACBA_B
Output
No
Input
RUPCUAPC
__PC
Output
Yes
Input
AIZU
_A
Output
No
"Correct Solution:
```
import re
A = input()
B = input()
B = B.replace('_', '.')
if re.search(B, A):
print('Yes')
else:
print('No')
```
| 89,194 |
Provide a correct Python 3 solution for this coding contest problem.
Taro had his own personal computer and set a password for login. However, Taro inadvertently forgot the password. Then, remembering that there was a piece of paper with the password written down, Taro found the paper and was surprised to see it. The paper was cut and there were only fragments, and there were some stains that made it unreadable. Taro decided to guess the password by referring to the memo.
Constraints
* The length of the character strings A and B is 1 to 1000 characters.
* The length of the B string does not exceed the length of the A string.
Input
String A
String B
Output
Output "Yes" or "No" on one line.
Examples
Input
ABCDE
ABC
Output
Yes
Input
KUSATSU
KSATSU
Output
No
Input
ABCABC
ACBA_B
Output
No
Input
RUPCUAPC
__PC
Output
Yes
Input
AIZU
_A
Output
No
"Correct Solution:
```
a = input()
b = input()
for i in range(len(a) - len(b) + 1):
f = True
for j in range(len(b)):
if a[i+j] == b[j]:
pass
else:
if b[j] == '_':
pass
else:
f = False
break
if f:
print('Yes')
exit()
print('No')
```
| 89,195 |
Provide a correct Python 3 solution for this coding contest problem.
Taro had his own personal computer and set a password for login. However, Taro inadvertently forgot the password. Then, remembering that there was a piece of paper with the password written down, Taro found the paper and was surprised to see it. The paper was cut and there were only fragments, and there were some stains that made it unreadable. Taro decided to guess the password by referring to the memo.
Constraints
* The length of the character strings A and B is 1 to 1000 characters.
* The length of the B string does not exceed the length of the A string.
Input
String A
String B
Output
Output "Yes" or "No" on one line.
Examples
Input
ABCDE
ABC
Output
Yes
Input
KUSATSU
KSATSU
Output
No
Input
ABCABC
ACBA_B
Output
No
Input
RUPCUAPC
__PC
Output
Yes
Input
AIZU
_A
Output
No
"Correct Solution:
```
a = input()
b = input()
length_a = len(a)
length_b = len(b)
def check():
for i in range(length_a - length_b + 1):
for j in range(length_b):
if b[j] == "_" or a[i + j] == b[j]:continue
else:break
else:
print("Yes")
return
print("No")
return
check()
```
| 89,196 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T:
* M v: (Mark) Mark node v.
* Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself.
Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence.
Input
The input consists of multiple datasets. Each dataset has the following format:
The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000.
The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node.
The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node.
The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print the sum of the outputs of all query operations in one line.
Example
Input
6 3
1
1
2
3
3
Q 5
M 3
Q 5
0 0
Output
4
"Correct Solution:
```
def solve():
import sys
file_input = sys.stdin
while True:
N, Q = map(int, file_input.readline().split())
if N == 0:
break
parent = [None, None]
parent += [int(file_input.readline()) for _ in range(N - 1)]
unmarked = [True] * (N + 1)
unmarked[1] = False
ans = 0
for _ in range(Q):
line = file_input.readline()
v = int(line[2:])
if line[0] == 'M':
unmarked[v] = False
else:
while unmarked[v]:
v = parent[v]
ans += v
print(ans)
solve()
```
| 89,197 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T:
* M v: (Mark) Mark node v.
* Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself.
Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence.
Input
The input consists of multiple datasets. Each dataset has the following format:
The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000.
The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node.
The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node.
The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print the sum of the outputs of all query operations in one line.
Example
Input
6 3
1
1
2
3
3
Q 5
M 3
Q 5
0 0
Output
4
"Correct Solution:
```
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15
class UnionFind():
def __init__(self,n):
self.n = n
self.parents = [-1]*n
def find(self,x): #根を見つける、繋ぎ直す
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def unite(self,x,y):
y = self.find(y)
x = self.find(x)
self.parents[y] += self.parents[x]
self.parents[x] = y
def same(self,x,y):#xとyが同じグループにいるか判定
return self.find(x) == self.find(y)
def solve(N,Q):
parents = [-1] * N
for i in range(1,N):
p = int(input()) - 1
parents[i] = p
query = []
mark = [0] * N
mark[0] = 1
for _ in range(Q):
q = input().split()
v = int(q[1]) - 1
if q[0] == 'M':
mark[v] += 1
query.append((0,v))
else:
query.append((1,v))
uf = UnionFind(N)
for i in range(N):
if mark[i] == 0:
uf.unite(i,parents[i])
ans = 0
for ty,v in query[::-1]:
if ty == 0:
mark[v] -= 1
if mark[v] == 0:
uf.unite(v,parents[v])
else:
ans += uf.find(v) + 1
print(ans)
def main():
while True:
N,M = map(int,input().split())
if N == 0:
return
solve(N,M)
if __name__ == '__main__':
main()
```
| 89,198 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T:
* M v: (Mark) Mark node v.
* Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root node is marked. Note that a node is an ancestor of itself.
Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence.
Input
The input consists of multiple datasets. Each dataset has the following format:
The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree T and the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000.
The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node.
The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node.
The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print the sum of the outputs of all query operations in one line.
Example
Input
6 3
1
1
2
3
3
Q 5
M 3
Q 5
0 0
Output
4
"Correct Solution:
```
def root(x):
while P[x-1]!=x:
x = P[x-1]
return x
def mark(x):
P[x-1] = x
while True:
n,q = map(int,input().split())
res = 0
if n==0:
break
P = [1]
for _ in range(n-1):
P.append(int(input()))
for _ in range(q):
com,num=map(str,input().split())
if com == "Q":
res += root(int(num))
else:
mark(int(num))
print(res)
```
| 89,199 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.