結果
問題 | No.25 有限小数 |
ユーザー | data9824 |
提出日時 | 2015-06-14 03:05:04 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,924 bytes |
コンパイル時間 | 612 ms |
コンパイル使用メモリ | 68,692 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-04-27 17:10:37 |
合計ジャッジ時間 | 1,567 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,940 KB |
testcase_14 | AC | 1 ms
6,940 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,944 KB |
testcase_18 | AC | 2 ms
6,944 KB |
testcase_19 | AC | 2 ms
6,940 KB |
testcase_20 | AC | 2 ms
6,940 KB |
testcase_21 | AC | 2 ms
6,944 KB |
testcase_22 | AC | 2 ms
6,940 KB |
testcase_23 | AC | 2 ms
6,944 KB |
testcase_24 | AC | 2 ms
6,940 KB |
testcase_25 | AC | 1 ms
6,944 KB |
testcase_26 | AC | 2 ms
6,944 KB |
testcase_27 | AC | 2 ms
6,940 KB |
testcase_28 | AC | 2 ms
6,940 KB |
testcase_29 | AC | 1 ms
6,944 KB |
testcase_30 | AC | 2 ms
6,940 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:74:17: warning: ‘result’ may be used uninitialized in this function [-Wmaybe-uninitialized] 74 | cout << result << endl; | ^~~~~~
ソースコード
#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; }