思路:x绝对小于根号n,再由s(x,m)可以缩小范围。1e9十六进制大约算出每位和相加100左右。这种题直接判断范围再暴力。
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstdlib> 6 #include<iostream> 7 #define LL long long 8 using namespace std; 9 10 int main() { 11 //freopen("in.txt","r",stdin); 12 __int64 t,n,m,ans; 13 scanf("%I64d",&t); 14 while(t--) { 15 scanf("%I64d%I64d",&n,&m); 16 __int64 x = sqrt(n*1.0); 17 ans = -1; 18 while(x) { 19 if(n%x==0) { 20 __int64 sum = 0,tem = x; 21 while(tem) { 22 sum+=tem%m; 23 tem/=m; 24 } 25 if(sum == n/x-x) { 26 ans = x; 27 } 28 } 29 if(n/x-x>112) 30 break; 31 x--; 32 } 33 printf("%I64d ",ans); 34 } 35 return 0; 36 }