No.1047 Zero (Novice)
タグ : / 解いたユーザー数 380
作問者 :
stoq
/ テスター :
trineutron
Problem Statement
S-kun has an integer $N$. Initially, $N=0$.
S-kun performs the following operation any number of times, at least once.
・Replace $N$ with $AN+B$.
Can $N=0$ hold again after some number of operations?
If it can, output the minimum number of operations; otherwise, output -1.
Input
$A\ B$
$A,B$ are integers.
$-10^9 \leq A,B \leq 10^9$
Output
If $N=0$ can hold again after some number of operations, output the minimum number of operations; otherwise, output -1.
Print a newline at the end.
Sample
Sample 1
Input
-1 2
Output
2
The first operation gives $N=-1 \cdot 0 +2 = 2$,
and the second operation gives $N=-1 \cdot 2 +2 = 0$.
Therefore, the minimum number of operations is 2.
Sample 2
Input
10 10
Output
-1
$N=0$ never holds again.
Sample 3
Input
1 0
Output
1
The first operation gives $N=1 \cdot 0 +0 = 0$.
Therefore, the minimum number of operations is 1.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。