数位和AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)For integers $b (b \geq 2)$ and $n (n \geq 1)$, let the function $f(b,n)$ be defined as follows:
对于整数 $b (b \geq 2)$ 和 $n (n \geq 1)$,定义函数 $f(b,n)$ 如下:AI (CI翻译)
$f(b,n) = n$, when$n < b$$f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b)$, when$n \geq b$
- 当
$n < b$时,$f(b,n) = n$ - 当
$n \geq b$时,$f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b)$
Here, ${\rm floor}(n / b)$ denotes the largest integer not exceeding $n / b$, and $n \ {\rm mod} \ b$ denotes the remainder of $n$ divided by $b$.
这里,${\rm floor}(n / b)$ 表示不超过 $n / b$ 的最大整数,$n \ {\rm mod} \ b$ 表示 $n$ 除以 $b$ 的余数。AI (CI翻译)
Less formally, $f(b,n)$ is equal to the sum of the digits of $n$ written in base $b$. For example, the following hold:
通俗地说,$f(b,n)$ 等于 $n$ 的 $b$ 进制表示中各数位之和。例如,有:AI (CI翻译)
$f(10,\,87654)=8+7+6+5+4=30$$f(100,\,87654)=8+76+54=138$
$f(10,\,87654)=8+7+6+5+4=30$$f(100,\,87654)=8+76+54=138$
You are given integers $n$ and $s$. Determine if there exists an integer $b (b \geq 2)$ such that $f(b,n)=s$. If the answer is positive, also find the smallest such $b$.
给定整数 $n$ 和 $s$。请判断是否存在整数 $b (b \geq 2)$,使得 $f(b,n)=s$。如果存在,还需要求出满足条件的最小的 $b$。AI (CI翻译)
Constraints
约束条件
AI (CI翻译)$1 \leq n \leq 10^{11}$$1 \leq s \leq 10^{11}$$n,\,s$are integers.
$1 \leq n \leq 10^{11}$$1 \leq s \leq 10^{11}$$n,\,s$均为整数。
Input
输入格式
AI (CI翻译)The input is given from Standard Input in the following format:
输入通过标准输入以如下格式给出:AI (CI翻译)
n
s
n
sAI (CI翻译)
Output
输出格式
AI (CI翻译)If there exists an integer $b (b \geq 2)$ such that $f(b,n)=s$, print the smallest such $b$. If such $b$ does not exist, print -1 instead.
如果存在整数 $b (b \geq 2)$,使得 $f(b,n)=s$,输出满足条件的最小的 $b$。如果不存在这样的 $b$,则输出 -1。AI (CI翻译)
样例
87654
30
10
87654
138
100
87654
45678
-1
31415926535
1
31415926535
1
31415926535
-1
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。