Problem 2125 简单的等式
Accept: 96 Submit: 375
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
Source
福州大学第十届程序设计竞赛// 因为 s(x,m) 不是很大,所以我们可以枚举 s(x,m) ,
// 然后二次方程求根,最后判断是不是对的
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#define maxn 50002
#define LL long long
using namespace std ;
int get( LL a , LL m )
{
int ans = 0 ;
while(a)
{
ans += a%m ;
a /= m ;
}
return ans ;
}
int main()
{
int i ,k ,j ;
LL n , m , ans ;
// freopen("in.txt","r",stdin) ;
cin >> k ;
while( k-- )
{
cin >> n >> m ;
for( i = 0 ; i < 900 ;i++ )
{
ans = (LL)((sqrt(i*i+4*n)-i)/2) ;
if(ans*ans+get(ans,m)*ans-n == 0 ) break ;
}
if( i == 900 ) puts("-1") ;
else cout << ans << endl;
}
return 0 ;
}