結果

問題 No.25 有限小数
ユーザー tenten
提出日時 2020-10-22 17:00:24
言語 Java
(openjdk 23)
結果
AC  
実行時間 114 ms / 5,000 ms
コード長 1,155 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 76,888 KB
実行使用メモリ 41,344 KB
最終ジャッジ日時 2024-07-21 09:22:29
合計ジャッジ時間 5,984 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long m = sc.nextLong();
        long gcd = getGCD(n, m);
        n /= gcd;
        m /= gcd;
        long x = m;
        int twoCount = 0;
        while (x % 2 == 0){
            x /= 2;
            twoCount++;
        }
        int fiveCount = 0;
        while (x % 5 == 0) {
            x /= 5;
            fiveCount++;
        }
        if (x != 1) {
            System.out.println(-1);
            return;
        }
        while (n % 10 == 0) {
            n /= 10;
        }
        n %= 10;
        if (twoCount > fiveCount) {
            System.out.println(5);
        } else {
            fiveCount -= twoCount;
            for (int i = 0; i < fiveCount; i++) {
                n *= 10;
                n /= 5;
                n %= 10;
            }
            System.out.println(n);
        }
     }
     
     static long getGCD(long x, long y) {
         if (x % y == 0) {
             return y;
         } else {
             return getGCD(y, x % y);
         }
     }
}
0