hdu4282

A very hard mathematic problem

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

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
 
  1. **=============================================**  
  2. ||快速pow(多次使用时及其有效)               ||  
  3. **=============================================**  
  4. __int64 qpow(int a, int b){  
  5.     __int64 c, d; c = 1; d = a;  
  6.     while (b > 0){  
  7.         if (b & 1)  
  8.           c *= d;  
  9.         b = b >> 1;  
  10.         d = d * d;  
  11.     }  
  12.     return c;  
  13. }  
  14. **=============================================**  
  15. ||快速1/sqrt(x)(牛顿迭代法)                    ||  
  16. **=============================================**  
  17. float InvSqrt (float x) {  
  18.      float xhalf = 0.5f * x;  
  19.      long i = *(long*)&x;  
  20.      i = 0x5f3759df - (i >> 1);  
  21.      x = *(float *)&i;  
  22.      x = x * (1.5f - xhalf * x * x);  
  23.      return x;  
  24. }  
**=============================================**
||快速pow(多次使用时及其有效)               ||
**=============================================**
__int64 qpow(int a, int b){
    __int64 c, d; c = 1; d = a;
    while (b > 0){
        if (b & 1)
          c *= d;
        b = b >> 1;
        d = d * d;
    }
    return c;
}
**=============================================**
||快速1/sqrt(x)(牛顿迭代法)                    ||
**=============================================**
float InvSqrt (float x) {
     float xhalf = 0.5f * x;
     long i = *(long*)&x;
     i = 0x5f3759df - (i >> 1);
     x = *(float *)&i;
     x = x * (1.5f - xhalf * x * x);
     return x;
}

 

(转)

 

  1. #include <stdio.h>   
  2. #include <math.h>   
  3.   
  4. __int64 k;  
  5. //其实我也试着写了一个pow。。只不过弱爆了。。不够快。。   
  6. /* 
  7. __int64 myqpow(__int64 x, __int64 y) 
  8. { 
  9.     if (y == 1) 
  10.     { 
  11.         return x; 
  12.     } 
  13.     __int64 tmp = qpow(x, y/2); 
  14.     if (y%2 == 1) 
  15.     { 
  16.         return (tmp * tmp * x); 
  17.     } 
  18.     else 
  19.     { 
  20.         return (tmp * tmp); 
  21.     } 
  22. } 
  23. */  
  24.   
  25. __int64 qpow(int a, int b)  
  26. {  
  27.     __int64 c, d; c = 1; d = a;  
  28.     while (b > 0)  
  29.     {  
  30.         if (b & 1)  
  31.           c *= d;  
  32.         b = b >> 1;  
  33.         d = d * d;  
  34.     }  
  35.     return c;  
  36. }  
  37.   
  38. __int64 solve(__int64 x, __int64 z)  
  39. {  
  40.     __int64 l = x + 1, r = 32768, y = (l + r) >> 1;//r = k - qpow(x, z)   
  41.     while (l <= r)  
  42.     {  
  43.         __int64 tmp = x * y * z + qpow(x, z) + qpow(y, z);  
  44.         if (tmp == k)  
  45.         {  
  46.             return y ;  
  47.         }  
  48.         else if (tmp > k || tmp < 0)  
  49.         {  
  50.             r = y - 1;  
  51.         }  
  52.         else  
  53.         {  
  54.             l = y + 1;  
  55.         }  
  56.         y = (l + r) >> 1;  
  57.     }  
  58. /* 
  59.     if (x * y * z + qpow(x, z) + qpow(y, z) == k) 
  60.     { 
  61.         return y ; 
  62.     } 
  63. */  
  64.     return 0 ;  
  65. }  
  66.   
  67. int main()  
  68. {  
  69.     while (scanf("%I64d", &k), k)      
  70.     {  
  71.         __int64 i, j, ans=0;  
  72.         for (i = 2; i <= 31; i++)  
  73.         {  
  74.             for (j = 1;  ; j++)  
  75.             {  
  76.                 __int64 tmp1 = qpow(j, i);  
  77.                 if (tmp1*2 > k || tmp1 < 0)  
  78.                 {  
  79.                     break ;  
  80.                 }  
  81.   
  82. //                __int64 a = solve(j, i);   
  83.                 if (solve(j, i))  
  84.                 {  
  85.                     ans++;  
  86. //                    printf("%I64d %I64d %I64d\n", j, a, i);   
  87.                 }  
  88.             }  
  89.         }  
  90.         printf("%I64d\n", ans);  
  91.     }  
  92.     return 0;  
  93. }  
posted @ 2013-05-22 18:40  哥的笑百度不到  阅读(145)  评论(0编辑  收藏  举报