HDU - 1496 Equations (hash)

题意:

多组测试数据。

每组数据有一个方程 a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0,方程中四个未知数 x1, x2, x3, x4 ∈ [-100, 100], 且都不为0。

给定a, b, c, d ∈ [-50, 50] ,且都不为0, 求上述条件下方程解的个数。

比赛的时候想到的是枚举三个,另一个可以直接算出来。但是一直TLE。。。结果就没做出来。

看了题解,用的hash,很巧妙。结果自己用map写还是T。。最后用数组写的。     _φ(❐_❐✧ 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define maxn 1000000

int hsh[2*maxn+10];
int main()
{
    int a, b, c, d;
    while(~scanf("%d%d%d%d", &a, &b, &c, &d))
    {
        int ans = 0;
        memset(hsh, 0, sizeof(hsh));

        for (int x1 = 1; x1 <= 100; x1++)
        for (int x2 = 1; x2 <= 100; x2++)
            hsh[maxn + x1*x1*a + x2*x2*b]++;

        for (int x3 = 1; x3 <= 100; x3++)
        for (int x4 = 1; x4 <= 100; x4++)
            ans += hsh[maxn - (x3*x3*c + x4*x4*d)];

        printf("%d\n", ans*16);
    }
}

猜你喜欢

转载自www.cnblogs.com/ruthank/p/9426769.html