Eqs(枚举+ hash)
2013-08-22 15:08
323 查看
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]即为解的个数。
View Code
题意:给出系数a1,a2,a3,a4,a5,求满足方程的解有多少组。
思路:有a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 可得 -(a1x13+ a2x23) = a3x33+ a4x43+ a5x53;
先枚举x1,x2,用hash[]记录 sum出现的次数,然后枚举后三个点,若左边出现的sum在右边可以找到,那么hash[sum]即为解的个数。
#include <cstdio> #include <string.h> #include <iostream> #define N 25000000 using namespace std; short hash[N+1]; int main() { int a1,a2,a3,a4,a5; while(cin>>a1>>a2>>a3>>a4>>a5) { int x1,x2,x3,x4,x5; int ans = 0; memset(hash,0,sizeof(hash)); for (x1 = -50; x1 <= 50; x1 ++) { if(!x1) continue; for (x2 = -50; x2 <= 50; x2 ++) { if (!x2) continue; int sum =(a1*x1*x1*x1+a2*x2*x2*x2)*(-1); if (sum < 0) sum += N; hash[sum]++; } } for (x3 = -50; x3 <= 50; x3 ++) { if(!x3) continue; for (x4 = -50; x4 <= 50; x4 ++) { if (!x4) continue; for (x5 = -50; x5 <= 50; x5 ++) { if (!x5) continue; int sum = a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5; if (sum < 0) sum += N; if (hash[sum]) ans += hash[sum]; } } } cout<<ans<<endl; } return 0; }
View Code
相关文章推荐
- poj 1840 Eqs(暴力枚举+hash)
- [POJ 1840]Eqs[hash][枚举]
- POJ 1840 Eqs(枚举+HASH)
- Codeforces 898F Restoring the Expression [hash+枚举]
- Poj 1840 Eqs(Hash)
- POJ 1840 Eqs (hash)
- POJ 1840 Eqs(简单hash入门)
- poj 1840 Eqs , hash
- poj 1840 Eqs 【hash】
- poj 1054 枚举+hash
- POJ 1840 ——Eqs 哈希,枚举
- hdu 4993(简单枚举或者hash)
- poj 1840 Eqs (hash)
- poj1840Eqs(hash)
- pku2769 枚举,hash
- POJ 1840 Eqs (大整数的Hash)
- Eqs hash表存储,注意hash要用char类型
- ACDREAM 1726 A Math game(折半枚举+hash)
- HDOJ4886(hash+枚举)
- poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】