D - 桁和 / Digit Sum
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 500500 points
For integers b(b≥2)b(b≥2) and n(n≥1)n(n≥1), let the function f(b,n)f(b,n) be defined as follows:
Here, floor(n/b)floor(n/b) denotes the largest integer not exceeding n/bn/b, and n mod bn mod b denotes the remainder of nn divided by bb.
Less formally, f(b,n)f(b,n) is equal to the sum of the digits of nn written in base bb. For example, the following hold:
You are given integers nn and ss. Determine if there exists an integer b(b≥2)b(b≥2) such that f(b,n)=sf(b,n)=s. If the answer is positive, also find the smallest such bb.
The input is given from Standard Input in the following format:
nn ss
If there exists an integer b(b≥2)b(b≥2) such that f(b,n)=sf(b,n)=s, print the smallest such bb. If such bb does not exist, print -1
instead.
Copy
87654 30
Copy
10
Copy
87654 138
Copy
100
Copy
87654 45678
Copy
-1
Copy
31415926535 1
Copy
31415926535
Copy
1 31415926535
Copy
-1
给出n和s求一个进制b使得n在b进制下各位数之和为s
详细思路:
第一种情况
x0+x1*b^1+x2*b^2+...=n
x0+x1+x2+...=s
这个时候b^2<=n
所以b<=根号n
这个时候暴力枚举即可
第三种情况暴力枚举
x0+x1*b^1=n;
x0+x1=s;
这个时候联立二式子
得到x1*(b-1)=n-s
我们枚举x1,然后判断是否成立,包括题目条件以及进制常识
#include
using namespace std;
#define ll long long
ll n,s;
bool check(ll base)
{
ll tmp=n;
ll ans=0;
while(tmp)
{
ans+=tmp%base;
tmp/=base;
}
return ans==s;
}
bool check(ll i,ll sum)
{
ll a1=s-i;//x0
ll a2=sum/i+1;//b
if(a1>=0&&a1=1;i--)
{
if(!(sum%i)&&(check(i,sum)))
{
printf("%lld\n",sum/i+1);
return 0;
}
}
printf("-1\n");
return 0;
}