您的位置:首页 > 其它

HDU 1496 Equations 哈希

2016-12-31 11:12 316 查看
传送门: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hash