結果

問題 No.25 有限小数
ユーザー yuki2006yuki2006
提出日時 2014-10-31 13:53:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 128 ms / 5,000 ms
コード長 1,911 bytes
コンパイル時間 3,481 ms
コンパイル使用メモリ 74,168 KB
実行使用メモリ 56,284 KB
最終ジャッジ日時 2023-08-09 22:49:53
合計ジャッジ時間 8,538 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,644 KB
testcase_01 AC 124 ms
55,516 KB
testcase_02 AC 127 ms
55,724 KB
testcase_03 AC 125 ms
55,576 KB
testcase_04 AC 125 ms
55,360 KB
testcase_05 AC 125 ms
55,412 KB
testcase_06 AC 124 ms
55,832 KB
testcase_07 AC 125 ms
56,284 KB
testcase_08 AC 122 ms
55,500 KB
testcase_09 AC 126 ms
56,112 KB
testcase_10 AC 125 ms
55,796 KB
testcase_11 AC 124 ms
55,408 KB
testcase_12 AC 124 ms
55,196 KB
testcase_13 AC 123 ms
55,600 KB
testcase_14 AC 124 ms
55,500 KB
testcase_15 AC 126 ms
55,500 KB
testcase_16 AC 128 ms
55,232 KB
testcase_17 AC 124 ms
55,536 KB
testcase_18 AC 123 ms
55,652 KB
testcase_19 AC 123 ms
55,764 KB
testcase_20 AC 122 ms
55,532 KB
testcase_21 AC 123 ms
56,192 KB
testcase_22 AC 122 ms
55,740 KB
testcase_23 AC 125 ms
55,660 KB
testcase_24 AC 125 ms
55,576 KB
testcase_25 AC 125 ms
55,516 KB
testcase_26 AC 124 ms
55,404 KB
testcase_27 AC 126 ms
55,536 KB
testcase_28 AC 124 ms
55,576 KB
testcase_29 AC 127 ms
55,488 KB
testcase_30 AC 125 ms
55,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Yuki025 {
    public static void main(String[] args) {
        Yuki025 p = new Yuki025();

    }

    Yuki025() {
        Scanner scanner = new Scanner(System.in);
        long numerator = scanner.nextLong();
        long denominator = scanner.nextLong();
        System.out.println(solve(numerator, denominator));
    }

    long gcd(long a, long b) {
        while (b > 0) {
            long c = a % b;
            a = b;
            b = c;
        }
        return a;
    }

    long pow(int v, int p, long init) {
        long ans = init % 10;
        for (int i = 0; i < p; i++) {
            ans *= v;
            ans %= 10;
        }
        return ans;
    }

    int getLastNonZero(long v) {
        long ans = v;
        while (ans % 10 == 0) {
            ans /= 10;
        }
        return (int) ans % 10;
    }

    int solve(long _numerator, long _denominator) {
        final long gcd = gcd(_numerator, _denominator);
        long numerator = _numerator / gcd;
        long denominator = _denominator / gcd;

        long num = denominator;
        if (num == 1) {
            return getLastNonZero(numerator);
        }
        int[] div = new int[6];
        int[] divs = {2, 5};
        for (int d : divs) {
            while (num > 1) {
                if (num % d > 0) {
                    break;
                }
                div[d]++;
                num /= d;
            }
        }
        if (num > 1) {
            return -1;
        }
        final int diff = Math.max(div[5], div[2]) - Math.min(div[5], div[2]);
        if (div[5] == div[2]) {
            return (int) (numerator % 10);
        } else if (div[5] < div[2]) {
            return (int) pow(5, diff, numerator);
        } else {
            return (int) pow(2, diff, numerator);
        }
    }

}
0