POJ 1840(哈希,数学,思维)

来源:互联网 发布:软件咨询服务购买合同 编辑:程序博客网 时间:2024/05/17 01:02

Eqs
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 14985 Accepted: 7353

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

Source

Romania OI 2002





题意:给出系数a1-----a5,求有多少满足等式的整数解


题解:并不会呀,智障选手看到数学就懵蔽了,查了题解,原来是使用hash表做,ORZ,这么神奇


http://blog.csdn.net/lyy289065406/article/details/6647387


要AC这题,就要对方程做一个变形


 

即先枚举x1和x2的组合,把所有出现过的 左值 记录打表,然后再枚举x3 x4 x5的组合得到的 右值,如果某个右值等于已经出现的左值,那么我们就得到了一个解

时间复杂度从 O(n^5)降低到 O(n^2+n^3),大约执行100W次

 

 

我们先定义一个映射数组hash[],初始化为0

对于方程左边,当x1=m  ,  x2= n时得到sum,则把用hash[]记录sum : hash[sum]++,表示sum这个值出现了1次




这里我觉得就不要开静态数组做hash了,直接 map更好,虽然效率不如hash表的O(1)查询,但是log(N)也是可以水过的,静态hash表比map快6倍。。。。。。,毕竟牺牲了大量空间


STL版:


#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#include<bitset>#include<utility>#include<functional>#include<iomanip>#include<sstream>#include<ctime>using namespace std;#define N int(1e5)#define inf int(0x3f3f3f3f)#define mod int(1e9+7)typedef long long LL;map<int, int>Hash;int pow(int x){return x*x*x;}int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);int _time_jc = clock();#endifint a[10];while (~scanf("%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5])){Hash.clear();for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;int sum = (a[1] * pow(i) + a[2] * pow(j));Hash[sum]++;}}int ans = 0;for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;for (int k = -50; k <= 50; k++){if (!k)continue;int sum = a[3] * pow(i) + a[4] * pow(j) + a[5] * pow(k);if(Hash.count(-sum))ans += Hash[-sum];}}}printf("%d\n", ans);}return 0;}




静态数组Hash表:


#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#include<bitset>#include<utility>#include<functional>#include<iomanip>#include<sstream>#include<ctime>using namespace std;#define N int(25e6)#define inf int(0x3f3f3f3f)#define mod int(1e9+7)typedef long long LL;short Hash[N];int pow(int x){return x*x*x;}int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);int _time_jc = clock();#endifint a[10];while (~scanf("%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5])){memset(Hash, 0, sizeof(Hash));for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;int sum = (a[1] * pow(i) + a[2] * pow(j));if (sum < 0)sum += N;Hash[sum]++;}}int ans = 0;for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;for (int k = -50; k <= 50; k++){if (!k)continue;int sum = a[3] * pow(i) + a[4] * pow(j) + a[5] * pow(k);sum = -sum;if (sum < 0)sum += N;ans += Hash[sum];}}}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击