結果

問題 No.25 有限小数
コンテスト
ユーザー uafr_cs
提出日時 2017-11-11 10:14:45
言語 Java
(openjdk 26.0.2.1 + ACL)
コンパイル:
javac -J-Duser.language=en -encoding UTF8 -cp /opt/aclib/ac_library.jar _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true -cp .:/opt/aclib/ac_library.jar _class_
結果
AC  
実行時間 1,761 ms / 5,000 ms
+ 54µs
コード長 1,096 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,551 ms
コンパイル使用メモリ 88,632 KB
実行使用メモリ 66,132 KB
最終ジャッジ日時 2026-09-29 10:01:39
合計ジャッジ時間 16,948 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static long MOD = 1000000007;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		BigInteger N = new BigInteger(sc.next());
		BigInteger M = new BigInteger(sc.next());
		
		BigInteger answer = BigInteger.ZERO;
		
		HashSet<BigInteger> already = new HashSet<BigInteger>();
		
		boolean found = false;
		for(int tt = 0; tt <= 100000; tt++){
			//System.out.println(N + "/" + M);
			answer = answer.multiply(BigInteger.TEN);
			answer = answer.add(N.divide(M));
			
			final BigInteger mod = N.mod(M);
			
			if(mod.equals(BigInteger.ZERO)){
				found = true;
				break;
			}
			
			if(already.contains(mod)){
				System.out.println(-1);
				return;
			}
			
			already.add(mod);
			N = mod.multiply(BigInteger.TEN);
		}
		
		if(!found){ System.out.println(-1); return; }
		
		final String output = answer.toString().replace("0", "");
		System.out.println(output.charAt(output.length() - 1));
	}
}
0