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
- **=============================================**
- ||快速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;
- }
**=============================================** ||快速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; }
(转)
- #include <stdio.h>
- #include <math.h>
- __int64 k;
- //其实我也试着写了一个pow。。只不过弱爆了。。不够快。。
- /*
- __int64 myqpow(__int64 x, __int64 y)
- {
- if (y == 1)
- {
- return x;
- }
- __int64 tmp = qpow(x, y/2);
- if (y%2 == 1)
- {
- return (tmp * tmp * x);
- }
- else
- {
- return (tmp * tmp);
- }
- }
- */
- __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;
- }
- __int64 solve(__int64 x, __int64 z)
- {
- __int64 l = x + 1, r = 32768, y = (l + r) >> 1;//r = k - qpow(x, z)
- while (l <= r)
- {
- __int64 tmp = x * y * z + qpow(x, z) + qpow(y, z);
- if (tmp == k)
- {
- return y ;
- }
- else if (tmp > k || tmp < 0)
- {
- r = y - 1;
- }
- else
- {
- l = y + 1;
- }
- y = (l + r) >> 1;
- }
- /*
- if (x * y * z + qpow(x, z) + qpow(y, z) == k)
- {
- return y ;
- }
- */
- return 0 ;
- }
- int main()
- {
- while (scanf("%I64d", &k), k)
- {
- __int64 i, j, ans=0;
- for (i = 2; i <= 31; i++)
- {
- for (j = 1; ; j++)
- {
- __int64 tmp1 = qpow(j, i);
- if (tmp1*2 > k || tmp1 < 0)
- {
- break ;
- }
- // __int64 a = solve(j, i);
- if (solve(j, i))
- {
- ans++;
- // printf("%I64d %I64d %I64d\n", j, a, i);
- }
- }
- }
- printf("%I64d\n", ans);
- }
- return 0;
- }