結果

問題 No.141 魔法少女コバ
ユーザー aka_satana_ha
提出日時 2017-12-05 16:13:31
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 870 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 818,196 KB
最終ジャッジ日時 2024-11-28 16:47:36
合計ジャッジ時間 385,631 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 TLE * 71 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

M,N=[int(i) for i in input().split()]

#分子をM 分母をNとして
#(1) M<-M+N , N<-N
#(2) M<-N, N<-M

#互除法
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a % b)
g = gcd(M,N)
N=int(N/g)
M=int(M/g)

#幅優先探索
queue=[]
queue.append([1,1,0])   #分子, 分母, 回数
kaisuu_ans=-1
kaisuu_save=[[float("inf") for i in range(max(M,N)+1)] for j in range(max(M,N)+1)]
while(len(queue)>0):
    tmp=queue.pop(0)
    bunsuu=tmp[0:2]
    bunsi=bunsuu[0]
    bunbo=bunsuu[1]
    kaisuu=tmp[2]
    kaisuu_save[bunsi][bunbo]=kaisuu
    # print(tmp)
    if bunsuu==[M,N]:
        kaisuu_ans=kaisuu
        break
    if bunsi+bunbo<max(M,N)+1:
        if kaisuu_save[bunsi+bunbo][bunbo]>kaisuu+1:
            queue.append([bunsi+bunbo,bunbo,kaisuu+1])
    if kaisuu_save[bunbo][bunsi]>kaisuu+1:
        queue.append([bunbo,bunsi,kaisuu+1])
print(kaisuu_ans)
0