POJ-1804 Eqs 解題報告

Description

Consider equations having the following form: 
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 
The coefficients are given integers from the interval [-50,50]. 
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}. 

Determine how many solutions satisfy the given equation. 

Input

The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.

Output

The output will contain on the first line the number of the solutions for the given equation.

Sample Input

37 29 41 43 47

Sample Output

654


       題目鏈接:http://poj.org/problem?id=1840

       解法類型:hash

       解題思路:對方程變下形就可以了,然後直接建立hash表。這裏有很多建立hash表的方法,可以直接暴力建立沒有衝突情況的hash表,但內存會消耗很多。因此可以在建立時加衝突解決情況的處理,對其值取模,建立閉散性的hash表,極大地降低了內存的使用率,但同時帶來了時間消耗的增加。

       算法實現:

//STATUS:C++_AC_2188MS_7948K
#include<stdio.h>
#include<string.h>
const int MAXN=999991;
int hash(int s);
int num[MAXN],saves[MAXN],p[110];
int main()
{
//	freopen("in.txt","r",stdin);
	int i,x1,x2,x3,x4,x5,a1,a2,a3,a4,a5,sum,t,ways;
	for(i=-50;i<0;i++)      //初始化
		p[i+50]=i*i*i;
	for(i=1;i<=50;i++)
		p[i+49]=i*i*i;
	
	while(~scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5))
	{
		memset(num,0,sizeof(num));      
		memset(saves,0,sizeof(saves));

		for(x1=0;x1<100;x1++)           //hash表的構建
			for(x2=0;x2<100;x2++)
				for(x3=0;x3<100;x3++){
					sum=a1*p[x1]+a2*p[x2]+a3*p[x3];
					t=hash(sum);
					num[t]++;
					saves[t]=sum;
				}

		ways=0;
		for(x4=0;x4<100;x4++)            //hash表的查找
			for(x5=0;x5<100;x5++){
				sum=-(a4*p[x4]+a5*p[x5]);
				t=hash(sum);
				ways+=num[t];
			}
		printf("%d\n",ways);
	}
	return 0;
}

int hash(int s)
{
	int t=s%MAXN;
	if(t<0)t+=MAXN;
	while(num[t]!=0 && saves[t]!=s)  //hash表爲空或者找到s
		t=(t+3)%MAXN;     //這裏處理不好,會超時,看數據,看RP!
	return t;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章