結果

問題 No.25 有限小数
ユーザー mbanmban
提出日時 2017-05-08 21:39:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 829 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 69,188 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 19:11:24
合計ジャッジ時間 2,920 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

long long GCD(long long a, long long b) {
	if (a < b) {
		swap(a, b);
	}
	long long r = a%b;
	while (r != 0)
	{
		a = b;
		b = r;
		r = a%b;
	}
	return b;
}

int main() {
	long long N, M;
	cin >> N;
	cin >> M;
	long long gcd = GCD(N, M);
	N /= gcd;
	M /= gcd;
	if (N%M == 0) {
		N /= M;
		while (N%10==0)
		{
			N /= 10;
		}
		cout << N % 10 << endl;
		return 0;
	}
	N %= 10;
	int two = 0;
	int five = 0;
	while (M % 2 == 0)
	{
		M /= 2;
		two++;
	}
	while (M % 5 == 0)
	{
		M /= 5;
		five++;
	}
	if (M != 1) {
		cout << -1 << endl;
		return 0;
	}
	if (two > five) {
		for (int i = 0; i < two - five; i++) {
			N *= 5;
			N %= 10;
		}
	} else {
		for (int i = 0; i < five - two; i++) {
			N *= 2;
			N %= 10;
		}
	}
	cout << N << endl;
	return 0;
}

0