結果
問題 | No.2119 一般化百五減算 |
ユーザー | 👑 rin204 |
提出日時 | 2022-11-04 21:32:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,030 ms / 2,000 ms |
コード長 | 3,071 bytes |
コンパイル時間 | 146 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 86,924 KB |
最終ジャッジ日時 | 2024-07-18 19:15:22 |
合計ジャッジ時間 | 5,196 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,480 KB |
testcase_01 | AC | 36 ms
53,120 KB |
testcase_02 | AC | 33 ms
52,736 KB |
testcase_03 | AC | 33 ms
53,248 KB |
testcase_04 | AC | 35 ms
52,992 KB |
testcase_05 | AC | 32 ms
53,120 KB |
testcase_06 | AC | 32 ms
52,864 KB |
testcase_07 | AC | 33 ms
52,736 KB |
testcase_08 | AC | 35 ms
52,864 KB |
testcase_09 | AC | 33 ms
52,992 KB |
testcase_10 | AC | 32 ms
52,992 KB |
testcase_11 | AC | 33 ms
53,120 KB |
testcase_12 | AC | 32 ms
53,120 KB |
testcase_13 | AC | 32 ms
53,120 KB |
testcase_14 | AC | 32 ms
52,736 KB |
testcase_15 | AC | 33 ms
53,120 KB |
testcase_16 | AC | 33 ms
53,120 KB |
testcase_17 | AC | 32 ms
53,120 KB |
testcase_18 | AC | 32 ms
53,248 KB |
testcase_19 | AC | 36 ms
52,736 KB |
testcase_20 | AC | 720 ms
83,472 KB |
testcase_21 | AC | 89 ms
80,240 KB |
testcase_22 | AC | 799 ms
83,944 KB |
testcase_23 | AC | 1,030 ms
86,924 KB |
testcase_24 | AC | 965 ms
85,108 KB |
ソースコード
def modinv(a, MOD): b = MOD u = 1 v = 0 while b: t = a // b a -= t * b u -= t * v a, b = b, a u, v = v, u u %= MOD return u def Garner(M, R): m_prod = M[0] C = R[0] for m, r in zip(M[1:], R[1:]): t = (r - C) * modinv(m_prod, m) % m C += t * m_prod m_prod *= m return C from math import gcd def isprime(n): if n <= 1: return False elif n == 2: return True elif n % 2 == 0: return False A = [2, 325, 9375, 28178, 450775, 9780504, 1795265022] s = 0 d = n - 1 while d % 2 == 0: s += 1 d >>= 1 for a in A: if a % n == 0: return True x = pow(a, d, n) if x != 1: for t in range(s): if x == n - 1: break x = x * x % n else: return False return True def pollard(n): if n % 2 == 0: return 2 if isprime(n): return n f = lambda x:(x * x + 1) % n step = 0 while 1: step += 1 x = step y = f(x) while 1: p = gcd(y - x + n, n) if p == 0 or p == n: break if p != 1: return p x = f(x) y = f(f(y)) def primefact(n): if n == 1: return [] p = pollard(n) if p == n: return [p] left = primefact(p) right = primefact(n // p) left += right return sorted(left) n = int(input()) m = int(input()) mr = [] for _ in range(m): b, c = map(int, input().split()) mr.append((b, c % b)) mm = {} rr = {} ng = False for m, r in mr: if m == 1: continue primes = primefact(m) bef = -1 x = 1 for p in primes: if p != bef: if bef != -1: if bef in mm: r_ = r % x if mm[bef] >= x: if rr[bef] % x != r_: ng = True break else: if r_ % mm[bef] != rr[bef]: ng = True break mm[bef] = x rr[bef] = r_ else: mm[bef] = x rr[bef] = r % x x = p bef = p else: x *= p if bef in mm: r_ = r % x if mm[bef] >= x: if rr[bef] % x != r_: ng = True else: if r_ % mm[bef] != rr[bef]: ng = True mm[bef] = x rr[bef] = r_ else: mm[bef] = x rr[bef] = r % x if ng: break if ng: print("NaN") else: M = [1] R = [0] for k in mm: M.append(mm[k]) R.append(rr[k]) ans = Garner(M, R) if ans <= n: print(ans) else: print("NaN")