POJ-1840 Eqs---二分
题目链接:
https://vjudge.net/problem/POJ-1840
题目大意:
给出一个5元3次方程,输入其5个系数,求它的解的个数
其中系数 ai∈[-50,50] 自变量xi∈[-50,0)∪(0,50]
注意:xi不为0
解题思路:
五重循环肯定TLE,所以选择三重循环+两重循环,然后排序,二分找相同的数字即可
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<map> 6 #include<set> 7 #include<cmath> 8 #include<algorithm> 9 #include<vector> 10 using namespace std; 11 typedef long long ll; 12 ll cnt[105]; 13 const int maxn = 1e6 + 10; 14 ll sum1[maxn]; 15 ll sum2[maxn]; 16 int main() 17 { 18 ll a, b, c, d, e; 19 cin >> a >> b >> c >> d >> e; 20 for(int i = -50; i <= 50; i++)cnt[i + 50] = i * i * i; 21 int tot1 = 0, tot2 = 0; 22 for(int i = -50; i <= 50; i++) 23 { 24 if(!i)continue; 25 for(int j = -50; j <= 50; j++) 26 { 27 if(!j)continue; 28 for(int k = -50; k <= 50; k++) 29 { 30 if(!k)continue; 31 sum1[tot1++] = a * cnt[i + 50] + b * cnt[j + 50] + c * cnt[k + 50]; 32 } 33 } 34 } 35 sort(sum1, sum1 + tot1); 36 37 for(int i = -50; i <= 50; i++) 38 { 39 if(!i)continue; 40 for(int j = -50; j <= 50; j++) 41 { 42 if(!j)continue; 43 sum2[tot2++] = - d * cnt[i + 50] - e * cnt[j + 50]; 44 } 45 } 46 sort(sum2, sum2 + tot2); 47 int ans = 0; 48 for(int i = 0; i < tot2; i++) 49 { 50 ans += (upper_bound(sum1, sum1 + tot1, sum2[i]) - sum1) - (lower_bound(sum1, sum1 + tot1, sum2[i]) - sum1); 51 } 52 cout<<ans<<endl; 53 return 0; 54 }
越努力,越幸运