题意:给出一个整数K,问满足等式X^Z + Y^Z + XYZ = K的不同(X,Y,Z)有多少个。 (X < Y, Z > 1),(0 < K < 2^31)
分析:有已知可得:Z的范围为2-30,所以可以,枚举Z和X,二分Y。(题目数据略坑。。。)
Code:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int inf=0x3f3f3f3f;
LL POW(LL x,LL y){
LL ans=1;
while(y--) ans*=x;
return ans;
}
int main()
{
LL k;
while(scanf("%I64d",&k),k){
LL cnt=0;
LL tmp=(LL)sqrt((double)k);
if(tmp*tmp==k) cnt+=(tmp-1)/2;//一个优化。这里tmp一定要减一啊。。
for(LL z=3;z<31;z++){
for(LL x=1;;x++){
if(POW(x,z)>=(k/2)) break;
LL l=x+1,r=k;
while(l<=r){
LL y=(l+r)/2;
LL ans=POW(x,z)+POW(y,z)+x*y*z;
if(ans==k) {cnt++;break;}
if(ans>k||ans<0) r=y-1;//计算结果可能溢出64位,这里还要判断ans是否小于0!
else l=y+1;
}
}
}
cout<<cnt<<endl;
}
return 0;
}