結果

問題 No.186 中華風 (Easy)
ユーザー shotoyooshotoyoo
提出日時 2020-09-13 19:03:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,691 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 86,836 KB
実行使用メモリ 71,412 KB
最終ジャッジ日時 2023-09-27 01:01:41
合計ジャッジ時間 2,976 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,308 KB
testcase_01 AC 69 ms
71,160 KB
testcase_02 AC 70 ms
71,072 KB
testcase_03 AC 69 ms
71,068 KB
testcase_04 AC 79 ms
71,160 KB
testcase_05 AC 75 ms
71,104 KB
testcase_06 AC 74 ms
71,224 KB
testcase_07 AC 74 ms
71,068 KB
testcase_08 AC 70 ms
71,312 KB
testcase_09 AC 69 ms
71,264 KB
testcase_10 AC 68 ms
71,412 KB
testcase_11 AC 68 ms
71,036 KB
testcase_12 AC 70 ms
71,264 KB
testcase_13 AC 70 ms
71,108 KB
testcase_14 AC 69 ms
71,312 KB
testcase_15 AC 70 ms
71,316 KB
testcase_16 AC 70 ms
70,944 KB
testcase_17 AC 70 ms
71,304 KB
testcase_18 AC 70 ms
71,040 KB
testcase_19 AC 71 ms
71,108 KB
testcase_20 AC 72 ms
71,092 KB
testcase_21 AC 69 ms
71,072 KB
testcase_22 AC 70 ms
70,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


xy = [tuple(map(int, input().split())) for _ in range(3)]
from math import gcd
def gcd2(a, b):
    """a*x + b*y = gcd(a,b)なるx,yも求める
    """
    l = []
    while b:
        l.append(divmod(a,b))
        a, b = b, a%b
    x, y = 1, 0
    for aa,bb in l[::-1]:
        x, y = y, x - aa*y
    return a, x, y
def modinv(x, M):
    """素数ではないM、Mと互いに素なxに対し
    x * y == 1 mod M なるyを求める
    """
    a,xx,yy = gcd2(x,M)
#     l = []
#     while M:
#         l.append(divmod(x,M))
#         x, M = M, x%M
#     xx, yy = 1, 0
#     for aa,bb in l[::-1]:
#         xx, yy = yy, xx - aa*yy
    return a,xx%M
def crt(rs, ms):
    """x == rs[i] mod ms[i] をみたすxをを求め、(x, l(=ms))を返す
    (そのようなxは x + i * lとして書ける)
    存在しない場合はNoneを返す
    長さが0の配列を渡すと(0,1)を返す
    """
    r0 = 0
    m0 = 1
    for r1,m1 in zip(rs, ms):
        if m0<m1:
            m0, m1 = m1, m0
            r0, r1 = r1, r0
        if m0%m1==0:
            if r0%m1 != r1:
                return None,None
            else:
                continue
#         print(m0,m1)
        g,im = modinv(m0, m1)
        u1 = m1//g
        if (r1-r0)%g!=0:
            return None,None
        x = (r1-r0) // g % u1 * im % u1
        r0 += x * m0
        m0 *= u1
        if r0<0:
            r0 += m0
    return r0,m0
r,m = crt([item[0] for item in xy], [item[1] for item in xy])
if r is not None:
    if r==0:
        r += m
    print(r)
else:
    print(-1)
0