結果

問題 No.25 有限小数
ユーザー htensaihtensai
提出日時 2020-01-15 09:31:28
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,374 bytes
コンパイル時間 2,071 ms
コンパイル使用メモリ 74,608 KB
実行使用メモリ 63,136 KB
最終ジャッジ日時 2023-08-30 01:35:13
合計ジャッジ時間 9,116 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
56,424 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

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