結果

問題 No.25 有限小数
ユーザー H3PO4H3PO4
提出日時 2023-02-20 10:40:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 911 bytes
コンパイル時間 696 ms
コンパイル使用メモリ 87,360 KB
実行使用メモリ 7,676 KB
最終ジャッジ日時 2023-09-28 09:49:43
合計ジャッジ時間 7,836 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <unordered_map>

long long solve(long long N, long long M) {
    long long g = std::gcd(M, N);
    N /= g;
    M /= g;
    while (N % 10 == 0) { N %= 10; }

    std::unordered_map<int, int> ct = {{2, 0},
                                       {5, 0}};
    for (const auto &x: {2, 5}) {
        while (M % x == 0) {
            ct.at(x)++;
            M /= x;
        }
    }
    if (M != 1) { return -1; }
    
    if (ct.at(2) <= ct.at(5)) {
        for (int i = 0; i < ct.at(5) - ct.at(2); i++) {
            N *= 2;
            if (N % 10 == 0) { N /= 10; }
        }
    } else {
        for (int i = 0; i < ct.at(2) - ct.at(5); i++) {
            N *= 5;
            if (N % 10 == 0) { N /= 10; }
        }
    }
    N %= 10;
    return N;
}

int main() {
    long long N, M;
    std::cin >> N;
    std::cin >> M;
    std::cout << solve(N, M) << std::endl;
}
0