結果

問題 No.25 有限小数
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-30 17:31:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,990 ms / 5,000 ms
コード長 1,204 bytes
コンパイル時間 530 ms
コンパイル使用メモリ 68,364 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-06 18:22:58
合計ジャッジ時間 7,677 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 559 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 4,990 ms
4,376 KB
testcase_26 AC 313 ms
4,380 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <map>
using namespace std;

typedef long long ll;

ll GCD(ll a, ll b) {
    if (b == 0) return a;
    else return GCD(b, a % b);
}

map<ll, int> PrimeFactorization(ll n) {
    map<ll, int> res;
    for (ll i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            res[i]++;
            n /= i;
        }
    }
    if (n != 1) res[n]++;
    return res;
}

int main() {
    ll N, M, gcd;
    cin >> N >> M;

    gcd = GCD(N, M);
    N /= gcd;
    M /= gcd;

    map<ll, int> factors = PrimeFactorization(M);
    int exp2 = 0, exp5 = 0;
    for (auto it = factors.begin(); it != factors.end(); it++) {
        if (it->first == 2) {
            exp2 = it->second;
        } else if (it->first == 5) {
            exp5 = it->second;
        } else {
            cout << -1 << endl;
            return 0;
        }
    }
    // 確実に下一桁が0以外の数字にするために
    while (N % 10 == 0) {
        N /= 10;
    }
    for (int i = 0; i < max(exp2, exp5) - exp2; i++) {
        N = (N * 2) % 10;
    }
    for (int i = 0; i < max(exp2, exp5) - exp5; i++) {
        N = (N * 5) % 10;
    }
    cout << (N % 10) << endl;

    return 0;
}
0