結果

問題 No.25 有限小数
コンテスト
ユーザー tottoripaper
提出日時 2014-11-16 20:16:42
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 860 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 647 ms
コンパイル使用メモリ 62,964 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-10 21:23:04
合計ジャッジ時間 1,874 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<limits>

inline int count(long long& n, int x){
    int res = 0;
    while(n % x == 0){res += 1; n /= x;}
    return res;
}

long long expr(long long a, int n){
    long long res = 1ll;
    while(n > 0){
        if(n & 1){res = res * a % 10ll;}
        a = a * a % 10ll;
        n >>= 1;
    }
    return res;
}

int main(){
    long long N, M;
    scanf("%lld %lld", &N, &M);

    long long g = std::__gcd(N, M);
    N /= g;
    M /= g;

    int n2 = count(M, 2), n5 = count(M, 5);
    if(M != 1){
        puts("-1");
        return 0;
    }

    while(N % 10 == 0){N /= 10;}
    N %= 10;

    int a = std::max(n2, n5) - n2,
        b = std::max(n2, n5) - n5,
        c = std::min(a, b);

    printf("%lld\n", N * expr(2, a-c) * expr(5, b-c) % 10ll);
}
0