output_description stringlengths 15 956 | submission_id stringlengths 10 10 | status stringclasses 3 values | problem_id stringlengths 6 6 | input_description stringlengths 9 2.55k | attempt stringlengths 1 13.7k | problem_description stringlengths 7 5.24k | samples stringlengths 2 2.72k |
|---|---|---|---|---|---|---|---|
Print the minimum distance to be traveled.
* * * | s292682432 | Wrong Answer | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | N = int(input())
A = list(map(int, input().split()))
b = [0] * 9
for a in A:
if a < 400:
b[0] += 1
elif a < 800:
b[1] += 1
elif a < 1200:
b[2] += 1
elif a < 1600:
b[3] += 1
elif a < 2000:
b[4] += 1
elif a < 2400:
b[5] += 1
elif a < 2800:
b[6] += 1
elif a < 3200:
b[7] += 1
else:
b[8] += 1
ans = 8
for B in b[:8]:
if B == 0:
ans -= 1
ans2 = ans + b[8]
print(ans, ans2)
| Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s075210582 | Accepted | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | N = int(input())
L = input().split(" ")
Lint = []
for i in L:
Lint.append(int(i))
Lint.sort()
print(str(Lint[-1] - Lint[0]))
| Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s806731452 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | arr = map(int,input().split(" "))
mx=arr(0)
mn=arr(0)
for a in srr:
if(mx<a):
mx=a
else if(mn>a):
mn=a
print(mx-mn) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s498293988 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | # -*- coding: utf-8 -*-
x = int(input())
# x = 8
y = input()
# y = ["3","1","4","1","5","9","2","6"]
for i in range(x):
y[i] = int(y[i])
Max = max(y)
Min = min(y)
ans = Max - Min
print(ans)
| Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s937685727 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | houseTime = int(input())
place = input().split()
place = int(i) for i in place
place = sorted(place)
print(place[houseTime - 1] - place[0])
| Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s924361167 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | N = int(input())
a = sorted([int(input()) for _ in range(N)])
print(a[-1]-a[0N = int(input())
a = map(int, input().split())
a = sorted(a)
print(a[-1]-a[0]) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s895036655 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | N = int(input())
a = list(map(int, input().split())
print(max(a) - min(a)) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s336255846 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | input(); print((lambda xs: max(xs) - min(xs))[int(x) for x in input().split()]) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s036070655 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | n=int(input())
hoge=list(map(int,input().split()))
print(max(hoge)-min()hoge) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s788366528 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | input();l=list(map(int,input().split()).sort();print(l[-1]-l[0]) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s083760172 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | n=int(input())
a=list(map(int.input().split())
print(max(a)-min(a)) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s143638521 | Wrong Answer | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | print((lambda a: max(a) - min(a))(list(map(int, input().split()))))
| Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s236580093 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | n = int(input())
a = map(int, input()split())
print(max(a)-min(a)) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s001206132 | Accepted | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | tmp = input()
zahyo = [int(n) for n in input().split(" ")]
print(max(zahyo) - min(zahyo))
| Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s610208861 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | N = int(input())
a = list(map(int, input().split()))
print(abs(max(a)-min(a)) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s503513943 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | n=input()
x=input().split()
print(x.[-1]-x.[0]) | Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print the minimum distance to be traveled.
* * * | s544985903 | Runtime Error | p03694 | Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | input() + (lambda a: print(max(a) - min(a)))(list(map(int, input().split())))
| Statement
It is only six months until Christmas, and AtCoDeer the reindeer is now
planning his travel to deliver gifts.
There are N houses along _TopCoDeer street_. The i-th house is located at
coordinate a_i. He has decided to deliver gifts to all these houses.
Find the minimum distance to be traveled when AtCoDeer can start and end his
travel at any positions. | [{"input": "4\n 2 3 7 9", "output": "7\n \n\nThe travel distance of 7 can be achieved by starting at coordinate 9 and\ntraveling straight to coordinate 2. \nIt is not possible to do with a travel distance of less than 7, and thus 7 is\nthe minimum distance to be traveled.\n\n* * *"}, {"input": "8\n 3 1 4 1 5 9 2 6", "output": "8\n \n\nThere may be more than one house at a position."}] |
Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to
Query j.
* * * | s487202880 | Wrong Answer | p02986 | Input is given from Standard Input in the following format:
N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q | import sys
sys.setrecursionlimit(10**7)
n, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(n - 1)]
xyuv = [list(map(int, input().split())) for _ in range(q)]
g = [[] for _ in range(n)]
for a, b, c, d in abcd:
a, b = a - 1, b - 1
g[a].append((b, c, d))
g[b].append((a, c, d))
# LCAを求める。自作する。
# 各頂点の深さ。根は深さ0
depth = [0] * n
tour = []
hatsu = [0] * n
dist = [0] * n
tmp = 0
def et(p, v, tour):
global tmp
for nv, c, d in g[v]:
if nv == p:
continue
hatsu[nv] = len(tour)
tour.append((nv, c, d))
depth[nv] = depth[v] + 1
tmp += d
dist[nv] = tmp
tour = et(v, nv, tour)
tmp -= d
tour.append((nv, c, -d))
return tour
tour.append((0, 0, 0))
tour = et(-1, 0, tour)
class SegmentTree:
def __init__(self, size, f=lambda x, y: x + y, default=0):
self.size = pow(2, (size - 1).bit_length())
self.f = f
self.default = default
self.data = [default] * (self.size * 2)
def update(self, i, x):
i += self.size
self.data[i] = x
while i:
i >>= 1
self.data[i] = self.f(self.data[i * 2], self.data[i * 2 + 1])
# 区間[l,r)へのクエリ
def query(self, l, r):
l, r = l + self.size, r + self.size
lret, rret = self.default, self.default
while l < r:
if l & 1:
lret = self.f(self.data[l], lret)
l += 1
if r & 1:
r -= 1
rret = self.f(self.data[r], rret)
l >>= 1
r >>= 1
return self.f(lret, rret)
def func(x, y):
if x[0] < y[0]:
return x
return y
st = SegmentTree(len(tour), func, (n + 1, 0))
for i, x in enumerate(tour):
v = x[0]
st.update(i, (depth[v], v))
def lca(u, v):
return st.query(hatsu[u], hatsu[v] + 1)[1]
dc = {}
dcary = {}
for i, (v, c, d) in enumerate(tour):
if c in dc:
ci = 1 if d > 0 else -1
dc[c].append((dc[c][-1][0] + ci, dc[c][-1][1] + d))
dcary[c].append(i)
else:
dc[c] = [(0, 0), (1, d)]
dcary[c] = [0, i]
from bisect import bisect_right as bl
for x, y, u, v in xyuv:
u, v = u - 1, v - 1
w = lca(u, v)
uvlen = dist[u] + dist[v] - dist[w] * 2
if x not in dc:
print(uvlen)
continue
widx = bl(dcary[x], hatsu[w]) - 1
wnum, wsum = dc[x][widx]
uidx = bl(dcary[x], hatsu[u]) - 1
vidx = bl(dcary[x], hatsu[v]) - 1
unum, usum = dc[x][uidx]
vnum, vsum = dc[x][vidx]
uvsum = usum + vsum - wsum * 2
uvnum = unum + vnum - wnum * 2
# print(uvlen-uvsum+uvnum*y,uvlen,usum,vsum,wsum,uvnum)
print(uvlen - uvsum + uvnum * y)
exit()
for k in dc:
print("=====", k, "=====")
print(dcary[k])
print(dc[k])
print(hatsu)
"""
5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 3 4
4 1000 3 4
"""
| Statement
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i, and the color and length of that edge are
c_i and d_i, respectively. Here the color of each edge is represented by an
integer between 1 and N-1 (inclusive). The same integer corresponds to the
same color, and different integers correspond to different colors.
Answer the following Q queries:
* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) | [{"input": "5 3\n 1 2 1 10\n 1 3 2 20\n 2 4 4 30\n 5 2 1 40\n 1 100 1 4\n 1 100 1 5\n 3 1000 3 4", "output": "130\n 200\n 60\n \n\nThe graph in this input is as follows:\n\n\n\nHere the edges of Color 1 are shown as solid red lines, the edge of Color 2 is\nshown as a bold green line, and the edge of Color 4 is shown as a blue dashed\nline.\n\n * Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.\n * Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.\n * Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths."}] |
Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to
Query j.
* * * | s478554072 | Wrong Answer | p02986 | Input is given from Standard Input in the following format:
N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q | import sys
import math
from collections import defaultdict
input = sys.stdin.readline
N, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(N - 1)]
g = [[] for _ in range(N)]
for a, b, c, d in abcd:
g[a - 1].append((b - 1, c, d))
g[b - 1].append((a - 1, c, d))
children = [[] for _ in range(N)]
parents = [[] for _ in range(N)] # 先祖(自分を含む)
distance = [math.inf] * N # rootへの距離
distance_by_color = [
defaultdict(lambda: (0, 0)) for _ in range(N)
] # rootへの色ごとの(辺の数, 距離)
# nodeid, parents, dist, dist_by_color
q = [(0, [0], 0, defaultdict(lambda: (0, 0)))]
while len(q) > 0:
v, p, dist, dist_c = q.pop()
parents[v] = p
distance[v] = dist
distance_by_color[v] = dist_c
for u, c, d in g[v]:
p__ = p[-2] if len(p) >= 2 else None
if u != p__:
dist_c_ = dist_c.copy()
dist_c_[c] = (dist_c_[c][0] + 1, dist_c_[c][1] + d)
p_ = p + [u]
q.append((u, p_, dist + d, dist_c_))
children[v].append(u)
def is_ok(mid, parents1, parents2):
try:
return parents1[mid] == parents2[mid]
except IndexError:
return False
def lca(parents1, parents2):
ng = min(len(parents1), len(parents2)) + 1
ok = 0
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if is_ok(mid, parents1, parents2):
ok = mid
else:
ng = mid
return ok
def color_diff(edges_info, y):
return edges_info[0] * y - edges_info[1]
for _ in range(Q):
x, y, u, v = map(int, input().split())
u -= 1
v -= 1
ans = 0
p = lca(parents[u], parents[v])
ans += distance[u]
ans += distance[v]
ans -= 2 * distance[p]
ans += color_diff(distance_by_color[u][x], y)
ans += color_diff(distance_by_color[v][x], y)
ans -= 2 * color_diff(distance_by_color[p][x], y)
print(ans)
| Statement
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i, and the color and length of that edge are
c_i and d_i, respectively. Here the color of each edge is represented by an
integer between 1 and N-1 (inclusive). The same integer corresponds to the
same color, and different integers correspond to different colors.
Answer the following Q queries:
* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) | [{"input": "5 3\n 1 2 1 10\n 1 3 2 20\n 2 4 4 30\n 5 2 1 40\n 1 100 1 4\n 1 100 1 5\n 3 1000 3 4", "output": "130\n 200\n 60\n \n\nThe graph in this input is as follows:\n\n\n\nHere the edges of Color 1 are shown as solid red lines, the edge of Color 2 is\nshown as a bold green line, and the edge of Color 4 is shown as a blue dashed\nline.\n\n * Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.\n * Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.\n * Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths."}] |
Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to
Query j.
* * * | s577487401 | Runtime Error | p02986 | Input is given from Standard Input in the following format:
N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q | INF = 10**9
class RMQ:
def __init__(self, a):
self.n = len(a)
self.size = 2 ** (self.n - 1).bit_length()
self.data = [(INF, INF) for _ in range(2 * self.size - 1)]
self.initialize(a)
# Initialize data
def initialize(self, a):
for i in range(self.n):
self.data[self.size + i - 1] = a[i][:]
for i in range(self.size - 2, -1, -1):
self.data[i] = min(self.data[i * 2 + 1], self.data[i * 2 + 2])[:]
# Update ak as x
def update(self, k, x):
k += self.size - 1
self.data[k] = x
while k > 0:
k = (k - 1) // 2
self.data[k] = min(self.data[2 * k + 1], self.data[2 * k + 2])[:]
# Min value in [l, r)
def query(self, l, r):
L = l + self.size
R = r + self.size
s = (INF, INF)
while L < R:
if R & 1:
R -= 1
s = min(s, self.data[R - 1])[:]
if L & 1:
s = min(s, self.data[L - 1])[:]
L += 1
L >>= 1
R >>= 1
return s
class LCA:
def __init__(self, edge, root):
self.edge = edge[:]
self.path = []
self.depth = []
self.color = []
self.weight = []
self.index = [0] * n
self.euler_tour(-1, root, 0, 0, 0, 0)
dat = list(zip(self.depth, self.path))
self.rmq = RMQ(dat)
# Lowest ancestor of u, v
def get_lca(self, u, v):
l, r = self.index[u], self.index[v]
if l > r:
l, r = r, l
return self.rmq.query(l, r + 1)[1]
def get_depth(self, v):
return self.depth[self.index[v]]
def euler_tour(self, prev, v, d, k, c, w):
self.index[v] = k
self.path.append(v)
self.depth.append(d)
self.color.append(c)
self.weight.append(w)
k += 1
for dest, c, w in self.edge[v]:
if prev == dest:
continue
k = self.euler_tour(v, dest, d + 1, k, c, w)
self.path.append(v)
self.depth.append(d)
self.color.append(-c)
self.weight.append(-w)
k += 1
return k
n, q = [int(item) for item in input().split()]
edge = [[] for _ in range(n)]
for i in range(n - 1):
u, v, c, w = [int(item) for item in input().split()]
u -= 1
v -= 1
edge[u].append([v, c, w])
edge[v].append([u, c, w])
lca = LCA(edge, 0)
memo = [dict() for _ in range(n)]
query = []
for i in range(q):
x, y, u, v = [int(item) for item in input().split()]
u -= 1
v -= 1
ca = lca.get_lca(u, v)
memo[u][x] = None
memo[v][x] = None
memo[ca][x] = None
query.append([x, y, u, v])
colors = [0] * n
c_depth = [0] * n
depth = 0
for i, (p, c, w) in enumerate(zip(lca.path, lca.color, lca.weight)):
if i == 0:
continue
else:
if c < 0:
colors[abs(c)] -= 1
else:
colors[abs(c)] += 1
c_depth[abs(c)] += w
depth += w
for key in memo[p].keys():
memo[p][key] = [colors[key], c_depth[key]]
memo[p][0] = depth
for x, y, u, v in query:
ca = lca.get_lca(u, v)
# Calc basic distance
ans = memo[u][0] + memo[v][0] - memo[ca][0] * 2
# Append color diff
ans += (memo[v][x][0] - memo[ca][x][0]) * y - (memo[v][x][1] - memo[ca][x][1])
ans += (memo[u][x][0] - memo[ca][x][0]) * y - (memo[u][x][1] - memo[ca][x][1])
print(ans)
| Statement
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i, and the color and length of that edge are
c_i and d_i, respectively. Here the color of each edge is represented by an
integer between 1 and N-1 (inclusive). The same integer corresponds to the
same color, and different integers correspond to different colors.
Answer the following Q queries:
* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) | [{"input": "5 3\n 1 2 1 10\n 1 3 2 20\n 2 4 4 30\n 5 2 1 40\n 1 100 1 4\n 1 100 1 5\n 3 1000 3 4", "output": "130\n 200\n 60\n \n\nThe graph in this input is as follows:\n\n\n\nHere the edges of Color 1 are shown as solid red lines, the edge of Color 2 is\nshown as a bold green line, and the edge of Color 4 is shown as a blue dashed\nline.\n\n * Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.\n * Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.\n * Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths."}] |
Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to
Query j.
* * * | s196775346 | Runtime Error | p02986 | Input is given from Standard Input in the following format:
N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q | import math
from collections import defaultdict, Counter
n, q = map(int, input().split())
tree = defaultdict(lambda: defaultdict(list))
for _ in range(n - 1):
u, v, color, dist = map(int, input().split())
tree[u][v] = [color, dist]
tree[v][u] = [color, dist]
distance = [-1] * (n + 1)
distance[1] = 0
colors = [0] * (n + 1)
colors[1] = {}
aux_colors = [0] * (n + 1)
aux_colors[1] = {}
clr_template = defaultdict(int)
def add(color, color_dict, dist):
c = Counter(color_dict)
c[color] = c.get(color, 0) + dist
return c
def aux_add(color, color_dict):
c = Counter(color_dict)
c[color] = c.get(color, 0) + 1
return c
E = []
depth = [0] * (n + 1)
def dfs_eulerTour(src):
global E, colors, distance
E.append(src)
for nxt in tree[src]:
if distance[nxt] == -1:
color, dist = tree[src][nxt]
colors[nxt] = add(color, colors[src], dist)
aux_colors[nxt] = aux_add(color, aux_colors[src])
distance[nxt] = distance[src] + dist
depth[nxt] = depth[src] + 1
dfs_eulerTour(nxt)
E.append(src)
dfs_eulerTour(1)
# print(E)
# print(colors)
# print(depth)
D = [depth[x] for x in E]
N = len(D)
def get_H():
H = [-1] * (n + 1)
vis_set = set()
for i in range(N):
if not E[i] in vis_set:
vis_set.add(E[i])
H[E[i]] = i
return H
H = get_H()
M = [[0] * (int(math.log2(N)) + 1) for i in range(N + 1)]
def preprocess(M, N, D):
for i in range(N):
M[i][0] = i
for j in range(1, int(math.log2(N)) + 1):
i = 0
while i + (1 << j) - 1 < N:
M[i][j] = (
M[i][j - 1]
if D[M[i][j - 1]] <= D[M[i + (1 << (j - 1))][j - 1]]
else M[i + (1 << (j - 1))][j - 1]
)
i += 1
preprocess(M, N, D)
def RMQ(i, j):
k = int(math.log2(j - i))
return M[i][k] if D[M[i][k]] <= D[M[j - (1 << k)][k]] else M[j - (1 << k)][k]
def LCA(u, v):
if u == v:
return u
elif H[u] > H[v]:
u, v = v, u
q = RMQ(H[u], H[v])
return E[q]
else:
q = RMQ(H[u], H[v])
return E[q]
def get_colors(u, v, lca):
cu = Counter(colors[u])
cv = Counter(colors[v])
clca = Counter(colors[lca])
ac_u = Counter(aux_colors[u])
ac_v = Counter(aux_colors[v])
ac_lca = Counter(aux_colors[lca])
return ac_u + ac_v - ac_lca - ac_lca, cu + cv - clca - clca
for _ in range(q):
color, dist, u, v = map(int, input().split())
lca = LCA(u, v)
net_dist = distance[u] + distance[v] - 2 * distance[lca]
count_clrs, clrs = get_colors(u, v, lca)
net_dist += count_clrs.get(color, 0) * dist - clrs.get(color, 0)
print(net_dist)
| Statement
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i, and the color and length of that edge are
c_i and d_i, respectively. Here the color of each edge is represented by an
integer between 1 and N-1 (inclusive). The same integer corresponds to the
same color, and different integers correspond to different colors.
Answer the following Q queries:
* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) | [{"input": "5 3\n 1 2 1 10\n 1 3 2 20\n 2 4 4 30\n 5 2 1 40\n 1 100 1 4\n 1 100 1 5\n 3 1000 3 4", "output": "130\n 200\n 60\n \n\nThe graph in this input is as follows:\n\n\n\nHere the edges of Color 1 are shown as solid red lines, the edge of Color 2 is\nshown as a bold green line, and the edge of Color 4 is shown as a blue dashed\nline.\n\n * Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.\n * Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.\n * Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths."}] |
Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to
Query j.
* * * | s685099646 | Wrong Answer | p02986 | Input is given from Standard Input in the following format:
N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q | # verified: ABC014-D
# calculate LCA O(logN) for each query
# kprev[k][i]: 2^k parent of node i
# if there is no node returns "None"
# Depth[i]: Depth of node i from rootNode
class Doubling:
def __init__(self, graph, Depth, prev, rootNode=0):
self.graph = graph
self.N = len(graph)
self.Log_N = (self.N - 1).bit_length()
self.rootNode = rootNode
# BFSしない
self.prev = prev
self.Depth = Depth
# construct
self.kprev = [self.prev]
S = self.prev
for k in range(self.Log_N):
T = [None] * self.N
for i in range(self.N):
if S[i] is None:
continue
T[i] = S[S[i]]
self.kprev.append(T)
S = T
# LCA
def LCAquery(self, u, v):
dd = self.Depth[v] - self.Depth[u]
if dd < 0:
u, v = v, u
dd = -dd
# set same Depth
for k in range(self.Log_N + 1):
if dd & 1:
v = self.kprev[k][v]
dd >>= 1
if u == v:
return u
for k in reversed(range(self.Log_N)):
pu = self.kprev[k][u]
pv = self.kprev[k][v]
if pu != pv:
u, v = pu, pv
return self.kprev[0][u]
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
from bisect import bisect_right
N, Q = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
a, b, c, d = map(int, input().split())
graph[a - 1].append((b - 1, c, d))
graph[b - 1].append((a - 1, c, d))
Query = [list(map(int, input().split())) for _ in range(Q)]
D = [-1] * N
D[0] = 0
EulerTour = [(0, +1)]
Color = [0] * N
Weight = [0] * N
PointFront = [0] * N
prev = [None] * N
Depth = [0] * N
def dfs(p):
PointFront[p] = len(EulerTour)
EulerTour.append((p, +1))
for np, c, d in graph[p]:
if D[np] == -1:
Color[np] = c
Weight[np] = d
D[np] = D[p] + d
Depth[np] = Depth[p] + 1
prev[np] = p
dfs(np)
EulerTour.append((p, -1))
def dfs2():
stack = [0]
while stack:
p = stack[-1]
for np, c, d in graph[p]:
if D[np] == -1:
PointFront[np] = len(EulerTour)
EulerTour.append((np, +1))
Color[np] = c
Weight[np] = d
D[np] = D[p] + d
Depth[np] = Depth[p] + 1
prev[np] = p
stack.append(np)
if p == stack[-1]:
EulerTour.append((p, -1))
stack.pop()
# dfs(0)
dfs2()
Indexes = [[] for _ in range(N)]
Weights = [[0] for _ in range(N)]
Colors = [[0] for _ in range(N)]
for i, (p, num) in enumerate(EulerTour):
if p == 0:
continue
w = Weight[p] * num
c = Color[p]
Weights[c].append(Weights[c][-1] + w)
Colors[c].append(Colors[c][-1] + num)
Indexes[c].append(i)
# point までにある color 指定された時のスコア
def fixed_score(point, color, replace):
ind = bisect_right(Indexes[color], PointFront[point])
return D[point] - Weights[color][ind] + Colors[color][ind] * replace
def main():
doubling = Doubling(graph, Depth, prev)
ans = []
for x, y, u, v in Query:
u -= 1
v -= 1
lca = doubling.LCAquery(u, v)
ans.append(
fixed_score(u, x, y) + fixed_score(v, x, y) - 2 * fixed_score(lca, x, y)
)
print(*ans, sep="\n")
if __name__ == "__main__":
main()
| Statement
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i, and the color and length of that edge are
c_i and d_i, respectively. Here the color of each edge is represented by an
integer between 1 and N-1 (inclusive). The same integer corresponds to the
same color, and different integers correspond to different colors.
Answer the following Q queries:
* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) | [{"input": "5 3\n 1 2 1 10\n 1 3 2 20\n 2 4 4 30\n 5 2 1 40\n 1 100 1 4\n 1 100 1 5\n 3 1000 3 4", "output": "130\n 200\n 60\n \n\nThe graph in this input is as follows:\n\n\n\nHere the edges of Color 1 are shown as solid red lines, the edge of Color 2 is\nshown as a bold green line, and the edge of Color 4 is shown as a blue dashed\nline.\n\n * Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.\n * Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.\n * Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths."}] |
Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to
Query j.
* * * | s551798851 | Wrong Answer | p02986 | Input is given from Standard Input in the following format:
N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q | import sys
input = sys.stdin.buffer.readline
N, Q = map(int, input().split())
E = []
for _ in range(N - 1):
E.append(tuple(map(int, input().split())))
for _ in range(Q):
x, y, u, v = map(int, input().split())
| Statement
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i, and the color and length of that edge are
c_i and d_i, respectively. Here the color of each edge is represented by an
integer between 1 and N-1 (inclusive). The same integer corresponds to the
same color, and different integers correspond to different colors.
Answer the following Q queries:
* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) | [{"input": "5 3\n 1 2 1 10\n 1 3 2 20\n 2 4 4 30\n 5 2 1 40\n 1 100 1 4\n 1 100 1 5\n 3 1000 3 4", "output": "130\n 200\n 60\n \n\nThe graph in this input is as follows:\n\n\n\nHere the edges of Color 1 are shown as solid red lines, the edge of Color 2 is\nshown as a bold green line, and the edge of Color 4 is shown as a blue dashed\nline.\n\n * Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.\n * Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.\n * Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths."}] |
Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to
Query j.
* * * | s765818948 | Accepted | p02986 | Input is given from Standard Input in the following format:
N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q | class Tree: # weighted
def __init__(self, n, edge):
self.n = n
self.tree = [[] for _ in range(n)]
for e in edge:
self.tree[e[0] - 1].append((e[1] - 1, e[2] - 1, e[3]))
self.tree[e[1] - 1].append((e[0] - 1, e[2] - 1, e[3]))
def setroot(self, root):
self.root = root
self.parent = [None for _ in range(self.n)]
self.parent[root] = -1
self.depth = [None for _ in range(self.n)]
self.depth[root] = 0
self.distance = [None for _ in range(self.n)]
self.distance[root] = 0
self.order = []
self.order.append(root)
self.color = [None for _ in range(self.n)]
self.cost = [0 for _ in range(self.n)]
self.size = [1 for _ in range(self.n)]
stack = [root]
while stack:
node = stack.pop()
for adj, color, cost in self.tree[node]:
if self.parent[adj] is None:
self.parent[adj] = node
self.depth[adj] = self.depth[node] + 1
self.distance[adj] = self.distance[node] + cost
self.color[adj] = color
self.cost[adj] = cost
self.order.append(adj)
stack.append(adj)
for node in self.order[::-1]:
for adj, color, cost in self.tree[node]:
if self.parent[node] == adj:
continue
self.size[node] += self.size[adj]
def heavylight_decomposition(self):
self.order = [None for _ in range(self.n)]
self.head = [None for _ in range(self.n)]
self.head[self.root] = self.root
self.next = [None for _ in range(self.n)]
stack = [self.root]
order = 0
while stack:
node = stack.pop()
self.order[node] = order
order += 1
maxsize = 0
for adj, color, cost in self.tree[node]:
if self.parent[node] == adj:
continue
if maxsize < self.size[adj]:
maxsize = self.size[adj]
self.next[node] = adj
for adj, color, cost in self.tree[node]:
if self.parent[node] == adj or self.next[node] == adj:
continue
self.head[adj] = adj
stack.append(adj)
if self.next[node] is not None:
self.head[self.next[node]] = self.head[node]
stack.append(self.next[node])
def range_hld(self, u, v, edge=False):
res = []
while True:
if self.order[u] > self.order[v]:
u, v = v, u
if self.head[u] != self.head[v]:
res.append((self.order[self.head[v]], self.order[v] + 1))
v = self.parent[self.head[v]]
else:
res.append((self.order[u] + edge, self.order[v] + 1))
return res
def subtree_hld(self, u):
return self.order[u], self.order[u] + self.size[u]
def lca_hld(self, u, v):
while True:
if self.order[u] > self.order[v]:
u, v = v, u
if self.head[u] != self.head[v]:
v = self.parent[self.head[v]]
else:
return u
class SegmentTree:
def __init__(self, arr, func=min, ie=2**63):
self.h = (len(arr) - 1).bit_length()
self.n = 2**self.h
self.ie = ie
self.func = func
self.tree = [ie for _ in range(2 * self.n)]
for i in range(len(arr)):
self.tree[self.n + i] = arr[i]
for i in range(1, self.n)[::-1]:
self.tree[i] = func(self.tree[2 * i], self.tree[2 * i + 1])
def set(self, idx, x):
idx += self.n
self.tree[idx] = x
while idx:
idx >>= 1
self.tree[idx] = self.func(self.tree[2 * idx], self.tree[2 * idx + 1])
def query(self, lt, rt):
lt += self.n
rt += self.n
vl = vr = self.ie
while rt - lt > 0:
if lt & 1:
vl = self.func(vl, self.tree[lt])
lt += 1
if rt & 1:
rt -= 1
vr = self.func(self.tree[rt], vr)
lt >>= 1
rt >>= 1
return self.func(vl, vr)
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(N - 1)]
query = [tuple(map(int, input().split())) for _ in range(Q)]
tree = Tree(N, edge)
tree.setroot(0)
tree.heavylight_decomposition()
arr = [None for _ in range(N)]
for i in range(N):
arr[tree.order[i]] = (tree.cost[i], 0)
def func(x, y):
x0, x1 = x
y0, y1 = y
return x0 + y0, x1 + y1
ie = (0, 0)
st = SegmentTree(arr, func, ie)
query_color = [[] for _ in range(N)]
for j in range(Q):
x, y, u, v = query[j]
query_color[x - 1].append((y, u, v, j))
node_color = [[] for _ in range(N)]
for i in range(1, N):
node_color[tree.color[i]].append(i)
res = [0 for _ in range(Q)]
for i in range(N):
for node in node_color[i]:
st.set(tree.order[node], (0, 1))
for y, u, v, j in query_color[i]:
for lt, rt in tree.range_hld(u - 1, v - 1, True):
d, k = st.query(lt, rt)
res[j] += d + k * y
for node in node_color[i]:
st.set(tree.order[node], (tree.cost[node], 0))
print("\n".join(map(str, res)))
| Statement
There is a tree with N vertices numbered 1 to N. The i-th edge in this tree
connects Vertex a_i and Vertex b_i, and the color and length of that edge are
c_i and d_i, respectively. Here the color of each edge is represented by an
integer between 1 and N-1 (inclusive). The same integer corresponds to the
same color, and different integers correspond to different colors.
Answer the following Q queries:
* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.) | [{"input": "5 3\n 1 2 1 10\n 1 3 2 20\n 2 4 4 30\n 5 2 1 40\n 1 100 1 4\n 1 100 1 5\n 3 1000 3 4", "output": "130\n 200\n 60\n \n\nThe graph in this input is as follows:\n\n\n\nHere the edges of Color 1 are shown as solid red lines, the edge of Color 2 is\nshown as a bold green line, and the edge of Color 4 is shown as a blue dashed\nline.\n\n * Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.\n * Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.\n * Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths."}] |
Print one integer — the sum over ((A_i \cdot A_j) \bmod P).
* * * | s410833770 | Accepted | p02590 | Input is given from Standard Input in the following format.
N
A_1 A_2 \cdots A_N | def convolve(A, B):
# 畳み込み # 要素は整数
# 3 つ以上の場合は一度にやった方がいい
import numpy as np
dtype = np.int64 # np.float128 は windows では動かない?
fft, ifft = np.fft.rfft, np.fft.irfft
a, b = len(A), len(B)
if a == b == 1:
return np.array([A[0] * B[0]])
n = a + b - 1 # 返り値のリストの長さ
k = 1 << (n - 1).bit_length() # n 以上の最小の 2 冪
AB = np.zeros((2, k), dtype=dtype)
AB[0, :a] = A
AB[1, :b] = B
return np.rint(ifft(fft(AB[0]) * fft(AB[1]))).astype(np.int64)[:n]
import sys
import numpy as np
input = sys.stdin.readline
P = 200003
N = int(input())
A = sorted(map(int, input().split()))
A = [a for a in A if a]
Bucket = [0] * P
for a in A:
Bucket[a] += 1
Perm = []
Perm_inv = [0] * (P)
p = 1
for i in range(P):
Perm.append(p)
p = p * 2 % P
for i, p in enumerate(Perm):
Perm_inv[p] = i
#
# print(Perm[:9])
# print(Perm_inv[:9])
def perm(arr, P):
res = [0] * len(arr)
for a, p in zip(arr, P):
res[p] = a
return res
B = perm(Bucket, Perm_inv)
C = convolve(B, B)
C_ = C[:P].copy()
C_[: len(C) - P + 1] += C[P - 1 :]
C_ = perm(C_, Perm)
# for i, c in enumerate(C_):
# if c:
# print(i%P)
Ans = C_ * np.arange(P)
an = Ans.sum()
diag = sum(a * a % P for a in A)
ans = (an - diag) // 2
print(ans)
| Statement
Let’s take a prime P = 200\,003. You are given N integers A_1, A_2, \ldots,
A_N. Find the sum of ((A_i \cdot A_j) \bmod P) over all N \cdot (N-1) / 2
unordered pairs of elements (i < j).
Please note that the sum isn't computed modulo P. | [{"input": "4\n 2019 0 2020 200002", "output": "474287\n \n\nThe non-zero products are:\n\n * 2019 \\cdot 2020 \\bmod P = 78320\n * 2019 \\cdot 200002 \\bmod P = 197984\n * 2020 \\cdot 200002 \\bmod P = 197983\n\nSo the answer is 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287.\n\n* * *"}, {"input": "5\n 1 1 2 2 100000", "output": "600013"}] |
Print one integer — the sum over ((A_i \cdot A_j) \bmod P).
* * * | s044560753 | Runtime Error | p02590 | Input is given from Standard Input in the following format.
N
A_1 A_2 \cdots A_N | import sys
input = sys.stdin.readline
(N,) = map(int, input().split())
S = []
for _ in range(N):
s = input().strip()
S.append(s)
S.sort(key=lambda x: len(x))
D = dict()
dd = set()
R = 0
for s in S:
l = len(s)
d = [0] * 26
for c in dd:
d[c] += 1
mm = 0
tmp = 0
for i in range(l - 1, -1, -1):
cc = ord(s[i]) - 97
tmp = 26 * tmp + cc
R += d[cc]
d[cc] = 0
if tmp in D:
for c in D[tmp]:
d[c] += 1
if i == 0:
f = cc
if i == 1:
nn = tmp
mm += 1
if l != 1:
if nn not in D:
D[nn] = set()
D[nn].add(f)
else:
dd.add(f)
print(R)
| Statement
Let’s take a prime P = 200\,003. You are given N integers A_1, A_2, \ldots,
A_N. Find the sum of ((A_i \cdot A_j) \bmod P) over all N \cdot (N-1) / 2
unordered pairs of elements (i < j).
Please note that the sum isn't computed modulo P. | [{"input": "4\n 2019 0 2020 200002", "output": "474287\n \n\nThe non-zero products are:\n\n * 2019 \\cdot 2020 \\bmod P = 78320\n * 2019 \\cdot 200002 \\bmod P = 197984\n * 2020 \\cdot 200002 \\bmod P = 197983\n\nSo the answer is 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287.\n\n* * *"}, {"input": "5\n 1 1 2 2 100000", "output": "600013"}] |
Print one integer — the sum over ((A_i \cdot A_j) \bmod P).
* * * | s230336401 | Accepted | p02590 | Input is given from Standard Input in the following format.
N
A_1 A_2 \cdots A_N | import numpy as np
class FastFourierTransform:
"""FFT"""
def multiplySameArr(self, A):
"""multiply by FFT(A*A)"""
N = 1
# 1. N > deg(g) + deg(h)となる2の冪乗を選ぶ
while N < 2 * len(A):
N *= 2
# 2. 離散フーリエ変換の計算
nf = np.zeros(N, int)
nf[: len(A)] = A
nf = np.fft.fft(nf)
nf = nf * nf
# 3. 離散フーリエ逆変換
nf = np.fft.ifft(nf)
# コピー
ans = np.zeros(N, int)
for n in range(N):
# 係数を1/N倍 => np.fftの場合、すでに実行済
# ans.append( round(nf[n].real / N) )
if nf[n].real != 0:
ans[n] = int(nf[n].real + 0.5)
return ans
if __name__ == "__main__":
N = int(input())
A = list(map(int, input().split()))
P = 200_003
rootP = 2
# order of 1 to P-1 after fft
order = [1]
x = rootP
while x != 1:
order.append(x)
x *= rootP
x %= P
ORDER_SIZE = len(order) # 200_002
# create order index
where = np.zeros(P, int)
for i in range(ORDER_SIZE):
where[order[i]] = i
# count non zero items
prevCnt = np.zeros(P, int)
resCnt = np.zeros(P, int)
for n in range(N):
if A[n] != 0:
prevCnt[where[A[n]]] += 1
# remove Ax * Ax pattern count
resCnt[(A[n] * A[n]) % P] -= 1
# fft to cnt (calc nC2 and all Ax * Ay)
clsFFT = FastFourierTransform()
fftRes = clsFFT.multiplySameArr(prevCnt)
for r in range(len(fftRes)):
if fftRes[r] != 0:
# get result(Ax*Ay==resCnt.index) Cnt by summary fftRes
resCnt[order[r % ORDER_SIZE]] += fftRes[r]
ans = 0
for p in range(P):
if resCnt[p] != 0:
# remove j <= i pattern count
# and
# calc summary value((Ax * Ay cnt) * (Ax * Ay)))
ans += (resCnt[p] // 2) * p
print(ans)
| Statement
Let’s take a prime P = 200\,003. You are given N integers A_1, A_2, \ldots,
A_N. Find the sum of ((A_i \cdot A_j) \bmod P) over all N \cdot (N-1) / 2
unordered pairs of elements (i < j).
Please note that the sum isn't computed modulo P. | [{"input": "4\n 2019 0 2020 200002", "output": "474287\n \n\nThe non-zero products are:\n\n * 2019 \\cdot 2020 \\bmod P = 78320\n * 2019 \\cdot 200002 \\bmod P = 197984\n * 2020 \\cdot 200002 \\bmod P = 197983\n\nSo the answer is 0 + 78320 + 197984 + 0 + 0 + 197983 = 474287.\n\n* * *"}, {"input": "5\n 1 1 2 2 100000", "output": "600013"}] |
Print N lines. The i-th line should contain B_i.
* * * | s406879911 | Wrong Answer | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | def find(A):
X = [(A[i], i) for i in range(len(A))]
X = sorted(X)
ans = [0] * len(X)
p1 = (len(A) - 1) // 2
p2 = len(A) // 2
for i in range(len(X)):
a, b = X[i]
if i < p2:
ans[b] = A[p2]
else:
ans[b] = A[p1]
return ans
A = [str(x) for x in find(list(map(int, input().strip().split(" "))))]
print("\n".join(A))
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s145188149 | Runtime Error | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> pii;
typedef vector<pi> vpi;
typedef set<ll> si;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define f first
#define s second
ll INF = 1e18;
ll MOD = 1e9+7;
#define lb lower_bound
#define ub upper_bound
#define SZ(x) (ll)x.size()
#define ALL(x) x.begin(),x.end()
int l,r,A[100100], B[100100], N;
int main(){
cin>>N;
for (int i=0;i<N;++i){cin>>A[i];B[i] = A[i];}
sort(A,A+N);
l = A[N/2-1];
r = A[N/2];
for (int i=0;i<N;++i){
if (B[i] <= l)cout<<r<<'\n';
else cout<<l<<'\n';
}
} | Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s167263573 | Runtime Error | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | from sys import exit
r, s = map(int, input().split())
a = [input() for i in range(r)]
def check(x):
cnt = [0 for i in range(300)]
for i in x:
cnt[ord(i)] += 1
odd = sum(i % 2 for i in cnt)
if odd != len(x) % 2:
print("NO")
exit(0)
midr, mids = None, None
dr = dict()
for i in range(r):
b = tuple(sorted(a[i]))
if b not in dr:
dr[b] = 1
else:
dr[b] += 1
odd = 0
for k in dr:
if dr[k] % 2 == 1:
odd += 1
midr = k
if odd != r % 2:
print("NO")
exit(0)
if odd == 1:
check(midr)
ds = dict()
for j in range(s):
b = tuple(sorted([a[i][j] for i in range(r)]))
if b not in ds:
ds[b] = 1
else:
ds[b] += 1
odd = 0
for k in ds:
if ds[k] % 2 == 1:
odd += 1
mids = k
if odd != s % 2:
print("NO")
exit(0)
if odd == 1:
check(mids)
if mids:
for k in dr:
indeksi = [i for i in range(r) if tuple(sorted(a[i])) == k]
check([mids[i] for i in indeksi])
if midr:
for k in ds:
indeksi = [
j for j in range(s) if tuple(sorted([a[i][j] for i in range(r)])) == k
]
check([midr[j] for j in indeksi])
print("YES")
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s376107543 | Runtime Error | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | n, m = map(int, input().split())
cou = 0
if n != 0 and m != 0:
if n / m < 0:
cou += 1
n *= -1
elif n < 0 and m < 0:
cou += 2
n *= -1
m *= -1
else:
continue
print(cou + abs(n - m))
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s543314880 | Runtime Error | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | CODE
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s287790341 | Runtime Error | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | A
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s995554058 | Runtime Error | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | print(r.text[:300] + "...")
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s859923372 | Runtime Error | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | n = int(input())
x = list(map(int,input().split())
for i in range(n/2):
print(x[n/2])
for i in range(n/2):
print(x[(n-1)//2])
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print N lines. The i-th line should contain B_i.
* * * | s345169284 | Accepted | p03381 | Input is given from Standard Input in the following format:
N
X_1 X_2 ... X_N | # -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
class Scanner:
@staticmethod
def int():
return int(sys.stdin.readline().rstrip())
@staticmethod
def string():
return sys.stdin.readline().rstrip()
@staticmethod
def map_int():
return [int(x) for x in Scanner.string().split()]
@staticmethod
def string_list(n):
return [input() for i in range(n)]
@staticmethod
def int_list_list(n):
return [Scanner.map_int() for i in range(n)]
@staticmethod
def int_cols_list(n):
return [int(input()) for i in range(n)]
class Math:
@staticmethod
def gcd(a, b):
if b == 0:
return a
return Math.gcd(b, a % b)
@staticmethod
def lcm(a, b):
return (a * b) // Math.gcd(a, b)
@staticmethod
def roundUp(a, b):
return -(-a // b)
@staticmethod
def toUpperMultiple(a, x):
return Math.roundUp(a, x) * x
@staticmethod
def toLowerMultiple(a, x):
return (a // x) * x
@staticmethod
def nearPow2(n):
if n <= 0:
return 0
if n & (n - 1) == 0:
return n
ret = 1
while n > 0:
ret <<= 1
n >>= 1
return ret
@staticmethod
def sign(n):
if n == 0:
return 0
if n < 0:
return -1
return 1
@staticmethod
def isPrime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
d = int(n**0.5) + 1
for i in range(3, d + 1, 2):
if n % i == 0:
return False
return True
class PriorityQueue:
def __init__(self, l=[]):
self.__q = l
heapq.heapify(self.__q)
return
def push(self, n):
heapq.heappush(self.__q, n)
return
def pop(self):
return heapq.heappop(self.__q)
MOD = int(1e09) + 7
INF = int(1e15)
def calc(N):
return sum(int(x) for x in str(N))
def main():
# sys.stdin = open("sample.txt")
N = Scanner.int()
A = Scanner.map_int()
B = sorted(A)
m1 = B[N // 2 - 1]
m2 = B[N // 2]
for i in range(N):
if A[i] < m2:
print(m2)
else:
print(m1)
return
if __name__ == "__main__":
main()
| Statement
When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the
(\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.
You are given N numbers X_1, X_2, ..., X_N, where N is an even number. For
each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i,
that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.
Find B_i for each i = 1, 2, ..., N. | [{"input": "4\n 2 4 4 3", "output": "4\n 3\n 3\n 4\n \n\n * Since the median of X_2, X_3, X_4 is 4, B_1 = 4.\n * Since the median of X_1, X_3, X_4 is 3, B_2 = 3.\n * Since the median of X_1, X_2, X_4 is 3, B_3 = 3.\n * Since the median of X_1, X_2, X_3 is 4, B_4 = 4.\n\n* * *"}, {"input": "2\n 1 2", "output": "2\n 1\n \n\n* * *"}, {"input": "6\n 5 5 4 4 3 3", "output": "4\n 4\n 4\n 4\n 4\n 4"}] |
Print either `yes` or `no` in a line for each dataset, in the order of the
input. If all words in the dataset can be considered to be ordered
lexicographically, print `yes`. Otherwise, print `no`. | s085781689 | Runtime Error | p01646 | The input consists of multiple datasets. Each dataset is formatted as follows:
n
string_1
...
string_n
Each dataset consists of n+1 lines. The first line of each dataset contains an
integer that indicates n (1 \leq n \leq 500). The i-th line of the following n
lines contains string_i, which consists of up to 10 English lowercase letters.
The end of the input is `0`, and this should not be processed. | def add_edge(node, adj_lst, adj_rev, s1, s2):
ind = 0
max_len = min(len(s1), len(s2))
while ind < max_len and s1[ind] == s2[ind]:
ind += 1
if ind == max_len:
if max_len < len(s1):
return True
return False
c1 = ord(s1[ind]) - ord("a")
c2 = ord(s2[ind]) - ord("a")
adj_lst[c1].add(c2)
adj_rev[c2].add(c1)
node.add(c1)
node.add(c2)
return False
def main():
while True:
n = int(input())
if n == 0:
break
lst = [input() for _ in range(n)]
node = set()
adj_lst = [set() for _ in range(26)]
adj_rev = [set() for _ in range(26)]
blank_flag = False
for i in range(n):
for j in range(i + 1, n):
blank_flag = blank_flag or add_edge(
node, adj_lst, adj_rev, lst[i], lst[j]
)
L = []
visited = [False] * 26
cycle_flag = False
def visit(n):
global cycle_flag
if cycle_flag:
return
if visited[n] == 2:
cycle_flag = True
elif visited[n] == 0:
visited[n] = 2
for to in adj_lst[n]:
visit(to)
visited[n] = 1
L.append(n)
L = []
for n in node:
visit(n)
if cycle_flag or blank_flag:
print("no")
else:
print("yes")
main()
| Statement
We found a dictionary of the Ancient Civilization Mayo (ACM) during excavation
of the ruins. After analysis of the dictionary, we revealed they used a
language that had not more than 26 letters. So one of us mapped each letter to
a different English alphabet and typed all the words in the dictionary into a
computer.
How the words are ordered in the dictionary, especially whether they are
ordered lexicographically, is an interesting topic to many people. As a good
programmer, you are requested to write a program to judge whether we can
consider the words to be sorted in a lexicographical order.
Note: In a lexicographical order, a word always precedes other words it is a
prefix of. For example, `ab` precedes `abc`, `abde`, and so on. | [{"input": "cba\n cab\n b\n a\n 3\n bca\n ab\n a\n 5\n abc\n acb\n b\n c\n c\n 5\n abc\n acb\n c\n b\n b\n 0", "output": "yes\n no\n yes\n no"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s263045094 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | print(int(int(input()) ** 2 * 3))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s470908944 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | print(3 * input() * input())
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s193545053 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | print((input() ** 2) * 3)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s919672983 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | print(3 * int(input()) ^ 2)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s938318223 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | import glob
# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = "ABC\\134\\A"
# テスト用ファイル置き場のトップ
TOP_PATH = "C:\\AtCoder"
class Common:
problem = []
index = 0
def __init__(self, rel_path):
self.rel_path = rel_path
def initialize(self, path):
file = open(path)
self.problem = file.readlines()
self.index = 0
return
def input_data(self):
try:
IS_TEST
self.index += 1
return self.problem[self.index - 1]
except NameError:
return input()
def resolve(self):
pass
def exec_resolve(self):
try:
IS_TEST
for path in glob.glob(TOP_PATH + "\\" + self.rel_path + "/*.txt"):
print("Test: " + path)
self.initialize(path)
self.resolve()
print("\n\n")
except NameError:
self.resolve()
class A(Common):
def resolve(self):
r = int(self.input_data())
print(str(3 * r * r))
solver = A(REL_PATH)
solver.exec_resolve()
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s212845394 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | N = int(input())
As = list(map(int, input().split()))
As.insert(0, 0)
Ans = [0 for _ in range(N + 1)]
Ans[N] = As[N]
for i in range(N - 1, 0, -1):
p = i * 2
s = 0
while p <= N:
s += Ans[p]
p += i
Ans[i] = 0 if s % 2 == As[i] else 1
M = sum(Ans)
print(M)
if M > 0:
bs = ""
for i in range(1, N + 1):
if Ans[i] != 0:
bs += "%d " % i
bs = bs.rstrip(" ")
print(bs)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s366527627 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | S = int(input())
for i in range(10**6):
if int((S + i**2) ** (1 / 2)) == (S + i**2) ** (1 / 2):
y = i
x = int((S + i**2) ** (1 / 2))
print("{} {} {} {} {} {}".format(0, 0, x, y, y, x))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s534525386 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | N = int(input())
ans = 0
lst = list(map(int, input().split()))
slice_lst = lst[: int(N / 2)]
for i, k in enumerate(reversed(slice_lst)):
sub_lst = lst[len(slice_lst) - i - 1 : len(lst) : len(slice_lst) - i]
if k != sum(sub_lst) % 2:
if k == 0:
lst[len(slice_lst) - i - 1] = 1
else:
ans = -1
break
if ans == -1:
print(ans)
else:
ans_lst = []
for i, k in enumerate(lst):
if k == 1:
ans_lst.append(str(i + 1))
print(len(ans_lst))
result = " ".join(ans_lst)
print(result)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s015111682 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | print(((int(input())) ** 2) * 3)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s637469511 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | R = int(input())
print(3 * R * R)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s955975817 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | print(int(3 * input()) ** 2)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s125775404 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | print(3 * int(input) ** 2)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s216807406 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | print(3 * (input() ** 2))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s758241913 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | print(int(input) ** 2 * 3)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s227985378 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | print(3 * (int(input() ** 2)))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s953483202 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | print(int(input()) ** 3)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s945438176 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | print(3 ** int(input()))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s444817388 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | def a(r):
return 3 * r**2
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s505162693 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | int = int(input())
print(3 * int * int)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s488555989 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | num = input()
print(3 * num * num)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s901420960 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | usrInput = int(input())
print(usrInput * usrInput * 3)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s427839606 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | dodecagon = int(input())
print(3 * dodecagon * dodecagon)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s763080047 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | n = int(input())
A = list(map(int, input().split()))
box = [0] * n
for i in range(n - 1, -1, -1):
if n // (i + 1) <= 1 and A[i] == 1:
box[i] = 1
elif n // (i + 1) <= 1 and A[i] == 0:
pass
elif n // (i + 1) > 1:
if A[i] == sum(box[2 * (i + 1) - 1 :: (i + 1)]) % 2:
pass
elif A[i] != sum(box[2 * (i + 1) - 1 :: (i + 1)]) % 2:
box[i] = 1
ans = ""
for i in range(n):
if box[i] == 1:
ans += str(A[i]) + " "
if sum(box) % 2 == A[0]:
print(sum(box))
print(ans)
else:
print("-1")
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s409386566 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | a = [int(input()) for i in [0] * int(input())]
b = max(a)
if a.count(b) > 1:
print("\n".join([b for i in a]))
else:
c = [b for i in a]
c[a.index(b)] = max(a[: a.index(b)] + a[a.index(b) + 1 :])
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s054931937 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | n = int(input())
a = [0] + list(map(int, input().split()))
for i in range(n // 2, 0, -1):
a[i] = sum(a[i::i]) % 2
print(sum(a))
print(*(i for i, b in enumerate(a) if b))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s776713160 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | a, b = map(int, input().split(" "))
print(-(-a // (1 + 2 * b)))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s391924400 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | print(3 * pow(int(input(), 2)))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s672944674 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | print(str(int(input()) ** 2 * 3))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s667895923 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | print(3 * pow(int(input()), 2))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s748502611 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | print(3 * pow(2, int(input())))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s606142523 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | print(pow(2, int(input())) * 3)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s323287286 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | A = input()
print(3 * A**2)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s969336865 | Accepted | p02969 | Input is given from Standard Input in the following format:
r | A = int(input())
print(3 * (A**2))
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s457666854 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | def dodecagon(r):
return 3 * r ^ 2
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s427881450 | Wrong Answer | p02969 | Input is given from Standard Input in the following format:
r | def resolve():
s = int(input())
print(s * s * 3)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
Print an integer representing the area of the regular dodecagon.
* * * | s862285541 | Runtime Error | p02969 | Input is given from Standard Input in the following format:
r | def add(n):
return n + 1
if __name__ == "__main__":
n = int(input())
buf = input().split()
a = [int(s) for s in buf]
boxes = [0] * n
for i in reversed(range(n)):
pick_boxes = boxes[i :: i + 1]
pick_boxes.pop(0)
if pick_boxes:
if (sum(pick_boxes) % 2) == a[i]:
boxes[i] = 0
else:
boxes[i] = 1
else:
boxes[i] = a[i]
box_name = [i for i, x in enumerate(boxes) if x == 1]
box_name = list(map(add, box_name))
print(len(box_name))
if len(box_name) != 0:
box_name = " ".join(map(str, box_name))
print(box_name)
| Statement
It is known that the area of a regular dodecagon inscribed in a circle of
radius a is 3a^2.
Given an integer r, find the area of a regular dodecagon inscribed in a circle
of radius r. | [{"input": "4", "output": "48\n \n\nThe area of the regular dodecagon is 3 \\times 4^2 = 48.\n\n* * *"}, {"input": "15", "output": "675\n \n\n* * *"}, {"input": "80", "output": "19200"}] |
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest
such b. If such b does not exist, print `-1` instead.
* * * | s410403577 | Runtime Error | p04014 | The input is given from Standard Input in the following format:
n
s | import sys
import math
n=int(sys.stdin.readline().strip())
s=int(sys.stdin.readline().strip())
def divlist(n):
sl=[]
ll=[]
for x in range(1,int(math.sqrt(n))+1):
if n%x==0:
sl.append(x)
ll.append(n//x)
ll.reverse()
sl.extend(ll)
return sl
def f(b,n):
if n<b:
return n
else:
return f(b,n//b)+(n%b)
if n<s:
print(-1
elif n==s:
print(n+1)
else:
dl=divlist(n-s)
for x in dl:
if s==f(x+1,n):
print(x+1)
sys.exit()
print(-1)
| Statement
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined
as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and
n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base
b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq
2) such that f(b,n)=s. If the answer is positive, also find the smallest such
b. | [{"input": "87654\n 30", "output": "10\n \n\n* * *"}, {"input": "87654\n 138", "output": "100\n \n\n* * *"}, {"input": "87654\n 45678", "output": "-1\n \n\n* * *"}, {"input": "31415926535\n 1", "output": "31415926535\n \n\n* * *"}, {"input": "1\n 31415926535", "output": "-1"}] |
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest
such b. If such b does not exist, print `-1` instead.
* * * | s585561753 | Runtime Error | p04014 | The input is given from Standard Input in the following format:
n
s | N, A = map(int, input().split())
X = [int(e) for e in input().split()]
# dp[i][k][s]: i番目まで見てk個選んで総和sとなる場合の数
# 初期条件: dp[0][0][0] = 1
# 遷移: dp[i][k][s] += dp[i-1][k-1][s-X[i]] + dp[i-1][k][s]
# 答え: sum(dp[N][k][k*A] 1 <= k <= N)
s_max = 2500
dp = [[[0] * (s_max + 1) for k in range(N + 1)] for i in range(N + 1)]
dp[0][0][0] = 1
for i in range(1, N + 1):
for k in range(N + 1):
for s in range(s_max + 1):
dp[i][k][s] += dp[i - 1][k][s]
if k != 0 and 0 <= s - X[i - 1] <= s_max:
dp[i][k][s] += dp[i - 1][k - 1][s - X[i - 1]]
ans = 0
for k in range(1, N + 1):
s = k * A
ans += dp[N][k][s]
print(ans)
| Statement
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined
as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and
n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base
b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq
2) such that f(b,n)=s. If the answer is positive, also find the smallest such
b. | [{"input": "87654\n 30", "output": "10\n \n\n* * *"}, {"input": "87654\n 138", "output": "100\n \n\n* * *"}, {"input": "87654\n 45678", "output": "-1\n \n\n* * *"}, {"input": "31415926535\n 1", "output": "31415926535\n \n\n* * *"}, {"input": "1\n 31415926535", "output": "-1"}] |
If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest
such b. If such b does not exist, print `-1` instead.
* * * | s727667078 | Wrong Answer | p04014 | The input is given from Standard Input in the following format:
n
s | print(-1)
| Statement
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined
as follows:
* f(b,n) = n, when n < b
* f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b
Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and
n \ {\rm mod} \ b denotes the remainder of n divided by b.
Less formally, f(b,n) is equal to the sum of the digits of n written in base
b. For example, the following hold:
* f(10,\,87654)=8+7+6+5+4=30
* f(100,\,87654)=8+76+54=138
You are given integers n and s. Determine if there exists an integer b (b \geq
2) such that f(b,n)=s. If the answer is positive, also find the smallest such
b. | [{"input": "87654\n 30", "output": "10\n \n\n* * *"}, {"input": "87654\n 138", "output": "100\n \n\n* * *"}, {"input": "87654\n 45678", "output": "-1\n \n\n* * *"}, {"input": "31415926535\n 1", "output": "31415926535\n \n\n* * *"}, {"input": "1\n 31415926535", "output": "-1"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s789611663 | Wrong Answer | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | """
自宅用PCでの解答
"""
import math
# import numpy as np
import itertools
import queue
import bisect
from collections import deque, defaultdict
import heapq as hpq
from sys import stdin, setrecursionlimit
# from scipy.sparse.csgraph import dijkstra
# from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9 + 7 # 998244353
dir = [(-1, 0), (0, -1), (1, 0), (0, 1)]
alp = "abcdefghijklmnopqrstuvwxyz"
def main():
n = int(ipt())
ans = [0] * n
a = [int(i) for i in ipt().split()]
cts = [(a[0], 0)]
d = defaultdict(int)
d[a[0]] = 1
tot = a[0]
for i, ai in enumerate(a[1::]):
tot += ai
d[ai] += 1
if cts[-1][0] < ai:
cts.append((ai, i + 1))
# print(d,cts)
nms = sorted(d.keys(), reverse=True)
lc = len(cts) - 1
na = cts[lc][0]
ps = cts[lc][1]
sm = 0
for i in nms:
sm += d[i]
ans[ps] += d[i] * (i - na)
if i == na:
lc -= 1
na = cts[lc][0]
ans[ps] += sm * (i - na)
ps = cts[lc][1]
if lc == 0:
break
ans[0] = tot - sum(ans[1::])
for i in ans:
print(i)
return None
if __name__ == "__main__":
main()
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s463731720 | Runtime Error | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | #include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long lint;
typedef vector<int>vi;
typedef pair<int,int>pii;
typedef pair<lint,int>pli;
#define rep(i,n)for(int i=0;i<(int)(n);++i)
int main(){
int n;
cin>>n;
vector<lint>a(n);
rep(i,n)cin>>a[i];
vector<pli>b(n);
rep(i,n)b[i]=pli(a[i],i);
sort(b.rbegin(),b.rend());
vector<lint>ans(n);
int mi=n;
rep(i,n){
if(i==n-1||b[i].first!=b[i+1].first){
mi=min(b[i].second,mi);
lint diff=b[i].first-(i==n-1?0:b[i+1].first);
ans[mi]+=diff*(i+1);
}
}
rep(i,n)cout<<ans[i]<<endl;
}
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s901657233 | Accepted | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | def examC():
N, M = LI()
if N - M // 2 >= 0:
ans = M // 2
else:
ans = N + (M - N * 2) // 4
print(ans)
return
def examD():
N = I()
S = SI()
for i1, i2 in [[0, 0], [0, 1], [1, 0], [1, 1]]:
flag = True
sheep = [1] * N
sheep[0] = i1
sheep[1] = i2
for j in range(1, N - 1):
if S[j] == "o":
cur = 1
else:
cur = 0
sheep[j + 1] = (sheep[j] ^ cur) ^ sheep[j - 1]
if S[-1] == "o":
cur = 1
else:
cur = 0
if sheep[0] != (sheep[-1] ^ cur) ^ sheep[-2]:
flag = False
if S[0] == "o":
cur = 1
else:
cur = 0
if sheep[1] != (sheep[0] ^ cur) ^ sheep[-1]:
flag = False
if flag:
ans = ""
for i in sheep:
if i == 1:
ans += "S"
else:
ans += "W"
print(ans)
return
print(-1)
return
def examE():
N = I()
A = LI()
Ai = [[0, 0]]
for i in range(N):
Ai.append([A[i], i])
Ai = sorted(Ai, key=lambda x: x[1], reverse=True)
Ai = sorted(Ai, key=lambda x: x[0], reverse=True)
# print(Ai)
ans = [0] * N
now = N
for i in range(N):
now = min(now, Ai[i][1])
ans[now] += (i + 1) * (Ai[i][0] - Ai[i + 1][0])
for v in ans:
print(v)
return
def examF():
ans = 0
print(ans)
return
import sys, copy, bisect, itertools, heapq, math
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque
def I():
return int(sys.stdin.readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LSI():
return list(map(str, sys.stdin.readline().split()))
def LS():
return sys.stdin.readline().split()
def SI():
return sys.stdin.readline().strip()
global mod, mod2, inf, alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord("a") + i) for i in range(26)]
if __name__ == "__main__":
examE()
"""
"""
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s981493828 | Runtime Error | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | N = int(input())
A = list(map(int, input().split()))
num = A[0]
data = [[A[0], 0]]
for i in range(1, N):
if A[i] > num:
data.append([A[i], i])
num = A[i]
B = sorted(A, reverse=True)
ans = [0] * N
kkk = 0
for i in range(len(data) - 1, 0, -1):
zzz = data[i][0]
while kkk < N and B[kkk] >= zzz:
kkk += 1
ans[data[i][1]] = (zzz - data[i - 1][0]) * kkk
cnt += 1
ans[0] = sum(A) - sum(ans)
for u in ans:
print(u)
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s519118842 | Runtime Error | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | n = int(input())
lis = list(map(int, input().split()))
key = lis[0]
num = [0]
li = [key]
for i in range(1, n):
if lis[i] > key:
line = lis[i]
num.append(i)
li.append(line)
m = len(li)
nu = [li[0]] + [li[i + 1] - li[i] for i in range(m - 1)]
he = [0] * m
cou = [0] * m
from bisect import bisect_left
for k in lis:
sen = bisect_left(li, k)
cou[sen] += k - li[sen]
he[sen] += 1
t = 0
for i in reversed(range(m)):
t += he[i]
he[i] = t
for i in range(m):
cou[i] += he[i] * nu[i]
ans = [0] * n
for i, c in zip(num, cou):
ans[i] = c
for r in ans:
print(r)
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s920177765 | Wrong Answer | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | # coding: utf-8
import sys
from bisect import bisect_left, bisect_right, insort
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
"""
左側で自分より小さい数字との差
右側で自分より大きい数字の数
"""
N = ir()
A = lr()
left = [0] * N
right = [0] * N
X = 0
for i, a in enumerate(A):
if a > X:
left[i] = a - X
X = a
target = []
for i, a in enumerate(A[::-1]):
index = bisect_left(target, a)
insort(target, a)
right[N - 1 - i] = i + 1 - index
answer = [l * r for l, r in zip(left, right)]
print("\n".join(map(str, answer)))
# 22
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s561172878 | Accepted | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor
import numpy as np
if os.getenv("LOCAL"):
sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
class BinaryIndexedTree:
# http://hos.ac/slides/20140319_bit.pdf
def __init__(self, size):
"""
:param int size:
"""
self._bit = [0] * size
self._size = size
def add(self, i, w):
"""
i 番目に w を加える
:param int i:
:param int w:
:return:
"""
x = i + 1
while x <= self._size:
self._bit[x - 1] += w
x += x & -x
def sum(self, i):
"""
0 番目から i 番目までの合計
:param int i:
:return:
"""
ret = 0
x = i + 1
while x > 0:
ret += self._bit[x - 1]
x -= x & -x
return ret
def __len__(self):
return self._size
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
A = np.array(A, dtype=int)
ans = np.zeros(N, dtype=int)
# for i in range(N):
# if A[i] <= 0:
# continue
# ans[i] = np.minimum(A, A[i]).sum()
# A = np.maximum(0, A - A[i])
# print(ans)
counts = Counter(A)
counts[0] = 0
counts[IINF] = 0
keys = list(sorted(counts.keys()))
ki = 0
cnt = 0
s = 0
for i in range(N):
if A[i] - s <= 0:
continue
m = A[i]
# s 以上 m 以下のやつの合計
p = s
while keys[ki] <= m:
ans[i] += (keys[ki] - p) * (N - cnt)
p = keys[ki]
cnt += counts[keys[ki]]
ki += 1
s = A[i]
print(*ans, sep="\n")
# [10 7 0 4 0 3 0 2 3 0]
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s366735708 | Wrong Answer | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | n = int(input())
a = list(map(int, input().split()))
s = sorted([[a[i], i] for i in range(n)], key=lambda x: (-x[0], x[1]))
if n == 1:
print(a[0])
else:
c = 1
t = [0] * n
while 1:
if len(s) == 1:
t[s[0][1]] = c * (s[0][0])
break
elif s[0][1] > s[1][1] and s[0][0] != s[1][0]:
t[s[0][1]] = c * (s[0][0] - s[1][0])
c += 1
s.pop(0)
elif s[0][0] == s[1][0]:
p = s[0][1]
while len(s) != 1:
if s[0][0] == s[1][0]:
c += 1
s.pop(0)
else:
t[p] = c * (s[0][0] - s[1][0])
c += 1
s.pop(0)
break
else:
t[p] += sum(a) - sum(t)
break
else:
t[s[0][1]] += sum(a) - sum(t)
break
for i in t:
print(i)
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s756962731 | Wrong Answer | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | def solve():
N = int(input())
A = list(map(int, input().split()))
ans = [0] * N
lis = [0] * N
for i, a in enumerate(A):
lis[i] = [a, i]
lis.sort(key=lambda x: (-x[0], -x[1]))
lis.append([0, 0])
for i in range(N):
if lis[i][0] == lis[i + 1][0]:
continue
else:
ans[lis[i][1]] = i + lis[i][0] - lis[i + 1][0]
return ans
print(*solve(), sep="\n")
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print N lines. The i-th line should contain the number of the occurrences of
the integer i in the lexicographically smallest sequence that can be
constructed.
* * * | s463930560 | Accepted | p03801 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_{N} | class BIT:
def __init__(self, n):
self.N = n + 1
self.bit = [0] * self.N
def bit_sum(self, i):
s = 0
i += 1
while i > 0:
s += self.bit[i]
i -= i & -i
return s
def bit_add(self, i, n):
i += 1
while i < self.N:
self.bit[i] += n
i += i & -i
N, *A = map(int, open(0).read().split())
inf = [(a, i) for i, a in enumerate(A)]
n = A[0]
ls = [0]
for i in range(1, N):
if A[i] > n:
n = A[i]
ls.append(i)
B = BIT(N)
C = BIT(N)
inf.sort(reverse=True)
cnt = [0] * N
ccnt = [0] * N
for a, i in inf:
B.bit_add(i, a)
cnt[i] = B.bit_sum(N - 1) - B.bit_sum(i)
for a, i in inf:
C.bit_add(i, 1)
ccnt[i] = C.bit_sum(N - 1) - C.bit_sum(i)
ans = [0] * N
s = sum(A)
p = 0
x = 0
for i in range(len(ls) - 1, 0, -1):
a = ls[i]
b = ls[i - 1]
m = cnt[b] - A[b] * ccnt[b]
n = m - x
ans[a] = n
s -= n
x += ans[a]
ans[0] = s
print("\n".join(map(str, ans)))
| Statement
Snuke loves constructing integer sequences.
There are N piles of stones, numbered 1 through N. The pile numbered i
consists of a_i stones.
Snuke will construct an integer sequence s of length Σa_i, as follows:
1. Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.
2. Select a pile with one or more stones remaining, and remove a stone from that pile.
3. If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.
We are interested in the lexicographically smallest sequence that can be
constructed. For each of the integers 1,2,3,...,N, how many times does it
occur in the lexicographically smallest sequence? | [{"input": "2\n 1 2", "output": "2\n 1\n \n\nThe lexicographically smallest sequence is constructed as follows:\n\n * Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.\n * Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.\n * Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.\n\nThe resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2\noccurs once.\n\n* * *"}, {"input": "10\n 1 2 1 3 2 4 2 5 8 1", "output": "10\n 7\n 0\n 4\n 0\n 3\n 0\n 2\n 3\n 0"}] |
Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K}
\gcd(a,b,c)}.
* * * | s793635495 | Runtime Error | p02713 | Input is given from Standard Input in the following format:
K | from sys import stdin
import numpy as np
def gcd(num_list):
tmp = np.gcd(num_list[0], num_list[1])
return np.gcd(tmp, num_list[2])
def A():
n = stdin.readline().rstrip()
flag = bool()
for st in n:
if st == "7":
flag = True
if flag:
print("Yes")
else:
print("No")
def B():
n = stdin.readline().rstrip()
sum = 0
for i in range(1, int(n) + 1):
if not (i % 3 == 0 or i % 5 == 0):
sum = sum + i
print(sum)
def C():
n = stdin.readline().rstrip()
sum = 0
lis = list()
lis = [
[a, b, c]
for a in range(1, int(n) + 1)
for b in range(1, int(n) + 1)
for c in range(1, int(n) + 1)
]
for i in lis:
sum = sum + gcd(i)
print(sum)
if __name__ == "__main__":
C()
| Statement
Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.
Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c. | [{"input": "2", "output": "9\n \n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)\n+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2) =1+1+1+1+1+1+1+2=9\n\nThus, the answer is 9.\n\n* * *"}, {"input": "200", "output": "10813692"}] |
Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K}
\gcd(a,b,c)}.
* * * | s275019365 | Wrong Answer | p02713 | Input is given from Standard Input in the following format:
K | N = int(input())
gcd_sum_of_2 = 9
if N == 1:
print(1)
elif N == 2:
print(gcd_sum_of_2)
else:
M = N - 2
for i in range(M):
gcd_sum_of_2 += (i + 2) * 3 + i + 3
print(gcd_sum_of_2)
| Statement
Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.
Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c. | [{"input": "2", "output": "9\n \n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)\n+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2) =1+1+1+1+1+1+1+2=9\n\nThus, the answer is 9.\n\n* * *"}, {"input": "200", "output": "10813692"}] |
Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K}
\gcd(a,b,c)}.
* * * | s817790443 | Accepted | p02713 | Input is given from Standard Input in the following format:
K | s = "0 1 9 30 76 141 267 400 624 885 1249 1590 2208 2689 3411 4248 5248 6081 7485 8530 10248 11889 13687 15228 17988 20053 22569 25242 28588 31053 35463 38284 42540 46581 50893 55362 61824 65857 71247 76884 84388 89349 97881 103342 111528 120141 128047 134580 146316 154177 164817 174438 185836 194157 207927 218812 233268 245277 257857 268182 288216 299257 313635 330204 347836 362973 383709 397042 416448 434025 456967 471948 499740 515581 536073 559758 583960 604833 633651 652216 683712 709065 734233 754734 793188 818917 846603 874512 909496 933081 977145 1006126 1041504 1073385 1106467 1138536 1187112 1215145 1255101 1295142 1342852 1373253 1422195 1453816 1502376 1553361 1595437 1629570 1691292 1726717 1782111 1827492 1887772 1925853 1986837 2033674 2089776 2145333 2197483 2246640 2332104 2379085 2434833 2490534 2554600 2609625 2693919 2742052 2813988 2875245 2952085 3003306 3096024 3157249 3224511 3306240 3388576 3444609 3533637 3591322 3693924 3767085 3842623 3912324 4027884 4102093 4181949 4270422 4361548 4427853 4548003 4616104 4718640 4812789 4918561 5003286 5131848 5205481 5299011 5392008 5521384 5610705 5739009 5818390 5930196 6052893 6156139 6239472 6402720 6493681 6623853 6741078 6864016 6953457 7094451 7215016 7359936 7475145 7593865 7689630 7886244 7984165 8130747 8253888 8403448 8523897 8684853 8802826 8949612 9105537 9267595 9376656 9574704 9686065 9827097 9997134 10174780 10290813 10493367 10611772 10813692"
ss = s.split()
n = int(input())
print(ss[n])
| Statement
Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.
Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c. | [{"input": "2", "output": "9\n \n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)\n+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2) =1+1+1+1+1+1+1+2=9\n\nThus, the answer is 9.\n\n* * *"}, {"input": "200", "output": "10813692"}] |
Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K}
\gcd(a,b,c)}.
* * * | s717366457 | Runtime Error | p02713 | Input is given from Standard Input in the following format:
K | from itertools import product
x1 = int(input())
x2 = input()
R_i = [i for i, x in enumerate(x2) if x == "R"]
G_i = [i for i, x in enumerate(x2) if x == "G"]
B_i = {i for i, x in enumerate(x2) if x == "B"}
R = len(R_i)
G = len(G_i)
B = len(B_i)
xs = map(sorted, product(R_i, G_i))
s = 0
for a in xs:
k = a[1] * 2 - a[0] # 2j-i
if k in B_i:
s += 1
i = a[0] * 2 - a[1] # 2j-k
if i in B_i:
s += 1
j = (a[0] + a[1]) / 2
if j.is_integer():
j = int(j)
if j in B_i:
s += 1
print(R * G * B - s)
| Statement
Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.
Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c. | [{"input": "2", "output": "9\n \n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)\n+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2) =1+1+1+1+1+1+1+2=9\n\nThus, the answer is 9.\n\n* * *"}, {"input": "200", "output": "10813692"}] |
Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K}
\gcd(a,b,c)}.
* * * | s266744675 | Wrong Answer | p02713 | Input is given from Standard Input in the following format:
K | # ユークリッドの互除法
def euclid(m, n):
# m > n となるように入れ替え
if m < n:
m, n = n, m
while n != 0:
m, n = n, m % n
return m
num = int(input("")) # input()で入力を受け取ってint()で数字型に変換します。
ans_sum = 0
for a in range(1, num + 1):
for b in range(1, a):
gcd_ab = euclid(a, num + 1)
for c in range(1, num + 1):
ans_sum = ans_sum + euclid(gcd_ab, c)
ans_sum = ans_sum * 2
print(ans_sum)
| Statement
Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.
Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c. | [{"input": "2", "output": "9\n \n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)\n+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2) =1+1+1+1+1+1+1+2=9\n\nThus, the answer is 9.\n\n* * *"}, {"input": "200", "output": "10813692"}] |
Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K}
\gcd(a,b,c)}.
* * * | s224667381 | Accepted | p02713 | Input is given from Standard Input in the following format:
K | [
print(
sum(
[
g(g(i, j), k)
for g in [__import__("math").gcd]
for n in [int(input())]
for r in [range(1, n + 1)]
for i in r
for j in r
for k in r
]
)
)
]
| Statement
Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.
Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c. | [{"input": "2", "output": "9\n \n\n\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)\n+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2) =1+1+1+1+1+1+1+2=9\n\nThus, the answer is 9.\n\n* * *"}, {"input": "200", "output": "10813692"}] |
For each same operation, print 1 if x and y are in the same set, otherwise 0,
in a line. | s906542069 | Accepted | p02343 | n q
com1 x1 y1
com2 x2 y2
...
comq xq yq
In the first line, n and q are given. Then, q queries are given where com
represents the type of queries. '0' denotes unite and '1' denotes same
operation. | #!python3
import sys
iim = lambda: map(int, input().rstrip().split())
def resolve():
N, Q = iim()
S = [[i] for i in range(N)]
def find(i):
x = S[i]
while True:
k = S[i][0]
if k == i:
x[0] = i
return i
S[i] = x
i = k
def union(i, j):
ai = find(i)
aj = find(j)
if ai != aj:
S[aj][0] = S[ai][0]
S[aj] = S[ai]
def same(i, j):
ai = find(i)
aj = find(j)
return ai == aj
ans = []
for com, x, y in (map(int, line.split()) for line in sys.stdin):
if com == 0:
union(x, y)
# print(x, y, *tuple(map(lambda x: (x, hex(id(x))[-4:]), S)))
elif com == 1:
ans.append(1 if same(x, y) else 0)
else:
raise 1
print(*ans, sep="\n")
if __name__ == "__main__":
resolve()
| Disjoint Set
Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.
First of all, the program should read an integer n, then make a disjoint set
where each element consists of 0, 1, ... n−1 respectively.
Next, the program should read an integer q and manipulate the set for q
queries. There are two kinds of queries for different operations:
* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set. | [{"input": "12\n 0 1 4\n 0 2 3\n 1 1 2\n 1 3 4\n 1 1 4\n 1 3 2\n 0 1 3\n 1 2 4\n 1 3 0\n 0 0 4\n 1 0 2\n 1 3 0", "output": "0\n 1\n 1\n 1\n 0\n 1\n 1"}] |
For each same operation, print 1 if x and y are in the same set, otherwise 0,
in a line. | s879250654 | Accepted | p02343 | n q
com1 x1 y1
com2 x2 y2
...
comq xq yq
In the first line, n and q are given. Then, q queries are given where com
represents the type of queries. '0' denotes unite and '1' denotes same
operation. | import sys
class UnionFind(object):
def __init__(self):
self.node_id = {}
self.parent_id = []
self.rank = []
def find(self, node_id):
while node_id != self.parent_id[node_id]:
pp = self.parent_id[self.parent_id[node_id]]
self.parent_id[node_id] = pp
node_id = pp
return node_id
def add(self, x):
if x in self.node_id:
return self.node_id[x]
node_id = len(self.node_id)
self.node_id[x] = node_id
self.parent_id.append(len(self.parent_id))
self.rank.append(0)
return node_id
def union(self, p, q):
i = self.find(p)
j = self.find(q)
if i == j:
return
if self.rank[i] < self.rank[j]:
self.parent_id[i] = j
elif self.rank[i] > self.rank[j]:
self.parent_id[j] = i
else:
self.parent_id[j] = i
self.rank[i] += 1
def dump_groups(self):
result = []
group_id = {}
for x, i in self.node_id.items():
j = self.find(i)
if j not in group_id:
group_id[j] = len(result)
result.append([])
result[group_id[j]].append(x)
return result
def dump_dot(self):
result = """digraph { graph [rankdir=LR];\n"""
for i, target in enumerate(self.parent_id):
result += """"%s" [label=\"%s\",shape=plaintext,shape=circle];\n""" % (i, i)
if i != target:
result += """"%s" -> "%s";\n""" % (i, target)
result += "}"
return result
uf = UnionFind()
(n, q) = sys.stdin.readline().split()
for i in range(int(n)):
uf.add(i)
for i in range(int(q)):
(c, x, y) = sys.stdin.readline().split()
if c == "0":
uf.union(int(x), int(y))
else:
print(1 if uf.find(int(x)) == uf.find(int(y)) else 0)
| Disjoint Set
Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.
First of all, the program should read an integer n, then make a disjoint set
where each element consists of 0, 1, ... n−1 respectively.
Next, the program should read an integer q and manipulate the set for q
queries. There are two kinds of queries for different operations:
* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set. | [{"input": "12\n 0 1 4\n 0 2 3\n 1 1 2\n 1 3 4\n 1 1 4\n 1 3 2\n 0 1 3\n 1 2 4\n 1 3 0\n 0 0 4\n 1 0 2\n 1 3 0", "output": "0\n 1\n 1\n 1\n 0\n 1\n 1"}] |
For each same operation, print 1 if x and y are in the same set, otherwise 0,
in a line. | s359455231 | Accepted | p02343 | n q
com1 x1 y1
com2 x2 y2
...
comq xq yq
In the first line, n and q are given. Then, q queries are given where com
represents the type of queries. '0' denotes unite and '1' denotes same
operation. | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10000)
N, Q = tuple(map(int, input().split()))
tree = [[-1, 1] for _ in range(N)] # [next, rank]
def find(i):
if tree[i][0] == -1:
group = i
else:
group = find(tree[i][0])
return group
def unite(x, y):
px = find(x)
py = find(y)
if tree[px][1] == tree[py][1]: # rank is same
tree[py][0] = px
tree[px][1] += 1
else:
if tree[px][1] < tree[py][1]:
px, py = py, px
tree[py][0] = px
for _ in range(Q):
com, x, y = tuple(map(int, input().split()))
if com: # same
print(int(find(x) == find(y)))
else: # unite
if not int(find(x) == find(y)):
unite(x, y)
| Disjoint Set
Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.
First of all, the program should read an integer n, then make a disjoint set
where each element consists of 0, 1, ... n−1 respectively.
Next, the program should read an integer q and manipulate the set for q
queries. There are two kinds of queries for different operations:
* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set. | [{"input": "12\n 0 1 4\n 0 2 3\n 1 1 2\n 1 3 4\n 1 1 4\n 1 3 2\n 0 1 3\n 1 2 4\n 1 3 0\n 0 0 4\n 1 0 2\n 1 3 0", "output": "0\n 1\n 1\n 1\n 0\n 1\n 1"}] |
Print the number of codes among the given N codes that correctly solve this
problem.
* * * | s358666223 | Accepted | p03102 | Input is given from Standard Input in the following format:
N M C
B_1 B_2 ... B_M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
\vdots
A_{N1} A_{N2} ... A_{NM} | inp = list(map(int, input().split()))
b = list(map(int, input().split()))
n = inp[0]
m = inp[1]
c = inp[2]
wa = 0
seikai = 0
for i in range(n):
retu = list(map(int, input().split()))
for j in range(m):
kake = retu[j] * b[j]
wa = wa + kake
if wa + c > 0:
seikai = seikai + 1
wa = 0
print(seikai)
| Statement
There are N pieces of source code. The characteristics of the i-th code is
represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.
Additionally, you are given integers B_1, B_2, ..., B_M and C.
The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2}
B_2 + ... + A_{iM} B_M + C > 0.
Among the N codes, find the number of codes that correctly solve this problem. | [{"input": "2 3 -10\n 1 2 3\n 3 2 1\n 1 2 2", "output": "1\n \n\nOnly the second code correctly solves this problem, as follows:\n\n * Since 3 \\times 1 + 2 \\times 2 + 1 \\times 3 + (-10) = 0 \\leq 0, the first code does not solve this problem.\n * 1 \\times 1 + 2 \\times 2 + 2 \\times 3 + (-10) = 1 > 0, the second code solves this problem.\n\n* * *"}, {"input": "5 2 -4\n -2 5\n 100 41\n 100 40\n -3 0\n -6 -2\n 18 -13", "output": "2\n \n\n* * *"}, {"input": "3 3 0\n 100 -100 0\n 0 100 100\n 100 100 100\n -100 100 100", "output": "0\n \n\nAll of them are _Wrong Answer_. Except yours."}] |
No matter how good your plan is, a "dull-year" would eventually come, in which
the bamboos do not flower in any block. For each dataset, output in a line an
integer meaning how many years from now the first dull-year comes after the
first _m_ years.
Note that the input of _m_ = 2 and _n_ = 500,000 (the last dataset of the
Sample Input) gives the largest answer. | s322392405 | Accepted | p01095 | The input consists of at most 50 datasets, each in the following format.
_m_ _n_
An integer _m_ (2 ≤ _m_ ≤ 100) represents the lifetime (in years) of the
bamboos with the shortest lifetime that Dr. ACM can use for gardening. An
integer _n_ (1 ≤ _n_ ≤ 500,000) represents the number of blocks.
The end of the input is indicated by a line containing two zeros. | from bisect import bisect_left
def rwh_primes2(n):
# https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
"""Input n>=6, Returns a list of primes, 2 <= p < n"""
correction = n % 6 > 1
n = {0: n, 1: n - 1, 2: n + 4, 3: n + 3, 4: n + 2, 5: n + 1}[n % 6]
sieve = [True] * (n // 3)
sieve[0] = False
for i in range(int(n**0.5) // 3 + 1):
if sieve[i]:
k = 3 * i + 1 | 1
sieve[((k * k) // 3) :: 2 * k] = [False] * (
(n // 6 - (k * k) // 6 - 1) // k + 1
)
sieve[(k * k + 4 * k - 2 * k * (i & 1)) // 3 :: 2 * k] = [False] * (
(n // 6 - (k * k + 4 * k - 2 * k * (i & 1)) // 6 - 1) // k + 1
)
return [2, 3] + [3 * i + 1 | 1 for i in range(1, n // 3 - correction) if sieve[i]]
if __name__ == "__main__":
prime = rwh_primes2(7368791 + 10)
while True:
m, n = map(int, input().split())
if m == 0 and n == 0:
break
small_prime = prime[: bisect_left(prime, m**2)]
composite = []
for i in range(1, m + 1):
for j in range(i, m + 1):
x = i * j
if m <= x <= m**2 and x not in small_prime:
for p in small_prime:
if x % p == 0:
if x < m * p:
composite.append(x)
break
composite = sorted(list(set(composite)))
pp = bisect_left(prime, m)
cp = 0
sz = len(composite)
for i in range(n + 1):
if cp < sz:
if composite[cp] < prime[pp]:
ans = composite[cp]
cp += 1
else:
ans = prime[pp]
pp += 1
else:
ans = prime[pp + n - i]
break
print(ans)
| Bamboo Blossoms
The bamboos live for decades, and at the end of their lives, they flower to
make their seeds. Dr. ACM, a biologist, was fascinated by the bamboos in
blossom in his travel to Tsukuba. He liked the flower so much that he was
tempted to make a garden where the bamboos bloom annually. Dr. ACM started
research of improving breed of the bamboos, and finally, he established a
method to develop bamboo breeds with controlled lifetimes. With this method,
he can develop bamboo breeds that flower after arbitrarily specified years.
Let us call bamboos that flower _k_ years after sowing "_k_ -year-bamboos."
_k_ years after being sowed, _k_ -year-bamboos make their seeds and then die,
hence their next generation flowers after another _k_ years. In this way, if
he sows seeds of _k_ -year-bamboos, he can see bamboo blossoms every _k_
years. For example, assuming that he sows seeds of 15-year-bamboos, he can see
bamboo blossoms every 15 years; 15 years, 30 years, 45 years, and so on, after
sowing.
Dr. ACM asked you for designing his garden. His garden is partitioned into
blocks, in each of which only a single breed of bamboo can grow. Dr. ACM
requested you to decide which breeds of bamboos should he sow in the blocks in
order to see bamboo blossoms in at least one block for as many years as
possible.
You immediately suggested to sow seeds of one-year-bamboos in all blocks. Dr.
ACM, however, said that it was difficult to develop a bamboo breed with short
lifetime, and would like a plan using only those breeds with long lifetimes.
He also said that, although he could wait for some years until he would see
the first bloom, he would like to see it in every following year. Then, you
suggested a plan to sow seeds of 10-year-bamboos, for example, in different
blocks each year, that is, to sow in a block this year and in another block
next year, and so on, for 10 years. Following this plan, he could see bamboo
blossoms in one block every year except for the first 10 years. Dr. ACM
objected again saying he had determined to sow in all blocks this year.
After all, you made up your mind to make a sowing plan where the bamboos bloom
in at least one block for as many consecutive years as possible after the
first _m_ years (including this year) under the following conditions:
* the plan should use only those bamboo breeds whose lifetimes are _m_ years or longer, and
* Dr. ACM should sow the seeds in all the blocks only this year. | [{"input": "1\n 3 4\n 10 20\n 100 50\n 2 500000\n 0 0", "output": "11\n 47\n 150\n 7368791"}] |
No matter how good your plan is, a "dull-year" would eventually come, in which
the bamboos do not flower in any block. For each dataset, output in a line an
integer meaning how many years from now the first dull-year comes after the
first _m_ years.
Note that the input of _m_ = 2 and _n_ = 500,000 (the last dataset of the
Sample Input) gives the largest answer. | s960090695 | Accepted | p01095 | The input consists of at most 50 datasets, each in the following format.
_m_ _n_
An integer _m_ (2 ≤ _m_ ≤ 100) represents the lifetime (in years) of the
bamboos with the shortest lifetime that Dr. ACM can use for gardening. An
integer _n_ (1 ≤ _n_ ≤ 500,000) represents the number of blocks.
The end of the input is indicated by a line containing two zeros. | MAX = 7368792
for e in iter(input, "0 0"):
m, n = map(int, e.split())
table = [False] * MAX
for _ in range(n):
table[m::m] = [True] * len(table[m::m])
while table[m]:
m += 1
print(m)
| Bamboo Blossoms
The bamboos live for decades, and at the end of their lives, they flower to
make their seeds. Dr. ACM, a biologist, was fascinated by the bamboos in
blossom in his travel to Tsukuba. He liked the flower so much that he was
tempted to make a garden where the bamboos bloom annually. Dr. ACM started
research of improving breed of the bamboos, and finally, he established a
method to develop bamboo breeds with controlled lifetimes. With this method,
he can develop bamboo breeds that flower after arbitrarily specified years.
Let us call bamboos that flower _k_ years after sowing "_k_ -year-bamboos."
_k_ years after being sowed, _k_ -year-bamboos make their seeds and then die,
hence their next generation flowers after another _k_ years. In this way, if
he sows seeds of _k_ -year-bamboos, he can see bamboo blossoms every _k_
years. For example, assuming that he sows seeds of 15-year-bamboos, he can see
bamboo blossoms every 15 years; 15 years, 30 years, 45 years, and so on, after
sowing.
Dr. ACM asked you for designing his garden. His garden is partitioned into
blocks, in each of which only a single breed of bamboo can grow. Dr. ACM
requested you to decide which breeds of bamboos should he sow in the blocks in
order to see bamboo blossoms in at least one block for as many years as
possible.
You immediately suggested to sow seeds of one-year-bamboos in all blocks. Dr.
ACM, however, said that it was difficult to develop a bamboo breed with short
lifetime, and would like a plan using only those breeds with long lifetimes.
He also said that, although he could wait for some years until he would see
the first bloom, he would like to see it in every following year. Then, you
suggested a plan to sow seeds of 10-year-bamboos, for example, in different
blocks each year, that is, to sow in a block this year and in another block
next year, and so on, for 10 years. Following this plan, he could see bamboo
blossoms in one block every year except for the first 10 years. Dr. ACM
objected again saying he had determined to sow in all blocks this year.
After all, you made up your mind to make a sowing plan where the bamboos bloom
in at least one block for as many consecutive years as possible after the
first _m_ years (including this year) under the following conditions:
* the plan should use only those bamboo breeds whose lifetimes are _m_ years or longer, and
* Dr. ACM should sow the seeds in all the blocks only this year. | [{"input": "1\n 3 4\n 10 20\n 100 50\n 2 500000\n 0 0", "output": "11\n 47\n 150\n 7368791"}] |
Print the score.
* * * | s801599502 | Accepted | p03552 | Input is given from Standard Input in the following format:
N Z W
a_1 a_2 ... a_N | n, z, w = map(int, input().split())
t = tuple(map(int, input().split()))
if n == 1:
print(abs(t[0] - w))
exit()
# dp[i][j] := i枚引いた後,jから始めるときの最善スコア
# j=0ならxから,j=1ならyから
# 求めたいのはdp[0][0]
dp = [[0] * 2 for _ in range(n)]
# n-1枚引いた後は分かる
for j in range(2):
dp[n - 1][j] = abs(t[n - 2] - t[n - 1])
# dp[i+1][j]が分かっているときのdp[i][j]を求める
for i in range(n - 2, -1, -1):
for j in range(2):
# xスタートの時
if j == 0:
# n-i枚残っているので,
# 取る枚数は1, 2, ..., n-i-1, n-i枚のどれか
# n-i-i枚までのときは,k枚引くとして以下が考えられる
l = [dp[i + k][1] for k in range(1, n - i)]
# n-i枚引くときは
q = abs(t[n - 1] - w) if i == 0 else abs(t[n - 1] - t[i - 1])
l.append(q)
dp[i][j] = max(l)
else:
l = [dp[i + k][0] for k in range(1, n - i)]
if i != 0:
q = abs(t[n - 1] - t[i - 1])
l.append(q)
dp[i][j] = min(l)
print(dp[0][0])
| Statement
We have a deck consisting of N cards. Each card has an integer written on it.
The integer on the i-th card from the top is a_i.
Two people X and Y will play a game using this deck. Initially, X has a card
with Z written on it in his hand, and Y has a card with W written on it in his
hand. Then, starting from X, they will alternately perform the following
action:
* Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.
The game ends when there is no more card in the deck. The score of the game is
the absolute difference of the integers written on the cards in the two
players' hand.
X will play the game so that the score will be maximized, and Y will play the
game so that the score will be minimized. What will be the score of the game? | [{"input": "3 100 100\n 10 1000 100", "output": "900\n \n\nIf X draws two cards first, Y will draw the last card, and the score will be\n|1000 - 100| = 900.\n\n* * *"}, {"input": "3 100 1000\n 10 100 100", "output": "900\n \n\nIf X draws all the cards first, the score will be |1000 - 100| = 900.\n\n* * *"}, {"input": "5 1 1\n 1 1 1 1 1", "output": "0\n \n\n* * *"}, {"input": "1 1 1\n 1000000000", "output": "999999999"}] |
Print the score.
* * * | s322328592 | Accepted | p03552 | Input is given from Standard Input in the following format:
N Z W
a_1 a_2 ... a_N | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
from math import *
import random
def readln():
return list(map(int, (input().split(" "))))
n, z, w = readln()
a = readln()
f = [0 for i in range(n)]
min_f = [0 for i in range(n)]
max_min_f = [0 for i in range(n)]
s = [a[n - i - 1] for i in range(n)]
s.append(w)
f[0] = abs(s[0] - s[1])
min_f[0] = f[0]
max_min_f[0] = f[0]
for i in range(1, n):
f[i] = max(abs(s[0] - s[i + 1]), max_min_f[i - 1])
min_f[i] = min(f[i], min_f[i - 1])
max_min_f[i] = max(min_f[i], max_min_f[i - 1])
print(f[n - 1])
# F[i] 表示y手里是i,x的最优解
| Statement
We have a deck consisting of N cards. Each card has an integer written on it.
The integer on the i-th card from the top is a_i.
Two people X and Y will play a game using this deck. Initially, X has a card
with Z written on it in his hand, and Y has a card with W written on it in his
hand. Then, starting from X, they will alternately perform the following
action:
* Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.
The game ends when there is no more card in the deck. The score of the game is
the absolute difference of the integers written on the cards in the two
players' hand.
X will play the game so that the score will be maximized, and Y will play the
game so that the score will be minimized. What will be the score of the game? | [{"input": "3 100 100\n 10 1000 100", "output": "900\n \n\nIf X draws two cards first, Y will draw the last card, and the score will be\n|1000 - 100| = 900.\n\n* * *"}, {"input": "3 100 1000\n 10 100 100", "output": "900\n \n\nIf X draws all the cards first, the score will be |1000 - 100| = 900.\n\n* * *"}, {"input": "5 1 1\n 1 1 1 1 1", "output": "0\n \n\n* * *"}, {"input": "1 1 1\n 1000000000", "output": "999999999"}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.