結果

問題 No.141 魔法少女コバ
コンテスト
ユーザー Hachimori
提出日時 2015-02-01 23:33:53
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 542 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 669 ms
コンパイル使用メモリ 74,232 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-07 20:44:50
合計ジャッジ時間 2,165 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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