poj 1840 Eqs

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

题意:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,给出a1,a2,a3,a4,a5,且xi在-50到50(除零外),有多少种满足条件的组合;

思路:用hash,这里有两种,一种用map建立,不过我刚开始超时了,后来发现   for (i = -50; i <= 50; i++)   x[m++] = i * i * i; 先这样处理下就行了,不过时间还是挺长的,另一种就是普通的hash了,这里我用的静态加挂链的方式;

代码:

View Code
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int qq = 1000100;
struct node
{
int data;
int num;
bool biao;
node *next;
}link[qq];
int n = 0;
int a[6] = {0};
int temp;
node *get()
{
node *tt;
tt = new node;
tt->next = NULL;
tt->num = 1;
return tt;
}
void ff(int temp,node *t,int biao)
{
if(t->data == temp)
t->num++;
else
if(t->next != NULL)
ff(temp,t->next,biao);
else
{
node *p;
p=get();
p->data = temp;
p->biao = biao;
t->next = p;
}
}
int fin(int temp,node *p,int biao)
{
if(p->data == temp && p->biao == biao)
return p->num;
else
if(p->next != NULL)
return fin(temp,p->next,biao);
else
return 0;
}
int main()
{
memset(link,0,sizeof(link));
for(int i = 1;i <= 5; ++i)
scanf("%d",&a[i]);
for(int i = -50; i <= 50; ++i)
if(i != 0)
{
for(int j = -50; j <= 50; ++j)
if(j != 0)
{
for(int k = -50; k <= 50; ++k)
if(k != 0)
{
int biao = 0;
temp = a[1]*i*i*i+a[2]*j*j*j+a[3]*k*k*k;
if(temp < 0)
{
biao = 1;
temp = -temp;
}
if(link[temp%qq].num)
ff(temp,&link[temp%qq],biao);
else
{
link[temp%qq].data = temp;
link[temp%qq].next = NULL;
link[temp%qq].num = 1;
link[temp%qq].biao = biao;
}
}
}
}
__int64 ans = 0;
for(int i = -50; i <= 50; ++i)
if(i != 0)
{
for(int j = -50; j <= 50; ++j)
{
if(j == 0)
continue;
int biao = 1;
temp = a[4]*i*i*i+a[5]*j*j*j;
if(temp <= 0)
{
temp = -temp;
biao = 0;
}
if(link[temp%qq].num)
ans = ans + fin(temp,&link[temp%qq],biao);
}
}
printf("%I64d\n",ans);
return 0;
}



posted @ 2012-03-30 21:07  LT-blogs  阅读(153)  评论(0编辑  收藏  举报