![](http://acm.fzu.edu.cn/image/problem.gif)
Accept: 51 Submit: 483
Time Limit: 1000 mSec Memory Limit : 32768 KB
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
4
4 10
110 10
15 2
432 13
Sample Output
-1
10
3
18
/*
注:未提交
思路:枚举(0<=x<=sqrt(n))
*/
#include<stdio.h>
#include<math.h>
__int64 add(__int64 x,__int64 m){
__int64 sum=0;
while(x){
sum+=x%m;
x/=m;
}
sum+=x%m;
return sum;
}
int main()
{
__int64 t;
scanf("%d",&t);
while(t--){
__int64 n,m;
scanf("%I64d %I64d",&n,&m);
for(__int64 x=0;x<=(__int64)sqrt(n);x++){
__int64 sum=add(x,m);
if(x*x+sum*x-n==0)
if(x>0){
printf("%I64d\n",x);
break;
}
}
if(x>sqrt(n))
printf("-1\n");
}
return 0;
}