結果

問題 No.25 有限小数
ユーザー H3PO4H3PO4
提出日時 2023-02-20 10:42:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 907 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 88,376 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-28 09:49:46
合計ジャッジ時間 2,231 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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