No.25 Terminating Decimal
タグ : / 解いたユーザー数 208
作問者 :
Problem Statement
peach wants to know whether a number given as a fraction can be represented as a terminating decimal.
A "terminating decimal" is a number represented by a decimal with finitely many digits, such as \(1 / 8 = 0.125\).
A number such as \(4 / 3 = 1.33333\dots\) does not qualify, because it cannot be represented by a decimal with finitely many digits.
You are given \(2\) natural numbers \((N,M)\), each representable as a \(64\)-bit signed integer.
Determine whether \(N/M\) can be represented as a terminating decimal.
If it can be represented as a terminating decimal, output the digit at the lowest place that is not \(0\).
If it cannot be represented as a terminating decimal, output \(-1\).
Even when the value does not become a non-integer decimal, find the digit at the lowest place that is not \(0\).
Input
N M
Line \(1\) contains the natural number \(N\ (1 \leq N \leq 9,223,372,036,854,775,807)\), the numerator of the fraction.
Line \(2\) contains the natural number \(M\ (1 \leq M \leq 9,223,372,036,854,775,807)\), the denominator of the fraction.
Output
If it can be represented as a terminating decimal, output the last digit that is not \(0\).
If it cannot be represented as a terminating decimal, output \(-1\).
Print a newline at the end.
Sample
Sample 1
Input
7 5
Output
4
\(7 / 5 = 1.4\), which can be represented as a terminating decimal, so the last digit is \(4\).
Sample 2
Input
2 3
Output
-1
\(2/3\) cannot be represented as a terminating decimal, so the answer is \(-1\).
Sample 3
Input
10 3
Output
-1
Sample 4
Input
20 2
Output
1
\(20/2\) equals \(10\), and looking from the lowest place, the last digit that is not \(0\) is this one, so output \(1\).
Sample 5
Input
12345 30517578125
Output
6
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。