HDU 1496 Equations(大整数的hash)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1496

题目解析: 类似如POJ 1840 不过注意特判0的情况,这种情况很多,不加会超时。

#include
#include
#include
using namespace std;
const int maxn = 1200000;
int main ()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int a, b, c, d;
    int l = -100, r = 100;
    while(scanf("%d %d %d %d",&a, &b, &c, &d) != EOF)
    {
        if(a>0 && b>0 && c>0 && d>0 || a<0 && b<0 && c<0 && d<0)  
        {  
            printf("0\n");  
            continue;  
        } 
        
        mapMap;

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

                int sum = maxn + -1*(a*x1*x1 + b*x2*x2);

                Map[sum]++;
            }
        }

        int ans = 0;
        for(int x3 = l; x3 <= r; x3++)
        {
            if(!x3) continue;
            for(int x4 = l; x4 <= r; x4++)
            {
                if(!x4) continue;

                int sum = maxn + c*x3*x3 + d*x4*x4;
                ans += Map[sum];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(数据结构-Hash)