poj1840Eqs(哈希判重)
来源:互联网 发布:mysql默认引擎 编辑:程序博客网 时间:2024/06/07 10:47
题目链接:
传送门
思路:
这道题是一个简单的hash的应用,如果直接暴力的话肯定承受不了5重for循环,所以比赛的时候我先到分成两组,但是后来用到了许多数组,然后想到数字太大,还先到stl判重,
后来搞出来还是在本地跑的很慢,就放弃了。。后来看到题解,不得不说太牛了,我的思路是对的,首先把方程分成左右两边,然后分别暴力,因为计算结果的上限可能达到
50*50*50*50*2=1250000,所以取值范围为-12500000--12500000,所以为了避免冲突,应该将hash数组开到2*1250000+1,为什么加1呢,是因为如果出现-12500000那么加上25000000,那么值为12500000,那么就与正的12500000冲突了。。还有最开始先算的3重循环,还是过了,说明这个题目的数据非常水。。。那么最后这个题目就解决了。。。。还有一个小常识65536KB 差不多能开int型数组1677w左右,所以应该用short型数组。。那么就可以开到25000000了。。。。
题目:
Eqs
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 12220 Accepted: 5968
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.
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
代码:
#include<cstdio>#include<iostream>#include<cstring>using namespace std;const int maxn=25000000+10;short hash[maxn];int a1,a2,a3,a4,a5;int main(){ int ans,temp; while(~scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)) { ans=0; memset(hash,0,sizeof(hash)); for(int i=-50;i<=50;i++) for(int j=-50;j<=50;j++) { if(i==0||j==0) continue; temp=(a1*i*i*i+a2*j*j*j)*(-1); if(temp<0) temp=temp+maxn; hash[temp]++; } for(int i=-50;i<=50;i++) for(int j=-50;j<=50;j++) for(int k=-50;k<=50;k++) { if(i==0||j==0||k==0) continue; temp=a3*i*i*i+a4*j*j*j+a5*k*k*k; if(temp<0) temp=temp+maxn; if(hash[temp]) ans=ans+hash[temp]; } printf("%d\n",ans); } return 0;}
1 1
- poj1840Eqs(哈希判重)
- poj1840Eqs【散列表】
- POJ1840Eqs(哈希)
- POJ1840Eqs(哈希表)
- POJ--3394[Containers] 哈希判重
- uva10422 bfs+哈希判重 TLE
- codeforces 225D 搜索 哈希判重
- uva 10603 Fill(隐式图搜索,哈希判重)
- POJ 1465 Multiple (广搜,哈希判重)
- uva:321 - The New Villa(bfs + 哈希判重)
- 不重复随机数生成;O(1)时间;二进制哈希判重;
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
- uva310 L-system L系统 哈希判重又TLE
- poj 3007 Organize Your Train part II 哈希判重
- USACO Magic Squares, 难题,康托展开,位运算,BFS,哈希判重等……
- R语言学习-线性回归
- PL/SQL 设置登陆数据库的 配置文件 tnsnames.ora(oracle文件)
- 6条网页设计配色原则
- MySQL: 修改(windows系统下)mysql默认连接数的方法
- 7.3 sizeof关键字
- poj1840Eqs(哈希判重)
- 小黑小波比.Ubuntu下的截图
- JavaScript与Java通信
- 父亲的爱
- 【贯明专栏】不以物喜 不以己悲
- 然而有一次,夜里醒来
- “吃好了没?”他看着我问道。
- Visual Studio:error MSB8020
- 黑马程序员——泛型(Generic)