message stringlengths 2 44.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 276 109k | cluster float64 23 23 | __index_level_0__ int64 552 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}. | instruction | 0 | 276 | 23 | 552 |
Tags: binary search, combinatorics, two pointers
Correct Solution:
```
from sys import *
from math import *
n,k=map(int,stdin.readline().split())
a=list(map(int,stdin.readline().split()))
ans=0
for i in range(n-2):
x=a[i]+k
l=i+2
h=n-1
j=0
f=0
while l<=h:
m=(l+h)//2
if a[m]>x:
h=m-1
elif a[m]<=x:
j=m
l=m+1
f=1
if f==1:
y=j-i-2
ans+=(y+1)*(y+2)//2
print(ans)
``` | output | 1 | 276 | 23 | 553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Submitted Solution:
```
# n=int(input())
# n,k=map(int,input().split())
'''l=0
r=10**13
while l+1<r:
mid=(l+r)//2
val=(max(0,b_b*mid-b)*rb+max(0,b_s*mid-s)*rs+max(0,b_c*mid-b)*rc)
if val>money:
r=mid
if val<=money:
l=mid'''
# arr=list(map(int,input().split()))
# n=int(input())
# n,k=map(int,input().split())
# arr=list(map(int,input().split()))
n,d=map(int,input().split())
arr=list(map(int,input().split()))
i=0
ans=0
for j in range(n):
while j-i>=3 and arr[j]-arr[i]>d:
i+=1
if arr[j]-arr[i]<=d:
ans+=(j-i)*(j-i-1)//2
print(ans)
``` | instruction | 0 | 279 | 23 | 558 |
Yes | output | 1 | 279 | 23 | 559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Submitted Solution:
```
import bisect
n,d = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans =0
for i in range(0,n):
x = bisect.bisect_right(a,a[i]+d)
ans+=(x-i-1)*(x-i-2)//2
print(ans)
``` | instruction | 0 | 280 | 23 | 560 |
Yes | output | 1 | 280 | 23 | 561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Submitted Solution:
```
from bisect import bisect_left
def sol(a,n,d):
ans = 0
for i in range(n-2):
x = a[i]+d
pos = bisect_left(a,x)
if pos < n and a[pos] == x:
pos+=1
x = pos - (i+2)
ans+=((x*(x+1))//2)
return ans
n,d = map(int,input().split())
a = [int(i) for i in input().split()]
print(sol(a,n,d))
``` | instruction | 0 | 281 | 23 | 562 |
Yes | output | 1 | 281 | 23 | 563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Submitted Solution:
```
n, d = list(map(int, input().split()))
x = list(map(int, input().split()))
i = 0
j = i + 2
count = 0
in_betn = 1
while(j-i-1 >= 1 and j<n):
# print(x[j] - x[i])
while(j<n and x[j] - x[i] <= d):
count += (in_betn * (in_betn+1) / 2)
j += 1
in_betn += 1
# print("here")
in_betn -= 1
i += 1
print(int(count))
``` | instruction | 0 | 282 | 23 | 564 |
No | output | 1 | 282 | 23 | 565 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Submitted Solution:
```
n, d = map(int, input().split())
x = list(map(int, input().split()))
k = 0
j = 1
for i in range(n):
p = 0
while (j < n and x[j] - x[i] <= d):
k += (max(0, (j - i - 1)))
j += 1
p += 1
if (p == 0):
k += (max(0, j - i - 2))
print(k)
``` | instruction | 0 | 283 | 23 | 566 |
No | output | 1 | 283 | 23 | 567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Submitted Solution:
```
def cnk(n):
return 0 if 3 > n else n * (n - 1) * (n - 2) // 6
n, d = input().split()
n, d = int(n), float(d)
dots = list(map(int, input().split()))
l, r, res = 0, 0, 0
while l < n:
while r < n and dots[r] - dots[l] <= d:
r += 1
res += cnk(r - l)
if r == n:
break
while l < r and dots[r] - dots[l] > d:
l += 1
print(res)
``` | instruction | 0 | 284 | 23 | 568 |
No | output | 1 | 284 | 23 | 569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Submitted Solution:
```
def busquedaBinaria(arr,n,item):
aux2=0
i=0
k=item
while(i!=len(arr)):
val = arr[i] + k
#print(val)
encontrado = False
primero = 0
ultimo = len(arr) - 1
#print("entro en while1")
while primero <= ultimo:
#print("entro en while2")
puntoMedio = (primero + ultimo) // 2
if arr[puntoMedio] == val:
#print("lo encontro")
encontrado = True
break
else:
if item < arr[puntoMedio]:
ultimo = puntoMedio - 1
else:
primero = puntoMedio + 1
if (encontrado==True and (i+2)!=(k)):
encontrado=False
posicion =abs(puntoMedio - i - 1)
#print(puntoMedio)
#print(posicion)
aux2 += posicion*(posicion + 1) // 2
#print(aux2)
#print("")
i += 1
k = item
elif((encontrado== False) and (i+2)!=(k)):
k-=1
else:
k=item
i+=1
return aux2
arr=[]
n,k = list(map(int,input().strip().split()))
arr = list(map(int,input().strip().split()))
print(busquedaBinaria(arr,n,k))
``` | instruction | 0 | 285 | 23 | 570 |
No | output | 1 | 285 | 23 | 571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
* Each piece should contain at least l numbers.
* The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
Input
The first line contains three space-separated integers n, s, l (1 β€ n β€ 105, 0 β€ s β€ 109, 1 β€ l β€ 105).
The second line contains n integers ai separated by spaces ( - 109 β€ ai β€ 109).
Output
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
Examples
Input
7 2 2
1 3 1 2 4 1 2
Output
3
Input
7 2 2
1 100 1 100 1 100 1
Output
-1
Note
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
Submitted Solution:
```
n, s, l = map(int, input().split(' '))
L = list(map(int, input().split(' ')))
ans = 1
i = l
maxi = max(L[:l])
mini = min(L[:l])
left = 0
right = i
if maxi - mini > s :
ans = -1
else :
while i < n:
new = L[i]
i+= 1
right += 1
maxi = max(maxi, new)
mini = min(mini, new)
if maxi - mini > s :
if left+l > i :
ans = -1
break
ans += 1
new_left = i
maxi = new
mini = new
while new_left != left+l :
new_left -= 1
pmaxi = maxi
pmini = mini
maxi = max(L[new_left], maxi)
mini = min(L[new_left], mini)
if maxi - mini > s :
new_left += 1
maxi = pmaxi
mini = pmini
left = new_left
break
left = new_left
print(ans)
``` | instruction | 0 | 378 | 23 | 756 |
No | output | 1 | 378 | 23 | 757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
* Each piece should contain at least l numbers.
* The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
Input
The first line contains three space-separated integers n, s, l (1 β€ n β€ 105, 0 β€ s β€ 109, 1 β€ l β€ 105).
The second line contains n integers ai separated by spaces ( - 109 β€ ai β€ 109).
Output
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
Examples
Input
7 2 2
1 3 1 2 4 1 2
Output
3
Input
7 2 2
1 100 1 100 1 100 1
Output
-1
Note
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
Submitted Solution:
```
n, s, l = map(int, input().split(' '))
L = list(map(int, input().split(' ')))
ans = 1
i = l
maxi = max(L[:l])
mini = min(L[:l])
left = 0
right = i
if maxi - mini > s :
ans = -1
else :
while i < n:
new = L[i]
i+= 1
right += 1
maxi = max(maxi, new)
mini = min(mini, new)
if maxi - mini > s :
if left+l > i-1:
ans = -1
break
ans += 1
new_left = i-1
maxi = new
mini = new
while new_left != left+l :
new_left -= 1
pmaxi = maxi
pmini = mini
maxi = max(L[new_left], maxi)
mini = min(L[new_left], mini)
if maxi - mini > s :
new_left += 1
maxi = pmaxi
mini = pmini
left = new_left
break
left = new_left
print(ans)
``` | instruction | 0 | 379 | 23 | 758 |
No | output | 1 | 379 | 23 | 759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 β€ x_i, y_i β€ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
Submitted Solution:
```
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
x3, y3 = map(int, input().split())
j, k = x2-x1, y2-y1
l, m = x3-x1, y3-y1
a = ( j**2 + k**2 )**0.5
b = ( l**2 + m**2 )**0.5
c = ( (j-l)**2 + (k-m)**2 )**0.5
S = abs(j*m-k*l)/2
T = (a+b+c)/2
M = max(a, b, c)
r = S / (2*S/M + T)
print(r)
``` | instruction | 0 | 663 | 23 | 1,326 |
Yes | output | 1 | 663 | 23 | 1,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 β€ x_i, y_i β€ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
Submitted Solution:
```
a=[list(map(int,input().split()))for i in range(3)]
s,t,u=((a[0][0]-a[1][0])**2+(a[0][1]-a[1][1])**2)**0.5,((a[2][0]-a[1][0])**2+(a[2][1]-a[1][1])**2)**0.5,((a[0][0]-a[2][0])**2+(a[0][1]-a[2][1])**2)**0.5
p=(s+t+u)/2
q=(p*(p-s)*(p-t)*(p-u))**0.5
r=2*q/(s+t+u)
g=max(s,t,u)
print(g/(2+g/r))
``` | instruction | 0 | 664 | 23 | 1,328 |
Yes | output | 1 | 664 | 23 | 1,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 β€ x_i, y_i β€ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
Submitted Solution:
```
p = [[int(i) for i in input().split()] for i in range(3)]
a = []
for i in range(3): a.append((p[i][0]-p[(i+1)%3][0])**2+(p[i][1]-p[(i+1)%3][1])**2)
a.sort()
print(a[2]**0.5/
(((2*(a[0]*a[2])**0.5+a[0]+a[2]-a[1])/(2*(a[0]*a[2])**0.5-a[0]-a[2]+a[1]))**0.5+
((2*(a[1]*a[2])**0.5+a[1]+a[2]-a[0])/(2*(a[1]*a[2])**0.5-a[1]-a[2]+a[0]))**0.5+2))
``` | instruction | 0 | 666 | 23 | 1,332 |
Yes | output | 1 | 666 | 23 | 1,333 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 β€ x_i, y_i β€ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
Submitted Solution:
```
def euclid(a,b,c,d):
return ( (a-c)**2 + (b-d)**2 )**0.5
def triangle_area(p1,p2,p3):
dx1, dx2 = p3[0] - p1[0], p2[0] - p1[0]
dy1, dy2 = p3[1] - p1[1], p2[1] - p1[1]
return dy2*dx1 - dy1*dx2
x1, y1 = [ int(v) for v in input().split() ]
x2, y2 = [ int(v) for v in input().split() ]
x3, y3 = [ int(v) for v in input().split() ]
a = euclid(x1, y1, x2, y2)
b = euclid(x2, y2, x3, y3)
c = euclid(x3, y3, x1, y1)
l = a + b + c
s = triangle_area((x1,y1),(x2,y2),(x3,y3))
ar = s / ( l + (2*s / a) )
br = s / ( l + (2*s / b) )
cr = s / ( l + (2*s / c) )
print(max(ar,br,cr))
``` | instruction | 0 | 667 | 23 | 1,334 |
No | output | 1 | 667 | 23 | 1,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 β€ x_i, y_i β€ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
Submitted Solution:
```
def norm(x1, y1, x2, y2):
return ((x1-x2)**2 + (y1-y2)**2)**0.5
def d(a, b, c, x, y):
return abs(a*x + b*y + c) / (a**2 + b**2)**0.5
def points2line(x1, y1, x2, y2):
la = y1 - y2
lb = x2 - x1
lc = x1 * (y2 - y1) + y1 * (x1 - x2)
return la, lb, lc
#from numpy import argmax
def argmax(L):
return max(enumerate(L), key=lambda x: x[1])[0]
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
x3, y3 = map(int, input().split())
a = norm(x3, y3, x2, y2)
b = norm(x1, y1, x3, y3)
c = norm(x1, y1, x2, y2)
xc = (a*x1 + b*x2 + c*x3)/(a+b+c)
yc = (a*y1 + b*y2 + c*y3)/(a+b+c)
iw = argmax([a, b, c])
w = [a, b, c][int(iw)]
L = [[x1, y1], [x2, y2], [x3, y3]]
del L[int(iw)]
h = d(*points2line(*L[0], *L[1]), xc, yc)
print(w*h/(2*h + w))
``` | instruction | 0 | 668 | 23 | 1,336 |
No | output | 1 | 668 | 23 | 1,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 β€ x_i, y_i β€ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
Submitted Solution:
```
ai = lambda: list(map(int, input().split()))
import numpy as np
import math
def calc(A,B,C):
a = np.linalg.norm(B - C)
b = np.linalg.norm(C - A)
c = np.linalg.norm(A - B)
cosC = np.inner(A-C, B-C)/(b*a)
if cosC <= 0:
return 0
tanC = math.sqrt(1/(cosC**2)-1)
return (1 - 1/cosC + tanC) * (a+b-c)/4
a = np.array(ai())
b = np.array(ai())
c = np.array(ai())
print(max(calc(a,b,c),calc(b,c,a),calc(c,a,b)))
``` | instruction | 0 | 669 | 23 | 1,338 |
No | output | 1 | 669 | 23 | 1,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).
He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.
Constraints
* 0 β€ x_i, y_i β€ 1000
* The coordinates are integers.
* The three points are not on the same line.
Input
The input is given from Standard Input in the following format:
x_1 y_1
x_2 y_2
x_3 y_3
Output
Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.
Examples
Input
0 0
1 1
2 0
Output
0.292893218813
Input
3 1
1 5
4 9
Output
0.889055514217
Submitted Solution:
```
import numpy as np
import math
def calc(A,B,C):
a = np.linalg.norm(B - C)
b = np.linalg.norm(C - A)
c = np.linalg.norm(A - B)
cosC = np.inner(A-C, B-C)/(b*a)
if cosC <= 0:
return 0
tanC = math.sqrt(1/(cosC**2)-1)
return (1 - 1/cosC + tanC) * (a+b-c)/4
a = np.array(ai())
b = np.array(ai())
c = np.array(ai())
print(max(calc(a,b,c),calc(b,c,a),calc(c,a,b)))
``` | instruction | 0 | 670 | 23 | 1,340 |
No | output | 1 | 670 | 23 | 1,341 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 885 | 23 | 1,770 |
Tags: dp, math
Correct Solution:
```
n = int(input())
if n%2==0:
if n%4==0:
print(4**(n//4))
else:
print((4**(n//4))*2)
else:
print(0)
``` | output | 1 | 885 | 23 | 1,771 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 886 | 23 | 1,772 |
Tags: dp, math
Correct Solution:
```
n = eval(input())
if n % 2 != 0:
print(0)
else:
print(int(2**(n/2)))
``` | output | 1 | 886 | 23 | 1,773 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 887 | 23 | 1,774 |
Tags: dp, math
Correct Solution:
```
n = int(input())
print( ( 1 << ( n // 2 ) if n & 1 == 0 else 0 ))
``` | output | 1 | 887 | 23 | 1,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 888 | 23 | 1,776 |
Tags: dp, math
Correct Solution:
```
n = int(input())
if n % 2 == 1:
print(0)
else:
print((int(2 ** (n/2))))
``` | output | 1 | 888 | 23 | 1,777 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 889 | 23 | 1,778 |
Tags: dp, math
Correct Solution:
```
t = int(input())
if t%2==0: print(2**(t//2))
else: print(0)
``` | output | 1 | 889 | 23 | 1,779 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 890 | 23 | 1,780 |
Tags: dp, math
Correct Solution:
```
n=int(input())
if n&1==1:
print(0)
else:
print(int(pow(2,n/2)))
``` | output | 1 | 890 | 23 | 1,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 891 | 23 | 1,782 |
Tags: dp, math
Correct Solution:
```
dim = int(input())
print(2 ** (dim // 2) if dim % 2 == 0 else 0)
``` | output | 1 | 891 | 23 | 1,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles. | instruction | 0 | 892 | 23 | 1,784 |
Tags: dp, math
Correct Solution:
```
n = int(input())
if n % 2 == 0:
print(round(2**(n / 2)))
else:
print(0)
``` | output | 1 | 892 | 23 | 1,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
nn=int(input())
print(0) if nn%2!=0 else print(int(pow(2,nn/2)))
``` | instruction | 0 | 893 | 23 | 1,786 |
Yes | output | 1 | 893 | 23 | 1,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
def scand():
return int(input())
def scana():
return [int(x) for x in input().split()]
def solve(x):
if x%2:
return 0
return int(2**(x//2))
n=scand()
print(solve(n))
``` | instruction | 0 | 894 | 23 | 1,788 |
Yes | output | 1 | 894 | 23 | 1,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
n = int(input())
if n%2 == 0:
print(2**(3*n//6))
else:
print(0)
``` | instruction | 0 | 895 | 23 | 1,790 |
Yes | output | 1 | 895 | 23 | 1,791 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
# ///==========Libraries, Constants and Functions=============///
#mkraghav
import sys
inf = float("inf")
mod = 1000000007
def get_array(): return list(map(int, sys.stdin.readline().split()))
def get_ints(): return map(int, sys.stdin.readline().split())
def input(): return sys.stdin.readline()
def int1():return int(input())
import string
import math
from itertools import combinations
# ///==========MAIN=============///
def main():
n=int1()
if n==1:
print(0)
else:
if n%2==0:
print(pow(2,n//2))
else:
print(0)
if __name__ == "__main__":
main()
``` | instruction | 0 | 896 | 23 | 1,792 |
Yes | output | 1 | 896 | 23 | 1,793 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
n=int(input())
print(n*int(n%2==0))
``` | instruction | 0 | 897 | 23 | 1,794 |
No | output | 1 | 897 | 23 | 1,795 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
# code by RAJ BHAVSAR
n = int(input())
if(n == 1):
print(0)
else:
print(n)
``` | instruction | 0 | 898 | 23 | 1,796 |
No | output | 1 | 898 | 23 | 1,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
n = int(input())
print(2**(n//2))
``` | instruction | 0 | 899 | 23 | 1,798 |
No | output | 1 | 899 | 23 | 1,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a given integer n. Find the number of ways to fill all 3 Γ n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
<image> This picture describes when n = 4. The left one is the shape and the right one is 3 Γ n tiles.
Input
The only line contains one integer n (1 β€ n β€ 60) β the length.
Output
Print the number of ways to fill.
Examples
Input
4
Output
4
Input
1
Output
0
Note
In the first example, there are 4 possible cases of filling.
In the second example, you cannot fill the shapes in 3 Γ 1 tiles.
Submitted Solution:
```
n = int(input())
print(n//4)
``` | instruction | 0 | 900 | 23 | 1,800 |
No | output | 1 | 900 | 23 | 1,801 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob has a rectangular chocolate bar of the size W Γ H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
Input
The first line contains 3 integers W, H and n (1 β€ W, H, n β€ 100) β width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β coordinates of the endpoints of the i-th break (0 β€ xi, 1 β€ xi, 2 β€ W, 0 β€ yi, 1 β€ yi, 2 β€ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output
Output n + 1 numbers β areas of the resulting parts in the increasing order.
Examples
Input
2 2 2
1 0 1 2
0 1 1 1
Output
1 1 2
Input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
Output
1 1 1 1
Input
2 4 2
0 1 2 1
0 3 2 3
Output
2 2 4 | instruction | 0 | 1,157 | 23 | 2,314 |
Tags: dfs and similar, implementation
Correct Solution:
```
w,h,n=list(map(int,input().split()))
a=[[0 for i in range(2*w-1)] for j in range(2*h-1)]
for i in range(1,2*h-1,2):
for j in range(1,2*w-1,2):
a[i][j]=' '
for i in range(n):
x1,y1,x2,y2=list(map(int,input().split()))
if x1==x2:
if x1!=0 and x1!=w:
for j in range(min(y1,y2),max(y1,y2)):
a[2*h-2-2*(j)][2*x1-1]=' '
else:
if y1!=0 and y1!=h:
for j in range(min(x1,x2),max(x1,x2)):
a[2*h-1-2*y1][2*j]=' '
b=[]
c=1
for i in range(0,2*h-1,2):
for j in range(0,2*w-1,2):
if a[i][j]==0:
d=i
e=j
while d<2*h-1 and a[d][e]==0 and a[d-1][e]!=' ' or d==i:
d+=2
d-=2
while e<2*w-1 and a[d][e]==0 and a[d][e-1]!=' ' or e==j:
e+=2
e-=2
b.append(((e-j)//2+1)*((d-i)//2+1))
for k in range(i,d+1,2):
for l in range(j,e+1,2):
a[k][l]=c
c+=1
b+=[0]*(n+1-len(b))
print(*sorted(b))
``` | output | 1 | 1,157 | 23 | 2,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob has a rectangular chocolate bar of the size W Γ H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
Input
The first line contains 3 integers W, H and n (1 β€ W, H, n β€ 100) β width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β coordinates of the endpoints of the i-th break (0 β€ xi, 1 β€ xi, 2 β€ W, 0 β€ yi, 1 β€ yi, 2 β€ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output
Output n + 1 numbers β areas of the resulting parts in the increasing order.
Examples
Input
2 2 2
1 0 1 2
0 1 1 1
Output
1 1 2
Input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
Output
1 1 1 1
Input
2 4 2
0 1 2 1
0 3 2 3
Output
2 2 4 | instruction | 0 | 1,158 | 23 | 2,316 |
Tags: dfs and similar, implementation
Correct Solution:
```
w, h, n = map(int, input().split(' '))
hblock = [[False for i in range(w)] for i in range(h)]
vblock = [[False for i in range(w)] for i in range(h)]
for i in range(n):
x1, y1, x2, y2 = map(int, input().split(' '))
if x1 == x2:
for j in range(y1, y2):
hblock[j][x1-1] = True
else:
for j in range(x1, x2):
vblock[y1-1][j] = True
areas = []
vis = [[False for i in range(w)] for i in range(h)]
for i in range(h):
for j in range(w):
if vis[i][j]:
continue
width = j
while width < w and not hblock[i][width]:
width += 1
height = i
while height < h and not vblock[height][j]:
height += 1
width = min(w - 1, width) - j + 1
height = min(h - 1, height) - i + 1
areas.append(width * height)
for p in range(height):
for q in range(width):
vis[i + p][j + q] = True
areas.sort()
print(' '.join(map(str, areas)))
``` | output | 1 | 1,158 | 23 | 2,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob has a rectangular chocolate bar of the size W Γ H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
Input
The first line contains 3 integers W, H and n (1 β€ W, H, n β€ 100) β width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β coordinates of the endpoints of the i-th break (0 β€ xi, 1 β€ xi, 2 β€ W, 0 β€ yi, 1 β€ yi, 2 β€ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output
Output n + 1 numbers β areas of the resulting parts in the increasing order.
Examples
Input
2 2 2
1 0 1 2
0 1 1 1
Output
1 1 2
Input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
Output
1 1 1 1
Input
2 4 2
0 1 2 1
0 3 2 3
Output
2 2 4 | instruction | 0 | 1,159 | 23 | 2,318 |
Tags: dfs and similar, implementation
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
w, h, n = map(int, input().split())
mat = [[0] * (2 * w) for _ in range(2 * h)]
for x1, y1, x2, y2 in (map(int, input().split()) for _ in range(n)):
if x1 == x2:
for y in range(2 * y1, 2 * y2):
mat[y][2 * x1 - 1] = 1
else:
for x in range(2 * x1, 2 * x2):
mat[2 * y1 - 1][x] = 1
ans = []
for i in range(0, 2 * h, 2):
for j in range(0, 2 * w, 2):
if mat[i][j]:
continue
mat[i][j] = 1
size = 1
stack = [(i, j)]
while stack:
y, x = stack.pop()
for dy, dx in ((1, 0), (-1, 0), (0, 1), (0, -1)):
if 0 <= y + dy * 2 < 2 * h and 0 <= x + dx * 2 < 2 * w and mat[y + dy][x + dx] == 0 and mat[y + dy * 2][x + dx * 2] == 0:
mat[y + dy * 2][x + dx * 2] = 1
size += 1
stack.append((y + dy * 2, x + dx * 2))
ans.append(size)
print(*sorted(ans))
``` | output | 1 | 1,159 | 23 | 2,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob has a rectangular chocolate bar of the size W Γ H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
Input
The first line contains 3 integers W, H and n (1 β€ W, H, n β€ 100) β width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β coordinates of the endpoints of the i-th break (0 β€ xi, 1 β€ xi, 2 β€ W, 0 β€ yi, 1 β€ yi, 2 β€ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output
Output n + 1 numbers β areas of the resulting parts in the increasing order.
Examples
Input
2 2 2
1 0 1 2
0 1 1 1
Output
1 1 2
Input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
Output
1 1 1 1
Input
2 4 2
0 1 2 1
0 3 2 3
Output
2 2 4 | instruction | 0 | 1,160 | 23 | 2,320 |
Tags: dfs and similar, implementation
Correct Solution:
```
WHn = [[0,0]]
# WHn[0].extend(list(map(int, input().split(' '))))
WHn[0].extend([int(x) for x in input().split(' ')])
n = range(WHn[0][-1])
lines = []
lines_aux = []
for i in n:
lines.append( [int(x) for x in input().split(' ')] )
lines_aux.append(True)
while any(lines_aux):
for i in n:
if lines_aux[i]:
for unidad in WHn:
rangex = range(unidad[0], unidad[2]+1)
rangey = range(unidad[1], unidad[3]+1)
if (lines[i][0] in rangex) and (lines[i][3] in rangey) and (lines[i][2] in rangex) and (lines[i][1] in rangey) and\
( (lines[i][0:3:2] == unidad[0:3:2]) or (lines[i][1:4:2] == unidad[1:4:2]) ):
WHn.append([unidad[0],unidad[1],lines[i][2],lines[i][3]])
WHn.append([lines[i][0],lines[i][1],unidad[2],unidad[3]])
WHn.remove(unidad)
lines_aux[i] = False
break
for i,unidad in enumerate(WHn):
WHn[i] = (unidad[2] - unidad[0])*(unidad[3] - unidad[1])
WHn.sort()
result = ''
for i in WHn:
result += '{} '
print(result[:-1].format(*WHn))
``` | output | 1 | 1,160 | 23 | 2,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob has a rectangular chocolate bar of the size W Γ H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
Input
The first line contains 3 integers W, H and n (1 β€ W, H, n β€ 100) β width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β coordinates of the endpoints of the i-th break (0 β€ xi, 1 β€ xi, 2 β€ W, 0 β€ yi, 1 β€ yi, 2 β€ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output
Output n + 1 numbers β areas of the resulting parts in the increasing order.
Examples
Input
2 2 2
1 0 1 2
0 1 1 1
Output
1 1 2
Input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
Output
1 1 1 1
Input
2 4 2
0 1 2 1
0 3 2 3
Output
2 2 4
Submitted Solution:
```
WHn = [[0,0]]
# WHn[0].extend(list(map(int, input().split(' '))))
WHn[0].extend([int(x) for x in input().split(' ')])
n = WHn[0][-1]
for i in range(n):
# lines = list(map(int, input().split(' ')))
lines = [int(x) for x in input().split(' ')]
for unidad in WHn:
rangex = range(unidad[0], unidad[2]+1)
rangey = range(unidad[1], unidad[3]+1)
if (lines[0] in rangex) and (lines[3] in rangey) and\
(lines[2] in rangex) and (lines[1] in rangey):
WHn.append([unidad[0],unidad[1],lines[2],lines[3]])
WHn.append([lines[0],lines[1],unidad[2],unidad[3]])
WHn.remove(unidad)
break
for i,unidad in enumerate(WHn):
WHn[i] = (unidad[2] - unidad[0])*(unidad[3] - unidad[1])
WHn.sort()
print(WHn)
``` | instruction | 0 | 1,161 | 23 | 2,322 |
No | output | 1 | 1,161 | 23 | 2,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob has a rectangular chocolate bar of the size W Γ H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
Input
The first line contains 3 integers W, H and n (1 β€ W, H, n β€ 100) β width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β coordinates of the endpoints of the i-th break (0 β€ xi, 1 β€ xi, 2 β€ W, 0 β€ yi, 1 β€ yi, 2 β€ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output
Output n + 1 numbers β areas of the resulting parts in the increasing order.
Examples
Input
2 2 2
1 0 1 2
0 1 1 1
Output
1 1 2
Input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
Output
1 1 1 1
Input
2 4 2
0 1 2 1
0 3 2 3
Output
2 2 4
Submitted Solution:
```
w,h,n=list(map(int,input().split()))
a=[[0 for i in range(2*w-1)] for j in range(2*h-1)]
for i in range(1,2*h-1,2):
for j in range(1,2*w-1,2):
a[i][j]=' '
for i in range(n):
x1,y1,x2,y2=list(map(int,input().split()))
if x1==x2:
if x1!=0 and x1!=w:
for j in range(max(1,min(y1,y2)),max(y1,y2)+1):
a[2*h-2-2*(j-1)][2*x1-1]=' '
else:
if y1!=0 and y1!=h:
for j in range(min(x1,x2),max(x1,x2)):
a[2*h-1-2*y1][2*j]=' '
b=[]
c=1
for i in range(0,2*h-1,2):
for j in range(0,2*w-1,2):
if a[i][j]==0:
d=i
e=j
while d<2*h-1 and a[d][e]==0 and a[d-1][e]!=' ' or d==i:
d+=2
d-=2
while e<2*w-1 and a[d][e]==0 and a[d][e-1]!=' ' or e==j:
e+=2
e-=2
b.append(((e-j)//2+1)*((d-i)//2+1))
for k in range(i,d+1,2):
for l in range(j,e+1,2):
a[k][l]=c
c+=1
b+=[0]*(n+1-len(b))
print(*sorted(b))
``` | instruction | 0 | 1,162 | 23 | 2,324 |
No | output | 1 | 1,162 | 23 | 2,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob has a rectangular chocolate bar of the size W Γ H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
Input
The first line contains 3 integers W, H and n (1 β€ W, H, n β€ 100) β width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β coordinates of the endpoints of the i-th break (0 β€ xi, 1 β€ xi, 2 β€ W, 0 β€ yi, 1 β€ yi, 2 β€ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order.
It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output
Output n + 1 numbers β areas of the resulting parts in the increasing order.
Examples
Input
2 2 2
1 0 1 2
0 1 1 1
Output
1 1 2
Input
2 2 3
1 0 1 2
0 1 1 1
1 1 2 1
Output
1 1 1 1
Input
2 4 2
0 1 2 1
0 3 2 3
Output
2 2 4
Submitted Solution:
```
WHn = [[0,0]]
# WHn[0].extend(list(map(int, input().split(' '))))
WHn[0].extend([int(x) for x in input().split(' ')])
n = WHn[0][-1]
for i in range(n):
# lines = list(map(int, input().split(' ')))
lines = [int(x) for x in input().split(' ')]
for unidad in WHn:
rangex = range(unidad[0], unidad[2]+1)
rangey = range(unidad[1], unidad[3]+1)
if (lines[0] in rangex) and (lines[3] in rangey) and\
(lines[2] in rangex) and (lines[1] in rangey):
WHn.append([unidad[0],unidad[1],lines[2],lines[3]])
WHn.append([lines[0],lines[1],unidad[2],unidad[3]])
WHn.remove(unidad)
break
for i,unidad in enumerate(WHn):
WHn[i] = (unidad[2] - unidad[0])*(unidad[3] - unidad[1])
WHn.sort()
result = ""
for i in WHn:
result+="{} "
print(result[:-1].format(*WHn))
``` | instruction | 0 | 1,163 | 23 | 2,326 |
No | output | 1 | 1,163 | 23 | 2,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roland loves growing flowers. He has recently grown a beautiful rose at point (0, 0) of the Cartesian coordinate system. The rose is so beautiful that Roland is afraid that the evil forces can try and steal it.
To protect the rose, Roland wants to build n watch towers. Let's assume that a tower is a point on the plane at the distance of at most r from the rose. Besides, Roland assumes that the towers should be built at points with integer coordinates and the sum of squares of distances between all pairs of towers must be as large as possible. Note, that Roland may build several towers at the same point, also he may build some of them at point (0, 0).
Help Roland build the towers at the integer points so that the sum of squares of distances between all towers is maximum possible. Note that the distance in this problem is defined as the Euclidian distance between points.
Input
The first line contains two integers, n and r (2 β€ n β€ 8; 1 β€ r β€ 30).
Output
In the first line print an integer β the maximum possible sum of squared distances. In the i-th of the following n lines print two integers, xi, yi β the coordinates of the i-th tower. Each tower must be inside or on the border of the circle with radius r. Note that there may be several towers located at the same point of the plane, also some towers can be located at point (0, 0).
If there are multiple valid optimal arrangements, choose any of them.
Examples
Input
4 1
Output
16
0 1
0 1
0 -1
0 -1
Input
3 6
Output
312
0 6
5 -3
-5 -3
Submitted Solution:
```
from math import cos, sin, pi, floor
from copy import deepcopy
n, r = map(int, input().split(' '))
r2 = r * r
def score(cs):
res = 0
for i in range(n):
for j in range(i + 1, n):
x1, y1 = cs[i]
x2, y2 = cs[j]
res += (x2 - x1) ** 2 + (y2 - y1) ** 2
return res
def check(cs):
for x, y in cs:
if x * x + y * y > r2:
return False
return True
def checkP(p):
x, y = p
if x * x + y * y > r2:
return False
return True
cs = []
step = 2 * pi / n
for i in range(n):
x = r * cos(step * i)
y = r * sin(step * i)
cs += [[floor(x), floor(y)]]
res = 0
resV = []
def brute(level):
global cs, res, resV
if level >= n:
if check(cs):
curScore = score(cs)
if curScore > res:
res = curScore
resV = deepcopy(cs)
return
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
cs[level][0] += dx
cs[level][1] += dy
if checkP(cs[level]):
brute(level + 1)
cs[level][1] -= dy
cs[level][0] -= dx
#brute(level + 1)
#cs[level][0] += 1
#brute(level + 1)
#cs[level][1] += 1
#brute(level + 1)
#cs[level][0] -= 1
#brute(level + 1)
#cs[level][1] -= 1
brute(0)
print(res)
for pair in resV:
print(*pair)
``` | instruction | 0 | 1,244 | 23 | 2,488 |
No | output | 1 | 1,244 | 23 | 2,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roland loves growing flowers. He has recently grown a beautiful rose at point (0, 0) of the Cartesian coordinate system. The rose is so beautiful that Roland is afraid that the evil forces can try and steal it.
To protect the rose, Roland wants to build n watch towers. Let's assume that a tower is a point on the plane at the distance of at most r from the rose. Besides, Roland assumes that the towers should be built at points with integer coordinates and the sum of squares of distances between all pairs of towers must be as large as possible. Note, that Roland may build several towers at the same point, also he may build some of them at point (0, 0).
Help Roland build the towers at the integer points so that the sum of squares of distances between all towers is maximum possible. Note that the distance in this problem is defined as the Euclidian distance between points.
Input
The first line contains two integers, n and r (2 β€ n β€ 8; 1 β€ r β€ 30).
Output
In the first line print an integer β the maximum possible sum of squared distances. In the i-th of the following n lines print two integers, xi, yi β the coordinates of the i-th tower. Each tower must be inside or on the border of the circle with radius r. Note that there may be several towers located at the same point of the plane, also some towers can be located at point (0, 0).
If there are multiple valid optimal arrangements, choose any of them.
Examples
Input
4 1
Output
16
0 1
0 1
0 -1
0 -1
Input
3 6
Output
312
0 6
5 -3
-5 -3
Submitted Solution:
```
from math import cos, sin, pi, floor
from copy import deepcopy
n, r = map(int, input().split(' '))
r2 = r * r
def score(cs):
res = 0
for i in range(n):
for j in range(i + 1, n):
x1, y1 = cs[i]
x2, y2 = cs[j]
res += (x2 - x1) ** 2 + (y2 - y1) ** 2
return res
def check(cs):
for x, y in cs:
if x * x + y * y > r2:
return False
return True
def checkP(p):
x, y = p
if x * x + y * y > r2:
return False
return True
res = 0
resV = []
def brute(level):
global cs, res, resV
if level >= n:
if check(cs):
curScore = score(cs)
if curScore > res:
res = curScore
resV = deepcopy(cs)
return
for dx in [0, 1]:
for dy in [0, 1]:
cs[level][0] += dx
cs[level][1] += dy
if checkP(cs[level]):
brute(level + 1)
cs[level][1] -= dy
cs[level][0] -= dx
cs = []
stepBase = 2 * pi / n
for baseCount in range(n // 2):
step = stepBase * (baseCount + 1)
for i in range(n):
x = r * cos(step * i)
y = r * sin(step * i)
cs += [[floor(x), floor(y)]]
brute(0)
print(res)
for pair in resV:
print(*pair)
``` | instruction | 0 | 1,245 | 23 | 2,490 |
No | output | 1 | 1,245 | 23 | 2,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roland loves growing flowers. He has recently grown a beautiful rose at point (0, 0) of the Cartesian coordinate system. The rose is so beautiful that Roland is afraid that the evil forces can try and steal it.
To protect the rose, Roland wants to build n watch towers. Let's assume that a tower is a point on the plane at the distance of at most r from the rose. Besides, Roland assumes that the towers should be built at points with integer coordinates and the sum of squares of distances between all pairs of towers must be as large as possible. Note, that Roland may build several towers at the same point, also he may build some of them at point (0, 0).
Help Roland build the towers at the integer points so that the sum of squares of distances between all towers is maximum possible. Note that the distance in this problem is defined as the Euclidian distance between points.
Input
The first line contains two integers, n and r (2 β€ n β€ 8; 1 β€ r β€ 30).
Output
In the first line print an integer β the maximum possible sum of squared distances. In the i-th of the following n lines print two integers, xi, yi β the coordinates of the i-th tower. Each tower must be inside or on the border of the circle with radius r. Note that there may be several towers located at the same point of the plane, also some towers can be located at point (0, 0).
If there are multiple valid optimal arrangements, choose any of them.
Examples
Input
4 1
Output
16
0 1
0 1
0 -1
0 -1
Input
3 6
Output
312
0 6
5 -3
-5 -3
Submitted Solution:
```
from math import cos, sin, pi, floor
from copy import deepcopy
n, r = map(int, input().split(' '))
r2 = r * r
def score(cs):
res = 0
for i in range(n):
for j in range(i + 1, n):
x1, y1 = cs[i]
x2, y2 = cs[j]
res += (x2 - x1) ** 2 + (y2 - y1) ** 2
return res
def check(cs):
for x, y in cs:
if x * x + y * y > r2:
return False
return True
def checkP(p):
x, y = p
if x * x + y * y > r2:
return False
return True
res = 0
resV = []
def brute(level):
global cs, res, resV
if level >= n:
if check(cs):
curScore = score(cs)
if curScore > res:
res = curScore
resV = deepcopy(cs)
return
for dx in [0, 1]:
for dy in [0, 1]:
cs[level][0] += dx
cs[level][1] += dy
if checkP(cs[level]):
brute(level + 1)
cs[level][1] -= dy
cs[level][0] -= dx
cs = []
stepBase = 2 * pi / n
for baseCount in range(n // 2):
step = stepBase * (baseCount + 1)
cs = []
for i in range(n):
x = r * cos(step * i)
y = r * sin(step * i)
cs += [[floor(x), floor(y)]]
brute(0)
print(res)
for pair in resV:
print(*pair)
``` | instruction | 0 | 1,246 | 23 | 2,492 |
No | output | 1 | 1,246 | 23 | 2,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roland loves growing flowers. He has recently grown a beautiful rose at point (0, 0) of the Cartesian coordinate system. The rose is so beautiful that Roland is afraid that the evil forces can try and steal it.
To protect the rose, Roland wants to build n watch towers. Let's assume that a tower is a point on the plane at the distance of at most r from the rose. Besides, Roland assumes that the towers should be built at points with integer coordinates and the sum of squares of distances between all pairs of towers must be as large as possible. Note, that Roland may build several towers at the same point, also he may build some of them at point (0, 0).
Help Roland build the towers at the integer points so that the sum of squares of distances between all towers is maximum possible. Note that the distance in this problem is defined as the Euclidian distance between points.
Input
The first line contains two integers, n and r (2 β€ n β€ 8; 1 β€ r β€ 30).
Output
In the first line print an integer β the maximum possible sum of squared distances. In the i-th of the following n lines print two integers, xi, yi β the coordinates of the i-th tower. Each tower must be inside or on the border of the circle with radius r. Note that there may be several towers located at the same point of the plane, also some towers can be located at point (0, 0).
If there are multiple valid optimal arrangements, choose any of them.
Examples
Input
4 1
Output
16
0 1
0 1
0 -1
0 -1
Input
3 6
Output
312
0 6
5 -3
-5 -3
Submitted Solution:
```
from math import cos, sin, pi, floor
from copy import deepcopy
n, r = map(int, input().split(' '))
r2 = r * r
def score(cs):
res = 0
for i in range(n):
for j in range(i + 1, n):
x1, y1 = cs[i]
x2, y2 = cs[j]
res += (x2 - x1) ** 2 + (y2 - y1) ** 2
return res
def check(cs):
for x, y in cs:
if x * x + y * y > r2:
return False
return True
cs = []
step = 2 * pi / n
for i in range(n):
x = r * cos(step * i)
y = r * sin(step * i)
cs += [[floor(x), floor(y)]]
res = 0
resV = []
def brute(level):
global cs, res, resV
if level >= n:
if check(cs):
curScore = score(cs)
if curScore > res:
res = curScore
resV = deepcopy(cs)
return
brute(level + 1)
cs[level][0] += 1
brute(level + 1)
cs[level][1] += 1
brute(level + 1)
cs[level][0] -= 1
brute(level + 1)
cs[level][1] -= 1
brute(0)
print(res)
for pair in cs:
print(*pair)
``` | instruction | 0 | 1,247 | 23 | 2,494 |
No | output | 1 | 1,247 | 23 | 2,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.
Count the number of distinct points with integer coordinates, which are covered by at least one segment.
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of segments.
Each of the next n lines contains four integers Ax_i, Ay_i, Bx_i, By_i (-10^6 β€ Ax_i, Ay_i, Bx_i, By_i β€ 10^6) β the coordinates of the endpoints A, B (A β B) of the i-th segment.
It is guaranteed that no two segments lie on the same line.
Output
Print a single integer β the number of distinct points with integer coordinates, which are covered by at least one segment.
Examples
Input
9
0 0 4 4
-1 5 4 0
4 0 4 4
5 2 11 2
6 1 6 7
5 6 11 6
10 1 10 7
7 0 9 8
10 -1 11 -1
Output
42
Input
4
-1 2 1 2
-1 0 1 0
-1 0 0 3
0 3 1 0
Output
7
Note
The image for the first example:
<image>
Several key points are marked blue, the answer contains some non-marked points as well.
The image for the second example:
<image> | instruction | 0 | 1,656 | 23 | 3,312 |
Tags: fft, geometry, number theory
Correct Solution:
```
"""
1036E : Covered Points
Borrows primarily from : https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
"""
def gcd(a, b):
return a if b == 0 else gcd(b, a % b)
def cross(x1, y1, x2, y2):
return x1 * y2 - x2 * y1
def online(line, x, y):
""" If the x and y are within the range of min and max of x and y's respectively
then the points MAY lie on the line
"""
a, b, c, d = line
if min(a, c) <= x <= max(a, c) and min(b, d) <= y <= max(b, d):
return True
return False
def CF1036E():
N = int(input())
lines = []
count = 0
# Read input line segment and find the lines covered by each line segment
for _ in range(N):
x1, y1, x2, y2 = map(int, input().split())
count += gcd(abs(x1 - x2), abs(y1 - y2)) + 1
lines.append((x1, y1, x2, y2))
# Deal with the intersecting points
for i in range(N):
d = set() # Unique intersecting points
for j in range(i+1, N):
px, py, qx, qy = lines[i]
rx, ry, sx, sy = lines[j]
vecx = (px - qx, rx - sx)
vecy = (py - qy, ry - sy)
# Cross of two lines
area = cross(vecx[0], vecx[1], vecy[0], vecy[1])
# Parallel line has no intersecting points
if area == 0: continue
# Computation of the exact point
# This has been referenced from : https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
lineA = cross(px, py, qx, qy)
lineB = cross(rx, ry, sx, sy)
x = cross(lineA, lineB, vecx[0], vecx[1]) / area
y = cross(lineA, lineB, vecy[0], vecy[1]) / area
# Verify the points are good.
# If the points are integers and lie of the lines they are valid.
if not (x % 1 == 0 and y % 1 == 0): continue
if not (online(lines[i], x, y) and online(lines[j], x, y)): continue
d.add((x, y))
count -= len(d)
return count
if __name__ == '__main__':
res = CF1036E()
print(res)
``` | output | 1 | 1,656 | 23 | 3,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.
Count the number of distinct points with integer coordinates, which are covered by at least one segment.
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of segments.
Each of the next n lines contains four integers Ax_i, Ay_i, Bx_i, By_i (-10^6 β€ Ax_i, Ay_i, Bx_i, By_i β€ 10^6) β the coordinates of the endpoints A, B (A β B) of the i-th segment.
It is guaranteed that no two segments lie on the same line.
Output
Print a single integer β the number of distinct points with integer coordinates, which are covered by at least one segment.
Examples
Input
9
0 0 4 4
-1 5 4 0
4 0 4 4
5 2 11 2
6 1 6 7
5 6 11 6
10 1 10 7
7 0 9 8
10 -1 11 -1
Output
42
Input
4
-1 2 1 2
-1 0 1 0
-1 0 0 3
0 3 1 0
Output
7
Note
The image for the first example:
<image>
Several key points are marked blue, the answer contains some non-marked points as well.
The image for the second example:
<image> | instruction | 0 | 1,657 | 23 | 3,314 |
Tags: fft, geometry, number theory
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
class Point(object):
COUNTER_CLOCKWISE = 1
CLOCKWISE = -1
ONLINE_BACK = 2
ONLINE_FRONT = -2
ON_SEGMENT = 0
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __add__(self, other: 'Point'):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other: 'Point'):
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, k):
return Point(self.x * k, self.y * k)
def __repr__(self):
return f'x = {self.x}, y = {self.y}'
def norm(self):
return self.x**2 + self.y**2
def dot(self, other: 'Point'):
return self.x * other.x + self.y * other.y
def cross(self, other: 'Point'):
return self.x * other.y - self.y * other.x
def ccw(self, p1: 'Point', p2: 'Point'):
vector_a = p1 - self
vector_b = p2 - self
prod = vector_a.cross(vector_b)
if prod > 0:
return self.COUNTER_CLOCKWISE
elif prod < 0:
return self.CLOCKWISE
elif vector_a.dot(vector_b) < 0:
return self.ONLINE_BACK
elif vector_a.norm() < vector_b.norm():
return self.ONLINE_FRONT
else:
return self.ON_SEGMENT
class Segment(object):
def __init__(self, p1: Point, p2: Point):
self.p1 = p1
self.p2 = p2
def is_intersected(self, other: 'Segment'):
return (
self.p1.ccw(self.p2, other.p1) * self.p1.ccw(self.p2, other.p2) <= 0
and other.p1.ccw(other.p2, self.p1) * other.p1.ccw(other.p2, self.p2) <= 0
)
if __name__ == '__main__':
from math import gcd
from collections import Counter
n = int(input())
ans = 0
segments = []
for _ in range(n):
ax, ay, bx, by = map(int, input().split())
segments.append(Segment(Point(ax, ay), Point(bx, by)))
g = gcd(abs(ax - bx), abs(ay - by))
ans += g + 1
triangle = {(i * (i + 1)) >> 1: i for i in range(n + 1)}
intersection = Counter()
for i in range(n):
for j in range(i + 1, n):
if segments[i].is_intersected(segments[j]):
base = segments[j].p2 - segments[j].p1
d1 = abs(base.cross(segments[i].p1 - segments[j].p1))
d2 = abs(base.cross(segments[i].p2 - segments[j].p1))
p2 = segments[i].p2 - segments[i].p1
if (p2.x * d1) % (d1 + d2) == 0 and (p2.y * d1) % (d1 + d2) == 0:
x = segments[i].p1.x + (p2.x * d1) // (d1 + d2)
y = segments[i].p1.y + (p2.y * d1) // (d1 + d2)
intersection[x, y] += 1
ans -= sum(triangle[v] for v in intersection.values())
print(ans)
``` | output | 1 | 1,657 | 23 | 3,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.
Count the number of distinct points with integer coordinates, which are covered by at least one segment.
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of segments.
Each of the next n lines contains four integers Ax_i, Ay_i, Bx_i, By_i (-10^6 β€ Ax_i, Ay_i, Bx_i, By_i β€ 10^6) β the coordinates of the endpoints A, B (A β B) of the i-th segment.
It is guaranteed that no two segments lie on the same line.
Output
Print a single integer β the number of distinct points with integer coordinates, which are covered by at least one segment.
Examples
Input
9
0 0 4 4
-1 5 4 0
4 0 4 4
5 2 11 2
6 1 6 7
5 6 11 6
10 1 10 7
7 0 9 8
10 -1 11 -1
Output
42
Input
4
-1 2 1 2
-1 0 1 0
-1 0 0 3
0 3 1 0
Output
7
Note
The image for the first example:
<image>
Several key points are marked blue, the answer contains some non-marked points as well.
The image for the second example:
<image> | instruction | 0 | 1,658 | 23 | 3,316 |
Tags: fft, geometry, number theory
Correct Solution:
```
import math
def intersectSeg( pt1, pt2, ptA, ptB, interge=False):
""" this returns the intersection of two segment (pt1,pt2) and (ptA,ptB)
returns a tuple: (xi, yi, valid, r, s), where
(xi, yi) is the intersection
r is the scalar multiple such that (xi,yi) = pt1 + r*(pt2-pt1)
s is the scalar multiple such that (xi,yi) = pt1 + s*(ptB-ptA)
valid == 0 if there are 0 or inf. intersections (invalid)
valid == 1 if it has a unique intersection ON the segment """
def is_interge(x):
if abs(round(x) - x) < 0.0000001:
return True
return False
DET_TOLERANCE = 0.00000001
# the first line is pt1 + r*(pt2-pt1)
# in component form:
x1, y1 = pt1; x2, y2 = pt2
dx1 = x2 - x1; dy1 = y2 - y1
# the second line is ptA + s*(ptB-ptA)
x, y = ptA; xB, yB = ptB;
dx = xB - x; dy = yB - y;
# we need to find the (typically unique) values of r and s
# that will satisfy
#
# (x1, y1) + r(dx1, dy1) = (x, y) + s(dx, dy)
#
# which is the same as
#
# [ dx1 -dx ][ r ] = [ x-x1 ]
# [ dy1 -dy ][ s ] = [ y-y1 ]
#
# whose solution is
#
# [ r ] = _1_ [ -dy dx ] [ x-x1 ]
# [ s ] = DET [ -dy1 dx1 ] [ y-y1 ]
#
# where DET = (-dx1 * dy + dy1 * dx)
#
# if DET is too small, they're parallel
#
DET = (-dx1 * dy + dy1 * dx)
if math.fabs(DET) < DET_TOLERANCE: return (0,0,0,0,0)
# now, the determinant should be OK
DETinv = 1.0/DET
# find the scalar amount along the "self" segment
r = DETinv * (-dy * (x-x1) + dx * (y-y1))
# find the scalar amount along the input line
s = DETinv * (-dy1 * (x-x1) + dx1 * (y-y1))
# return the average of the two descriptions
xi = (x1 + r*dx1 + x + s*dx)/2.0
yi = (y1 + r*dy1 + y + s*dy)/2.0
if interge == True and (is_interge(xi)==False or is_interge(yi)==False):
return (0,0,0,0,0)
xi, yi = round(xi), round(yi)
valid = 1
if xi < max(min(x1,x2), min(x, xB)) or xi > min(max(x1,x2), max(x,xB)) or \
yi < max(min(y1,y2), min(y, yB)) or yi > min(max(y1,y2), max(y,yB)):
return (0,0,0,0,0)
return ( xi, yi, valid, r, s )
def gcd(a, b):
if b == 0:
return a
return gcd(b, a%b)
n = int(input())
seg = [list(map(int, input().split())) for _ in range(n)]
cnt = 0
for x1, y1, x2, y2 in seg:
cnt += gcd(abs(x1-x2), abs(y1-y2)) + 1
sub = 0
for i in range(n):
d = set()
for j in range(i+1):
x, y, valid, _, _ = intersectSeg(seg[i][:2], seg[i][2:], seg[j][:2], seg[j][2:], interge=True)
if valid == 0: continue
if (x, y) not in d:
d.add((x, y))
sub += len(d)
print(cnt-sub)
``` | output | 1 | 1,658 | 23 | 3,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.
Count the number of distinct points with integer coordinates, which are covered by at least one segment.
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of segments.
Each of the next n lines contains four integers Ax_i, Ay_i, Bx_i, By_i (-10^6 β€ Ax_i, Ay_i, Bx_i, By_i β€ 10^6) β the coordinates of the endpoints A, B (A β B) of the i-th segment.
It is guaranteed that no two segments lie on the same line.
Output
Print a single integer β the number of distinct points with integer coordinates, which are covered by at least one segment.
Examples
Input
9
0 0 4 4
-1 5 4 0
4 0 4 4
5 2 11 2
6 1 6 7
5 6 11 6
10 1 10 7
7 0 9 8
10 -1 11 -1
Output
42
Input
4
-1 2 1 2
-1 0 1 0
-1 0 0 3
0 3 1 0
Output
7
Note
The image for the first example:
<image>
Several key points are marked blue, the answer contains some non-marked points as well.
The image for the second example:
<image> | instruction | 0 | 1,659 | 23 | 3,318 |
Tags: fft, geometry, number theory
Correct Solution:
```
'''
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
'''
def gcd(a, b):
return a if b == 0 else gcd(b, a % b)
def cross(x1, y1, x2, y2):
return x1 * y2 - x2 * y1
def online(line, x, y):
a, b, c, d = line
if min(a, c) <= x <= max(a, c) and min(b, d) <= y <= max(b, d):
return True
else:
return False
def CF1036E():
N = int(input())
lines = []
count = 0
# Find the lines covered by each line segment
for _ in range(N):
x1, y1, x2, y2 = map(int, input().split())
count += gcd(abs(x1 - x2), abs(y1 - y2)) + 1
lines.append((x1, y1, x2, y2))
# Deal with the intersecting points
for i in range(N):
d = set()
for j in range(i+1, N):
px, py, qx, qy = lines[i]
rx, ry, sx, sy = lines[j]
line1p = (px - qx, rx - sx)
line2p = (py - qy, ry - sy)
# Cross of two lines
area = cross(line1p[0], line1p[1], line2p[0], line2p[1])
# Parallel line has no intersection
if area == 0: continue
lineA = cross(px, py, qx, qy)
lineB = cross(rx, ry, sx, sy)
x = cross(lineA, lineB, line1p[0], line1p[1]) / area
y = cross(lineA, lineB, line2p[0], line2p[1]) / area
# Verify the points are good
if not (x % 1 == 0 and y % 1 == 0): continue
if not (online(lines[i], x, y) and online(lines[j], x, y)): continue
d.add((x, y))
count -= len(d)
return count
if __name__ == '__main__':
res = CF1036E()
print(res)
``` | output | 1 | 1,659 | 23 | 3,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.
Count the number of distinct points with integer coordinates, which are covered by at least one segment.
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of segments.
Each of the next n lines contains four integers Ax_i, Ay_i, Bx_i, By_i (-10^6 β€ Ax_i, Ay_i, Bx_i, By_i β€ 10^6) β the coordinates of the endpoints A, B (A β B) of the i-th segment.
It is guaranteed that no two segments lie on the same line.
Output
Print a single integer β the number of distinct points with integer coordinates, which are covered by at least one segment.
Examples
Input
9
0 0 4 4
-1 5 4 0
4 0 4 4
5 2 11 2
6 1 6 7
5 6 11 6
10 1 10 7
7 0 9 8
10 -1 11 -1
Output
42
Input
4
-1 2 1 2
-1 0 1 0
-1 0 0 3
0 3 1 0
Output
7
Note
The image for the first example:
<image>
Several key points are marked blue, the answer contains some non-marked points as well.
The image for the second example:
<image> | instruction | 0 | 1,660 | 23 | 3,320 |
Tags: fft, geometry, number theory
Correct Solution:
```
#
import sys
def getIntList():
return list(map(int, input().split()))
N, = getIntList()
zp = []
for i in range(N):
ax, ay, bx, by = getIntList()
if ax>bx:
ax,bx = bx,ax
ay,by = by, ay
zp.append( (ax,ay, bx,by))
res = 0
def gcd(a,b):
if b==0:return a
return gcd(b, a%b)
zgcd = []
for i in range(N):
ax, ay, bx, by = zp[i]
tx = abs(bx-ax)
ty = abs(by - ay)
g = gcd(tx, ty)
res += g+1
zgcd .append(g)
"""
ax + k1 dax = bx + k2 dbx
ay + k1 day = by + k2 dby
"""
for i in range(N):
ax = zp[i][0]
dax = (zp[i][2] - ax) // zgcd[i]
ay = zp[i][1]
day = (zp[i][3] - ay) // zgcd[i]
cross = []
for j in range(i+1, N):
#dprint('node',i,j)
bx = zp[j][0]
dbx = (zp[j][2] - bx) // zgcd[j]
by = zp[j][1]
dby = (zp[j][3] - by) // zgcd[j]
#dprint(ax,dax,ay,day)
#dprint(bx,dbx,by,dby)
t1 = ax * day - ay * dax - bx * day + by * dax
t2 = dbx *day - dby * dax
#dprint(t1,t2)
if t2==0:
continue
if t1%t2!=0:
continue
k2 = t1 // t2
if k2 <0 or k2 > zgcd[j]:
continue
if dax!=0:
t3 = k2*dbx + bx - ax
if t3%dax!=0:
continue
k1 = t3//dax
else:
t3 = k2* dby + by - ay
if t3%day !=0:
continue
k1 = t3//day
if k1<0 or k1 > zgcd[i]:
continue
#dprint(ax + k1 * dax, ay+k1 * day)
cross.append(k1)
if not cross: continue
cross.sort()
d = 1
for j in range(1, len(cross)):
if cross[j]!=cross[j-1]:
d+=1
res-=d
print(res)
``` | output | 1 | 1,660 | 23 | 3,321 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.
Count the number of distinct points with integer coordinates, which are covered by at least one segment.
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of segments.
Each of the next n lines contains four integers Ax_i, Ay_i, Bx_i, By_i (-10^6 β€ Ax_i, Ay_i, Bx_i, By_i β€ 10^6) β the coordinates of the endpoints A, B (A β B) of the i-th segment.
It is guaranteed that no two segments lie on the same line.
Output
Print a single integer β the number of distinct points with integer coordinates, which are covered by at least one segment.
Examples
Input
9
0 0 4 4
-1 5 4 0
4 0 4 4
5 2 11 2
6 1 6 7
5 6 11 6
10 1 10 7
7 0 9 8
10 -1 11 -1
Output
42
Input
4
-1 2 1 2
-1 0 1 0
-1 0 0 3
0 3 1 0
Output
7
Note
The image for the first example:
<image>
Several key points are marked blue, the answer contains some non-marked points as well.
The image for the second example:
<image> | instruction | 0 | 1,661 | 23 | 3,322 |
Tags: fft, geometry, number theory
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 18 10:09:57 2018
@author: a.teffal
Chalenge : Covered Points
"""
from math import gcd
def covered_points(xa, ya, xb, yb):
'''
assumes all parameters are integers
Returns the covered points by the segement A-B having integer
coordinates
'''
#this just to have A in the left and B in the right
if xb < xa :
temp_x = xa
xa = xb
xb = temp_x
temp_y = ya
ya = yb
yb = temp_y
y_0 = abs(yb - ya)
x_0 = xb - xa
#pgdc_y_x = gcd(y_0, x_0)
return gcd(y_0, x_0) + 1
def intersection2(xa, ya, xb, yb, xc, yc, xd, yd):
if max(xa, xb) < min(xc,xd):
return ()
if max(ya, yb) < min(yc,yd):
return ()
# if both A-B and C - D ara parallel to x-axis
# then no intersection (it is garanted that no segments lie on the same line)
if (xa == xb and xc == xd) or (ya == yb and yc == yd):
return ()
if ya == yb and yc == yd:
return ()
a1 = yb - ya
b1 = xb - xa
#c1 = xa*(yb - ya) - ya*(xb - xa)
c1 = xa*a1 - ya*b1
a2 = yd - yc
b2 = xd - xc
#c2 = xc*(yd - yc) - yc*(xd - xc)
c2 = xc*a2 - yc*b2
det = a1 * b2 - a2 * b1
if det == 0:
return ()
detx = c1 * b2 - c2 * b1
dety = -a1 * c2 + a2 * c1
if (detx % det) != 0 or (dety % det) !=0 :
return ()
x = int(detx/det)
y = int(dety/det)
if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or x > max(xc, xd) :
return ()
if y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or y > max(yc, yd) :
return ()
return (x, y)
if __name__ == "__main__":
#number of segments
n = int(input())
#initiate lists of point coordinates
Ax = [0]*n
Ay = [0]*n
Bx = [0]*n
By = [0]*n
n_cov = 0
intersections = {}
#counting covered by each segment
for i in range(n):
line = input().split(sep = ' ')
Ax[i] = int(line[0])
Ay[i] = int(line[1])
Bx[i] = int(line[2])
By[i] = int(line[3])
n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i])
#substructing reapted points (intersection)
for i in range(n):
for j in range(i+1, n):
temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j])
if len(temp)==0:
continue
if temp in intersections:
intersections[temp].append(i)
intersections[temp].append(j)
else:
intersections[temp] = [i, j]
for i in intersections:
n_cov = n_cov - len(set(intersections[i])) + 1
print(n_cov)
``` | output | 1 | 1,661 | 23 | 3,323 |
End of preview. Expand
in Data Studio
- Downloads last month
- 7