线性探查法

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

这个方法好快哦。

View Code
#include "iostream"
#include "string"
using namespace std;
#define MAX 40000
typedef struct hash{
    int key; //乘积
    int num; //乘积个数
}hash;

hash h[MAX];

int Line(int k){ //线性探查法
    int d = k%MAX;
    if(d<0){
        d+=MAX;
    }
    while(h[d].num && h[d].key!=k){
        d = (d+1)%MAX;
    }
    return d;
}

int main(){
    int a, b, c, d;
    while(cin>>a>>b>>c>>d){
        if(a>0 && b>0 && c>0 && d>0){
            cout<<"0"<<endl;
            continue;
        }
        if(a<0 && b<0 && c<0 && d<0){
            cout<<"0"<<endl;
            continue;
        }
        int tmp, p;
        memset(h, 0, sizeof(h));
        for(int i=1; i<=100; i++){
            for(int j=1; j<=100; j++){
                tmp = a*i*i+b*j*j;
                p = Line(tmp);
                h[p].key = tmp;
                h[p].num++;
            }
        }

        int Ans = 0;
        for(int i=1; i<=100; i++){
            for(int j=1; j<=100; j++){
                tmp = -(c*i*i+d*j*j);
                p = Line(tmp);
                Ans+=h[p].num;
            }
        }
        cout<<(Ans<<4)<<endl;
    }
}

转载于:https://www.cnblogs.com/o8le/archive/2012/05/20/2510746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值