結果

問題 No.2117 中国剰余定理入門
ユーザー vwxyzvwxyz
提出日時 2023-06-14 14:49:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,739 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 10,816 KB
実行使用メモリ 10,556 KB
最終ジャッジ日時 2023-09-05 03:04:44
合計ジャッジ時間 2,286 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,548 KB
testcase_01 AC 34 ms
10,436 KB
testcase_02 AC 34 ms
10,408 KB
testcase_03 AC 34 ms
10,344 KB
testcase_04 AC 35 ms
10,468 KB
testcase_05 AC 34 ms
10,408 KB
testcase_06 AC 35 ms
10,552 KB
testcase_07 AC 34 ms
10,428 KB
testcase_08 AC 34 ms
10,492 KB
testcase_09 AC 35 ms
10,412 KB
testcase_10 AC 34 ms
10,528 KB
testcase_11 AC 35 ms
10,364 KB
testcase_12 AC 34 ms
10,556 KB
testcase_13 AC 34 ms
10,456 KB
testcase_14 AC 35 ms
10,452 KB
testcase_15 AC 34 ms
10,428 KB
testcase_16 AC 35 ms
10,452 KB
testcase_17 AC 34 ms
10,488 KB
testcase_18 AC 34 ms
10,476 KB
testcase_19 AC 34 ms
10,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
write=sys.stdout.write

def Extended_Euclid(n,m):
    stack=[]
    while m:
        stack.append((n,m))
        n,m=m,n%m
    if n>=0:
        x,y=1,0
    else:
        x,y=-1,0
    for i in range(len(stack)-1,-1,-1):
        n,m=stack[i]
        x,y=y,x-(n//m)*y
    return x,y

def LCM(n,m):
    if n or m:
        return abs(n)*abs(m)//math.gcd(n,m)
    return 0

def CRT(remainder_lst,mod_lst):
    assert len(remainder_lst)==len(mod_lst)
    if not remainder_lst:
        return 0,1
    remainder,mod=remainder_lst[0],mod_lst[0]
    for r,m in zip(remainder_lst[1:],mod_lst[1:]):
        if (r,m)==(-1,0):
            remainder,mod=-1,0
            break
        r%=m
        g=math.gcd(mod,m)
        lcm=LCM(mod,m)
        if remainder%g!=r%g:
            remainder,mod=-1,0
            break
        remainder,mod=(r+m*((remainder-r)//g)*Extended_Euclid(m//g,mod//g)[0])%lcm,lcm
    return remainder,mod

R,M=[],[]
for _ in range(2):
    m,r=map(int,readline().split())
    R.append(r)
    M.append(m)
r,m=CRT(R,M)
if r==-1:
    ans="NaN"
else:
    ans=r
print(ans)
0