結果

問題 No.25 有限小数
ユーザー drymousedrymouse
提出日時 2024-02-17 23:11:56
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 758 ms
コンパイル使用メモリ 70,496 KB
実行使用メモリ 10,024 KB
最終ジャッジ日時 2024-09-28 23:56:30
合計ジャッジ時間 7,171 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 TLE * 1 -- * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

long long long_gcd(long long a, long long b) {
    if (a < b) {
        long long x = a;
        a = b; b = x;
    }
    long long r;
    for (;b > 0;){
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int last_dig(long long a) {
    if (a == 0) {return 0;}
    for (;a % 10 == 0;) {
        a /= 10;
    }
    return a % 10;
}

int main(void) {
    long long N, M;
    cin >> N >> M;
    long long daiyaku = long_gcd(N, M);
    long long nshou = N / daiyaku;
    long long mshou = M / daiyaku;
    if (mshou == 1) {
        cout << last_dig(nshou) << endl;
        return 0;
    }
    for (long long i = 3; i < mshou + 1; i+=2) {
        if (i % 5 == 0) {
            continue;;
        } else if (mshou % i == 0) {
            cout << -1 << endl;
            return 0;
        }
    }
    long long digit = nshou % mshou;
    for (;mshou % 2 == 0;) {
        digit *= 5;
        mshou /= 2;

    }
    digit = last_dig(digit);
    for (;mshou % 5 == 0;) {
        digit <<= 1;
        mshou /= 5;
    }
    digit = last_dig(digit);
    cout << digit << endl;
    
    return 0;
}
0