結果

問題 No.25 有限小数
ユーザー tsukiotsukio
提出日時 2016-03-23 20:03:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 927 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 52,412 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-09 22:58:54
合計ジャッジ時間 1,637 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
using namespace std;

int main() {
	long long n, m;
	cin >> n >> m;

	long long divided = n;
	long long mod = m;
	long long gcd = 0;

	while (true) {
		long long surplus = divided % mod;
		if (surplus == 0) {
			gcd = mod;
			break;
		}
		else {
			divided = mod;
			mod = surplus;
		}
	}

	long long numerator = n / gcd;
	while (numerator % 10 == 0) {
		numerator /= 10;
	}
	numerator %= 10;

	long long denominator = m / gcd;
	int pf2 = 0;
	int pf5 = 0;
	while (denominator > 1) {
		if (denominator % 2 == 0) {
			denominator /= 2;
			pf2++;
		}
		else if (denominator % 5 == 0) {
			denominator /= 5;
			pf5++;
		}
		else {
			break;
		}
	}

	if (denominator > 1) {
		cout << -1 << endl;
		return 0;
	}

	while (pf2 > pf5) {
		numerator = (numerator * 5) % 10;
		pf5++;
	}

	while (pf5 > pf2) {
		numerator = (numerator * 2) % 10;
		pf2++;
	}

	cout << numerator << endl;

	return 0;
}
0