您的位置:首页 > 其它

Eqs(枚举+ hash)

2013-08-22 15:08 323 查看
http://poj.org/problem?id=1840

题意:给出系数a1,a2,a3,a4,a5,求满足方程的解有多少组。

思路:有a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 可得 -(a1x13+ a2x23) = a3x33+ a4x43+ a5x53;

先枚举x1,x2,用hash[]记录 sum出现的次数,然后枚举后三个点,若左边出现的sum在右边可以找到,那么hash[sum]即为解的个数。

#include <cstdio>
#include <string.h>
#include <iostream>
#define N 25000000
using namespace std;
short hash[N+1];

int main()
{
int a1,a2,a3,a4,a5;
while(cin>>a1>>a2>>a3>>a4>>a5)
{
int x1,x2,x3,x4,x5;
int ans = 0;
memset(hash,0,sizeof(hash));
for (x1 = -50; x1 <= 50; x1 ++)
{
if(!x1) continue;
for (x2 = -50; x2 <= 50; x2 ++)
{
if (!x2) continue;
int sum =(a1*x1*x1*x1+a2*x2*x2*x2)*(-1);
if (sum < 0)
sum += N;
hash[sum]++;

}
}
for (x3 = -50; x3 <= 50; x3 ++)
{
if(!x3) continue;
for (x4 = -50; x4 <= 50; x4 ++)
{
if (!x4) continue;
for (x5 = -50; x5 <= 50; x5 ++)
{
if (!x5) continue;
int sum = a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
if (sum < 0)
sum += N;
if (hash[sum])
ans += hash[sum];
}
}
}
cout<<ans<<endl;
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: