問題一覧 > 通常問題

No.25 Terminating Decimal

レベル : / AI推定難易度 : 1000 / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 208
作問者 : yuki2006
お気に入りにしたユーザー ProblemId : 70 / 自分の提出
問題文最終更新日: 2026-06-25 01:02:01
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 25 (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。