POJ 1840 Eqs (大整数的Hash)

题目链接: http://poj.org/problem?id=1840


题目大意: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0  xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}. 


解题思路: 将等式转化为 -(a1x13+ a2x23) = a3x33+ a4x43+ a5x5这样就直接减少了时间复杂度  

将等式左边值设为Hash的下标,为了不为负,左右两边都加上一个50*50^3*3 = 18750000 所以要加一个比它大的数,我这里用的是25000000


#include<cstdio>
#include<cstring>
const int maxn = 25000001;
short Hash[maxn];
int l = -50, r = 50;
int main ()
{
    int a1,a2,a3,a4,a5;
    while(scanf("%d %d %d %d %d", &a1, &a2, &a3, &a4, &a5) != EOF)
    {
        memset(Hash, 0, sizeof(Hash));

        for(int x1 = l; x1 <= r; x1++)
        {
            if(!x1) continue;
            for(int x2 = l; x2 <= r; x2++)
            {
                if(!x2) continue;

                int sum = a1*x1*x1*x1 + a2*x2*x2*x2;
                if(sum < 0) sum += 25000000;
                Hash[sum]++;
            }
        }

        int ans = 0;
        for(int x3 = l; x3 <= r; x3++)
        {
            if(!x3) continue;
            for(int x4 = l; x4 <= r; x4++)
            {
                if(!x4) continue;
                for(int x5 = l; x5 <= r; x5++)
                {
                    if(!x5) continue;
                    int sum = a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
                    if(sum < 0) sum += 25000000;
                    ans += Hash[sum];
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}



你可能感兴趣的:(hash)