首页 > ACM题库 > HDU-杭电 > HDU 4282-A very hard mathematic problem-分治-[解题报告]HOJ
2015
05-23

HDU 4282-A very hard mathematic problem-分治-[解题报告]HOJ

A very hard mathematic problem

问题描述 :

  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.

输入:

  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.
  

输出:

  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.
  

样例输入:

9
53
6
0

样例输出:

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

A very hard mathematic problem

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

题意: X^Z + Y^Z + XYZ = K, (X < Y, Z > 1)。告诉你最后K(0
< K < 2^31)
的值,问符合这个等式的X,Y,Z的组合有多少种

题解:因为幂次的增长速度比较快,我们可以枚举X和Z,然后二分搜索是否存在对应的Y。对于A的B次方我们可以先打表储存,之后只需调用即可。

代码:

#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
LL mat[50001][32]={0};
bool binarySearch(int x,int z,int cnt)
{
    int l=x+1,r=50000,mid;
    for(;l<=r;)
    {
        mid=(l+r)>>1;
        if(mat[mid][z]==0)
        {
            r=mid-1;
            continue;
        }
        if(mat[mid][z]+x*mid*z<cnt)
           l=mid+1;
        else if(mat[mid][z]+x*mid*z>cnt)
           r=mid-1;
        else
            return true;
    }
    return false;
}
int main()
{
    int k;
    for(int i=1;i<=50000;++i)
    {
        mat[i][1]=i;
        for(int j=2;j<=31;++j)
        {
            mat[i][j]=mat[i][j-1]*i;
            if(mat[i][j]>2147483648LL) break;//这个2147483648LL中的LL一定要加上

        }
    }
    for(;scanf("%d",&k),k;)
    {
        long long summ=0;
        int cnt;
        for(int x=1;x<=50000&&x<=k;++x)
            for(int z=2;z<=31;++z)
            {
                if(mat[x][z]==0) break;
                cnt=k-mat[x][z];
                if(cnt-x*z<=0) break;
                if(binarySearch(x,z,cnt))
                    summ++;
            }
        printf("%I64d\n",summ);
    }
    return 0;
}

来源:http://blog.csdn.net/acm_ted/article/details/7960779

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/acm_ted/article/details/7960779