message stringlengths 2 45.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 254 108k | cluster float64 3 3 | __index_level_0__ int64 508 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1. | instruction | 0 | 33,521 | 3 | 67,042 |
Tags: binary search, constructive algorithms, greedy, implementation, math
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
m = 1
c = 1
d = 0
l = []
while m != n:
if 2 * c + m >= n:
l.append(n - (m + c))
d += 1
break
l.append(min(c, (n - m - 2 * c) // 2))
c += min(c, (n - m - 2 * c) // 2)
m += c
d += 1
print(d)
print(*l)
``` | output | 1 | 33,521 | 3 | 67,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1. | instruction | 0 | 33,522 | 3 | 67,044 |
Tags: binary search, constructive algorithms, greedy, implementation, math
Correct Solution:
```
# Aim for the moon. If you miss, you may hit a star. W. Clement Stone
# by : Blue Edge - Create some chaos
def log(n:int) -> int:
ans=-1
while n:
ans+=1
n>>=1
return ans
for _ in range(int(input())):
n=int(input())
a=[1]
b=2
s=1
while b<=n-s:
a.append(b)
s+=b
b<<=1
if n-s:
a.append(n-s)
if a[-2]>a[-1]:
z=a[-2]+a[-1]
a[-2]=z//2
a[-1]=z-(z//2)
m=log(n)+1
c=[]
for i in range(1,m):
c.append(a[i]-a[i-1])
print(m-1)
print(*c)
# print(a)
``` | output | 1 | 33,522 | 3 | 67,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1. | instruction | 0 | 33,523 | 3 | 67,046 |
Tags: binary search, constructive algorithms, greedy, implementation, math
Correct Solution:
```
from sys import stdin
input = stdin.readline
mod = int(1e9 + 7)
def put():
return map(int, input().split())
def fail():
print(-1)
exit()
def seive(N):
factor = []
x = N
i = 2
while i**2 <= N:
if x%i==0:
factor.append(i)
while x%i==0:
x//=i
i+=1
if x>1:
factor.append(x)
return factor
for _ in range(int(input())):
n = int(input())
i,l = 1,[]
while i<=n:
n -= i
l.append(i)
i *= 2
if n:
l.append(n)
l.sort()
print(len(l) - 1)
for i in range(1,len(l)):
print(l[i] - l[i-1], end=' ')
print()
``` | output | 1 | 33,523 | 3 | 67,047 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
from collections import deque
def solve():
n=int(input())
mass,d=0,1
summ=deque()
while mass<n:
mass+=2**(d-1)
summ.append(2**(d-1))
d+=1
if mass==n:
print(d-2)
for i in range(d-2):
print(summ[i],end=' ')
elif mass>n:
diff=mass-n
j=d-3
while diff>summ[j]:
summ[j+1]=summ[j]
diff=diff-summ[j]
j-=1
if diff<=summ[j]:
maxx=2*summ[j]
minn=summ[j]
if maxx-diff>=minn:
summ[j+1]=maxx-diff
print(d-2)
for i in range(1,d-1):
print(summ[i]-summ[i-1],end=' ')
print()
t=int(input())
for i in range(t):
solve()
``` | instruction | 0 | 33,524 | 3 | 67,048 |
Yes | output | 1 | 33,524 | 3 | 67,049 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
# -*- coding: utf-8 -*-
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
N = INT()
C = []
k = 0
sm = 0
while sm + 2**k <= N:
sm += 2**k
C.append(2**k)
k += 1
if sm < N:
C.append(N-sm)
C.sort()
ans = []
for i in range(len(C)-1):
ans.append(C[i+1] - C[i])
print(len(ans))
print(*ans)
``` | instruction | 0 | 33,525 | 3 | 67,050 |
Yes | output | 1 | 33,525 | 3 | 67,051 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
import sys
input=sys.stdin.readline
t = int(input())
for _ in range(t):
i=1
n=int(input())
ans=[]
while i<=n:
ans.append(i)
n-=i
i*=2
if n>0:
ans.append(n)
ans.sort()
print(len(ans)-1)
for i in range(1,len(ans)):
print(ans[i]-ans[i-1], end=' ')
print()
``` | instruction | 0 | 33,526 | 3 | 67,052 |
Yes | output | 1 | 33,526 | 3 | 67,053 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
import math
for ii in range(int(input())):
p = int(input())
ans = [1]
sum = 1
while sum<= p :
sum += 2*ans[-1]
ans.append(2*ans[-1])
sum -= ans[-1]
ans.pop()
diff = p - sum
#print(diff,ans)
x = 0
for i in range(len(ans)-1) :
if ans[i] <= diff and ans[i+1] > diff :
x = i
break
if diff >= ans[-1] :ans.append(diff)
elif diff == 0 : pass
else: ans = ans[0:x+1] + [diff] + ans[x+1:]
result = []
for i in range(len(ans)-1):
result.append(ans[i+1]-ans[i])
print(len(result))
print(*result)
``` | instruction | 0 | 33,527 | 3 | 67,054 |
Yes | output | 1 | 33,527 | 3 | 67,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input()) - 1
answer = []
cur = 1
while n - cur * 2 >= cur * 2:
answer.append(cur)
n -= cur * 2
cur *= 2
if n == cur:
answer.append(0)
else:
if n <= cur * 2:
answer.append(n - cur)
else:
if n % 2 == 0:
answer += [(n - 2 * cur) // 2, 0]
else:
answer += [0, n - 2 * cur]
print(len(answer))
print(*answer)
``` | instruction | 0 | 33,528 | 3 | 67,056 |
No | output | 1 | 33,528 | 3 | 67,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
t=int(input())
for i in range(t):
n=int(input())
b = list(bin(n-1))[2:]
if(n==2):
print("1\n0")
continue
# print(b)
ct=[int(b[i]) for i in range(len(b))]
check = False
for j in range(len(b)-2,-1,-1):
if(b[j]=='0'):
ct[j]+=1
if(not check):
ct[j]+=1
check=True
else:
if(check):
ct[j]-=1
check=False
val = 2
kitne = len(ct)-ct.count(0)
print(kitne)
for j in range(len(b)-2,-1,-1):
if(ct[j]==0):
break
print(val//2,end=" ")
for k in range(ct[j]-1):
print(0,end=" ")
val*=2
print()
``` | instruction | 0 | 33,529 | 3 | 67,058 |
No | output | 1 | 33,529 | 3 | 67,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
def sol():
import math
n=int(input())
d=int(math.log2(n))-1
res=[x for x in range(1,d+1)]
c=1+n-2**int(math.log2(n))
while c>0:
res.insert(int(math.log2(c)), 0)
c=c-2**int(math.log2(c))
d+=1
print (d)
print (str(res).strip('[]'))
case=int(input())
for i in range(1,case+1):
sol()
``` | instruction | 0 | 33,530 | 3 | 67,060 |
No | output | 1 | 33,530 | 3 | 67,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
T = int(input())
for case in range(T):
n = int(input())
o = 0
arr = []
while 2 ** o - 1 < n:
arr.append(2 ** o)
o += 1
diff = 2 ** o -1 - n
#print(arr)
#print("min",o,"diff",diff)
b = str(bin(diff))[2:][::-1]
#print(b)
o2 = 0
arr2 = []
while o2 < len(b):
if b[o2] == "0":
arr2.append(0)
else:
arr2.append(2 ** o2)
o2 += 1
#print(arr,arr2)
for i in range(1,len(arr2)+1):
arr[i] -= arr2[i-1]
#print(arr)
ret = []
for i in range(len(arr)-1):
ret.append(str(arr[i+1]-arr[i]))
days = len(ret)
ret = " ".join(ret)
print(days)
print(ret)
``` | instruction | 0 | 33,531 | 3 | 67,062 |
No | output | 1 | 33,531 | 3 | 67,063 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5.
Also, every night, the mass of every bacteria will increase by one.
Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains an integer n (2 β€ n β€ 10^9) β the sum of bacteria masses that Phoenix is interested in.
Output
For each test case, if there is no way for the bacteria to exactly achieve total mass n, print -1. Otherwise, print two lines.
The first line should contain an integer d β the minimum number of nights needed.
The next line should contain d integers, with the i-th integer representing the number of bacteria that should split on the i-th day.
If there are multiple solutions, print any.
Example
Input
3
9
11
2
Output
3
1 0 2
3
1 1 2
1
0
Note
In the first test case, the following process results in bacteria with total mass 9:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5 each.
* Night 1: All bacteria's mass increases by one. There are now two bacteria with mass 1.5.
* Day 2: None split.
* Night 2: There are now two bacteria with mass 2.5.
* Day 3: Both bacteria split. There are now four bacteria with mass 1.25.
* Night 3: There are now four bacteria with mass 2.25.
The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.
In the second test case, the following process results in bacteria with total mass 11:
* Day 1: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5.
* Night 1: There are now two bacteria with mass 1.5.
* Day 2: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5.
* Night 2: There are now three bacteria with masses 1.75, 1.75, and 2.5.
* Day 3: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75.
* Night 3: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75.
The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.
In the third test case, the bacterium does not split on day 1, and then grows to mass 2 during night 1.
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return stdin.read().split()
range = xrange # not for python 3.0+
for t in range(input()):
n=input()
i=0
while ((1<<i)-1)<n:
i+=1
c=i-1
n-=((1<<c)-1)
ans=[]
if n%2:
ans.append(0)
if c>1:
ans.append(1)
for i in range(1,c-1):
if (1<<i)&n:
ans.append(0)
ans.append(1<<i)
if c>1 and n&(1<<(c-1)):
#print n,1<<c
ans.append(0)
print len(ans)
pr_arr(ans)
``` | instruction | 0 | 33,532 | 3 | 67,064 |
No | output | 1 | 33,532 | 3 | 67,065 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is now 125 years later, but humanity is still on the run from a humanoid-cyborg race determined to destroy it. Or perhaps we are getting some stories mixed up here... In any case, the fleet is now smaller. However, in a recent upgrade, all the navigation systems have been outfitted with higher-dimensional, linear-algebraic jump processors.
Now, in order to make a jump, a ship's captain needs to specify a subspace of the d-dimensional space in which the events are taking place. She does so by providing a generating set of vectors for that subspace.
Princess Heidi has received such a set from the captain of each of m ships. Again, she would like to group up those ships whose hyperspace jump subspaces are equal. To do so, she wants to assign a group number between 1 and m to each of the ships, so that two ships have the same group number if and only if their corresponding subspaces are equal (even though they might be given using different sets of vectors).
Help Heidi!
Input
The first line of the input contains two space-separated integers m and d (2 β€ m β€ 30 000, 1 β€ d β€ 5) β the number of ships and the dimension of the full underlying vector space, respectively. Next, the m subspaces are described, one after another. The i-th subspace, which corresponds to the i-th ship, is described as follows:
The first line contains one integer ki (1 β€ ki β€ d). Then ki lines follow, the j-th of them describing the j-th vector sent by the i-th ship. Each of the j lines consists of d space-separated integers aj, j = 1, ..., d, that describe the vector <image>; it holds that |aj| β€ 250. The i-th subspace is the linear span of these ki vectors.
Output
Output m space-separated integers g1, ..., gm, where <image> denotes the group number assigned to the i-th ship. That is, for any 1 β€ i < j β€ m, the following should hold: gi = gj if and only if the i-th and the j-th subspaces are equal. In addition, the sequence (g1, g2, ..., gm) should be lexicographically minimal among all sequences with that property.
Example
Input
8 2
1
5 0
1
0 1
1
0 1
2
0 6
0 1
2
0 1
1 0
2
-5 -5
4 3
2
1 1
0 1
2
1 0
1 0
Output
1 2 2 2 3 3 3 1
Note
In the sample testcase, the first and the last subspace are equal, subspaces 2 to 4 are equal, and subspaces 5 to 7 are equal.
Recall that two subspaces, one given as the span of vectors <image> and another given as the span of vectors <image>, are equal if each vector vi can be written as a linear combination of vectors w1, ..., wk (that is, there exist coefficients <image> such that vi = Ξ±1w1 + ... + Ξ±kwk) and, similarly, each vector wi can be written as a linear combination of vectors v1, ..., vn.
Recall that a sequence (g1, g2, ..., gm) is lexicographically smaller than a sequence (h1, h2, ..., hm) if there exists an index i, 1 β€ i β€ m, such that gi < hi and gj = hj for all j < i.
Submitted Solution:
```
from fractions import *
m,d = [int(x) for x in input().split(' ')]
v = {}
cur = 0
for i in range(m):
k = int(input())
a = []
for j in range(k):
g = [Fraction(int(x)) for x in input().split(' ')]
a.append(g)
n = len(a)
m = len(a[0])
c = 0
r = 0
while c < m and r < n:
j = r
for i in range(r+1, n):
if abs(a[i][c]) > abs(a[j][c]):
j = i
if a[j][c] == 0:
c = c + 1
continue
for i in range(m):
a[j][i], a[r][i] = a[r][i], a[j][i]
s = 1 / a[r][c]
for j in range(m):
a[r][j] *= s
for i in range(n):
if i != r:
t = a[i][c]
for j in range(m):
a[i][j] -= t * a[r][j]
r = r + 1
c = c + 1
z = []
for x in a[:r]:
z.append(tuple(x))
b = hash(tuple(z))
if b not in v:
cur = cur + 1
v[b] = cur
print(v[b], end=' ')
``` | instruction | 0 | 33,984 | 3 | 67,968 |
No | output | 1 | 33,984 | 3 | 67,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It is now 125 years later, but humanity is still on the run from a humanoid-cyborg race determined to destroy it. Or perhaps we are getting some stories mixed up here... In any case, the fleet is now smaller. However, in a recent upgrade, all the navigation systems have been outfitted with higher-dimensional, linear-algebraic jump processors.
Now, in order to make a jump, a ship's captain needs to specify a subspace of the d-dimensional space in which the events are taking place. She does so by providing a generating set of vectors for that subspace.
Princess Heidi has received such a set from the captain of each of m ships. Again, she would like to group up those ships whose hyperspace jump subspaces are equal. To do so, she wants to assign a group number between 1 and m to each of the ships, so that two ships have the same group number if and only if their corresponding subspaces are equal (even though they might be given using different sets of vectors).
Help Heidi!
Input
The first line of the input contains two space-separated integers m and d (2 β€ m β€ 30 000, 1 β€ d β€ 5) β the number of ships and the dimension of the full underlying vector space, respectively. Next, the m subspaces are described, one after another. The i-th subspace, which corresponds to the i-th ship, is described as follows:
The first line contains one integer ki (1 β€ ki β€ d). Then ki lines follow, the j-th of them describing the j-th vector sent by the i-th ship. Each of the j lines consists of d space-separated integers aj, j = 1, ..., d, that describe the vector <image>; it holds that |aj| β€ 250. The i-th subspace is the linear span of these ki vectors.
Output
Output m space-separated integers g1, ..., gm, where <image> denotes the group number assigned to the i-th ship. That is, for any 1 β€ i < j β€ m, the following should hold: gi = gj if and only if the i-th and the j-th subspaces are equal. In addition, the sequence (g1, g2, ..., gm) should be lexicographically minimal among all sequences with that property.
Example
Input
8 2
1
5 0
1
0 1
1
0 1
2
0 6
0 1
2
0 1
1 0
2
-5 -5
4 3
2
1 1
0 1
2
1 0
1 0
Output
1 2 2 2 3 3 3 1
Note
In the sample testcase, the first and the last subspace are equal, subspaces 2 to 4 are equal, and subspaces 5 to 7 are equal.
Recall that two subspaces, one given as the span of vectors <image> and another given as the span of vectors <image>, are equal if each vector vi can be written as a linear combination of vectors w1, ..., wk (that is, there exist coefficients <image> such that vi = Ξ±1w1 + ... + Ξ±kwk) and, similarly, each vector wi can be written as a linear combination of vectors v1, ..., vn.
Recall that a sequence (g1, g2, ..., gm) is lexicographically smaller than a sequence (h1, h2, ..., hm) if there exists an index i, 1 β€ i β€ m, such that gi < hi and gj = hj for all j < i.
Submitted Solution:
```
from fractions import *
m,d = [int(x) for x in input().split(' ')]
v = {}
cur = 0
for i in range(m):
k = int(input())
a = []
for j in range(k):
g = [Fraction(int(x)) for x in input().split(' ')]
a.append(g)
n = len(a)
m = len(a[0])
c = 0
r = 0
while c < m and r < n:
j = r
for i in range(r+1, n):
if abs(a[i][c]) > abs(a[j][c]):
j = i
if a[j][c] == 0:
c = c + 1
continue
for i in range(m):
a[j][i], a[r][i] = a[r][i], a[j][i]
s = 1 / a[r][c]
for j in range(m):
a[r][j] *= s
for i in range(n):
if i != r:
t = a[i][c]
for j in range(m):
a[i][j] -= t * a[r][j]
r = r + 1
c = c + 1
z = []
for x in a[:r]:
z += x
b = hash(tuple(z))
if b not in v:
cur = cur + 1
v[b] = cur
print(v[b], end=' ')
``` | instruction | 0 | 33,985 | 3 | 67,970 |
No | output | 1 | 33,985 | 3 | 67,971 |
Provide a correct Python 3 solution for this coding contest problem.
Suppose that there are some light sources and many spherical balloons. All light sources have sizes small enough to be modeled as point light sources, and they emit light in all directions. The surfaces of the balloons absorb light and do not reflect light. Surprisingly in this world, balloons may overlap.
You want the total illumination intensity at an objective point as high as possible. For this purpose, some of the balloons obstructing lights can be removed. Because of the removal costs, however, there is a certain limit on the number of balloons to be removed. Thus, you would like to remove an appropriate set of balloons so as to maximize the illumination intensity at the objective point.
The following figure illustrates the configuration specified in the first dataset of the sample input given below. The figure shows the xy-plane, which is enough because, in this dataset, the z-coordinates of all the light sources, balloon centers, and the objective point are zero. In the figure, light sources are shown as stars and balloons as circles. The objective point is at the origin, and you may remove up to 4 balloons. In this case, the dashed circles in the figure correspond to the balloons to be removed.
<image>
Figure G.1: First dataset of the sample input.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
N M R
S1x S1y S1z S1r
...
SNx SNy SNz SNr
T1x T1y T1z T1b
...
TMx TMy TMz TMb
Ex Ey Ez
The first line of a dataset contains three positive integers, N, M and R, separated by a single space. N means the number of balloons that does not exceed 2000. M means the number of light sources that does not exceed 15. R means the number of balloons that may be removed, which does not exceed N.
Each of the N lines following the first line contains four integers separated by a single space. (Six, Siy, Siz) means the center position of the i-th balloon and Sir means its radius.
Each of the following M lines contains four integers separated by a single space. (Tjx, Tjy, Tjz) means the position of the j-th light source and Tjb means its brightness.
The last line of a dataset contains three integers separated by a single space. (Ex, Ey, Ez) means the position of the objective point.
Six, Siy, Siz, Tjx, Tjy, Tjz, Ex, Ey and Ez are greater than -500, and less than 500. Sir is greater than 0, and less than 500. Tjb is greater than 0, and less than 80000.
At the objective point, the intensity of the light from the j-th light source is in inverse proportion to the square of the distance, namely
Tjb / { (Tjx β Ex)2 + (Tjy β Ey)2 + (Tjz β Ez)2 },
if there is no balloon interrupting the light. The total illumination intensity is the sum of the above.
You may assume the following.
1. The distance between the objective point and any light source is not less than 1.
2. For every i and j, even if Sir changes by Ξ΅ (|Ξ΅| < 0.01), whether the i-th balloon hides the j-th light or not does not change.
The end of the input is indicated by a line of three zeros.
Output
For each dataset, output a line containing a decimal fraction which means the highest possible illumination intensity at the objective point after removing R balloons. The output should not contain an error greater than 0.0001.
Example
Input
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 240
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 260
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
5 1 3
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
5 1 2
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
0 0 0
Output
3.5
3.6
1.1666666666666667
0.0 | instruction | 0 | 34,202 | 3 | 68,404 |
"Correct Solution:
```
from itertools import product
from sys import stdin, stdout
readline = stdin.readline
write = stdout.write
def dist2(x0, y0, z0, x1, y1, z1):
return (x0 - x1)**2 + (y0 - y1)**2 + (z0 - z1)**2
def dot(x0, y0, z0, x1, y1, z1):
return x0*x1 + y0*y1 + z0*z1
def cross2(x0, y0, z0, x1, y1, z1):
return (y0*z1 - y1*z0)**2 + (z0*x1 - z1*x0)**2 + (x0*y1 - x1*y0)**2
while 1:
N, M, R = map(int, readline().split())
if N == M == R == 0:
break
S = [list(map(int, readline().split())) for i in range(N)]
T = [list(map(int, readline().split())) for i in range(M)]
ex, ey, ez = map(int, readline().split())
S = [(sx - ex, sy - ey, sz - ez, sr) for sx, sy, sz, sr in S]
T = [(tx - ex, ty - ey, tz - ez, tb) for tx, ty, tz, tb in T]
L = [tb / (tx**2 + ty**2 + tz**2) for tx, ty, tz, tb in T]
rem = [0]*M
for i in range(M):
tx, ty, tz, tb = T[i]
ld = tx**2 + ty**2 + tz**2
for j in range(N):
sx, sy, sz, sr = S[j]
sr2 = sr**2
ok = 1
dd1 = (sx**2 + sy**2 + sz**2 <= sr2)
dd2 = (dist2(sx, sy, sz, tx, ty, tz) <= sr2)
if dd1 ^ dd2:
ok = 0
elif dd1 == dd2 == 0:
if (cross2(sx, sy, sz, tx, ty, tz) <= sr2 * ld):
if (dot(sx, sy, sz, tx, ty, tz) >= 0 and
dot(tx-sx, ty-sy, tz-sz, tx, ty, tz) >= 0):
ok = 0
if not ok:
rem[i] |= 1 << j
ans = 0
for P in product([0, 1], repeat=M):
need = 0
for i in range(M):
if P[i]: need |= rem[i]
if bin(need).count('1') <= R:
ans = max(ans, sum(L[i] for i in range(M) if P[i]))
write("%.10f\n" % ans)
``` | output | 1 | 34,202 | 3 | 68,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose that there are some light sources and many spherical balloons. All light sources have sizes small enough to be modeled as point light sources, and they emit light in all directions. The surfaces of the balloons absorb light and do not reflect light. Surprisingly in this world, balloons may overlap.
You want the total illumination intensity at an objective point as high as possible. For this purpose, some of the balloons obstructing lights can be removed. Because of the removal costs, however, there is a certain limit on the number of balloons to be removed. Thus, you would like to remove an appropriate set of balloons so as to maximize the illumination intensity at the objective point.
The following figure illustrates the configuration specified in the first dataset of the sample input given below. The figure shows the xy-plane, which is enough because, in this dataset, the z-coordinates of all the light sources, balloon centers, and the objective point are zero. In the figure, light sources are shown as stars and balloons as circles. The objective point is at the origin, and you may remove up to 4 balloons. In this case, the dashed circles in the figure correspond to the balloons to be removed.
<image>
Figure G.1: First dataset of the sample input.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
N M R
S1x S1y S1z S1r
...
SNx SNy SNz SNr
T1x T1y T1z T1b
...
TMx TMy TMz TMb
Ex Ey Ez
The first line of a dataset contains three positive integers, N, M and R, separated by a single space. N means the number of balloons that does not exceed 2000. M means the number of light sources that does not exceed 15. R means the number of balloons that may be removed, which does not exceed N.
Each of the N lines following the first line contains four integers separated by a single space. (Six, Siy, Siz) means the center position of the i-th balloon and Sir means its radius.
Each of the following M lines contains four integers separated by a single space. (Tjx, Tjy, Tjz) means the position of the j-th light source and Tjb means its brightness.
The last line of a dataset contains three integers separated by a single space. (Ex, Ey, Ez) means the position of the objective point.
Six, Siy, Siz, Tjx, Tjy, Tjz, Ex, Ey and Ez are greater than -500, and less than 500. Sir is greater than 0, and less than 500. Tjb is greater than 0, and less than 80000.
At the objective point, the intensity of the light from the j-th light source is in inverse proportion to the square of the distance, namely
Tjb / { (Tjx β Ex)2 + (Tjy β Ey)2 + (Tjz β Ez)2 },
if there is no balloon interrupting the light. The total illumination intensity is the sum of the above.
You may assume the following.
1. The distance between the objective point and any light source is not less than 1.
2. For every i and j, even if Sir changes by Ξ΅ (|Ξ΅| < 0.01), whether the i-th balloon hides the j-th light or not does not change.
The end of the input is indicated by a line of three zeros.
Output
For each dataset, output a line containing a decimal fraction which means the highest possible illumination intensity at the objective point after removing R balloons. The output should not contain an error greater than 0.0001.
Example
Input
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 240
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 260
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
5 1 3
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
5 1 2
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
0 0 0
Output
3.5
3.6
1.1666666666666667
0.0
Submitted Solution:
```
from itertools import product
def dist2(x0, y0, z0, x1, y1, z1):
return (x0 - x1)**2 + (y0 - y1)**2 + (z0 - z1)**2
def dot(x0, y0, z0, x1, y1, z1):
return x0*x1 + y0*y1 + z0*z1
def cross2(x0, y0, z0, x1, y1, z1):
return (y0*z1 - y1*z0)**2 + (z0*x1 - z1*x0)**2 + (x0*y1 - x1*y0)**2
while 1:
N, M, R = map(int, input().split())
if N == M == R == 0:
break
S = [list(map(int, input().split())) for i in range(N)]
T = [list(map(int, input().split())) for i in range(M)]
ex, ey, ez = map(int, input().split())
rem = [set() for i in range(M)]
for i in range(M):
tx, ty, tz, tb = T[i]
r = rem[i]
for j in range(N):
sx, sy, sz, sr = S[j]
ok = 1
dd1 = (dist2(sx, sy, sz, ex, ey, ez) <= sr**2)
dd2 = (dist2(sx, sy, sz, tx, ty, tz) <= sr**2)
if dd1 ^ dd2:
ok = 0
if dd1 == dd2 == 0:
if (cross2(sx - ex, sy - ey, sz - ez, tx - ex, ty - ey, tz - ez)
<= sr**2 * dist2(ex, ey, ez, tx, ty, tz)):
if (dot(sx - ex, sy - ey, sz - ez, tx - ex, ty - ey, tz - ez) >= 0 and
dot(sx - tx, sy - ty, sz - tz, ex - tx, ey - ty, ez - tz) >= 0):
ok = 0
if not ok:
r.add(j)
ans = 0
for P in product([0, 1], repeat=M):
need = set()
for i in range(M):
if P[i]:
need |= rem[i]
if len(need) <= R:
res = 0
for i in range(M):
if P[i]:
tx, ty, tz, tb = T[i]
res += tb / dist2(ex, ey, ez, tx, ty, tz)
ans = max(ans, res)
print("%.10f" % ans)
``` | instruction | 0 | 34,203 | 3 | 68,406 |
No | output | 1 | 34,203 | 3 | 68,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose that there are some light sources and many spherical balloons. All light sources have sizes small enough to be modeled as point light sources, and they emit light in all directions. The surfaces of the balloons absorb light and do not reflect light. Surprisingly in this world, balloons may overlap.
You want the total illumination intensity at an objective point as high as possible. For this purpose, some of the balloons obstructing lights can be removed. Because of the removal costs, however, there is a certain limit on the number of balloons to be removed. Thus, you would like to remove an appropriate set of balloons so as to maximize the illumination intensity at the objective point.
The following figure illustrates the configuration specified in the first dataset of the sample input given below. The figure shows the xy-plane, which is enough because, in this dataset, the z-coordinates of all the light sources, balloon centers, and the objective point are zero. In the figure, light sources are shown as stars and balloons as circles. The objective point is at the origin, and you may remove up to 4 balloons. In this case, the dashed circles in the figure correspond to the balloons to be removed.
<image>
Figure G.1: First dataset of the sample input.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
N M R
S1x S1y S1z S1r
...
SNx SNy SNz SNr
T1x T1y T1z T1b
...
TMx TMy TMz TMb
Ex Ey Ez
The first line of a dataset contains three positive integers, N, M and R, separated by a single space. N means the number of balloons that does not exceed 2000. M means the number of light sources that does not exceed 15. R means the number of balloons that may be removed, which does not exceed N.
Each of the N lines following the first line contains four integers separated by a single space. (Six, Siy, Siz) means the center position of the i-th balloon and Sir means its radius.
Each of the following M lines contains four integers separated by a single space. (Tjx, Tjy, Tjz) means the position of the j-th light source and Tjb means its brightness.
The last line of a dataset contains three integers separated by a single space. (Ex, Ey, Ez) means the position of the objective point.
Six, Siy, Siz, Tjx, Tjy, Tjz, Ex, Ey and Ez are greater than -500, and less than 500. Sir is greater than 0, and less than 500. Tjb is greater than 0, and less than 80000.
At the objective point, the intensity of the light from the j-th light source is in inverse proportion to the square of the distance, namely
Tjb / { (Tjx β Ex)2 + (Tjy β Ey)2 + (Tjz β Ez)2 },
if there is no balloon interrupting the light. The total illumination intensity is the sum of the above.
You may assume the following.
1. The distance between the objective point and any light source is not less than 1.
2. For every i and j, even if Sir changes by Ξ΅ (|Ξ΅| < 0.01), whether the i-th balloon hides the j-th light or not does not change.
The end of the input is indicated by a line of three zeros.
Output
For each dataset, output a line containing a decimal fraction which means the highest possible illumination intensity at the objective point after removing R balloons. The output should not contain an error greater than 0.0001.
Example
Input
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 240
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 260
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
5 1 3
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
5 1 2
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
0 0 0
Output
3.5
3.6
1.1666666666666667
0.0
Submitted Solution:
```
from itertools import product
from sys import stdin, stdout
readline = stdin.readline
write = stdout.write
def dist2(x0, y0, z0, x1, y1, z1):
return (x0 - x1)**2 + (y0 - y1)**2 + (z0 - z1)**2
def dot(x0, y0, z0, x1, y1, z1):
return x0*x1 + y0*y1 + z0*z1
def cross2(x0, y0, z0, x1, y1, z1):
return (y0*z1 - y1*z0)**2 + (z0*x1 - z1*x0)**2 + (x0*y1 - x1*y0)**2
while 1:
N, M, R = map(int, readline().split())
if N == M == R == 0:
break
S = [list(map(int, readline().split())) for i in range(N)]
T = [list(map(int, readline().split())) for i in range(M)]
ex, ey, ez = map(int, readline().split())
L = [tb / dist2(ex, ey, ez, tx, ty, tz) for tx, ty, tz, tb in T]
rem = [set() for i in range(M)]
for i in range(M):
tx, ty, tz, tb = T[i]
r = rem[i]
ld = dist2(ex, ey, ez, tx, ty, tz)
for j in range(N):
sx, sy, sz, sr = S[j]
sr2 = sr**2
ok = 1
dd1 = (dist2(sx, sy, sz, ex, ey, ez) <= sr2)
dd2 = (dist2(sx, sy, sz, tx, ty, tz) <= sr2)
if dd1 ^ dd2:
ok = 0
elif dd1 == dd2 == 0:
if (cross2(sx - ex, sy - ey, sz - ez, tx - ex, ty - ey, tz - ez)
<= sr2 * ld):
if (dot(sx - ex, sy - ey, sz - ez, tx - ex, ty - ey, tz - ez) >= 0 and
dot(sx - tx, sy - ty, sz - tz, ex - tx, ey - ty, ez - tz) >= 0):
ok = 0
if not ok:
r.add(j)
ans = 0
for P in product([0, 1], repeat=M):
need = set()
for i in range(M):
if P[i]:
need |= rem[i]
if len(need) <= R:
ans = max(ans, sum(L[i] for i in range(M) if P[i]))
write("%.10f\n" % ans)
``` | instruction | 0 | 34,204 | 3 | 68,408 |
No | output | 1 | 34,204 | 3 | 68,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose that there are some light sources and many spherical balloons. All light sources have sizes small enough to be modeled as point light sources, and they emit light in all directions. The surfaces of the balloons absorb light and do not reflect light. Surprisingly in this world, balloons may overlap.
You want the total illumination intensity at an objective point as high as possible. For this purpose, some of the balloons obstructing lights can be removed. Because of the removal costs, however, there is a certain limit on the number of balloons to be removed. Thus, you would like to remove an appropriate set of balloons so as to maximize the illumination intensity at the objective point.
The following figure illustrates the configuration specified in the first dataset of the sample input given below. The figure shows the xy-plane, which is enough because, in this dataset, the z-coordinates of all the light sources, balloon centers, and the objective point are zero. In the figure, light sources are shown as stars and balloons as circles. The objective point is at the origin, and you may remove up to 4 balloons. In this case, the dashed circles in the figure correspond to the balloons to be removed.
<image>
Figure G.1: First dataset of the sample input.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
N M R
S1x S1y S1z S1r
...
SNx SNy SNz SNr
T1x T1y T1z T1b
...
TMx TMy TMz TMb
Ex Ey Ez
The first line of a dataset contains three positive integers, N, M and R, separated by a single space. N means the number of balloons that does not exceed 2000. M means the number of light sources that does not exceed 15. R means the number of balloons that may be removed, which does not exceed N.
Each of the N lines following the first line contains four integers separated by a single space. (Six, Siy, Siz) means the center position of the i-th balloon and Sir means its radius.
Each of the following M lines contains four integers separated by a single space. (Tjx, Tjy, Tjz) means the position of the j-th light source and Tjb means its brightness.
The last line of a dataset contains three integers separated by a single space. (Ex, Ey, Ez) means the position of the objective point.
Six, Siy, Siz, Tjx, Tjy, Tjz, Ex, Ey and Ez are greater than -500, and less than 500. Sir is greater than 0, and less than 500. Tjb is greater than 0, and less than 80000.
At the objective point, the intensity of the light from the j-th light source is in inverse proportion to the square of the distance, namely
Tjb / { (Tjx β Ex)2 + (Tjy β Ey)2 + (Tjz β Ez)2 },
if there is no balloon interrupting the light. The total illumination intensity is the sum of the above.
You may assume the following.
1. The distance between the objective point and any light source is not less than 1.
2. For every i and j, even if Sir changes by Ξ΅ (|Ξ΅| < 0.01), whether the i-th balloon hides the j-th light or not does not change.
The end of the input is indicated by a line of three zeros.
Output
For each dataset, output a line containing a decimal fraction which means the highest possible illumination intensity at the objective point after removing R balloons. The output should not contain an error greater than 0.0001.
Example
Input
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 240
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
12 5 4
0 10 0 1
1 5 0 2
1 4 0 2
0 0 0 2
10 0 0 1
3 -1 0 2
5 -1 0 2
10 10 0 15
0 -10 0 1
10 -10 0 1
-10 -10 0 1
10 10 0 1
0 10 0 260
10 0 0 200
10 -2 0 52
-10 0 0 100
1 1 0 2
0 0 0
5 1 3
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
5 1 2
1 2 0 2
-1 8 -1 8
-2 -3 5 6
-2 1 3 3
-4 2 3 5
1 1 2 7
0 0 0
0 0 0
Output
3.5
3.6
1.1666666666666667
0.0
Submitted Solution:
```
from itertools import product
def dist2(x0, y0, z0, x1, y1, z1):
return (x0 - x1)**2 + (y0 - y1)**2 + (z0 - z1)**2
def dot(x0, y0, z0, x1, y1, z1):
return x0*x1 + y0*y1 + z0*z1
def cross2(x0, y0, z0, x1, y1, z1):
return (y0*z1 - y1*z0)**2 + (z0*x1 - z1*x0)**2 + (x0*y1 - x1*y0)**2
while 1:
N, M, R = map(int, input().split())
if N == M == R == 0:
break
S = [list(map(int, input().split())) for i in range(N)]
T = [list(map(int, input().split())) for i in range(M)]
ex, ey, ez = map(int, input().split())
L = [tb / dist2(ex, ey, ez, tx, ty, tz) for tx, ty, tz, tb in T]
rem = [set() for i in range(M)]
for i in range(M):
tx, ty, tz, tb = T[i]
r = rem[i]
ld = dist2(ex, ey, ez, tx, ty, tz)
for j in range(N):
sx, sy, sz, sr = S[j]
sr2 = sr**2
ok = 1
dd1 = (dist2(sx, sy, sz, ex, ey, ez) <= sr2)
dd2 = (dist2(sx, sy, sz, tx, ty, tz) <= sr2)
if dd1 ^ dd2:
ok = 0
elif dd1 == dd2 == 0:
if (cross2(sx - ex, sy - ey, sz - ez, tx - ex, ty - ey, tz - ez)
<= sr2 * ld):
if (dot(sx - ex, sy - ey, sz - ez, tx - ex, ty - ey, tz - ez) >= 0 and
dot(sx - tx, sy - ty, sz - tz, ex - tx, ey - ty, ez - tz) >= 0):
ok = 0
if not ok:
r.add(j)
ans = 0
for P in product([0, 1], repeat=M):
need = set()
for i in range(M):
if P[i]:
need |= rem[i]
if len(need) <= R:
ans = max(ans, sum(L[i] for i in range(M) if P[i]))
print("%.10f" % ans)
``` | instruction | 0 | 34,205 | 3 | 68,410 |
No | output | 1 | 34,205 | 3 | 68,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image> | instruction | 0 | 34,317 | 3 | 68,634 |
Tags: brute force, geometry
Correct Solution:
```
#!/usr/bin/env python
from math import gcd
def get_2dline(p1, p2):
if p1 == p2:
return (0, 0, 0)
_p1, _p2 = min(p1, p2), max(p1, p2)
a, b, c = _p2[1] - _p1[1], _p1[0] - _p2[0], _p1[1] * _p2[0] - _p1[0] * _p2[1]
g = gcd(gcd(a, b), c)
return (a // g, b // g, c // g)
is_parallel = lambda l1, l2: l1[0] * l2[1] == l2[0] * l1[1]
def main():
n = int(input())
p = [(0, 0)] * n
for i in range(n):
p[i] = tuple(map(int, input().split()))
cnt = 0
lines = set()
for i in range(n):
for j in range(i + 1, n):
line = get_2dline(p[i], p[j])
if line not in lines:
for other in lines:
cnt += not is_parallel(line, other)
lines.add(line)
print(cnt)
if __name__ == "__main__":
main()
``` | output | 1 | 34,317 | 3 | 68,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image> | instruction | 0 | 34,318 | 3 | 68,636 |
Tags: brute force, geometry
Correct Solution:
```
import sys
import collections
import math
import heapq
from operator import itemgetter
def getint():
return int(input())
def getints():
return [int(x) for x in input().split(' ')]
n = getint()
points = [tuple(getints()) for _ in range(n)]
result = total = 0
slopes = collections.defaultdict(set)
for i in range(n - 1):
for j in range(i + 1, n):
x1, y1, x2, y2 = points[i][0], points[i][1], points[j][0], points[j][1]
a, b = y1 - y2, x1 - x2
d = math.gcd(a, b)
a, b = a // d, b // d
if a < 0 or (a == 0 and b < 0):
a, b = -a, -b
c = a * x1 - b * y1
slope = (a, b)
if c not in slopes[slope]:
total += 1
slopes[slope].add(c)
result += total - len(slopes[slope])
print(str(result))
``` | output | 1 | 34,318 | 3 | 68,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image> | instruction | 0 | 34,319 | 3 | 68,638 |
Tags: brute force, geometry
Correct Solution:
```
# AC
import sys
from math import gcd
class Main:
def __init__(self):
self.buff = None
self.index = 0
def next(self):
if self.buff is None or self.index == len(self.buff):
self.buff = self.next_line()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_line(self):
return sys.stdin.readline().split()
def next_ints(self):
return [int(x) for x in sys.stdin.readline().split()]
def next_int(self):
return int(self.next())
def solve(self):
n = self.next_int()
pr = [(self.next_int(), self.next_int()) for _ in range(0, n)]
ks = {}
ct = 0
res = 0
ls = set()
for i in range(0, n):
for j in range(i + 1, n):
dy = pr[j][1] - pr[i][1]
dx = pr[j][0] - pr[i][0]
if dx < 0:
dx *= -1
dy *= -1
elif dx == 0:
dy = abs(dy)
g = gcd(abs(dx), abs(dy))
k = (dx // g, dy // g)
x, y = pr[i][0], pr[i][1]
if dx == 0:
a = (pr[i][0], 0)
else:
cl = abs(x) // k[0]
if x >= 0:
x -= cl * k[0]
y -= cl * k[1]
else:
x += cl * k[0]
y += cl * k[1]
while x < 0:
x += k[0]
y += k[1]
a = (x, y)
if (k, a) in ls:
continue
else:
ls.add((k, a))
ct += 1
if k not in ks:
ks[k] = 1
else:
ks[k] += 1
res += ct - ks[k]
print(res)
if __name__ == '__main__':
Main().solve()
``` | output | 1 | 34,319 | 3 | 68,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image> | instruction | 0 | 34,321 | 3 | 68,642 |
Tags: brute force, geometry
Correct Solution:
```
from random import random
from collections import defaultdict
from fractions import Fraction
import math
import re
import fractions
N = int(input())
lines = defaultdict(set)
poles = []
for _ in range(N):
x, y = map(int, input().split(" "))
for xx, yy in poles:
if yy - y == 0:
slope = Fraction(10**6, 1)
const = y
else:
slope = Fraction(xx - x, yy - y)
const = x - (slope * y)
lines[slope].add(const)
poles.append((x, y))
result = 0
visited = 0
# print(lines)
for slope in lines:
result += visited * len(lines[slope])
visited += len(lines[slope])
print(result)
``` | output | 1 | 34,321 | 3 | 68,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image> | instruction | 0 | 34,322 | 3 | 68,644 |
Tags: brute force, geometry
Correct Solution:
```
import sys,math,itertools
from collections import Counter,deque,defaultdict
from bisect import bisect_left,bisect_right
from heapq import heappop,heappush,heapify, nlargest
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
n = inp()
a = [tuple(map(int, sys.stdin.readline().split())) for _ in range(n)]
d = defaultdict(int)
z = set()
yp = set()
xp = set()
for i in range(n):
for j in range(i+1,n):
x1,y1 = a[i]; x2,y2 = a[j]
Y = y2-y1; X = x2-x1
if X < 0: Y *= -1; X *= -1
if X == 0:
xp.add(x1)
elif Y == 0:
yp.add(y1)
else:
g = math.gcd(X,abs(Y))
X //= g; Y //= g
c = x1*Y-y1*X
if (X,Y,c) in z: continue
d[(X,Y)] += 1
z.add((X,Y,c))
m = len(z) + len(yp) + len(xp)
res = 0
for x,y,c in list(z):
res += m-d[(x,y)]
for YY in list(yp):
res += m-len(yp)
for XX in list(xp):
res += m-len(xp)
print(res//2)
``` | output | 1 | 34,322 | 3 | 68,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image> | instruction | 0 | 34,323 | 3 | 68,646 |
Tags: brute force, geometry
Correct Solution:
```
import sys
import math
def main():
# sys.stdin = open('E:\\Sublime\\in.txt', 'r')
# sys.stdout = open('E:\\Sublime\\out.txt', 'w')
# sys.stderr = open('E:\\Sublime\\err.txt', 'w')
n = int(sys.stdin.readline().strip())
p = []
for _ in range(n):
x, y = map(int, sys.stdin.readline().strip().split())
p.append((x, y))
s = {}
for i in range(n):
x1, y1 = p[i]
for j in range(i + 1, n):
x2, y2 = p[j]
slope = 0
cons = 0
if x1 == x2:
slope = 10**10
cons = x1
else:
slope = (y1 - y2) / (x1 - x2)
cons = (y1 * x2 - x1 * y2) / (x2 - x1)
if slope in s:
if cons in s[slope]:
s[slope][cons] += 1
else:
s[slope][cons] = 1
else:
s[slope] = {cons: 1}
p = []
for m in s:
p.append(len(s[m]))
s = sum(p)
ans = 0
for x in p:
ans += x * (s - x)
print(ans // 2)
if __name__ == '__main__':
main()
# hajj
# γγγγγγ οΌΏοΌΏ
# γγγγγοΌοΌγγγ
# γγγγγ| γ_γ _ l
# γ γγγοΌ` γοΌΏxγ
# γγ γ /γγγ γ |
# γγγ /γ γ½γγ οΎ
# γ γ βγγ|γ|γ|
# γοΌοΏ£|γγ |γ|γ|
# γ| (οΏ£γ½οΌΏ_γ½_)__)
# γοΌΌδΊγ€
``` | output | 1 | 34,323 | 3 | 68,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image> | instruction | 0 | 34,324 | 3 | 68,648 |
Tags: brute force, geometry
Correct Solution:
```
def gcd(x, y):
if y == 0:
return x
else:
return gcd(y, x % y)
from collections import *
n = int(input())
ps = []
for i in range(n):
ps.append([int(i) for i in input().split(' ')])
xs = 0
ys = 0
lines = defaultdict(int)
cache = set()
xcache = set()
ycache = set()
for i in range(n):
for j in range(i+1, n):
if ps[i][0] == ps[j][0]:
if ps[i][0] in xcache:
continue
xcache.add(ps[i][0])
xs += 1
continue
if ps[i][1] == ps[j][1]:
if ps[i][1] in ycache:
continue
ycache.add(ps[i][1])
ys += 1
continue
tmpp = sorted([ps[i], ps[j]])
# if
tmppp = (tmpp[1][0] - tmpp[0][0], tmpp[1][1] - tmpp[0][1])
gcdd = gcd(abs(tmpp[1][0] - tmpp[0][0]), abs(tmpp[1][1] - tmpp[0][1]))
tmppp = (tmppp[0]//gcdd, tmppp[1]//gcdd)
kkk = (tmppp[1], tmppp[0])
if tmppp[1] < 0:
kkk = (-tmppp[1], -tmppp[0])
qqq = (tmppp[1] * (-ps[i][0]) + tmppp[0] * ps[i][1], tmppp[0])
if qqq[0] < 0:
qqq = (-qqq[0], -qqq[1])
line = (kkk, qqq)
if line in cache:
continue
cache.add(line)
lines[kkk] += 1
tmp = xs + ys + sum(lines.values())
print((tmp*(tmp-1) - sum(v*(v-1) for v in lines.values()) - xs*(xs-1) - ys*(ys-1)) // 2)
``` | output | 1 | 34,324 | 3 | 68,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
import math
class Pt:
def __init__(self, *args):
if len(args) == 0:
self.x, self.y = 0, 0
elif len(args) == 1:
self.x, self.y = map(int, args[0].split())
else:
self.x, self.y = args[0], args[1]
def __str__(self):
return str(self.x) + ' ' + str(self.y)
def __add__(self, other):
return Pt(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Pt(self.x - other.x, self.y - other.y)
def __mul__(self, other):
return Pt(self.x * other, self.y * other)
def __rmul__(self, other):
return self * other
def __truediv__(self, other):
return Pt(self.x / other, self.y / other)
def __abs__(self):
return math.hypot(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def dot(self, other):
return self.x * other.x + self.y * other.y
def cross(self, other):
return self.x * other.y - self.y * other.x
@staticmethod
def get_straight(self, other):
a = self.y - other.y
b = other.x - self.x
c = self.cross(other)
return a, b, c
class Straight:
def __init__(self, *args):
if len(args) == 1:
self.a, self.b, self.c = map(int, args[0].split())
elif len(args) == 2:
self.a, self.b, self.c = Pt.get_straight(*args)
elif len(args) == 3:
self.a, self.b, self.c = args
def __str__(self):
return ' '.join(map(str, [self.a, self.b, self.c]))
def __eq__(self, other):
if self.b != 0 or other.b != 0:
return self.a * other.b == other.a * self.b and self.c * other.b == other.c * self.b
val1 = math.sqrt(self.a ** 2 + self.b ** 2)
val2 = math.sqrt(other.a ** 2 + other.b ** 2)
a1, c1 = self.a / val1, self.c / val1
a2, c2 = other.a / val2, other.c / val2
if (a1 < 0) != (a2 < 0):
a1, a2, c1, c2 = a1, -a2, c1, -c2
return a1 == a2 and c1 == c2
def perpendicular(self, point: Pt):
return Straight(-self.b, self.a, self.b * point.x - self.a * point.y)
def get_value(self, point):
return self.a * point.x + self.b * point.y + self.c
def is_own(self, point: Pt):
return self.get_value(point) == 0
def same_side(self, pt1, pt2):
return (self.get_value(pt1) < 0) == (self.get_value(pt2) < 0)
def intersection(self, other):
d = Pt(self.a, self.b).cross(Pt(other.a, other.b))
dx = Pt(self.c, self.b).cross(Pt(other.c, other.b))
dy = Pt(self.a, self.c).cross(Pt(other.a, other.c))
return Pt(-dx / d, -dy / d)
def dist_from_point(self, point):
val = math.sqrt(self.a ** 2 + self.b ** 2)
return abs(Straight(self.a / val, self.b / val, self.c / val).get_value(point))
def parallel(self, dist):
val = math.sqrt(self.a ** 2 + self.b ** 2)
return Straight(self.a, self.b, self.c - dist * val)
def is_parallel(self, other):
return self.a * other.b == self.b * other.a
def is_perpendicular(self, other):
per = Straight(-self.b, self.a, 0)
return per.a * other.b == per.b * other.a
n = int(input())
points = [Pt(input()) for _ in range(n)]
straights = []
for i in range(n):
for j in range(i + 1, n):
st = Straight(points[i], points[j])
ok = True
for x in straights:
if st == x:
ok = False
break
if ok:
straights.append(st)
res = 0
m = len(straights)
for i in range(m):
for j in range(i + 1, m):
if not straights[i].is_parallel(straights[j]):
res += 1
print(res)
``` | instruction | 0 | 34,325 | 3 | 68,650 |
Yes | output | 1 | 34,325 | 3 | 68,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
from sys import maxsize
n = int(input())
p = []
for i in range(n):
w = list(map(int, input().split()))
p.append(w)
p.sort()
s = {}
for i in range(n):
x1, y1 = p[i]
for j in range(i + 1, n):
x2, y2 = p[j]
k = 0
b = 0
if x1 - x2 == 0:
k = maxsize
b = x1
else:
k = (y2 - y1) / (x2 - x1)
b = (y1 * x2 - x1 * y2) / (x2 - x1)
if k in s:
if b in s[k]:
s[k][b] += 1
else:
s[k][b] = 1
else:
s[k] = {b: 1}
ans = 0
cnt = 0
for i in s:
ans += cnt * len(s[i])
cnt += len(s[i])
print(ans)
``` | instruction | 0 | 34,326 | 3 | 68,652 |
Yes | output | 1 | 34,326 | 3 | 68,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
def gcd(a,b):
if not b: return a
return gcd(b, a%b)
n = int(input())
P = [[int(x) for x in input().split()] for _ in range(n)]
L = []
def addLine(x,y,dx,dy):
if dx < 0:
dx *= -1
dy *= -1
elif dx == 0:
if dy < 0:
dy *= -1
g = gcd(dx,dy)
dx //= g
dy //= g
if dx:
k = x//dx
else:
k = y//dy
x -= k*dx
y -= k*dy
L.append((x,y,dx,dy))
for i in range(n):
for j in range(i+1,n):
xi,yi = P[i]
xj,yj = P[j]
dx,dy = xi-xj,yi-yj
addLine(xi,yi,dx,dy)
from collections import defaultdict as dd, deque
L = list(set(L))
res = 0
C = dd(int)
for x,y,dx,dy in L:
C[dx,dy] += 1
ss = sum(C.values())
for x in C.values():
res += (ss-x)*x
#for i in range(len(L)):
# for j in range(i+1, len(L)):
# x1,y1,dx1,dy1 = L[i]
# x2,y2,dx2,dy2 = L[j]
# if dx1 != dx2 or dy1 != dy2:
# #print(L[i])
# #print(L[j])
# #print('---')
# res += 1
print(res//2)
``` | instruction | 0 | 34,327 | 3 | 68,654 |
Yes | output | 1 | 34,327 | 3 | 68,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
n = int(input())
T = []
P = []
for i in range(n):
x, y = map(int, input().split())
T.append([x, y])
for i in range(n):
for j in range(i + 1, n):
x1, y1 = T[i]
x2, y2 = T[j]
a = y2 - y1
b = x1 - x2
c = -(a * x1 + b * y1)
if a != 0:
b /= a
c /= a
a = 1
else:
a = 0
c /= b
b = 1
P.append([a, b, c])
P.sort()
newp = [P[0]]
for i in range(len(P) - 1):
if P[i] != P[i + 1]:
newp.append(P[i + 1])
P = newp
was = []
sos = dict()
for a, b, c in P:
if b != 0 and a != 0:
z = a / b
elif a == 0:
z = 'kek'
else:
z = 'lol'
if z not in sos:
sos[z] = 1
else:
sos[z] += 1
sus = 0
sussqua = 0
for i in sos:
sus += sos[i]
sussqua += sos[i] ** 2
print((sus ** 2 - sussqua) // 2)
``` | instruction | 0 | 34,328 | 3 | 68,656 |
Yes | output | 1 | 34,328 | 3 | 68,657 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
for i in arr:
stdout.write(str(i)+' ')
stdout.write('\n')
range = xrange # not for python 3.0+
n=input()
l=[]
for i in range(n):
l.append(in_arr())
d1=Counter()
d2=Counter()
for i in range(n):
for j in range(i-1,-1,-1):
try:
m=(l[i][1]-l[j][1])/float(l[i][0]-l[j][0])
c=l[i][1]-(m*l[i][0])
except:
m=5000
c=l[i][0]
d1[(m,c)]+=1
d2[m]+=1
ans=0
for i in d1:
for j in d1:
if i==j:
break
if i[0]!=j[0]:
#print i,j
ans+=1
pr_num(ans)
``` | instruction | 0 | 34,329 | 3 | 68,658 |
No | output | 1 | 34,329 | 3 | 68,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
def get_k(a, b):
x1, y1 = a
x2, y2 = b
if x2 != x1:
return (y2 - y1) / (x2 - x1)
return 10**10
def get_offset(a, k2):
x, y = a
return y - k2 * x
s = set()
cnt = 0
a = []
n = int(input())
for i in range(n):
a.append(tuple(map(int, input().split())))
lines = []
for i in range(n):
for j in range(n):
if i >= j: continue
line = (a[i], a[j])
k1 = get_k(*line)
o1 = get_offset(line[0], k1)
if k1 == 10**10:
o1 = line[0][0]
if not (k1, o1) in s: lines.append((a[i], a[j]))
s.add((round(k1 + 5e-12, 11), round(o1 + 5e-12, 11)))
def upper_bound(i, el):
l, r = i, len(new)
while r - l > 1:
m = (l + r) // 2
if new[m] > el:
r = m
else:
l = m
return len(new) - r
cnt = 0
new = [get_k(*l1) for l1 in lines]
new.sort()
for i, el in enumerate(new):
cnt += upper_bound(i, el + 1e-11)
print(cnt)
``` | instruction | 0 | 34,330 | 3 | 68,660 |
No | output | 1 | 34,330 | 3 | 68,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
inf = 1 << 100
s = set()
n = int(input())
a = []
ans = 0
for i in range(n):
b, c = tuple(map(int, input().split()))
a.append((b, c))
for i in range(n):
for j in range(i+1, n):
if a[i][0] - a[j][0] == 0:
m = inf
c = a[i][0]
else:
m = (a[i][1] - a[j][1]) / (a[i][0] - a[j][0])
c = a[i][1] - m*a[i][0]
s = s ^ {(m ,c)}
#print(m, c)
l = sorted(s)
i = 1
cnt = 1
ans = (len(l)*(len(l)-1))//2
while i < len(l):
#print(i, l[i])
if l[i][0] != l[i-1][0]:
ans -= (cnt*(cnt-1))//2
cnt = 1
else:
#print("oooo")
cnt += 1
i += 1
ans -= (cnt*(cnt-1))//2
print(ans)
``` | instruction | 0 | 34,331 | 3 | 68,662 |
No | output | 1 | 34,331 | 3 | 68,663 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
lines = set()
points = []
num = int(input())
for i in range(num):
points.append([int(j) for j in input().split()])
for i in range(num):
for j in range(i):
dx = (points[j][0] - points[i][0])
if dx == 0:
lines.add((32785947687736461, points[i][0]))
else:
m = (points[j][1] - points[i][1])/dx
lines.add((int(10000000000*m), int(10000000000*(points[i][1] - m * points[i][0]))))
ms = {}
for l in lines:
if l[0] in ms:
ms[l[0]] += 1
else:
ms[l[0]] = 1
total = 0
#print(lines)
#print(ms)
#print(type(ms.values()))
t = list(ms.values())
s = sum(t)
for a in t:
total += a * (s-a)
print(total//2)
``` | instruction | 0 | 34,332 | 3 | 68,664 |
No | output | 1 | 34,332 | 3 | 68,665 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This problem is same as the next one, but has smaller constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (x_i, y_i). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
Input
The first line contains a single integer n (2 β€ n β€ 50) β the number of electric poles.
Each of the following n lines contains two integers x_i, y_i (-10^4 β€ x_i, y_i β€ 10^4) β the coordinates of the poles.
It is guaranteed that all of these n points are distinct.
Output
Print a single integer β the number of pairs of wires that are intersecting.
Examples
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
Note
In the first example:
<image>
In the second example:
<image>
Note that the three poles (0, 0), (0, 2) and (0, 4) are connected by a single wire.
In the third example:
<image>
Submitted Solution:
```
#!/usr/bin/env python3
import atexit
import io
import sys
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B
@atexit.register
def write():
sys.__stdout__.write(_O_B.getvalue())
def gcd(x, y):
while(y):
x, y = y, x % y
return x
def main():
d=dict()
s=set()
x=[]
y=[]
n=int(input())
for _ in range(n):
tx,ty=map(int,input().split())
x.append(tx)
y.append(ty)
for i in range(n):
for j in range(i+1,n):
g=gcd(abs(x[i]-x[j]),abs(y[i]-y[j]))
sign=(x[i]-x[j])*(y[i]-y[j])
if sign<0:
sign=-1
elif sign>0:
sign=1
else:
if y[i]-y[j]==0:
if x[i]-x[j] > 0:
sign="+inf"
elif x[i]-x[j] < 0:
sign="-inf"
else:
sign=0
# print((x[i],y[i]),(x[j],y[j]))
# print((abs(y[i]-y[j])//g,abs(x[i]-x[j])//g,sign,((x[i]-x[j])*y[i])-((y[i]-y[j])*x[i])))
s.add((abs(y[i]-y[j])//g,abs(x[i]-x[j])//g,sign,((x[i]-x[j])*y[i])-((y[i]-y[j])*x[i])))
# print(s)
for i in s:
try:
d[(i[0],i[1],i[2])]+=1
except KeyError:
d[(i[0],i[1],i[2])]=1
ans=0
# print(d)
su=list(d.values())
ln=len(su)
for i in range(ln):
tans=0
for j in range(i+1,ln):
tans+=su[j]
ans+=(tans*su[i])
print(ans)
if __name__=='__main__':
main()
``` | instruction | 0 | 34,333 | 3 | 68,666 |
No | output | 1 | 34,333 | 3 | 68,667 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left.
Submitted Solution:
```
gl, gr = map(int, input().split())
bl, br = map(int, input().split())
if br in range(gl-1, ((gl+1)*2)+1) or bl in range(gr-1,((gr+1)*2)+1):
print('YES')
else:
print('NO')
``` | instruction | 0 | 34,756 | 3 | 69,512 |
Yes | output | 1 | 34,756 | 3 | 69,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left.
Submitted Solution:
```
bl,br=map(int,input().split())
al,ar=map(int,input().split())
if bl-1<=ar<=2*bl+2 or br-1<=al<=2*br+2:
print("YES")
else:
print('NO')
``` | instruction | 0 | 34,757 | 3 | 69,514 |
Yes | output | 1 | 34,757 | 3 | 69,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left.
Submitted Solution:
```
ar, al = map(int, input().split())
br, bl = map(int, input().split())
print('YES' if 2 * (ar + 1) >= bl >= ar - 1 or 2 * (al + 1) >= br >= al - 1 else 'NO')
``` | instruction | 0 | 34,758 | 3 | 69,516 |
Yes | output | 1 | 34,758 | 3 | 69,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left.
Submitted Solution:
```
def compatible(a,b):
return (a<=b+1) and (2*a>=b-2);
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
print(['NO','YES'][compatible(a[0],b[1]) or compatible(a[1],b[0])])
``` | instruction | 0 | 34,759 | 3 | 69,518 |
Yes | output | 1 | 34,759 | 3 | 69,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left.
Submitted Solution:
```
r=lambda b,g: b>=g-1 and b<=2*g
a,b=map(int,input().split())
c,d=map(int,input().split())
if r(a,c) or r(a,d) or r(b,c) or r(b,d):print("YES")
else:print("NO")
``` | instruction | 0 | 34,760 | 3 | 69,520 |
No | output | 1 | 34,760 | 3 | 69,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
n=int(input())
a=list(map(lambda x: int(x), input().split()))
a=[0,0]+a
hashmap={}
for i in range(2,len(a)):
count=0
# while a[j]!=0:
# count+=1
# j=a[j]
count=1+a[a[i]]
if count not in hashmap:
hashmap[count]=0
hashmap[count]+=1
a[i]=count
total=1
for i in hashmap:
if hashmap[i]%2!=0:
total+=1
print(total)
``` | instruction | 0 | 34,873 | 3 | 69,746 |
Yes | output | 1 | 34,873 | 3 | 69,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
#sys.setrecursionlimit(300000)
#threading.stack_size(10**8)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
#-------------------------------------------------------------------------
n=int(input())
a=list(map(int,input().split()))
r=[0]*n
r[0]=1
d=dict()
d[1]=1
for i in range (n-1):
r[i+1]=r[a[i]-1]+1
if r[i+1] in d:
d[r[i+1]]+=1
else:
d[r[i+1]]=1
res=0
for i in d:
res+=d[i]%2
#print(r)
print(res)
``` | instruction | 0 | 34,874 | 3 | 69,748 |
Yes | output | 1 | 34,874 | 3 | 69,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
n = int(input())
par = [None] + [int(i) - 1 for i in input().split()]
children = [[] for _ in range(n)]
for child in range(1, n):
children[par[child]].append(child)
count = 0
nodesAtCurrLevel = [0]
while nodesAtCurrLevel:
count += len(nodesAtCurrLevel) % 2
nodesAtNextLevel = []
for node in nodesAtCurrLevel:
nodesAtNextLevel += children[node]
nodesAtCurrLevel = nodesAtNextLevel
print(count)
``` | instruction | 0 | 34,875 | 3 | 69,750 |
Yes | output | 1 | 34,875 | 3 | 69,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
from functools import reduce
n = int(input())
a = list(map(int, input().split()))
d = [0 for i in range(n)]
for i, j in enumerate(a):
k = j - 1
d[i + 1] = d[k] + 1
M = [0 for i in range(max(d) + 1)]
ans = 0
for i in d:
M[i] += 1
print(reduce(lambda x, y: x + (y % 2), M))
``` | instruction | 0 | 34,876 | 3 | 69,752 |
Yes | output | 1 | 34,876 | 3 | 69,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
import sys
sys.setrecursionlimit(sys.maxsize)
from collections import Counter
#from io import StringIO
#sys.stdin = StringIO(open(__file__.replace('.py', '.in')).read())
n = int(input())
a = list(map(int, input().split()))
# for i in range(n-1):
# print(i + 2, a[i])
adj = {}
for i in range(n-1):
if a[i] not in adj:
adj[a[i]] = []
adj[a[i]].append(i + 2)
def dfs(node, cnt):
global mx, adj, counter
#if counter[node] == 0:
# return
cnt += counter[node]
mx = max(mx, cnt)
if node in adj:
for ch in adj[node]:
dfs(ch, cnt)
counter = Counter(a)
for k, v in counter.items():
counter[k] = v % 2
# print(adj)
#
# print('---')
#
# for k, v in counter.items():
# print(k, v)
#
# print('---')
#
mx = 1
dfs(1, 1)
print(mx)
``` | instruction | 0 | 34,877 | 3 | 69,754 |
No | output | 1 | 34,877 | 3 | 69,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
from collections import Counter
n = int(input())
p_list =[int(x) for x in input().split()]
mapping = [0] + p_list
res = 1
p_counter = Counter(p_list)
while p_counter:
res += p_counter[1] % 2
p_counter = Counter((map(lambda y: mapping[y-1], p_counter)))
del p_counter[0]
print(res)
``` | instruction | 0 | 34,878 | 3 | 69,756 |
No | output | 1 | 34,878 | 3 | 69,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
n = int(input())
s = list(map(int, input().split()))
count = 1
ans = [1]
o = 2
for i in range(len(s)):
d = []
j = 0
while j < len(s):
if s[j] in ans:
d.append(j + o)
s.pop(j)
o += 1
else:
j += 1
if d == []:
break
count += (len(d) % 2)
ans = d.copy()
print(count)
``` | instruction | 0 | 34,879 | 3 | 69,758 |
No | output | 1 | 34,879 | 3 | 69,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
Input
First line of input contains single integer number n (2 β€ n β€ 100 000) β number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 β€ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Output
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
Examples
Input
3
1 1
Output
1
Input
5
1 2 2 2
Output
3
Input
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
Output
4
Note
In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Submitted Solution:
```
n = int(input())
graph = [[] for i in range(n)]
prev = list(map(int, input().split()))
s = dict()
ans = 0
def dfs(v, level):
global ans, graph
for u in graph[v]:
dfs(u, level + 1)
if len(graph[v]) % 2 and level not in s:
if level not in s:
s[level] = 0
s[level] += 1
for i in range(n - 1):
graph[prev[i] - 1].append(i + 1)
dfs(0, 0)
for i in s.values():
if i % 2 != 0:
ans += 1
print(ans + 1)
``` | instruction | 0 | 34,880 | 3 | 69,760 |
No | output | 1 | 34,880 | 3 | 69,761 |
Provide a correct Python 3 solution for this coding contest problem.
The University of Aizu has a park covered with grass, and there are no trees or buildings that block the sunlight. On sunny summer days, sprinklers installed in the park operate to sprinkle water on the lawn. The frog Pyonkichi lives in this park. Pyonkichi is not good at hot weather, and on summer days when the sun is strong, he will dry out and die if he is not exposed to the water of the sprinkler. The sprinklers installed in the park are supposed to sprinkle only one sprinkler at a time to save water, so Pyonkichi must move according to the operation of the sprinklers.
<image>
---
(a) Map of the park
<image> | <image>
--- | ---
(b) Pyonkichi's jump range | (c) Sprinkler watering range
The park is as shown in (a) of the above figure, the location in the park is represented by the coordinates 0 to 9 in each of the vertical and horizontal directions, and the black β is the sprinkler, which indicates the order in which the numbers operate. This is just an example, and the location and order of operation of each sprinkler changes daily.
Pyonkichi is clumsy, so he can only jump a certain distance to move. Pyonkichi's jumpable range is as shown in (b) above, and he cannot move to any other location. Moreover, one jump consumes a lot of physical strength, so you have to rest in the water for a while.
The range in which the sprinkler can sprinkle water is as shown in the above figure (c), including the coordinates of the sprinkler itself. Each sprinkler will stop after a period of watering, and the next sprinkler will start working immediately. Pyonkichi shall jump only once at this time, and shall not jump until the next watering stops. Also, since the park is surrounded on all sides by scorching asphalt, it is assumed that you will not jump in a direction that would cause you to go out of the park.
This summer was extremely hot. Was Pyonkichi able to survive? One day, read the initial position of Pyonkichi, the position of the sprinkler and the operation order, and if there is a movement path that Pyonkichi can survive, "OK", how? If you die even if you do your best, create a program that outputs "NA". However, the maximum number of sprinklers is 10, and Pyonkichi will jump from the initial position at the same time as the first sprinkler is activated.
Input
Given multiple datasets. Each dataset is given in the following format:
px py
n
x1 y1 x2 y2 ... xn yn
The first line gives the abscissa px and ordinate py of the initial position of Pyonkichi. The second line gives the number of sprinklers n. The third line gives the abscissa xi and the ordinate yi of the sprinkler that operates third.
The input ends with two zero lines. The number of datasets does not exceed 20.
Output
For each dataset, print OK if it is alive, or NA on one line otherwise.
Example
Input
6 1
10
6 4 3 3 1 2 0 5 4 6 1 8 5 9 7 7 8 6 8 3
6 1
10
6 4 3 3 1 2 0 5 4 6 1 8 5 9 7 7 8 6 9 0
0 0
Output
OK
NA | instruction | 0 | 35,059 | 3 | 70,118 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
import os
import math
def jump_candidate(pos):
x = pos[0]
y = pos[1]
candidate_list = []
A = []
A.append((-2, 0))
A.append((-2, -1))
A.append((-2, +1))
A.append((+2, 0))
A.append((+2, -1))
A.append((+2, +1))
A.append((0, -2))
A.append((-1, -2))
A.append((+1, -2))
A.append((0, +2))
A.append((-1, +2))
A.append((+1, +2))
for a in A:
next_x = x + a[0]
next_y = y + a[1]
if 0 <= next_x <= 9 and 0 <= next_y <= 9:
candidate_list.append((next_x, next_y))
return candidate_list
def in_sprinkler_range(frog_pos, sprinkler_pos):
fx = frog_pos[0]
fy = frog_pos[1]
sx = sprinkler_pos[0]
sy = sprinkler_pos[1]
if sx - 1 <= fx <= sx + 1 and sy - 1 <= fy <= sy + 1:
return True
else:
return False
for s in sys.stdin:
px, py = map(int, s.split())
if px == py == 0:
break
n = int(input())
lst = list(map(int, input().split()))
# solve
survivers = [(px, py)]
for i in range(n):
sprinkler = (lst[2 * i], lst[2 * i + 1])
next_survivers = []
for surviver in survivers:
pos_candidates = jump_candidate(surviver)
for jump_pos in pos_candidates:
if in_sprinkler_range(sprinkler, jump_pos):
next_survivers.append(jump_pos)
survivers = next_survivers
if len(survivers) == 0:
print('NA')
else:
print('OK')
``` | output | 1 | 35,059 | 3 | 70,119 |
Provide a correct Python 3 solution for this coding contest problem.
The University of Aizu has a park covered with grass, and there are no trees or buildings that block the sunlight. On sunny summer days, sprinklers installed in the park operate to sprinkle water on the lawn. The frog Pyonkichi lives in this park. Pyonkichi is not good at hot weather, and on summer days when the sun is strong, he will dry out and die if he is not exposed to the water of the sprinkler. The sprinklers installed in the park are supposed to sprinkle only one sprinkler at a time to save water, so Pyonkichi must move according to the operation of the sprinklers.
<image>
---
(a) Map of the park
<image> | <image>
--- | ---
(b) Pyonkichi's jump range | (c) Sprinkler watering range
The park is as shown in (a) of the above figure, the location in the park is represented by the coordinates 0 to 9 in each of the vertical and horizontal directions, and the black β is the sprinkler, which indicates the order in which the numbers operate. This is just an example, and the location and order of operation of each sprinkler changes daily.
Pyonkichi is clumsy, so he can only jump a certain distance to move. Pyonkichi's jumpable range is as shown in (b) above, and he cannot move to any other location. Moreover, one jump consumes a lot of physical strength, so you have to rest in the water for a while.
The range in which the sprinkler can sprinkle water is as shown in the above figure (c), including the coordinates of the sprinkler itself. Each sprinkler will stop after a period of watering, and the next sprinkler will start working immediately. Pyonkichi shall jump only once at this time, and shall not jump until the next watering stops. Also, since the park is surrounded on all sides by scorching asphalt, it is assumed that you will not jump in a direction that would cause you to go out of the park.
This summer was extremely hot. Was Pyonkichi able to survive? One day, read the initial position of Pyonkichi, the position of the sprinkler and the operation order, and if there is a movement path that Pyonkichi can survive, "OK", how? If you die even if you do your best, create a program that outputs "NA". However, the maximum number of sprinklers is 10, and Pyonkichi will jump from the initial position at the same time as the first sprinkler is activated.
Input
Given multiple datasets. Each dataset is given in the following format:
px py
n
x1 y1 x2 y2 ... xn yn
The first line gives the abscissa px and ordinate py of the initial position of Pyonkichi. The second line gives the number of sprinklers n. The third line gives the abscissa xi and the ordinate yi of the sprinkler that operates third.
The input ends with two zero lines. The number of datasets does not exceed 20.
Output
For each dataset, print OK if it is alive, or NA on one line otherwise.
Example
Input
6 1
10
6 4 3 3 1 2 0 5 4 6 1 8 5 9 7 7 8 6 8 3
6 1
10
6 4 3 3 1 2 0 5 4 6 1 8 5 9 7 7 8 6 9 0
0 0
Output
OK
NA | instruction | 0 | 35,060 | 3 | 70,120 |
"Correct Solution:
```
def solve():
px, py = map(int, input().split())
if px == 0:
return False
N = int(input())
*vs, = map(int, input().split())
P = []
for i in range(N):
x, y = vs[2*i:2*i+2]
P.append((x-1, y-1))
W = H = 10
dd = ((-2, 1), (-2, 0), (-2, -1), (-1, -2), (0, -2), (1, -2), (2, -1), (2, 0), (2, 1), (1, 2), (0, 2), (-1, 2))
memo = {}
def dfs(i, x, y):
key = (i, x, y)
if key in memo:
return memo[key]
if i == N:
return 1
x0, y0 = P[i]
for dx, dy in dd:
nx = x + dx; ny = y + dy
if not 0 <= nx < W or not 0 <= ny < H:
continue
if -1 <= x0 - nx <= 1 and -1 <= y0 - ny <= 1:
if dfs(i+1, nx, ny):
return 1
memo[key] = 0
return 0
print("OK" if dfs(0, px-1, py-1) else "NA")
return True
while solve():
...
``` | output | 1 | 35,060 | 3 | 70,121 |
Provide a correct Python 3 solution for this coding contest problem.
The University of Aizu has a park covered with grass, and there are no trees or buildings that block the sunlight. On sunny summer days, sprinklers installed in the park operate to sprinkle water on the lawn. The frog Pyonkichi lives in this park. Pyonkichi is not good at hot weather, and on summer days when the sun is strong, he will dry out and die if he is not exposed to the water of the sprinkler. The sprinklers installed in the park are supposed to sprinkle only one sprinkler at a time to save water, so Pyonkichi must move according to the operation of the sprinklers.
<image>
---
(a) Map of the park
<image> | <image>
--- | ---
(b) Pyonkichi's jump range | (c) Sprinkler watering range
The park is as shown in (a) of the above figure, the location in the park is represented by the coordinates 0 to 9 in each of the vertical and horizontal directions, and the black β is the sprinkler, which indicates the order in which the numbers operate. This is just an example, and the location and order of operation of each sprinkler changes daily.
Pyonkichi is clumsy, so he can only jump a certain distance to move. Pyonkichi's jumpable range is as shown in (b) above, and he cannot move to any other location. Moreover, one jump consumes a lot of physical strength, so you have to rest in the water for a while.
The range in which the sprinkler can sprinkle water is as shown in the above figure (c), including the coordinates of the sprinkler itself. Each sprinkler will stop after a period of watering, and the next sprinkler will start working immediately. Pyonkichi shall jump only once at this time, and shall not jump until the next watering stops. Also, since the park is surrounded on all sides by scorching asphalt, it is assumed that you will not jump in a direction that would cause you to go out of the park.
This summer was extremely hot. Was Pyonkichi able to survive? One day, read the initial position of Pyonkichi, the position of the sprinkler and the operation order, and if there is a movement path that Pyonkichi can survive, "OK", how? If you die even if you do your best, create a program that outputs "NA". However, the maximum number of sprinklers is 10, and Pyonkichi will jump from the initial position at the same time as the first sprinkler is activated.
Input
Given multiple datasets. Each dataset is given in the following format:
px py
n
x1 y1 x2 y2 ... xn yn
The first line gives the abscissa px and ordinate py of the initial position of Pyonkichi. The second line gives the number of sprinklers n. The third line gives the abscissa xi and the ordinate yi of the sprinkler that operates third.
The input ends with two zero lines. The number of datasets does not exceed 20.
Output
For each dataset, print OK if it is alive, or NA on one line otherwise.
Example
Input
6 1
10
6 4 3 3 1 2 0 5 4 6 1 8 5 9 7 7 8 6 8 3
6 1
10
6 4 3 3 1 2 0 5 4 6 1 8 5 9 7 7 8 6 9 0
0 0
Output
OK
NA | instruction | 0 | 35,061 | 3 | 70,122 |
"Correct Solution:
```
class Vector2:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector2(self.x + other.x, self.y + other.y)
def __hash__(self):
return self.x * 10 + self.y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def create_water_range():
l = []
for item in [-1, 0, 1]:
for item2 in [-1, 0, 1]:
l.append(Vector2(item, item2))
return l
def create_jump_range():
l = []
for item in [-2, 2]:
for item2 in [-1, 0, 1]:
l.append(Vector2(item, item2))
l.append(Vector2(item2, item))
return l
WATER_RANGE = create_water_range()
JUMP_RANGE = create_jump_range()
def is_live(water_count, point, LIMIT, WATER_POINT_LIST):
if LIMIT <= water_count:
return True
center = WATER_POINT_LIST[water_count]
water_point_set = set(center + water for water in WATER_RANGE)
for jump in JUMP_RANGE:
new_point = point + jump
if 0 <= new_point.x <= 9 and 0 <= new_point.y <= 9:
if new_point in water_point_set:
result = is_live(water_count + 1, new_point, LIMIT, WATER_POINT_LIST)
if result:
return result
return False
while True:
x, y = [int(item) for item in input().split(" ")]
if x == 0 and y == 0:
break
my_point = Vector2(x, y)
water_count = int(input())
point_list = [int(item) for item in input().split(" ")]
water_point_list = []
for x, y in zip(point_list[::2], point_list[1::2]):
water_point_list.append(Vector2(x, y))
result = is_live(0, my_point, water_count, water_point_list)
if result:
print("OK")
else:
print("NA")
``` | output | 1 | 35,061 | 3 | 70,123 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.