E - A very hard mathematic problem题解

                                                                E - A very hard mathematic problem

Time Limit:1000MS     Memory Limit:32768KB     64bitIO Format:%I64d & %I64u

Description

  Haoren is very good at solving mathematic problems.Today he is working a problem like this: 
  Find threepositive integers X, Y and Z (X < Y, Z > 1) that holds
   X^Z + Y^Z + XYZ= K
  where K isanother given integer.
  Here the operator“^” means power, e.g., 2^3 = 2 * 2 * 2.
  Finding asolution is quite easy to Haoren. Now he wants to challenge more: What’s thetotal number of different solutions?
  Surprisingly, heis unable to solve this one. It seems that it’s really a very hard mathematicproblem.
  Now, it’s yourturn.

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 theend of input.
  

Output

  Output the total number of solutions in a line foreach 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


这是一道求不定方程解的个数的题(不定方程是数论中最古老的分支之一)。分析如下:

X^Z + Y^Z + XYZ= K,( 0< X < Y, Z > 1)

易得y>=2, z>=2,   xyz>=4, x^z>=1,        2^z <= k.

当z=2时,原方程退化为(x+y)^2=k.

当z >= 3时 2<=y<=(k-4-1)^(1/z) 且 x <= (y-1).

联立几个方程,容易写出以下代码:

#include"stdio.h"
#include"math.h"
int main()
{
	int k,zmax,xmax,ymax,t,c,i,j,h;
	double d1,d2;
	while(scanf("%d",&k)!=EOF&&k)
	{
		if(k<9)
		{
			printf("0\n");
			continue;
		}

		zmax=-1;
		t=k;
		while(t)
		{
			t<<=1;
			zmax++;
		}
		c=0;
		d1=sqrt(1.0*k);
		t=(int)d1;
		if(t*t==k)
		{
			c+=(t-1)/2;
		}
		for(i=3;i<=zmax;i++)
		{
			ymax=(int)pow(1.0*(k-5),1.0/i);
			for(j=2;j<=ymax;j++)
			{
				d2=k-pow(1.0*j,i);
				xmax=(int)pow(1.0*(d2-4),1.0/i);
				xmax=(xmax>=j? (j-1):xmax); 
				for(h=1;h<=xmax;h++)
				{
					if((int)(pow(h,i)+pow(j,i))==(k-i*j*h))
						c++;
				}


			}
		}
		printf("%d\n",c);
		
	}
	return 0;
}

yinjili         E       Accepted  288KB      203 ms       C++  638 B

你可能感兴趣的:(数论,summer,不定方程,Training,NUC)