Equations - http://acm.hdu.edu.cn/showproblem.php?pid=1496
代码
大数组无冲突hash【153MS】
#include <bits/stdc++.h>
using namespace std;
#define MXN 1000000
int a, b, c, d, h[(MXN<<1)+10], cnt;
int main() {
while (scanf("%d %d %d %d",&a, &b, &c, &d) == 4){
if(a*b > 0 && c*d > 0 && a*c > 0){
printf("0\n");
continue;
}
memset(h, 0, sizeof h), cnt = 0;
for(int i = 1; i <= 100; i++){
for(int j = 1; j <= 100; j++){
h[a*i*i+b*j*j+MXN]++;
}
}
for(int i = 1; i <= 100; i++){
for(int j = 1; j <= 100; j++){
cnt += h[-c*i*i-d*j*j+MXN];
}
}
printf("%d\n", cnt<<4);
}
return 0;
}
小数组有冲突hash【31MS】
#include <bits/stdc++.h>
using namespace std;
#define MXN 50021
int a, b, c, d, h[MXN], g[MXN];
int hsh(int x){// 计算x的hash值(即:在数组h和g中的存储位置)
int t = x%MXN;
if(t < 0) t += MXN;
while(h[t] != 0 && g[t] != x) t = (t+1)%MXN;
return t;
}
int main() {
while (scanf("%d %d %d %d",&a, &b, &c, &d) == 4){
if(a*b > 0 && c*d > 0 && a*c > 0){ // a、b、c同号
printf("0\n");
continue;
}
memset(h, 0, sizeof h);
int cnt = 0, tmp, p;
for(int i = 1; i <= 100; i++){
for(int j = 1; j <= 100; j++){
tmp = a*i*i+b*j*j;
p = hsh(tmp), h[p]++, g[p] = tmp;
}
}
for(int i = 1; i <= 100; i++){
for(int j = 1; j <= 100; j++){
tmp = -c*i*i-d*j*j;
p = hsh(tmp), cnt += h[p];
}
}
printf("%d\n", cnt<<4);
}
return 0;
}