涉及算法:简单的哈希表
题目大意:对于给定的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;
}
}