hdu 4282 A very hard mathematic problem

枚举Y和Z,确定X的范围,然后二分查找X就行。。。但由于二分查找写错,把队友坑了。。。对于二分答案来说,形如if(ok(m)) r=m; else l=m+1;的,一般while里面是(l<r);而对于if(ok(m)) r=m-1; else l=m+1;while里面则是(l <= r) 。。。。。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>

using namespace std;
#define intt long long
intt x, y, z, k, sqr, ans;

intt poww(intt a, intt b)
{
    intt ret = 1;
    for(intt i=1; i<=b; i++)
        ret *= a;
    return ret;
}

intt calc(intt x, intt y, intt z)
{
    return poww(x, z) + poww(y, z) + x * y * z;
}

int main()
{
    while(cin>>k, k)
    {
        sqr = sqrt((double) k);
        ans = 0;
        if(sqr * sqr == k) ans = (sqr-1) / 2;   //z为2的时候,k为平方数的情况
        for(z = 3; z <= 32; z ++)
        {
            for(y = 2; y <= (1 << (31 / z + 1)); y++)
            {
                intt l = 1, r = y, m;
                bool flag = 0;
                while(l <= r)
                {
                    m = (l + r) >> 1;
                    intt t = calc(m, y, z);
                    if(t == k)
                    {
                        flag = 1;
                        break;
                    }
                    else if(t > k)  r = m - 1;
                    else    l = m + 1;
                }
                if(flag && m < y)    ans++;
            }
        }
        cout<<ans<<endl;
    }
}


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