HDU 1496 Equations 哈希

来源:互联网 发布:mac 删除输入法记忆 编辑:程序博客网 时间:2024/05/01 07:07

传送门:HDU 1496 Equations

分析:
打表平方数,分正负两组分别哈希。

代码如下:

#include <stdio.h>#include <string.h>const int MAXN = 50*100*100*2 + 5;int hash_pos[MAXN];int hash_neg[MAXN];int pf[101];int main() {    int a,b,c,d,i,j,cnt,tmp;    for(i=1; i<101; i++) // 预先打好平方表        pf[i] = i*i;    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;        }        memset(hash_pos,0,sizeof(hash_pos)        memset(hash_neg,0,sizeof(hash_neg));        cnt = 0;        for(i=1; i<101; i++) {            for(j=1; j<101; j++) {                tmp = a*pf[i] + b*pf[j];                tmp < 0 ? hash_neg[-tmp]++ : hash_pos[tmp]++;  // 0赋在pos数组            }        }        for(i=1; i<101; i++) {            for(j=1; j<101; j++) {                tmp = c*pf[i] + d*pf[j];                cnt += (tmp > 0 ? hash_neg[tmp] : hash_pos[-tmp]); // 0取的时候也要从pos数组中取            }        }        printf("%d\n",cnt<<4);    }    return 0;}
1 0
原创粉丝点击