結果

問題 No.25 有限小数
ユーザー htensai
提出日時 2020-01-15 09:32:54
言語 Java
(openjdk 23)
結果
MLE  
実行時間 -
コード長 1,398 bytes
コンパイル時間 2,581 ms
コンパイル使用メモリ 79,364 KB
実行使用メモリ 527,612 KB
最終ジャッジ日時 2024-12-31 06:22:34
合計ジャッジ時間 53,986 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22 TLE * 7 MLE * 2
権限があれば一括ダウンロードができます

ソースコード

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;
            }
            set.add(n);
            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