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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章