結果

問題 No.141 魔法少女コバ
ユーザー Hachimori
提出日時 2015-02-01 23:33:53
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 542 bytes
コンパイル時間 647 ms
コンパイル使用メモリ 56,660 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-23 05:32:57
合計ジャッジ時間 2,563 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;


int M, N;

void read() {
    cin >> M >> N;
}


int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}


void work() {
    int div = gcd(M, N);
    M /= div;
    N /= div;

    int cnt = 0;
    while (M > 1 || N > 1) {
        if (M > N) {
            cnt += N == 1 ? M - 1 : M / N;
            M %= N;
        }
        else {
            ++cnt;
            swap(M, N);
        }
    }

    cout << cnt << endl;
}


int main() {
    read();
    work();
    return 0;
}
0