HDU - 4282 A very hard mathematic problem

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

题意:给你一个整数k,问你x^z+y^z+z*x*y=k的解有多少种,其中y>x>0,k<2^31;

1.因为x最小为1,且y>x,所以y最小为2;

2.因为x,y>0,且k<2^31,所以z<=30;

3.当z=2时,x^2+y^2+2*x*y=(x+y)^2=k;也就是说当z=2时式子有解时,k是一个完全平方数,也就是求y>x时(x+y)^2=k的解法有多少种;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
long long pow(long long x,int y)
{
    long long ans=1;
    for(int i=1;i<=y;i++)
        ans*=x;
    return ans;
}
int main()
{
    int k;
    while(~scanf("%d",&k))
    {
        if(k==0)
            break;
        long long sum=0;
        long long tx,ty;
        long long t=(int)sqrt(k*1.0);//当z为2时;
        if(t*t==k)//判断是不是k是不是完全平方数;
            sum+=(t-1)/2;
        for(int z=3;z<31;z++)//z最小从3开始
        {
            for(long long x=1; ;x++)
            {
                tx=pow(x,z);
                if(tx>=k/2)
                    break;
                for(long long y=x+1; ;y++)
                {
                    ty=pow(y,z);
                    if(tx+ty+y*x*z>k)//大于k的时候结束;
                        break;
                    else if(tx+ty+x*y*z==k)
                    {
                        sum++;
                        break;//记得break;
                    }
                }
            }
        }
        printf("%lld\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41925919/article/details/80273718