#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\n",ans); } return 0; }
原文:http://blog.csdn.net/libin56842/article/details/41978505