Haoren is very good at solving mathematic problems. Today he is working a problem like this:
Find three positive integers X, Y and Z (X < Y, Z > 1) that holds
X^Z + Y^Z + XYZ = K
where K is another given integer.
Here the operator “^” means power, e.g., 2^3 = 2 * 2 * 2.
Finding a solution is quite easy to Haoren. Now he wants to challenge more: What’s the total number of different solutions?
Surprisingly, he is unable to solve this one. It seems that it’s really a very hard mathematic problem.
Now, it’s your turn.
Input
There are multiple test cases.
For each case, there is only one integer K (0 < K < 2^31) in a line.
K = 0 implies the end of input.
Output
Output the total number of solutions in a line for each test case.
Sample Input
9
53
6
0
Sample Output
1
1
0
Hint
9 = 1^2 + 2^2 + 1 * 2 * 2
53 = 2^3 + 3^3 + 2 * 3 * 3
http://acm.hdu.edu.cn/showproblem.php?pid=4282
题意:给定 K ,求满足 X^Z + Y^Z + XYZ = K 这个等式的解。
分析:f(x)=x^b + a*b*x + a^b - k;枚举a 和b,二分找 x (x的范围[a+1,pow(k,1/b)]);
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int k,maxa;
inline int f(int a,int b,int x)
{
return pow(x*1.0,b*1.0)+pow(a*1.0,b*1.0)+a*b*x-k;
}
bool judge(int a,int b)
{
int L,R,M,t;
L=a+1;R=maxa;
if(f(a,b,L)>0||f(a,b,R)<0) return false;
while(L<=R)
{
M=(L+R)>>1;
t=f(a,b,M);
if(t==0) return true;
else if(t>0) R=M-1;
else L=M+1;
}
return false;
}
int main()
{
int ans,a,b;
while(scanf("%d",&k)==1&&k)
{
ans=0;
for(b=2;b<=30;b++)
{
maxa=pow(k*1.0,1.0/b);
for(a=1;a<=maxa;a++)
{
if(judge(a,b))
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
本文介绍了一个复杂的数学问题:寻找三个正整数X、Y、Z,使得X^Z + Y^Z + XYZ等于给定的整数K。通过枚举和二分查找的方法实现了对该问题的有效解决。
886

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



