Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 14985 | Accepted: 7353 |
Description
Input
Output
Sample Input
37 29 41 43 47
Sample Output
654
Source
题意:给出系数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 CDZSC freopen("i.txt", "r", stdin); //freopen("o.txt","w",stdout); int _time_jc = clock(); #endif int 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 CDZSC freopen("i.txt", "r", stdin); //freopen("o.txt","w",stdout); int _time_jc = clock(); #endif int 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; }