結果

問題 No.25 有限小数
ユーザー data9824data9824
提出日時 2015-06-14 03:05:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,924 bytes
コンパイル時間 595 ms
コンパイル使用メモリ 68,916 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-09 22:54:20
合計ジャッジ時間 1,734 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 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,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 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,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:74:10: warning: ‘result’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  cout << result << endl;
          ^~~~~~

ソースコード

diff #

#include <iostream>
#include <map>

using namespace std;

long long gcd(long long x, long long y) {
	do {
		if (x < y) {
			swap(x, y);
		}
		x = x % y;
	} while (x > 0LL);
	return y;
}

int getLastDigit(long long x) {
	while (x % 10 == 0 && x > 0) {
		x /= 10;
	}
	x %= 10;
	return (int)x;
}

int multiply(long long x, long long y) {
	while (x > 0) {
		long long digit = (x % 10) * y;
		if (digit % 10 != 0) {
			return digit % 10;
		}
		x /= 10;
		x += digit / 10;
	}
	return 0;
}

int main() {
	long long n, m;
	cin >> n >> m;
	int result;
	if (n % m == 0LL) {
		result = getLastDigit(n / m);
	} else {
		long long divisor = gcd(n, m);
		long long numerator = n / divisor;
		long long denominator = m / divisor;
		map<long long, long long> numeratorFactors;
		long long numeratorRemaining = numerator;
		for (; numeratorRemaining % 2 == 0 && numeratorRemaining > 1; numeratorRemaining /= 2) {
			++numeratorFactors[2];
		}
		for (; numeratorRemaining % 5 == 0 && numeratorRemaining > 1; numeratorRemaining /= 5) {
			++numeratorFactors[5];
		}
		map<long long, long long> denominatorFactors;
		long long denominatorRemaining = denominator;
		for (; denominatorRemaining % 2 == 0 && denominatorRemaining > 1; denominatorRemaining /= 2) {
			++denominatorFactors[2];
		}
		for (; denominatorRemaining % 5 == 0 && denominatorRemaining > 1; denominatorRemaining /= 5) {
			++denominatorFactors[5];
		}
		if (denominatorRemaining > 1) {
			result = -1;
		} else if (denominatorFactors[2] == denominatorFactors[5]) {
			result = getLastDigit(1 * numerator);
		} else if (denominatorFactors[2] > denominatorFactors[5]) {
			result = multiply(numerator, 5);
		} else if (denominatorFactors[2] < denominatorFactors[5]) {
			long long factor5 = denominatorFactors[5] - denominatorFactors[2];
			const int digit5[] = { 6, 2, 4, 8 };
			result = multiply(numerator, digit5[factor5 % 4]);
		}
	}
	cout << result << endl;
	return 0;
}
0