码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 1496 Equations (hash)

时间:2018-08-05 19:37:57      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:\n   include   测试数据   string   一个   cstring   bubuko   数组   ios   

题意:

多组测试数据。

每组数据有一个方程 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);
    }
}

 

HDU - 1496 Equations (hash)

标签:\n   include   测试数据   string   一个   cstring   bubuko   数组   ios   

原文地址:https://www.cnblogs.com/ruthank/p/9426769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!