結果

問題 No.25 有限小数
ユーザー リチウム
提出日時 2014-11-17 21:41:32
言語 Java
(openjdk 23)
結果
AC  
実行時間 141 ms / 5,000 ms
コード長 1,038 bytes
コンパイル時間 2,437 ms
コンパイル使用メモリ 77,376 KB
実行使用メモリ 55,736 KB
最終ジャッジ日時 2024-11-15 21:07:15
合計ジャッジ時間 7,583 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	static long GCD(long a, long b) { //最大公約数
		if (a % b == 0 || b % a == 0) {
			return Math.min(a, b);
		} else {
			return GCD(a % b, b % a);
		}
	}
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong();
		long m=sc.nextLong();
		long g=GCD(n,m);
		n/=g;
		m/=g;
		if(m==1){
			while(n%10==0){
				n/=10;
			}
			System.out.println(n%10);
			return;
		}
		int two=0;
		int five=0;
		boolean next=false;
		long c=m;
		do{
			next=false;
			if(c%2==0){
				c/=2;
				two++;
				next=true;
			}
			if(c%5==0){
				c/=5;
				five++;
				next=true;
			}
		}while(next);
		
		int N=(int)(n%10);
		
		if(c!=1 || (two==0 && five==0)){
			System.out.println(-1);
			return;
		}
		else if(two>0 && five>0)System.out.println(N);
		
		else if(two>0)System.out.println(5);
		
		else{
			int x=0;
			while(x<five){
				N*=2;
				N%=10;
				x++;
			}
			System.out.println(N);
		}
		
		
	}}
0