HDU 4282 A very hard mathematic problem(二分查找)

题目链接

网络赛啊。。。知道了算法还错了N次,托了N天,开始的代码+上一句话就AC了,TLE,我以为二分写搓了呢。。。

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cmath>

 4 #include <cstring>

 5 #include <cstdlib>

 6 #define ll __int64

 7 ll f(int x,int z)

 8 {

 9     int i;

10     ll ans = 1;

11     for(i = 1;i <= z;i ++)

12     {

13         ans *= x;

14         if(ans > 2147483647)//这里会越界,而且加上这句话效率也会大大提高。

15         return 2147483647;

16     }

17     return ans;

18 }

19 int main()

20 {

21     int x,z,n,k,num,str,end,mid;

22     ll sv,ss;

23     while(scanf("%d",&k)!=EOF)

24     {

25         if(k == 0) break;

26         n = (int)sqrt(k+0.5);

27         num = 0;

28         for(x = 1;x <= n;x ++)

29         {

30             for(z = 2;z <= 30;z ++)

31             {

32                 ss = f(x,z);

33                 sv = x*z;

34                 if(ss + sv> k)

35                 break;

36                 str = x+1;

37                 end = n;

38                 while(str < end)

39                 {

40                     mid = (end-str)/2+str;

41                     if(ss+f(mid,z)+sv*mid < k)

42                     {

43                         str = mid+1;

44                     }

45                     else

46                     {

47                         end = mid;

48                     }

49                 }

50                 if(ss+f(str,z)+sv*str == k)

51                 {

52                     num ++;

53                 }

54             }

55         }

56         printf("%d\n",num);

57     }

58     return 0;

59 }

你可能感兴趣的:(Math)