2012天津赛区网络赛第五题---A very hard mathematic problem(hdu4284)

来源:互联网 发布:《人生》知乎 编辑:程序博客网 时间:2024/05/16 17:36

      题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4282

       比赛的时候,思路就是暴搜,但队友做出来了,就做别的去了,之后看题解,大多数都是使用二分的方法解决的,但暴搜也是可以过的,先贴上暴搜的代码,有时间间再写写二分。

      暴搜的时候注意Z==2 时,要特殊处理,否则会超时的。

代码:

#include<stdio.h>#include<math.h>int K;int x,y,z;int ans;int main(){while(scanf("%d",&K)!=EOF && K!= 0){ans = 0;int t = (int)sqrt(K*1.0);if(t*t == K)ans += (t-1)/2;for(z=3;z<31;z++){for(x=1;;x++){__int64 temp = (__int64)pow(x*1.0,z*1.0);if(temp > K/2)break;for(y=x+1;;y++){  __int64  temp1 = (__int64)pow(y*1.0,z*1.0);                  if(temp1 + temp + x*y*z > K)  break;  if(temp1 + temp + x*y*z == K)  {  ans ++;  break;  }}}}printf("%d\n",ans);}return 0;}


 

 

原创粉丝点击