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 length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s208141384 | Accepted | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | # -*- coding: utf-8 -*-
"""
Created on Sun Aug 4 22:01:34 2019
@author: miz18
"""
class SegmentTree:
def __init__(self, L, op, ide):
self.op = op
self.ide = ide
self.sz = len(L)
self.n = 1
self.s = 1
for i in range(1000):
self.n *= 2
self.s += 1
if self.n >= self.sz:
break
self.node = [self.ide] * (2 * self.n - 1)
for i in range(self.sz):
self.node[i + self.n - 1] = L[i]
for i in range(self.n - 2, -1, -1):
self.node[i] = self.op(self.node[i * 2 + 1], self.node[i * 2 + 2])
def add(self, a, x):
k = a + self.n - 1
self.node[k] += x
for i in range(1000):
k = (k - 1) // 2
self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
if k <= 0:
break
def substitute(self, a, x):
k = a + self.n - 1
self.node[k] = min(x, self.node[k])
for i in range(1000):
k = (k - 1) // 2
self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
if k <= 0:
break
def get_one(self, a):
k = a + self.n - 1
return self.node[k]
def get(self, l, r):
res = self.ide
n = self.n
if self.sz <= r or 0 > l:
print("ERROR: the indice are wrong.")
return False
for i in range(self.s):
count = 2**i - 1
a = (r + 1) // n
b = (l - 1) // n
if a - b == 3:
res = self.op(self.node[count + b + 1], res)
res = self.op(self.node[count + b + 2], res)
right = a * n
left = (b + 1) * n - 1
break
if a - b == 2:
res = self.op(self.node[count + b + 1], res)
right = a * n
left = (b + 1) * n - 1
break
n = n // 2
# left
n1 = n // 2
for j in range(i + 1, self.s):
count = 2**j - 1
a = (left + 1) // n1
b = (l - 1) // n1
if a - b == 2:
res = self.op(self.node[count + b + 1], res)
left = (b + 1) * n1 - 1
n1 = n1 // 2
# right
n1 = n // 2
for j in range(i + 1, self.s):
count = 2**j - 1
a = (r + 1) // n1
b = (right - 1) // n1
if a - b == 2:
res = self.op(self.node[count + b + 1], res)
right = a * n1
n1 = n1 // 2
return res
N, M = list(map(int, input().split()))
road = []
for i in range(M):
road.append(list(map(int, input().split())))
road.sort(key=lambda x: x[2])
road.sort(key=lambda x: x[0])
R = [10**27] * N
R[0] = 0
ST = SegmentTree(R, min, 10**27)
for i in range(M):
now_min = ST.get(road[i][0] - 1, road[i][1] - 1)
ST.substitute(road[i][1] - 1, now_min + road[i][2])
ans = ST.get_one(N - 1)
if ans >= 10**27:
print(-1)
else:
print(ans)
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s413874647 | Accepted | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | # from collections import Counter,defaultdict,deque
import sys
# import bisect
# import math
# import itertools
# import string
import queue
# import copy
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7
def inp(): # n=1
return int(input())
def inpm(): # x=1,y=2
return map(int, input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
return list(input().split())
def inplm(n): # x=[] 複数行
return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
return sorted([list(map(int, input().split())) for _ in range(n)])
def sortx(x, n, k):
if k == 0:
x.sort(key=lambda y: y[1, n])
else:
x.sort(reversed=True, key=lambda y: y[1, n])
def graph():
n = inp()
g = [[] for _ in range(n)]
for i in range(n):
a = inp()
a -= 1
g[i].append(a)
g[a].append(i)
return n, g
def graphm():
n, m = inpm()
g = [[] for _ in range(n)]
for _ in range(m):
a, b, w = inpm()
a -= 1
b -= 1
g[a].append((b, w))
g[b].append((a, w))
return n, m, g
def dijkstra(s, n, g): # sからの最短距離 頂点数n
s -= 1
que = queue.Queue()
d = [10**15 for _ in range(n)]
d[s] = 0
que.put((0, s))
while not que.empty():
p = que.get()
v = p[1]
if d[v] < p[0]:
continue
for i in range(len(g[v])):
edge = g[v][i]
if d[edge[0]] > d[v] + edge[1]:
d[edge[0]] = d[v] + edge[1]
que.put((d[edge[0]], edge[0]))
return d
def dijkstra1(s, n, g): # sからの最短距離 頂点数n
s -= 1
que = []
d = [10**15 for _ in range(n)]
d[s] = 0
heappush(que, (0, s))
while len(que) > 0:
p = heappop(que)
v = p[1]
if d[v] < p[0]:
continue
for i in range(len(g[v])):
edge = g[v][i]
if d[edge[0]] > d[v] + edge[1]:
d[edge[0]] = d[v] + edge[1]
heappush(que, (d[edge[0]], edge[0]))
return d
def main():
n, m, g = graphm()
s = 1
for i in range(n - 1):
a = i + 1
b = i
g[a].append((b, 0))
dis = dijkstra1(s, n, g)
if dis[n - 1] == 10**15:
print(-1)
else:
print(dis[n - 1])
if __name__ == "__main__":
main()
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s790196763 | Accepted | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | import sys
input = sys.stdin.readline
def I():
return int(input())
def MI():
return map(int, input().split())
def LI():
return list(map(int, input().split()))
def main():
N, M = MI()
inf = 10**16
##########################################
import heapq
class Dijkstra:
"""
・有向 / 無向は問わない(無向の場合は,逆向きの辺もたす)
・負のコストがない場合のみ
・計算量はO(E log|V|)
・heapを使うことで頂点を走査する必要がなくなる(代わりに,距離更新したものは確定でなくともqueに入れておく)
"""
# 最短のpathをたす
class Edge:
# 重み付き有向辺
def __init__(self, _to, _cost):
self.to = _to
self.cost = _cost
def __init__(self, V):
# 引数Vは頂点数
self.G = [[] for _ in range(V)] # 隣接リストG[u][i]が頂点uのi番目の辺
self._E = 0 # 辺の数
self._V = V # 頂点数
# proparty - 辺の数
def E(self):
return self._E
# proparty - 頂点数
def V(self):
return self._V
def add(self, _from, _to, _cost):
# 2頂点と辺のコストを追加
self.G[_from].append(self.Edge(_to, _cost))
self._E += 1
def add2(self, _from, _to, _cost):
# 2頂点と辺のコスト(無向)を追加
self.G[_from].append(self.Edge(_to, _cost))
self.G[_to].append(self.Edge(_from, _cost))
self._E += 2
def shortest_path(self, s): # ,g):
# 始点sから頂点iまでの最短経路長のリストを返す
que = [] # priority queue
d = [inf] * self.V()
prev = [
None
] * self.V() # prev[j]は,sからjへ最短経路で行くときのjの一つ前の場所
d[s] = 0
heapq.heappush(que, (0, s)) # 始点の距離と頂点番号をヒープに追加
while len(que) != 0:
# キューに格納されてある中で一番コストが小さい頂点を取り出す
cost, v = heapq.heappop(que)
# キューに格納された最短経路長候補がdの距離よりも大きい場合に処理をスキップ
if d[v] < cost:
continue
# 頂点vに隣接する各頂点iに対して,vを経由した場合の距離を計算して,これがd[i]よりも小さい場合に更新
for i in range(len(self.G[v])):
e = self.G[v][i] # vのi個目の隣接辺
if d[e.to] > d[v] + e.cost:
d[e.to] = d[v] + e.cost # 更新
prev[e.to] = v
heapq.heappush(
que, (d[e.to], e.to)
) # queに新たな最短経路長候補を追加
"""#sからgまでの最短経路
path = []
pos = g #今いる場所,ゴールで初期化
for _ in range(self.V()+1):
path.append(pos)
if pos == s:
break
#print("pos:",format(pos))
pos = prev[pos]
path.reverse()
#print(path)"""
return d # ,path
########################
djk = Dijkstra(N)
for i in range(M):
l, r, c = MI()
l -= 1
r -= 1
djk.add(l, r, c)
for i in range(1, N):
djk.add(i, i - 1, 0)
d = djk.shortest_path(0)
ans = d[-1]
if ans >= inf:
print(-1)
else:
print(ans)
main()
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s413554618 | Wrong Answer | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | class Node:
def __init__(self, x):
self.data = x
self.count = 1
self.left = None
self.right = None
def insert(node, x):
if node is None:
return Node(x)
if node.data == x:
node.count += 1
elif x < node.data:
node.left = insert(node.left, x)
else:
node.right = insert(node.right, x)
return node
def search_min(node):
if node.left is None:
return node.data
return search_min(node.left)
def delete_min(node):
if node.left is None:
if node.count > 1:
node.count -= 1
else:
return node.right
else:
node.left = delete_min(node.left)
return node
def delete(node, x):
if node:
if x == node.data:
if node.count > 1:
node.count -= 1
if node.left is None:
return node.right
elif node.right is None:
return node.left
else:
node.data = search_min(node.right)
node.right = delete_min(node.right)
elif x < node.data:
node.left = delete(node.left, x)
else:
node.right = delete(node.right, x)
return node
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s114396999 | Runtime Error | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | import pandas as pd
import sys
N, M = map(int, input().split())
L = []
R = []
C = []
for m in range(M):
l, r, c = map(int, input().split())
L.append(l)
R.append(r)
C.append(c)
data = pd.DataFrame({"L": L, "R": R, "C": C})
position = 1
min_c = 0
while True:
data_tmp = data[(data["L"] <= position) & (data["R"] > position)]
if len(data_tmp) == 0:
print(-1)
sys.exit()
min_c_tmp = data_tmp["C"].min()
min_c += min_c_tmp
position = data_tmp[data_tmp["C"] == min_c_tmp]["R"].max()
if position == N:
print(min_c)
break
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s418242208 | Wrong Answer | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | print(-1)
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s530568663 | Wrong Answer | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | N, M = map(int, input().split())
ls = []
for i in range(M):
ls.append(list(map(int, input().split())))
ls.sort()
print(ls)
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s684654441 | Accepted | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | """
演算子は要素とセットでモノイドを形成するようなものでなければならない。
すなわち、結合律が成り立ち単位元が存在する必要がある。
equalityはsetを高速化するために使う。めんどい場合はNoneにしてもOK
"""
class SegmentTree:
@classmethod
def all_identity(cls, operator, equality, identity, size):
return cls(
operator, equality, identity, [identity] * (2 << (size - 1).bit_length())
)
@classmethod
def from_initial_data(cls, operator, equality, identity, data):
size = 1 << (len(data) - 1).bit_length()
temp = [identity] * (2 * size)
temp[size : size + len(data)] = data
data = temp
for i in reversed(range(size)):
data[i] = operator(data[2 * i], data[2 * i + 1])
return cls(operator, equality, identity, data)
# これ使わずファクトリーメソッド使いましょうね
def __init__(self, operator, equality, identity, data):
if equality is None:
equality = lambda a, b: False
self.op = operator
self.eq = equality
self.id = identity
self.data = data
self.size = len(data) // 2
def _interval(self, a, b):
a += self.size
b += self.size
ra = self.id
rb = self.id
data = self.data
op = self.op
while a < b:
if a & 1:
ra = op(ra, data[a])
a += 1
if b & 1:
b -= 1
rb = op(data[b], rb)
a >>= 1
b >>= 1
return op(ra, rb)
def __getitem__(self, i):
if isinstance(i, slice):
return self._interval(
0 if i.start is None else i.start,
self.size if i.stop is None else i.stop,
)
elif isinstance(i, int):
return self.data[i + self.size]
def __setitem__(self, i, v):
i += self.size
data = self.data
op = self.op
eq = self.eq
while i and not eq(data[i], v):
data[i] = v
v = op(data[i ^ 1], v) if i & 1 else op(v, data[i ^ 1])
i >>= 1
def __iter__(self):
return iter(self.data[self.size :])
N, M = map(int, input().split())
memo = sorted(tuple(map(int, input().split())) for _ in range(M))
seg = SegmentTree.all_identity(min, None, float("inf"), N)
seg[0] = 0
for l, r, c in memo:
seg[r - 1] = min(seg[r - 1], seg[l - 1 : r] + c)
res = seg[N - 1]
print(res if res != float("inf") else -1)
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
Print the length of the shortest path from Vertex 1 to Vertex N in the final
graph. If there is no shortest path, print `-1` instead.
* * * | s830229971 | Accepted | p02868 | Input is given from Standard Input in the following format:
N M
L_1 R_1 C_1
:
L_M R_M C_M | from heapq import heappush, heappop
from collections import defaultdict
INF = float("inf")
def dijkstra(V, E, s):
"""
Input:
V = list(range(N))
E = defaultdict(dict)
s (in V) : start vertex
Output:
dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
"""
dist = {v: INF for v in V}
heap = []
dist[s] = 0
rem = len(V)
heappush(heap, (0, s))
while heap and rem:
d, v = heappop(heap)
# don't need to check when the stored value is greater than provisional distance
if d > dist[v]:
continue
rem -= 1
for u, c in E[v].items():
temp = d + c
if temp < dist[u]:
dist[u] = temp
heappush(heap, (temp, u))
return dist
N, M = map(int, input().split())
V = list(range(N))
E = defaultdict(dict)
for i in range(N):
E[i + 1][i] = 0
for _ in range(M):
l, r, c = map(int, input().split())
l, r = l - 1, r - 1
E[l][r] = min(c, E[l].setdefault(r, INF))
dist = dijkstra(V, E, 0)
print(dist[N - 1] if dist[N - 1] != INF else -1)
| Statement
We have N points numbered 1 to N arranged in a line in this order.
Takahashi decides to make an undirected graph, using these points as the
vertices. In the beginning, the graph has no edge. Takahashi will do M
operations to add edges in this graph. The i-th operation is as follows:
* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.
The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as
input.
Takahashi wants to solve the shortest path problem in the final graph
obtained. Find the length of the shortest path from Vertex 1 to Vertex N in
the final graph. | [{"input": "4 3\n 1 3 2\n 2 4 3\n 1 4 6", "output": "5\n \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n 1 2 1\n 3 4 2", "output": "-1\n \n\n* * *"}, {"input": "10 7\n 1 5 18\n 3 4 8\n 1 3 5\n 4 7 10\n 5 9 8\n 6 10 5\n 8 10 3", "output": "28"}] |
The output should be composed of lines each of which contains a single non-
negative integer. It is the length of the prime gap that contains the
corresponding positive integer in the input if it is a composite number, or 0
otherwise. No other characters should occur in the output. | s842635085 | Runtime Error | p00855 | The input is a sequence of lines each of which contains a single positive
integer. Each positive integer is greater than 1 and less than or equal to the
100000th prime number, which is 1299709. The end of the input is indicated by
a line containing a single zero. | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
def sieve_of_erastosthenes(num):
input_list = [
False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)
]
input_list[0] = input_list[1] = False
input_list[2] = input_list[3] = input_list[5] = True
sqrt = math.sqrt(num)
for serial in range(3, num, 2):
if serial >= sqrt:
return input_list
for s in range(serial**2, num, serial):
input_list[s] = False
primeTable = sieve_of_erastosthenes(13 * 10**5)
while True:
k = int(input())
if k == 0:
break
if primeTable[k]:
print(0)
else:
i = 0
while i < k or primeTable[i] == False:
if primeTable[i]:
prevPrime = i
i += 1
print(i - prevPrime)
| B: Prime Gap
The sequence of _n_ \- 1 consecutive composite numbers (positive integers that
are not prime and not equal to 1) lying between two successive prime numbers
_p_ and _p_ \+ _n_ is called a _prime_ gap of length _n_. For example, (24,
25, 26, 27, 28) between 23 and 29 is a prime gap of length 6.
Your mission is to write a program to calculate, for a given positive integer
_k_ , the length of the prime gap that contains _k_. For convenience, the
length is considered 0 in case no prime gap contains _k_. | [{"input": "11\n 27\n 2\n 492170\n 0", "output": "0\n 6\n 0\n 114"}] |
Print the number of pairs of a subsequence of S and a subsequence of T such
that the subsequences are the same in content, modulo 10^9+7.
* * * | s619569452 | Accepted | p03003 | Input is given from Standard Input in the following format:
N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M} | import os
import sys
import numpy as np
if os.getenv("LOCAL"):
sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
N, M = list(map(int, sys.stdin.readline().split()))
S = list(map(int, sys.stdin.readline().split()))
T = list(map(int, sys.stdin.readline().split()))
S = np.array([-1] + S)
T = np.array([-1] + T)
# # dp[i][j]: S[i] と T[j] で終わる組み合わせの数
# # S[i] != T[j] のとき 0
# # S[i] == T[j] のとき、sum(dp[:i][:j])
# dp = np.zeros((len(S), len(T)), dtype=int)
# dp[0][0] = 1
# # dp の二次元累積和
# cumsum = np.ones(len(T), dtype=int)
# for i in range(1, len(S)):
# for j in range(1, len(T)):
# if S[i] != T[j]:
# continue
# # dp[i][j] = dp[:i, :j].sum() % MOD
# dp[i][j] = cumsum[j - 1] % MOD
# cumsum += dp[i].cumsum()
# cumsum %= MOD
# print(cumsum[-1])
cumsum = np.ones(len(T), dtype=int)
for i in range(1, len(S)):
cumsum[1:] += ((T == S[i])[1:] * cumsum[:-1] % MOD).cumsum()
cumsum %= MOD
print(cumsum[-1])
| Statement
You are given two integer sequences S and T of length N and M, respectively,
both consisting of integers between 1 and 10^5 (inclusive).
In how many pairs of a subsequence of S and a subsequence of T do the two
subsequences are the same in content?
Here the subsequence of A is a sequence obtained by removing zero or more
elements from A and concatenating the remaining elements without changing the
order.
For both S and T, we distinguish two subsequences if the sets of the indices
of the removed elements are different, even if the subsequences are the same
in content.
Since the answer can be tremendous, print the number modulo 10^9+7. | [{"input": "2 2\n 1 3\n 3 1", "output": "3\n \n\nS has four subsequences: (), (1), (3), (1, 3).\n\nT has four subsequences: (), (3), (1), (3, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 1 \\times 1 pair of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (3),\nfor a total of three pairs.\n\n* * *"}, {"input": "2 2\n 1 1\n 1 1", "output": "6\n \n\nS has four subsequences: (), (1), (1), (1, 1).\n\nT has four subsequences: (), (1), (1), (1, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 2 \\times 2 pairs of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (1,1),\nfor a total of six pairs. Note again that we distinguish two subsequences if\nthe sets of the indices of the removed elements are different, even if the\nsubsequences are the same in content.\n\n* * *"}, {"input": "4 4\n 3 4 5 6\n 3 4 5 6", "output": "16\n \n\n* * *"}, {"input": "10 9\n 9 6 5 7 5 9 8 5 6 7\n 8 6 8 5 5 7 9 9 7", "output": "191\n \n\n* * *"}, {"input": "20 20\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "846527861\n \n\nBe sure to print the number modulo 10^9+7."}] |
Print the number of pairs of a subsequence of S and a subsequence of T such
that the subsequences are the same in content, modulo 10^9+7.
* * * | s950439554 | Accepted | p03003 | Input is given from Standard Input in the following format:
N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M} | MOD = 10**9 + 7
def main(S, T):
l = len(T) + 1
dp = [0] * l
# sdp = [[0] * (len(T) + 1) for _ in range(len(S) + 1)]
# for i, s in enumerate(S):
# for j, t in enumerate(T):
# v = sdp[i][j + 1] + sdp[i + 1][j] + (1 if s == t else -sdp[i][j])
# sdp[i + 1][j + 1] = v % MOD
for i, s in enumerate(S):
tmp = [0] * l
for j, t in enumerate(T):
v = dp[j + 1] + tmp[j] + (1 if s == t else -dp[j])
tmp[j + 1] = v % MOD
dp = tmp
# return (sdp[-1][-1] + 1)
return dp[-1] + 1
if __name__ == "__main__":
if True:
_, _ = map(int, input().strip().split())
S = input().strip().split()
T = input().strip().split()
else:
S = range(1000)
T = range(1000)
print(main(S, T))
| Statement
You are given two integer sequences S and T of length N and M, respectively,
both consisting of integers between 1 and 10^5 (inclusive).
In how many pairs of a subsequence of S and a subsequence of T do the two
subsequences are the same in content?
Here the subsequence of A is a sequence obtained by removing zero or more
elements from A and concatenating the remaining elements without changing the
order.
For both S and T, we distinguish two subsequences if the sets of the indices
of the removed elements are different, even if the subsequences are the same
in content.
Since the answer can be tremendous, print the number modulo 10^9+7. | [{"input": "2 2\n 1 3\n 3 1", "output": "3\n \n\nS has four subsequences: (), (1), (3), (1, 3).\n\nT has four subsequences: (), (3), (1), (3, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 1 \\times 1 pair of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (3),\nfor a total of three pairs.\n\n* * *"}, {"input": "2 2\n 1 1\n 1 1", "output": "6\n \n\nS has four subsequences: (), (1), (1), (1, 1).\n\nT has four subsequences: (), (1), (1), (1, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 2 \\times 2 pairs of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (1,1),\nfor a total of six pairs. Note again that we distinguish two subsequences if\nthe sets of the indices of the removed elements are different, even if the\nsubsequences are the same in content.\n\n* * *"}, {"input": "4 4\n 3 4 5 6\n 3 4 5 6", "output": "16\n \n\n* * *"}, {"input": "10 9\n 9 6 5 7 5 9 8 5 6 7\n 8 6 8 5 5 7 9 9 7", "output": "191\n \n\n* * *"}, {"input": "20 20\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "846527861\n \n\nBe sure to print the number modulo 10^9+7."}] |
Print the number of pairs of a subsequence of S and a subsequence of T such
that the subsequences are the same in content, modulo 10^9+7.
* * * | s853979037 | Accepted | p03003 | Input is given from Standard Input in the following format:
N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M} | import copy
n, m = map(int, input().split())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
dp = [0 for i in range(max(n, m))]
mod = 10**9 + 7
if n > m:
a = s
b = t
else:
a = t
b = s
c = copy.copy(n)
n = copy.copy(m)
m = copy.copy(c)
l = []
for i in range(m):
now = b[i]
bl = []
for j in range(n):
if now == a[j]:
bl.append(j)
l.append(bl)
for i in range(m):
if len(l[i]) > 0:
dp2 = copy.copy(dp)
now1 = 0
k = 0
k1 = len(l[i])
count = 0
while k < k1:
if now1 == l[i][k]:
dp[now1] += 1
dp[now1] += count
dp[now1] %= mod
k += 1
count += dp2[now1]
count %= mod
now1 += 1
# print(dp)
print((sum(dp) + 1) % mod)
| Statement
You are given two integer sequences S and T of length N and M, respectively,
both consisting of integers between 1 and 10^5 (inclusive).
In how many pairs of a subsequence of S and a subsequence of T do the two
subsequences are the same in content?
Here the subsequence of A is a sequence obtained by removing zero or more
elements from A and concatenating the remaining elements without changing the
order.
For both S and T, we distinguish two subsequences if the sets of the indices
of the removed elements are different, even if the subsequences are the same
in content.
Since the answer can be tremendous, print the number modulo 10^9+7. | [{"input": "2 2\n 1 3\n 3 1", "output": "3\n \n\nS has four subsequences: (), (1), (3), (1, 3).\n\nT has four subsequences: (), (3), (1), (3, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 1 \\times 1 pair of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (3),\nfor a total of three pairs.\n\n* * *"}, {"input": "2 2\n 1 1\n 1 1", "output": "6\n \n\nS has four subsequences: (), (1), (1), (1, 1).\n\nT has four subsequences: (), (1), (1), (1, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 2 \\times 2 pairs of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (1,1),\nfor a total of six pairs. Note again that we distinguish two subsequences if\nthe sets of the indices of the removed elements are different, even if the\nsubsequences are the same in content.\n\n* * *"}, {"input": "4 4\n 3 4 5 6\n 3 4 5 6", "output": "16\n \n\n* * *"}, {"input": "10 9\n 9 6 5 7 5 9 8 5 6 7\n 8 6 8 5 5 7 9 9 7", "output": "191\n \n\n* * *"}, {"input": "20 20\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "846527861\n \n\nBe sure to print the number modulo 10^9+7."}] |
Print the number of pairs of a subsequence of S and a subsequence of T such
that the subsequences are the same in content, modulo 10^9+7.
* * * | s991499307 | Wrong Answer | p03003 | Input is given from Standard Input in the following format:
N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M} | N, K = map(int, input().split())
A = []
A = list(map(int, input().split()))
cnt = 0
for i in range(0, N):
x = 0
for j in range(i, N):
x = x + A[j]
if x >= K:
cnt = cnt + 1
print(cnt)
| Statement
You are given two integer sequences S and T of length N and M, respectively,
both consisting of integers between 1 and 10^5 (inclusive).
In how many pairs of a subsequence of S and a subsequence of T do the two
subsequences are the same in content?
Here the subsequence of A is a sequence obtained by removing zero or more
elements from A and concatenating the remaining elements without changing the
order.
For both S and T, we distinguish two subsequences if the sets of the indices
of the removed elements are different, even if the subsequences are the same
in content.
Since the answer can be tremendous, print the number modulo 10^9+7. | [{"input": "2 2\n 1 3\n 3 1", "output": "3\n \n\nS has four subsequences: (), (1), (3), (1, 3).\n\nT has four subsequences: (), (3), (1), (3, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 1 \\times 1 pair of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (3),\nfor a total of three pairs.\n\n* * *"}, {"input": "2 2\n 1 1\n 1 1", "output": "6\n \n\nS has four subsequences: (), (1), (1), (1, 1).\n\nT has four subsequences: (), (1), (1), (1, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 2 \\times 2 pairs of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (1,1),\nfor a total of six pairs. Note again that we distinguish two subsequences if\nthe sets of the indices of the removed elements are different, even if the\nsubsequences are the same in content.\n\n* * *"}, {"input": "4 4\n 3 4 5 6\n 3 4 5 6", "output": "16\n \n\n* * *"}, {"input": "10 9\n 9 6 5 7 5 9 8 5 6 7\n 8 6 8 5 5 7 9 9 7", "output": "191\n \n\n* * *"}, {"input": "20 20\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "846527861\n \n\nBe sure to print the number modulo 10^9+7."}] |
Print the number of pairs of a subsequence of S and a subsequence of T such
that the subsequences are the same in content, modulo 10^9+7.
* * * | s153664221 | Accepted | p03003 | Input is given from Standard Input in the following format:
N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M} | mo = 10**9 + 7
n, m = map(int, input().split())
s = [0] + [int(i) for i in input().split()]
t = [0] + [int(i) for i in input().split()]
dp = [[0] * (1 + n) for i in range(1 + m)]
sm = [[0] * (1 + n) for i in range(1 + m)]
for i in range(1, 1 + m):
if t[i] == s[1]:
dp[i][1] = 1
sm[i][1] = sm[i - 1][1] + dp[i][1] % mo
for i in range(1, 1 + n):
if s[i] == t[1]:
dp[1][i] = 1
sm[1][i] = sm[1][i - 1] + dp[1][i] % mo
for i in range(2, 1 + m):
for j in range(2, 1 + n):
dp[i][j] = 1 + sm[i - 1][j - 1] if t[i] == s[j] else 0
sm[i][j] = dp[i][j] + sm[i - 1][j] + sm[i][j - 1] - sm[i - 1][j - 1]
sm[i][j] %= mo
print(1 + sm[m][n] % mo)
| Statement
You are given two integer sequences S and T of length N and M, respectively,
both consisting of integers between 1 and 10^5 (inclusive).
In how many pairs of a subsequence of S and a subsequence of T do the two
subsequences are the same in content?
Here the subsequence of A is a sequence obtained by removing zero or more
elements from A and concatenating the remaining elements without changing the
order.
For both S and T, we distinguish two subsequences if the sets of the indices
of the removed elements are different, even if the subsequences are the same
in content.
Since the answer can be tremendous, print the number modulo 10^9+7. | [{"input": "2 2\n 1 3\n 3 1", "output": "3\n \n\nS has four subsequences: (), (1), (3), (1, 3).\n\nT has four subsequences: (), (3), (1), (3, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 1 \\times 1 pair of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (3),\nfor a total of three pairs.\n\n* * *"}, {"input": "2 2\n 1 1\n 1 1", "output": "6\n \n\nS has four subsequences: (), (1), (1), (1, 1).\n\nT has four subsequences: (), (1), (1), (1, 1).\n\nThere are 1 \\times 1 pair of subsequences in which the subsequences are both\n(), 2 \\times 2 pairs of subsequences in which the subsequences are both (1),\nand 1 \\times 1 pair of subsequences in which the subsequences are both (1,1),\nfor a total of six pairs. Note again that we distinguish two subsequences if\nthe sets of the indices of the removed elements are different, even if the\nsubsequences are the same in content.\n\n* * *"}, {"input": "4 4\n 3 4 5 6\n 3 4 5 6", "output": "16\n \n\n* * *"}, {"input": "10 9\n 9 6 5 7 5 9 8 5 6 7\n 8 6 8 5 5 7 9 9 7", "output": "191\n \n\n* * *"}, {"input": "20 20\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "846527861\n \n\nBe sure to print the number modulo 10^9+7."}] |
Print the answer.
* * * | s646825175 | Wrong Answer | p03453 | Input is given from Standard Input in the following format:
N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M | from heapq import heapify, heappop, heappush
N, M = map(int, input().split())
S, T = map(int, input().split())
Graph = [set() for i in range(N)]
Edge = []
mod = 10**9 + 7
inf = float("inf")
for i in range(N):
Edge.append((i, i, 0))
for i in range(M):
a, b, c = map(int, input().split())
Graph[a - 1].add((c, b - 1))
Graph[b - 1].add((c, a - 1))
Edge.append((a - 1, b - 1, c))
Edge.append((b - 1, a - 1, c))
def dijkstra(s, n, links):
Visited = [False] * n
Visited[s] = True
cost = [inf] * n
P = [0] * n
P[s] = 1
cost[s] = 0
heap = [(0, s)]
heapify(heap)
while heap:
hc, hp = heappop(heap)
for c, p in links[hp]:
if Visited[p]:
continue
if c + hc < cost[p]:
cost[p] = c + hc
P[p] = P[hp]
heappush(heap, (cost[p], p))
elif c + hc == cost[p]:
P[p] = (P[p] + P[hp]) % mod
else:
Visited[p] = True
Visited[hp] = True
return cost, P
cost_S, P_S = dijkstra(S - 1, N, Graph)
cost_T, P_T = dijkstra(T - 1, N, Graph)
D = cost_S[T - 1]
List = []
for a, b, c in Edge:
if (
cost_S[a] + c + cost_T[b] == D
and cost_S[a] < D / 2
and cost_T[b] < D / 2
and c > 0
):
List.append((P_S[a] * P_T[b]) % mod)
if (
cost_S[a] + c + cost_T[b] == D
and cost_S[a] == D / 2
and cost_T[b] == D / 2
and c == 0
):
List.append((P_S[a] * P_T[b]) % mod)
Sum = 0
for i in List:
Sum = (Sum + i) % mod
ans = 0
for i, c in enumerate(List):
ans = (ans + (Sum - List[i]) * List[i]) % mod
print(ans)
| Statement
We have a graph with N vertices and M edges, and there are two people on the
graph: Takahashi and Aoki.
The i-th edge connects Vertex U_i and Vertex V_i. The time it takes to
traverse this edge is D_i minutes, regardless of direction and who traverses
the edge (Takahashi or Aoki).
Takahashi departs Vertex S and Aoki departs Vertex T at the same time.
Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the
shortest time possible. Find the number of the pairs of ways for Takahashi and
Aoki to choose their shortest paths such that they never meet (at a vertex or
on an edge) during the travel, modulo 10^9 + 7. | [{"input": "4 4\n 1 3\n 1 2 1\n 2 3 1\n 3 4 1\n 4 1 1", "output": "2\n \n\nThere are two ways to choose shortest paths that satisfies the condition:\n\n * Takahashi chooses the path 1 \\rightarrow 2 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 4 \\rightarrow 1.\n * Takahashi chooses the path 1 \\rightarrow 4 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 2 \\rightarrow 1.\n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "8 13\n 4 2\n 7 3 9\n 6 2 3\n 1 6 4\n 7 6 9\n 3 8 9\n 1 2 2\n 2 8 12\n 8 6 9\n 2 5 5\n 4 2 18\n 5 3 7\n 5 1 515371567\n 4 8 6", "output": "6"}] |
Print the answer.
* * * | s356763879 | Runtime Error | p03453 | Input is given from Standard Input in the following format:
N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M | import sys
input = sys.stdin.readline
INF = 10**18
mod = 10**9+7
# AOJ GRL_1_A Single Sourse Shortest Path
import heapq as hp
N, M = map(int, input().split())
s, t = map(int, input().split())
s -= 1; t -= 1
graph = [[] for _ in range(N)]
for _ in range(M):
n, m, d = map(int, input().split())
graph[n-1].append((d, m-1))
graph[m-1].append((d, n-1)) #無向グラフならこれ使う
def dijkstra(s):
D = [INF for _ in range(N)] # 頂点iへの最短距離がD[i]
B = [0]*N
D[s] = 0
B[s] = 1
q = [] # しまっていく優先度付きキュー
hp.heappush(q, (0, s))
while q:
nd, np = hp.heappop(q)
if D[np] < nd:
continue
for d, p in graph[np]:
if D[p] > D[np] + d:
D[p] = D[np] + d
B[p] = B[np]
hp.heappush(q, (D[p], p))
elif D[p] == D[np] + d:
B[p] = (B[p] + B[np])%mod
return D, B
sD, sB = dijkstra(s)
tD, tB = dijkstra(t)
distance = sD[t]
ans = 0
for n in range(N):
for d, m in graph[n]:
if sD[n] + d + tD[m] == distance and 2*sD[n] < distance < 2*(sD[n]+d):
ans = (ans + (sB[n]*tB[m])**2)%mod
if sD[n] + tD[n] == distance and 2*sD[n] == distance:
ans = (ans + (sB[n]*tB[n])**2) % mod
ans = (sB[t]*tB[s] - ans) % mod
print(ans)
//...
| Statement
We have a graph with N vertices and M edges, and there are two people on the
graph: Takahashi and Aoki.
The i-th edge connects Vertex U_i and Vertex V_i. The time it takes to
traverse this edge is D_i minutes, regardless of direction and who traverses
the edge (Takahashi or Aoki).
Takahashi departs Vertex S and Aoki departs Vertex T at the same time.
Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the
shortest time possible. Find the number of the pairs of ways for Takahashi and
Aoki to choose their shortest paths such that they never meet (at a vertex or
on an edge) during the travel, modulo 10^9 + 7. | [{"input": "4 4\n 1 3\n 1 2 1\n 2 3 1\n 3 4 1\n 4 1 1", "output": "2\n \n\nThere are two ways to choose shortest paths that satisfies the condition:\n\n * Takahashi chooses the path 1 \\rightarrow 2 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 4 \\rightarrow 1.\n * Takahashi chooses the path 1 \\rightarrow 4 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 2 \\rightarrow 1.\n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "8 13\n 4 2\n 7 3 9\n 6 2 3\n 1 6 4\n 7 6 9\n 3 8 9\n 1 2 2\n 2 8 12\n 8 6 9\n 2 5 5\n 4 2 18\n 5 3 7\n 5 1 515371567\n 4 8 6", "output": "6"}] |
Print the answer.
* * * | s091618972 | Wrong Answer | p03453 | Input is given from Standard Input in the following format:
N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M | def getLeftmostBit(n):
m = 0
while n > 1:
n = n >> 1
m += 1
return m
"""
/* Given the position of previous leftmost
set bit in n (or an upper bound on
leftmost position) returns the new
position of leftmost set bit in n */
"""
def getNextLeftmostBit(n, m):
temp = 1 << m
while n < temp:
temp = temp >> 1
m -= 1
return m
# The main recursive function used by countSetBits()
# def _countSetBits(n, m)
# Returns count of set bits present in
# all numbers from 1 to n
def countSetBits(n):
# Get the position of leftmost set
# bit in n. This will be used as an
# upper bound for next set bit function
m = getLeftmostBit(n)
# Use the position
return _countSetBits(n, m)
def _countSetBits(n, m):
# Base Case: if n is 0, then set bit
# count is 0
if n == 0:
return 0
# /* get position of next leftmost set bit */
m = getNextLeftmostBit(n, m)
# If n is of the form 2^x-1, i.e., if n
# is like 1, 3, 7, 15, 31, .. etc,
# then we are done.
# Since positions are considered starting
# from 0, 1 is added to m
if n == (1 << (m + 1)) - 1:
return (m + 1) * (1 << m)
# update n for next recursive call
n = n - (1 << m)
return (n + 1) + countSetBits(n) + m * (1 << (m - 1))
# Driver code
n = 17
print("Total set bit count is", countSetBits(n))
| Statement
We have a graph with N vertices and M edges, and there are two people on the
graph: Takahashi and Aoki.
The i-th edge connects Vertex U_i and Vertex V_i. The time it takes to
traverse this edge is D_i minutes, regardless of direction and who traverses
the edge (Takahashi or Aoki).
Takahashi departs Vertex S and Aoki departs Vertex T at the same time.
Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the
shortest time possible. Find the number of the pairs of ways for Takahashi and
Aoki to choose their shortest paths such that they never meet (at a vertex or
on an edge) during the travel, modulo 10^9 + 7. | [{"input": "4 4\n 1 3\n 1 2 1\n 2 3 1\n 3 4 1\n 4 1 1", "output": "2\n \n\nThere are two ways to choose shortest paths that satisfies the condition:\n\n * Takahashi chooses the path 1 \\rightarrow 2 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 4 \\rightarrow 1.\n * Takahashi chooses the path 1 \\rightarrow 4 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 2 \\rightarrow 1.\n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "8 13\n 4 2\n 7 3 9\n 6 2 3\n 1 6 4\n 7 6 9\n 3 8 9\n 1 2 2\n 2 8 12\n 8 6 9\n 2 5 5\n 4 2 18\n 5 3 7\n 5 1 515371567\n 4 8 6", "output": "6"}] |
Print the answer.
* * * | s666889390 | Accepted | p03453 | Input is given from Standard Input in the following format:
N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M | from heapq import heappush, heappop
import sys
input = sys.stdin.readline
INF = float("inf")
MOD = 10**9 + 7
def Dijkstra(adjList, vSt):
numV = len(adjList)
numUsed = 0
costs = [INF] * numV
costs[vSt] = 0
nums = [0] * numV
nums[vSt] = 1
PQ = []
heappush(PQ, (0, vSt))
while PQ:
cNow, vNow = heappop(PQ)
if cNow > costs[vNow]:
continue
numUsed += 1
if numUsed == numV:
break
for v2, wt in adjList[vNow]:
c2 = cNow + wt
if c2 < costs[v2]:
costs[v2] = c2
nums[v2] = nums[vNow]
heappush(PQ, (c2, v2))
elif c2 == costs[v2]:
nums[v2] = (nums[v2] + nums[vNow]) % MOD
return (costs, nums)
def solve():
N, M = map(int, input().split())
S, T = map(int, input().split())
S, T = S - 1, T - 1
edges = [tuple(map(int, input().split())) for _ in range(M)]
adjL = [[] for _ in range(N)]
for u, v, d in edges:
adjL[u - 1].append((v - 1, d))
adjL[v - 1].append((u - 1, d))
costSs, numSs = Dijkstra(adjL, S)
costTs, numTs = Dijkstra(adjL, T)
minCost = costSs[T]
ans = (numSs[T] ** 2) % MOD
for v in range(N):
if costSs[v] == costTs[v] == minCost / 2:
k = (numSs[v] * numTs[v]) % MOD
ans -= (k**2) % MOD
ans %= MOD
for u, v, d in edges:
u, v = u - 1, v - 1
if costSs[u] > costSs[v]:
u, v = v, u
if (
costSs[u] < minCost / 2
and costTs[v] < minCost / 2
and costSs[u] + d + costTs[v] == minCost
):
k = (numSs[u] * numTs[v]) % MOD
ans -= (k**2) % MOD
ans %= MOD
print(ans)
solve()
| Statement
We have a graph with N vertices and M edges, and there are two people on the
graph: Takahashi and Aoki.
The i-th edge connects Vertex U_i and Vertex V_i. The time it takes to
traverse this edge is D_i minutes, regardless of direction and who traverses
the edge (Takahashi or Aoki).
Takahashi departs Vertex S and Aoki departs Vertex T at the same time.
Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the
shortest time possible. Find the number of the pairs of ways for Takahashi and
Aoki to choose their shortest paths such that they never meet (at a vertex or
on an edge) during the travel, modulo 10^9 + 7. | [{"input": "4 4\n 1 3\n 1 2 1\n 2 3 1\n 3 4 1\n 4 1 1", "output": "2\n \n\nThere are two ways to choose shortest paths that satisfies the condition:\n\n * Takahashi chooses the path 1 \\rightarrow 2 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 4 \\rightarrow 1.\n * Takahashi chooses the path 1 \\rightarrow 4 \\rightarrow 3, and Aoki chooses the path 3 \\rightarrow 2 \\rightarrow 1.\n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "3 3\n 1 3\n 1 2 1\n 2 3 1\n 3 1 2", "output": "2\n \n\n* * *"}, {"input": "8 13\n 4 2\n 7 3 9\n 6 2 3\n 1 6 4\n 7 6 9\n 3 8 9\n 1 2 2\n 2 8 12\n 8 6 9\n 2 5 5\n 4 2 18\n 5 3 7\n 5 1 515371567\n 4 8 6", "output": "6"}] |
Print the amount of change as an integer.
* * * | s221808588 | Accepted | p02612 | Input is given from Standard Input in the following format:
N | print(abs((-int(input()) % 1000)))
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s774259351 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | input1 = input()
print(1000 - int(input1) % 1000)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s997738399 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | dash = int(input())
otsuri1 = dash % 1000
otsuri2 = 1000 - otsuri1
print(otsuri2)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s795346064 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | print(1000 - int(input()) % 1000 if int(input()) % 1000 else 0)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s681033186 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | print((1000 - (int(input) % 1000)) % 1000)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s172080420 | Accepted | p02612 | Input is given from Standard Input in the following format:
N | n = int(input())
rounded = round(n, -3)
if rounded < n:
rounded += 1000
print(rounded - n)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s014080863 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | n = int(input()) - 1
k = list(str(n))
kk = int(k[0])
print((kk + 1) * 1000 - n - 1)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s530194617 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | def f(y):
if 0 <= y <= 999:
print(1000 - y)
elif 1000 <= y <= 10000:
b = int(y / 100)
c = int(str(b)[1])
g = 9 - c
h = int(y / 10)
i = int(str(h)[2])
if y % 1000 == 0:
print("0")
elif y % 500 == 0 and x % 1000 != 0:
print("500")
elif y % 100 == 0 and x % 500 != 0:
f = 10 - c
print(f * 100)
elif y % 10 == 0 and x % 100 != 0:
j = 10 - i
print(g * 100 + j * 10)
elif y % 10 != 0:
k = int(str(y)[3])
l = 9 - i
m = 10 - k
print(g * 100 + l * 10 + m)
y = input()
x = f(int(y))
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s036896796 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | import copy
H, W, K = map(int, input().split())
L = [[x for x in input()] for i in range(H)]
ans = 0
for i in range(2**H):
for j in range(2**W):
C = copy.deepcopy(L)
for k in range(H):
if i >> k & 1:
C[k] = ["r"] * W
for l in range(W):
if j >> l & 1:
for m in range(H):
C[m][l] = "r"
count = 0
for p in range(H):
for q in range(W):
if C[p][q] == "#":
count += 1
if count == K:
ans += 1
print(ans)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s771236182 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | def get_binaly_arr(num, digit):
return list(bin(num)[2:].zfill(digit))
def get_droped_mat(mat, binaly_arr, direction):
if direction == "H": # 行をdrop!
return [mat[i] for i in range(len(mat)) if binaly_arr[i] != "0"]
elif direction == "W": # 列をdrop!
return [
[arr[i] for i in range(len(arr)) if binaly_arr[i] != "0"] for arr in mat
]
else:
print("INVALID DIRECTION!")
def count_black(mat):
return sum([sum([1 for item in arr if item == "#"]) for arr in mat])
if __name__ == "__main__":
H, W, K = list(map(int, input().split()))
c = []
for i in range(0, H):
c.append(list(input()))
H_pattern_num = 2**H
W_pattern_num = 2**W
count = 0
ite_num = 0
for i in range(0, H_pattern_num):
for j in range(0, W_pattern_num):
# バイナリ配列を生成
H_binaly_arr = get_binaly_arr(i, H)
W_binaly_arr = get_binaly_arr(j, W)
mat_temp = c.copy()
mat_temp = get_droped_mat(c, H_binaly_arr, direction="H")
mat_temp = get_droped_mat(mat_temp, W_binaly_arr, direction="W")
if K == count_black(mat_temp):
count += 1
print(count)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s326428779 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | N = int(input())
a, w, t, r = 0, 0, 0, 0
for i in range(N):
S = input()
if S == "AC":
a += 1
elif S == "WA":
w += 1
elif S == "TLE":
t += 1
else:
r += 1
print("AC × " + str(a))
print("WA × " + str(w))
print("TLE × " + str(t))
print("RE × " + str(r))
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s098162389 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | n,*s=open(0)
for t in['AC','WA','TLE','RE']:print(t,'x',s.count(t+'\n')) | Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s724689755 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | print(abs(int(input()) % 1000 - 1000))
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s398748886 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | test = int(input())
print(1000 - test % 1000)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s704467929 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | n, k = map(int, input().split())
a = list(map(int, input().split()))
s = sorted(a, reverse=True)
mod = 10**9 + 7
ans = 1
# 全部の要素が負の数であり奇数個の場合は大きい順に取ったものが答え
if k % 2 == 1 and s[0] < 0:
for i in range(k):
ans = ans * s[i] % mod
print(ans)
exit() # 終了
l = 0
r = n - 1
# 次の処理のための計算 (kが奇数だった場合)
# この処理の必要性
# 奇数個だった場合三個のうち二つで正の数の最大値を作れば良い
if k % 2 == 1:
ans = ans * s[l]
l += 1
for _ in range(k // 2):
ml = s[l] * s[l + 1]
mr = s[r] * s[r - 1]
if ml > mr:
ans = ans * ml % mod
l += 2
else:
ans = ans * mr % mod
r -= 2
print(ans)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s913212839 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | n = int(input("1から10000までの数字を入力してください"))
div = n % 1000
exc = n - div * 1000
print("exc")
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s532502750 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | N = int(input())
N %= 1000
if N != 1000:
N = 1000 - N
if N == 1000:
N = 0
count = 0
if N >= 500:
N -= 500
count += 1
while N >= 100:
N -= 100
count += 1
while N >= 50:
N -= 50
count += 1
while N >= 10:
N -= 10
count += 1
while N >= 5:
N -= 5
count += 1
while N >= 1:
N -= 1
count += 1
print(count)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s562772174 | Accepted | p02612 | Input is given from Standard Input in the following format:
N | n, x = int(input()), 1000
a = n % x
print(x - a if a else 0)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s510527661 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | n = int(input())
dict = {"AC": 0, "WA": 0, "TLE": 0, "RE": 0}
for _ in range(n):
s = input()
if s in dict:
dict[s] = dict[s] + 1
for k, l in dict.items():
print(k + " x " + str(l))
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s125485271 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | n = int(input())
mai = n // 1000
mai += 1
print(mai * 1000 - n)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s238119716 | Accepted | p02612 | Input is given from Standard Input in the following format:
N | a = 1000 - int(input()) % 1000
print(0 if a == 1000 else a)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s455683775 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | n = str(input())
print(int(n[1:4])) | Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s837698412 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | a = int(input().rstrip())
t = (a // 1000 + 1) * 1000
print(t - a)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s364882175 | Accepted | p02612 | Input is given from Standard Input in the following format:
N | a=int(input())
print(str((1000-(a%1000))%1000)) | Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s140716851 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | X = int(input())
print(X % 1000)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s327326695 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | t = int(input())
t %= 1000
print(1000 - t)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s882245238 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | T = int(input())
a = T // 1000
T = T - 1000 * a
print(1000 - T)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s714582303 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | import numpy as np
import itertools
h = int(input())
w = int(input())
k = int(input())
s = [list(input()) for i in range(h)]
data = np.zeros(h * w).reshape(h, w)
for i in range(h):
for j in range(w):
if s[i][j] == "#":
data[i][j] = 1
ans = 0
## 塗らない場合
if np.sum(data == 1) == k:
ans = 1
## すべて塗った場合
if k == 0:
ans = 1
a = list()
b = list()
for i in range(1, h):
a += list(itertools.combinations(range(h), i))
for i in range(1, w):
b += list(itertools.combinations(range(w), i))
for i in a:
for j in b:
temp = data.copy()
temp[i, :] = 2
temp[:, j] = 2
if np.sum(temp == 1) == k:
ans += 1
for i in a:
temp = data.copy()
temp[i, :] = 2
if np.sum(temp == 1) == k:
ans += 1
for j in b:
temp = data.copy()
temp[:, j] = 2
if np.sum(temp == 1) == k:
ans += 1
print(ans)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s403385045 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | N = int(input())
A_list = list(map(int, input().split()))
A_list.sort(reverse=True)
indx_list = []
for i in range(1, 100000, 2):
indx_list.append(i)
insx_list = []
for j in range(0, 20):
insx_list.extend(indx_list[: 2**j])
mx_cmfrt = 0
reached_list = [A_list[0], A_list[0]]
for j in range(1, len(A_list)):
cmfrt = min(reached_list[insx_list[j - 1] - 1], reached_list[insx_list[j - 1]])
reached_list.insert(insx_list[j - 1], A_list[j])
mx_cmfrt += cmfrt
print(mx_cmfrt)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s158075991 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | N=int(input())
m=int(N/1000)+1
change=1000*m-N
print(change) | Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s077399806 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | -int(input()) % 1000
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s356893128 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | N= input()
x=int(N)
if x<1000:
print(1000-x)
if x > 999:
while True: #0 1 2 4 5が出力される
x -= 1000
if x <= 1000:
break
print(1000-x)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s078081254 | Accepted | p02612 | Input is given from Standard Input in the following format:
N | s = int(input())
for i in range(1,11):
if (1000 * i) >= s:
print((1000 * i) - s)
break | Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s081475789 | Wrong Answer | p02612 | Input is given from Standard Input in the following format:
N | n = int(input())
k = 0
while k <= n:
k += 1000
print(k - n)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s580676566 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
ps = []
ns = []
zs = []
for i in range(n):
if a[i] > 0:
ps.append(a[i])
elif a[i] < 0:
ns.append(a[i])
else:
zs.append(a[i])
if k == 1:
print(max(a))
elif len(ps) + len(ns) < k:
print(0)
elif len(ps) == 0:
ans = 1
if k % 2 == 0:
ns = sorted(ns)
for i in range(k):
ans = ans * ns[i] % mod
elif len(zs) > 0:
print(0)
else:
ns = sorted(ns, reverse=True)
for i in range(k):
ans = ans * ns[i] % mod
print(ans)
else:
ans = 1
ps = sorted(ps, reverse=True)
ns = sorted(ns)
if k % 2 == 0:
pp = []
for i in range(0, min(k, len(ps)), 2):
try:
pp.append(ps[i] * ps[i + 1])
except:
pass
else:
pp = []
ans = ps[0]
for i in range(1, len(ps), 2):
try:
pp.append(ps[i] * ps[i + 1])
except:
pass
np = []
for i in range(0, len(ns), 2):
try:
np.append(ns[i] * ns[i + 1])
except:
pass
pnp = sorted(pp + np, reverse=True)
if len(pnp) >= k // 2:
for i in range(k // 2):
ans = ans * pnp[i] % mod
print(ans)
else:
print(0)
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
Print the amount of change as an integer.
* * * | s377477491 | Runtime Error | p02612 | Input is given from Standard Input in the following format:
N | print(1000 - int(input()) % 1000) % 1000
| Statement
We will buy a product for N yen (the currency of Japan) at a shop.
If we use only 1000-yen bills to pay the price, how much change will we
receive?
Assume we use the minimum number of bills required. | [{"input": "1900", "output": "100\n \n\nWe will use two 1000-yen bills to pay the price and receive 100 yen in change.\n\n* * *"}, {"input": "3000", "output": "0\n \n\nWe can pay the exact price."}] |
For each vertex, print its ID and the distance separated by a space character
in a line respectively. Print in order of vertex IDs. | s136055748 | Accepted | p02242 | In the first line, an integer $n$ denoting the number of vertices in $G$ is
given. In the following $n$ lines, adjacency lists for each vertex $u$ are
respectively given in the following format:
$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$
Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target
vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of
vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge
connecting $u$ and $v_i$ (from $u$ to $v_i$). | n = int(input())
G = [[] for i in range(n)]
for ni in range(n):
u, k, *tmp = [int(i) for i in input().split()]
for ki in range(k):
G[ni].append(tmp[:2])
del tmp[:2]
INF = 100 * 100000
D = [INF for i in range(n)]
D[0] = 0
C = [0 for i in range(n)]
while 0 in C:
U = [INF if C[i] == 1 else D[i] for i in range(n)]
u = U.index(min(U))
for i in range(len(G[u])):
v = G[u][i][0]
if D[u] + G[u][i][1] < D[v]:
D[v] = D[u] + G[u][i][1]
C[u] = 1
for ni in range(n):
print(ni, D[ni])
| Single Source Shortest Path
For a given weighted graph $G = (V, E)$, find the shortest path from a source
to each vertex. For each vertex $u$, print the total weight of edges on the
shortest path from vertex $0$ to $u$. | [{"input": "5\n 0 3 2 3 3 1 1 2\n 1 2 0 2 3 4\n 2 3 0 3 3 1 4 1\n 3 4 2 1 0 1 1 4 4 3\n 4 2 2 1 3 3", "output": "0 0\n 1 2\n 2 2\n 3 1\n 4 3"}] |
For each vertex, print its ID and the distance separated by a space character
in a line respectively. Print in order of vertex IDs. | s455586095 | Accepted | p02242 | In the first line, an integer $n$ denoting the number of vertices in $G$ is
given. In the following $n$ lines, adjacency lists for each vertex $u$ are
respectively given in the following format:
$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$
Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target
vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of
vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge
connecting $u$ and $v_i$ (from $u$ to $v_i$). | # -*- coding: utf-8 -*-
from heapq import heappop, heappush
def dijkstra(edges, start, *, adj_matrix=False, default_value=float("inf")):
"""
Returns best costs to each node from 'start' node in the given graph.
(Single Source Shortest Path - SSSP)
If edges only include costs and destination nodes on possible edges,
adj_matrix should be False(default), and it is generally when this functions works much faster
Note that costs in edges should follow the rules below
1. The cost from a node itself should be 0,
2. If there is no edge between nodes, the cost between them should be default_value
3. The costs can't be negative
# sample input when given as adjacency matrix (adj_matrix=True)
edges = [[0, 2, 5, 4, inf, inf], # node 0
[2, 0, inf, 3, 6, inf], # node 1
[5, inf, 0, 2, inf, 6], # node 2
[4, 3, 2, 0, 2, inf], # node 3
[inf, 6, inf, 2, 0, 4], # node 4
[inf, inf, 6, inf, 4, 0]] # node 5
# sample input when given not as adjacency matrix (adj_matrix=False)
edges = [[(1, 2), (2, 5), (3, 4)], # node 0
[(0, 2), (3, 3), (4, 6)], # node 1
[(0, 5), (3, 2), (5, 6)], # node 2
[(0, 4), (1, 3), (2, 2), (4, 2)], # node 3
[(1, 6), (3, 2), (5, 4)], # node 4
[(2, 6), (4, 4)]] # node 5
"""
n = len(edges)
inf = float("inf")
costs = [inf] * n
costs[start] = 0
pq, rem = [(0, start)], n - 1
while pq and rem:
tmp_cost, tmp_node = heappop(pq)
if costs[tmp_node] < tmp_cost:
continue
rem -= 1
nxt_edges = (
(
(node, cost)
for node, cost in enumerate(edges[tmp_node])
if cost != default_value
)
if adj_matrix
else edges[tmp_node]
)
for nxt_node, nxt_cost in nxt_edges:
new_cost = tmp_cost + nxt_cost
if costs[nxt_node] > new_cost:
costs[nxt_node] = new_cost
heappush(pq, (new_cost, nxt_node))
return costs
def dijkstra_route(
edges, start, goal, *, adj_matrix=False, default_value=float("inf"), verbose=False
):
"""
Trys to find the best route to the 'goal' from the 'start'
"""
n = len(edges)
inf = float("inf")
costs = [inf] * n
costs[start] = 0
pq, rem = [(0, start)], n - 1
prevs = [-1 for _ in [None] * n]
while pq and rem:
tmp_cost, tmp_node = heappop(pq)
if costs[tmp_node] < tmp_cost:
continue
rem -= 1
nxt_edges = (
(
(node, cost)
for node, cost in enumerate(edges[tmp_node])
if cost != default_value
)
if adj_matrix
else edges[tmp_node]
)
for nxt_node, nxt_cost in nxt_edges:
new_cost = tmp_cost + nxt_cost
if costs[nxt_node] > new_cost:
costs[nxt_node] = new_cost
heappush(pq, (new_cost, nxt_node))
prevs[nxt_node] = tmp_node
min_route = []
prev = goal
cnt = 0
while prev != start:
min_route.append(prev)
prev = prevs[prev]
cnt += 1
if prev == -1 or cnt > n:
raise NoRouteError(
"There is no possible route in this graph \nedges: {} \nstart: {} \ngoal: {}".format(
edges, start, goal
)
)
else:
min_route.append(prev)
min_route = min_route[::-1]
min_cost = costs[goal]
if verbose:
print("---route---")
for node in min_route[:-1]:
print("{} -> ".format(node), end="")
else:
print(min_route[-1])
print("---distance---")
print(min_cost)
return costs, min_route
class NoRouteError(Exception):
pass
if __name__ == "__main__":
"""
an example of using this function
AIZU ONLINE JUDGE - ALDS_1_12_B
"""
n = int(input())
edges = [[] for i in range(n)]
for i in range(n):
i, k, *kedges = map(int, input().split())
for edge in zip(kedges[::2], kedges[1::2]):
edges[i].append(edge)
for i, cost in enumerate(dijkstra(edges, 0)):
print(i, cost)
| Single Source Shortest Path
For a given weighted graph $G = (V, E)$, find the shortest path from a source
to each vertex. For each vertex $u$, print the total weight of edges on the
shortest path from vertex $0$ to $u$. | [{"input": "5\n 0 3 2 3 3 1 1 2\n 1 2 0 2 3 4\n 2 3 0 3 3 1 4 1\n 3 4 2 1 0 1 1 4 4 3\n 4 2 2 1 3 3", "output": "0 0\n 1 2\n 2 2\n 3 1\n 4 3"}] |
For each vertex, print its ID and the distance separated by a space character
in a line respectively. Print in order of vertex IDs. | s225138759 | Accepted | p02242 | In the first line, an integer $n$ denoting the number of vertices in $G$ is
given. In the following $n$ lines, adjacency lists for each vertex $u$ are
respectively given in the following format:
$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$
Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target
vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of
vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge
connecting $u$ and $v_i$ (from $u$ to $v_i$). | import sys
readline = sys.stdin.readline
write = sys.stdout.write
N = int(readline())
G = [None] * N
W = 0
for i in range(N):
j, k, *V = map(int, readline().split())
G[i] = [(v, c) for v, c in zip(V[::2], V[1::2])]
if V:
W = max(W, max(V[1::2]))
def dial(N, G, W, s):
B = [-1] * (N * W + 1)
L = [-1] * (N * W + 1)
(*prv,) = range(-1, N - 1)
(*nxt,) = range(1, N + 1)
nxt[-1] = -1
prv[s + 1] = s - 1
nxt[s - 1] = s + 1 if s < N - 1 else -1
prv[s] = nxt[s] = -1
B[0] = L[0] = s
B[N * W] = +(s == 0)
L[N * W] = N - 1 if s < N - 1 else N - 2
dist = [N * W] * N
dist[s] = 0
for w in range(N * W):
v = B[w]
while v != -1:
for x, c in G[v]:
e = w + c
if e < dist[x]:
d = dist[x]
dist[x] = e
p = prv[x]
n = nxt[x]
if p != -1:
nxt[p] = n
else:
B[d] = n
if n != -1:
prv[n] = p
else:
L[d] = p
l = L[e]
if l != -1:
nxt[l] = x
else:
B[e] = x
prv[x] = l
nxt[x] = -1
L[e] = x
v = nxt[v]
return dist
dist = dial(N, G, W, 0)
ans = []
for i in range(N):
ans.append("%d %d\n" % (i, dist[i]))
sys.stdout.writelines(ans)
| Single Source Shortest Path
For a given weighted graph $G = (V, E)$, find the shortest path from a source
to each vertex. For each vertex $u$, print the total weight of edges on the
shortest path from vertex $0$ to $u$. | [{"input": "5\n 0 3 2 3 3 1 1 2\n 1 2 0 2 3 4\n 2 3 0 3 3 1 4 1\n 3 4 2 1 0 1 1 4 4 3\n 4 2 2 1 3 3", "output": "0 0\n 1 2\n 2 2\n 3 1\n 4 3"}] |
For each vertex, print its ID and the distance separated by a space character
in a line respectively. Print in order of vertex IDs. | s722291383 | Wrong Answer | p02242 | In the first line, an integer $n$ denoting the number of vertices in $G$ is
given. In the following $n$ lines, adjacency lists for each vertex $u$ are
respectively given in the following format:
$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$
Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target
vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of
vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge
connecting $u$ and $v_i$ (from $u$ to $v_i$). | n = int(input())
from collections import defaultdict
G = defaultdict(list)
for i in range(n):
i = [int(i) for i in input().split()]
k = i[1]
G[i[0]] = list(zip(i[2::2], i[3::2]))
def mind(Q, d):
c = float("inf")
ret = 0
for i, j in enumerate(d):
if c > j and i in Q:
c = j
ret = i
return ret
def dijkstra(G):
d = [0] + [float("inf")] * (n - 1)
Q = set(range(n))
while len(Q) != 0:
u = mind(Q, d)
print(u)
Q = Q - set([u])
for v, c in G[u]:
if d[v] > d[u] + c:
d[v] = d[u] + c
return d
for i, j in enumerate(dijkstra(G)):
print(i, j)
| Single Source Shortest Path
For a given weighted graph $G = (V, E)$, find the shortest path from a source
to each vertex. For each vertex $u$, print the total weight of edges on the
shortest path from vertex $0$ to $u$. | [{"input": "5\n 0 3 2 3 3 1 1 2\n 1 2 0 2 3 4\n 2 3 0 3 3 1 4 1\n 3 4 2 1 0 1 1 4 4 3\n 4 2 2 1 3 3", "output": "0 0\n 1 2\n 2 2\n 3 1\n 4 3"}] |
For each vertex, print its ID and the distance separated by a space character
in a line respectively. Print in order of vertex IDs. | s641165514 | Accepted | p02242 | In the first line, an integer $n$ denoting the number of vertices in $G$ is
given. In the following $n$ lines, adjacency lists for each vertex $u$ are
respectively given in the following format:
$u$ $k$ $v_1$ $c_1$ $v_2$ $c_2$ ... $v_k$ $c_k$
Vertices in $G$ are named with IDs $0, 1, ..., n-1$. $u$ is ID of the target
vertex and $k$ denotes its degree. $v_i (i = 1, 2, ... k)$ denote IDs of
vertices adjacent to $u$ and $c_i$ denotes the weight of a directed edge
connecting $u$ and $v_i$ (from $u$ to $v_i$). | import sys
import heapq
input = sys.stdin.readline
INF = 2 << 28
def dijkstra(G, start, n): # グラフ,初期頂点,頂点数
dist = [INF] * n
dist[start] = 0
used = [False] * n
used[0] = True
q = []
for v in G[start]:
heapq.heappush(q, v)
while len(q) > 0:
now = heapq.heappop(q)
if used[now[1]]:
continue
v = now[1]
dist[v] = now[0]
used[v] = True
for e in G[v]:
if not used[e[1]]:
heapq.heappush(q, (e[0] + dist[v], e[1]))
return dist
N = int(input().rstrip())
g = [[] for _ in range(N)]
for _ in range(N):
u, k, *c = map(int, input().split())
for j in range(0, k * 2, 2):
g[u].append((c[j + 1], c[j]))
ans = dijkstra(g, 0, N)
for i, x in enumerate(ans):
print(i, x)
| Single Source Shortest Path
For a given weighted graph $G = (V, E)$, find the shortest path from a source
to each vertex. For each vertex $u$, print the total weight of edges on the
shortest path from vertex $0$ to $u$. | [{"input": "5\n 0 3 2 3 3 1 1 2\n 1 2 0 2 3 4\n 2 3 0 3 3 1 4 1\n 3 4 2 1 0 1 1 4 4 3\n 4 2 2 1 3 3", "output": "0 0\n 1 2\n 2 2\n 3 1\n 4 3"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s045706452 | Accepted | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n, t = int(input()), list(map(int, input().split()))
T = sum(t)
print(
*[
T - t[i - 1] + j
for i, j in [list(map(int, input().split())) for _ in range(int(input()))]
],
sep="\n"
)
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s333479153 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | I = input
I()
t = [int(i) for i in I().S()]
for i in " " * int(input()):
p, x = map(int, I().split())
print(sum(t) + x - t[p - 1])
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s397393900 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n=int(input())
t=list(map(int,input().split()))
m=int(input())
for i in range(m):
p,x=map(int,input().split())
y=t[p-1]
t[p-1]=x:
print(sum(t))
t[p-1]=y | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s384749603 | Wrong Answer | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | 6
9
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s787014098 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | # -*- coding: utf-8 -*-
#############
# Libraries #
#############
import sys
input = sys.stdin.readline
import math
# from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache
#############
# Constants #
#############
MOD = 10**9 + 7
INF = float("inf")
#############
# Functions #
#############
######INPUT######
def I():
return int(input().strip())
def S():
return input().strip()
def IL():
return list(map(int, input().split()))
def SL():
return list(map(str, input().split()))
def ILs(n):
return list(int(input()) for _ in range(n))
def SLs(n):
return list(input().strip() for _ in range(n))
def ILL(n):
return [list(map(int, input().split())) for _ in range(n)]
def SLL(n):
return [list(map(str, input().split())) for _ in range(n)]
######OUTPUT######
def P(arg):
print(arg)
return
def Y():
print("Yes")
return
def N():
print("No")
return
def E():
exit()
def PE(arg):
print(arg)
exit()
def YE():
print("Yes")
exit()
def NE():
print("No")
exit()
#####Shorten#####
def DD(arg):
return defaultdict(arg)
#####Inverse#####
def inv(n):
return pow(n, MOD - 2, MOD)
######Combination######
kaijo_memo = []
def kaijo(n):
if len(kaijo_memo) > n:
return kaijo_memo[n]
if len(kaijo_memo) == 0:
kaijo_memo.append(1)
while len(kaijo_memo) <= n:
kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
return kaijo_memo[n]
gyaku_kaijo_memo = []
def gyaku_kaijo(n):
if len(gyaku_kaijo_memo) > n:
return gyaku_kaijo_memo[n]
if len(gyaku_kaijo_memo) == 0:
gyaku_kaijo_memo.append(1)
while len(gyaku_kaijo_memo) <= n:
gyaku_kaijo_memo.append(
gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo), MOD - 2, MOD) % MOD
)
return gyaku_kaijo_memo[n]
def nCr(n, r):
if n == r:
return 1
if n < r or r < 0:
return 0
ret = 1
ret = ret * kaijo(n) % MOD
ret = ret * gyaku_kaijo(r) % MOD
ret = ret * gyaku_kaijo(n - r) % MOD
return ret
######Factorization######
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-(n**0.5) // 1)) + 1):
if temp % i == 0:
cnt = 0
while temp % i == 0:
cnt += 1
temp //= i
arr.append([i, cnt])
if temp != 1:
arr.append([temp, 1])
if arr == []:
arr.append([n, 1])
return arr
#####MakeDivisors######
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n // i)
return divisors
#####LCM#####
def lcm(a, b):
return a * b // gcd(a, b)
#####BitCount#####
def count_bit(n):
count = 0
while n:
n &= n - 1
count += 1
return count
#####ChangeBase#####
def base_10_to_n(X, n):
if X // n:
return base_10_to_n(X // n, n) + [X % n]
return [X % n]
def base_n_to_10(X, n):
return sum(int(str(X)[-i]) * n**i for i in range(len(str(X))))
#####IntLog#####
def int_log(n, a):
count = 0
while n >= a:
n //= a
count += 1
return count
#############
# Main Code #
#############
N = I()
T = IL()
s = sum(T)
M = IL()
for m, t in range(M):
P(s - T[m] + t)
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s900196934 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n = int(input())
t = list(map(int, input().split())
t = [0] + t
maxRed = 0
for _ in range(int(input())):
p, x = map(int, input().split())
maxRed = max(maxRed, t[p]-x)
print(sum(t)-maxRed) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s472399206 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n=int(input())
l=[0]+list(map(int,input().split()))\
ans=sum(l)
for i in range(int(input())):
a,s=map(int,input().split())
print(ans-l[a]+s) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s297339938 | Accepted | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | num_of_problems = int(input())
times = input().split()
for i in range(num_of_problems):
times[i] = int(times[i])
num_of_drinks = int(input())
changes = []
for i in range(num_of_drinks):
l = input().split()
l[0] = int(l[0])
l[1] = int(l[1])
changes.append(l)
for i in changes:
diff = times[i[0] - 1] - i[1]
times[i[0] - 1] = i[1]
print(sum(times))
times[i[0] - 1] += diff
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s874754922 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | import math
from collections import defaultdict
import itertools
N = int(input())
town = [[int(i) for i in input().split(" ")] for _ in range(N)]
dist = 0
c = 0
for v in itertools.permutations(town, len(town)):
c += 1
sx = v[0][0]
sy = v[0][1]
for i in v[1::]:
dist += math.sqrt((sx - i[0]) ** 2 + (sy - i[1]) ** 2)
sx, sy = i[0], i[1]
print(dist / c)
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s875337201 | Accepted | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = input()
t = input().split()
T = [int(i) for i in t]
S = sum(T)
M = int(input())
for i in range(0, M):
N = input().split()
print(S - T[int(N[0]) - 1] + int(N[1]))
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s032647280 | Accepted | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | x = int(input())
first = list(map(int, input().split()))
drink_count = int(input())
count = 0
Total = sum(first)
drink = [list(map(int, input().split())) for i in range(drink_count)]
for T in range(drink_count):
change_num = drink[count][0]
score = first[change_num - 1] - drink[count][1]
print(Total - score)
count += 1
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s278170432 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = int(input())
T = input().split()
M = int(input())
sumT = 0
p = []
x = []
for k in range(N)):
sumT += int(T[k])
for i in range(M):
P, X = map(int, input().split())
p.append(P)
x.append(X)
for i in range(len(T)):
print(sumT - int(T[i]) + int(x[i])) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s807417704 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N=int(input())
T_N=int(input().split())
list_time=[]
for i in range(N)
list_time.append(T_N[i])
sum=sum(list_time)
M=int(input())
for i in range(M):
p,x=map(int,input.split())
ans=sum+(x-list_time[p-1])
print(ans) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s108222615 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = int(input())
t = input().split()
T = []
for i in range(N):
T.append(int(t[i]))
M = int(input())
time = []
total = sum(T)
for i in range(M):
p,x = (int(i) for i in input().split())
time.append(total - T[p] + x)
print(time) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s199543041 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = int(input())
t = input().split()
T = []
for i in range(N):
T.append(int(t[i]))
M = int(input())
time = []
total = sum(T)
for k in range(M):
p,x = (int(i) for i in input().split())
time.append(total - T[p] + x)
print(time) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s821210610 | Wrong Answer | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | num = int(input())
lis = [int(m) for m in input().split()]
drinknum = int(input())
li = [[int(n) for n in input().split()] for d in range(drinknum)]
for i in li:
l = lis
l[i[0] - 1] = i[1]
print(sum(lis))
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s304671384 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | import numpy as np
N = int(input())
T = list(map(int, input().split()))
M = int(input())
for i in range(M):
P, X = map(int, input().split())
T_new = T
T_new[P = X
print(np.array(T_new).sum()) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s469200635 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | print(sum(T[: P - 1]) + X + sum(T[P:]))
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s240777750 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | #ABC050.B
N = int(input())
T = int(input().split())
S = sum(T)
M = int(input())
for _ in range(M):
p,x = map(int,input().split())
ans = S - T[] + x[]
print(ans) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s519246089 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = int(input())
T = list(int(input().split()))
S = sum(T)
M = int(input())
for _ in range(M):
p,x = map(int,input().split())
ans = S - T[] + x
print(ans) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s464251729 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n = int(input())
t = list(map(int, (input().split()))
time = sum(t)
m = int(input())
for i in range(m)
p, x = map(int, input()/split())
pritn(time - t[p+1] + x) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s810750281 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | #include <bits/stdc++.h>
using namespace std;
// Define
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T> using pvector = vector<pair<T, T>>;
template <class T>
using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
constexpr const ll dx[4] = {1, 0, -1, 0};
constexpr const ll dy[4] = {0, 1, 0, -1};
constexpr const ll MOD = 1e9 + 7;
constexpr const ll mod = 998244353;
constexpr const ll INF = 1LL << 60;
constexpr const ll inf = 1 << 30;
constexpr const char rt = '\n';
constexpr const char sp = ' ';
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define elif else if
#define all(a, v, ...) \
([&](decltype((v)) w) { return (a)(begin(w), end(w), ##__VA_ARGS__); })(v)
#define rall(a, v, ...) \
([&](decltype((v)) w) { return (a)(rbegin(w), rend(w), ##__VA_ARGS__); })(v)
#define fi first
#define se second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// Debug
#define debug(...) \
{ \
cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \
for (auto &&X : {__VA_ARGS__}) cerr << "[" << X << "] "; \
cerr << rt; \
}
#define dump(a, h, w) \
{ \
cerr << __LINE__ << ": " << #a << " = [" << rt; \
rep(_i, h) { \
rep(_j, w) cerr << a[_i][_j] << sp; \
cerr << rt; \
} \
cerr << "]" << rt; \
}
#define vdump(a, n) \
{ \
cerr << __LINE__ << ": " << #a << " = ["; \
rep(_i, n) if (_i) cerr << sp << a[_i]; \
else cerr << a[_i]; \
cerr << "]" << rt; \
}
// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i)
#define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i)
#define each(i, a) for (auto &&i : a)
// Stream
#define fout(n) cout << fixed << setprecision(n)
struct io {
io() { cin.tie(nullptr), ios::sync_with_stdio(false); }
} io;
// Speed
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// Math
inline constexpr ll gcd(const ll a, const ll b) {
return b ? gcd(b, a % b) : a;
}
inline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; }
inline constexpr ll modulo(const ll n, const ll m = MOD) {
ll k = n % m;
return k + m * (k < 0);
}
inline constexpr ll chmod(ll &n, const ll m = MOD) {
n %= m;
return n += m * (n < 0);
}
inline constexpr ll mpow(ll a, ll n, const ll m = MOD) {
ll r = 1;
rep(i, 64) {
if (n & (1LL << i)) r *= a;
chmod(r, m);
a *= a;
chmod(a, m);
}
return r;
}
inline ll inv(const ll n, const ll m = MOD) {
ll a = n, b = m, x = 1, y = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
x -= t * y;
swap(x, y);
}
return modulo(x, m);
}
signed main() {
ll N;
cin >> N;
ll A[N];
rep(i, N) cin >> A[i];
ll M, S = accumulate(A, A + N, 0LL);
cin >> M;
rep(i, M) {
ll C, D;
cin >> C >> D;
cout << S - A[C - 1] + D << rt;
}
}
// -g -D_GLIBCXX_DEBUG -fsanitize=undefined
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s285217470 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M |
N = int(input())
T = list(map(int, input().split()))
M = int(input())
data = []
for x in range(M):
p,x = map(int, input().split())
p -= 1
data.append([p, x])
result = 2 ** 60
for p,x in data:
res = 0
for i,t in enumerate(T):
if (i == p):
res += x
else:
res += t
print (res) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s032637498 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = int(input())
Problems = map(int, input().split())
M = int(input())
arr = []
for i in range(M):
P, T = map(int, input().split())
sum = 0
for j in range(N):
if j != P:
sum += Problems[j]
else:
sum += T
arr[i] = sum
for num in arr:
print(num) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s533069247 | Wrong Answer | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = int(input()) # 問題数
tlst = list(map(int, input().split()))
initlst = tlst.copy() # 1問にかかる時間
M = int(input()) # ドリンクの数
for _ in range(M):
P, x = map(int, input().split()) # 何個目が時間変わる
tlst[P - 1] = x
print(sum(tlst))
tlst = initlst
# print(tlst)
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s094979515 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | package main
import (
"bufio"
"fmt"
"os"
"reflect"
"strconv"
)
var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
func main() {
sc.Split(bufio.ScanWords)
defer out.Flush() // !!!!coution!!!! you must use Fprint(out, ) not Print()
/* --- code ---*/
n := nextInt()
ts := nextInts(n)
m := nextInt()
for i := 0; i < m; i++ {
p, x := nextInt()-1, nextInt()
ans := 0
for j := 0; j < n; j++ {
if j != p {
ans += ts[j]
} else {
ans += x
}
}
fmt.Fprint(out, ans, "\n")
}
}
func next() string {
sc.Scan()
return sc.Text()
}
func nextInt() int {
a, _ := strconv.Atoi(next())
return a
}
func nextFloat64() float64 {
a, _ := strconv.ParseFloat(next(), 64)
return a
}
func nextInts(n int) []int {
ret := make([]int, n)
for i := 0; i < n; i++ {
ret[i] = nextInt()
}
return ret
}
func nextFloats(n int) []float64 {
ret := make([]float64, n)
for i := 0; i < n; i++ {
ret[i] = nextFloat64()
}
return ret
}
func nextStrings(n int) []string {
ret := make([]string, n)
for i := 0; i < n; i++ {
ret[i] = next()
}
return ret
}
func PrintOut(src interface{}, joinner string) {
switch reflect.TypeOf(src).Kind() {
case reflect.Slice:
s := reflect.ValueOf(src)
for idx := 0; idx < s.Len(); idx++ {
fmt.Fprintf(out, "%v", s.Index(idx))
if idx < s.Len()-1 {
fmt.Fprintf(out, "%s", joinner)
} else {
fmt.Fprintln(out)
}
}
default:
fmt.Fprintln(out, "fuck")
}
}
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s434469852 | Accepted | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n1 = int(input())
li = list(map(int, input().split()))
n2 = int(input())
for i in range(n2):
p, x = map(int, input().split())
a = li[p - 1]
li[p - 1] = x
print(sum(li))
li[p - 1] = a
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s880892720 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n = int(input())
ts = list(map(int, input(.split())))
su = sum(ts)
m = int(input())
for _ in range(m):
p, x = list(map(int, input(.split())))
ans = su - ts[p] + x
print(ans) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s580159982 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | n = int(input())
ts = list(map(int, input().split())))
su = sum(ts)
m = int(input())
for _ in range(m):
p, x = list(map(int, input().split())))
ans = su - ts[p] + x
print(ans) | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s134267593 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | # ABC050.B
N = int(input())
T = list(map(int, input().split() for _ in range(N)))
S = sum(T)
M = int(input())
for _ in range(M):
a = S
p, x = map(int, input().split())
a -= T[p - 1]
a += x
print(a)
| Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
For each drink, calculate how many seconds it takes Joisino to solve all the
problems if she takes that drink, and print the results, one per line.
* * * | s956211100 | Runtime Error | p03845 | The input is given from Standard Input in the following format:
N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M | N = int(input())
T = list(map(int, input().split()))
M = int(input())
P, X = [], []
for i in range(M):
pi, xi = map(int, input().split)
P.append(pi)
X.append(xi)
count = 0
for i in T:
count += T[i]
for i in X:
count += X[i]
count -= T[i]
print(count | Statement
Joisino is about to compete in the final round of a certain programming
competition. In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).
Also, there are M kinds of drinks offered to the contestants, numbered 1
through M. If Joisino takes drink i(1≦i≦M), her brain will be stimulated and
the time it takes for her to solve problem P_i will become X_i seconds. It
does not affect the time to solve the other problems.
A contestant is allowed to take exactly one of the drinks before the start of
the contest. For each drink, Joisino wants to know how many seconds it takes
her to solve all the problems if she takes that drink. Here, assume that the
time it takes her to solve all the problems is equal to the sum of the time it
takes for her to solve individual problems. Your task is to write a program to
calculate it instead of her. | [{"input": "3\n 2 1 4\n 2\n 1 1\n 2 3", "output": "6\n 9\n \n\nIf Joisino takes drink 1, the time it takes her to solve each problem will be\n1, 1 and 4 seconds, respectively, totaling 6 seconds.\n\nIf Joisino takes drink 2, the time it takes her to solve each problem will be\n2, 3 and 4 seconds, respectively, totaling 9 seconds.\n\n* * *"}, {"input": "5\n 7 2 3 8 5\n 3\n 4 2\n 1 7\n 4 13", "output": "19\n 25\n 30"}] |
Print the number of non-empty (contiguous) substrings of S that are divisible
by P when regarded as an integer written in base ten.
* * * | s416534903 | Wrong Answer | p02757 | Input is given from Standard Input in the following format:
N P
S | #!/usr/bin/env python
class subset:
def __init__(self, string):
self.string = string
self.len = len(string)
self.split = int((self.len + 1) / 2)
if self.len == 1:
self.half = [self]
else:
self.half = [subset(string[: self.split]), subset(string[self.split :])]
self.msum = None
self.msumr = None
def get_msum(self):
if self.msum is not None:
return self.msum
if self.len == 1:
return [int(self.string) % p]
else:
self.msum = self.half[1].get_msum()
add = self.msum[-1]
self.msum.extend(
[(v * dm[self.half[1].len] + add) % p for v in self.half[0].get_msum()]
)
# self.msum = [
# (v * dm[self.half[1].len] + self.half[1].get_msum()[-1]) % p
# for v in self.half[0].get_msum()
# ]
# self.msum.extend(self.half[1].get_msum())
return self.msum
def get_msumr(self):
if self.msumr is not None:
return self.msumr
if self.len == 1:
return [int(self.string) % p]
else:
self.msumr = self.half[0].get_msumr()
add = self.msumr[-1]
self.msumr.extend(
[
(add * dm[i + 1] + v) % p
for i, v in enumerate(self.half[1].get_msumr())
]
)
return self.msumr
def get_primnum(self):
global p
if self.len == 1:
if int(self.string) % p == 0:
return 1
else:
return 0
c = self.half[0].get_primnum() + self.half[1].get_primnum()
rc = [0] * p
for a in self.half[0].get_msum():
rc[a] += 1
# print(self.string, rc)
for i, b in enumerate(self.half[1].get_msumr()):
idx = (-b * dmi[i + 1]) % p
# print(i, b, idx)
c += rc[idx]
return c
def mypow(n, m, p):
if m == 0:
return 1
elif m % 2 == 0:
return (mypow(n, int(m / 2), p) ** 2) % p
elif m % 2 == 1:
return (mypow(n, int(m / 2), p) ** 2 * n) % p
n, p = (int(val) for val in input().split())
string = input()
m = []
dm = []
dmi = []
for i in range(n):
if i == 0:
dm.append(1)
else:
dm.append((dm[i - 1] * 10) % p)
dmi.append(mypow(dm[i], p - 2, p))
ss = subset(string)
print(ss.get_primnum())
| Statement
Takahashi has a string S of length N consisting of digits from `0` through
`9`.
He loves the prime number P. He wants to know how many non-empty (contiguous)
substrings of S \- there are N \times (N + 1) / 2 of them - are divisible by P
when regarded as integers written in base ten.
Here substrings starting with a `0` also count, and substrings originated from
different positions in S are distinguished, even if they are equal as strings
or integers.
Compute this count to help Takahashi. | [{"input": "4 3\n 3543", "output": "6\n \n\nHere S = `3543`. There are ten non-empty (contiguous) substrings of S:\n\n * `3`: divisible by 3.\n\n * `35`: not divisible by 3.\n\n * `354`: divisible by 3.\n\n * `3543`: divisible by 3.\n\n * `5`: not divisible by 3.\n\n * `54`: divisible by 3.\n\n * `543`: divisible by 3.\n\n * `4`: not divisible by 3.\n\n * `43`: not divisible by 3.\n\n * `3`: divisible by 3.\n\nSix of these are divisible by 3, so print 6.\n\n* * *"}, {"input": "4 2\n 2020", "output": "10\n \n\nHere S = `2020`. There are ten non-empty (contiguous) substrings of S, all of\nwhich are divisible by 2, so print 10.\n\nNote that substrings beginning with a `0` also count.\n\n* * *"}, {"input": "20 11\n 33883322005544116655", "output": "68"}] |
Print the number of non-empty (contiguous) substrings of S that are divisible
by P when regarded as an integer written in base ten.
* * * | s480498751 | Runtime Error | p02757 | Input is given from Standard Input in the following format:
N P
S | a, b = [int(c) for c in input().split(" ")]
S = input()
count = 0
for i in range(1, a):
for j in range(0, a):
if j + i <= a + 1:
subStr = S[j : j + i]
print(subStr)
if len(subStr) == i:
if int(subStr) % b == 0:
count += 1
if int(S) % b == 0:
count += 1
print(count)
| Statement
Takahashi has a string S of length N consisting of digits from `0` through
`9`.
He loves the prime number P. He wants to know how many non-empty (contiguous)
substrings of S \- there are N \times (N + 1) / 2 of them - are divisible by P
when regarded as integers written in base ten.
Here substrings starting with a `0` also count, and substrings originated from
different positions in S are distinguished, even if they are equal as strings
or integers.
Compute this count to help Takahashi. | [{"input": "4 3\n 3543", "output": "6\n \n\nHere S = `3543`. There are ten non-empty (contiguous) substrings of S:\n\n * `3`: divisible by 3.\n\n * `35`: not divisible by 3.\n\n * `354`: divisible by 3.\n\n * `3543`: divisible by 3.\n\n * `5`: not divisible by 3.\n\n * `54`: divisible by 3.\n\n * `543`: divisible by 3.\n\n * `4`: not divisible by 3.\n\n * `43`: not divisible by 3.\n\n * `3`: divisible by 3.\n\nSix of these are divisible by 3, so print 6.\n\n* * *"}, {"input": "4 2\n 2020", "output": "10\n \n\nHere S = `2020`. There are ten non-empty (contiguous) substrings of S, all of\nwhich are divisible by 2, so print 10.\n\nNote that substrings beginning with a `0` also count.\n\n* * *"}, {"input": "20 11\n 33883322005544116655", "output": "68"}] |
Print the number of non-empty (contiguous) substrings of S that are divisible
by P when regarded as an integer written in base ten.
* * * | s524811490 | Accepted | p02757 | Input is given from Standard Input in the following format:
N P
S | from collections import Counter
N, P = list(map(int, input().split()))
S = [int(c) for c in input().rstrip()]
if P in [2, 5]:
total = 0
for i, x in enumerate(S):
if x % P == 0:
# If P == 2
# all substring ending here with last digit even
# If P == 5
# all substring ending here with last digit 5 or 0
total += i + 1
print(total)
else:
# Note P is relatively prime with 10
# If S is '1234'
# Want suffixes:
# s[4] = 1234
# s[3] = 234
# s[2] = 34
# s[1] = 4
# s[0] = 0
#
# Then s[j] - s[i] gives the value of the number int(S[-j:-i]) * 10**(N -i)
# Since P doesn't divide 10, if P divides s[j] - s[i] it also divides the number
CHECK = False
suf = [0]
power = 1
for x in reversed(S):
suf.append(suf[-1] + x * power)
power *= 10
if not CHECK:
# Don't mod when checking
suf[-1] %= P
power %= P
if CHECK:
# Check
N = len(S)
for i in range(N):
for j in range(i + 1, N + 1):
print(
i,
j,
int("".join(map(str, S[N - j : N - i]))) * (10**i),
suf[j] - suf[i],
)
print(suf)
# Count number of s[j] - s[i] pairs that are 0 mod P
total = 0
counts = Counter()
for x in suf:
x = x % P
total += counts[x]
counts[x] += 1
print(total)
| Statement
Takahashi has a string S of length N consisting of digits from `0` through
`9`.
He loves the prime number P. He wants to know how many non-empty (contiguous)
substrings of S \- there are N \times (N + 1) / 2 of them - are divisible by P
when regarded as integers written in base ten.
Here substrings starting with a `0` also count, and substrings originated from
different positions in S are distinguished, even if they are equal as strings
or integers.
Compute this count to help Takahashi. | [{"input": "4 3\n 3543", "output": "6\n \n\nHere S = `3543`. There are ten non-empty (contiguous) substrings of S:\n\n * `3`: divisible by 3.\n\n * `35`: not divisible by 3.\n\n * `354`: divisible by 3.\n\n * `3543`: divisible by 3.\n\n * `5`: not divisible by 3.\n\n * `54`: divisible by 3.\n\n * `543`: divisible by 3.\n\n * `4`: not divisible by 3.\n\n * `43`: not divisible by 3.\n\n * `3`: divisible by 3.\n\nSix of these are divisible by 3, so print 6.\n\n* * *"}, {"input": "4 2\n 2020", "output": "10\n \n\nHere S = `2020`. There are ten non-empty (contiguous) substrings of S, all of\nwhich are divisible by 2, so print 10.\n\nNote that substrings beginning with a `0` also count.\n\n* * *"}, {"input": "20 11\n 33883322005544116655", "output": "68"}] |
Print the minimum integer m that satisfies the condition.
* * * | s520977416 | Accepted | p03146 | Input is given from Standard Input in the following format:
s | #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def f(n: int) -> int:
if n % 2 == 0:
return n // 2
return 3 * n + 1
def main() -> None:
s = int(input())
seen = set()
seen.add(s)
a = [s]
m = 2
while True:
latest = f(a[-1])
if latest in seen:
print(m)
exit(0)
m += 1
a.append(latest)
seen.add(latest)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self, **kwargs):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
| Statement
A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows:
* The first term s is given as input.
* Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.
* a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.
Find the minimum integer m that satisfies the following condition:
* There exists an integer n such that a_m = a_n (m > n). | [{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}] |
Print the minimum integer m that satisfies the condition.
* * * | s544236479 | Runtime Error | p03146 | Input is given from Standard Input in the following format:
s | s = int(input())
a = [s]
def f(n):
if n % 2 == 0:
return(n//2)
else:
return(3*n+1)
for i in range(1000001):
if f(a[i]) in a:
print(i+1)
break
a.append(f(a[i])
| Statement
A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows:
* The first term s is given as input.
* Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.
* a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.
Find the minimum integer m that satisfies the following condition:
* There exists an integer n such that a_m = a_n (m > n). | [{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}] |
Print the minimum integer m that satisfies the condition.
* * * | s511476341 | Accepted | p03146 | Input is given from Standard Input in the following format:
s | import math
from collections import deque
from collections import defaultdict
# 自作関数群
def readInt():
return int(input())
def readInts():
return list(map(int, input().split()))
def readChar():
return input()
def readChars():
return input().split()
def factorization(n):
res = []
if n % 2 == 0:
res.append(2)
for i in range(3, math.floor(n // 2) + 1, 2):
if n % i == 0:
c = 0
for j in res:
if i % j == 0:
c = 1
if c == 0:
res.append(i)
return res
def fact2(n):
p = factorization(n)
res = []
for i in p:
c = 0
z = n
while 1:
if z % i == 0:
c += 1
z /= i
else:
break
res.append([i, c])
return res
def fact(n): # 階乗
ans = 1
m = n
for _i in range(n - 1):
ans *= m
m -= 1
return ans
def comb(n, r): # コンビネーション
if n < r:
return 0
l = min(r, n - r)
m = n
u = 1
for _i in range(l):
u *= m
m -= 1
return u // fact(l)
def combmod(n, r, mod):
return (fact(n) / fact(n - r) * pow(fact(r), mod - 2, mod)) % mod
def printQueue(q):
r = copyQueue(q)
ans = [0] * r.qsize()
for i in range(r.qsize() - 1, -1, -1):
ans[i] = r.get()
print(ans)
class UnionFind:
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x): # root
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -1 * self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x): # much time
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()} # 1~n
def bitArr(n): # ビット全探索
x = 1
zero = "0" * n
ans = []
ans.append([0] * n)
for i in range(2**n - 1):
ans.append(list(map(lambda x: int(x), list((zero + bin(x)[2:])[-1 * n :]))))
x += 1
return ans
def arrsSum(a1, a2):
for i in range(len(a1)):
a1[i] += a2[i]
return a1
def maxValue(a, b, v):
v2 = v
for i in range(v2, -1, -1):
for j in range(v2 // a + 1): # j:aの個数
k = i - a * j
if k % b == 0:
return i
return -1
def copyQueue(q):
nq = queue.Queue()
n = q.qsize()
for i in range(n):
x = q.get()
q.put(x)
nq.put(x)
return nq
s = readInt()
ex = [s]
while 1:
if ex[-1] % 2:
ex.append(3 * ex[-1] + 1)
else:
ex.append(ex[-1] // 2)
if ex.count(ex[-1]) == 2:
break
print(len(ex))
| Statement
A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows:
* The first term s is given as input.
* Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.
* a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.
Find the minimum integer m that satisfies the following condition:
* There exists an integer n such that a_m = a_n (m > n). | [{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}] |
Print the minimum integer m that satisfies the condition.
* * * | s514169771 | Wrong Answer | p03146 | Input is given from Standard Input in the following format:
s | s = int(input())
tmp = [s]
for m in range(2, 1000000):
if tmp[-1] % 2 == 0:
if tmp[-1] / 2 in tmp:
print(tmp, m)
exit()
else:
tmp.append(tmp[-1] / 2)
else:
if 3 * tmp[-1] + 1 in tmp:
print(tmp, m)
exit()
else:
tmp.append(3 * tmp[-1] + 1)
| Statement
A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows:
* The first term s is given as input.
* Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.
* a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.
Find the minimum integer m that satisfies the following condition:
* There exists an integer n such that a_m = a_n (m > n). | [{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}] |
Print the minimum integer m that satisfies the condition.
* * * | s961044038 | Runtime Error | p03146 | Input is given from Standard Input in the following format:
s | a=int(input())
s=set(a)
i=1
while True:
if a % 2==1:
a = a * 3 + 1
else:
a = a//2
if a in s:
break
s|={a}
i+=1
print(i) | Statement
A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows:
* The first term s is given as input.
* Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.
* a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.
Find the minimum integer m that satisfies the following condition:
* There exists an integer n such that a_m = a_n (m > n). | [{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}] |
Print the minimum integer m that satisfies the condition.
* * * | s599153825 | Runtime Error | p03146 | Input is given from Standard Input in the following format:
s | #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
#define ll long long
#define ll1 1ll
#define ONE 1ll
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using pii = pair<int, int>;
// template<class T> ostream &operator<<(ostream &os,T &t){dump(t);return os;}
template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T> void printvv(const vector<vector<T>> &v) {
cerr << endl;
rep(i, 0, v.size()) rep(j, 0, v[i].size()) {
if (typeid(v[i][j]).name() == typeid(INF).name() and v[i][j] == INF) {
cerr << "INF";
}
else
cerr << v[i][j];
cerr << (j == v[i].size() - 1 ? '\n' : ' ');
}
cerr << endl;
}
/*
typedef __int128_t Int;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
__int128 parse(string &s) {
__int128 ret = 0;
for (int i = 0; i < s.length(); i++)
if ('0' <= s[i] && s[i] <= '9')
ret = 10 * ret + s[i] - '0';
return ret;
}
*/
#ifndef _DEBUG
#define printvv(...)
#endif
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
signed main(signed argc, char *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int s; cin >> s;
set<int>st;
st.insert(s);
auto f = [&](int n) {
if (n % 2 == 0)return n / 2;
else return 3 * n + 1;
};
int i = 2;
for (;; i++) {
s = f(s);
if (st.count(s))break;
st.insert(s);
}
cout << i << endl;
return 0;
}
| Statement
A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows:
* The first term s is given as input.
* Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.
* a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.
Find the minimum integer m that satisfies the following condition:
* There exists an integer n such that a_m = a_n (m > n). | [{"input": "8", "output": "5\n \n\na=\\\\{8,4,2,1,4,2,1,4,2,1,......\\\\}. As a_5=a_2, the answer is 5.\n\n* * *"}, {"input": "7", "output": "18\n \n\na=\\\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\\\}.\n\n* * *"}, {"input": "54", "output": "114"}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.