POJ 1840 Eqs hash

题意:解方程组a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,x属于[50,50]且x!=0输入a1,a2,a3,a4,a5,输出一共有多少种满足方程的解。
题解:左右分开,hash

#include <iostream>
using namespace std;

#define prime 14999  
int cube[105], hash[prime][100], a[6];

void get_cube()
{
	int t = -1;
	for( int i = -50; i <= 50; i++ )
	{
		if ( i == 0 ) continue;
		cube[++t] = i*i*i;
	}
}

void get_hash()
{
	int sum, temp, i, j, k;
	for ( i = 0; i < 100; ++i )
	{
		for ( j = 0; j < 100; ++j )
		{
			k = 0;
			sum = -(cube[i]*a[1] + cube[j]*a[2]);
			temp = sum % prime;
			if ( temp < 0 ) temp += prime;
			while ( hash[temp][k] != -1 ) ++k;
			hash[temp][k] = sum;
		}
	}
}

int eqs ()
{
	int i, j, k, h;
	int sum, temp, ans = 0;
	for ( i = 0; i < 100; ++i )
		for ( j = 0; j < 100; ++j )
			for ( k = 0; k < 100; ++k )
			{
				sum = cube[i]*a[3] + cube[j]*a[4] + cube[k]*a[5];
				temp = sum % prime;
				if ( temp < 0 ) temp += prime;
				h = 0;
				while ( hash[temp][h] != -1 )
				{
					if ( hash[temp][h] == sum )
						++ans;
					++h;
				}
			}
	return ans;
}

int main()
{
	memset(cube,0,sizeof(cube));
	memset(hash,-1,sizeof(hash));
	for ( int i = 1; i <= 5; ++i )
		scanf("%d",a+i);
	get_cube();
	get_hash();
	printf("%d\n",eqs());
	return 0;
}


你可能感兴趣的:(POJ 1840 Eqs hash)