#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int M =12500000;
short hash[M*2+10];
//给出coefficients ai 求 a1 X1^3 + a2 X2^3 + a3X3^3 + a4X4^3 + a5 X5^3=0 的解个数 xi∈[-50,50] xi != 0
// 暴力枚举 5个循环 10^10 肯定TLE
// 所以变形 a3X3^3 + a4X4^3 + a5 X5^3=-(a1 X1^3 + a2 X2^3)
// 枚举右边的和并保存在HASH中 O(n^2) 在暴力枚举左边的和 O(n^3) 看在HAsh中是否存在即可
int main()
{
int a1,a2,a3,a4,a5;
cin>>a1>>a2>>a3>>a4>>a5;
memset(hash,0,sizeof(hash));
for(int x1=-50;x1<=50;x1++)
{
if(!x1) continue;//xi !=0
for(int x2=-50;x2<=50;x2++)
{
if(!x2) continue;
int t1=a1*x1*x1*x1;
int t2=a2*x2*x2*x2;
long sum=-1*(t1+t2); // -M <=sum<=M
if(sum<0)
sum+=M*2; // 下标不为负数 离散化后 负数sum: M<sum<2M
hash[sum]++; // a+b=sum x1=a,x2=b 和x1=b,x2=a 视为不同解
}
}
int ans=0;
for(int x3=-50;x3<=50;x3++)
{
if(!x3) continue;
for(int x4=-50;x4<=50;x4++)
{
if(!x4) continue;
for(int x5=-50;x5<=50;x5++)
{
if(!x5) continue;
long sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
if(sum<0)
sum+=M*2;
ans+=hash[sum];
}
}
}
cout<<ans<<endl;
return 0;
}
poj 1840 哈希+离散化
最新推荐文章于 2021-07-14 21:01:54 发布