【整数哈希】 poj1840 Eqs

题目来源:poj 1840

参考:https://blog.csdn.net/lyy289065406/article/details/6647387

#include 
#include 
#include 
using namespace std;
short hashTable[25000001]; //hash[sum]表示值等于sum的解的个数
int main()
{
    int a1, a2, a3, a4, a5;
    while (scanf("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5) == 5)
    {
        memset(hashTable, 0, sizeof(hashTable));
        for (int x1 = -50; x1 <= 50; x1++)
        {
            if (!x1)
                continue;

            for (int x2 = -50; x2 <= 50; x2++)
            {
                if (!x2)
                    continue;
                int sum = (a1 * x1 * x1 * x1 + a2 * x2 * x2 * x2) * (-1);
                if (sum < 0)
                    sum += 25000000;
                hashTable[sum]++;
            }
        }

        int solution = 0;

        for (int x3 = -50; x3 <= 50; x3++)
        {
            if (!x3)
                continue;
            for (int x4 = -50; x4 <= 50; x4++)
            {
                if (!x4)
                    continue;
                for (int x5 = -50; x5 <= 50; x5++)
                {
                    if (!x5)
                        continue;
                    int sum = a3 * x3 * x3 * x3 + a4 * x4 * x4 * x4 + a5 * x5 * x5 * x5;
                    if (sum < 0)
                        sum += 25000000;
                    if (hashTable[sum])
                        solution += hashTable[sum];
                }
            }
        }
        cout << solution << endl;
    }
    return 0;
}

你可能感兴趣的:(基础算法)