text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha's little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister.
Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother's drawing.
At first, the line going through two points, that brother drew, doesn't intersect or touch any circle.
Also, no two circles intersect or touch, and there is no pair of circles such that one circle is located inside another.
Moreover, for each circle, Masha drew a square of the minimal area with sides parallel axis such that this circle is located inside the square and noticed that there is no two squares intersect or touch and there is no pair of squares such that one square is located inside other.
Now Masha wants to draw circle of minimal possible radius such that it goes through two points that brother drew and doesn't intersect any other circle, but other circles can touch Masha's circle and can be located inside it.
It's guaranteed, that answer won't exceed 1012. It should be held for hacks as well.
Input
First line contains four integers x1, y1, x2, y2 ( - 105 ≤ x1, y1, x2, y2 ≤ 105) — coordinates of points that brother drew. First point has coordinates (x1, y1) and second point has coordinates (x2, y2). These two points are different.
The second line contains single integer n (1 ≤ n ≤ 105) — the number of circles that brother drew.
Next n lines contains descriptions of circles. Each line contains three integers xi, yi, ri ( - 105 ≤ xi, yi ≤ 105, 1 ≤ ri ≤ 105) describing circle with center (xi, yi) and radius ri.
Output
Output smallest real number, that it's possible to draw a circle with such radius through given points in such a way that it doesn't intersect other circles.
The output is considered correct if it has a relative or absolute error of at most 10 - 4.
Examples
Input
2 4 7 13
3
3 0 1
12 4 2
-4 14 2
Output
5.1478150705
Input
-2 3 10 -10
2
7 0 3
-5 -5 2
Output
9.1481831923
Note
<image> <image>
Submitted Solution:
```
import math
# from matplotlib.patches import Circle as pyC
# import matplotlib.pyplot as plt
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def copy(self):
return Point(self.x, self.y)
def unitize(self):
return self.copy() / self.length()
def length(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
def rotate(self, other):
return Point(self.x * other.x - self.y * other.y, self.x * other.y + self.y * other.x)
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, other):
if isinstance(other, Point):
return self.x * other.x + self.y * other.y
else:
return Point(self.x * other, self.y * other)
def __truediv__(self, other):
return Point(self.x / other, self.y / other)
def __repr__(self):
return self.__str__()
def __str__(self):
return "<Point x: %s, y: %s>"%(self.x, self.y)
class Circle:
def __init__(self, p, r):
self.p = p
self.r = r
def read():
return (map(int, input().split()))
def checkCircle(circle):
for c in circles:
if c.r + circle.r - 0.0001 > (c.p - circle.p).length() > abs(c.r - circle.r) + 0.0001:
return False
return True
def generateCircle(circle):
M = (circle.p - O).rotate(Point(Du.y, Du.x))
_ML = M.x ** 2 + M.y ** 2
_CR = circle.r**2
_HH = P**2 - _CR - _ML
A = 4 * M.x**2 - 4 * _CR
B = 4 * M.x * _HH + 8 * M.x * _CR
C = _HH**2 - 4 * _CR * _ML
Delta = B**2 - 4 * A * C
_Delta = math.sqrt(Delta)
X1 = Point((-B + _Delta) / 2 / A, 0)
X2 = Point((-B - _Delta) / 2 / A, 0)
R1 = (Point(0, P) - X1).length()
R2 = (Point(0, P) - X2).length()
C1 = Circle(X1.rotate(Point(Du.y, -Du.x)) + O, R1)
C2 = Circle(X2.rotate(Point(Du.y, -Du.x)) + O, R2)
# print([P, Mt, M, (R1, R2)])
# print([X1, X2])
if R1 < R2:
return X1, C1, X2, C2
else:
return X2, C2, X1, C1
def main():
simple = Circle((p1 + p2) / 2, (p1 - p2).length() / 2)
if checkCircle(simple):
return simple.r
minR = None
for circle in circles:
c1, c2 = generateCircle(circle)
# print([c1.p.x, c1.p.y, c1.r])
# print([c2.p.x, c2.p.y, c2.r])
# ax.add_patch(pyC(xy = (c1.p.x, c1.p.y), radius = c1.r, alpha = 0.5))
# ax.add_patch(pyC(xy = (c2.p.x, c2.p.y), radius = c2.r, alpha = 0.5))
if minR is None or minR > c1.r:
if checkCircle(c1):
minR = c1.r
if minR is None or minR > c2.r:
if checkCircle(c2):
minR = c2.r
return minR
class Intervals:
def __init__(self):
self.l = []
# self.add(1, 5.5)
def add(self, a, b):
t = -1
tt = -1
flag = False
for i, s in enumerate(self.l):
if t == -1:
if s[0] > a:
t = i
flag = True
elif s[0] <= a < s[1] - DURATION:
t = i
if b <= s[1]:
return
if t != -1:
if s[0] + DURATION < b <= s[1]:
tt = i
else:
break
# print([a, b])
if t == -1:
self.l.append((a, b))
else:
k = (min(self.l[i][0], a), max(self.l[tt][1], b))
if tt == -1:
if flag:
k = (k[0], b)
else:
del self.l[t:]
else:
del self.l[t:tt + 1]
self.l.insert(t, k)
# print(self.l)
def min(self):
if len(self.l) == 0:
return 10**12
elif self.l[0][0] == 0:
return self.l[0][1]
else:
return self.l[0][0]
DURATION = 0.0001
(x1, y1, x2, y2) = read()
p1 = Point(x1, y1)
p2 = Point(x2, y2)
D = p1 - p2
P = D.length() / 2
O = (p1 + p2) / 2
Du = D.unitize()
# fig = plt.figure()
# ax = fig.add_subplot(111)
circles = []
lP = Intervals()
lN = Intervals()
(n, ) = read()
for i in range(n):
cx, cy, cr = read()
# ax.add_patch(pyC(xy = (cx, cy), radius = cr, alpha = 0.5))
c = Circle(Point(cx, cy), cr)
# circles.append(c)
x1, c1, x2, c2 = generateCircle(c)
if x1.x > 0:
if x2.x < 0:
lP.add(0, c1.r)
lN.add(0, c2.r)
else:
lP.add(c1.r, c2.r)
else:
if x2.x > 0:
lP.add(0, c2.r)
lN.add(0, c1.r)
else:
lN.add(c1.r, c2.r)
# print([cx, cy, cr, c1.r, c2.r])
# print(l.l)
# print([lP.min(), lN.min()])
lP.add(0, P)
lN.add(0, P)
print(min(lP.min(), lN.min()))
# x, y = 0, 0
# ax.plot(x, y, 'ro')
# plt.show()
```
No
| 103,400 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha's little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister.
Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother's drawing.
At first, the line going through two points, that brother drew, doesn't intersect or touch any circle.
Also, no two circles intersect or touch, and there is no pair of circles such that one circle is located inside another.
Moreover, for each circle, Masha drew a square of the minimal area with sides parallel axis such that this circle is located inside the square and noticed that there is no two squares intersect or touch and there is no pair of squares such that one square is located inside other.
Now Masha wants to draw circle of minimal possible radius such that it goes through two points that brother drew and doesn't intersect any other circle, but other circles can touch Masha's circle and can be located inside it.
It's guaranteed, that answer won't exceed 1012. It should be held for hacks as well.
Input
First line contains four integers x1, y1, x2, y2 ( - 105 ≤ x1, y1, x2, y2 ≤ 105) — coordinates of points that brother drew. First point has coordinates (x1, y1) and second point has coordinates (x2, y2). These two points are different.
The second line contains single integer n (1 ≤ n ≤ 105) — the number of circles that brother drew.
Next n lines contains descriptions of circles. Each line contains three integers xi, yi, ri ( - 105 ≤ xi, yi ≤ 105, 1 ≤ ri ≤ 105) describing circle with center (xi, yi) and radius ri.
Output
Output smallest real number, that it's possible to draw a circle with such radius through given points in such a way that it doesn't intersect other circles.
The output is considered correct if it has a relative or absolute error of at most 10 - 4.
Examples
Input
2 4 7 13
3
3 0 1
12 4 2
-4 14 2
Output
5.1478150705
Input
-2 3 10 -10
2
7 0 3
-5 -5 2
Output
9.1481831923
Note
<image> <image>
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
# import matplotlib.pyplot as plt
"""
created by shhuan at 2017/11/4 01:33
"""
x1, y1, x2, y2 = map(int, input().split())
n = int(input())
C = []
for i in range(n):
C.append([int(x) for x in input().split()])
def check(X, Y, R):
for x, y, r in C:
d = math.sqrt((X-x)**2 + (Y-y)**2)
if abs(R-r) < d - 1e-5 and d + 1e-5 < R+r:
return False, (x, y, r)
return True, None
def check2(c1, c2):
if not c2:
return True
x1, y1, r1 = c1
x2, y2, r2 = c2
d = math.sqrt((x1-x2)**2 +(y1-y2)**2)
return not (abs(r1-r2) < d < r1+r2)
hi = 10**12
lo = 0
L = math.sqrt((x1-x2)**2 + (y1-y2)**2) / 2
L2 = L**2
xm, ym = (x1+x2)/2, (y1+y2)/2
xm2, ym2 = xm**2, ym**2
vectv = x2-x1, y2-y1
dy, dx = y2-y1, x2-x1
c = dy/dx
c2 = c**2
c21 = c2+1
d = c*ym + xm
e = d - xm
e2 = e**2
f = (e*c+ym)/c21
f2 = f**2
rd = L2+ym2+e2
def cal(R):
g = (R ** 2 - rd) / c21 + f2
if abs(g) < 1e-6:
g = 0
if g < 0:
return []
Y = math.sqrt(g)
Y1 = Y + f
Y2 = -Y + f
X1 = -c * Y1 + d
X2 = -c * Y2 + d
return [(X1, Y1), (X2, Y2)]
R = L
while True:
XY = cal(R)
X1, Y1 = XY[0]
u1, c1 = check(X1, Y1, R)
if u1:
break
lo = R
hi = 1e12
while abs(lo-hi) > 1e-6:
r = lo + (hi-lo) / 2
xy = cal(r)
X1, Y1 = xy[0]
if check2((X1, Y1, r), c1):
hi = r
else:
lo = r
if abs(R-lo) < 1e-6:
break
R = lo
ans = R
R = L
while True:
XY = cal(R)
X2, Y2 = XY[1]
u2, c2 = check(X2, Y2, R)
if u2:
break
lo = R
hi = 1e12
while abs(lo-hi) > 1e-6:
r = lo + (hi-lo) / 2
xy = cal(r)
X2, Y2 = xy[1]
if check2((X2, Y2, r), c2):
hi = r
else:
lo = r
if abs(R-lo) < 1e-6:
break
R = lo
ans = min(ans, R)
R = ans
print(ans)
# XY = cal(ans)
#
# plt.figure()
# fig, ax = plt.subplots()
# circles = []
# xs = [x for x, _, _ in C]
# ys = [y for _, y, _ in C]
# xlim = [min(xs), max(xs)]
# ylim = [min(ys), max(ys)]
# ll, lr = min(xlim[0], ylim[0])-R, max(xlim[1], ylim[1])+R
# ax.set_xlim([ll, lr])
# ax.set_ylim([ll, lr])
#
# for x, y, r in C:
# circles.append(plt.Circle((x, y), r, color='b'))
#
#
# plt.plot([x1, x2], [y1, y2])
#
# circles.append(plt.Circle(XY[0], R, color='r'))
# circles.append(plt.Circle(XY[1], R, color='r'))
# for c in circles:
# ax.add_artist(c)
# c.set_clip_box(ax.bbox)
# c.set_facecolor("none") # "none" not None
#
# plt.show()
```
No
| 103,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha's little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister.
Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother's drawing.
At first, the line going through two points, that brother drew, doesn't intersect or touch any circle.
Also, no two circles intersect or touch, and there is no pair of circles such that one circle is located inside another.
Moreover, for each circle, Masha drew a square of the minimal area with sides parallel axis such that this circle is located inside the square and noticed that there is no two squares intersect or touch and there is no pair of squares such that one square is located inside other.
Now Masha wants to draw circle of minimal possible radius such that it goes through two points that brother drew and doesn't intersect any other circle, but other circles can touch Masha's circle and can be located inside it.
It's guaranteed, that answer won't exceed 1012. It should be held for hacks as well.
Input
First line contains four integers x1, y1, x2, y2 ( - 105 ≤ x1, y1, x2, y2 ≤ 105) — coordinates of points that brother drew. First point has coordinates (x1, y1) and second point has coordinates (x2, y2). These two points are different.
The second line contains single integer n (1 ≤ n ≤ 105) — the number of circles that brother drew.
Next n lines contains descriptions of circles. Each line contains three integers xi, yi, ri ( - 105 ≤ xi, yi ≤ 105, 1 ≤ ri ≤ 105) describing circle with center (xi, yi) and radius ri.
Output
Output smallest real number, that it's possible to draw a circle with such radius through given points in such a way that it doesn't intersect other circles.
The output is considered correct if it has a relative or absolute error of at most 10 - 4.
Examples
Input
2 4 7 13
3
3 0 1
12 4 2
-4 14 2
Output
5.1478150705
Input
-2 3 10 -10
2
7 0 3
-5 -5 2
Output
9.1481831923
Note
<image> <image>
Submitted Solution:
```
import math
# from matplotlib.patches import Circle as pyC
# import matplotlib.pyplot as plt
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def copy(self):
return Point(self.x, self.y)
def unitize(self):
return self.copy() / self.length()
def length(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
def rotate(self, other):
return Point(self.x * other.x - self.y * other.y, self.x * other.y + self.y * other.x)
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, other):
if isinstance(other, Point):
return self.x * other.x + self.y * other.y
else:
return Point(self.x * other, self.y * other)
def __truediv__(self, other):
return Point(self.x / other, self.y / other)
def __repr__(self):
return self.__str__()
def __str__(self):
return "<Point x: %s, y: %s>"%(self.x, self.y)
class Circle:
def __init__(self, p, r):
self.p = p
self.r = r
def read():
return (map(int, input().split()))
def checkCircle(circle):
for c in circles:
if c.r + circle.r - 0.0001 > (c.p - circle.p).length() > abs(c.r - circle.r) + 0.0001:
return False
return True
def generateCircle(circle):
M = (circle.p - O).rotate(Point(Du.y, Du.x))
_ML = M.x ** 2 + M.y ** 2
_CR = circle.r**2
_HH = P**2 - _CR - _ML
A = 4 * M.x**2 - 4 * _CR
B = 4 * M.x * _HH + 8 * M.x * _CR
C = _HH**2 - 4 * _CR * _ML
Delta = B**2 - 4 * A * C
_Delta = math.sqrt(Delta)
X1 = Point((-B + _Delta) / 2 / A, 0)
X2 = Point((-B - _Delta) / 2 / A, 0)
R1 = (Point(0, P) - X1).length()
R2 = (Point(0, P) - X2).length()
C1 = Circle(X1.rotate(Point(Du.y, -Du.x)) + O, R1)
C2 = Circle(X2.rotate(Point(Du.y, -Du.x)) + O, R2)
# print([P, Mt, M, (R1, R2)])
# print([X1, X2])
if R1 < R2:
return X1, C1, X2, C2
else:
return X2, C2, X1, C1
def main():
simple = Circle((p1 + p2) / 2, (p1 - p2).length() / 2)
if checkCircle(simple):
return simple.r
minR = None
for circle in circles:
c1, c2 = generateCircle(circle)
# print([c1.p.x, c1.p.y, c1.r])
# print([c2.p.x, c2.p.y, c2.r])
# ax.add_patch(pyC(xy = (c1.p.x, c1.p.y), radius = c1.r, alpha = 0.5))
# ax.add_patch(pyC(xy = (c2.p.x, c2.p.y), radius = c2.r, alpha = 0.5))
if minR is None or minR > c1.r:
if checkCircle(c1):
minR = c1.r
if minR is None or minR > c2.r:
if checkCircle(c2):
minR = c2.r
return minR
class Intervals:
def __init__(self):
self.l = []
# self.add(1, 5.5)
def add(self, a, b):
t = -1
tt = -1
flag = False
for i, s in enumerate(self.l):
if t == -1:
if s[0] > a:
t = i
flag = True
elif s[0] <= a < s[1] - DURATION:
t = i
if b <= s[1]:
return
if t != -1:
if s[0] + DURATION < b:
tt = i
elif s[0] >= b:
break
# print([a, b])
if t == -1:
self.l.append((a, b))
else:
k = (min(self.l[i][0], a), max(self.l[tt][1], b))
if tt == -1:
if flag:
k = (k[0], b)
else:
del self.l[t:]
else:
del self.l[t:tt + 1]
self.l.insert(t, k)
# print(self.l)
def min(self):
if len(self.l) == 0:
return 10**12
elif self.l[0][0] == 0:
return self.l[0][1]
else:
return self.l[0][0]
DURATION = 0.0001
(x1, y1, x2, y2) = read()
p1 = Point(x1, y1)
p2 = Point(x2, y2)
D = p1 - p2
P = D.length() / 2
O = (p1 + p2) / 2
Du = D.unitize()
# fig = plt.figure()
# ax = fig.add_subplot(111)
circles = []
lP = Intervals()
lN = Intervals()
(n, ) = read()
for i in range(n):
cx, cy, cr = read()
# ax.add_patch(pyC(xy = (cx, cy), radius = cr, alpha = 0.5))
c = Circle(Point(cx, cy), cr)
# circles.append(c)
x1, c1, x2, c2 = generateCircle(c)
if x1.x > 0:
if x2.x < 0:
lP.add(0, c1.r)
lN.add(0, c2.r)
else:
lP.add(c1.r, c2.r)
else:
if x2.x > 0:
lP.add(0, c2.r)
lN.add(0, c1.r)
else:
lN.add(c1.r, c2.r)
# print([cx, cy, cr, c1.r, c2.r])
# print(l.l)
# print([lP.min(), lN.min()])
lP.add(0, P)
lN.add(0, P)
print(min(lP.min(), lN.min()))
# x, y = 0, 0
# ax.plot(x, y, 'ro')
# plt.show()
```
No
| 103,402 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define two functions f and g on positive integer numbers.
<image>
<image>
You need to process Q queries. In each query, you will be given three integers l, r and k. You need to print the number of integers x between l and r inclusive, such that g(x) = k.
Input
The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries.
Q lines follow, each of which contains 3 integers l, r and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).
Output
For each query, print a single line containing the answer for that query.
Examples
Input
4
22 73 9
45 64 6
47 55 7
2 62 4
Output
1
4
0
8
Input
4
82 94 6
56 67 4
28 59 9
39 74 4
Output
3
1
1
5
Note
In the first example:
* g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9
* g(47) = g(48) = g(60) = g(61) = 6
* There are no such integers between 47 and 55.
* g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4
Submitted Solution:
```
z,zz=input,lambda:list(map(int,z().split()))
fast=lambda:stdin.readline().strip()
zzz=lambda:[int(i) for i in fast().split()]
szz,graph,mod,szzz=lambda:sorted(zz()),{},10**9+7,lambda:sorted(zzz())
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
p=ceil(x**.5)+1
for i in range(2,p):
if (x%i==0 and x!=2) or x==0:return 0
return 1
def dfs(u,visit,graph):
visit[u]=1
for i in graph[u]:
if not visit[i]:
dfs(i,visit,graph)
###########################---Test-Case---#################################
"""
If you Know me , Then you probably don't know me !
"""
###########################---START-CODING---##############################
def solve(x):
if x<10: return x
p=1
while x:
if x%10:p*=(x%10)
x//=10
return solve(p)
res=[]
ans=[[]]
for i in range(9):
ans+=[[0]*1000001]
for i in range(1,1000001):
ans[solve(i)][i]+=1
for i in range(1,10):
for j in range(1,1000001):
ans[i][j]+=ans[i][j-1]
num=int(z())
for i in ' '*num:
l,r,k=zzz()
res.append(ans[k][r]-ans[k][l])
for i in res:print(i)
```
No
| 103,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define two functions f and g on positive integer numbers.
<image>
<image>
You need to process Q queries. In each query, you will be given three integers l, r and k. You need to print the number of integers x between l and r inclusive, such that g(x) = k.
Input
The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries.
Q lines follow, each of which contains 3 integers l, r and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).
Output
For each query, print a single line containing the answer for that query.
Examples
Input
4
22 73 9
45 64 6
47 55 7
2 62 4
Output
1
4
0
8
Input
4
82 94 6
56 67 4
28 59 9
39 74 4
Output
3
1
1
5
Note
In the first example:
* g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9
* g(47) = g(48) = g(60) = g(61) = 6
* There are no such integers between 47 and 55.
* g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4
Submitted Solution:
```
l = []
for i in range(1000055):
l.append(0)
def g(n):
if(n < 10):
return n
elif(l[i] != 0):
return l[i]
else:
l[n] = g(f(n, 1))
return l[n]
def f(n, k):
while(n > 0):
if(n % 10 != 0):
k *= n % 10
n = int(n / 10)
if(n == 0):
return k
if(n % 10 != 0):
k *= n % 10
k = f(int(n % 10), k)
l[n] = k
return k
n = int(input())
for i in range(n):
a, b, c = input().split()
a = int(a)
b = int(b)
c = int(c)
v = 0
for j in range(a, b + 1):
if(g(j) == c):
v = v + 1;
print(v)
```
No
| 103,404 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define two functions f and g on positive integer numbers.
<image>
<image>
You need to process Q queries. In each query, you will be given three integers l, r and k. You need to print the number of integers x between l and r inclusive, such that g(x) = k.
Input
The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries.
Q lines follow, each of which contains 3 integers l, r and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).
Output
For each query, print a single line containing the answer for that query.
Examples
Input
4
22 73 9
45 64 6
47 55 7
2 62 4
Output
1
4
0
8
Input
4
82 94 6
56 67 4
28 59 9
39 74 4
Output
3
1
1
5
Note
In the first example:
* g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9
* g(47) = g(48) = g(60) = g(61) = 6
* There are no such integers between 47 and 55.
* g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4
Submitted Solution:
```
import bisect
q = int(input())
l = []
r = []
k = []
for i in range(q):
a, b, c = list(map(int,input().strip().split()))
l.append(a)
r.append(b)
k.append(c)
memo = dict()
def compute(n):
if n == 0:
return 1
curr = n % 10
if curr == 0:
curr = 1
return curr * compute(n // 10)
def solve(n):
if n > 10:
return solve(compute(n))
else:
return n
for i in range(min(l),max(r)+1):
curr = solve(i)
if curr in memo:
memo[curr].append(i)
else:
memo[curr] = [i]
for i in range(q):
lindex = bisect.bisect_left(memo[k[i]],l[i])
rindex = bisect.bisect_left(memo[k[i]],r[i])
print(rindex - lindex)
```
No
| 103,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define two functions f and g on positive integer numbers.
<image>
<image>
You need to process Q queries. In each query, you will be given three integers l, r and k. You need to print the number of integers x between l and r inclusive, such that g(x) = k.
Input
The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries.
Q lines follow, each of which contains 3 integers l, r and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).
Output
For each query, print a single line containing the answer for that query.
Examples
Input
4
22 73 9
45 64 6
47 55 7
2 62 4
Output
1
4
0
8
Input
4
82 94 6
56 67 4
28 59 9
39 74 4
Output
3
1
1
5
Note
In the first example:
* g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9
* g(47) = g(48) = g(60) = g(61) = 6
* There are no such integers between 47 and 55.
* g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4
Submitted Solution:
```
def f(x):
dig=0
prod=1
while(x>0):
dig=x%10
prod = prod*dig
x=int(x/10)
return prod
def g(x):
if(x<10):
return x
else:
return g(f(x))
n = int(input())
tot = []
for i in range(n):
arr=[]
arr = list(map(int,input().split()))
tot.append(arr)
l = arr[0]
r=arr[1]
k=arr[2]
cnt=0
for j in range(l,r+1):
cand = g(j)
if(g(j) == k):
cnt+=1
print(cnt)
```
No
| 103,406 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input())
if n <= 5:
print(-1)
else:
print(1, 2)
print(2, 3)
print(2, 4)
for i in range(5, n+1):
print(4, i)
for i in range(2, n+1):
print(1, i)
```
| 103,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
if n < 6:
print(-1)
else:
l = []
o = []
x = (3+n)//2
for i in range(3,x+1):
l.append((1,i))
for i in range(x+1,n+1):
o.append((2,i))
sys.stdout.write("1"+" "+"2"+"\n")
for x in l:
sys.stdout.write(str(x[0]) + " " + str(x[1]) + "\n")
for x in o:
sys.stdout.write(str(x[0]) + " " + str(x[1]) + "\n")
sys.stdout.write("1"+" "+"2"+"\n")
p = 2
for i in range(3,n+1):
sys.stdout.write(str(p) + " " + str(i) + "\n")
p = i
```
| 103,408 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
n=int(input())
#section 1
if n<6:
print(-1)
else:
print(1,2)
print(1,3)
print(1,4)
print(2,5)
print(2,6)
for i in range(7,n+1):
print(1,i)
#section 2
for i in range(2,n+1):
print(1,i)
"""
1 2
1 3
1 4
2 5
2 6
1 7
1 8
1 2
2 3
3 4
4 5
1 6
1 7
1 8
"""
```
| 103,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
k = int(input())
if(k<=5):
print(-1)
for i in range(2, k+1):
print(1,i)
else:
for i in range(2, k-1):
print(1, i)
print(k-2, k-1)
print(k-2, k)
for i in range(2, k+1):
print(1,i)
```
| 103,410 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
import sys
import math
from collections import Counter
from operator import itemgetter
import queue
def IO():
sys.stdin=open("pyinput.txt", 'r')
sys.stdout=open("pyoutput.txt", 'w')
def GCD(a, b):
if(b==0): return a
else: return GCD(b, a%b)
def LCM(a, b): return a*(b//GCD(a, b))
def scan(TYPE_1, TYPE_2=0):
if(TYPE_1==int):
return map(int, sys.stdin.readline().strip().split())
elif(TYPE_1==float):
return map(float, sys.stdin.readline().strip().split())
elif(TYPE_1==list and TYPE_2==float):
return list(map(float, sys.stdin.readline().strip().split()))
elif(TYPE_1==list and TYPE_2==int):
return list(map(int, sys.stdin.readline().strip().split()))
elif(TYPE_1==str):
return sys.stdin.readline().strip()
else: print("ERROR!!!!")
def main():
n=int(input())
if(n<6):
print(-1)
else:
print(1, 2)
print(1, 3)
print(1, 4)
for i in range(5, n+1):
print(4, i)
for i in range(2, n+1):
print(1, i)
# IO()
main()
```
| 103,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input())
for i in range(2,n+1):
if n <= 5:
print("-1")
break
if i < 5:
print("1",i)
else:
print("2",i)
for i in range(1,n):
print(i,i+1)
```
| 103,412 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input())
if n < 6:
print(-1)
else:
baseans = [(1,2), (1,3), (1,4), (4,5), (4,6)]
for p in baseans:
print('%d %d' % (p[0], p[1]))
for i in range(7, n+1):
print('%d %d' % (4, i))
for i in range(2, n+1):
print('%d %d' % (1, i))
```
| 103,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Tags: constructive algorithms, trees
Correct Solution:
```
n = int(input());
if ( n <= 5):
print (-1);
else:
print (str(1)+ " "+str(2));
print (str(2)+" "+str(3));
print (str(2)+" "+str(4));
print (str(4)+" "+str(5));
print (str(4) + " "+str(6));
k = 5;
while ( k < n - 1 ):
print ( str(4) + " " + str(k+2) );
k+=1;
h = 0;
while ( h < n - 1 ):
print ( str(1) + " " + str(h + 2) );
h+=1;
```
| 103,414 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n <= 5:
print(-1)
else:
print('1 2', '1 3', '1 4', '2 5', sep = '\n')
for i in range(6, n + 1):
print('2 {0:d}'.format(i))
for i in range(2, n + 1):
print('1 {0:d}'.format(i))
```
Yes
| 103,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n < 6:
print(-1)
else:
print(1, 2)
if (n) % 2 == 0:
for i in range(3, (n) // 2 + 2):
print(1, i)
for i in range((n) // 2 + 2, n + 1):
print(2, i)
else:
for i in range(3, (n) // 2 + 2):
print(1, i)
for i in range((n) // 2 + 2, n + 1):
print(2, i)
for i in range(2, n + 1):
print(i, i - 1)
```
Yes
| 103,416 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
def print_wrong(l):
if l<6:
print('-1')
else:
for i in range(0, l-1):
if i % 2 == 0:
first = '1 '
else:
first = '2 '
print(first + str(i + 2))
def print_right(l):
for i in range(0, l - 1):
print('1 %s' % str(i + 2))
print_wrong(n)
print_right(n)
```
Yes
| 103,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
import math
import heapq
from queue import Queue
def main(n):
if n<6:
print(-1)
else:
print(1,2)
print(2,3)
print(2,4)
print(4,5)
print(4,6)
a=7
while a<=n:
print(2,a)
a+=1
a=2
while a<=n:
print(1,a)
a+=1
return
n=int(input())
(main(n))
```
Yes
| 103,418 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n in range(2, 4+1):
print("-1")
for i in range(2, n+1):
print("1", i)
else:
# Wrong Answer
print("1 2")
p = n // 2 - 2
for i in range(p+1):
print("2", i+3)
for i in range(n-p-3):
print("1", i+p+4)
# Right Answer
for i in range(n - 1):
print("1", i + 2)
```
No
| 103,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n <= 4:
print(-1)
else:
n3 = (n - 1) // 2
n2 = n - n3 - 1
for i in range(n2):
print(1, i+2)
for i in range(n3):
print(2, i+n2+2)
for i in range(n-1):
print(1, i+2)
```
No
| 103,420 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n=int(input())
if(n<6):
print(-1)
else:
print(1,2)
#print(1,3)
print(1,4)
for i in range(4,n):
print(2,i+1)
for i in range(n-1):
print(1,i+1+1)
```
No
| 103,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
* Root the tree at node 1.
* Count the number of nodes at an even depth. Let it be evenCnt.
* Count the number of nodes at an odd depth. Let it be oddCnt.
* The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
Input
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
Output
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
Examples
Input
2
Output
-1
1 2
Input
8
Output
1 2
1 3
2 4
2 5
3 6
4 7
4 8
1 2
1 3
2 4
2 5
2 6
3 7
6 8
Note
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: <image> In the second tree, the algorithm will find an answer with 3 nodes which is correct: <image>
Submitted Solution:
```
n = int(input())
if n == 2:
print(-1)
print(1, 2)
elif n == 3:
print(-1)
print(1, 2)
print(2, 3)
else:
for i in range(1, n):
print(i, i +1)
print(1, 2)
for i in range(3, n + 1):
print(2, i)
```
No
| 103,422 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some company is going to hold a fair in Byteland. There are n towns in Byteland and m two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are k types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least s different types of goods. It costs d(u,v) coins to bring goods from town u to town v where d(u,v) is the length of the shortest path from u to v. Length of a path is the number of roads in this path.
The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of n towns.
Input
There are 4 integers n, m, k, s in the first line of input (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 10^{5}, 1 ≤ s ≤ k ≤ min(n, 100)) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.
In the next line there are n integers a_1, a_2, …, a_n (1 ≤ a_{i} ≤ k), where a_i is the type of goods produced in the i-th town. It is guaranteed that all integers between 1 and k occur at least once among integers a_{i}.
In the next m lines roads are described. Each road is described by two integers u v (1 ≤ u, v ≤ n, u ≠ v) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.
Output
Print n numbers, the i-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town i. Separate numbers with spaces.
Examples
Input
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
Output
2 2 2 2 3
Input
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
Output
1 1 1 2 2 1 1
Note
Let's look at the first sample.
To hold a fair in town 1 you can bring goods from towns 1 (0 coins), 2 (1 coin) and 4 (1 coin). Total numbers of coins is 2.
Town 2: Goods from towns 2 (0), 1 (1), 3 (1). Sum equals 2.
Town 3: Goods from towns 3 (0), 2 (1), 4 (1). Sum equals 2.
Town 4: Goods from towns 4 (0), 1 (1), 5 (1). Sum equals 2.
Town 5: Goods from towns 5 (0), 4 (1), 3 (2). Sum equals 3.
Submitted Solution:
```
'''input
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
'''
def main():
n, m, k, s = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
g = {i: [] for i in range(n)}
for i in range(m):
u, v = [int(x) for x in input().split()]
g[u - 1].append(v - 1)
g[v - 1].append(u - 1)
for u in range(m):
if len(g[u]) + 1 >= s:
print(s - 1, end=' ')
else:
result = 0
needed = s - 1
dist = 1
queue = [v for v in g[u]]
done = set()
while needed:
if len(queue) >= needed:
result += (dist * needed)
needed = 0
break
result += (dist * len(queue))
needed -= len(queue)
for v in queue:
if v not in done:
queue.extend(g[v])
done.add(v)
dist += 1
print(result, end=' ')
print()
main()
```
No
| 103,423 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some company is going to hold a fair in Byteland. There are n towns in Byteland and m two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are k types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least s different types of goods. It costs d(u,v) coins to bring goods from town u to town v where d(u,v) is the length of the shortest path from u to v. Length of a path is the number of roads in this path.
The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of n towns.
Input
There are 4 integers n, m, k, s in the first line of input (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 10^{5}, 1 ≤ s ≤ k ≤ min(n, 100)) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.
In the next line there are n integers a_1, a_2, …, a_n (1 ≤ a_{i} ≤ k), where a_i is the type of goods produced in the i-th town. It is guaranteed that all integers between 1 and k occur at least once among integers a_{i}.
In the next m lines roads are described. Each road is described by two integers u v (1 ≤ u, v ≤ n, u ≠ v) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.
Output
Print n numbers, the i-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town i. Separate numbers with spaces.
Examples
Input
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
Output
2 2 2 2 3
Input
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
Output
1 1 1 2 2 1 1
Note
Let's look at the first sample.
To hold a fair in town 1 you can bring goods from towns 1 (0 coins), 2 (1 coin) and 4 (1 coin). Total numbers of coins is 2.
Town 2: Goods from towns 2 (0), 1 (1), 3 (1). Sum equals 2.
Town 3: Goods from towns 3 (0), 2 (1), 4 (1). Sum equals 2.
Town 4: Goods from towns 4 (0), 1 (1), 5 (1). Sum equals 2.
Town 5: Goods from towns 5 (0), 4 (1), 3 (2). Sum equals 3.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 9 11:35:51 2018
@author: du-wan
"""
import random
import math
#1.find shortest path from src to dest
#2.create nodes with the following attributes: index, item_type, shortest_path
#3.sort nodes by shortest_path and store in vec
#4.iterate through vec, and print if item_type has not already been seen. Break iteration,
# when we have seen s unique types.
class heap_node:
def __init__(self, idx, item, cost):
self.idx = idx
self.item = item
self.cost = -cost
class max_heap:
def __init__(self, vec):
self.maxHeap = vec
def insert(self, node):
self.maxHeap.append(node)
self.heapify()
return self.maxHeap
def heapify(self):
n = len(self.maxHeap)//2
for i in range(n, -1, -1):
self.__heapify(len(self.maxHeap), i)
def __heapify(self, n, i):
#helper method to bubble down parent and child switch nodes
l = 2*i+1
r = 2*i+2
largest = i
if (l < n) and (self.maxHeap[l].cost > self.maxHeap[largest].cost):
largest = l
if (r < n) and (self.maxHeap[r].cost > self.maxHeap[largest].cost):
largest = r
if i != largest:
self.maxHeap[i], self.maxHeap[largest] = self.maxHeap[largest], self.maxHeap[i]
self.__heapify(n, largest)
def sort(self):
self.heapify()
sorted_vec = []
n = len(self.maxHeap)
for i in range(n-1, -1, -1):
sorted_vec.append(self.maxHeap[0])
self.maxHeap[0], self.maxHeap[i] = self.maxHeap[i], self.maxHeap[0]
self.__heapify(i, 0) #__heapify heapifies starting from current index up to index i
# self.heapify()
return sorted_vec
def shortest_path(graph, src, dest, shortest_path_graph, visited):
n = len(graph[src])
for e in range(n):
if (e not in visited):
if (shortest_path_graph[src] + graph[src][e]) < shortest_path_graph[e]:
shortest_path_graph[e] = shortest_path_graph[src] + graph[src][e]
shortest_path(graph, e, dest, shortest_path_graph, visited)
visited.append(src)
def get_min_cost(graph, src, item_vec, items_required):
shortest_path_graph = [math.inf for _ in range(len(graph))]
shortest_path_graph[src] = 0
visited = []
shortest_path(graph, src, len(graph)-1, shortest_path_graph, visited)
city_vec = max_heap([])
for i in range(len(shortest_path_graph)):
city_vec.insert(heap_node(i, item_vec[i], shortest_path_graph[i]))
sorted_city_vec = city_vec.sort()
res = []
item_seen = []
for city in sorted_city_vec:
if len(res) >= items_required:
break
if city.item in item_seen:
continue
item_seen.append(city.item)
res.append(city.cost)
min_cost = 0
for cost in res:
min_cost += cost
return cost*(-1)
def main():
n_v, n_e, n_items, items_required = [int(x) for x in input().split()]
graph = [[math.inf for _ in range(n_v)] for _ in range(n_v)]
item_vec = [int(x) for x in input().split()]
cities_in_graph = []
for _ in range(n_e):
v1, v2 = [int(x) for x in input().split()]
v1 -= 1
v2 -= 1
if v1 not in cities_in_graph:
cities_in_graph.append(v1)
if v2 not in cities_in_graph:
cities_in_graph.append(v2)
graph[v1][v2] = 1
# print(graph)
#choices = [i for i in range(1,101)]
# graph = []
# for _ in range(10):
# graph.append([random.choice(choices) for _ in range(10)])
# item_vec = [1,2,3,4,5,6,7,8,9,10]
# items_required = 5
min_cost_vec = []
for dest in range(len(graph)):
min_cost_vec.append(get_min_cost(graph, dest, item_vec, items_required))
output = ""
for i in range(len(min_cost_vec)):
output += (" " + str(min_cost_vec[i]))
print(output)
return(min_cost_vec)
main()
```
No
| 103,424 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some company is going to hold a fair in Byteland. There are n towns in Byteland and m two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are k types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least s different types of goods. It costs d(u,v) coins to bring goods from town u to town v where d(u,v) is the length of the shortest path from u to v. Length of a path is the number of roads in this path.
The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of n towns.
Input
There are 4 integers n, m, k, s in the first line of input (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 10^{5}, 1 ≤ s ≤ k ≤ min(n, 100)) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.
In the next line there are n integers a_1, a_2, …, a_n (1 ≤ a_{i} ≤ k), where a_i is the type of goods produced in the i-th town. It is guaranteed that all integers between 1 and k occur at least once among integers a_{i}.
In the next m lines roads are described. Each road is described by two integers u v (1 ≤ u, v ≤ n, u ≠ v) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.
Output
Print n numbers, the i-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town i. Separate numbers with spaces.
Examples
Input
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
Output
2 2 2 2 3
Input
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
Output
1 1 1 2 2 1 1
Note
Let's look at the first sample.
To hold a fair in town 1 you can bring goods from towns 1 (0 coins), 2 (1 coin) and 4 (1 coin). Total numbers of coins is 2.
Town 2: Goods from towns 2 (0), 1 (1), 3 (1). Sum equals 2.
Town 3: Goods from towns 3 (0), 2 (1), 4 (1). Sum equals 2.
Town 4: Goods from towns 4 (0), 1 (1), 5 (1). Sum equals 2.
Town 5: Goods from towns 5 (0), 4 (1), 3 (2). Sum equals 3.
Submitted Solution:
```
from collections import deque
from math import inf
n, m, k, s = map(int, input().split())
a = [int(e) - 1 for e in input().split()]
t = [[] for i in range(n)]
for i in range(m):
u, v = map(lambda x:int(x) - 1, input().split())
t[u].append(v)
t[v].append(u)
d = [[inf] * k for i in range(n)]
for i in range(k):
q = deque()
for j, e in enumerate(a):
if e == i:
q.append((j, 0))
while q:
u, l = q.popleft()
if d[u][i] != inf:
continue
d[u][i] = l
for v in t[u]:
if d[v][i] == inf:
q.append((v, l + 1))
print(d)
res = []
for i in range(n):
d[i].sort()
res.append(sum(d[i][:s]))
print(' '.join(map(str, res)))
```
No
| 103,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Some company is going to hold a fair in Byteland. There are n towns in Byteland and m two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are k types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least s different types of goods. It costs d(u,v) coins to bring goods from town u to town v where d(u,v) is the length of the shortest path from u to v. Length of a path is the number of roads in this path.
The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of n towns.
Input
There are 4 integers n, m, k, s in the first line of input (1 ≤ n ≤ 10^{5}, 0 ≤ m ≤ 10^{5}, 1 ≤ s ≤ k ≤ min(n, 100)) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.
In the next line there are n integers a_1, a_2, …, a_n (1 ≤ a_{i} ≤ k), where a_i is the type of goods produced in the i-th town. It is guaranteed that all integers between 1 and k occur at least once among integers a_{i}.
In the next m lines roads are described. Each road is described by two integers u v (1 ≤ u, v ≤ n, u ≠ v) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.
Output
Print n numbers, the i-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town i. Separate numbers with spaces.
Examples
Input
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
Output
2 2 2 2 3
Input
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
Output
1 1 1 2 2 1 1
Note
Let's look at the first sample.
To hold a fair in town 1 you can bring goods from towns 1 (0 coins), 2 (1 coin) and 4 (1 coin). Total numbers of coins is 2.
Town 2: Goods from towns 2 (0), 1 (1), 3 (1). Sum equals 2.
Town 3: Goods from towns 3 (0), 2 (1), 4 (1). Sum equals 2.
Town 4: Goods from towns 4 (0), 1 (1), 5 (1). Sum equals 2.
Town 5: Goods from towns 5 (0), 4 (1), 3 (2). Sum equals 3.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 9 11:35:51 2018
@author: du-wan
"""
import random
import math
#1.find shortest path from src to dest
#2.create nodes with the following attributes: index, item_type, shortest_path
#3.sort nodes by shortest_path and store in vec
#4.iterate through vec, and print if item_type has not already been seen. Break iteration,
# when we have seen s unique types.
class heap_node:
def __init__(self, idx, item, cost):
self.idx = idx
self.item = item
self.cost = -cost
class max_heap:
def __init__(self, vec):
self.maxHeap = vec
def insert(self, node):
self.maxHeap.append(node)
self.heapify()
return self.maxHeap
def heapify(self):
n = len(self.maxHeap)//2
for i in range(n, -1, -1):
self.__heapify(len(self.maxHeap), i)
def __heapify(self, n, i):
#helper method to bubble down parent and child switch nodes
l = 2*i+1
r = 2*i+2
largest = i
if (l < n) and (self.maxHeap[l].cost > self.maxHeap[largest].cost):
largest = l
if (r < n) and (self.maxHeap[r].cost > self.maxHeap[largest].cost):
largest = r
if i != largest:
self.maxHeap[i], self.maxHeap[largest] = self.maxHeap[largest], self.maxHeap[i]
self.__heapify(n, largest)
def sort(self):
self.heapify()
sorted_vec = []
n = len(self.maxHeap)
for i in range(n-1, -1, -1):
sorted_vec.append(self.maxHeap[0])
self.maxHeap[0], self.maxHeap[i] = self.maxHeap[i], self.maxHeap[0]
self.__heapify(i, 0) #__heapify heapifies starting from current index up to index i
# self.heapify()
return sorted_vec
def shortest_path(graph, src, dest, shortest_path_graph, visited):
if src == dest:
return
n = len(graph[src])
for e in range(n):
if (e not in visited):
if (shortest_path_graph[src] + graph[src][e]) < shortest_path_graph[e]:
shortest_path_graph[e] = shortest_path_graph[src] + graph[src][e]
shortest_path(graph, e, dest, shortest_path_graph, visited)
visited.append(src)
def get_min_cost(graph, src, item_vec, items_required):
shortest_path_graph = [math.inf for _ in range(len(graph))]
shortest_path_graph[src] = 0
visited = []
shortest_path(graph, src, len(graph)-1, shortest_path_graph, visited)
city_vec = max_heap([])
for i in range(len(shortest_path_graph)):
city_vec.insert(heap_node(i, item_vec[i], shortest_path_graph[i]))
sorted_city_vec = city_vec.sort()
res = []
item_seen = []
for city in sorted_city_vec:
if len(res) >= items_required:
break
if city.item in item_seen:
continue
item_seen.append(city.item)
res.append(city.cost)
min_cost = 0
for cost in res:
min_cost += cost
return cost*(-1)
def main():
n_v, n_e, n_items, items_required = [int(x) for x in input().split()]
graph = [[0 for _ in range(n_v)] for _ in range(n_v)]
item_vec = [int(x) for x in input().split()]
cities_in_graph = []
for _ in range(n_e):
v1, v2 = [int(x) for x in input().split()]
v1 -= 1
v2 -= 1
if v1 not in cities_in_graph:
cities_in_graph.append(v1)
if v2 not in cities_in_graph:
cities_in_graph.append(v2)
graph[v1][v2] = 1
# print(graph)
#choices = [i for i in range(1,101)]
#graph = []
#for _ in range(10):
# graph.append([random.choice(choices) for _ in range(10)])
#item_vec = [1,2,3,4,5,6,7,8,9,10]
#items_required = 5
min_cost_vec = []
for dest in cities_in_graph:
min_cost_vec.append(get_min_cost(graph, dest, item_vec, items_required))
output = ""
for i in range(len(min_cost_vec)):
output += (" " + str(min_cost_vec[i]))
print(output)
return(min_cost_vec)
main()
```
No
| 103,426 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
N = int(input())
A = list(map(int, input().split()))
N = len(A) // 3
A = A[:3*N]
#print(N, A)
A.append(0)
A.append(0)
ans = 0
def ind(a, b):
t = sorted((a, b))
return t[0] * (N+1) + t[1]
def inv(i):
b = i % (N+1)
a = i // (N + 1)
return a, b
dp = [-1] * (N+1)**2
dp[ind(A[0],A[1])] = 0
current_max = 0
dp1 = [-1] * (N+1)
dp1[A[0]] = 0
dp1[A[1]] = 0
ans = 0
for i in range(0, N):
a = A[2+3*i]
b = A[2+3*i+1]
c = A[2+3*i+2]
#print(a,b,c)
if a == b == c:
ans+=1
continue
dp_new = {}
for x, y, z in [(a, b, c), (b, c, a), (c, a, b)]:
# choose 1 (x), leave y, z
i = ind(y, z)
if i not in dp_new:
dp_new[i] = 0
dp_new[i] = max(dp_new[i], current_max)
if dp[ind(x, x)] >= 0:
dp_new[i] = max(dp_new[i], dp[ind(x, x)] + 1)
# choose 2 (x, y), leave t, z
for t in range(1, N+1):
i = ind(t,z)
# Not making three
if dp1[t] >= 0:
if i not in dp_new:
dp_new[i]=0
dp_new[i] = max(dp_new[i], dp1[t])
# Making three (x==y)
if x == y and dp[ind(t, x)] >= 0:
if i not in dp_new:
dp_new[i] = 0
dp_new[i] = max(dp_new[i], dp[ind(t, x)] + 1)
#print(a,b,c)
#print({inv(i):v for i, v in dp_new.items() })
for i, s in dp_new.items():
dp[i] = max(dp[i], s)
ta, tb = inv(i)
dp1[ta] = max(dp1[ta], s)
dp1[tb] = max(dp1[tb], s)
current_max = max(current_max, s)
#print(doubles)
print(ans + max(dp1))
```
| 103,427 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
from collections import deque
def upd(a, b, v):
global N
dp[a][b] = max(dp[a][b], v)
dp[b][a] = max(dp[b][a], v)
dp[N][a] = max(dp[N][a], v)
dp[N][b] = max(dp[N][b], v)
dp[a][N] = max(dp[a][N], v)
dp[b][N] = max(dp[b][N], v)
dp[N][N] = max(dp[N][N], v)
N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))
INF = 1 << 32
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
upd(A[0], A[1], 0)
base = 0
q = deque()
for i in range(2, 3 * N - 1, 3):
x, y, z = A[i], A[i + 1], A[i + 2]
if x == y == z: # x, y, z 3文字すべて等しいとき
base += 1
else:
for _ in range(3):
# a, b, x, y, z のうち, b, y, z を消すとき
for k in range(N):
v = dp[k][N]
if y == z:
v = max(v, dp[k][y] + 1)
q.append((k, x, v))
# a, b, x, y, z のうち, a, b, z を消すとき
v = max(dp[z][z] + 1, dp[N][N])
q.append((x, y, v))
# x, y, z をローテート
x, y, z = z, x, y
while q:
a, b, v = q.popleft()
upd(a, b, v)
l = A[-1]
ans = max(dp[N][N], dp[l][l] + 1)
print(ans + base)
```
| 103,428 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
from typing import List
class DP:
N: int
table: List[List[int]]
max_all: int
max_arr: List[int]
inc: int
def __init__(self, N: int, x: int, y: int):
self.N = N
self.table = [[-(1 << 28)] * (N + 1) for _ in range(N + 1)]
self.table[x][y] = self.table[y][x] = 0
self.max_all = 0
self.max_arr = [-(1 << 28)] * (N + 1)
self.max_arr[x] = self.max_arr[y] = 0
self.inc = 0
def update(self, p: int, q: int, r: int) -> None:
if p == q == r:
self.inc += 1
return
npqr = self._update_pqr(p, q, r)
nqrp = self._update_pqr(q, r, p)
nrpq = self._update_pqr(r, p, q)
npq = self._update_pq(p, q)
nqr = self._update_pq(q, r)
nrp = self._update_pq(r, p)
np = self._update_p(p)
nq = self._update_p(q)
nr = self._update_p(r)
pq, qr, rp = None, None, None
if p == q:
pq = self._update_pair(p, r)
if q == r:
qr = self._update_pair(q, p)
if r == p:
rp = self._update_pair(r, q)
self._update_maximum(q, r, npqr)
self._update_maximum(r, p, nqrp)
self._update_maximum(p, q, nrpq)
self._update_maximum(p, q, npq)
self._update_maximum(q, r, nqr)
self._update_maximum(r, p, nrp)
for k in range(self.N + 1):
self._update_maximum(k, p, np[k])
self._update_maximum(k, q, nq[k])
self._update_maximum(k, r, nr[k])
if pq:
for k in range(self.N + 1):
self._update_maximum(k, r, pq[k])
if qr:
for k in range(self.N + 1):
self._update_maximum(k, p, qr[k])
if rp:
for k in range(self.N + 1):
self._update_maximum(k, q, rp[k])
def _update_maximum(self, x: int, y: int, v: int) -> None:
self.table[x][y] = self.table[y][x] = max(self.table[x][y], v)
self.max_all = max(self.max_all, v)
self.max_arr[x] = max(self.max_arr[x], v)
self.max_arr[y] = max(self.max_arr[y], v)
def _update_pair(self, p: int, q: int) -> List[int]:
return [max(self.table[k][q], self.table[k][p] + 1, self.table[p][k] + 1) for k in range(self.N + 1)]
def _update_pqr(self, p: int, q: int, r: int) -> int:
return max(self.table[q][r], self.table[p][p] + 1)
def _update_pq(self, p: int, q: int) -> int:
return max(self.table[p][q], self.max_all)
def _update_p(self, p) -> List[int]:
return [max(self.table[p][k], self.table[k][p], self.max_arr[k]) for k in range(self.N + 1)]
def ans(self):
return self.max_all + self.inc
N = int(input())
A = list(map(int, input().split())) + [0, 0]
dp = DP(N, A[0], A[1])
for i in range(N):
Z = A[3 * i + 2: 3 * i + 5]
dp.update(Z[0], Z[1], Z[2])
# print("out", dp.table, dp.max_all, dp.max_arr)
print(dp.ans())
```
| 103,429 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit
INF = 1 << 50
def run():
N, *A_ = map(int, read().split())
dp = [[-INF] * (N+1) for _ in range(N+1)]
dp_max = [-INF] * (N+1)
a, b = A_[:2]
dp[a][b] = dp[b][a] = dp_max[a] = dp_max[b] = 0
triplets = 0
chunks = []
for i in range(N-1):
x,y,z = A_[3*i+2 : 3*i+5]
if x == y == z:
triplets+=1
else:
chunks.append((x,y,z))
for x,y,z in chunks:
cache = []
# transition : a = x = y
if y == z:
x,y,z = y,z,x
elif z == x:
x,y,z = z,x,y
if x == y:
for i in range(1, N+1):
cache.append((z, i, dp[x][i] + 1))
# trainsition : a = b = x
cache.append((y, z, dp[x][x] + 1))
cache.append((z, x, dp[y][y] + 1))
cache.append((x, y, dp[z][z] + 1))
# trainsition : only fill next
MAX = max(dp_max)
cache.append((x, y, MAX))
cache.append((y, z, MAX))
cache.append((z, x, MAX))
for i in range(1, N+1):
cache.append((i, x, dp_max[i]))
cache.append((i, y, dp_max[i]))
cache.append((i, z, dp_max[i]))
# Update
for a, b, value in cache:
dp[a][b] = dp[b][a] = max(value, dp[a][b])
dp_max[a] = max(dp_max[a], dp[a][b])
dp_max[b] = max(dp_max[b], dp[a][b])
a = A_[-1]
dp[a][a] += 1
dp_max[a] = max(dp[a][a], dp_max[a])
print(max(dp_max) + triplets)
#print(chunks)
if __name__ == "__main__":
run()
```
| 103,430 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
n = int(input())
a = [*map(int, input().split())]
for i in range(n * 3):
a[i] -= 1
INF = 1<<30
dp = [[-INF] * n for i in range(n)]
mx = [-INF] * n
mx_all = -INF
add_all = 0
dp[a[0]][a[1]] = dp[a[1]][a[0]] = 0
mx[a[0]] = mx[a[1]] = 0
mx_all = 0
for i in range(n - 1):
x, y, z = sorted(a[i * 3 + 2 : i * 3 + 5])
update = []
# パターン1
if x == y == z:
add_all += 1
continue
# パターン2
if x == y:
for j in range(n):
update.append((j, z, dp[j][x] + 1))
if y == z:
for j in range(n):
update.append((j, x, dp[j][y] + 1))
# パターン3
update.append((y, z, dp[x][x] + 1))
update.append((x, z, dp[y][y] + 1))
update.append((x, y, dp[z][z] + 1))
# パターン2-2
for j in range(n):
update.append((j, x, mx[j]))
update.append((j, y, mx[j]))
update.append((j, z, mx[j]))
# パターン3-2
update.append((y, z, mx_all))
update.append((x, z, mx_all))
update.append((x, y, mx_all))
# in-place にするために更新を遅延させる
for j, k, val in update:
if dp[j][k] < val:
dp[j][k] = dp[k][j] = val
if mx[j] < val:
mx[j] = val
if mx[k] < val:
mx[k] = val
if mx_all < val:
mx_all = val
# 最後の 1 回
if mx_all < dp[a[-1]][a[-1]] + 1:
mx_all = dp[a[-1]][a[-1]] + 1
print(mx_all + add_all)
```
| 103,431 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
N = int(input())
A = list(map(int,input().split()))
INF = 2*N
DP = [[-INF]*(N+1) for _ in range(N+1)]
M = 0
rowM = [-INF]*(N+1)
def chmax(i,j,k):
DP[i][j] = max(k,DP[i][j])
DP[j][i] = max(k,DP[j][i])
rowM[i] = max(rowM[i],DP[i][j])
rowM[j] = max(rowM[j],DP[i][j])
global M
M = max(M,k)
ans = 0
DP[A[0]][A[1]] = 0
DP[A[1]][A[0]] = 0
rowM[A[0]] = 0
rowM[A[1]] = 0
for i in range(N-1):
new = A[3*i+2:3*i+5]
change = []
if len(set(new)) == 1:
ans += 1
continue
if len(set(new)) == 2:
for j in range(3):
if new.count(new[j]) == 2:
X = new[j]
else:
Y = new[j]
for j in range(1,N+1):
change.append((j,Y,DP[X][j]+1))
X,Y,Z = new
change.append((X,Y,DP[Z][Z]+1))
change.append((Y,Z,DP[X][X]+1))
change.append((X,Z,DP[Y][Y]+1))
change.append((X,Y,M))
change.append((X,Z,M))
change.append((Y,Z,M))
for j in range(1,N+1):
for k in range(3):
change.append((j,new[k],rowM[j]))
for j,k,l in change:
chmax(j,k,l)
M = max(M,DP[A[-1]][A[-1]]+1)
print(ans+M)
```
| 103,432 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
B = A[i*3+2:i*3+5]
cnt = len(set(B))
if cnt == 1:
ans += 1
elif cnt == 2:
a1 = B[0]^B[1]^B[2]
a2 = (sum(B) - a1) // 2
#
# X[a1], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(ma, X[a2][a2], X[a1][a1] + 1)
# X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, X[a1][j], X[j][a1]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, MA[j]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
#
X[a2] = [max(X[a2][j], MA[j]) for j in range(N+1)]
MA = [max(MA[j], X[a1][j], X[a2][j]) for j in range(N+1)]
MA[a1] = max(MA[a1], max(X[a1]), X[a2][a1])
MA[a2] = max(MA[a2], X[a1][a2], X[a2][a2])
ma = max(ma, MA[a1], MA[a2])
else:
a, b, c = B
X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
X[a] = [max(X[a][j], MA[j]) for j in range(N+1)]
X[b] = [max(X[b][j], MA[j]) for j in range(N+1)]
X[c] = [max(X[c][j], MA[j]) for j in range(N+1)]
MA[a] = max(MA[a], max(X[a]))
MA[b] = max(MA[b], X[a][b], max(X[b]))
MA[c] = max(MA[c], X[a][c], X[b][c], max(X[c]))
MA = [max(MA[j], X[a][j], X[b][j], X[c][j]) for j in range(N+1)]
ma = max(ma, MA[a], MA[b], MA[c])
print(ans + max(ma, max(max(x) for x in X)))
```
| 103,433 |
Provide a correct Python 3 solution for this coding contest problem.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
"Correct Solution:
```
import sys
input = sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
ANS=0
B=[A[0],A[1]]
C=[]
D=A[-1]
for i in range(N-1):
if A[i*3+2]==A[i*3+3]==A[i*3+4]:
ANS+=1
else:
C.append((A[i*3+2],A[i*3+3],A[i*3+4]))
DP=[[-1]*(N+1) for i in range(N+1)]
MAXDP=[-1]*(N+1)
SCMAX=0
DP[B[0]][B[1]]=DP[B[1]][B[0]]=0
MAXDP[B[0]]=0
MAXDP[B[1]]=0
for x,y,z in C:
#for d in DP:
# print(*d)
#print()
#print(x,y,z)
#print()
NLIST=[]
SCMAX2=0
NMAXDP=[i for i in MAXDP]
xy=max(0,DP[x][y],DP[z][z]+1)
yz=max(0,DP[y][z],DP[x][x]+1)
zx=max(0,DP[z][x],DP[y][y]+1)
if x==y:
for i in range(N+1):
if DP[i][x]!=-1:
NLIST.append((i,z,max(DP[i][z],DP[i][x]+1)))
#DP[i][z]=DP[z][i]=max(DP[i][z],DP[i][x]+1)
NMAXDP[i]=max(NMAXDP[i],DP[i][z],DP[z][i])
NMAXDP[z]=max(NMAXDP[z],DP[i][z],DP[z][i])
if y==z:
for i in range(N+1):
if DP[i][y]!=-1:
NLIST.append((i,x,max(DP[i][x],DP[i][y]+1)))
#DP[i][x]=DP[x][i]=max(DP[i][x],DP[i][y]+1)
NMAXDP[i]=max(NMAXDP[i],DP[i][x],DP[x][i])
NMAXDP[x]=max(NMAXDP[x],DP[i][x],DP[x][i])
if z==x:
for i in range(N+1):
if DP[i][x]!=-1:
NLIST.append((i,y,max(DP[i][y],DP[i][x]+1)))
#DP[i][y]=DP[y][i]=max(DP[i][y],DP[i][x]+1)
NMAXDP[i]=max(NMAXDP[i],DP[i][y],DP[y][i])
NMAXDP[y]=max(NMAXDP[y],DP[i][y],DP[y][i])
for i,j,sc in NLIST:
DP[i][j]=DP[j][i]=max(DP[i][j],sc)
SCMAX2=max(SCMAX,DP[i][j])
#for d in DP:
# print(*d)
#print()
for i in range(N+1):
DP[i][x]=DP[x][i]=max(DP[i][x],MAXDP[i])
DP[i][y]=DP[y][i]=max(DP[i][y],MAXDP[i])
DP[i][z]=DP[z][i]=max(DP[i][z],MAXDP[i])
SCMAX2=max(SCMAX2,DP[i][x],DP[i][y],DP[i][z])
NMAXDP[i]=max(NMAXDP[i],DP[i][x],DP[i][y],DP[i][z])
NMAXDP[x]=max(NMAXDP[x],DP[i][x])
NMAXDP[y]=max(NMAXDP[y],DP[i][y])
NMAXDP[z]=max(NMAXDP[z],DP[i][z])
DP[x][y]=DP[y][x]=max(SCMAX,xy)
DP[y][z]=DP[z][y]=max(SCMAX,yz)
DP[z][x]=DP[x][z]=max(SCMAX,zx)
SCMAX2=max(SCMAX2,DP[x][y],DP[y][z],DP[z][x])
MAXDP=NMAXDP
MAXDP[x]=max(MAXDP[x],DP[x][y],DP[y][x],DP[z][x],DP[x][z])
MAXDP[y]=max(MAXDP[y],DP[x][y],DP[y][x],DP[z][y],DP[y][z])
MAXDP[z]=max(MAXDP[z],DP[x][z],DP[z][x],DP[z][y],DP[y][z])
SCMAX=SCMAX2
#for d in DP:
# print(*d)
DP[D][D]+=1
DPANS=0
for d in DP:
DPANS=max(max(d),DPANS)
print(ANS+DPANS)
```
| 103,434 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input().strip())+2
A = list(map(int,input().split()))+[N-1,N]
INF = N
dp = [[-INF for j in range(N)] for i in range(N)]
dpmax = -INF
dpmaxj = [-INF for i in range(N)]
score = 0
a,b,c,d,e = [i-1 for i in sorted(A[:5])]
if a == b == c:
dp[d][e] = dp[e][d] = dpmaxj[d] = dpmaxj[e] = dpmax = 1
elif b == c == d:
dp[a][e] = dp[e][a] = dpmaxj[a] = dpmaxj[e] = dpmax = 1
elif c == d == e:
dp[a][b] = dp[b][a] = dpmaxj[a] = dpmaxj[b] = dpmax = 1
else:
L = [a,b,c,d,e]
for i in range(5):
for j in range(i+1,5):
p,q = L[i],L[j]
dp[p][q] = dp[q][p] = dpmaxj[p] = dpmaxj[q] = 0
dpmax = 0
def update(i,j):
dpmaxj[i] = max(dpmaxj[i],dp[i][j])
dpmaxj[j] = max(dpmaxj[j],dp[i][j])
for i in range(N-3):
prevdpmax = dpmax
prevdpmaxj = dpmaxj[:]
dpd = [dp[k][k] for k in range(N)]
a,b,c = A[i*3+5:i*3+8]
a -= 1
b -= 1
c -= 1
prevdpka = [dp[i][a] for i in range(N)]
prevdpla = [dp[a][i] for i in range(N)]
prevdpkb = [dp[i][b] for i in range(N)]
prevdplb = [dp[b][i] for i in range(N)]
if a == b == c:
score += 1
continue
elif a == b:
for k in range(N):
dp[k][c] = max(dp[k][c],max(prevdpka[k],prevdpla[k]) + 1)
dp[c][k] = max(dp[c][k],max(prevdpka[k],prevdpla[k]) + 1)
dpmax = max(dpmax,dp[k][c],dp[c][k])
update(k,c)
update(c,k)
elif a == c:
for k in range(N):
dp[k][b] = max(dp[k][b],max(prevdpka[k],prevdpla[k]) + 1)
dp[b][k] = max(dp[b][k],max(prevdpka[k],prevdpla[k]) + 1)
dpmax = max(dpmax,dp[k][b],dp[b][k])
update(k,b)
update(b,k)
elif b == c:
for k in range(N):
dp[k][a] = max(dp[k][a],max(prevdpkb[k],prevdplb[k]) + 1)
dp[a][k] = max(dp[a][k],max(prevdpkb[k],prevdplb[k]) + 1)
dpmax = max(dpmax,dp[k][a],dp[a][k])
update(k,a)
update(a,k)
dp[b][c] = max(dp[b][c],dpd[a] + 1)
dp[c][b] = max(dp[c][b],dpd[a] + 1)
dp[a][c] = max(dp[a][c],dpd[b] + 1)
dp[c][a] = max(dp[c][a],dpd[b] + 1)
dp[a][b] = max(dp[a][b],dpd[c] + 1)
dp[b][a] = max(dp[b][a],dpd[c] + 1)
dpmax = max(dpmax,dp[b][c],dp[a][c],dp[a][b],dp[c][b],dp[c][a],dp[b][a])
dp[a][b] = max(0,dp[a][b],prevdpmax)
dp[b][a] = max(0,dp[b][a],prevdpmax)
dp[b][c] = max(0,dp[b][c],prevdpmax)
dp[c][b] = max(0,dp[c][b],prevdpmax)
dp[a][c] = max(0,dp[a][c],prevdpmax)
dp[c][a] = max(0,dp[c][a],prevdpmax)
update(a,b)
update(a,c)
update(b,a)
update(b,c)
update(c,a)
update(c,b)
for j in range(N):
dp[j][a] = max(dp[j][a],prevdpmaxj[j])
dp[j][b] = max(dp[j][b],prevdpmaxj[j])
dp[j][c] = max(dp[j][c],prevdpmaxj[j])
dp[a][j] = max(dp[a][j],prevdpmaxj[j])
dp[b][j] = max(dp[b][j],prevdpmaxj[j])
dp[c][j] = max(dp[c][j],prevdpmaxj[j])
update(j,a)
update(j,b)
update(j,c)
update(a,j)
update(b,j)
update(c,j)
print(max(dp[N-2][N-1],dp[N-1][N-2])+score)
```
Yes
| 103,435 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
X = [[-10**10] * (N+1) for _ in range(N+1)]
X[A[0]][A[1]] = 0
ans = 0
ma = 0
MA = [-10**10] * (N+1)
MA[A[0]] = MA[A[1]] = 0
for i in range(N):
B = A[i*3+2:i*3+5]
cnt = len(set(B))
if cnt == 1:
ans += 1
elif cnt == 2:
a1 = B[0]^B[1]^B[2]
a2 = (sum(B) - a1) // 2
X[a1], X[a1][a2], X[a2][a2] = [max(X[a2][j] + 1, X[j][a2] + 1, MA[j]) for j in range(N+1)], max(X[a1][a2], X[a2][a2] + 1, ma), max(ma, X[a2][a2], X[a1][a1] + 1)
X[a2] = [max(X[a2][j], MA[j]) for j in range(N+1)]
MA = [max(m, x1, x2) for m, x1, x2 in zip(MA, X[a1], X[a2])]
MA[a1] = max(MA[a1], max(X[a1]), X[a2][a1])
MA[a2] = max(MA[a2], X[a1][a2], X[a2][a2])
ma = max(ma, MA[a1], MA[a2])
else:
a, b, c = B
# X[a][b], X[a][c], X[b][c] = max(X[a][b], ma, X[c][c] + 1), max(X[a][c], ma, X[b][b] + 1), max(X[b][c], ma, X[a][a] + 1)
X[a][b], X[a][c], X[b][c] = max(ma, X[c][c] + 1), max(ma, X[b][b] + 1), max(ma, X[a][a] + 1)
X[a] = [max(x, m) for x, m in zip(X[a], MA)]
X[b] = [max(x, m) for x, m in zip(X[b], MA)]
X[c] = [max(x, m) for x, m in zip(X[c], MA)]
MA[a] = max(MA[a], max(X[a]))
MA[b] = max(MA[b], X[a][b], max(X[b]))
MA[c] = max(MA[c], X[a][c], X[b][c], max(X[c]))
MA = [max(m, xa, xb, xc) for m, xa, xb, xc in zip(MA, X[a], X[b], X[c])]
ma = max(ma, MA[a], MA[b], MA[c])
print(ans + max(ma, max(max(x) for x in X)))
```
Yes
| 103,436 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
def solve(n, aaa):
dp = [[-1] * n for _ in range(n)]
dp_max = [-1] * n
ans = 0
a0, a1 = sorted(aaa[:2])
dp[a0][a1] = 0
dp_max[a0] = 0
dp_max[a1] = 0
for k in range(n - 1):
a0, a1, a2 = sorted(aaa[2 + 3 * k:5 + 3 * k])
if a0 == a1 and a1 == a2:
ans += 1
continue
dp_max_max = max(dp_max)
update_tasks = []
if a0 == a1 or a1 == a2:
rev = a1 == a2
if rev:
a0, a2 = a2, a0
for b in range(n):
c, d = min(a0, b), max(a0, b)
e, f = min(a2, b), max(a2, b)
update_tasks.append((e, f, dp_max[b]))
if dp[c][d] != -1:
update_tasks.append((e, f, dp[c][d] + 1))
update_tasks.append((a0, a0, dp_max_max))
if dp[a2][a2] != -1:
update_tasks.append((a0, a0, dp[a2][a2] + 1))
if rev:
update_tasks.append((a2, a0, dp_max_max))
else:
update_tasks.append((a0, a2, dp_max_max))
else:
update_tasks.append((a0, a1, dp_max_max))
update_tasks.append((a0, a2, dp_max_max))
update_tasks.append((a1, a2, dp_max_max))
if dp[a0][a0] != -1:
update_tasks.append((a1, a2, dp[a0][a0] + 1))
if dp[a1][a1] != -1:
update_tasks.append((a0, a2, dp[a1][a1] + 1))
if dp[a2][a2] != -1:
update_tasks.append((a0, a1, dp[a2][a2] + 1))
for b in range(n):
b_max = dp_max[b]
for a in (a0, a1, a2):
c, d = min(a, b), max(a, b)
update_tasks.append((c, d, b_max))
for a, b, c in update_tasks:
dp[a][b] = max(dp[a][b], c)
dp_max[a] = max(dp_max[a], c)
dp_max[b] = max(dp_max[b], c)
a = aaa[-1]
dp[a][a] += 1
return max(map(max, dp)) + ans
n = int(input())
aaa = [a - 1 for a in map(int, input().split())]
ans = solve(n, aaa)
print(ans)
```
Yes
| 103,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input().strip())+2
A = list(map(int,input().split()))+[N-1,N]
INF = N
dp = [[-INF for j in range(N)] for i in range(N)]
dpmax = -INF
dpmaxj = [-INF for i in range(N)]
score = 0
a,b,c,d,e = [i-1 for i in sorted(A[:5])]
if a == b == c:
dp[d][e] = dp[e][d] = dpmaxj[d] = dpmaxj[e] = dpmax = 1
elif b == c == d:
dp[a][e] = dp[e][a] = dpmaxj[a] = dpmaxj[e] = dpmax = 1
elif c == d == e:
dp[a][b] = dp[b][a] = dpmaxj[a] = dpmaxj[b] = dpmax = 1
else:
L = [a,b,c,d,e]
for i in range(5):
for j in range(i+1,5):
p,q = L[i],L[j]
dp[p][q] = dp[q][p] = dpmaxj[p] = dpmaxj[q] = 0
dpmax = 0
def update(i,j):
dpmaxj[i] = max(dpmaxj[i],dp[i][j])
dpmaxj[j] = max(dpmaxj[j],dp[i][j])
for i in range(N-3):
prevdpmax = dpmax
prevdpmaxj = dpmaxj[:]
dpd = [dp[k][k] for k in range(N)]
a,b,c = A[i*3+5:i*3+8]
a -= 1
b -= 1
c -= 1
prevdpka = [dp[i][a] for i in range(N)]
prevdpla = [dp[a][i] for i in range(N)]
prevdpkb = [dp[i][b] for i in range(N)]
prevdplb = [dp[b][i] for i in range(N)]
if a == b == c:
score += 1
continue
elif a == b:
for k in range(N):
dp[k][c] = max(dp[k][c],max(prevdpka[k],prevdpla[k]) + 1); update(k,c)
dp[c][k] = max(dp[c][k],max(prevdpka[k],prevdpla[k]) + 1); update(c,k)
dpmax = max(dpmax,dp[k][c],dp[c][k])
elif a == c:
for k in range(N):
dp[k][b] = max(dp[k][b],max(prevdpka[k],prevdpla[k]) + 1); update(k,b)
dp[b][k] = max(dp[b][k],max(prevdpka[k],prevdpla[k]) + 1); update(b,k)
dpmax = max(dpmax,dp[k][b],dp[b][k])
elif b == c:
for k in range(N):
dp[k][a] = max(dp[k][a],max(prevdpkb[k],prevdplb[k]) + 1); update(k,a)
dp[a][k] = max(dp[a][k],max(prevdpkb[k],prevdplb[k]) + 1); update(a,k)
dpmax = max(dpmax,dp[k][a],dp[a][k])
dp[b][c] = max(dp[b][c],dpd[a] + 1); update(b,c)
dp[c][b] = max(dp[c][b],dpd[a] + 1); update(c,b)
dp[a][c] = max(dp[a][c],dpd[b] + 1); update(a,c)
dp[c][a] = max(dp[c][a],dpd[b] + 1); update(c,a)
dp[a][b] = max(dp[a][b],dpd[c] + 1); update(a,b)
dp[b][a] = max(dp[b][a],dpd[c] + 1); update(b,a)
dpmax = max(dpmax,dp[b][c],dp[a][c],dp[a][b],dp[c][b],dp[c][a],dp[b][a])
dp[a][b] = max(0,dp[a][b],prevdpmax); update(a,b)
dp[b][a] = max(0,dp[b][a],prevdpmax); update(b,a)
dp[b][c] = max(0,dp[b][c],prevdpmax); update(b,c)
dp[c][b] = max(0,dp[c][b],prevdpmax); update(c,b)
dp[a][c] = max(0,dp[a][c],prevdpmax); update(a,c)
dp[c][a] = max(0,dp[c][a],prevdpmax); update(c,a)
for j in range(N):
dp[j][a] = max(dp[j][a],prevdpmaxj[j]); update(j,a)
dp[j][b] = max(dp[j][b],prevdpmaxj[j]); update(j,b)
dp[j][c] = max(dp[j][c],prevdpmaxj[j]); update(j,c)
dp[a][j] = max(dp[a][j],prevdpmaxj[j]); update(a,j)
dp[b][j] = max(dp[b][j],prevdpmaxj[j]); update(b,j)
dp[c][j] = max(dp[c][j],prevdpmaxj[j]); update(c,j)
print(max(dp[N-2][N-1],dp[N-1][N-2])+score)
```
Yes
| 103,438 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
n = int(input())
A = list(map(lambda x:int(x)-1,input().split()))
two = [0]*n
one = [0]*n
two[A[0]] += 1
two[A[1]] += 1
ans = 0
for i in range(n-1):
nxt = A[3*i+2:3*i+5]
nxt.sort()
if nxt[0] == nxt[1] == nxt[2]:
ans += 1
continue
if nxt[1] == nxt[2]:
nxt.reverse()
if nxt[0] == nxt[1]:
if one[nxt[0]] >= 1:
ans += 1
one = [0]*n
two[nxt[2]] += 1
continue
if two[nxt[0]] >= 1:
ans += 1
two[nxt[0]] -= 1
one = [0]*n
for i in range(n):
one[i] += two[i]
two = [0]*n
two[nxt[2]] += 1
continue
if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
else:
two[nxt[2]] += 1
two[nxt[0]] += 2
continue
if two[nxt[0]] >= 1 and two[nxt[0]] + one[nxt[0]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
two[nxt[1]] += 1
two[nxt[2]] += 1
continue
if two[nxt[1]] >= 1 and two[nxt[1]] + one[nxt[1]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
two[nxt[0]] += 1
two[nxt[2]] += 1
continue
if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
ans += 1
two = [0]*n
one = [0]*n
two[nxt[1]] += 1
two[nxt[0]] += 1
continue
for i in range(3):
two[nxt[i]] += 1
if two[A[-1]] >= 1 and one[A[-1]] + two[A[-1]]>= 2:
ans += 1
print(ans)
```
No
| 103,439 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
import numpy as np
n = int(input())
A = [int(i) for i in input().split()]
ans = 0
stack = 0
count = np.zeros(n)
for i in range(3*n):
stack += 1
count[A[i]-1] += 1
if count[A[i]-1] == 3 and stack < 6:
ans += 1
count[A[i]-1] = 0
stack -= 3
elif count[A[i]-1] == 3 and stack > 5:
count = np.zeros(n)
ans += 1
stack -= 3
print(ans)
```
No
| 103,440 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
from collections import defaultdict
n = int(input())
A = list(map(int, input().split()))
remain = defaultdict(int)
must = defaultdict(int)
if n == 1:
if A[0] == A[1] and A[1] == A[2]:
print(1)
exit()
else:
print(0)
exit()
ans = 0
remain[A[0]] += 1
remain[A[1]] += 1
for i in range(1, n):
nex = defaultdict(int)
nex1 = A[3 * i - 1]
nex2 = A[3 * i]
nex3 = A[3 * i + 1]
if nex1 == nex2 and nex2 == nex3:
ans += 1
continue
elif nex1 == nex2:
if remain[nex1] >= 1:
remain[nex1] -= 1
ans += 1
remain[nex3] += 1
elif remain[nex3] >= 2:
remain[nex3] -= 2
ans += 1
remain = defaultdict(int)
remain[nex1] += 2
else:
remain[nex1] += 2
remain[nex3] += 1
elif nex2 == nex3:
if remain[nex2] >= 1:
remain[nex2] -= 1
ans += 1
remain[nex1] += 1
elif remain[nex1] >= 2:
remain[nex1] -= 2
ans += 1
remain = defaultdict(int)
remain[nex2] += 2
else:
remain[nex2] += 2
remain[nex1] += 1
elif nex3 == nex1:
if remain[nex1] >= 1:
remain[nex1] -= 1
ans += 1
remain[nex2] += 1
elif remain[nex2] >= 2:
remain[nex2] -= 2
ans += 1
remain = defaultdict(int)
remain[nex1] += 2
else:
remain[nex1] += 2
remain[nex2] += 1
else:
if remain[nex1] >= 2:
remain[nex1] -= 2
ans += 1
remain = defaultdict(int)
remain[nex2] += 1
remain[nex3] += 1
elif remain[nex2] >= 2:
remain[nex2] -= 2
ans += 1
remain = defaultdict(int)
remain[nex3] += 1
remain[nex1] += 1
elif remain[nex3] >= 2:
remain[nex3] -= 2
ans += 1
remain = defaultdict(int)
remain[nex1] += 1
remain[nex2] += 1
else:
remain[nex1] += 1
remain[nex2] += 1
remain[nex3] += 1
if remain[A[-1]] >= 2:
ans += 1
print(ans)
```
No
| 103,441 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.
You will do the following operation N-1 times:
* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.
Find the maximum number of points you can gain.
Constraints
* 1 \leq N \leq 2000
* 1 \leq A_i \leq N
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 \cdots A_{3N}
Output
Print the maximum number of points you can gain.
Examples
Input
2
1 2 1 2 2 1
Output
2
Input
3
1 1 2 2 3 3 3 2 1
Output
1
Input
3
1 1 2 2 2 3 3 3 1
Output
3
Submitted Solution:
```
# -*- coding: utf-8 -*-
#############
# Libraries #
#############
import sys
input = sys.stdin.readline
import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache
#############
# Constants #
#############
MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"
#############
# Functions #
#############
######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]
#####Shorten#####
def DD(arg): return defaultdict(arg)
#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)
######Combination######
kaijo_memo = []
def kaijo(n):
if(len(kaijo_memo) > n): return kaijo_memo[n]
if(len(kaijo_memo) == 0): kaijo_memo.append(1)
while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
return kaijo_memo[n]
gyaku_kaijo_memo = []
def gyaku_kaijo(n):
if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
return gyaku_kaijo_memo[n]
def nCr(n,r):
if n == r: return 1
if n < r or r < 0: return 0
ret = 1
ret = ret * kaijo(n) % MOD
ret = ret * gyaku_kaijo(r) % MOD
ret = ret * gyaku_kaijo(n-r) % MOD
return ret
######Factorization######
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1: arr.append([temp, 1])
if arr==[]: arr.append([n, 1])
return arr
#####MakeDivisors######
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
return divisors
#####MakePrimes######
def make_primes(N):
max = int(math.sqrt(N))
seachList = [i for i in range(2,N+1)]
primeNum = []
while seachList[0] <= max:
primeNum.append(seachList[0])
tmp = seachList[0]
seachList = [i for i in seachList if i % tmp != 0]
primeNum.extend(seachList)
return primeNum
#####GCD#####
def gcd(a, b):
while b: a, b = b, a % b
return a
#####LCM#####
def lcm(a, b):
return a * b // gcd (a, b)
#####BitCount#####
def count_bit(n):
count = 0
while n:
n &= n-1
count += 1
return count
#####ChangeBase#####
def base_10_to_n(X, n):
if X//n: return base_10_to_n(X//n, n)+[X%n]
return [X%n]
def base_n_to_10(X, n):
return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))
def base_10_to_n_without_0(X, n):
X -= 1
if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
return [X%n]
#####IntLog#####
def int_log(n, a):
count = 0
while n>=a:
n //= a
count += 1
return count
#############
# Main Code #
#############
N = I()+2
if N == 3:
print(1)
exit()
A = IL()+[N-1,N]
dp = [[0 for j in range(N)] for i in range(N)]
dpmax = 0
dpmaxk = [0 for i in range(N)]
dpmaxl = [0 for i in range(N)]
score = 0
a,b,c,d,e = sorted(A[:5])
a -= 1
b -= 1
c -= 1
d -= 1
e -= 1
if a == b == c:
dp[d][e] = 1
dp[e][d] = 1
elif b == c == d:
dp[a][e] = 1
dp[e][a] = 1
elif c == d == e:
dp[a][b] = 1
dp[b][a] = 1
for i in range(N-3):
prevdpmax = dpmax
prevdpmaxk = dpmaxk[:]
prevdpmaxl = dpmaxl[:]
dpd = [dp[k][k] for k in range(N)]
a,b,c = A[i*3+5:i*3+8]
a -= 1
b -= 1
c -= 1
if a == b == c:
score += 1
continue
elif a == b:
for k in range(N):
dp[k][c] = max(dp[k][a],dp[a][k]) + 1
dp[c][k] = max(dp[k][a],dp[a][k]) + 1
dpmax = max(dpmax,dp[k][c])
dpmaxk[k] = max(dpmaxk[k],dp[k][c])
dpmaxl[c] = max(dpmaxl[c],dp[k][c])
elif a == c:
for k in range(N):
dp[k][b] = max(dp[k][a],dp[a][k]) + 1
dp[b][k] = max(dp[k][a],dp[a][k]) + 1
dpmax = max(dpmax,dp[k][b])
dpmaxk[k] = max(dpmaxk[k],dp[k][b])
dpmaxl[b] = max(dpmaxl[b],dp[k][b])
elif b == c:
for k in range(N):
dp[k][a] = max(dp[k][b],dp[b][k]) + 1
dp[a][k] = max(dp[k][b],dp[b][k]) + 1
dpmax = max(dpmax,dp[k][a])
dpmaxk[k] = max(dpmaxk[k],dp[k][a])
dpmaxl[a] = max(dpmaxl[a],dp[k][a])
dp[b][c] = max(dp[b][c],dpd[a] + 1)
dp[c][b] = max(dp[c][b],dpd[a] + 1)
dp[a][c] = max(dp[a][c],dpd[b] + 1)
dp[c][a] = max(dp[c][a],dpd[b] + 1)
dp[a][b] = max(dp[a][b],dpd[c] + 1)
dp[b][a] = max(dp[b][a],dpd[c] + 1)
dpmax = max(dpmax,dp[b][c],dp[a][c],dp[a][b])
dpmaxk[b] = max(dpmaxk[b],dp[b][c],dp[b][a])
dpmaxl[c] = max(dpmaxl[c],dp[b][c],dp[a][c])
dpmaxk[a] = max(dpmaxk[a],dp[a][c],dp[a][b])
dpmaxl[c] = max(dpmaxl[c],dp[a][c],dp[b][c])
dpmaxk[a] = max(dpmaxk[a],dp[a][b],dp[a][c])
dpmaxl[b] = max(dpmaxl[b],dp[a][b],dp[c][b])
dp[a][b] = prevdpmax
dp[b][a] = prevdpmax
dp[b][c] = prevdpmax
dp[c][b] = prevdpmax
dp[a][c] = prevdpmax
dp[c][a] = prevdpmax
for j in range(N):
dp[j][a] = prevdpmaxk[j]
dp[j][b] = prevdpmaxk[j]
dp[j][c] = prevdpmaxk[j]
dp[a][j] = prevdpmaxl[j]
dp[b][j] = prevdpmaxl[j]
dp[c][j] = prevdpmaxl[j]
z = A[-1]-1
print(max(dp[N-2][N-1],dp[N-1][N-2])+score)
```
No
| 103,442 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
a=0
N=int(input())
for i in range(1,N+1):
if i%3!=0 and i%5!=0:
a+=i
print(a)
```
| 103,443 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
s=0
N=int(input())
for a in range(1,N+1):
if a%3!=0 and a%5!=0:
s+=a
print(s)
```
| 103,444 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
print(sum([i for i in range(1, int(input())+1) if i % 3 != 0 and i % 5 != 0]))
```
| 103,445 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
n=int(input())
ct=0
for i in range(1,n+1):
if i%3!=0 and i%5!=0: ct+=i
print(ct)
```
| 103,446 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
N = int(input())
print(sum([i for i in range(1, N+1) if i%5!=0 and i%3!=0]))
```
| 103,447 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
S = int(input())
print(sum([i+1 for i in range(S) if not ((i+1)%3==0 or (i+1)%5==0 )]))
```
| 103,448 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
n = int(input())
print(sum(filter(lambda x : x % 3 != 0 and x % 5 != 0, range(1, n+1))))
```
| 103,449 |
Provide a correct Python 3 solution for this coding contest problem.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
"Correct Solution:
```
print(sum([i for i in range(int(input()) + 1) if (i % 3 != 0 and i % 5 != 0)]))
```
| 103,450 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
N=int(input())
print(sum([i for i in range(1,N+1) if i%3 and i%5]))
```
Yes
| 103,451 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
n=int(input())
a=0
for i in range(n):
if (i+1)%3!=0 and (i+1)%5!=0 :
a+=i+1
print(a)
```
Yes
| 103,452 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
N = int(input())
print(sum([i for i in range(1, N + 1) if (i % 3) != 0 and (i % 5) != 0]))
```
Yes
| 103,453 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
n=list(range(1,int(input())+1))
print(sum([i for i in n if i%3!=0and i%5!=0]))
```
Yes
| 103,454 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
n = input()
sum = 0
for i in range(1, n+1):
if i % 3 != 0 and i % 5 != 0:
sum += i
print(sum)
```
No
| 103,455 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
n = int(input())
s = 0
for i in range(1, n):
if i % 3 != 0 and i % 5 != 0:
s += i
print(s)
```
No
| 103,456 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
# coding: utf-8
# Your code here!
N = int(input())
ans = 0
for i in range(N):
if i % 3 == 0 or i % 5 == 0:
pass
else:
ans += i
print(ans)
```
No
| 103,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us define the FizzBuzz sequence a_1,a_2,... as follows:
* If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.
* If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.
* If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.
* If none of the above holds, a_i=i.
Find the sum of all numbers among the first N terms of the FizzBuzz sequence.
Constraints
* 1 \leq N \leq 10^6
Input
Input is given from Standard Input in the following format:
N
Output
Print the sum of all numbers among the first N terms of the FizzBuzz sequence.
Examples
Input
15
Output
60
Input
1000000
Output
266666333332
Submitted Solution:
```
n = int(input())
fb = 0
for i in range(n):
if(i%5 != 0 and i%3 != 0):
fb = fb + i
print(fb)
```
No
| 103,458 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
m1,d1=map(int,input().split())
m2,d2=map(int,input().split())
if d2 < d1:
print(1)
else:print(0)
```
| 103,459 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
m1,d = map(int,input().split())
m2,d = map(int,input().split())
print(int(m1!=m2))
```
| 103,460 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
A = input().split()
B = input().split()
if A[0] == B[0]:
print("0")
else:
print("1")
```
| 103,461 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
n,m=map(int,input().split())
s,t=map(int,input().split())
print(s-n)
```
| 103,462 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
m,d = map(int,input().split())
mm,dd = map(int,input().split())
if dd == 1:
print(1)
else:
print(0)
```
| 103,463 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
M1,_,M2,_=map(int,open(0).read().split())
print(M2-M1)
```
| 103,464 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
m1,d1=map(int,input().split())
m2,d2=map(int,input().split())
print("1" if d2==1 else "0")
```
| 103,465 |
Provide a correct Python 3 solution for this coding contest problem.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
"Correct Solution:
```
M1 = input()
M2,D2 = map(int,input().split())
if D2 == 1:
print(1)
else :
print(0)
```
| 103,466 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
M1,D1=map(int,input().split())
M2,D2=map(int,input().split())
print(0 if M1==M2 else 1)
```
Yes
| 103,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
m1,d1=map(int,input().split())
m2,d2=map(int,input().split())
print(int(m1!=m2))
```
Yes
| 103,468 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
input()
_, D2 = input().strip().split()
if D2 == "1":
print("1")
else:
print("0")
```
Yes
| 103,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
D1=input()
M2, D2 = [int(n) for n in input().split()]
print(1 if D2==1 else 0)
```
Yes
| 103,470 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
for i in range(2):
a[i], b[i] = map(int, input().split())
if b[1] = 1:
print(1)
else:
print(0)
```
No
| 103,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
N = int(input())
S = input()
ans = 0
for i in range(10):
l = 0
while(True):
if(i == int(S[l])):
for j in range(10):
m = l + 1
while(True):
if(j == int(S[m])):
for k in range(10):
for n in range(m+1, N):
if(k == int(S[n])):
ans += 1
break
m += 1
if(m >= N - 1 or j == int(S[m - 1])):
break
l += 1
if(l >= N - 2 or i == int(S[l - 1])):
break
print(ans)
```
No
| 103,472 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
M1,D1=map(int,input().split())
if M1== 1 or 3 or 5 or 7 or 8 or 10:
if D1=31:
print(1)
else:
print(0)
elif M1== 4 or 6 or 9 or 11 :
if D1=30:
print(1)
else:
print(0)
else:
if D1 ==28:
print(1)
else:
print(0)
```
No
| 103,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.
Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.
Constraints
* Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
* The date 2019-M_2-D_2 follows 2019-M_1-D_1.
Input
Input is given from Standard Input in the following format:
M_1 D_1
M_2 D_2
Output
If the date 2019-M_1-D_1 is the last day of a month, print `1`; otherwise, print `0`.
Examples
Input
11 16
11 17
Output
0
Input
11 30
12 1
Output
1
Submitted Solution:
```
m1=int(input())
d1=int(input())
m2=int(input())
d2=int(input())
if m1+1==m2:
print(1)
else:
print(0)
```
No
| 103,474 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
from functools import lru_cache
n, a = int(input()), list(map(int, input().split()))
@lru_cache(maxsize=None)
def f(x, y, l, r):
if r - l + 1 <= 2:
return 0
return min(a[i] * (x + y) + f(x, x + y, l, i) + f(x + y, y, i, r) for i in range(l + 1 , r))
print(a[0] + a[-1] + f(1, 1, 0, n - 1))
```
| 103,475 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
"""
https://atcoder.jp/contests/agc035/tasks/agc035_d
毎回1枚カードが減り、寄与が2倍になる
両端のカードは消せない → 寄与は1倍のまま
端から2枚目のカードしか選ばなくていいみたいなの無いかな?
端から2枚目を選ぶと、1つは端の寄与になり、もう一つは動の寄与になる
つまり、動の寄与の総和は変化しない
そうでないのを選ぶと、動の寄与に2倍かかる
→ 2**18 になっていい感じのオーダーにはなる
検証:
5枚で真ん中を最初に選ぶべきなのを作れるか?
0,X,Y,Z,0
0,X+Y,Y+Z,0
0,X+2Y+Z,Y+Z
X+2Y+Z,X+3Y+2Z
→2X+3Y+3Z or 3X+3Y+2Z (真ん中を選んだ場合)
0,X,Y,Z,0
X,X+Y,Z,0 → X,X+Y+Z,Z → 2X+Y+Z,X+Y+2Z → 3X+2Y+3Z
2X+Y,X+Y+Z,0 → 3X+2Y+Z,X+Y+Z → 4X+3Y+2Z
すなわち、真ん中を選んだ方がいい場合もある…
最後まで残した数は2倍だけの寄与で済む
最後まで残す数を決め打ちする?
最後の数で左右を分ける
→さらにその区間で最後に残すやつは3倍の寄与で済む
1 4 3 5 2 5 3 4 1
→こんな感じ
あとは分割統治で、
ある点で区切った場合の左右の最小を足して上に返せばよい
計算量は?
長さ1の場合1
2の場合 1+1 = 2
3の場合 2+1+1+2 = 6
4の場合 6+
dp[0] = 0
dp[1] = 1
dp[n] = 2*(dp[n-1]+dp[n-2]…+dp[0])
dp[16] = 9565938 なので十分可能
"""
from sys import stdin
def want(l,r,lb,rb):
if l > r:
return 0
elif l == r:
return A[l] * (lb+rb)
nmin = float("inf")
for i in range(l,r+1):
nmin = min(nmin , A[i]*(lb+rb) + want(l,i-1,lb,lb+rb) + want(i+1,r,lb+rb,rb) )
return nmin
N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
print (A[0] + A[-1] + want(1,N-2,1,1) )
```
| 103,476 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
from functools import lru_cache
def solve(n, aaa):
@lru_cache(maxsize=None)
def search_min(li, ri, lc, rc):
w = ri - li
if w == 1:
return 0
lrc = lc + rc
if w == 2:
return aaa[li + 1] * lrc
if w == 3:
a1, a2 = aaa[li + 1], aaa[li + 2]
return (a1 + a2) * lrc + min(a1 * lc, a2 * rc)
ret = min(aaa[i] * lrc + search_min(li, i, lc, lrc) + search_min(i, ri, lrc, rc) for i in range(li + 1, ri))
return ret
return search_min(0, n - 1, 1, 1) + aaa[0] + aaa[-1]
n = int(input())
aaa = list(map(int, input().split()))
print(solve(n, aaa))
```
| 103,477 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
from functools import lru_cache
N = int(input())
A = list(map(int, input().split()))
@lru_cache(maxsize=None)
def dfs(l, r, wl, wr):
if r <= l + 1:
return 0
v = min(dfs(l, m, wl, wl+wr) + dfs(m, r, wl+wr, wr) + A[m] * (wl+wr) for m in range(l+1, r))
return v
print(dfs(0, N-1, 1, 1) + A[0] + A[N-1])
```
| 103,478 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
from functools import lru_cache
N = int(input())
A = list(map(int, input().split()))
@lru_cache(None)
def f(l, r, xl, xr):
if l+1 == r:
return xl * A[l] + xr * A[r]
tmp = float("inf")
for m in range(l+1, r):
v = f(l, m, xl, xl+xr) + f(m, r, xl+xr, xr) - (xl+xr) * A[m]
tmp = min(tmp, v)
return tmp
print(f(0, N-1, 1, 1))
```
| 103,479 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
# ikatakosさんの提出を見てlru_cacheなるものを知った。
# まだまだ知らないことがたくさんありますね。
from functools import lru_cache
N = int(input())
A = [int(i) for i in input().split()]
@lru_cache(maxsize=None)
def rec(l, r, xl, xr) :
if l + 1 == r :
return 0
return min(rec(l, m, xl, xl+xr)+rec(m, r, xl+xr, xr)+A[m]*(xl+xr) for m in range(l+1, r))
print(A[0] + rec(0, N-1, 1, 1) + A[N-1])
```
| 103,480 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
from functools import lru_cache
def solve(n, aaa):
@lru_cache(maxsize=None)
def search_min(li, ri, lc, rc):
w = ri - li
base = aaa[li] * lc
if w == 1:
return base
lrc = lc + rc
if w == 2:
return base + aaa[li + 1] * lrc
if w == 3:
a1, a2 = aaa[li + 1], aaa[li + 2]
return base + (a1 + a2) * lrc + min(a1 * lc, a2 * rc)
ret = min(search_min(li, i, lc, lrc) + search_min(i, ri, lrc, rc) for i in range(li + 1, ri))
return ret
return search_min(0, n - 1, 1, 1) + aaa[-1]
n = int(input())
aaa = list(map(int, input().split()))
print(solve(n, aaa))
```
| 103,481 |
Provide a correct Python 3 solution for this coding contest problem.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
"Correct Solution:
```
N=int(input())
A=list(map(int,input().split()))
memo={}
def dp(i,j,L,R):
if (i,j,L,R) in memo:
return memo[(i,j,L,R)]
if i==j:
memo[(i,j,L,R)]=0
memo[(i,j,L,R)]=(L+R)*A[i]
return (L+R)*A[i]
elif i>j:
return 0
ans=10**18
for k in range(i,j+1):
test=dp(i,k-1,L,L+R)+dp(k+1,j,L+R,R)+A[k]*(L+R)
ans=min(ans,test)
memo[(i,j,L,R)]=0
memo[(i,j,L,R)]=ans
return ans
res=dp(1,N-2,1,1)
print(res+A[0]+A[-1])
```
| 103,482 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
Submitted Solution:
```
#YouTube
import sys
sys.setrecursionlimit(10**6)
def kukan(l,r,el=1,er=1):
em=el+er
if l+1==r:
return 0
if l+2==r:
return a[l+1]*em
t=(l,r,el,er)
if t in memo:
return memo[t]
re=10**11
for m in range(l+1,r):
tmp=kukan(l,m,el,em)+kukan(m,r,em,er)+a[m]*em
if tmp<re:
re=tmp
memo[t]=re
return re
n=int(input())
a=list(map(int,input().split()))
memo={}
print(a[0]+kukan(0,n-1)+a[-1])
```
Yes
| 103,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
AtCoder A
"""
import sys, math, random
import numpy as np
# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
# def main():
# N=I()
# A= list(map(int,input().split()))
# A=np.asarray(A)
# while len(A)>2:
# print(A)
# i=np.argmin(A[1:len(A)-1])
# i=i+1
# print(i)
# A[i-1]+=A[i]
# A[i+1]+=A[i]
# A=np.delete(A,i)
# print(A[0]+A[1])
# print(A,B)
def main():
N=I()
a= list(map(int,input().split()))
A=np.asarray(a)
B=np.asarray(a)
while len(A)>2:
# print(A)
b=np.argmin(B[1:len(B)-1])
a=np.argmin(A[1:len(A)-1])
if B[b+1]<A[a+1]:
i=b+1
else:
i=a+1
# i=i+1
# print(i)
A[i-1]+=A[i]
A[i+1]+=A[i]
A=np.delete(A,i)
B=np.delete(B,i)
print(A[0]+A[1])
# print(A,B)
if __name__ == "__main__":
main()
```
No
| 103,484 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
for i in range(n - 2):
k = a.index(min(a[1:len(a) - 1]))
p = a[k]
a[k - 1] += p
a[k + 1] += p
del a[k]
print(sum(a))
```
No
| 103,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
Submitted Solution:
```
import sys
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
NN = N
# N = 2になるまで繰り返す
if N >= 5:
for n in range(N-5):
m = np.min(A[1:NN-1])
sum3i = np.max(A)*3+1
p = np.argmin(A[1:NN-1])
p += 1
dd = min([NN-p+1, p])
for i in range(1,NN-1):
if A[i] == m:
sum3 = A[i-1] + A[i+1]
if sum3 < sum3i:
sum3i = sum3
p = i
elif sum3 == sum3i:
d = min([NN-i-1, i])
if d < dd:
dd = d
p = i
#print(A[1:NN-1])
#p = np.argmin(A[1:NN-1])
#p = int(p+1)
A[p-1] += A[p]
A[p+1] += A[p]
A = np.delete(A,p)
NN -= 1
B = np.zeros(5)
B[0] = A[0] + 3*A[1] + 2*A[2] + 3*A[3] + A[4]
B[1] = A[0] + 4*A[1] + 3*A[2] + 2*A[3] + A[4]
B[2] = A[0] + 2*A[1] + 5*A[2] + 3*A[3] + A[4]
B[3] = A[0] + 3*A[1] + 5*A[2] + 2*A[3] + A[4]
B[4] = A[0] + 2*A[1] + 3*A[2] + 4*A[3] + A[4]
print(int(np.min(B)))
else:
for n in range(N-2):
m = np.min(A[1:NN-1])
sum3i = np.max(A)*3+1
p = np.argmin(A[1:NN-1])
p += 1
dd = min([NN-p+1, p])
for i in range(1,NN-1):
if A[i] == m:
sum3 = A[i-1] + A[i+1]
if sum3 < sum3i:
sum3i = sum3
p = i
elif sum3 == sum3i:
d = min([NN-i-1, i])
if d < dd:
dd = d
p = i
#print(A[1:NN-1])
#p = np.argmin(A[1:NN-1])
#p = int(p+1)
A[p-1] += A[p]
A[p+1] += A[p]
A = np.delete(A,p)
NN -= 1
print(np.sum(A))
```
No
| 103,486 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.
Snuke will repeat the following operation until two cards remain:
* Choose three consecutive cards from the stack.
* Eat the middle card of the three.
* For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.
* Return the two cards to the original position in the stack, without swapping them.
Find the minimum possible sum of the integers written on the last two cards remaining.
Constraints
* 2 \leq N \leq 18
* 0 \leq A_i \leq 10^9 (1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible sum of the integers written on the last two cards remaining.
Examples
Input
4
3 1 4 2
Output
16
Input
6
5 2 4 1 6 9
Output
51
Input
10
3 1 4 1 5 9 2 6 5 3
Output
115
Submitted Solution:
```
N = int(input())
A = [int(v) for v in input().split()][::-1]
while len(A) > 2:
ans = [(i, a+c+b+b) for i, (a, b, c) in enumerate(zip(A[:-2], A[1:-1], A[2:]))]
m = ans[0][1]
i = ans[0][0]
for a in ans[1:]:
if a[1] < m:
m = a[1]
i = a[0]
A = A[:i] + [A[i]+A[i+1], A[i+2]+A[i+1]] + A[i+3:]
print(A[0] + A[1])
```
No
| 103,487 |
Provide a correct Python 3 solution for this coding contest problem.
There is a very long bench. The bench is divided into M sections, where M is a very large integer.
Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:
* We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).
After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of `X` and `-`: the i-th character of the string is `X` if the i-th section from the left in the interval is occupied, and `-` otherwise. Note that the photo is directed. For example, `-X--X` and `X--X-` are different photos.
What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.
Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):
p + \frac{q}{e} + \frac{r}{e^2}
Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.
Constraints
* 1 \leq N \leq 1000
* |s| = N
* s consists of `X` and `-`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print three rational numbers p, q, r, separated by spaces.
Examples
Input
1
X
Output
500000004 0 500000003
Input
3
---
Output
0 0 0
Input
5
X--X-
Output
0 0 1
Input
5
X-X-X
Output
500000004 0 833333337
Input
20
-X--X--X-X--X--X-X-X
Output
0 0 183703705
Input
100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-
Output
0 0 435664291
"Correct Solution:
```
#参考URL
#https://qiita.com/ageprocpp/items/104c051b2ec2086f5a9b
mod=10**9+7
def value(obj):
if type(obj)==modint:
tmp=obj.number
else:
tmp=obj
return tmp%mod
class modint():
number=0
def __init__(self,num):
self.number=num%mod
def __add__(self,other):
tmp=value(other)
return modint(self.number+tmp)
def __iadd__(self,other):
tmp=value(other)
self.number+=tmp
self.number%=mod
return self
def __sub__(self,other):
tmp=value(other)
return modint(self.number-tmp)
def __isub__(self,other):
tmp=value(other)
self.number-=tmp
self.number%=mod
return self
def __mul__(self,other):
tmp=value(other)
return modint(self.number*tmp)
def __imul__(self,other):
tmp=value(other)
self.number*=tmp
self.number%=mod
return self
def __truediv__(self,other):
tmp=value(other)
return self*pow(tmp,mod-2,mod)
def __itruediv(self,other):
tmp=value(other)
self.number=self/tmp
self.number%=mod
return self
def __repr__(self):
return str(self.number)
def __eq__(self,other):
return value(self)==value(other)
def __ne__(self,other):
return value(self)!=value(other)
def __str__(self):
return str(self.number)
def __int__(self):
return self.number
def __hash__(self):
return self.number
class polynomial():
sequence=[0]
def __init__(self,seq):
self.sequence=[]
for i in seq:
self.sequence.append(value(i))
def get(self,x):
res=modint(0)
tmp=modint(1)
for a in self.sequence:
res+=tmp*a
tmp*=x
if tmp==0:
break
return res
def __add__(self,other):
res=[]
for i,a in enumerate(self.sequence):
try:
res[i]=a
except IndexError:
res.append(modint(0))
res[i]=a
for i,a in enumerate(other.sequence):
try:
res[i]+=a
except IndexError:
res.append(modint(0))
res[i]+=a
return polynomial(res)
def __iadd__(self,other):
return self+other
def __sub__(self,other):
res=[]
for i,a in enumerate(self.sequence):
try:
res[i]=a
except IndexError:
res.append(modint(0))
res[i]=a
for i,a in enumerate(other.sequence):
try:
res[i]-=a
except IndexError:
res.append(modint(0))
res[i]-=a
return polynomial(res)
def __isub__(self,other):
return self-other
def indef_integral(self):
res=[modint(0)]
for i,a in enumerate(self.sequence):
res.append(modint(a)/(i+1))
return polynomial(res)
def integrate(self,flag):
#0: 0->x
#1:x->1
if flag==0:
return self.indef_integral()
else:
tmp=self.indef_integral()
return polynomial([tmp.get(1)])-tmp
def __repr__(self):
return str(self.sequence)
def __str__(self):
return str(self.sequence)
class func():
P=polynomial([modint(0)])
Q=polynomial([modint(0)])
C=modint(0)
def __init__(self,poly1,poly2,cnst):
self.P=poly1
self.Q=poly2
self.C=cnst
def __add__(self,other):
PP=self.P+other.P
QQ=self.Q+other.Q
CC=self.C+other.C
return func(PP,QQ,CC)
def __iadd__(self,other):
return self+other
def __sub__(self,other):
PP=self.P-other.P
QQ=self.Q-other.Q
CC=self.C-other.C
return func(PP,QQ,CC)
def __isub__(self,other):
return self-other
def dintegrate(self,flag):
if flag==0:
PP=self.P.integrate(0)+polynomial([self.C])
QQ=self.Q.integrate(0)
CC=modint(-1)*self.C
else:
PP=self.P.integrate(1)
QQ=self.Q.integrate(1)+polynomial([modint(-1)*self.C])
CC=self.C
return func(PP,QQ,CC)
MAX_N=1009
N=int(input())
s=input()
fact=[modint(0) for i in range(MAX_N+1)]
inv=[modint(0) for i in range(MAX_N+1)]
fact[0]=modint(1)
for i in range(MAX_N):
fact[i+1]=fact[i]*(i+1)
inv[-1]=modint(1)/fact[-1]
for i in range(MAX_N-1,-1,-1):
inv[i]=inv[i+1]*(i+1)
dp=[[[[func(polynomial([modint(0)]),polynomial([modint(0)]),modint(0)) for l in range(2)]for k in range(2)]for j in range(2)] for i in range(MAX_N+1)]
if s[0]=="X":
dp[0][0][1][0]=func(polynomial([modint(0)]),polynomial([modint(0)]),modint(1))
else:
dp[0][0][0][1]=func(polynomial([modint(0)]),polynomial([modint(0)]),modint(1))
dp[0][1][1][1]=func(polynomial([modint(1)]),polynomial([modint(0)]),modint(-1))
for i in range(1,N):
if s[i]=="X":
dp[i][0][1][0]+=dp[i-1][1][1][0].dintegrate(0)
dp[i][0][1][0]+=dp[i-1][1][1][1].dintegrate(0)
dp[i][0][1][0]+=dp[i-1][0][0][1].dintegrate(1)
dp[i][0][1][0]+=dp[i-1][0][1][1].dintegrate(1)
dp[i][0][1][0]+=dp[i-1][1][0][1].dintegrate(1)
dp[i][0][1][0]+=dp[i-1][1][1][1].dintegrate(1)
else:
dp[i][0][0][1]+=dp[i-1][1][1][0].dintegrate(0)
dp[i][0][0][1]+=dp[i-1][1][1][1].dintegrate(0)
dp[i][1][1][1]+=dp[i-1][0][1][0].dintegrate(0)
dp[i][1][1][1]+=dp[i-1][0][1][1].dintegrate(0)
dp[i][0][0][1]+=dp[i-1][0][1][0].dintegrate(1)
dp[i][0][0][1]+=dp[i-1][0][1][1].dintegrate(1)
dp[i][0][0][1]+=dp[i-1][1][1][0].dintegrate(1)
dp[i][0][0][1]+=dp[i-1][1][1][1].dintegrate(1)
a=modint(0)
b=modint(0)
c=modint(0)
p = dp[N-1][0][1][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][0][1][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][0][1][1].C
b-= dp[N-1][0][1][1].C
p = dp[N-1][1][1][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][1][1][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][1][1][1].C
b-= dp[N-1][1][1][1].C
for x in range(2):
for i,v in enumerate(dp[N-1][x][1][0].P.sequence):
a+=modint(1)*v*fact[i]
for j in range(i+1):
b-=modint(1)*v*fact[i]*inv[j]
for i,v in enumerate(dp[N-1][x][1][0].Q.sequence):
b+=modint(1)*v*fact[i]
for j in range(i+1):
c-=modint(1)*v*fact[i]*inv[j]
a+=dp[N-1][x][1][0].C/2
c-=dp[N-1][x][1][0].C/2
p = dp[N-1][0][0][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][0][0][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][0][0][1].C
b-= dp[N-1][0][0][1].C
p = dp[N-1][1][0][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][1][0][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][1][0][1].C
b-= dp[N-1][1][0][1].C
for x in range(2):
for i,v in enumerate(dp[N-1][x][0][1].P.sequence):
a-=modint(1)*v*fact[i]
for j in range(i+1):
b+=modint(1)*v*fact[i]*inv[j]
for i,v in enumerate(dp[N-1][x][0][1].Q.sequence):
b-=modint(1)*v*fact[i]
for j in range(i+1):
c+=modint(1)*v*fact[i]*inv[j]
a-=dp[N-1][x][0][1].C/2
c+=dp[N-1][x][0][1].C/2
print(a,b,c)
```
| 103,488 |
Provide a correct Python 3 solution for this coding contest problem.
There is a very long bench. The bench is divided into M sections, where M is a very large integer.
Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:
* We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).
After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of `X` and `-`: the i-th character of the string is `X` if the i-th section from the left in the interval is occupied, and `-` otherwise. Note that the photo is directed. For example, `-X--X` and `X--X-` are different photos.
What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.
Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):
p + \frac{q}{e} + \frac{r}{e^2}
Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.
Constraints
* 1 \leq N \leq 1000
* |s| = N
* s consists of `X` and `-`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print three rational numbers p, q, r, separated by spaces.
Examples
Input
1
X
Output
500000004 0 500000003
Input
3
---
Output
0 0 0
Input
5
X--X-
Output
0 0 1
Input
5
X-X-X
Output
500000004 0 833333337
Input
20
-X--X--X-X--X--X-X-X
Output
0 0 183703705
Input
100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-
Output
0 0 435664291
"Correct Solution:
```
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return input().rstrip()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10
fc = [1] * 1010
ifc = [1] * 1010
Bn = [0] * 1010
def ad(x, y=0):
return (x + y) % mod
def mlt(x, y=1):
return (x * y) % mod
def pwr(x, y):
return pow(x, y, mod)
def inv(x):
return pwr(x, mod - 2)
def bn(x):
if x > 0 and Bn[x] == 0: Bn[x] = ad(1, mlt(x, bn(x - 1)))
return Bn[x]
#solve
def solve():
for i in range(1, 1010): fc[i] = fc[i - 1] * i
for i in range(1010): ifc[i] = inv(fc[i])
num = 1
dp = [[[0] * 1010 for _ in range(2)] for k in range(1010)]
dpc = [0] * 1010
f = [False] * 1010
n = II()
s = S()
if n == 1 and s == "-":
print("{} 0 {}".format((mod + 1) // 2, (mod + 1) // 2))
return
q = 0
r = n
n = -1
if s[q] == "-":
q += 1
if s[q] == "-":
q += 1
n += 1
num += 1
if s[q] == "-":
print("0 0 0")
return
for i in range(q, r):
if s[i] == "X":
if num == 0:
print("0 0 0")
return
if n >= 0:
f[n] = num - 1
n += 1
num = 0
else:
if num > 1:
print("0 0 0")
return
num += 1
if num == 2:
f[n] = 1
n += 1
dpc[0] = 1
for i in range(n):
if f[i]:
for j in range(2):
for k in range(2 * n + 2):
kari = inv(k * k + 3 * k + 2)
dp[i + 1][j][0] = ad(dp[i + 1][j][0], mlt(dp[i][j][k], kari))
dp[i + 1][j][1] = ad(dp[i + 1][j][1], mlt(dp[i][j][k], mod - kari))
dp[i + 1][1][0] = ad(dp[i + 1][1][0], dpc[i])
dp[i + 1][1][1] = ad(dp[i + 1][1][1], mod - dpc[i])
else:
for j in range(2):
for k in range(2 * n + 2):
kari = inv(k + 1)
dp[i + 1][j][0] = ad(dp[i + 1][j][0], mlt(dp[i][j][k], kari))
dp[i + 1][j][1] = ad(dp[i + 1][j][1], mlt(dp[i][j][k], mod - kari))
dp[i + 1][j][k + 2] = ad(dp[i + 1][j][k + 2], mlt(dp[i][j][k], inv(k * k + 3 * k + 2)))
dp[i + 1][1][1] = ad(dp[i + 1][1][1], dpc[i])
dp[i + 1][1][0] = ad(dp[i + 1][1][0], mod - dpc[i])
dpc[i + 1] = ad(dpc[i + 1], dpc[i])
ans = [0] * 3
for j in range(2):
for k in range(2 * n + 2):
ans[j] = ad(ans[j], mlt(dp[n][j][k], fc[k]))
ans[j + 1] = ad(ans[j + 1], mlt(dp[n][j][k], mod - ad(fc[k], bn(k))))
kari = (mod + 1) // 2
ans[0] = ad(ans[0], mlt(dpc[n], kari))
ans[2] = ad(ans[2], mlt(dpc[n], mod - kari))
print("{} {} {}".format(ans[0], ans[1], ans[2]))
return
#main
if __name__ == '__main__':
solve()
```
| 103,489 |
Provide a correct Python 3 solution for this coding contest problem.
There is a very long bench. The bench is divided into M sections, where M is a very large integer.
Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:
* We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).
After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of `X` and `-`: the i-th character of the string is `X` if the i-th section from the left in the interval is occupied, and `-` otherwise. Note that the photo is directed. For example, `-X--X` and `X--X-` are different photos.
What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.
Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):
p + \frac{q}{e} + \frac{r}{e^2}
Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.
Constraints
* 1 \leq N \leq 1000
* |s| = N
* s consists of `X` and `-`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print three rational numbers p, q, r, separated by spaces.
Examples
Input
1
X
Output
500000004 0 500000003
Input
3
---
Output
0 0 0
Input
5
X--X-
Output
0 0 1
Input
5
X-X-X
Output
500000004 0 833333337
Input
20
-X--X--X-X--X--X-X-X
Output
0 0 183703705
Input
100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-
Output
0 0 435664291
"Correct Solution:
```
from copy import *
N=int(input())
S=input()
DP=[[[[0]*2 for i in range(2)] for j in range(2)] for k in range(N)]
mod=10**9+7
for i in range(N):
for j in range(2):
for k in range(2):
for l in range(2):
DP[i][j][k][l]=[[],[],0]
if S[0]=='X':
DP[0][0][1][0]=[[],[],1]
else:
DP[0][0][0][1]=[[],[],1]
DP[0][1][1][1]=[[1],[],mod-1]
g1=[1,1]
g2=[1,1]
inv=[0,1]
c=[1,2]
for i in range(2,N+5):
g1.append((g1[-1]*i)%mod)
inv.append((-inv[mod%i]*(mod//i))%mod)
g2.append((g2[-1]*inv[-1])%mod)
c.append((c[-1]+g2[i])%mod)
def add(x,y):
for j in range(2):
for i in range(len(y[j])-len(x[j])):
x[j].append(0)
for i in range(len(y[j])):
x[j][i]=(x[j][i]+y[j][i])%mod
x[2]=(x[2]+y[2])%mod
def integrate(p):
p[2]=-p[2]%mod
for j in range(2):
p[j].append(0)
for i in range(len(p[j])-2,-1,-1):
p[j][i+1]=p[j][i]*inv[i+1]%mod
p[j][i]=0
def fx0(p):
integrate(p)
p[0][0]=(p[0][0]-p[2])%mod
def f1x(p):
integrate(p)
for j in range(2):
for i in range(len(p[j])):
p[j][i]=-p[j][i]%mod
p[2]=-p[2]%mod
for i in range(2):
p[i][0]=-sum(p[i])%mod
p[1][0]=(p[1][0]-p[2])%mod
for i in range(N-1):
if S[i+1]=='X':
add(DP[i][1][1][0],DP[i][1][1][1])
fx0(DP[i][1][1][0])
add(DP[i][1][1][1],DP[i][1][0][1])
add(DP[i][1][1][1],DP[i][0][1][1])
add(DP[i][1][1][1],DP[i][0][0][1])
f1x(DP[i][1][1][1])
for j in range(2):
add(DP[i+1][0][1][0],DP[i][1][1][j])
else:
t=[[],[],0]
add(DP[i][0][1][0],DP[i][0][1][1])
add(DP[i][1][1][0],DP[i][1][1][1])
add(DP[i+1][1][1][1],DP[i][0][1][0])
fx0(DP[i+1][1][1][1])
add(t,DP[i][0][1][0])
add(t,DP[i][1][1][0])
f1x(t)
fx0(DP[i][1][1][0])
add(DP[i+1][0][0][1],DP[i][1][1][0])
add(DP[i+1][0][0][1],t)
def f10(p,o,e):
r=[0]*3
q=deepcopy(p)
integrate(q)
r[0]=(sum(q[0])-q[2])*o%mod
r[1]=(sum(q[1])+q[2])*o%mod
for j in range(2):
for i in range(len(p[j])):
r[j]=(r[j]+p[j][i]*g1[i]*e)%mod
r[j+1]=(r[j+1]-p[j][i]*g1[i]*c[i]*e)%mod
r[0]=(r[0]+p[2]*inv[2]*e)%mod
r[2]=(r[2]-p[2]*inv[2]*e)%mod
return r
add(DP[-1][0][1][1],DP[-1][1][1][1])
add(DP[-1][0][1][0],DP[-1][1][1][0])
add(DP[-1][0][0][1],DP[-1][1][0][1])
X=[f10(DP[-1][0][1][1],1,0),f10(DP[-1][0][1][0],0,1),f10(DP[-1][0][0][1],1,mod-1)]
A=[sum(map(lambda x:x[i],X))%mod for i in range(3)]
print(*A)
```
| 103,490 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a very long bench. The bench is divided into M sections, where M is a very large integer.
Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:
* We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).
After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of `X` and `-`: the i-th character of the string is `X` if the i-th section from the left in the interval is occupied, and `-` otherwise. Note that the photo is directed. For example, `-X--X` and `X--X-` are different photos.
What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.
Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):
p + \frac{q}{e} + \frac{r}{e^2}
Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.
Constraints
* 1 \leq N \leq 1000
* |s| = N
* s consists of `X` and `-`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print three rational numbers p, q, r, separated by spaces.
Examples
Input
1
X
Output
500000004 0 500000003
Input
3
---
Output
0 0 0
Input
5
X--X-
Output
0 0 1
Input
5
X-X-X
Output
500000004 0 833333337
Input
20
-X--X--X-X--X--X-X-X
Output
0 0 183703705
Input
100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-
Output
0 0 435664291
Submitted Solution:
```
#参考URL
#https://qiita.com/ageprocpp/items/104c051b2ec2086f5a9b
mod=10**9+7
def value(obj):
if type(obj)==modint:
tmp=obj.number
else:
tmp=obj
return tmp%mod
class modint():
number=0
def __init__(self,num):
self.number=num%mod
def __add__(self,other):
tmp=value(other)
return modint(self.number+tmp)
def __iadd__(self,other):
tmp=value(other)
self.number+=tmp
self.number%=mod
return self
def __sub__(self,other):
tmp=value(other)
return modint(self.number-tmp)
def __isub__(self,other):
tmp=value(other)
self.number-=tmp
self.number%=mod
return self
def __mul__(self,other):
tmp=value(other)
return modint(self.number*tmp)
def __imul__(self,other):
tmp=value(other)
self.number*=tmp
self.number%=mod
return self
def __truediv__(self,other):
tmp=value(other)
return self*pow(tmp,mod-2,mod)
def __itruediv(self,other):
tmp=value(other)
self.number=self/tmp
self.number%=mod
return self
def __repr__(self):
return str(self.number)
def __eq__(self,other):
return value(self)==value(other)
def __ne__(self,other):
return value(self)!=value(other)
def __str__(self):
return str(self.number)
def __int__(self):
return self.number
def __hash__(self):
return self.number
class polynomial():
sequence=[0]
def __init__(self,seq):
self.sequence=[]
for i in seq:
self.sequence.append(value(i))
def get(self,x):
res=modint(0)
tmp=modint(1)
for a in self.sequence:
res+=tmp*a
tmp*=x
if tmp==0:
break
return res
def __add__(self,other):
res=[]
for i,a in enumerate(self.sequence):
try:
res[i]=a
except IndexError:
res.append(modint(0))
res[i]=a
for i,a in enumerate(other.sequence):
try:
res[i]+=a
except IndexError:
res.append(modint(0))
res[i]+=a
return polynomial(res)
def __iadd__(self,other):
return self+other
def __sub__(self,other):
res=[]
for i,a in enumerate(self.sequence):
try:
res[i]=a
except IndexError:
res.append(modint(0))
res[i]=a
for i,a in enumerate(other.sequence):
try:
res[i]-=a
except IndexError:
res.append(modint(0))
res[i]-=a
return polynomial(res)
def __isub__(self,other):
return self-other
def indef_integral(self):
res=[modint(0)]
for i,a in enumerate(self.sequence):
res.append(modint(a)/(i+1))
return polynomial(res)
def integrate(self,flag):
#0: 0->x
#1:x->1
if flag==0:
return self.indef_integral()
else:
tmp=self.indef_integral()
return polynomial([tmp.get(1)])-tmp
def __repr__(self):
return str(self.sequence)
def __str__(self):
return str(self.sequence)
class func():
P=polynomial([modint(0)])
Q=polynomial([modint(0)])
C=modint(0)
def __init__(self,poly1,poly2,cnst):
self.P=poly1
self.Q=poly2
self.C=cnst
def __add__(self,other):
PP=self.P+other.P
QQ=self.Q+other.Q
CC=self.C+other.C
return func(PP,QQ,CC)
def __iadd__(self,other):
return self+other
def __sub__(self,other):
PP=self.P-other.P
QQ=self.Q-other.Q
CC=self.C-other.C
return func(PP,QQ,CC)
def __isub__(self,other):
return self-other
def dintegrate(self,flag):
if flag==0:
PP=self.P.integrate(0)+polynomial([self.C])
QQ=self.Q.integrate(0)
CC=modint(-1)*self.C
else:
PP=self.P.integrate(1)
QQ=self.Q.integrate(1)+polynomial([modint(-1)*self.C])
CC=self.C
return func(PP,QQ,CC)
MAX_N=1009
N=int(input())
s=input()
fact=[modint(0) for i in range(MAX_N+1)]
inv=[modint(0) for i in range(MAX_N+1)]
fact[0]=modint(1)
for i in range(MAX_N):
fact[i+1]=fact[i]*(i+1)
inv[-1]=modint(1)/fact[-1]
for i in range(MAX_N-1,-1,-1):
inv[i]=inv[i+1]*(i+1)
dp=[[[[func(polynomial([modint(0)]),polynomial([modint(0)]),modint(0)) for l in range(2)]for k in range(2)]for j in range(2)] for i in range(MAX_N+1)]
if s[0]=="X":
dp[0][0][1][0]=func(polynomial([modint(0)]),polynomial([modint(0)]),modint(1))
else:
dp[0][0][0][1]=func(polynomial([modint(0)]),polynomial([modint(0)]),modint(1))
dp[0][1][1][1]=func(polynomial([modint(1)]),polynomial([modint(0)]),modint(-1))
for i in range(1,N):
if s[i]=="X":
dp[i][0][1][0]+=dp[i-1][1][1][0].dintegrate(0)
dp[i][0][1][0]+=dp[i-1][1][1][1].dintegrate(0)
dp[i][0][1][0]+=dp[i-1][0][0][1].dintegrate(1)
dp[i][0][1][0]+=dp[i-1][0][1][1].dintegrate(1)
dp[i][0][1][0]+=dp[i-1][1][0][1].dintegrate(1)
dp[i][0][1][0]+=dp[i-1][1][1][1].dintegrate(1)
else:
dp[i][0][0][1]+=dp[i-1][1][1][0].dintegrate(0)
dp[i][0][0][1]+=dp[i-1][1][1][1].dintegrate(0)
dp[i][1][1][1]+=dp[i-1][0][1][0].dintegrate(0)
dp[i][1][1][1]+=dp[i-1][0][1][1].dintegrate(0)
dp[i][0][0][1]+=dp[i-1][0][1][0].dintegrate(1)
dp[i][0][0][1]+=dp[i-1][0][1][1].dintegrate(1)
dp[i][0][0][1]+=dp[i-1][1][1][0].dintegrate(1)
dp[i][0][0][1]+=dp[i-1][1][1][1].dintegrate(1)
a=modint(0)
b=modint(0)
c=modint(0)
p = dp[N-1][0][1][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][0][1][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][0][1][1].C
b-= dp[N-1][0][1][1].C
p = dp[N-1][1][1][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][1][1][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][1][1][1].C
b-= dp[N-1][1][1][1].C
for x in range(2):
for i,v in enumerate(dp[N-1][x][1][0].P.sequence):
a+=modint(1)*v*fact[i]
for j in range(i+1):
b-=modint(1)*v*fact[i]*inv[j]
for i,v in enumerate(dp[N-1][x][1][0].Q.sequence):
b+=modint(1)*v*fact[i]
for j in range(i+1):
c-=modint(1)*v*fact[i]*inv[j]
a+=dp[N-1][x][1][0].C/2
c-=dp[N-1][x][1][0].C/2
p = dp[N-1][0][0][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][0][0][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][0][0][1].C
b-= dp[N-1][0][0][1].C
p = dp[N-1][1][0][1].P.integrate(0)
a+= p.get(1)-p.get(0)
p = dp[N-1][1][0][1].Q.integrate(0)
b+= p.get(1)-p.get(0)
a+= dp[N-1][1][0][1].C
b-= dp[N-1][1][0][1].C
for x in range(2):
for i,v in enumerate(dp[N-1][x][0][1].P.sequence):
a-=modint(1)*v*fact[i]
for j in range(i+1):
b+=modint(1)*v*fact[i]*inv[j]
for i,v in enumerate(dp[N-1][x][0][1].Q.sequence):
b-=modint(1)*v*fact[i]
for j in range(i+1):
c+=modint(1)*v*fact[i]*inv[j]
a-=dp[N-1][x][0][1].C/2
c+=dp[N-1][x][0][1].C/2
print(a,b,c)
```
No
| 103,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a very long bench. The bench is divided into M sections, where M is a very large integer.
Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:
* We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).
After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of `X` and `-`: the i-th character of the string is `X` if the i-th section from the left in the interval is occupied, and `-` otherwise. Note that the photo is directed. For example, `-X--X` and `X--X-` are different photos.
What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.
Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):
p + \frac{q}{e} + \frac{r}{e^2}
Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.
Constraints
* 1 \leq N \leq 1000
* |s| = N
* s consists of `X` and `-`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print three rational numbers p, q, r, separated by spaces.
Examples
Input
1
X
Output
500000004 0 500000003
Input
3
---
Output
0 0 0
Input
5
X--X-
Output
0 0 1
Input
5
X-X-X
Output
500000004 0 833333337
Input
20
-X--X--X-X--X--X-X-X
Output
0 0 183703705
Input
100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-
Output
0 0 435664291
Submitted Solution:
```
print("Hello World")
```
No
| 103,492 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a very long bench. The bench is divided into M sections, where M is a very large integer.
Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:
* We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).
After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of `X` and `-`: the i-th character of the string is `X` if the i-th section from the left in the interval is occupied, and `-` otherwise. Note that the photo is directed. For example, `-X--X` and `X--X-` are different photos.
What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.
Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):
p + \frac{q}{e} + \frac{r}{e^2}
Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.
Constraints
* 1 \leq N \leq 1000
* |s| = N
* s consists of `X` and `-`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print three rational numbers p, q, r, separated by spaces.
Examples
Input
1
X
Output
500000004 0 500000003
Input
3
---
Output
0 0 0
Input
5
X--X-
Output
0 0 1
Input
5
X-X-X
Output
500000004 0 833333337
Input
20
-X--X--X-X--X--X-X-X
Output
0 0 183703705
Input
100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-
Output
0 0 435664291
Submitted Solution:
```
print('I am tourist')
```
No
| 103,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a very long bench. The bench is divided into M sections, where M is a very large integer.
Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:
* We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).
After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of `X` and `-`: the i-th character of the string is `X` if the i-th section from the left in the interval is occupied, and `-` otherwise. Note that the photo is directed. For example, `-X--X` and `X--X-` are different photos.
What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.
Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):
p + \frac{q}{e} + \frac{r}{e^2}
Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.
Constraints
* 1 \leq N \leq 1000
* |s| = N
* s consists of `X` and `-`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print three rational numbers p, q, r, separated by spaces.
Examples
Input
1
X
Output
500000004 0 500000003
Input
3
---
Output
0 0 0
Input
5
X--X-
Output
0 0 1
Input
5
X-X-X
Output
500000004 0 833333337
Input
20
-X--X--X-X--X--X-X-X
Output
0 0 183703705
Input
100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-
Output
0 0 435664291
Submitted Solution:
```
from copy import *
N=int(input())
S=input()
DP=[[[[0]*2 for i in range(2)] for j in range(2)] for k in range(N)]
mod=10**9+7
for i in range(N):
for j in range(2):
for k in range(2):
for l in range(2):
DP[i][j][k][l]=[[],[],0]
if S[0]=='X':
DP[0][0][1][0]=[[],[],1]
else:
DP[0][0][0][1]=[[],[],1]
DP[0][1][1][1]=[[1],[],mod-1]
g1=[1,1]
g2=[1,1]
inv=[0,1]
c=[1,2]
for i in range(2,N+5):
g1.append((g1[-1]*i)%mod)
inv.append((-inv[mod%i]*(mod//i))%mod)
g2.append((g2[-1]*inv[-1])%mod)
c.append((c[-1]+g2[i])%mod)
def add(x,y):
for j in range(2):
for i in range(len(y[j])-len(x[j])):
x[j].append(0)
for i in range(len(y[j])):
x[j][i]=(x[j][i]+y[j][i])%mod
x[2]=(x[2]+y[2])%mod
def integrate(p):
p[2]=-p[2]%mod
for j in range(2):
p[j].append(0)
for i in range(len(p[j])-2,-1,-1):
p[j][i+1]=p[j][i]*inv[i+1]%mod
p[j][i]=0
def fx0(p):
integrate(p)
p[0][0]=(p[0][0]-p[2])%mod
def f1x(p):
integrate(p)
for j in range(2):
for i in range(len(p[j])):
p[j][i]=-p[j][i]%mod
p[2]=-p[2]%mod
for i in range(2):
p[i][0]=-sum(p[i])%mod
p[1][0]=(p[1][0]-p[2])%mod
for i in range(N-1):
if S[i+1]=='X':
add(DP[i][1][1][0],DP[i][1][1][1])
fx0(DP[i][1][1][0])
add(DP[i][1][1][1],DP[i][1][0][1])
add(DP[i][1][1][1],DP[i][0][1][1])
add(DP[i][1][1][1],DP[i][0][0][1])
f1x(DP[i][1][1][1])
for j in range(2):
add(DP[i+1][0][1][0],DP[i][1][1][j])
else:
t=[[],[],0]
add(DP[i][0][1][0],DP[i][0][1][1])
add(DP[i][1][1][0],DP[i][1][1][1])
add(DP[i+1][1][1][1],DP[i][0][1][0])
fx0(DP[i+1][1][1][1])
add(t,DP[i][0][1][0])
add(t,DP[i][1][1][0])
f1x(t)
fx0(DP[i][1][1][0])
add(DP[i+1][0][0][1],DP[i][1][1][0])
add(DP[i+1][0][0][1],t)
def f10(p,o,e):
r=[0]*3
q=deepcopy(p)
integrate(q)
r[0]=(sum(q[0])-q[2])*o%mod
r[1]=(sum(q[1])+q[2])*o%mod
for j in range(2):
for i in range(len(p[j])):
r[j]=(r[j]+p[j][i]*g1[i]*e)%mod
r[j+1]=(r[j+1]-p[j][i]*g1[i]*c[i]*e)%mod
r[0]=(r[0]+p[2]*inv[2])%mod
r[2]=(r[2]-p[2]*inv[2])%mod
return r
add(DP[-1][0][1][1],DP[-1][1][1][1])
add(DP[-1][0][1][0],DP[-1][1][1][0])
add(DP[-1][0][0][1],DP[-1][1][0][1])
X=[f10(DP[-1][0][1][1],1,0),f10(DP[-1][0][1][0],0,1),f10(DP[-1][0][0][1],1,mod-1)]
A=[sum(map(lambda x:x[i],X))%mod for i in range(3)]
print(*A)
```
No
| 103,494 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is practicing shiritori alone again today.
Shiritori is a game as follows:
* In the first turn, a player announces any one word.
* In the subsequent turns, a player announces a word that satisfies the following conditions:
* That word is not announced before.
* The first character of that word is the same as the last character of the last word announced.
In this game, he is practicing to announce as many words as possible in ten seconds.
You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.
Constraints
* N is an integer satisfying 2 \leq N \leq 100.
* W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N
W_1
W_2
:
W_N
Output
If every word announced by Takahashi satisfied the conditions, print `Yes`; otherwise, print `No`.
Examples
Input
4
hoge
english
hoge
enigma
Output
No
Input
9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly
Output
Yes
Input
8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa
Output
No
Input
3
abc
arc
agc
Output
No
"Correct Solution:
```
n=int(input())
li=[input() for _ in range(n)]
if len(set(li))!=n:
print("No");exit()
else:
for i,j in zip(li,li[1:]):
if i[-1]!=j[0]:
print("No");exit()
print("Yes")
```
| 103,495 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is practicing shiritori alone again today.
Shiritori is a game as follows:
* In the first turn, a player announces any one word.
* In the subsequent turns, a player announces a word that satisfies the following conditions:
* That word is not announced before.
* The first character of that word is the same as the last character of the last word announced.
In this game, he is practicing to announce as many words as possible in ten seconds.
You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.
Constraints
* N is an integer satisfying 2 \leq N \leq 100.
* W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N
W_1
W_2
:
W_N
Output
If every word announced by Takahashi satisfied the conditions, print `Yes`; otherwise, print `No`.
Examples
Input
4
hoge
english
hoge
enigma
Output
No
Input
9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly
Output
Yes
Input
8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa
Output
No
Input
3
abc
arc
agc
Output
No
"Correct Solution:
```
print("No" if (lambda w:(sum([w.count(s)-1 for s in w])) or sum([1-(w[i][0] == w[i-1][-1]) for i in range(1,len(w))]))([input() for i in range(int(input()))]) else "Yes")
```
| 103,496 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is practicing shiritori alone again today.
Shiritori is a game as follows:
* In the first turn, a player announces any one word.
* In the subsequent turns, a player announces a word that satisfies the following conditions:
* That word is not announced before.
* The first character of that word is the same as the last character of the last word announced.
In this game, he is practicing to announce as many words as possible in ten seconds.
You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.
Constraints
* N is an integer satisfying 2 \leq N \leq 100.
* W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N
W_1
W_2
:
W_N
Output
If every word announced by Takahashi satisfied the conditions, print `Yes`; otherwise, print `No`.
Examples
Input
4
hoge
english
hoge
enigma
Output
No
Input
9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly
Output
Yes
Input
8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa
Output
No
Input
3
abc
arc
agc
Output
No
"Correct Solution:
```
L=[input()for x in' '*int(input())];print('YNeos'[any(i[-1]!=j[0]*(L.count(i))for i,j in zip(L,L[1:]))::2])
```
| 103,497 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is practicing shiritori alone again today.
Shiritori is a game as follows:
* In the first turn, a player announces any one word.
* In the subsequent turns, a player announces a word that satisfies the following conditions:
* That word is not announced before.
* The first character of that word is the same as the last character of the last word announced.
In this game, he is practicing to announce as many words as possible in ten seconds.
You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.
Constraints
* N is an integer satisfying 2 \leq N \leq 100.
* W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N
W_1
W_2
:
W_N
Output
If every word announced by Takahashi satisfied the conditions, print `Yes`; otherwise, print `No`.
Examples
Input
4
hoge
english
hoge
enigma
Output
No
Input
9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly
Output
Yes
Input
8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa
Output
No
Input
3
abc
arc
agc
Output
No
"Correct Solution:
```
n = int(input())
w = [input() for i in range(n)]
x = False
if n == len(set(w)):
if all(w[i-1][-1] == w[i][0] for i in range(1,n)):
x = True
print('Yes' if x else 'No')
```
| 103,498 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is practicing shiritori alone again today.
Shiritori is a game as follows:
* In the first turn, a player announces any one word.
* In the subsequent turns, a player announces a word that satisfies the following conditions:
* That word is not announced before.
* The first character of that word is the same as the last character of the last word announced.
In this game, he is practicing to announce as many words as possible in ten seconds.
You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.
Constraints
* N is an integer satisfying 2 \leq N \leq 100.
* W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N
W_1
W_2
:
W_N
Output
If every word announced by Takahashi satisfied the conditions, print `Yes`; otherwise, print `No`.
Examples
Input
4
hoge
english
hoge
enigma
Output
No
Input
9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly
Output
Yes
Input
8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa
Output
No
Input
3
abc
arc
agc
Output
No
"Correct Solution:
```
N = int(input())
w = [input()]
ans = 'Yes'
for i in range(N-1):
tmp = input()
if (tmp in w) or (w[-1][-1] != tmp[0]):
ans = 'No'
break
w.append(tmp)
print(ans)
```
| 103,499 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.