結果
問題 | No.2273 一点乗除区間積 |
ユーザー | ecottea |
提出日時 | 2023-03-09 04:04:15 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,521 bytes |
コンパイル時間 | 373 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 295,296 KB |
最終ジャッジ日時 | 2024-09-18 02:46:00 |
合計ジャッジ時間 | 12,442 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 58 ms
63,360 KB |
testcase_01 | AC | 60 ms
64,128 KB |
testcase_02 | AC | 59 ms
64,000 KB |
testcase_03 | AC | 57 ms
63,360 KB |
testcase_04 | AC | 57 ms
63,232 KB |
testcase_05 | AC | 59 ms
63,872 KB |
testcase_06 | AC | 59 ms
63,232 KB |
testcase_07 | AC | 58 ms
63,232 KB |
testcase_08 | AC | 57 ms
63,616 KB |
testcase_09 | AC | 58 ms
63,360 KB |
testcase_10 | AC | 58 ms
63,744 KB |
testcase_11 | AC | 58 ms
63,104 KB |
testcase_12 | AC | 58 ms
63,488 KB |
testcase_13 | AC | 57 ms
63,872 KB |
testcase_14 | AC | 58 ms
63,488 KB |
testcase_15 | AC | 59 ms
64,256 KB |
testcase_16 | AC | 220 ms
78,976 KB |
testcase_17 | AC | 275 ms
79,744 KB |
testcase_18 | AC | 794 ms
81,584 KB |
testcase_19 | AC | 2,624 ms
274,816 KB |
testcase_20 | TLE | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
ソースコード
import re import sys sys.setrecursionlimit(100000) def factor_integer(n): ps = [] es = [] i = 2 while (i * i <= n): e = 0 while n % i == 0: e += 1 n //= i if e > 0: ps += [i] es += [e] i += 1 if n > 1: ps += [n] es += [1] return ps, es class SegmentTree: def __init__(self, ini, op, e): self.n = len(ini) self.op = op self.e = e self.v = [e] * (2 * self.n) for i in range(self.n): self.v[self.n + i] = ini[i] for i in range(self.n - 1, 0, -1): self.v[i] = op(self.v[2 * i], self.v[2 * i + 1]) def set(self, i, x): i += self.n self.v[i] = x while i > 1: i >>= 1 self.v[i] = self.op(self.v[2 * i], self.v[2 * i + 1]) def prod(self, l, r): resl = self.e resr = self.e l += self.n r += self.n while l < r: if l & 1: resl = self.op(resl, self.v[l]) l += 1 if r & 1: resr = self.op(self.v[r - 1], resr) l >>= 1 r >>= 1 return self.op(resl, resr) def get(self, i): return self.v[i + self.n] def dump(self): print(self.v[self.n:]) content = input() pattern = r'^(\d+) (\d+) (\d+)$' result = re.match(pattern, content) if not result: sys.exit("format error.") N, B, Q = map(int, result.groups()) if not (1 <= N and N <= 10**5): sys.exit("N") if not (1 <= B and B <= 10**9): sys.exit("B") if not (1 <= Q and Q <= 10**5): sys.exit("Q") ps, es = factor_integer(B) K = len(ps) def op(x, y): res = [0] * (K + 1) for k in range(K): res[k] = x[k] + y[k] res[K] = x[K] * y[K] % B return res e = ([0] * K) + [1] content = input() pattern = '^' for n in range(N): pattern += r'(\d+)' if n < N - 1: pattern += ' ' pattern += '$' result = re.match(pattern, content) if not result: sys.exit("format error.") A = list(map(int, result.groups())) ini = [] for n in range(N): x = [0] * (K + 1) if A[n] != 0: for k in range(K): while A[n] % ps[k] == 0: A[n] //= ps[k] x[k] += 1 x[K] = A[n] % B ini += [x] seg = SegmentTree(ini, op, e) for _ in range(Q): content = input() pattern = r'^(\d+) (\d+) (\d+) (\d+)$' result = re.match(pattern, content) if not result: sys.exit("format error.") j, m, l, r = map(int, result.groups()) if not (0 <= j and j < N): sys.exit("j") if not (0 <= m and m <= 10**18): sys.exit("m") if not (0 <= l and l <= r and r < N): sys.exit("l, r") x = seg.get(j) divisible = False if m == B: divisible = True if x[K] != 0: for k in range(K): if x[k] < es[k]: divisible = False break if divisible: for k in range(K): x[k] -= es[k] if not divisible: if m != 0: for k in range(K): while m % ps[k] == 0: m //= ps[k] x[k] += 1 x[K] = x[K] * m % B seg.set(j, x) x = seg.prod(l, r + 1) if x[K] != 0: for k in range(K): x[K] = x[K] * pow(ps[k], x[k], B) % B print(x[K])