結果

問題 No.25 有限小数
ユーザー tottoripaper
提出日時 2014-11-16 20:16:42
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 860 bytes
コンパイル時間 380 ms
コンパイル使用メモリ 43,460 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-15 21:07:05
合計ジャッジ時間 1,246 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:24:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   24 |     scanf("%lld %lld", &N, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#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