結果

問題 No.25 有限小数
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-09 23:18:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 123 ms / 5,000 ms
コード長 1,179 bytes
コンパイル時間 3,507 ms
コンパイル使用メモリ 73,908 KB
実行使用メモリ 56,452 KB
最終ジャッジ日時 2023-08-19 06:19:25
合計ジャッジ時間 8,189 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
56,060 KB
testcase_01 AC 106 ms
55,852 KB
testcase_02 AC 108 ms
56,220 KB
testcase_03 AC 112 ms
55,972 KB
testcase_04 AC 110 ms
56,044 KB
testcase_05 AC 116 ms
56,324 KB
testcase_06 AC 123 ms
55,876 KB
testcase_07 AC 110 ms
55,716 KB
testcase_08 AC 115 ms
56,332 KB
testcase_09 AC 114 ms
56,264 KB
testcase_10 AC 115 ms
55,968 KB
testcase_11 AC 111 ms
55,868 KB
testcase_12 AC 111 ms
56,020 KB
testcase_13 AC 110 ms
54,200 KB
testcase_14 AC 118 ms
56,084 KB
testcase_15 AC 114 ms
55,916 KB
testcase_16 AC 110 ms
55,776 KB
testcase_17 AC 113 ms
56,212 KB
testcase_18 AC 109 ms
55,780 KB
testcase_19 AC 113 ms
56,380 KB
testcase_20 AC 109 ms
55,556 KB
testcase_21 AC 107 ms
55,704 KB
testcase_22 AC 109 ms
56,116 KB
testcase_23 AC 108 ms
55,768 KB
testcase_24 AC 109 ms
56,452 KB
testcase_25 AC 112 ms
55,728 KB
testcase_26 AC 111 ms
55,916 KB
testcase_27 AC 114 ms
55,824 KB
testcase_28 AC 107 ms
55,704 KB
testcase_29 AC 106 ms
55,916 KB
testcase_30 AC 105 ms
55,872 KB
権限があれば一括ダウンロードができます

ソースコード

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();
        long gcd = gcd(N,M);
        N/=gcd;
        M/=gcd;
        long ans = ans(N,M);
        System.out.println(ans);
    }
    static long gcd(long x, long y){
        while(true){
            long z=x%y;
            if(z==0){break;}
            x=y;
            y=z;
        }
        return y;
    }
    static int ans(long N, long M){
        long t=M;
        while(t%2==0){
            t/=2;
        }
        while(t%5==0){
            t/=5;
        }
        int r=-1;
        if(t==1){
            BigDecimal x = new BigDecimal(N);
            BigDecimal y = new BigDecimal(M);
            BigDecimal z = x.divide(y, 1000, BigDecimal.ROUND_UNNECESSARY);
            String w = z.toPlainString();
            for(int i=w.length()-1; i>=0; i--){
                int A = w.charAt(i);
                if(A!='0'&&A!='.'){
                    r=w.charAt(i)-'0';
                    break;
                }
            }
        }
        return r;
    }
}
0