結果

問題 No.141 魔法少女コバ
コンテスト
ユーザー Hachimori
提出日時 2015-02-01 23:33:53
言語 C++11
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 5,000 ms
+ 588µs
コード長 542 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 314 ms
コンパイル使用メモリ 73,188 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-27 16:02:13
合計ジャッジ時間 4,044 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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