Eqs(枚举+ hash)

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

题意:给出系数a1,a2,a3,a4,a5,求满足方程的解有多少组。

思路:有a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 可得 -(a1x13+ a2x23) = a3x33+ a4x43+ a5x53

先枚举x1,x2,用hash[]记录 sum出现的次数,然后枚举后三个点,若左边出现的sum在右边可以找到,那么hash[sum]即为解的个数。

 1 #include <cstdio>
 2 #include <string.h>
 3 #include <iostream>
 4 #define N 25000000
 5 using namespace std;
 6 short hash[N+1];
 7 
 8 int main()
 9 {
10     int a1,a2,a3,a4,a5;
11     while(cin>>a1>>a2>>a3>>a4>>a5)
12     {
13         int x1,x2,x3,x4,x5;
14         int ans = 0;
15         memset(hash,0,sizeof(hash));
16         for (x1 = -50; x1 <= 50; x1 ++)
17         {
18             if(!x1) continue;
19             for (x2 = -50; x2 <= 50; x2 ++)
20             {
21                 if (!x2) continue;
22                 int sum =(a1*x1*x1*x1+a2*x2*x2*x2)*(-1);
23                 if (sum < 0)
24                     sum += N;
25                 hash[sum]++;
26 
27             }
28         }
29         for (x3 = -50; x3 <= 50; x3 ++)
30         {
31             if(!x3) continue;
32             for (x4 = -50; x4 <= 50; x4 ++)
33             {
34                 if (!x4) continue;
35                 for (x5 = -50; x5 <= 50; x5 ++)
36                 {
37                     if (!x5) continue;
38                     int sum = a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
39                     if (sum < 0)
40                         sum += N;
41                     if (hash[sum])
42                         ans += hash[sum];
43                 }
44             }
45         }
46         cout<<ans<<endl;
47     }
48     return 0;
49 }
View Code
原文地址:https://www.cnblogs.com/lahblogs/p/3274919.html