結果

問題 No.25 有限小数
ユーザー htensai
提出日時 2020-01-15 09:31:28
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,374 bytes
コンパイル時間 2,637 ms
コンパイル使用メモリ 79,248 KB
実行使用メモリ 110,900 KB
最終ジャッジ日時 2024-12-31 06:14:41
合計ジャッジ時間 90,720 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 TLE * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long m = sc.nextLong();
        if (n % m == 0) {
            System.out.println(getLastNum(n / m));
            return;
        }
        n %= m;
        HashSet<Long> set = new HashSet<>();
        while (true) {
            if (set.contains(n)) {
                System.out.println(-1);
                return;
            }
            if (n >= Long.MAX_VALUE / 10) {
               BigInteger nn = BigInteger.valueOf(n).multiply(BigInteger.TEN);
               BigInteger mod = nn.mod(BigInteger.valueOf(m));
                if (mod.equals(BigInteger.ZERO)) {
                    System.out.println(getLastNum(nn.divide(BigInteger.valueOf(m)).longValue()));
                    return;
                }
                n = mod.longValue();
            } else {
                n *= 10;
                if (n % m == 0) {
                    System.out.println(getLastNum(n / m));
                    return;
                }
                n %= m;
            }
        }
    }
    
    static long getLastNum(long x) {
        while (x > 0) {
            if (x % 10 != 0) {
                return x % 10;
            }
            x /= 10;
        }
        return 0;
    }
}
0