【meet in middle】poj1840Eqs

震惊!map的常数居然如此之大

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

题目大意

给出一个五元三次方程的系数,求满足xi∈[-50,50]的方程根组数。

题目分析

算是一道比较经典也挺简单的meet in middle吧。

五个根都拿来枚举?$100^5$飞到天上去……

那么算出来前四个,就可以得到第五个根,再判断一下是否合法?$100^4$照样很悬。

从传统算法再延伸一下就可以得到meet in middle的核心思想:将所要解决的问题转化成$left=right$的形式,其中$left$和$right$的计算复杂度最好均匀一些(不过视情况而定,毕竟有些题时限很紧但是内存N/A)。我们保存所有left的信息,再枚举所有right比对是否匹配得上。这样一般来说,复杂度能从$O(n^k)$降为$O(n^\frac{k}{2})$。

话说map为什么这么慢!

 

 1 #include<cstdio>
 2 #include<map>
 3 
 4 short f[25000003];
 5 int a1,a2,a3,a4,a5,ans,t,g[103];
 6 
 7 int main()
 8 {
 9     scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
10     for (int i=-50; i<=50; i++)
11         g[i+50] = i*i*i;
12     for (int x=-50; x<=50; x++)
13         if (x)
14         for (int y=-50; y<=50; y++)
15             if (y){
16                 t = -a1*g[x+50]-a2*g[y+50];
17                 if (t < 0) t += 25000000;
18                 f[t]++;
19             }
20     for (int x=-50; x<=50; x++)
21         if (x)
22         for (int y=-50; y<=50; y++)
23             if (y)
24             for (int z=-50; z<=50; z++)
25                 if (z){
26                     t = a3*g[x+50]+a4*g[y+50]+a5*g[z+50];
27                     if (t < 0) t += 25000000;
28                     ans += f[t];
29                 }
30     printf("%d\n",ans);
31     return 0;
32 }

 

 

 

 

END

posted @ 2018-07-07 10:59  AntiQuality  阅读(181)  评论(0编辑  收藏  举报