A very hard mathematic problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1685 Accepted Submission(s): 510
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
比赛的时候想着数论找规律,终究没找到规律,暴力超时,明明是解方程,二分,却想不起来,越来越脑残了!!
View Code
1 #include<cmath> 2 #include<cstdio> 3 int n,zz; 4 int q_pow(int a, int k) 5 { 6 if(k==0) return 1; 7 if(k==1) return a; 8 if(k%2==0) return q_pow(a*a,k/2); 9 if(k%2==1) return a*q_pow(a,k-1); 10 } 11 int find(int low,int high,int j,int i) 12 { 13 int mid,res; 14 while(low<=high) 15 { 16 mid=(high+low)>>1; 17 res=zz+pow(mid,i)+i*j*mid-n; 18 if(res==0) return mid; 19 if(res<0) low=mid+1; 20 else high=mid-1; 21 } 22 return -1; 23 } 24 int main() 25 { 26 while(scanf("%d",&n)!=EOF&&n) 27 { 28 int res=0; 29 for(int i=2;i<=30;i++) 30 { 31 int _max=(int)(pow(n/2.0,1.0/i)+1)+1; 32 for(int j=1;j<=_max;j++) 33 { 34 if(q_pow(j,i)+q_pow(j+1,i)+j*(j+1)>n) break; 35 zz=(int)q_pow(j,i); 36 int now=find(j+1,(int)(pow(n+0.0,1.0/i)),j,i); 37 if (now>0)res++; 38 } 39 } 40 printf("%d\n",res); 41 } 42 }
pow函数还是自己定义吧……否则会WA,再WA……
Source