poj1840
来源:互联网 发布:photoshop 2016 mac 编辑:程序博客网 时间:2024/05/23 13:25
涉及算法:简单的哈希表
题目大意:对于给定的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;}}
0 0
- poj1840
- poj1840
- POJ1840
- poj1840
- POJ1840
- poj1840
- poj1840
- poj1840
- poj1840
- poj1840
- poj1840
- poj1840
- POJ1840
- poj1840--Eqs
- poj1840 Eqs
- poj1840 Eqs
- POJ1840 哈希表
- Eqs poj1840
- 解题报告 之 HDU5319 Painter
- 严重: Error listenerStart
- android获得圆角图片
- iOS 多线程NSThread-NSOperation-GCD
- 一致性hash和solr千万级数据分布式搜索引擎中的应用
- poj1840
- 微博登录过程分析
- SQLite学习笔记(7)-源代码整理
- 阿里校招-java研发 题目
- 个人的一点总结
- 分布式全文检索系统SolrCloud简介
- 黑马程序员-[OC]学习之旅-block和protocol的概念及使用
- AngularJS中文入门教程
- 珍藏文:花10钟看一看少走30年弯路