fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int tell;
  4. bool binarySearch(int,vector<pair<int,int>>,int,int);
  5. void makingnewvector(vector<int>,vector<pair<int,int>>&);
  6. int main()
  7. {
  8. int i,j,k,n;
  9. cin>>n;
  10. int arr[n];
  11. vector<int> lhs,rhs;
  12. vector<pair<int,int>> nlhs,nrhs;
  13. for(i=0;i<n;i++){
  14. cin>>arr[i];
  15. }
  16.  
  17. for(i=0; i<n; i++)
  18. for(j=0; j<n; j++)
  19. for(k=0; k<n; k++){
  20. if(arr[i])
  21. rhs.push_back(arr[i]*(arr[j]+arr[k]));
  22. lhs.push_back((arr[i]*arr[j])+arr[k]);
  23. }
  24.  
  25. sort(lhs.begin(),lhs.end());
  26. sort(rhs.begin(),rhs.end());
  27. makingnewvector(rhs,nrhs);
  28. makingnewvector(lhs,nlhs);
  29. int sum=0;
  30. for(i=0;i<nlhs.size();i++){
  31. if(binarySearch(nlhs[i].first,nrhs,0,nrhs.size()-1)){
  32. sum+=nlhs[i].second*tell;
  33. }
  34. }
  35. cout<<sum<<endl;
  36. return 0;
  37. }
  38. void makingnewvector(vector<int> hs,vector<pair<int,int>> &nhs)
  39. {
  40. int temp=hs[0],flag=1;
  41. for(int i=1;i<hs.size();i++){
  42. if(temp==hs[i])
  43. flag++;
  44. else{
  45. nhs.push_back(make_pair(temp,flag));
  46. temp=hs[i];
  47. flag=1;
  48. }
  49. }
  50. nhs.push_back(make_pair(temp,flag));
  51. }
  52. bool binarySearch(int n,vector<pair<int,int>> rhs,int low,int high)
  53. {
  54. int mid=(low+high)/2;
  55. if(n==rhs[mid].first){
  56. tell=rhs[mid].second;
  57. return true;
  58. }
  59. if(low==high)
  60. return false;
  61. if(n>rhs[mid].first)
  62. return binarySearch(n,rhs,mid+1,high);
  63. if(n<rhs[mid].first)
  64. return binarySearch(n,rhs,low,mid);
  65. }
  66.  
Time limit exceeded #stdin #stdout 5s 4197588KB
stdin
Standard input is empty
stdout
Standard output is empty