poj1840
2015-08-23 11:41
169 查看
涉及算法:简单的哈希表
题目大意:对于给定的a1,a2,a3,a4,a5求出a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3+a5*x5^3=0的解的个数
即求-(a1*x1^3+a2*x2^3)=a3*x3^3+a4*x4^3+a5*x5^3的解的个数
先枚举出左边的的所有数,存放到散列中,为了便于寻址采用hsah[leftNum]=leftNum出现的次数,这样时间复杂度由O(n^5)降低为O(n^2+n^3)
用short开数组,占内存小
题目大意:对于给定的a1,a2,a3,a4,a5求出a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3+a5*x5^3=0的解的个数
即求-(a1*x1^3+a2*x2^3)=a3*x3^3+a4*x4^3+a5*x5^3的解的个数
先枚举出左边的的所有数,存放到散列中,为了便于寻址采用hsah[leftNum]=leftNum出现的次数,这样时间复杂度由O(n^5)降低为O(n^2+n^3)
用short开数组,占内存小
import java.util.Scanner; public class Main_1840 { static int[] a=new int[6]; static short[] hash=new short[25000001]; public static void main(String[] args) { Scanner in=new Scanner(System.in); for(int i=1;i<=5;i++){ a[i]=in.nextInt(); } getHash(); solve(); System.out.println(ans); } static void getHash(){ for(int x1=-50;x1<=50;x1++){ if(x1==0) continue; for(int x2=-50;x2<=50;x2++){ if(x2==0) continue; hash[getLeftResult(x1, x2)]++; } } } static int ans=0; static int rightResult=0; static void solve(){ for(int x3=-50;x3<=50;x3++){ if(x3==0) continue; for(int x4=-50;x4<=50;x4++){ if(x4==0) continue; for(int x5=-50;x5<=50;x5++){ if(x5==0) continue; rightResult=getRightResult(x3, x4, x5); if(rightResult>=0 && rightResult<=25000000){ ans+=hash[rightResult]; } } } } } static int result=0; static int getLeftResult(int x1,int x2){ result=0; result=a[1]*x1*x1*x1+a[2]*x2*x2*x2; return -result+12500000;//0~25000000 } static int getRightResult(int x3,int x4,int x5){ result=0; result=a[3]*x3*x3*x3+a[4]*x4*x4*x4+a[5]*x5*x5*x5; return result+12500000; } }
相关文章推荐
- iOS 多线程NSThread-NSOperation-GCD
- 论攻击Web应用的常见技术
- js数组反转
- 白驹过隙之大一总结
- 严重: Error listenerStart
- 一小时学会用Python Socket 开发可并发的FTP服务器!! 推荐
- MFC(ftp)
- HDU 5417 Victor and Machine(模拟||递推)
- SQLite学习笔记(6)-SQLite架构
- 二叉树遍历的的例子
- 如何让vim像IDE一样一键放大缩小字号?
- Linux开机自动挂载Windows分区笔记
- MyEclipse6.5注册机留档备查,有效期至2017年
- 注册表详解与开机加速
- iOS: NSSortDescriptor 集合排序
- 网页正文、屏幕、滚动条相关
- C++实现九宫格游戏人机对战
- 图片
- 打豪车应用:uber详细攻略(附100元优步uber优惠码、uber优惠券、优步优惠码、优步优惠券)
- web开发中目录路径问题的解决