結果

問題 No.141 魔法少女コバ
ユーザー maguroguma
提出日時 2017-09-23 22:00:09
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 612 bytes
コンパイル時間 351 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-11-14 04:50:59
合計ジャッジ時間 5,253 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

import math

M,N = map(int, input().split())
gcd_mn = math.gcd(M, N)
M //= gcd_mn
N //= gcd_mn

ans = 0
# MとNが共に1,もしくは片方が負数となった時点でループを抜ける
while True:
    if (M==1 and N==1) or M<0 or N<0:
        break
    if M<N:
        temp = M
        M = N
        N = temp
        ans += 1
    else:
        # M<NとなるようにMをnN減じる
        n = M//N
        if M%N==0:
            M -= (n-1)*N
            ans += (n-1)
        else:
            M -= n*N
            ans += n

if M==1 and N==1:
    print(ans)
else:
    print(-1)
0