Problem Statement
For integers b(b≥2) and n(n≥1), let the function f(b,n) be defined as follows:
- f(b,n)=n, when n<b
- f(b,n)=f(b, floor(n⁄b))+(n mod b), when n≥b
Here, floor(n⁄b) denotes the largest integer not exceeding n⁄b, and n mod b denotes the remainder of n divided by b.
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(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≥2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.
Constraints
- 1≤n≤1011
- 1≤s≤1011
- n, s are integers.
Input
The input is given from Standard Input in the following format:
n
s
Output
If there exists an integer b(b≥2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print -1
instead.
Sample Input 1
87654
30
Sample Output 1
10
题意
给定一个\(n\)和一个\(s\),找到一个最小的\(b\)使得\(n\)在\(b\)进制下各位之和为\(s\).
分析
假设\(n\)表示为\(b\)进制之后各位上的数字从低位到高位分别为\(a_0,a_1,a_2,...\),显然我们可以得到如下两个等式
\[ a_0+a_1b+a_2b^2+a_3b^3+...=n \tag{1}\]
\[ a_0+a_1+a_2+a_3+... =s \tag{2}\]
并且对于\(\forall i \in [0,+\infty]\)都有\(a_i<b \bigwedge a_i>=0\);
- 当(1)式中的最高次幂至少二次时,\(b\leq \sqrt{n}\),所以我们可以枚举\(b\)的值从\(2\)到\(\sqrt n\),每次求出\(b\)进制下各位数字之和看是否等于\(s\)
- 当(1)式中的最高次幂为1次时,有\[ \left\{ \begin{aligned} a_0+a_1b=n \\ a_0+a_1=s\\ \end{aligned} \right. \]化简后有\(a_1(b-1)=n-s\),所以\(a_1\)和\(b-1\)均为\(n-s\)的因数.所以我们可以枚举所有\(n-s\)的因数,并取出所有满足条件的\(b\)的最小值.
- 当最高次幂为1次时,必须要满足\(n=s\).
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
ll n,s;
int main(int argc, char const *argv[])
{
scanf("%lld%lld", &n,&s);
ll b=1;
for (b = 2; b*b <= n; ++b)
{
ll t=n;
ll sum=0;
while(t){
sum+=t%b;
t/=b;
}
if(sum==s){
printf("%lld\n", b);
return 0;
}
}
if(n<s){
printf("-1\n");
return 0;
}
else if(n==s){
printf("%lld\n", n+1);
return 0;
}
ll i=1;
for (i = 1; i*i <= n-s; ++i)
{
if((n-s)%i==0){
ll b=i+1;
ll a1=(n-s)/i,a0=s-a1;
if(a1<b&&a0<b&&a0>=0){
printf("%lld\n", b);
return 0;
}
}
}
for ( ; i >= 1; --i)
{
if((n-s)%i==0){
ll j=(n-s)/i;
ll b=j+1;
ll a1=(n-s)/j,a0=s-a1;
if(a1<b&&a0<b&&a0>=0){
printf("%lld\n", b);
return 0;
}
}
}
printf("-1\n");
return 0;
}