結果

問題 No.25 有限小数
ユーザー htensaihtensai
提出日時 2020-01-15 09:32:54
言語 Java19
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,398 bytes
コンパイル時間 3,222 ms
コンパイル使用メモリ 74,648 KB
実行使用メモリ 411,944 KB
最終ジャッジ日時 2023-08-30 01:39:26
合計ジャッジ時間 13,866 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,240 KB
testcase_01 AC 122 ms
56,124 KB
testcase_02 AC 126 ms
55,944 KB
testcase_03 AC 121 ms
55,780 KB
testcase_04 AC 120 ms
55,732 KB
testcase_05 AC 121 ms
55,764 KB
testcase_06 AC 122 ms
55,976 KB
testcase_07 AC 120 ms
55,748 KB
testcase_08 AC 121 ms
55,792 KB
testcase_09 AC 122 ms
55,992 KB
testcase_10 AC 120 ms
55,588 KB
testcase_11 AC 123 ms
56,136 KB
testcase_12 AC 122 ms
56,020 KB
testcase_13 AC 120 ms
56,548 KB
testcase_14 AC 120 ms
55,768 KB
testcase_15 AC 123 ms
55,852 KB
testcase_16 AC 120 ms
55,852 KB
testcase_17 AC 122 ms
55,936 KB
testcase_18 AC 124 ms
56,104 KB
testcase_19 TLE -
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;
            }
            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