• HDU 1496 Equations(哈希表)


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

    【题目大意】

      给出一个方程ax1^2+bx2^2+cx3^2+dx4^2=0,求-100到100范围内的解集数量

    【题解】

      将ax1^2+bx2^2存入哈希,反查-cx3^2-dx4^2.

    【代码】

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int mod=1<<15;
    int n,a,b,c,d,x[200],head[mod],cnt;
    struct data{int x,nxt,s;}g[mod];
    long long ans;
    inline int Hash(int x){return (x+mod)&(mod-1);}
    void insert(int x){
        int key=Hash(x);
        for(int i=head[key];i!=-1;i=g[i].nxt){
            if(g[i].x==x){g[i].s++;return;}
        }g[cnt].s=1; g[cnt].x=x;
        g[cnt].nxt=head[key]; head[key]=cnt++;
    }
    int search(int x){
        int key=Hash(x);
        for(int i=head[key];i!=-1;i=g[i].nxt){
            if(g[i].x==x)return g[i].s;
        }return 0;
    }
    void init(){cnt=0;memset(head,-1,sizeof(head));ans=0;}
    int main(){
        for(int i=1;i<=100;i++)x[i]=i*i;
        while(~scanf("%d%d%d%d",&a,&b,&c,&d)){
            if(a*b>0&&b*c>0&&c*d>0){puts("0");continue;}init();
            for(int i=1;i<=100;i++)for(int j=1;j<=100;j++)insert(x[i]*a+x[j]*b);
            for(int i=1;i<=100;i++)for(int j=1;j<=100;j++)ans+=search(-x[i]*c-x[j]*d);
            printf("%lld
    ",ans<<4);
        }return 0;
    }
    

      

  • 相关阅读:
    Spring定时
    随记生成验证码
    缅怀过去
    java运行可以执行文件
    随 记
    TLD生成
    IT鸟的回忆录
    梦之物语
    VINSMono
    WIDOWX 250 6DOF
  • 原文地址:https://www.cnblogs.com/forever97/p/hdu1496.html
Copyright © 2020-2023  润新知