message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s. | instruction | 0 | 71,698 | 1 | 143,396 |
Tags: brute force, math
Correct Solution:
```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
"""
549 D
"""
from math import gcd
n, k = map(int, input().split())
a, b = map(int, input().split())
nk = n*k
cand1 = (gcd(nk, abs(i*k+b-a)) for i in range(n))
cand2 = (gcd(nk, abs(i*k-b-a)) for i in range(n))
cand3 = (gcd(nk, abs(i*k+b+a)) for i in range(n))
cand4 = (gcd(nk, abs(i*k-b+a)) for i in range(n))
ma = max(max(cand1), max(cand2), max(cand3), max(cand4))
cand1 = (gcd(nk, abs(i*k+b-a)) for i in range(n))
cand2 = (gcd(nk, abs(i*k-b-a)) for i in range(n))
cand3 = (gcd(nk, abs(i*k+b+a)) for i in range(n))
cand4 = (gcd(nk, abs(i*k-b+a)) for i in range(n))
mi = min(min(cand1), min(cand2), min(cand3), min(cand4))
print(nk//ma, nk//mi)
``` | output | 1 | 71,698 | 1 | 143,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s. | instruction | 0 | 71,699 | 1 | 143,398 |
Tags: brute force, math
Correct Solution:
```
from math import gcd
n,k=map(int,input().split())
a,b=map(int,input().split())
r=[]
i=1
p=n*k
while i<=n*k:
r.append(i)
i+=k
s=1+a
c=[]
for i in r:
c.append((i-b)%p)
c.append((i+b)%p)
mx=0
mn=1111111111111111111111111
for i in c:
dp=i-s
dp%=p
if not dp:ns=1
else:ns=p//gcd(p,dp)
mx=max(mx,ns)
mn=min(mn,ns)
print(mn,mx)
``` | output | 1 | 71,699 | 1 | 143,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s. | instruction | 0 | 71,700 | 1 | 143,400 |
Tags: brute force, math
Correct Solution:
```
from math import *
N, K = map(int, input().split())
A, B = map(int, input().split())
S = [A, K - A]
E = []
for i in range(N):
if i * K + B >= 0:
E += [i * K + B]
if i * K - B >= 0:
E += [i * K - B]
l = set()
for e in E:
l.add(abs(e - S[0]))
l.add(abs(e - S[1]))
mi, ma = None, None
f = N * K
#print(l)
for i in l:
rounds = f // gcd(i, f)
if mi == None:
mi = rounds
ma = rounds
else:
mi = min(mi, rounds)
ma = max(ma, rounds)
print(mi, end=' ')
print(ma)
'''
2 3
1 1
outputCopy
1 6
inputCopy
3 2
0 0
outputCopy
1 3
inputCopy
1 10
5 3
outputCopy
5 5
'''
``` | output | 1 | 71,700 | 1 | 143,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s. | instruction | 0 | 71,701 | 1 | 143,402 |
Tags: brute force, math
Correct Solution:
```
from math import gcd
n, k = map(int, input().split())
a, b = map(int, input().split())
x, y = int(1e15), -1
for i in range(1, n + 1):
for v in a - b, a + b, b - a, - a - b:
l = k * i + v % k
g = gcd(n * k, l)
x = min(x, n * k // g)
y = max(y, n * k // g)
print(x, y)
``` | output | 1 | 71,701 | 1 | 143,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s. | instruction | 0 | 71,702 | 1 | 143,404 |
Tags: brute force, math
Correct Solution:
```
from math import gcd
n, k = map(int, input().split())
a, b = map(int, input().split())
mn = float("inf")
mx = 0
def process(x):
global mn, mx
x %= n*k
pa = a
pb = (n*k-a) % (n*k)
da = (x+n*k - pa) % (n*k)
db = (x+n*k - pb) % (n*k)
if da == 0:
da = n*k
if db == 0:
db = n*k
aa = n*k//gcd(n*k, da)
ab = n*k//gcd(n*k, db)
mn = min(mn, aa)
mn = min(mn, ab)
mx = max(mx, aa)
mx = max(mx, ab)
for i in range(n):
process(i*k+b)
process((i+1)*k-b)
print(mn, mx)
``` | output | 1 | 71,702 | 1 | 143,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s. | instruction | 0 | 71,703 | 1 | 143,406 |
Tags: brute force, math
Correct Solution:
```
def gcd(a,b):
if(b == 0):
return a
else:
return gcd(b,a%b)
n,k = map(int,input().split())
a,b = map(int,input().split())
l = [b-a,-a-b]
g = []
for i in range(n):
for j in range(2):
u = k*i + l[j]
m1 = gcd(u,n*k)
g.append(m1)
#print(g)
max1 = min(g)
max2 = n*k//max1
min1 = max(g)
min2 = n*k//min1
if(a == b):
min2 = 1
if(a == 0 and b == 0):
max2 = n
print(min2,max2)
``` | output | 1 | 71,703 | 1 | 143,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
from math import gcd
n, k = map(int, input().split())
a, b = map(int, input().split())
res = set()
for i in range(100000):
res.add(n * k // gcd(a + b + k * i, n * k))
res.add(n * k // gcd(a - b + k * i, n * k))
print(min(res), max(res))
``` | instruction | 0 | 71,704 | 1 | 143,408 |
Yes | output | 1 | 71,704 | 1 | 143,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
import sys
from math import gcd
line = sys.stdin.readline().strip().split()
n = int(line[0])
k = int(line[1])
line = sys.stdin.readline().strip().split()
a = int(line[0])
b = int(line[1])
n1 = n
k1 = k
ans = 1
x = a + b
g1 = gcd(x, k)
k = k // g1
x = x // g1
g2 = gcd(n, k)
while g2 > 1:
n = n // g2
ans = ans * g2
g2 = gcd(n, k)
m1 = ans * k
m2 = ans * k * n
n = n1
k = k1
ans = 1
x = k - a + b
g1 = gcd(x, k)
k = k // g1
x = x // g1
g2 = gcd(n, k)
while g2 > 1:
n = n // g2
ans = ans * g2
g2 = gcd(n, k)
m3 = ans * k
m4 = ans * k * n
print(str(min([m1,m3])) + " " + str(max([m2,m4])))
``` | instruction | 0 | 71,705 | 1 | 143,410 |
Yes | output | 1 | 71,705 | 1 | 143,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
import math
n, k = map(int, input().split())
a, b = map(int, input().split())
mx = -1e18
mn = 1e18
for i in range(0, n):
x1 = math.gcd(n * k, i * k + k - a + b)
x2 = math.gcd(n * k, i * k + k - a - b)
x3 = math.gcd(n * k, i * k + k + a - b)
x4 = math.gcd(n * k, i * k + k + a + b)
mx = max(mx, max(x1, max(x2, max(x3, x4))))
mn = min(mn, min(x1, min(x2, min(x3, x4))))
print(str(n * k // mx) + " " + str(n * k // mn))
``` | instruction | 0 | 71,706 | 1 | 143,412 |
Yes | output | 1 | 71,706 | 1 | 143,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
import sys
from math import gcd
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))
n,k = I()
a,b = I()
if 0:
print(1,n*k)
else:
i=j=0;mi,ma=10**12,0
while i<=n*k:
l=abs(a-i+b)
st = (n*k)//gcd(l,n*k)
ma=max(ma,st);mi = min(mi,st)
i+=k
l=abs(a-i-b)
st = (n*k)//gcd(l,n*k)
ma=max(ma,st);mi = min(mi,st)
j+=k
print(mi,ma)
``` | instruction | 0 | 71,707 | 1 | 143,414 |
Yes | output | 1 | 71,707 | 1 | 143,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
from math import gcd
n, k = map(int, input().split())
a, b = map(int, input().split())
tot = n*k
mn, mx = 100000, 0
for ai in a, -a:
for ni in range(n):
for bi in b, -b:
pos1 = ai
pos2 = k*ni + bi
if pos1 == pos2:
x = 1
else:
x = tot // gcd(tot, abs(pos1 - pos2))
mn = min(mn, x)
mx = max(mx, x)
print(mn, mx)
``` | instruction | 0 | 71,708 | 1 | 143,416 |
No | output | 1 | 71,708 | 1 | 143,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
from math import gcd
n, k = map(int, input().split())
a, b = map(int, input().split())
x, y = n * k, 0
for l in range(1, int((n * k) ** 0.5) + 2):
d = gcd((n * k), l)
for kek in a + b, a - b, b - a, -a-b:
if l % k == kek % k:
x = min(x, n * k // d)
y = max(y, n * k // d)
l2 = (n * k) // d
for kek in a + b, a - b, b - a, -a-b:
if l2 % k == kek % k:
x = min(x, d)
y = max(y, d)
print(x, y)
``` | instruction | 0 | 71,709 | 1 | 143,418 |
No | output | 1 | 71,709 | 1 | 143,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
from math import gcd
n, k = map(int, input().split())
a, b = map(int, input().split())
x, y = n * k, 1
for l in range(1, int((n * k) ** 0.5) + 2):
d = gcd((n * k), l)
for kek in a + b, a - b, b - a, -a-b:
if l % k == kek % k:
x = min(x, n * k // d)
y = max(y, n * k // d)
l2 = (n * k) // d
for kek in a + b, a - b, b - a, -a-b:
if l2 % k == kek % k:
x = min(x, d)
y = max(y, d)
print(x, y)
``` | instruction | 0 | 71,710 | 1 | 143,420 |
No | output | 1 | 71,710 | 1 | 143,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through n β
k cities. The cities are numerated from 1 to n β
k, the distance between the neighboring cities is exactly 1 km.
Sergey does not like beetles, he loves burgers. Fortunately for him, there are n fast food restaurants on the circle, they are located in the 1-st, the (k + 1)-st, the (2k + 1)-st, and so on, the ((n-1)k + 1)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is k km.
Sergey began his journey at some city s and traveled along the circle, making stops at cities each l km (l > 0), until he stopped in s once again. Sergey then forgot numbers s and l, but he remembers that the distance from the city s to the nearest fast food restaurant was a km, and the distance from the city he stopped at after traveling the first l km from s to the nearest fast food restaurant was b km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.
Now Sergey is interested in two integers. The first integer x is the minimum number of stops (excluding the first) Sergey could have done before returning to s. The second integer y is the maximum number of stops (excluding the first) Sergey could have done before returning to s.
Input
The first line contains two integers n and k (1 β€ n, k β€ 100 000) β the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.
The second line contains two integers a and b (0 β€ a, b β€ k/2) β the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.
Output
Print the two integers x and y.
Examples
Input
2 3
1 1
Output
1 6
Input
3 2
0 0
Output
1 3
Input
1 10
5 3
Output
5 5
Note
In the first example the restaurants are located in the cities 1 and 4, the initial city s could be 2, 3, 5, or 6. The next city Sergey stopped at could also be at cities 2, 3, 5, 6. Let's loop through all possible combinations of these cities. If both s and the city of the first stop are at the city 2 (for example, l = 6), then Sergey is at s after the first stop already, so x = 1. In other pairs Sergey needs 1, 2, 3, or 6 stops to return to s, so y = 6.
In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so l is 2, 4, or 6. Thus x = 1, y = 3.
In the third example there is only one restaurant, so the possible locations of s and the first stop are: (6, 8) and (6, 4). For the first option l = 2, for the second l = 8. In both cases Sergey needs x=y=5 stops to go to s.
Submitted Solution:
```
n, k = map(int, input().split())
a, b = map(int, input().split())
nk = n*k
def gcd(a,b):
if b == 0:
return a
return gcd(b,a%b)
x = 10**18
y = 0
for z in range(1,n+1):
d = z*k + a + b
gn = gcd(nk, d)
x = min(x, nk//gn)
y = max(y, nk//gn)
d = z*k - a + b
x = min(x, nk//gn)
y = max(y, nk//gn)
d = z*k + a - b
x = min(x, nk//gn)
y = max(y, nk//gn)
d = z*k - a - b
x = min(x, nk//gn)
y = max(y, nk//gn)
print(*(x, y))
``` | instruction | 0 | 71,711 | 1 | 143,422 |
No | output | 1 | 71,711 | 1 | 143,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,870 | 1 | 143,740 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
import sys
input = sys.stdin.readline
N=32
E=[]
for i in range(31,11,-1):
for j in range(i+1,33):
E.append((i,j,2**(31-i)))
L,R=map(int,input().split())
LX=L
for i in range(22):
if L+(1<<i)-1<=R:
E.append((1,32-i,L))
#print((1,32-i,L))
LX=L+(1<<i)
else:
break
E.append((1,2,1))
L=LX
now=L
X=R-L+1
while X:
for i in range(30):
if (1<<i)>X:
break
i-=1
E.append((2,32-i-1,now-1-(1<<i)))
#print(((2,32-i-1,now-1-(1<<i))))
now+=(1<<i)
X-=(1<<i)
print("YES")
print(32,len(E))
for x,y,z in E:
print(x,y,z)
``` | output | 1 | 71,870 | 1 | 143,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,871 | 1 | 143,742 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
readints = lambda: map(int, input().split(' '))
n = 32
e = []
l, r = readints()
print('YES')
if l == r:
print(2, 1)
print(1, 2, l)
exit(0)
if l == 1:
e.append([1, n, 1])
l += 1
r = r - l + 1
k = 0
while r >> k >> 1:
k += 1
for i in range(k + 1):
e.append([n - i - 1, n, l - 1])
for j in range(i):
e.append([n - i - 1, n - j - 1, 1 << j])
if r >> i & 1:
e.append([1, n - i - 1, 1 + (r >> (i + 1) << (i + 1))])
print(n, len(e))
for u, v, w in e:
print(u, v, w)
``` | output | 1 | 71,871 | 1 | 143,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,872 | 1 | 143,744 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9+7
# md = 998244353
L, R = MI()
if L==R:
print("YES")
print(2,1)
print(1,2,L)
exit()
# [L,R] to [0,R-L]+L
r = R-L
n = r.bit_length()+1
# print(n)
ans = []
for i in range(2, n+1):
c = 1 << i-2
ans.append((1, i, 1))
for j in range(i+1, n+1):
ans.append((i, j, c))
# print(ans)
c = 1 << n-2
n += 1
update=False
for i in range(n-4, -1, -1):
if r >> i & 1:
ans.append((i+2, n, c))
c += 1 << i
update=True
n += 1
if update:ans.append((n-1, n, L))
ans.append((n-2, n, L))
ans.append((1, n, L))
print("YES")
print(n, len(ans))
for i, j, c in ans: print(i, j, c)
``` | output | 1 | 71,872 | 1 | 143,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,873 | 1 | 143,746 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
import sys
l, r = map(int,input().split())
print("YES")
d = r-l+1
d -= 1
if d == 0:
print("2 1")
print("1 2 " + str(l))
sys.exit()
db = format(d, "b")
# print(db)
n = 1 + len(db) + 1
A = []
mu = 1
mm = 1
for i in range(len(db)-1,-1,-1):
# print(i,"i")
if db[i] == "1":
mmh = mm
A.append([1, i+2, mmh])
mm += mu
mmh = mu//2
pl = 1
while mmh > 0:
A.append([i+2, i+2+pl, mmh])
mmh //= 2
pl += 1
mu *= 2
for i in range(1, n):
A.append([i, n, l])
print(n, len(A))
for i in range(len(A)):
print(A[i][0],A[i][1],A[i][2])
``` | output | 1 | 71,873 | 1 | 143,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,874 | 1 | 143,748 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
import sys
from collections import Counter
readline = sys.stdin.readline
limit = 22
L, R = map(int, readline().split())
D = R-L
if not D:
print('YES')
print(2, 1)
print(1, 2, L)
else:
K = D.bit_length()
Edge = [[] for _ in range(limit+1)]
for i in range(limit):
for j in range(i):
Edge[j].append((1<<(max(0, j-1)), i))
"""
Z = 10
dp = [Counter() for _ in range(Z)]
dp[0] = Counter([0])
for i in range(Z):
for d, j in Edge[i]:
if j < Z:
for k, v in dp[i].items():
dp[j][k+d] += v
"""
geta = 0
en = limit
Edge[0].append((1, en))
for i in range(K):
if (1<<i) & D:
Edge[i+1].append((geta+1, en))
geta += (1<<i)
Ei = []
for vn in range(limit):
for c, vf in Edge[vn]:
Ei.append((vn, vf, c))
gg = 1
if L != 1:
Ei.append((-1, 0, L-1))
gg += 1
NN = 0
for vn, vf, c in Ei:
NN = max(NN, vn, vf)
print('YES')
print(NN+gg, len(Ei))
for vn, vf, c in Ei:
print(f'{vn+gg} {vf+gg} {c}')
EE = [[] for _ in range(NN+geta+1)]
for vn, vf, c in Ei:
EE[vf+gg].append((c, vn+gg))
DD = [None]*(NN+gg+1)
def calc(x):
if x == 1:
return Counter([0])
if DD[x] is not None:
return DD[x]
res = Counter()
for d, vn in EE[x]:
for ky, val in calc(vn).items():
res[d+ky] += val
DD[x] = res
return res
#print(calc(NN+gg))
``` | output | 1 | 71,874 | 1 | 143,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,875 | 1 | 143,750 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
from sys import stdin, stdout
from collections import defaultdict
import math
def main():
L, R = list(map(int, stdin.readline().split()))
stdout.write("YES\n")
X = R - L + 1
pw = 1
node_cnt = 1
ans = []
while pw * 2 <= X:
node_cnt += 1
for i in range(node_cnt - 1):
ans.append((i+1, node_cnt, pw))
pw *= 2
X -= pw
if X > 0:
sm = pw
pw = int(pw/2)
node_cnt += 1
tmp_cnt = node_cnt - 1
while pw != 0:
if X >= pw:
ans.append((tmp_cnt, node_cnt, sm - pw))
X -= pw
sm += pw
pw = int(pw / 2)
tmp_cnt -= 1
for i in range(node_cnt):
ans.append((i+1, node_cnt+1, 1))
node_cnt += 1
if L > 1:
node_cnt += 1
ans.append((node_cnt -1, node_cnt, L-1))
stdout.write(f"{node_cnt} {len(ans)}\n")
for a in ans:
stdout.write(f"{a[0]} {a[1]} {a[2]}\n")
main()
``` | output | 1 | 71,875 | 1 | 143,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,876 | 1 | 143,752 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
from collections import defaultdict
from itertools import accumulate
import sys
input = sys.stdin.readline
'''
for CASES in range(int(input())):
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
sys.stdout.write(" ".join(map(str,ANS))+"\n")
'''
inf = 100000000000000000 # 1e17
mod = 998244353
A=[]
B=[]
C=[]
l,r = map(int, input().split())
base=l
r=r-l+1
l=l-l+1
if l==r:
print("YES")
print("2 1")
print("1 2",end=" ")
print(base)
sys.exit(0)
def link(lay):
if lay==2:
A.append(1)
B.append(2)
C.append(base)
else:
link(lay-1)
A.append(1)
B.append(lay)
C.append(base)
for x in range(2,lay):
A.append(x)
B.append(lay)
C.append(1<<(x-2))
from math import log
rnode=int(log(r-1,2))+2
link(rnode)
A.append(1)
B.append(30)
C.append(base)
S=bin(r-1)[2:]
S=S[::-1]
nowbase=1
for i in range(len(S)):
if S[i]=='1':
x=i+2
A.append(x)
B.append(30)
C.append(nowbase)
nowbase+=1<<(x-2)
print("YES")
print(30,len(A))
for i in range(len(A)):
print(A[i],B[i],C[i])
``` | output | 1 | 71,876 | 1 | 143,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6. | instruction | 0 | 71,877 | 1 | 143,754 |
Tags: bitmasks, constructive algorithms
Correct Solution:
```
import sys, io, os
if os.environ['USERNAME']=='kissz':
inp=open('in2.txt','r').readline
def debug(*args):
print(*args,file=sys.stderr)
else:
inp=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def debug(*args):
pass
# SCRIPT STARTS HERE
def bits(x):
B=[]
while x:
B.append(x&1)
x>>=1
return B
L,R=map(int,inp().split())
N=R-L
roads=[]
B=bits(N)
n=len(B)+2
for i in range(2,n):
roads.append((1,i,1))
for j in range(2,i):
roads.append((j,i,2**(j-2)))
roads.append((1,n,L))
X=L
for i in range(2,n):
b=B[i-2]
if b:
roads.append((i,n,X))
X+=2**(i-2)
print('YES')
print(n,len(roads))
for road in roads:
print(*road)
``` | output | 1 | 71,877 | 1 | 143,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6.
Submitted Solution:
```
import sys
input = sys.stdin.readline
l,r=map(int,input().split())
dif=r-l+1
print("YES")
n=2
while 2**(n-2)<dif:n+=1
edges=[]
for i in range(n-2):
for j in range(i+1,n-1):
if i==0:
edges.append([1,j+1,1])
else:
edges.append([i+1,j+1,2**(i-1)])
mx=l-1
i=1
if mx==0 or dif==2**(n-2):
edges.append([1,n,mx+1])
mx+=1
dif-=1
while dif:
if dif%2:
edges.append([i+1,n,mx])
mx+=2**(i-1)
dif//=2
i+=1
print(n,len(edges))
for e in edges:
print(*e)
``` | instruction | 0 | 71,878 | 1 | 143,756 |
Yes | output | 1 | 71,878 | 1 | 143,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6.
Submitted Solution:
```
import sys, io, os
if os.environ['USERNAME']=='kissz':
inp=open('in2.txt','r').readline
def debug(*args):
print(*args,file=sys.stderr)
else:
inp=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def debug(*args):
pass
# SCRIPT STARTS HERE
def bits(x):
B=[]
while x:
B.append(x&1)
x>>=1
return B
L,R=map(int,inp().split())
N=R-L
roads=[]
n=len(bits(N))+2
for i in range(2,len(bits(N))+2):
for j in range(1,i):
roads.append((j,i,2**(i-2)))
X=L+1
roads.append((1,n,L))
for i,b in enumerate(bits(N)):
if b:
base=2**i
roads.append((i+2,n,X-base))
X+=2**i
print('YES')
print(n,len(roads))
for road in roads:
print(*road)
``` | instruction | 0 | 71,879 | 1 | 143,758 |
No | output | 1 | 71,879 | 1 | 143,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6.
Submitted Solution:
```
import sys
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9+7
# md = 998244353
l, r = MI()
d = r-l+1
n = d.bit_length()
ans = []
for i in range(2, n+1):
c = 1 << i-2
for j in range(1, i):
ans.append((j, i, c))
c = 1 << n-1
for i in range(n-2, -1, -1):
if d >> i & 1:
n += 1
for j in range(i+1, 0, -1):
ans.append((j, n, c))
c += 1 << i
n += 1
for j in range(1, n):
ans.append((j, n, l))
print("YES")
print(n, len(ans))
for i, j, c in ans: print(i, j, c)
``` | instruction | 0 | 71,880 | 1 | 143,760 |
No | output | 1 | 71,880 | 1 | 143,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6.
Submitted Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
L, R = map(int, input().split())
N = R - L + 1
ANS = []
if N < 32:
ANS.append((1, 32, L))
for i in range(1, N):
ANS.append((1, i + 1, 1))
ANS.append((i + 1, 32, L + i - 1))
else:
ANS.append((1, 2, 1))
for i in range(20):
ANS.append((i + 2, i + 3, 1))
ANS.append((i + 2, i + 3, (1 << i) + 1))
ANS.append((1, 32, L))
ANS.append((1, 25, L))
ANS.append((1, 25, L + 1))
ANS.append((25, 32, 1))
for i in range(4):
ANS.append((25 + i, 26 + i, 1 << i + 1))
ANS.append((25 + i, 26 + i, 1 << i + 2))
ANS.append((26 + i, 32, 1))
L += 31 #####
N -= 31
while N:
a = N & -N
b = a.bit_length()
# print("a =", a, b)
ANS.append((b + 1, 32, L - b))
L += a
N ^= a
# print("ANS =", ANS)
print("YES")
print(32, len(ANS))
for a, b, c in ANS:
print(a, b, c)
``` | instruction | 0 | 71,881 | 1 | 143,762 |
No | output | 1 | 71,881 | 1 | 143,763 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some time ago Homer lived in a beautiful city. There were n blocks numbered from 1 to n and m directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them.
Homer discovered that for some two numbers L and R the city was (L, R)-continuous.
The city is said to be (L, R)-continuous, if
1. all paths from block 1 to block n are of length between L and R (inclusive); and
2. for every L β€ d β€ R, there is exactly one path from block 1 to block n whose length is d.
A path from block u to block v is a sequence u = x_0 β x_1 β x_2 β ... β x_k = v, where there is a road from block x_{i-1} to block x_{i} for every 1 β€ i β€ k. The length of a path is the sum of lengths over all roads in the path. Two paths x_0 β x_1 β ... β x_k and y_0 β y_1 β ... β y_l are different, if k β l or x_i β y_i for some 0 β€ i β€ min\\{k, l\}.
After moving to another city, Homer only remembers the two special numbers L and R but forgets the numbers n and m of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than 32 (because the city was small).
As the best friend of Homer, please tell him whether it is possible to find a (L, R)-continuous city or not.
Input
The single line contains two integers L and R (1 β€ L β€ R β€ 10^6).
Output
If it is impossible to find a (L, R)-continuous city within 32 blocks, print "NO" in a single line.
Otherwise, print "YES" in the first line followed by a description of a (L, R)-continuous city.
The second line should contain two integers n (2 β€ n β€ 32) and m (1 β€ m β€ \frac {n(n-1)} 2), where n denotes the number of blocks and m denotes the number of roads.
Then m lines follow. The i-th of the m lines should contain three integers a_i, b_i (1 β€ a_i < b_i β€ n) and c_i (1 β€ c_i β€ 10^6) indicating that there is a directed road from block a_i to block b_i of length c_i.
It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every 1 β€ i < j β€ m, either a_i β a_j or b_i β b_j.
Examples
Input
1 1
Output
YES
2 1
1 2 1
Input
4 6
Output
YES
5 6
1 2 3
1 3 4
1 4 5
2 5 1
3 5 1
4 5 1
Note
In the first example there is only one path from block 1 to block n = 2, and its length is 1.
In the second example there are three paths from block 1 to block n = 5, which are 1 β 2 β 5 of length 4, 1 β 3 β 5 of length 5 and 1 β 4 β 5 of length 6.
Submitted Solution:
```
import sys
input = sys.stdin.readline
l,r=map(int,input().split())
dif=r-l+1
print("YES")
n=2
while 2**(n-2)<dif:n+=1
edges=[]
for i in range(n-2):
for j in range(i+1,n-1):
if i==0:
edges.append([1,j+1,1])
else:
edges.append([i+1,j+1,2**(i-1)])
mx=l-1
i=1
if mx==0:
edges.append([1,n,1])
mx+=1
dif-=1
while dif:
if dif%2:
edges.append([i+1,n,mx])
mx+=2**(i-1)
dif//=2
i+=1
print(n,len(edges))
for e in edges:
print(*e)
``` | instruction | 0 | 71,882 | 1 | 143,764 |
No | output | 1 | 71,882 | 1 | 143,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,067 | 1 | 144,134 |
Tags: implementation
Correct Solution:
```
n = int(input())
H1 = list(map(int, input().split()))
H2 = list(map(int, input().split()))
av = list(map(int, input().split()))
ans = []
for i in range(n - 1):
ans.append(sum(H1[:i]) + av[i] + sum(H2[i:]))
ans.append(sum(H1) + av[-1])
ans.sort()
print(ans[0] + ans[1])
``` | output | 1 | 72,067 | 1 | 144,135 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,068 | 1 | 144,136 |
Tags: implementation
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
f=sum(l1)+l2[0]
e=[f]
for i in range(n-1) :
f+=-l2[i]-l1[i]+l2[i+1]+l[i]
e.append(f)
e=sorted(e)
print(e[0]+e[1])
``` | output | 1 | 72,068 | 1 | 144,137 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,069 | 1 | 144,138 |
Tags: implementation
Correct Solution:
```
n=int(input())
f=[0]+list(map(int,input().split()))
s=list(map(int,input().split()))+[0]
a=list(map(int,input().split()))
l=sorted([sum(f[:i+1])+a[i]+sum(s[i:]) for i in range(n)])
print(l[0]+l[1])
``` | output | 1 | 72,069 | 1 | 144,139 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,070 | 1 | 144,140 |
Tags: implementation
Correct Solution:
```
def f(i):
return sum(a1[0 : i]) + sum(a2[i : ]) + b[i]
n = int(input())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
b = list(map(int, input().split()))
m1 = 0
m2 = 1
if f(m1) > f(m2):
(m1, m2) = (m2, m1)
for i in range(2, n):
if f(i) < f(m1):
(m1, m2) = (i, m1)
elif f(i) < f(m2):
m2 = i
print(f(m1) + f(m2))
``` | output | 1 | 72,070 | 1 | 144,141 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,071 | 1 | 144,142 |
Tags: implementation
Correct Solution:
```
# brute force seems viable here
def traverse(street):
a = sum(street[0])
b = sum(street[1])
paths,d,e = [a+street[2][-1],b+street[2][0]],0,0
for s in range(length-2,0,-1):
d += street[0][s]
e += street[1][s]
p = a - d + e + street[2][s]
paths.append(p)
f,s = 1000000,1000000
for p in paths:
if p < f:
s = f; f = p
elif p < s: s=p
return f+s
length = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
street = [a,b,c]
print(traverse(street))
``` | output | 1 | 72,071 | 1 | 144,143 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,072 | 1 | 144,144 |
Tags: implementation
Correct Solution:
```
n = int(input())
shop_row = [0] + list(map(int, input().split()))
house_row = list(map(int, input().split())) + [0]
perek = list(map(int, input().split()))
for i in range(len(house_row) - 2, -1, -1):
house_row[i] += house_row[i + 1]
for i in range(1, len(shop_row)):
shop_row[i] += shop_row[i - 1]
rez = [None] * n
for i in range(n):
rez[i] = house_row[i] + shop_row[i] + perek[i]
rez.sort()
if len(rez) > 1:
print(rez[0] + rez[1])
else:
print(rez[0] + rez[0])
``` | output | 1 | 72,072 | 1 | 144,145 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,073 | 1 | 144,146 |
Tags: implementation
Correct Solution:
```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright Β© 2016 missingdays <missingdays@missingdays>
#
# Distributed under terms of the MIT license.
"""
"""
n = int(input())
b = []
b.append([int(i) for i in input().split()])
b.append([int(i) for i in input().split()])
a = [int(i) for i in input().split()]
b1s = [0]
b2s = [0]
for i in range(n-1):
b1s.append(b[0][i])
b1s[i+1] += b1s[i]
b2s.insert(0, b[1][n-i-2])
b2s[0] += b2s[1]
m = 10e9
for i in range(n):
for j in range(n):
if i == j:
continue
c = a[i] + a[j] + b1s[i] + b1s[j] + b2s[i] + b2s[j]
m = min(m, c)
print(m)
``` | output | 1 | 72,073 | 1 | 144,147 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4. | instruction | 0 | 72,074 | 1 | 144,148 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = [None, None]
a[0] = list(map(int, input().split()))
a[1] = list(map(int, input().split()))
b = list(map(int, input().split()))
d = []
for p in range(n):
d.append(sum(a[0][:p]) + b[p] + sum(a[1][p:]))
print(sum(sorted(d)[:2]))
``` | output | 1 | 72,074 | 1 | 144,149 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
N=int(input())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
C=list(map(int, input().split()))
cost = [[0],[0]]
for i in range(N-1):
cost[0].append(cost[0][i]+A[i])
cost[1].append(cost[1][i]+B[N-2-i])
cost[1].reverse()
min_cost = 99999999999
min_i=-1
for i in range(N):
if min_cost > cost[0][i]+cost[1][i]+C[i]:
min_cost = cost[0][i]+cost[1][i]+C[i]
min_i = i
min_cost2 = 99999999999
for i in range(N):
if min_cost2 > cost[0][i]+cost[1][i]+C[i] and i!=min_i:
min_cost2 = cost[0][i]+cost[1][i]+C[i]
print(min_cost+min_cost2)
``` | instruction | 0 | 72,075 | 1 | 144,150 |
Yes | output | 1 | 72,075 | 1 | 144,151 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
#!/usr/bin/env python
from sys import stdin
n = int(stdin.readline())
a1 = [int(x) for x in stdin.readline().split()]
a2 = [int(x) for x in stdin.readline().split()]
b = [int(x) for x in stdin.readline().split()]
cum1 = a1[:]
cum2 = a2[:]
for i in range(1, n - 1):
cum1[i] += cum1[i-1]
for i in range(n - 3, -1, -1):
cum2[i] += cum2[i+1]
min1, min2 = 1000000, 1000000
for i in range(n):
c = b[i]
if i:
c += cum1[i-1]
if i < n-1:
c += cum2[i]
if c <= min1:
min2 = min1
min1 = c
elif c < min2:
min2 = c
print(min1 + min2)
``` | instruction | 0 | 72,076 | 1 | 144,152 |
Yes | output | 1 | 72,076 | 1 | 144,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
#CLown1331
n = int(input())
ar1 = list(map(int, input().split()))
ar2 = list(map(int, input().split()))
br = list(map(int, input().split()))
pre = [0] * (n+1)
for i in range(n-1):
pre[i+1] = ar1[i] + pre[i]
suf = ar2
suf.append(0)
suf.append(0)
for i in range(n-1,-1,-1):
suf[i] += suf[i+1]
an = []
for i in range(n):
an.append(suf[i]+pre[i]+br[i])
an.sort()
print (an[0]+an[1])
``` | instruction | 0 | 72,077 | 1 | 144,154 |
Yes | output | 1 | 72,077 | 1 | 144,155 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
n = int(input())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
b = list(map(int, input().split()))
pref1 = [0] * n
for i in range(1, n):
pref1[i] = pref1[i - 1] + a1[i - 1]
pref2 = [0] * n
for i in range(1, n):
pref2[i] = pref2[i - 1] + a2[i - 1]
post2 = [sum(a2)] * n
for i in range(n):
post2[i] -= pref2[i]
minimum = -1
for i in range(n):
for j in range(n):
if j != i:
if minimum == -1:
minimum = pref1[i] + post2[i] + b[i] + pref1[j] + post2[j] + b[j]
else:
minimum = min(pref1[i] + post2[i] + b[i] + pref1[j] + post2[j] + b[j], minimum)
print(minimum)
``` | instruction | 0 | 72,078 | 1 | 144,156 |
Yes | output | 1 | 72,078 | 1 | 144,157 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
from sys import stdin
n = stdin.readline().rstrip()
n = int(n)
first_row = [int(i) for i in stdin.readline().rstrip().split()]
second_row = [int(i) for i in stdin.readline().rstrip().split()]
main_crossing = [int(i) for i in stdin.readline().rstrip().split()]
min_1 = float('inf')
min_2 = float('inf')
for i in range(n):
first_row_time = sum(first_row[:i])
second_row_time = sum(second_row[i:])
trip_time = first_row_time + second_row_time + main_crossing[i]
updated = False
if trip_time < min_1:
min_1 = trip_time
updated = True
if not updated and trip_time < min_2:
min_2 = trip_time
print(min_1 + min_2)
``` | instruction | 0 | 72,079 | 1 | 144,158 |
No | output | 1 | 72,079 | 1 | 144,159 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
n = int(input())
a0s = list(map(int, input().split()))
a1s = list(map(int, input().split()))
bs = list(map(int, input().split()))
def solve(n, a0s, a1s, bs):
cum = 0
cum0s = [0]
for a in a0s:
cum += a
cum0s.append(cum)
cum = sum(a1s)
cum1s = [cum]
for a in a1s:
cum -= a
cum1s.append(cum)
m = a0s[0]
min0s = [m]
for a in a0s[1:]:
m = min(m, a)
min0s.append(m)
min0s.append(m)
m = a1s[-1]
min1s = [0] * n
for i, a in enumerate(a1s[::-1], 1):
m = min(m, a)
min1s[-i] = m
min1s[0] = m
record = float('inf')
for i in range(n):
d = cum0s[i] + cum1s[i] + bs[i]
for j in range(i, n):
dist = d + cum0s[j] + cum1s[j] + bs[j]
if i == j:
dist += 2 * min(min0s[i], min0s[i])
if dist < record:
record = dist
return record
print(solve(n, a0s, a1s, bs))
``` | instruction | 0 | 72,080 | 1 | 144,160 |
No | output | 1 | 72,080 | 1 | 144,161 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
n=int(input())
arr=[]
rra=[]
x=list(map(int,input().split()))
y=list(map(int,input().split()))
z=list(map(int,input().split()))
rra.append(min(z))
for i in y :
x.append(i)
for i in range(n-1) :
k=min(x)
arr.append(k)
x.remove(k)
##print(arr,rra)
print(sum(arr)*2+sum(rra)*2)
``` | instruction | 0 | 72,081 | 1 | 144,162 |
No | output | 1 | 72,081 | 1 | 144,163 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 β€ i β€ 2, 1 β€ j β€ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 β€ j β€ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
<image> Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 β€ n β€ 50) β the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer β values aij (1 β€ aij β€ 100).
The last line contains n space-separated integers bj (1 β€ bj β€ 100).
Output
Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
* Laurenty crosses the avenue, the waiting time is 3;
* Laurenty uses the second crossing in the first row, the waiting time is 2;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty uses the first crossing in the first row, the waiting time is 1;
* Laurenty crosses the avenue, the waiting time is 1;
* Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
Submitted Solution:
```
# brute force seems viable here
def traverse(street):
a = sum(street[0])
b = sum(street[1])
paths,d,e = [a+street[2][-1],b+street[2][0]],0,0
for s in range(length-2,0,-1):
d += street[0][s]
e += street[1][s]
p = a - d + e + street[2][s]
paths.append(p)
f,s = 1000000,1000000
for p in paths:
if p < f:
s = f; f = p
else: s = p
return f+s
length = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
street = [a,b,c]
print(traverse(street))
``` | instruction | 0 | 72,082 | 1 | 144,164 |
No | output | 1 | 72,082 | 1 | 144,165 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
Input
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
* "." β an empty cell;
* "*" β a cell with road works;
* "S" β the cell where Igor's home is located;
* "T" β the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
Output
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
Examples
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note
The first sample is shown on the following picture:
<image>
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
<image> | instruction | 0 | 72,201 | 1 | 144,402 |
Tags: dfs and similar, graphs, implementation, shortest paths
Correct Solution:
```
# changed
def answer(grid):
def find(type):
for i, row in enumerate(grid):
for j, c in enumerate(row):
if c == type:
return (i, j)
return (-1, -1)
home = find("S")
work = find("T")
# mark 0 turns away from work
for i in range(work[0], -1, -1):
if grid[i][work[1]] == "*":
break;
grid[i][work[1]] = "0"
for i in range(work[0], len(grid)):
if grid[i][work[1]] == "*":
break;
grid[i][work[1]] = "0"
for j in range(work[1], -1, -1):
if grid[work[0]][j] == "*":
break;
grid[work[0]][j] = "0"
for j in range(work[1], len(grid[0])):
if grid[work[0]][j] == "*":
break;
grid[work[0]][j] = "0"
# check to get to 0 turns away from work
for i in range(home[0], -1, -1):
if grid[i][home[1]] == "*":
break;
x, y = i, home[1]
for xi in range(x, -1, -1):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for xi in range(x, len(grid)):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for yi in range(y, -1, -1):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
for xi in range(y, len(grid[0])):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
for i in range(home[0], len(grid)):
if grid[i][home[1]] == "*":
break;
x, y = i, home[1]
for xi in range(x, -1, -1):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for xi in range(x, len(grid)):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for yi in range(y, -1, -1):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
for xi in range(y, len(grid[0])):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
for j in range(home[1], -1, -1):
if grid[home[0]][j] == "*":
break;
x, y = home[0], j
for xi in range(x, -1, -1):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for xi in range(x, len(grid)):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for yi in range(y, -1, -1):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
for xi in range(y, len(grid[0])):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
for j in range(home[1], len(grid[0])):
if grid[home[0]][j] == "*":
break;
x, y = home[0], j
for xi in range(x, -1, -1):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for xi in range(x, len(grid)):
if grid[xi][y] == "*":
break;
if grid[xi][y] == "0":
return "YES"
for yi in range(y, -1, -1):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
for xi in range(y, len(grid[0])):
if grid[x][yi] == "*":
break;
if grid[x][yi] == "0":
return "YES"
return "NO"
if __name__ == "__main__":
n, m = map(int, input().split())
grid = []
for _ in range(n):
grid.append(list(input()))
print(answer(grid))
``` | output | 1 | 72,201 | 1 | 144,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
Input
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
* "." β an empty cell;
* "*" β a cell with road works;
* "S" β the cell where Igor's home is located;
* "T" β the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
Output
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
Examples
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note
The first sample is shown on the following picture:
<image>
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
<image> | instruction | 0 | 72,202 | 1 | 144,404 |
Tags: dfs and similar, graphs, implementation, shortest paths
Correct Solution:
```
n, m = map(int, input().split())
mat = []
ans = -1
for i in range(n):
mat.append(input())
s, d = -1, -1
for i in range(n):
for j in range(m):
if mat[i][j]=='S':
s = (i,j)
if mat[i][j]=='T':
d = (i,j)
q = [(s[0], s[1], -1, -1)]
vis = [0]*(n*m)
pos = 0
found = False
tmp = [(0,1,1),(0,-1,1),(1,0,0),(-1,0,0)]
for a,b,c in tmp:
x,y = d
while x<n and x>=0 and y<m and y>=0 and not mat[x][y] == '*':
vis[x*m+y]=-1
x+=a
y+=b
# hori = 1 vert = 0
while pos<len(q) and not found:
i,j,turn,dir = q[pos]
pos+= 1
if vis[i*m+j]==1:
continue
vis[i*m+j]=1
if turn>=1:
continue
for a,b,c in tmp:
if not dir==c:
t = 1
x,y = i+a*t, j+b*t
while x<n and x>=0 and y<m and y>=0 and not mat[x][y] == '*':
if vis[x*m+y]==-1:
found=True
ans=turn+1
break
q.append((x, y, turn+1, c))
t+=1
x,y = i+a*t, j+b*t
if found:
break
x,y = d
print("YES" if ans>-1 and ans<3 else 'NO')
``` | output | 1 | 72,202 | 1 | 144,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
Input
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
* "." β an empty cell;
* "*" β a cell with road works;
* "S" β the cell where Igor's home is located;
* "T" β the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
Output
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
Examples
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note
The first sample is shown on the following picture:
<image>
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
<image> | instruction | 0 | 72,203 | 1 | 144,406 |
Tags: dfs and similar, graphs, implementation, shortest paths
Correct Solution:
```
n, m = map(int, input().split())
data = [list(input().strip()) for i in range(n)]
for i, v in enumerate(data):
if "S" in v:
si = i
sk = v.index("S")
break
for i, v in enumerate(data):
if "T" in v:
ti = i
tk = v.index("T")
break
yes = False
for i in range(ti, -1, -1):
if data[i][tk] == 'S':
yes = True
break
elif data[i][tk] == '*':
break
else:
data[i][tk] = '0'
for i in range(ti, n):
if data[i][tk] == 'S':
yes = True
break
elif data[i][tk] == '*':
break
else:
data[i][tk] = '0'
for k in range(tk, -1, -1):
if data[ti][k] == 'S':
yes = True
break
elif data[ti][k] == '*':
break
else:
data[ti][k] = '0'
for k in range(tk, m):
if data[ti][k] == 'S':
yes = True
break
elif data[ti][k] == '*':
break
else:
data[ti][k] = '0'
#for v in data: print(*v, sep="")
if not yes:
for i in range(si, -1, -1):
if data[i][sk] == 'T':
yes = True
break
elif data[i][sk] == '0':
yes = True
break
elif data[i][sk] == '*':
break
else:
for k in range(sk, -1, -1):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
for k in range(sk, m):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
if not yes:
for i in range(si, n):
if data[i][sk] == 'T':
yes = True
break
elif data[i][sk] == '0':
yes = True
break
elif data[i][sk] == '*':
break
else:
for k in range(sk, -1, -1):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
for k in range(sk, m):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
if not yes:
for k in range(sk, -1, -1):
if data[si][k] == 'T':
yes = True
break
elif data[si][k] == '0':
yes = True
break
elif data[si][k] == '*':
break
else:
for i in range(si, -1, -1):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
for i in range(si, n):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
if not yes:
for k in range(sk, m):
if data[si][k] == 'T':
yes = True
break
elif data[si][k] == '0':
yes = True
break
elif data[si][k] == '*':
break
else:
for i in range(si, -1, -1):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
for i in range(si, n):
if data[i][k] == 'T':
yes = True
break
elif data[i][k] == '0':
yes = True
break
elif data[i][k] == '*':
break
if yes:
print("YES")
else:
print("NO")
``` | output | 1 | 72,203 | 1 | 144,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
Input
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
* "." β an empty cell;
* "*" β a cell with road works;
* "S" β the cell where Igor's home is located;
* "T" β the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
Output
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
Examples
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note
The first sample is shown on the following picture:
<image>
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
<image> | instruction | 0 | 72,204 | 1 | 144,408 |
Tags: dfs and similar, graphs, implementation, shortest paths
Correct Solution:
```
import sys
input = sys.stdin.readline
def q(sx, sy, gx, gy):
for i in range(sx, gx+1):
for j in range(sy, gy+1):
if S[i][j]=='*':
return False
return True
n, m = map(int, input().split())
S = [input()[:-1] for _ in range(n)]
for i in range(n):
for j in range(m):
if S[i][j]=='S':
Sx, Sy = i, j
if S[i][j]=='T':
Tx, Ty = i, j
for i in range(n):
if q(min(i, Sx), Sy, max(i, Sx), Sy) and q(i, min(Sy, Ty), i, max(Sy, Ty)) and q(min(i, Tx), Ty, max(i, Tx), Ty):
print('YES')
exit()
for i in range(m):
if q(Sx, min(i, Sy), Sx, max(i, Sy)) and q(min(Sx, Tx), i, max(Sx, Tx), i) and q(Tx, min(i, Ty), Tx, max(i, Ty)):
print('YES')
exit()
print('NO')
``` | output | 1 | 72,204 | 1 | 144,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
Input
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
* "." β an empty cell;
* "*" β a cell with road works;
* "S" β the cell where Igor's home is located;
* "T" β the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
Output
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
Examples
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note
The first sample is shown on the following picture:
<image>
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
<image> | instruction | 0 | 72,205 | 1 | 144,410 |
Tags: dfs and similar, graphs, implementation, shortest paths
Correct Solution:
```
from collections import deque as dq
st=0
class Graph:
def __init__(self,n=0,m=0):
self.g=[None for i in range(n)]
self.vis=[[False for j in range(m)] for i in range(n)]
self.dx=(1,0,-1,0)
self.dy=(0,1,0,-1)
self.p=[[[10]*4 for i in range(m)] for j in range(n)]
def affiche(self):
for i in range(len(self.g)):
print("".join(self.g[i]))
def readG(self,n):
for i in range(n):
self.g[i]=list(input())
def get(self,i,j):
return self.g[i][j]
def dfsIt(self,u):
global k,A
L=dq()
for t in range(4):
self.p[u[0]][u[1]][t]=0
L.append((u[0],u[1],t))
while len(L)!=0:
p=L.pop()
for t in range(4):
x=p[0]+self.dx[t]
y=p[1]+self.dy[t]
if x>=0 and x<n and y>=0 and y<m and self.g[x][y]!='*':
a=self.p[p[0]][p[1]][p[2]]+int(t!=p[2])
if a<self.p[x][y][t]:
self.p[x][y][t]=a
else:
continue
if self.p[x][y][t]>2:
continue
if self.g[x][y]=='T':
return "YES"
L.append((x,y,t))
return "NO"
n,m=map(int,input().split())
g=Graph(n,m)
g.readG(n)
def f():
global n,m,g,st
b=0
for i in range(n):
for j in range(m):
if g.get(i,j)=='S':
st=[i,j]
return
f()
print(g.dfsIt(st))
``` | output | 1 | 72,205 | 1 | 144,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
Input
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
* "." β an empty cell;
* "*" β a cell with road works;
* "S" β the cell where Igor's home is located;
* "T" β the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
Output
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
Examples
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note
The first sample is shown on the following picture:
<image>
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
<image> | instruction | 0 | 72,206 | 1 | 144,412 |
Tags: dfs and similar, graphs, implementation, shortest paths
Correct Solution:
```
n, m = map(int, input().split())
cells = [list(input()) for _ in range(n)]
ans = 'NO'
for r in range(n):
for c in range(m):
if cells[r][c] == "S":
sr, sc = r, c
elif cells[r][c] == "T":
tr, tc = r, c
for cc in range(m):
if ans == 'YES':
break
f = True
for c in range(min(cc, sc), max(cc, sc) + 1):
if cells[sr][c] == '*':
f = False
break
for r in range(min(sr, tr), max(sr, tr) + 1):
if cells[r][cc] == '*':
f = False
break
for c in range(min(cc, tc), max(cc, tc) + 1):
if cells[tr][c] == '*':
f = False
break
if f:
ans = 'YES'
for cr in range(n):
if ans == 'YES':
break
f = True
for r in range(min(cr, sr), max(cr, sr) + 1):
if cells[r][sc] == '*':
f = False
break
for c in range(min(sc, tc), max(sc, tc) + 1):
if cells[cr][c] == '*':
f = False
break
for r in range(min(cr, tr), max(cr, tr) + 1):
if cells[r][tc] == '*':
f = False
break
if f:
ans = 'YES'
print(ans)
``` | output | 1 | 72,206 | 1 | 144,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
Input
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
* "." β an empty cell;
* "*" β a cell with road works;
* "S" β the cell where Igor's home is located;
* "T" β the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
Output
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
Examples
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note
The first sample is shown on the following picture:
<image>
In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
<image> | instruction | 0 | 72,207 | 1 | 144,414 |
Tags: dfs and similar, graphs, implementation, shortest paths
Correct Solution:
```
import sys
sys.setrecursionlimit(20000)
lines = sys.stdin.read().split('\n')[:-1]
n, m = map(int, lines[0].split(' '))
lines = lines[1:]
road = []
start = ()
finish = ()
i = 0
j = 0
for line in lines:
for j in range(m):
if line[j] == 'S':
start = (i, j)
elif line[j] == 'T':
finish = (i, j)
road.append(line)
i += 1
(f_x, f_y) = finish
X = (1, 0)
Y = (0, 1)
def next_direction(d):
if d == X:
return Y
else:
return X
def neg_direction(d): return (-d[0], -d[1])
def increase_pos(d, pos):
p = (pos[0] + d[0], pos[1] + d[1])
if p[0] < 0 or p[1] < 0 or p[0] == n or p[1] == m or road[p[0]][p[1]] == '*':
return None
else:
return p
def can_reach(dir, idx, pos_1, target):
if pos_1 == None:
return False
if pos_1[idx] == target:
return True
return can_reach(dir, idx, increase_pos(dir, pos_1), target)
def search(pos, remaining, last_direction):
distance = 2 # distance in num of coordinates
x, y = pos
if (x == f_x):
distance -= 1
if (y == f_y):
distance -= 1
free_moves = remaining - distance
if remaining < distance:
return False
if distance == 0:
return True
d = next_direction(last_direction)
if free_moves > 0:
for dir in (d, neg_direction(d)):
p = increase_pos(dir, pos)
while p != None:
if search(p, remaining - 1, d):
return True
p = increase_pos(dir, p)
return False
else:
target = None
dir = None
idx = None
if d == X:
target = (f_x, y)
dir = neg_direction(d) if f_x < x else d
idx = 0
else:
target = (x, f_y)
dir = neg_direction(d) if f_y < y else d
idx = 1
return can_reach(dir, idx, pos, target[idx]) and search(target, remaining - 1, d)
result = search(start, 3, X) or search(start, 3, Y)
def bool_to_yes_no(b): return 'YES' if b else 'NO'
print(bool_to_yes_no(result), end='')
``` | output | 1 | 72,207 | 1 | 144,415 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.