HDU 4282 A very hard mathematic problem

来源:互联网 发布:柴鸡蛋还有哪些网络剧 编辑:程序博客网 时间:2024/06/05 08:29

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

import java.util.Scanner;public class Main {    final static int maxn = 5*10000+5;    static long[][] mat = new long[maxn][32];    public static void main(String[] args) {        /*         暴力超时的时候怎么办呢,一般用二分解;        long k;        Scanner in = new Scanner(System.in);        k = in.nextLong();        int count = 0;        for (long x = 1; x <= k; x++)        {            for (long y = x+1; y <= k; y++)            {                for (long z = 2; z <= 31; z++)                {                    if (Math.pow(x, z)+Math.pow(y, z)+x*y*z == k)                        count++;                }            }        }        System.out.println(count);        */        /*         * 二分的话,可以想到先枚举x,z,吧x^z所有的进行打表存储,再用二分去搜满足条件的y,如果满足,就count++         */        //打表存储        for (int x = 1; x <= 50000; x++)        {            mat[x][1] = x;//第一维存的是数,第二维存的是次方数,即x^z            for (int z = 2; z <= 31; z++)            {                mat[x][z] = mat[x][z-1]*x;                if (mat[x][z] > 2147483648L)                {                    break;                }            }        }        Scanner in = new Scanner(System.in);        long k;        while (in.hasNext())        {            k = in.nextLong();            if (k == 0) break;            int count = 0;            long cnt;            for (int x = 1; x <= 50000; 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 (ok(x, z, cnt))                    {                        count++;                    }                }            }            System.out.println(count);        }    }    private static boolean ok(int x, int z, long cnt) {        // TODO Auto-generated method stub        // cnt == k-mat[x][z]        int l = x+1, r = 50000, mid;        while (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;    }}
0 0
原创粉丝点击