1、http://acm.fzu.edu.cn/problem.php?pid=2125
2、题目
从小到大枚举s(x,m)的值,解出x的值,看看是否满足x^2+s(x,m)x-n=0,如果满足则输出x。
因为1<=n<=10^18,2<=m<=16,所以sum(x,m)的值不会超过1000
sum(x,m)取值范围有待解决
简单的等式Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
4
4 10
110 10
15 2
432 13
Sample Output
-1
10
3
18
#include<stdio.h>
#include<math.h>
#define ll long long
ll s(ll x,ll m)
{
ll ans=0;
while(x)
{
ans+=x%m;
x/=m;
}
return ans;
}
int main()
{
int t;
ll n,m;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&m);
int flag=0;
ll x;
for(int i=1;i<=200;i++)
{
x=(-i+sqrt(i*i+4*n))/2;
if(x*x+s(x,m)*x-n==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("-1\n");
else
printf("%I64d\n",x);
}
return 0;
}