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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值