Eqs 折半枚举+二分查找 大水题

                        Eqs

题目抽象:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 (*),给出a1,a2,a3,a4,a5.    ai属于[-50,50].

求有多少序列   x1,x2,x3,x4,x5 ,xi属于 [-50,50]-{0}.

思路:折半枚举+二分查找

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <map>
 7 using namespace std;
 8 typedef long long LL;
 9 const int MS=50;
10 const int SIZE=10000;
11 
12 int hash[SIZE];
13 int cnt;
14 
15 int main()
16 {
17     int a1,a2,a3,a4,a5;
18     while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)!=EOF)
19     {
20         cnt=0;
21         for(int i=-MS;i<=MS;i++)
22             for(int j=-MS;j<=MS;j++)
23         {
24             if(i==0||j==0)
25                 continue;
26             int t=a1*i*i*i+a2*j*j*j;
27             hash[cnt++]=t;
28         }
29         sort(hash,hash+cnt);
30         int ans=0;
31         for(int i=-MS;i<=MS;i++)
32             for(int j=-MS;j<=MS;j++)
33                 for(int k=-MS;k<=MS;k++)
34         {
35             if(i==0||j==0||k==0)
36                 continue;
37             int t=a3*i*i*i+a4*j*j*j+a5*k*k*k;
38             ans+=upper_bound(hash,hash+cnt,-t)-lower_bound(hash,hash+cnt,-t);
39         }
40         printf("%d\n",ans);
41     }
42     return 0;
43 }

 

posted on 2015-05-10 20:47  hutaishi  阅读(134)  评论(0编辑  收藏  举报

导航