2015
05-23

# A very hard mathematic problem

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.

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.

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.

9
53
6
0

1
1
0

Hint
9 = 1^2 + 2^2 + 1 * 2 * 2
53 = 2^3 + 3^3 + 2 * 3 * 3


A very hard mathematic problem

< K < 2^31)

#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
LL mat[50001][32]={0};
bool binarySearch(int x,int z,int cnt)
{
int l=x+1,r=50000,mid;
for(;l<=r;)
{
mid=(l+r)>>1;
if(mat[mid][z]==0)
{
r=mid-1;
continue;
}
if(mat[mid][z]+x*mid*z<cnt)
l=mid+1;
else if(mat[mid][z]+x*mid*z>cnt)
r=mid-1;
else
return true;
}
return false;
}
int main()
{
int k;
for(int i=1;i<=50000;++i)
{
mat[i][1]=i;
for(int j=2;j<=31;++j)
{
mat[i][j]=mat[i][j-1]*i;
if(mat[i][j]>2147483648LL) break;//这个2147483648LL中的LL一定要加上

}
}
for(;scanf("%d",&k),k;)
{
long long summ=0;
int cnt;
for(int x=1;x<=50000&&x<=k;++x)
for(int z=2;z<=31;++z)
{
if(mat[x][z]==0) break;
cnt=k-mat[x][z];
if(cnt-x*z<=0) break;
if(binarySearch(x,z,cnt))
summ++;
}
printf("%I64d\n",summ);
}
return 0;
}