# POJ - 1840 - Eqs = 思维

http://poj.org/problem?id=1840

$$O(n^2\log(n^2)+n^3\log(n^2))$$

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

map<int, int> M;

int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int a1, a2, a3, a4, a5;
scanf("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
for(int x1 = -50; x1 <= 50; ++x1) {
if(x1 == 0)
continue;
int p1 = a1 * x1 * x1 * x1;
for(int x2 = -50; x2 <= 50; ++x2) {
if(x2 == 0)
continue;
int p2 = a2 * x2 * x2 * x2;
M[p1 + p2]++;
}
}
ll ans = 0;
for(int x3 = -50; x3 <= 50; ++x3) {
if(x3 == 0)
continue;
int p3 = a3 * x3 * x3 * x3;
for(int x4 = -50; x4 <= 50; ++x4) {
if(x4 == 0)
continue;
int p4 = a4 * x4 * x4 * x4;
for(int x5 = -50; x5 <= 50; ++x5) {
if(x5 == 0)
continue;
int p5 = a5 * x5 * x5 * x5;
map<int, int>::iterator it = M.find(-p3 - p4 - p5);
if(it != M.end())
ans += it->second;
}
}
}
printf("%lld\n", ans);
}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

const int MAXN = 49999;
struct HashTable {
map<int, int> M[MAXN];
void insert(int x) {
int p = x % MAXN;
if(p < 0)
p += MAXN;
M[p][x]++;
}
int count(int x) {
int p = x % MAXN;
if(p < 0)
p += MAXN;
map<int, int>::iterator it = M[p].find(x);
if(it != M[p].end())
return it->second;
return 0;
}
} ht;

//寻找n以内的一个最大的质数
/*const int MAXP=2e6;
bool np[MAXP+1];
void find_p(int n){
np[1]=1;
for(int i=1;i<=n;++i){
if(np[i])
continue;
for(int j=i+i;j<=n;j+=i)
np[j]=1;
}
for(int i=n;;--i){
if(!np[i]){
printf("MAXP=%d\n",i);
break;
}
}
}*/

int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
//find_p(5e4);
int a1, a2, a3, a4, a5;
scanf("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
for(int x1 = -50; x1 <= 50; ++x1) {
if(x1 == 0)
continue;
int p1 = a1 * x1 * x1 * x1;
for(int x2 = -50; x2 <= 50; ++x2) {
if(x2 == 0)
continue;
int p2 = a2 * x2 * x2 * x2;
ht.insert(p1 + p2);
}
}
ll ans = 0;
for(int x3 = -50; x3 <= 50; ++x3) {
if(x3 == 0)
continue;
int p3 = a3 * x3 * x3 * x3;
for(int x4 = -50; x4 <= 50; ++x4) {
if(x4 == 0)
continue;
int p4 = a4 * x4 * x4 * x4;
for(int x5 = -50; x5 <= 50; ++x5) {
if(x5 == 0)
continue;
int p5 = a5 * x5 * x5 * x5;
ans += ht.count(-p3 - p4 - p5);
}
}
}
printf("%lld\n", ans);
}