标签:mes 最小值 long arc rom win lld 并且 class
For integers b(b≥2) and n(n≥1), let the function f(b,n) be defined as follows:
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:
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.
The input is given from Standard Input in the following format:
n
s
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.
87654
30
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\);
#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;
}
标签:mes 最小值 long arc rom win lld 并且 class
原文地址:https://www.cnblogs.com/sciorz/p/9062775.html