第 044 场 AtCoder Beginner Contest
D
数位和
Digit Sum
AtCoder
⌀ 2261
原题
← C→

数位和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)$
AI (CI翻译)

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$
AI (CI翻译)

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$ 均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入通过标准输入以如下格式给出:AI (CI翻译)

n
s

n
s
AI (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翻译)

样例

Input #1
87654
30
Output #1
10
Input #2
87654
138
Output #2
100
Input #3
87654
45678
Output #3
-1
Input #4
31415926535
1
Output #4
31415926535
Input #5
1
31415926535
Output #5
-1
题目信息
题号
arc060_b
时间限制
2 s
内存限制
256 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。