A very hard mathematic problem (HDU 4282) acm

(二分查找)外层两个循环,内层直接对x进行二分查找,关键在判定,如果直接用pow,一直超时,自己写一个POW,终于AC

#include <stdio.h>

#include <stdlib.h>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <string.h>
#include <limits.h>
#include <iostream>
#include <math.h>
using namespace std;
int POW(int i,int j)
{
int result=1;
for(int m=0;m<j;m++)
  result*=i;
return result;
}
int main()
{
int y,z,k;
int sum;
while (scanf("%d",&k))
{
sum=0;
if(k==0)break;
for(z=2;z<31;z++)
for(y=2;y<((int)(pow((double)k,1.0/z))+1);y++)
{
int left=1;
int right=min(y-1,((int)(pow((double)k-pow((double)y,z),1.0/z)))+1);
while (left<=right)
{
int middle=(left+right)/2;
if(((POW(y,z)+POW(middle,z)+middle*z*y)==k))
{
sum++;
break;
}
else if(((POW(y,z)+POW(middle,z)+middle*z*y)>k))
{
right=middle-1;
}
else
{
left=middle+1;
}
}
}
printf("%d\n",sum);
}
return 0;
}

你可能感兴趣的:(A very hard mathematic problem (HDU 4282) acm)