hdu 4282A very hard mathematic problem(枚举+二分)

A very hard mathematic problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3578    Accepted Submission(s): 1052


Problem Description
  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
 


题目大意很简单,解题思路也比较容易想的,先枚举z再枚举x然后二分y即可,时间用了200ms-。自己写的时候竟然因为用了库函数自带的pow函数然后有精度的损失,记得以前碰过这样的。一直以为是自己二分或者是范围枚举出错了。。。

题目地址:A very hard mathematic problem

AC代码:
#include
#include
#include
#include
using namespace std;

long long pow1(long long a,long long p)
{
    long long s=1;
    while(p)
    {
        if(p&1)
            s*=a;
        a*=a;
        p>>=1;
    }
    return s;
}

int main()
{
    long long k;
    long long x,y,z;
    long long res,flag;
    while(cin>>k&&k)
    {
        res=0;
        for(z=2;z<=30;z++)  //枚举z
        {
            long long tmp=pow(k/2.0,1.0/z);
            long long tmp1=pow(k*1.0,1.0/z);
            for(x=1;x<=tmp;x++)  //枚举x
            {
                flag=0;
                long long l,r;
                l=x+1,r=tmp1;
                while(l<=r)   //二分y
                {
                    y=(l+r)>>1;
                    long long s=pow1(x,z)+pow1(y,z)+x*y*z;
                    if(s>k) r=y-1;
                    else if(s



你可能感兴趣的:(枚举)