結果

問題 No.186 中華風 (Easy)
ユーザー titiatitia
提出日時 2022-05-12 03:25:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,126 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 8,468 KB
最終ジャッジ日時 2023-09-26 23:50:50
合計ジャッジ時間 1,624 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,452 KB
testcase_01 AC 17 ms
8,368 KB
testcase_02 AC 17 ms
8,352 KB
testcase_03 AC 17 ms
8,424 KB
testcase_04 AC 17 ms
8,312 KB
testcase_05 AC 17 ms
8,440 KB
testcase_06 AC 17 ms
8,300 KB
testcase_07 AC 17 ms
8,356 KB
testcase_08 AC 17 ms
8,300 KB
testcase_09 AC 17 ms
8,368 KB
testcase_10 AC 17 ms
8,272 KB
testcase_11 AC 16 ms
8,436 KB
testcase_12 AC 17 ms
8,368 KB
testcase_13 AC 16 ms
8,444 KB
testcase_14 AC 16 ms
8,316 KB
testcase_15 AC 16 ms
8,468 KB
testcase_16 AC 17 ms
8,344 KB
testcase_17 AC 16 ms
8,352 KB
testcase_18 AC 17 ms
8,340 KB
testcase_19 AC 16 ms
8,468 KB
testcase_20 AC 17 ms
8,412 KB
testcase_21 AC 16 ms
8,424 KB
testcase_22 AC 17 ms
8,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
def lcm(x,y):
    return x*y//gcd(x,y)

x1,y1=map(int,input().split())
x2,y2=map(int,input().split())
x3,y3=map(int,input().split())

# 拡張ユークリッドの互除法.ax+by=gcd(a,b)となる(x,y)を一つ求め、(x,y)とgcd(x,y)を返す.
def Ext_Euc(a,b,axy=(1,0),bxy=(0,1)): # axy=a*1+b*0,bxy=a*0+b*1なので,a,bに対応する係数の初期値は(1,0),(0,1)
    q,r=divmod(a,b)

    if r==0:
        return bxy,b # a*bxy[0]+b*bxy[1]=b
   
    rxy=(axy[0]-bxy[0]*q,axy[1]-bxy[1]*q) # rに対応する係数を求める.
    return Ext_Euc(b,r,bxy,rxy)

# 中国剰余定理(拡張ユークリッドの互除法を使う)
def Chirem(a,ma,b,mb): # N=a mod ma,N=b mod mbのときN=k mod(lcm(ma,mb))なるk,lcm(ma,mb)を返す.
    (p,q),d=Ext_Euc(ma,mb)
    if (a-b)%d!=0:
        return -1 # 解がないとき-1を出力
    return (b*ma*p+a*mb*q)//d%(ma*mb//d),ma*mb//d

k=Chirem(x1,y1,x2,y2)

if k==-1:
    print(k)
    exit()

else:
    x4,y4=k

k=Chirem(x4,y4,x3,y3)

if k==-1:
    print(k)
else:
    if k[0]==0:
        print(lcm(lcm(y1,y2),y3))
        

    else:
        print(k[0])
0