hdu 4282 A very hard mathematic problem(二分)

  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;
}

你可能感兴趣的:(hdu 4282 A very hard mathematic problem(二分))