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
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
long long s(long long x,long long m)
{
long long sum=0;
while(x)
{
sum+=x%m;
x/=m;
}
return sum;
}
int main()
{
int t;
long long n,m;
cin>>t;
while(t--)
{
long long x;
scanf("%I64d%I64d",&n,&m);
int flag=0;
for(int i=1;i<=200;i++)
{
x=(long long)(sqrt(i*i+4*n)/2-i/2);
if(x*x+x*s(x,m)-n==0)
{
flag=1;
break;
}
}
/*if(flag==1)
cout<<x<<endl;
else
cout<<"-1"<<endl;*/
printf("%I64d\n",flag?x:-1);
}
return 0;
}
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
long long s(long long x,long long m)
{
long long sum=0;
while(x)
{
sum+=x%m;
x/=m;
}
return sum;
}
int main()
{
int t;
long long n,m;
cin>>t;
while(t--)
{
long long x;
scanf("%I64d%I64d",&n,&m);
int flag=0;
for(int i=1;i<=200;i++)
{
x=(long long)(sqrt(i*i+4*n)/2-i/2);
if(x*x+x*s(x,m)-n==0)
{
flag=1;
break;
}
}
/*if(flag==1)
cout<<x<<endl;
else
cout<<"-1"<<endl;*/
printf("%I64d\n",flag?x:-1);
}
return 0;
}
本文提供了一种方法来解决特定形式的数学等式,并在给定条件下找出最小的正整数解。通过编程实现,解决了如何在特定约束下找到满足等式的最小正整数的问题。
775

被折叠的 条评论
为什么被折叠?



