Problem Description
如今有一个等式例如以下:x^2+s(x,m)x-n=0。
当中s(x,m)表示把x写成m进制时,每一个位数相加的和。如今,在给定n,m的情况下,求出满足等式的最小的正整数x。假设不存在,请输出-1。
Input
有T组測试数据。
下面有T(T<=100)行,每行代表一组測试数据。每一个測试数据有n(1<=n<=10^18),m(2<=m<=16)。
Output
输出T行,有1个数字。满足等式的最小的正整数x。假设不存在,请输出-1。
Sample Input
44 10110 1015 2432 13
Sample Output
-110318
我们能够知道。x绝对是小于等于根号n的
那么仅仅要枚举就能够了
可是一般的枚举是要超时的
可是我们能够发现s(x,m)必定有一个范围,能够作为跳出条件
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
int main()
{
__int64 t,n,m,ans;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&m);
__int64 x = sqrt(n*1.0);
ans = -1;
while(x)
{
if(n%x==0)
{
__int64 sum = 0,tem = x;
while(tem)
{
sum+=tem%m;
tem/=m;
}
if(sum == n/x-x)
{
ans = x;
}
}
if(n/x-x>90)
break;
x--;
}
printf("%I64d
",ans);
}
return 0;
}