POJ 1840 Eqs

Eqs
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 15010   Accepted: 7366

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. 

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


按要求模拟即可

hash是个神奇的东西

下方代码注释部分是先三层循环后二层,正文部分是先二层循环后三层。两者都是正解,但是由于list插入比读取慢,先二层更快


 1 /*
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<list>
 7 using namespace std;
 8 const int mxn=14997;
 9 list<int>ha[mxn*2];
10 list<int>::iterator it;
11 int x,x1,x2,x3,x4,x5;
12 int ans=0;
13 int main(){
14     scanf("%d%d%d%d%d",&x1,&x2,&x3,&x4,&x5);
15     int i,j,k;
16     for(i=-50;i<=50;i++)
17       for(j=-50;j<=50;j++)
18           for(k=-50;k<=50;k++){
19               if(i==0||j==0||k==0)continue;
20               x=i*i*i*x1+j*j*j*x2+k*k*k*x3;
21               ha[x%mxn+mxn].push_back(x);//hash //x%mxn+mxn保证hash完以后是正数 
22           }
23     for(i=-50;i<=50;i++)
24       for(j=-50;j<=50;j++){
25           if(i==0|j==0)continue;
26           x=-(i*i*i*x4+j*j*j*x5);
27           //检查hash
28         for(it=ha[x%mxn+mxn].begin();it!=ha[x%mxn+mxn].end();it++ ){
29             if(*it==x)ans++;
30         } 
31       }
32     printf("%d",ans);
33     return 0;
34     
35 }
36 */
37 #include<iostream>
38 #include<cstdio>
39 #include<cstring>
40 #include<algorithm>
41 #include<list>
42 using namespace std;
43 const int mxn=14997;
44 list<int>ha[mxn*2];
45 list<int>::iterator it;
46 int x,x1,x2,x3,x4,x5;
47 int ans=0;
48 int main(){
49     scanf("%d%d%d%d%d",&x1,&x2,&x3,&x4,&x5);
50     int i,j,k;
51     for(i=-50;i<=50;i++)
52       for(j=-50;j<=50;j++)
53           {
54               if(i==0||j==0)continue;
55               x=i*i*i*x1+j*j*j*x2;
56               ha[x%mxn+mxn].push_back(x);//hash //x%mxn+mxn保证hash完以后是正数 
57           }
58     for(i=-50;i<=50;i++)
59       for(j=-50;j<=50;j++)
60           for(k=-50;k<=50;k++){
61               if(i==0|j==0||k==0)continue;
62               x=-(i*i*i*x3+j*j*j*x4+k*k*k*x5);
63               //检查hash
64             for(it=ha[x%mxn+mxn].begin();it!=ha[x%mxn+mxn].end();it++ ){
65                 if(*it==x)ans++;
66             } 
67           }
68     printf("%d",ans);
69     return 0;
70     
71 }




niconiconi



本文为博主原创文章,转载请注明出处。
原文地址:https://www.cnblogs.com/SilverNebula/p/5550601.html