message
stringlengths
2
44.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
42
109k
cluster
float64
5
5
__index_level_0__
int64
84
217k
Provide a correct Python 3 solution for this coding contest problem. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809
instruction
0
107,642
5
215,284
"Correct Solution: ``` from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce import pprint sys.setrecursionlimit(10 ** 9) INF = 10 ** 13 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 k, q = LI() D = LI() for _ in range(q): n, x, m = LI() ret = x D_m = [d % m for d in D] zero_or_one = list(accumulate([1 if d == 0 else 0 for d in D_m])) Dm_acc = list(accumulate(D_m)) ret = x + (n - 1) // k * Dm_acc[-1] zero_ret = (n - 1) // k * zero_or_one[-1] if (n - 1) % k: ret += Dm_acc[(n - 1) % k - 1] zero_ret += zero_or_one[(n - 1) % k - 1] print(n - 1 - (ret // m - x // m) - zero_ret) ```
output
1
107,642
5
215,285
Provide a correct Python 3 solution for this coding contest problem. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809
instruction
0
107,643
5
215,286
"Correct Solution: ``` import sys input = sys.stdin.buffer.readline K, Q = map(int, input().split()) D = list(map(int, input().split())) NXM = [list(map(int, input().split())) for _ in range(Q)] for N, X, M in NXM: E = [d % M for d in D] cnt0 = 0 tmp0 = 0 for e in E: if e > 0: cnt0 += 1 tmp0 += e cnt = ((N - 1) // K) * cnt0 tmp = ((N - 1) // K) * tmp0 for i in range((N - 1) % K): if E[i] > 0: cnt += 1 tmp += E[i] tmp += X % M print(cnt - tmp // M) ```
output
1
107,643
5
215,287
Provide a correct Python 3 solution for this coding contest problem. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809
instruction
0
107,644
5
215,288
"Correct Solution: ``` import sys input = sys.stdin.buffer.readline K, Q = map(int, input().split()) D = list(map(int, input().split())) NXM = [list(map(int, input().split())) for _ in range(Q)] for N, X, M in NXM: E = [d % M for d in D] cnt, tmp = 0, 0 for e in E: if e > 0: cnt += 1 tmp += e cnt *= ((N - 1) // K) tmp *= ((N - 1) // K) for i in range((N - 1) % K): if E[i] > 0: cnt += 1 tmp += E[i] tmp += X % M print(cnt - tmp // M) ```
output
1
107,644
5
215,289
Provide a correct Python 3 solution for this coding contest problem. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809
instruction
0
107,645
5
215,290
"Correct Solution: ``` import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) #処理内容 def main(): K, Q = getlist() D = getlist() for _ in range(Q): N, X, M = getlist() X %= M d = [0] * K zeroCount = 0 for i in range(K): var = D[i] % M d[i] = var if var == 0: zeroCount += 1 Dsum = sum(d) p = int((N - 1) // K) q = (N - 1) % K An = X + Dsum * p # print(p, q) # print(An) # print(d) zero = 0 for i in range(q): An += d[i] if d[i] == 0: zero += 1 # print(X, An) # print(zeroCount * p + zero) # print(int(An // M) - int((X + M - 1) // M)) ans = N - 1 - (zeroCount * p + zero) - (int(An // M) - int(X // M)) print(ans) if __name__ == '__main__': main() ```
output
1
107,645
5
215,291
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` import sys input = sys.stdin.readline K, Q = map(int, input().split()) d = list(map(int, input().split())) for _ in range(Q): n, x, m = map(int, input().split()) x %= m dq = [y % m for y in d] for i in range(K): dq[i] += (dq[i] == 0) * m res = (sum(dq) * ((n - 1) // K) + sum(dq[: (n - 1) % K]) + x) // m print(n - 1 - res) ```
instruction
0
107,646
5
215,292
Yes
output
1
107,646
5
215,293
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` import sys def main(): K, Q = map(int, input().split()) D = list(map(int, input().split())) for _ in range(Q): n, x, m = map(int, input().split()) md = [D[i] % m for i in range(K)] smda = 0 mda0 = 0 for i in range((n - 1) % K): if md[i] == 0: mda0 += 1 smda += md[i] smd = smda md0 = mda0 for i in range((n - 1) % K, K): if md[i] == 0: md0 += 1 smd += md[i] roop = (n - 1) // K res = n - 1 - (x % m + smd * roop + smda) // m - md0 * roop - mda0 print(res) main() ```
instruction
0
107,647
5
215,294
Yes
output
1
107,647
5
215,295
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` #!/usr/bin/env python3 import sys debug = False def solve(ds, n, x, m): ds = [d % m for d in ds] nr_loop = (n - 1) // len(ds) lastloop_remaining = (n - 1) % len(ds) a_n = x cnt_zero = 0 for i in range(0, len(ds)): dmod = ds[i] % m a_n += (dmod) * nr_loop if dmod == 0: cnt_zero += nr_loop if i < lastloop_remaining: a_n += dmod if dmod == 0: cnt_zero += 1 cnt_decrease = a_n // m - x // m return n - 1 - cnt_decrease - cnt_zero def read_int_list(sep = " "): return [int(s) for s in sys.stdin.readline().split(sep)] def dprint(*args, **kwargs): if debug: print(*args, **kwargs) return def main(): k, q = read_int_list() d = read_int_list() for _ in range(0, q): n, x, m = read_int_list() print(str(solve(d, n, x, m))) if __name__ == "__main__": main() ```
instruction
0
107,648
5
215,296
Yes
output
1
107,648
5
215,297
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` def f_modularness(): K, Q = [int(i) for i in input().split()] D = [int(i) for i in input().split()] Queries = [[int(i) for i in input().split()] for j in range(Q)] def divceil(a, b): return (a + b - 1) // b ans = [] for n, x, m in Queries: last, eq = x, 0 # 末項、D_i % m == 0 となる D_i の数 for i,d in enumerate(D): num = divceil(n - 1 - i, K) # D_i を足すことは何回起きるか? last += (d % m) * num if d % m == 0: eq += num ans.append((n - 1) - (last // m - x // m) - eq) return '\n'.join(map(str, ans)) print(f_modularness()) ```
instruction
0
107,649
5
215,298
Yes
output
1
107,649
5
215,299
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` #!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl sys.setrecursionlimit(2147483647) from heapq import heappush, heappop,heappushpop from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter from itertools import permutations mod = 10**9 + 7 inf = float('inf') def I(): return int(sys.stdin.readline()) def LI(): return list(map(int,sys.stdin.readline().split())) k,q = LI() d = LI() nxm = [LI() for _ in range(q)] for n,x,m in nxm: e = list(map(lambda x:x%m,d)) zero = e.count(0) zero = zero*((n-1)//k) for i in range((n-1)%k+1): if e[i] == 0: zero += 1 f = list(accumulate(e)) s = f[-1] * ((n-1)//k) if (n-1)%k != 0: s += f[(n-1)%k-1] tmp = (s+x%m)//m print(n-1-zero-tmp) ```
instruction
0
107,650
5
215,300
No
output
1
107,650
5
215,301
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` import numpy as np k, q = map(int, input().split()) d = np.array(input().split(), dtype=np.int) for _ in range(q): n, x, m = map(int, input().split()) quot = (n-1) // k rest = (n-1) % k dmod = d % m same = (k - dmod.count_nonzero()) * quot + rest - dmod[:rest].count_nonzero() a_last = x % m + dmod.sum() * quot + dmod[:rest].sum() beyond = a_last // m print(n - 1 - same - beyond) ```
instruction
0
107,651
5
215,302
No
output
1
107,651
5
215,303
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` k, q = map(int, input().split()) d = list(map(int, input().split())) nxm = [map(int, input().split()) for _ in range(q)] for n, x, m in nxm: dd = [e % m for e in d] ans = n - 1 divq, divr = divmod(n - 1, k) dd_r = dd[:divr] ans -= dd.count(0) * divq ans -= dd_r.count(0) last = x + sum(dd) * divq + sum(dd_r) ans -= last // m - x // m print(ans) ```
instruction
0
107,652
5
215,304
No
output
1
107,652
5
215,305
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}. Process the following q queries in order: * The i-th query contains three integers n_i, x_i, and m_i. Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray} Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i). Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0). Constraints * All values in input are integers. * 1 \leq k, q \leq 5000 * 0 \leq d_i \leq 10^9 * 2 \leq n_i \leq 10^9 * 0 \leq x_i \leq 10^9 * 2 \leq m_i \leq 10^9 Input Input is given from Standard Input in the following format: k q d_0 d_1 ... d_{k - 1} n_1 x_1 m_1 n_2 x_2 m_2 : n_q x_q m_q Output Print q lines. The i-th line should contain the response to the i-th query. Examples Input 3 1 3 1 4 5 3 2 Output 1 Input 7 3 27 18 28 18 28 46 1000000000 1000000000 1 7 1000000000 2 10 1000000000 3 12 Output 224489796 214285714 559523809 Submitted Solution: ``` import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines k,q=map(int,readline().split()) D=list(map(int,readline().split())) NXM = map(int, read().split()) NXM=iter(NXM) NXM=zip(NXM,NXM,NXM) for n,x,m in NXM: tmpD=list(map(lambda x:x%m,D)) SD=[0]+tmpD n-=1 for i in range(1,k): SD[i+1]+=SD[i] x=x%m x+=SD[-1]*(n//k) x+=SD[n%k] count=tmpD.count(0)*(n//k) count+=tmpD[:n%k].count(0) ans=n-x//m-count print(ans) ```
instruction
0
107,653
5
215,306
No
output
1
107,653
5
215,307
Provide a correct Python 3 solution for this coding contest problem. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724
instruction
0
107,654
5
215,308
"Correct Solution: ``` 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(): mod=998244353 N=I() A=LI() MA=max(A) x=[0]*(MA+1) #数列x[i]はi*(iの個数) for i in range(N): x[A[i]]+=A[i] #print(x) #上位集合のゼータ変換 for k in range(1,MA+1): for s in range(k*2,MA+1,k):#sが2kからkずつ増える=>sはkの倍数(自分自身を数えないため,2kから) x[k]=(x[k]+x[s])%mod #print(x) #この段階でx[i]はiを約数に持つやつの和(個数も加味している) #積演算 for i in range(MA+1): x[i]=(x[i]*x[i])%mod #print(x) #メビウス変換で戻す for k in range(MA,0,-1): for s in range(k*2,MA+1,k): x[k]=(x[k]-x[s])%mod #この段階でx[i]はiをgcdに持つもの同士をかけたものの和 #print(x) #gcdでわる ans=0 for i in range(1,MA+1): if x[i]!=0: x[i]=(x[i]*pow(i,mod-2,mod))%mod ans=(ans+x[i])%mod #この段階でx[i]はiをgcdに持つもの同士のlcaの和 #print(x) ans=(ans-sum(A))%mod #A[i],A[i]の組を除去する.A[i]動詞のくみはlcaがA[i]になる """ ans=(ans*pow(2,mod-2,mod))%mod#A[i]*A[j] と A[j]*A[i]の被り除去 print(pow(2,mod-2,mod))=499122177 """ ans=(ans*499122177)%mod print(ans) main() ```
output
1
107,654
5
215,309
Provide a correct Python 3 solution for this coding contest problem. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724
instruction
0
107,655
5
215,310
"Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) MOD = 998244353 max_a = max(a) c = [0] * (max_a + 1) for i in range(1, max_a + 1): c[i] = pow(i, MOD - 2, MOD) for i in range(1, max_a): j = i while True: j += i if j <= max_a: c[j] -= c[i] c[j] %= MOD else: break cnt = [0] * (max_a + 1) for i in range(n): cnt[a[i]] += 1 ans = 0 for i in range(1, max_a + 1): tmp0 = 0 tmp1 = 0 j = i while True: if j <= max_a: tmp0 += cnt[j] * j tmp1 += cnt[j] * (j ** 2) else: break j += i tmp0 *= tmp0 ans += (tmp0 - tmp1) * c[i] ans %= MOD print((ans * pow(2, MOD - 2, MOD)) % MOD) ```
output
1
107,655
5
215,311
Provide a correct Python 3 solution for this coding contest problem. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724
instruction
0
107,656
5
215,312
"Correct Solution: ``` import sys readline = sys.stdin.readline MOD = 998244353 N = int(readline()) A = list(map(int, readline().split())) mA = max(A) + 1 T = [0]*mA i2 = pow(2, MOD-2, MOD) table = [0]*mA table2 = [0]*mA for a in A: table[a] = (table[a] + a)%MOD table2[a] = (table2[a] + a*a)%MOD prime = [True]*mA for p in range(2, mA): if not prime[p]: continue for k in range((mA-1)//p, 0, -1): table[k] = (table[k] + table[k*p])%MOD table2[k] = (table2[k] + table2[k*p])%MOD prime[k*p] = False T = [(x1*x1-x2)*i2%MOD for x1, x2 in zip(table, table2)] ans = 0 coeff = [0]*mA for i in range(1, mA): k = T[i] l = - coeff[i] + pow(i, MOD-2, MOD) ans = (ans+l*k)%MOD for j in range(2*i, mA, i): coeff[j] = (coeff[j] + l)%MOD print(ans) ```
output
1
107,656
5
215,313
Provide a correct Python 3 solution for this coding contest problem. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724
instruction
0
107,657
5
215,314
"Correct Solution: ``` import sys sys.setrecursionlimit(2147483647) INF=float("inf") MOD=998244353 input=lambda:sys.stdin.readline().rstrip() class modfact(object): def __init__(self,n): fact=[1]*(n+1); invfact=[1]*(n+1) for i in range(1,n+1): fact[i]=i*fact[i-1]%MOD invfact[n]=pow(fact[n],MOD-2,MOD) for i in range(n-1,-1,-1): invfact[i]=invfact[i+1]*(i+1)%MOD self.__fact=fact; self.__invfact=invfact def inv(self,n): assert(n>0) return self.__fact[n-1]*self.__invfact[n]%MOD def fact(self,n): return self.__fact[n] def invfact(self,n): return self.__invfact[n] def comb(self,n,k): if(k<0 or n<k): return 0 return self.__fact[n]*self.__invfact[k]*self.__invfact[n-k]%MOD def perm(self,n,k): if(k<0 or n<k): return 0 self.__fact[n]*self.__invfact[k]%MOD def prime(n): if n<=1: return [] S=[1]*(n+1) S[0]=0; S[1]=0 for i in range(2,n): if(S[i]==0): continue for j in range(2*i,n+1,i): S[j]=0 return [p for p in range(n+1) if(S[p])] def resolve(): n=int(input()) A=list(map(int,input().split())) V=max(A) C=[0]*(V+1) for a in A: C[a]+=1 P=prime(V) W=[1]*(V+1) for p in P: for i in range(p,V+1,p): W[i]*=(1-p) mf=modfact(V) for i in range(1,V+1): W[i]=(W[i]*mf.inv(i))%MOD ans=0 for d in range(1,V+1): s=0 # 和(後に2乗する) t=0 # 2乗の和 for i in range(d,V+1,d): s+=i*C[i] t+=(i**2)*C[i] ans+=W[d]*(s**2-t)//2 ans%=MOD print(ans) resolve() ```
output
1
107,657
5
215,315
Provide a correct Python 3 solution for this coding contest problem. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724
instruction
0
107,658
5
215,316
"Correct Solution: ``` import sys input = sys.stdin.buffer.readline N = int(input()) A = list(map(int, input().split())) zeta = [0] * (max(A) + 1) mod = 998244353 res = 0 for a in A: zeta[a] += a for i in range(1, len(zeta)): for j in range(2 * i, len(zeta), i): zeta[i] += zeta[j] for i in range(1, len(zeta)): zeta[i] *= zeta[i] for i in range(len(zeta) - 1, 0, -1): for j in range(2 * i, len(zeta), i): zeta[i] -= zeta[j] for a in A: zeta[a] -= a ** 2 for i in range(1, len(zeta)): zeta[i] //= 2 for i in range(1, len(zeta)): zeta[i] //= i res = (res + zeta[i]) % mod print(res) ```
output
1
107,658
5
215,317
Provide a correct Python 3 solution for this coding contest problem. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724
instruction
0
107,659
5
215,318
"Correct Solution: ``` N=int(input());A=list(map(int, input().split()));z=[0]*(max(A)+1);m=998244353;r=0 for a in A:z[a]+=a for i in range(1,len(z)): for j in range(2*i,len(z),i):z[i]+=z[j] for i in range(1,len(z)):z[i]*=z[i] for i in range(len(z)-1,0,-1): for j in range(2*i,len(z),i):z[i]-=z[j] for a in A:z[a]-=a**2 for i in range(1,len(z)): z[i]//=i*2;r=(r+z[i])%m print(r) ```
output
1
107,659
5
215,319
Provide a correct Python 3 solution for this coding contest problem. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724
instruction
0
107,661
5
215,322
"Correct Solution: ``` mod = 998244353 n = int(input()) a = list(map(int, input().split())) m = max(a) cnt = [0]*(m+1) for ai in a: cnt[ai]+=1 ans = 0 res = [0]*(m+1) for i in range(1, m+1)[::-1]: sm = 0 sq = 0 for j in range(i, m+1, i): sm+=j*cnt[j] sq+=cnt[j]*j*j res[i]-=res[j] res[i]+=sm*sm-sq ans+=res[i]//i ans%=mod print(ans*(pow(2, mod-2, mod))%mod) ```
output
1
107,661
5
215,323
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` N=int(input()) A=list(map(int,input().split())) V=max(A) mod=998244353 data=[0]*(V+1) data[1]=1 for i in range(2,V+1): data[i]=-(mod//i)*data[mod%i]%mod for i in range(1,V+1): for j in range(2,V//i+1): data[i*j]=(data[i*j]-data[i])%mod lst=[0]*(V+1) for a in A: lst[a]+=1 mod_2=pow(2,mod-2,mod) ans=0 for i in range(1,V+1): sum_1=0 sum_2=0 for j in range(1,V//i+1): zzz=i*j sum_1+=zzz*lst[zzz] sum_2+=zzz**2*lst[zzz] ans=(ans+(sum_1**2-sum_2)*data[i]*mod_2)%mod print(ans) ```
instruction
0
107,662
5
215,324
Yes
output
1
107,662
5
215,325
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` import sys def prepare(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv solo_invs = [0] + [f * i % MOD for f, i in zip(factorials, invs[1:])] return factorials, invs, solo_invs def decompose_inverses(solo_invs, MOD): # 各整数 g に対して、g の約数である各 i について dcm[i] を全て足すと 1/g になるような数列を作成 n = len(solo_invs) dcm = solo_invs[:] for i in range(1, n): d = dcm[i] for j in range(2 * i, n, i): dcm[j] -= d for i in range(1, n): dcm[i] %= MOD return dcm n, *aaa = map(int, sys.stdin.buffer.read().split()) MOD = 998244353 LIMIT = max(aaa) count = [0] * (LIMIT + 1) double = [0] * (LIMIT + 1) for a in aaa: count[a] += a double[a] += a * a _, _, solo_invs = prepare(LIMIT, MOD) dcm = decompose_inverses(solo_invs, MOD) ans = 0 inv2 = solo_invs[2] for d in range(1, LIMIT + 1): mulsum = sum(count[d::d]) ** 2 - sum(double[d::d]) % MOD if mulsum: ans = (ans + dcm[d] * mulsum * inv2) % MOD print(ans) ```
instruction
0
107,663
5
215,326
Yes
output
1
107,663
5
215,327
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` def main(): import sys input = sys.stdin.readline n = int(input()) a = tuple(map(int,input().split())) mod = 998244353 v = max(a) inv = [0]*(v+1) inv[1] = 1 for i in range(2,v+1): inv[i] = mod - (mod//i)*inv[mod%i]%mod #w w = [1]*(v+1) for i in range(2,v+1): w[i] = (inv[i]-w[i])%mod for j in range(i*2,v+1,i): w[j] = (w[j] + w[i])%mod #res res = 0 num = [0]*(v+1) for e in a: num[e] += 1 for d in range(1,v+1): s = 0 t = 0 for j in range(d,v+1,d): s = (s + num[j]*(j//d)) t = (t + (num[j]*(j//d))*(j//d)) AA = ((((s**2-t)//2)%mod*d)%mod)*d%mod res = (res + w[d]*AA%mod)%mod print(res%mod) if __name__ == '__main__': main() ```
instruction
0
107,664
5
215,328
Yes
output
1
107,664
5
215,329
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` import sys sys.setrecursionlimit(2147483647) INF=float("inf") MOD=998244353 input=lambda:sys.stdin.readline().rstrip() class modfact(object): def __init__(self,n): fact=[1]*(n+1); invfact=[1]*(n+1) for i in range(1,n+1): fact[i]=i*fact[i-1]%MOD invfact[n]=pow(fact[n],MOD-2,MOD) for i in range(n-1,-1,-1): invfact[i]=invfact[i+1]*(i+1)%MOD self.__fact=fact; self.__invfact=invfact def inv(self,n): assert(n>0) return self.__fact[n-1]*self.__invfact[n]%MOD def fact(self,n): return self.__fact[n] def invfact(self,n): return self.__invfact[n] def comb(self,n,k): if(k<0 or n<k): return 0 return self.__fact[n]*self.__invfact[k]*self.__invfact[n-k]%MOD def perm(self,n,k): if(k<0 or n<k): return 0 self.__fact[n]*self.__invfact[k]%MOD def prime(n): if n<=1: return [] S=[1]*(n+1) S[0]=0; S[1]=0 for i in range(2,n): if(S[i]==0): continue for j in range(2*i,n+1,i): S[j]=0 return [p for p in range(n+1) if(S[p])] def resolve(): n=int(input()) A=list(map(int,input().split())) V=max(A) C=[0]*(V+1) for a in A: C[a]+=1 P=prime(V) W=[1]*(V+1) for p in P: for i in range(p,V+1,p): W[i]*=(1-p) mf=modfact(V) for i in range(1,V+1): W[i]=(W[i]*mf.inv(i))%MOD ans=0 for d in range(1,V+1): s=0 # 和(後に2乗する) t=0 # 2乗の和 for i in range(d,V+1,d): s+=i*C[i]%MOD t+=(i**2)*C[i]%MOD ans+=W[d]*(s**2-t)*mf.inv(2) ans%=MOD print(ans) resolve() ```
instruction
0
107,665
5
215,330
Yes
output
1
107,665
5
215,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` import sys def prepare(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv solo_invs = [0] + [f * i % MOD for f, i in zip(factorials, invs[1:])] return factorials, invs, solo_invs def decompose_inverses(solo_invs, MOD): # 各整数 g に対して、g の約数である各 i について dcm[i] を全て足すと 1/g になるような数列を作成 n = len(solo_invs) dcm = solo_invs[:] for i in range(1, n): d = dcm[i] for j in range(2 * i, n, i): dcm[j] -= d for i in range(1, n): dcm[i] %= MOD return dcm n, *aaa = map(int, sys.stdin.buffer.read().split()) MOD = 998244353 LIMIT = max(aaa) count = [0] * (LIMIT + 1) double = [0] * (LIMIT + 1) for a in aaa: count[a] += a double[a] += a * a _, _, solo_invs = prepare(LIMIT, MOD) dcm = decompose_inverses(solo_invs, MOD) ans = 0 inv2 = solo_invs[2] for d in range(1, LIMIT + 1): mulsum = sum(count[d::d]) ** 2 - sum(double[d::d]) % MOD if mulsum: ans = (ans + dcm[d] * mulsum * inv2) % MOD print(ans) ```
instruction
0
107,666
5
215,332
No
output
1
107,666
5
215,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` def main(): import sys input = sys.stdin.readline n = int(input()) a = tuple(map(int,input().split())) mod = 998244353 v = max(a) inv = [0]*(v+1) inv[1] = 1 for i in range(2,v+1): inv[i] = mod - (mod//i)*inv[mod%i]%mod #w w = [1]*(v+1) for i in range(2,v+1): w[i] = (inv[i]-w[i])%mod for j in range(i*2,v+1,i): w[j] = (w[j] + w[i])%mod #res res = 0 num = [0]*(v+1) for e in a: num[e] += 1 for d in range(1,v+1): s = 0 t = 0 for j in range(d,v+1,d): s = (s + num[j]*(j//d))%mod t = (t + (num[j]*(j//d)%mod)*(j//d))%mod AA = ((((s**2-t)//2)%mod*d)%mod)*d%mod res = (res + w[d]*AA%mod)%mod print(res%mod) if __name__ == '__main__': main() ```
instruction
0
107,667
5
215,334
No
output
1
107,667
5
215,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` import sys sys.setrecursionlimit(4100000) n=int(input()) A=[int(x) for x in input().split()] import itertools #import fractions from functools import lru_cache #com = list(itertools.combinations(A, 2)) res = 0 waru=998244353 @lru_cache(maxsize=None) def gcd(a,b): if b == 0: return a return gcd(b,a % b) @lru_cache(maxsize=None) def lcm(x,y): r = (x * y) // gcd(x, y) % waru return r @lru_cache(maxsize=None) def goukei(i,j): if j < n-1: return lcm(A[i],A[j]) + goukei(i,j+1) else: return lcm(A[i],A[j]) for i in range(n-1): res += goukei(i,i+1) #for a, b in com: #res += lcm(a,b) print(res % waru) ```
instruction
0
107,668
5
215,336
No
output
1
107,668
5
215,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}. Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b): * \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j) Since the answer may be enormous, compute it modulo 998244353. Constraints * 1 \leq N \leq 200000 * 1 \leq A_i \leq 1000000 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_0\ A_1\ \cdots\ A_{N-1} Output Print the sum modulo 998244353. Examples Input 3 2 4 6 Output 22 Input 8 1 2 3 4 6 8 12 12 Output 313 Input 10 356822 296174 484500 710640 518322 888250 259161 609120 592348 713644 Output 353891724 Submitted Solution: ``` import math import sys sys.setrecursionlimit(10**6) N = int(input()) A = list(map(int , input().split())) #result = 0; def lcm(x, y): return (x * y) // math.gcd(x, y) def sigma2(func, frm, to): result1 = 0; #答えの受け皿 for i in range(frm, to+1): result1 += func(A[frm - 1],A[i]) #ここで関数を呼び出す。ちなみにここではi = x return result1 def sigma(sigma2, frm, to): result = 0; #答えの受け皿 for i in range(frm, to+1): result += sigma2(lcm,i + 1,N - 1) result %= 998244353 #ここで関数を呼び出す。ちなみにここではi = x return result if __name__ == "__main__": print(sigma(sigma2,0,N - 2) ) ```
instruction
0
107,669
5
215,338
No
output
1
107,669
5
215,339
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,670
5
215,340
"Correct Solution: ``` import sys from heapq import heappush, heappushpop Q = int(input()) high = [] low = [] sum_dh, sum_dl = 0, 0 sum_b = 0 MIN_X = -(10 ** 9) buf = [] for qi, line in enumerate(sys.stdin): q = list(map(int, line.split())) if len(q) == 1: x = -low[0] dx = x - MIN_X ans_l = dx * len(low) - sum_dl ans_h = sum_dh - dx * len(high) buf.append('{} {}\n'.format(x, ans_l + ans_h + sum_b)) else: _, a, b = q da = a - MIN_X sum_b += b if len(low) == len(high): h = heappushpop(high, a) heappush(low, -h) dh = h - MIN_X sum_dh += da - dh sum_dl += dh else: l = -heappushpop(low, -a) heappush(high, l) dl = l - MIN_X sum_dl += da - dl sum_dh += dl print(''.join(buf)) ```
output
1
107,670
5
215,341
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,671
5
215,342
"Correct Solution: ``` from heapq import* L,R=[],[] B=t=0 Q,*E=open(0) for e in E: if' 'in e:_,a,b=map(int,e.split());t^=1;a*=2*t-1;c=heappushpop([L,R][t],a);heappush([R,L][t],-c);B+=b+a-c-c else:print(-L[0],B-L[0]*t) ```
output
1
107,671
5
215,343
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,672
5
215,344
"Correct Solution: ``` import sys input = sys.stdin.readline Q = int(input()) Y = [] D = [] S = [0] * Q s = 0 for i in range(Q): a = list(map(int, input().split())) Y.append(a) if a[0] == 1: D.append(a[1]) s += a[2] S[i] = s D = sorted(list(set(D))) INV = {} for i in range(len(D)): INV[D[i]] = i N = 17 X = [0] * (2**(N+1)-1) C = [0] * (2**(N+1)-1) def add(j, x): i = 2**N + j - 1 while i >= 0: X[i] += x C[i] += 1 i = (i-1) // 2 def rangeof(i): s = (len(bin(i+1))-3) l = ((i+1) - (1<<s)) * (1<<N-s) r = l + (1<<N-s) return (l, r) def rangesum(a, b): l = a + (1<<N) r = b + (1<<N) s = 0 while l < r: if l%2: s += X[l-1] l += 1 if r%2: r -= 1 s += X[r-1] l >>= 1 r >>= 1 return s def rangecnt(a, b): l = a + (1<<N) r = b + (1<<N) s = 0 while l < r: if l%2: s += C[l-1] l += 1 if r%2: r -= 1 s += C[r-1] l >>= 1 r >>= 1 return s c = 0 su = 0 CC = [0] * (2**N) for i in range(Q): y = Y[i] if y[0] == 1: add(INV[y[1]], y[1]) CC[INV[y[1]]] += 1 c += 1 su += y[1] else: l, r = 0, 2**N while True: m = (l+r)//2 rc = rangecnt(0, m) if rc >= (c+1)//2: r = m elif rc + CC[m] <= (c-1)//2: l = m else: break rs = rangesum(0, m) print(D[m], D[m]*rc-rs+(su-rs)-(c-rc)*D[m]+S[i]) ```
output
1
107,672
5
215,345
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,673
5
215,346
"Correct Solution: ``` import sys from heapq import heappush, heappushpop Q = int(input()) high = [] low = [] sum_dh, sum_dl = 0, 0 sum_b = 0 buf = [] for qi, line in enumerate(sys.stdin): q = list(map(int, line.split())) if len(q) == 1: x = -low[0] ans_l = x * len(low) - sum_dl ans_h = sum_dh - x * len(high) buf.append('{} {}\n'.format(x, ans_l + ans_h + sum_b)) else: _, a, b = q sum_b += b if len(low) == len(high): h = heappushpop(high, a) heappush(low, -h) sum_dh += a - h sum_dl += h else: l = -heappushpop(low, -a) heappush(high, l) sum_dl += a - l sum_dh += l print(''.join(buf)) ```
output
1
107,673
5
215,347
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,674
5
215,348
"Correct Solution: ``` import heapq class DynamicMedian(): """値をO(logN)で追加し、中央値をO(1)で求める add(val): valを追加する median_low(): 小さい方の中央値(low median)を求める median_high(): 大きい方の中央値(high median)を求める """ def __init__(self): self.l_q = [] # 中央値以下の値を降順で格納する self.r_q = [] # 中央値以上の値を昇順で格納する self.l_sum = 0 self.r_sum = 0 def add(self, val): """valを追加する""" if len(self.l_q) == len(self.r_q): self.l_sum += val val = -heapq.heappushpop(self.l_q, -val) self.l_sum -= val heapq.heappush(self.r_q, val) self.r_sum += val else: self.r_sum += val val = heapq.heappushpop(self.r_q, val) self.r_sum -= val heapq.heappush(self.l_q, -val) self.l_sum += val def median_low(self): """小さい方の中央値を求める""" if len(self.l_q) + 1 == len(self.r_q): return self.r_q[0] else: return -self.l_q[0] def median_high(self): """大きい方の中央値を求める""" return self.r_q[0] def minimum_query(self): """キューに追加されている値 a1,...,aN に対して、|x-a1| + |x-a2| + ⋯ + |x-aN|の最小値を求める x = (a1,...,aN の中央値) となる""" res1 = (len(self.l_q) * self.median_high() - self.l_sum) res2 = (self.r_sum - len(self.r_q) * self.median_high()) return res1 + res2 q = int(input()) info = [list(map(int, input().split())) for i in range(q)] dm = DynamicMedian() b = 0 for i in range(q): if info[i][0] == 1: _, tmp_a, tmp_b = info[i] dm.add(tmp_a) b += tmp_b else: print(dm.median_low(), dm.minimum_query() + b) ```
output
1
107,674
5
215,349
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,675
5
215,350
"Correct Solution: ``` import heapq q = int(input()) l = [] r = [] ai = 0 bs = 0 for i in range(q): ipt = list(map(int,input().split())) if ipt[0] == 1: al = heapq.heappushpop(l,-ipt[1]) ar = heapq.heappushpop(r,ipt[1]) ai -= (al+ar) heapq.heappush(l,-ar) heapq.heappush(r,-al) bs += ipt[2] else: an = heapq.heappop(l) print(-an,bs+ai) heapq.heappush(l,an) ```
output
1
107,675
5
215,351
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,676
5
215,352
"Correct Solution: ``` import heapq Q = int(input()) # 中央値の左側の値、右側の値を管理する # heapqとする。rightは最小が興味あるが、leftは最大なので、-1をかけて扱う left, right = [], [] # 両方のSUMも管理する必要がある。毎回SUMしてたら間に合わん Lsum, Rsum = 0,0 Lcnt, Rcnt = 0,0 B = 0 for _ in range(Q): q = list(map(int, input().split())) if len(q) == 1: # 2 # heapqってPeakできないの・・・? l = (-1) *left[0] #l = (-1) * heapq.heappop(left) #heapq.heappush(left,(-1)*l) # (l-l1) + (l-l2) + ... + (l-l) + (r-l) + ... + (r1 - l) print(l, Rsum//2 - Lsum//2 + B) #print(left,right, Lsum, Rsum) else: # 1 _,a,b = q B += b # まず双方にaを突っ込む heapq.heappush(left,(-1)*a) heapq.heappush(right,a) Lsum += a Lcnt += 1 Rsum += a Rcnt += 1 # leftの最大値と、rightの最小値の関係が崩れていたら、交換する l = (-1)*left[0] r = right[0] #l = (-1) * heapq.heappop(left) #r = heapq.heappop(right) if l>=r: Lsum = Lsum - l + r Rsum = Rsum - r + l l,r = r,l _ = heapq.heappop(left) _ = heapq.heappop(right) heapq.heappush(left,(-1)*l) heapq.heappush(right,r) ```
output
1
107,676
5
215,353
Provide a correct Python 3 solution for this coding contest problem. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000
instruction
0
107,677
5
215,354
"Correct Solution: ``` import heapq q = int(input()) inf = 10000000000 left = [inf] right = [inf] minval = 0 for _ in range(q): query = list(map(int, input().split())) if query[0] == 1: _, a, b = query if a < -left[0]: v = -heapq.heappop(left) heapq.heappush(right, v) heapq.heappush(left, -a) heapq.heappush(left, -a) minval += abs(v - a) + b elif a > right[0]: v = heapq.heappop(right) heapq.heappush(left, -v) heapq.heappush(right, a) heapq.heappush(right, a) minval += abs(v - a) + b else: heapq.heappush(left, -a) heapq.heappush(right, a) minval += b else: print(-left[0], minval) ```
output
1
107,677
5
215,355
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` import sys from heapq import heappush, heappop read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): Q = int(readline()) hi = [] lo = [] hi_sum = lo_sum = 0 b_sum = 0 ans = [] for _ in range(Q): A = list(map(int, readline().split())) if len(A) == 3: a, b = A[1], A[2] b_sum += b if not lo: lo.append(-a) lo_sum += a else: if a <= -lo[0]: heappush(lo, -a) lo_sum += a else: heappush(hi, a) hi_sum += a if len(hi) > len(lo): x = heappop(hi) hi_sum -= x heappush(lo, -x) lo_sum += x elif len(hi) + 1 < len(lo): x = -heappop(lo) lo_sum -= x heappush(hi, x) hi_sum += x else: x = -lo[0] val = x * (len(lo) - len(hi)) - lo_sum + hi_sum + b_sum ans.append((x, val)) for x, val in ans: print(x, val) return if __name__ == '__main__': main() ```
instruction
0
107,678
5
215,356
Yes
output
1
107,678
5
215,357
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` from heapq import heappush, heappop def inpl(): return list(map(int, input().split())) Q = int(input()) L = [] R = [] B = 0 M = 0 for _ in range(Q): q = inpl() if len(q) == 3: B += q[2] if len(R) == 0: L.append(-q[1]) R.append(q[1]) continue M += min(abs(-L[0] - q[1]), abs(R[0] - q[1])) * (not (-L[0] <= q[1] <= R[0])) if q[1] < R[0]: heappush(L, -q[1]) heappush(L, -q[1]) heappush(R, -heappop(L)) else: heappush(R, q[1]) heappush(R, q[1]) heappush(L, -heappop(R)) else: print(-L[0], B+M) ```
instruction
0
107,679
5
215,358
Yes
output
1
107,679
5
215,359
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` # import numpy as np # from collections import defaultdict # from functools import reduce import heapq # s = input() Q = int(input()) # A = list(map(int, input().split())) # n, m, k = map(int, input().split()) A_left_sum = 0 A_left = [] A_right_sum = 0 A_right = [] A_med = None query_size = 0 b_sum = 0 # n = len(As) # n is odd: # A_left + As[n//2] + B_left # n is even: # A_left + B_left for i in range(Q): query = list(map(int, input().split())) if query[0] == 1: a = query[1] if A_med is None: A_med = a else: if query_size%2 == 0: # even left_max = -A_left[0] right_min = A_right[0] if a < left_max: heapq.heappushpop(A_left, -a) A_left_sum += a - left_max A_med = left_max elif a > right_min: heapq.heappushpop(A_right, a) A_right_sum += a - right_min A_med = right_min else: A_med = a else: # odd if A_med <= a: heapq.heappush(A_left, -A_med) heapq.heappush(A_right, a) A_left_sum += A_med A_right_sum += a else: heapq.heappush(A_left, -a) heapq.heappush(A_right, A_med) A_right_sum += A_med A_left_sum += a # print(As) # print(A_left) # print(A_right) query_size += 1 b_sum += query[2] else: if query_size%2 == 0: #even print(-A_left[0], A_right_sum - A_left_sum + b_sum) else: print(A_med, A_right_sum - A_left_sum + b_sum) ```
instruction
0
107,680
5
215,360
Yes
output
1
107,680
5
215,361
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` import heapq class PriorityQueue: def __init__(self): self.__heap = [] self.__count = 0 def empty(self) -> bool: return self.__count == 0 def dequeue(self): if self.empty(): raise Exception('empty') self.__count -= 1 return heapq.heappop(self.__heap) def enqueue(self, v): self.__count += 1 heapq.heappush(self.__heap, v) def __len__(self): return self.__count def absolute_minima(): Q = int(input()) L, R = PriorityQueue(), PriorityQueue() sL, sR = 0, 0 M = None B = 0 N = 0 for _ in range(Q): query = [int(s) for s in input().split()] if query[0] == 1: _, a, b = query B += b N += 1 if M is None: M = a elif M < a: R.enqueue(a) sR += a else: L.enqueue(-a) sL += a while len(R)-len(L) > 1: L.enqueue(-M) sL += M M = R.dequeue() sR -= M while len(L) > len(R): R.enqueue(M) sR += M M = -L.dequeue() sL -= M else: s = - sL + sR + B if N % 2 == 0: s -= M print(M, s) if __name__ == "__main__": absolute_minima() ```
instruction
0
107,681
5
215,362
Yes
output
1
107,681
5
215,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` Q = int(input()) cnt = 0 m = [] M = [] import heapq heapq.heapify(m) heapq.heapify(M) sum_m = 0 sum_M = 0 B = 0 cnt = 0 for i in range(Q): q = str(input()) if q != '2': _, a, b = map(int, q.split()) B += b cnt += 1 if len(m) != 0 and len(M) != 0: x1 = heapq.heappop(m)*(-1) x2 = heapq.heappop(M) sum_m -= x1 sum_M -= x2 if a <= x1: heapq.heappush(m, a*(-1)) sum_m += a heapq.heappush(M, x1) heapq.heappush(M, x2) sum_M += x1+x2 else: heapq.heappush(M, a) sum_M += a heapq.heappush(m, x1*(-1)) heapq.heappush(M, x2*(-1)) sum_m += x1+x2 elif len(m) != 0 and len(M) == 0: x1 = heapq.heappop(m)*(-1) sum_m -= x1 if a <= x1: heapq.heappush(m, a*(-1)) sum_m += a heapq.heappush(M, x1) sum_M += x1 else: heapq.heappush(M, a) sum_M += a heapq.heappush(m, x1*(-1)) sum_m += x1 else: heapq.heappush(m, a*(-1)) sum_m += a else: if cnt%2 == 1: x = heapq.heappop(m)*(-1) min_ = sum_M+sum_m+B-x*cnt print(x, min_) heapq.heappush(m, x*(-1)) else: if cnt == 0: print(0, 0) else: x = heapq.heappop(m)*(-1) min_ = sum_M+sum_m+B-x*cnt #print(sum_M, sum_m, B) print(x, min_) heapq.heappush(m, x*(-1)) ```
instruction
0
107,682
5
215,364
No
output
1
107,682
5
215,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` Q = int(input()) queries = [] for _ in range(Q): queries.append(input()) from bisect import insort_left ary, bsum, c = [], 0, 0 for query in queries: if query[0] == '2': absum = sum(abs(a - ary[(c-1)//2]) for a in ary) print(' '.join(map(str, (ary[(c-1)//2], absum + bsum)))) else: _, a, b = map(int, query.split()) insort_left(ary, a) bsum += b c += 1 ```
instruction
0
107,683
5
215,366
No
output
1
107,683
5
215,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` from collections import defaultdict,deque import sys,heapq,bisect,math,itertools,string,queue,datetime sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inpl(): return list(map(int, input().split())) def inpl_str(): return list(input().split()) Q = int(input()) LOWq = [] HIGHq = [] heapq.heapify(LOWq) heapq.heapify(HIGHq) SUM = 0 LOWsum = 0 HIGHsum = 0 LOWn = 0 HIGHn = 0 keisu = 0 ans = [] N = 0 ave = -INF for _ in range(Q): inp = inpl() if inp[0] == 1: query,a,b = inp SUM += a # 合計値 keisu += b # 係数 N += 1 bef_ave = ave # 前平均 ave = SUM/N # 平均 if a >= ave: heapq.heappush(HIGHq,a) HIGHsum += a HIGHn += 1 else: heapq.heappush(LOWq,-a) LOWsum += a LOWn += 1 if bef_ave < ave: while True: pop = heapq.heappop(HIGHq) HIGHsum -= pop HIGHn -= 1 if pop >= ave: heapq.heappush(HIGHq,pop) HIGHsum += pop HIGHn += 1 break else: heapq.heappush(LOWq,-pop) LOWsum += pop LOWn += 1 elif bef_ave > ave: while True: pop = -heapq.heappop(LOWq) LOWsum -= pop LOWn -= 1 if pop <= ave: heapq.heappush(LOWq,-pop) LOWsum += pop LOWn += 1 break else: heapq.heappush(HIGHq,pop) HIGHsum += pop HIGHn += 1 else: #print(LOWsum,HIGHsum) #print('n',LOWn,HIGHn) if LOWn == 0: x = heapq.heappop(HIGHq) heapq.heappush(HIGHq,x) else: x = -heapq.heappop(LOWq) heapq.heappush(LOWq,-x) tmp = (HIGHsum - LOWsum) + (LOWn-HIGHn)*x + keisu ans.append([x,tmp]) for azu,nyan in ans: print(azu,nyan) ```
instruction
0
107,684
5
215,368
No
output
1
107,684
5
215,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a function f(x), which is initially a constant function f(x) = 0. We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: * An update query `1 a b`: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x). * An evaluation query `2`: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints * All values in input are integers. * 1 \leq Q \leq 2 \times 10^5 * -10^9 \leq a, b \leq 10^9 * The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between. Examples Input 4 1 4 2 2 1 1 -8 2 Output 4 2 1 -3 Input 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Output -1000000000 3000000000 Submitted Solution: ``` Q = int(input()) query = [] mid = [] for _ in range(Q): a = list(map(int, input().split())) query.append(a) for i in range(len(query)): if query[i][0] == 1: mid.append(query[i][1:]) else: ans = 0 mid.sort(key=lambda x: x[0]) minx = int((len(mid)+1)/2) for j in mid: ans += abs(mid[minx-1][0]-j[0])+j[1] print(mid[minx-1][0],ans) ```
instruction
0
107,685
5
215,370
No
output
1
107,685
5
215,371
Provide a correct Python 3 solution for this coding contest problem. For $n$ dynamic arrays $A_i$ ($i = 0, 1, ..., n-1$), perform a sequence of the following operations: * pushBack($t$, $x$): Add element $x$ at the end of $A_t$. * dump($t$): Print all elements in $A_t$. * clear($t$): Clear $A_t$. If $A_t$ is empty, do nothing. $A_i$ is a 0-origin array and it is empty in the initial state. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * $-1,000,000,000 \leq x \leq 1,000,000,000$ * The total number of elements printed by dump operations do not exceed 500,000 Input The input is given in the following format. $n$ $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $t$ where the first digits 0, 1 and 2 represent pushBack, dump and clear operations respectively. Output For each dump operation, print elements of $A_t$ a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the array is empty, an empty line should be printed. Example Input 3 13 0 0 1 0 0 2 0 0 3 0 1 -1 0 2 4 0 2 5 1 0 1 1 1 2 2 1 1 0 1 1 1 2 Output 1 2 3 -1 4 5 1 2 3 4 5
instruction
0
107,847
5
215,694
"Correct Solution: ``` import sys input = sys.stdin.readline N,Q = map(int,input().split()) A = [[] for _ in range(N)] for _ in range(Q): q = list(map(int,input().split())) if q[0] == 0: A[q[1]].append(q[2]) elif q[0] == 1: if A[q[1]]: print(' '.join(map(str,A[q[1]]))) else: print() else: A[q[1]] = [] ```
output
1
107,847
5
215,695
Provide a correct Python 3 solution for this coding contest problem. For $n$ dynamic arrays $A_i$ ($i = 0, 1, ..., n-1$), perform a sequence of the following operations: * pushBack($t$, $x$): Add element $x$ at the end of $A_t$. * dump($t$): Print all elements in $A_t$. * clear($t$): Clear $A_t$. If $A_t$ is empty, do nothing. $A_i$ is a 0-origin array and it is empty in the initial state. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * $-1,000,000,000 \leq x \leq 1,000,000,000$ * The total number of elements printed by dump operations do not exceed 500,000 Input The input is given in the following format. $n$ $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $t$ where the first digits 0, 1 and 2 represent pushBack, dump and clear operations respectively. Output For each dump operation, print elements of $A_t$ a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the array is empty, an empty line should be printed. Example Input 3 13 0 0 1 0 0 2 0 0 3 0 1 -1 0 2 4 0 2 5 1 0 1 1 1 2 2 1 1 0 1 1 1 2 Output 1 2 3 -1 4 5 1 2 3 4 5
instruction
0
107,848
5
215,696
"Correct Solution: ``` n, q = list(map(int, input().split())) a = [[] for _ in range(n)] for _ in range(q): q = list(map(int, input().split())) if q[0]==0: a[q[1]].append(q[2]) elif q[0]==1: print(" ".join(map(str, a[q[1]]))) elif q[0]==2: a[q[1]].clear() ```
output
1
107,848
5
215,697
Provide a correct Python 3 solution for this coding contest problem. For $n$ dynamic arrays $A_i$ ($i = 0, 1, ..., n-1$), perform a sequence of the following operations: * pushBack($t$, $x$): Add element $x$ at the end of $A_t$. * dump($t$): Print all elements in $A_t$. * clear($t$): Clear $A_t$. If $A_t$ is empty, do nothing. $A_i$ is a 0-origin array and it is empty in the initial state. Constraints * $1 \leq n \leq 1,000$ * $1 \leq q \leq 500,000$ * $-1,000,000,000 \leq x \leq 1,000,000,000$ * The total number of elements printed by dump operations do not exceed 500,000 Input The input is given in the following format. $n$ $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $t$ $x$ or 1 $t$ or 2 $t$ where the first digits 0, 1 and 2 represent pushBack, dump and clear operations respectively. Output For each dump operation, print elements of $A_t$ a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the array is empty, an empty line should be printed. Example Input 3 13 0 0 1 0 0 2 0 0 3 0 1 -1 0 2 4 0 2 5 1 0 1 1 1 2 2 1 1 0 1 1 1 2 Output 1 2 3 -1 4 5 1 2 3 4 5
instruction
0
107,852
5
215,704
"Correct Solution: ``` from sys import stdin n,q = map(int,input().split(" ")) list2 = [[] for _ in range(n)] for i in range(q): cmd = stdin.readline().strip().split() if cmd[0] == "0": list2[int(cmd[1])].append(cmd[2]) elif cmd[0] == "1": print(*list2[int(cmd[1])]) else: list2[int(cmd[1])].clear() ```
output
1
107,852
5
215,705
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1. 1 l r x — increase all integers on the segment from l to r by values x; 2. 2 l r — find <image>, where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo 109 + 7. In this problem we define Fibonacci numbers as follows: f(1) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for all x > 2. Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha? Input The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). Then follow m lines with queries descriptions. Each of them contains integers tpi, li, ri and may be xi (1 ≤ tpi ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type. It's guaranteed that the input will contains at least one query of the second type. Output For each query of the second type print the answer modulo 109 + 7. Examples Input 5 4 1 1 2 1 1 2 1 5 1 2 4 2 2 2 4 2 1 5 Output 5 7 9 Note Initially, array a is equal to 1, 1, 2, 1, 1. The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5. After the query 1 2 4 2 array a is equal to 1, 3, 4, 3, 1. The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7. The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9. Submitted Solution: ``` fibs, prev = [0, 1], 1 def fib_to(new): global prev, fibs if new > prev: for i in range(prev+1, new+1): fibs.append(fibs[i-1] + fibs[i-2]) prev = new return fibs[new] n, m = map(int, input().split()) omg = list(map(int, input().split())) #print(omg) for i in range(m): q = list(map(int, input().split())) if q[0] == 1: l, r, x = q[1], q[2], q[3] for j in range(l-1, r): omg[j] += x #print(omg) else: l, r = q[1], q[2] s = 0 for j in range(l-1, r): s += fib_to(omg[j]) print(s) ```
instruction
0
108,302
5
216,604
No
output
1
108,302
5
216,605
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1. 1 l r x — increase all integers on the segment from l to r by values x; 2. 2 l r — find <image>, where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo 109 + 7. In this problem we define Fibonacci numbers as follows: f(1) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for all x > 2. Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha? Input The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). Then follow m lines with queries descriptions. Each of them contains integers tpi, li, ri and may be xi (1 ≤ tpi ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type. It's guaranteed that the input will contains at least one query of the second type. Output For each query of the second type print the answer modulo 109 + 7. Examples Input 5 4 1 1 2 1 1 2 1 5 1 2 4 2 2 2 4 2 1 5 Output 5 7 9 Note Initially, array a is equal to 1, 1, 2, 1, 1. The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5. After the query 1 2 4 2 array a is equal to 1, 3, 4, 3, 1. The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7. The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9. Submitted Solution: ``` def myround(x): return int(x) if (x - int(x) == 0) else int(x) + 1 raw = input().split() n = int(raw[0]) m = int(raw[1]) struct = [] tmpn = n while (True): struct.append([0 for j in range(tmpn)]) if tmpn == 1: break tmpn = myround(tmpn / 2) raw = input().split() for i in range(n): struct[0][i] = int(raw[i]) divider = [1000000007] def show(struct): for a in struct: for x in a: print(x, end=' ') print() def isStarts(depth, l): return (l % (1 << depth)) == 0 def inBounds(depth, l, r): # print(depth) return (1 << depth) <= (r - l + 1) def isEnds(depth, r): return ((l + 1) % (1 << depth)) == 0 def add(struct, depth, l, dx): # print('add') struct[depth][l // (1 << depth)] += dx def get(struct, start): res = 0 for i in range(len(struct)): res += struct[i][start - 1] start = myround(start / 2) # print('start = ' + str(start)) return res def mypow(i): if i == 1: return [[0, 1], [1, 1]] elif i > 1: mat = mypow(i // 2) if i % 2 == 0: return mult(mat, mat) else: return mult(mult(mat, mat), mypow(1)) def mult(mat1, mat2): return [[(mat1[0][0]% divider[0] * mat2[0][0]% divider[0])% divider[0] + (mat1[0][1]% divider[0] * mat2[1][0]% divider[0])% divider[0], (mat1[1][0]% divider[0] * mat2[0][0]% divider[0])% divider[0] + (mat1[1][1]% divider[0] * mat2[1][0]% divider[0])% divider[0]], [(mat1[0][0]% divider[0] * mat2[0][1]% divider[0])% divider[0] + (mat1[0][1]% divider[0] * mat2[1][1]% divider[0])% divider[0], (mat1[1][0]% divider[0] * mat2[0][1]% divider[0])% divider[0] + (mat1[1][1]% divider[0] * mat2[1][1]% divider[0])% divider[0]]] def countFib(i): mat = mypow(i) if mat != None: return(mat[0][1] % divider[0]) for i in range(m): raw = input().split() l = int(raw[1]) - 1 r = int(raw[2]) - 1 if raw[0] == '1': dx = int(raw[3]) depth = 0 while l <= r: while inBounds(depth, l, r): if isStarts(depth + 1, l): depth += 1 else: add(struct, depth, l, dx) l += (1 << depth) depth = max(depth - 1, 0) if inBounds(depth, l, r) and isStarts(depth, l): add(struct, depth, l, dx) l += (1 << depth) else: depth = 0 # print('l = ' + str(l)) # print('r = ' + str(r)) # while (inBounds(depth, l, r)): # if isEnds(depth, r): # break # depth -= 1 # break # show(struct) else: sum = 0 for i in range(l, r + 1): x = get(struct, i + 1) # print('x = ' + str(x)) sum += countFib(x) sum % divider[0] print(sum) ```
instruction
0
108,303
5
216,606
No
output
1
108,303
5
216,607
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types: 1. 1 l r x — increase all integers on the segment from l to r by values x; 2. 2 l r — find <image>, where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo 109 + 7. In this problem we define Fibonacci numbers as follows: f(1) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for all x > 2. Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha? Input The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). Then follow m lines with queries descriptions. Each of them contains integers tpi, li, ri and may be xi (1 ≤ tpi ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type. It's guaranteed that the input will contains at least one query of the second type. Output For each query of the second type print the answer modulo 109 + 7. Examples Input 5 4 1 1 2 1 1 2 1 5 1 2 4 2 2 2 4 2 1 5 Output 5 7 9 Note Initially, array a is equal to 1, 1, 2, 1, 1. The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5. After the query 1 2 4 2 array a is equal to 1, 3, 4, 3, 1. The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7. The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9. Submitted Solution: ``` def myround(x): return int(x) if (x - int(x) == 0) else int(x) + 1 raw = input().split() n = int(raw[0]) m = int(raw[1]) struct = [] tmpn = n while (True): struct.append([0 for j in range(tmpn)]) if tmpn == 1: break tmpn = myround(tmpn / 2) raw = input().split() for i in range(n): struct[0][i] = int(raw[i]) divider = [1000000007] def show(struct): for a in struct: for x in a: print(x, end=' ') print() def isStarts(depth, l): return (l % (1 << depth)) == 0 def inBounds(depth, l, r): # print(depth) return (1 << depth) <= (r - l + 1) def isEnds(depth, r): return ((l + 1) % (1 << depth)) == 0 def add(struct, depth, l, dx): # print('add') struct[depth][l // (1 << depth)] += dx def get(struct, start): res = 0 for i in range(len(struct)): res += struct[i][start - 1] start = myround(start / 2) # print('start = ' + str(start)) return res def mypow(i): if i == 1: return [[0, 1], [1, 1]] elif i > 1: mat = mypow(i // 2) if i % 2 == 0: return mult(mat, mat) else: return mult(mult(mat, mat), mypow(1)) def mult(mat1, mat2): return [[(mat1[0][0] * mat2[0][0] + mat1[0][1] * mat2[1][0]) % divider[0], (mat1[1][0] * mat2[0][0] + mat1[1][1] * mat2[1][0]) % divider[0]], [(mat1[0][0] * mat2[0][1] + mat1[0][1] * mat2[1][1]) % divider[0], (mat1[1][0] * mat2[0][1] + mat1[1][1] * mat2[1][1]) % divider[0]]] def countFib(i): mat = mypow(i) if mat != None: return(mat[0][1] % divider[0]) for i in range(m): raw = input().split() l = int(raw[1]) - 1 r = int(raw[2]) - 1 if raw[0] == '1': dx = int(raw[3]) depth = 0 while l <= r: while inBounds(depth, l, r): if isStarts(depth + 1, l): depth += 1 else: add(struct, depth, l, dx) l += (1 << depth) depth = max(depth - 1, 0) if inBounds(depth, l, r) and isStarts(depth, l): add(struct, depth, l, dx) l += (1 << depth) else: depth = 0 # print('l = ' + str(l)) # print('r = ' + str(r)) # while (inBounds(depth, l, r)): # if isEnds(depth, r): # break # depth -= 1 # break # show(struct) else: sum = 0 for i in range(l, r + 1): x = get(struct, i + 1) # print('x = ' + str(x)) sum += countFib(x) sum % divider[0] print(sum) ```
instruction
0
108,304
5
216,608
No
output
1
108,304
5
216,609
Provide a correct Python 3 solution for this coding contest problem. Given are N positive integers A_1,...,A_N. Consider positive integers B_1, ..., B_N that satisfy the following condition. Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds. Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N. Since the answer can be enormous, print the sum modulo (10^9 +7). Constraints * 1 \leq N \leq 10^4 * 1 \leq A_i \leq 10^6 * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7). Examples Input 3 2 3 4 Output 13 Input 5 12 12 12 12 12 Output 5 Input 3 1000000 999999 999998 Output 996989508
instruction
0
108,427
5
216,854
"Correct Solution: ``` from fractions import gcd N = int(input()) A = list(map(int, input().split())) MOD = 1000000007 lcm = 1 for a in A: lcm = a // gcd(lcm, a) * lcm ans = 0 for a in A: ans += lcm // a ans %= MOD print(ans) ```
output
1
108,427
5
216,855