結果

問題 No.25 有限小数
ユーザー yuki2006yuki2006
提出日時 2014-10-31 13:53:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 1,911 bytes
コンパイル時間 3,566 ms
コンパイル使用メモリ 77,184 KB
実行使用メモリ 51,996 KB
最終ジャッジ日時 2024-04-27 17:06:51
合計ジャッジ時間 8,530 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
51,996 KB
testcase_01 AC 129 ms
41,352 KB
testcase_02 AC 115 ms
41,268 KB
testcase_03 AC 117 ms
41,520 KB
testcase_04 AC 131 ms
41,656 KB
testcase_05 AC 131 ms
41,264 KB
testcase_06 AC 127 ms
41,300 KB
testcase_07 AC 128 ms
41,788 KB
testcase_08 AC 130 ms
41,332 KB
testcase_09 AC 115 ms
41,348 KB
testcase_10 AC 114 ms
40,152 KB
testcase_11 AC 130 ms
41,784 KB
testcase_12 AC 129 ms
41,412 KB
testcase_13 AC 129 ms
41,112 KB
testcase_14 AC 127 ms
41,204 KB
testcase_15 AC 128 ms
41,492 KB
testcase_16 AC 128 ms
41,660 KB
testcase_17 AC 131 ms
41,264 KB
testcase_18 AC 129 ms
41,292 KB
testcase_19 AC 133 ms
41,156 KB
testcase_20 AC 132 ms
41,148 KB
testcase_21 AC 129 ms
41,436 KB
testcase_22 AC 130 ms
41,564 KB
testcase_23 AC 127 ms
41,156 KB
testcase_24 AC 116 ms
40,016 KB
testcase_25 AC 129 ms
41,408 KB
testcase_26 AC 130 ms
40,940 KB
testcase_27 AC 129 ms
41,192 KB
testcase_28 AC 129 ms
41,472 KB
testcase_29 AC 116 ms
40,024 KB
testcase_30 AC 127 ms
41,200 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