結果

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

ソースコード

diff #

#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define INF 1000000000

using namespace std;
typedef long long ll;

int main(void) {
    ll M, N;
    cin >> M >> N;
    for (int i = 2; i*i <= N; i++) {
        if (M % i == 0 && N % i == 0) {
            while (N % i == 0 && M % i == 0) {
                M /= i;
                N /= i;
            }
        }
    }
    ll num = 0;
    while (1) {
        if (M == N) {
            if (M == 1) break;
            cout << -1 << endl;
            return 0;
        }
        if (M > N) {
            ll k = M/N;
            if (k != 1) {
                M -= (k-1)*N;
                num += k-1;
            }
            else {
                M -= N;
                num++;
            }
        } else {
            swap(M, N);
            num++;
        }
    }
    cout << num << endl;
    return 0;
}
0