A very hard mathematic problem HDU - 4282(二分)

Haoren is very good at solving mathematic problems. Today he is working a problem like this:
  Find three positive integers X, Y and Z (X < Y, Z > 1) that holds
   X^Z + Y^Z + XYZ = K
  where K is another given integer.
  Here the operator “^” means power, e.g., 2^3 = 2 * 2 * 2.
  Finding a solution is quite easy to Haoren. Now he wants to challenge more: What’s the total number of different solutions?
  Surprisingly, he is unable to solve this one. It seems that it’s really a very hard mathematic problem.
  Now, it’s your turn.
Input
  There are multiple test cases.
  For each case, there is only one integer K (0 < K < 2^31) in a line.
  K = 0 implies the end of input.
  
Output
  Output the total number of solutions in a line for each test case.
Sample Input
9
53
6
0
Sample Output
1
1
0

Hint
9 = 1^2 + 2^2 + 1 * 2 * 2
53 = 2^3 + 3^3 + 2 * 3 * 3

题意:
有多少个 x , y , z x,y,z 满足 xz + yz + x y z xyz = k k
思路:
k k < 231
1 ≤ x ≤ y ,z ≥ 2
那么易得 z z ≤ 31
于是可以枚举z,然后等式两边开z次方根。得到一个大于x,y的边界值
再枚举x,此时x,z,k都是已知的,就可以二分y判断是否存在这一一个y。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;
ll k;

ll Pow(ll a,ll b)
{
    ll res = 1;
    for(ll i = 1;i <= b;i++)
    {
        res *= a;
    }
    return res;
}

ll cal(ll x,ll y,ll z)
{
    return Pow(x,z) + Pow(y,z) + x * y * z - k;
}

bool judge(ll x,ll maxy,ll z)
{
    ll l = x + 1,r = maxy;
    while(l <= r)
    {
        ll mid = (l + r) >> 1;
        ll tmp = cal(x,mid,z);
        if(tmp == 0)return true;
        else if(tmp < 0)
        {
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }
    return false;
}

int main()
{
    while(~scanf("%lld",&k) && k)
    {
        int ans = 0;
        for(ll z = 2;z <= 31;z++)
        {
            ll maxy = pow(k,1.0/z);
            for(ll x = 1;x <= maxy;x++)
            {
                if(judge(x,maxy,z))ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

发布了676 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104261311