fork download
  1. #include <iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<math.h>
  5. using namespace std;
  6. int main() {
  7. long long n;
  8. cin>>n;
  9. map<long long ,long long>mp;
  10. long long x;
  11. while(n--)
  12. {
  13. cin>>x;
  14. mp[x]++;
  15. }
  16. vector<long long>v;
  17. for(long long i=0;i<=32;i++)
  18. {
  19. v.push_back((long long )pow(2,i));
  20. }
  21. long long c=0;
  22.  
  23.  
  24. for(auto i:v)
  25. {
  26. for(auto k:mp)
  27. {
  28. if(mp.find(i-k.first)!=mp.end())
  29. {
  30. if(i-k.first==k.first &&mp[i-k.first]<=1){continue;}
  31. c+=mp[i-k.first];
  32. if(i-k.first!=k.first)
  33. {
  34. auto it=i-k.first;
  35. mp.erase(it);
  36.  
  37. }
  38.  
  39. }
  40. }
  41. }
  42. cout<<c;
  43. return 0;
  44. }
Success #stdin #stdout 0.03s 25836KB
stdin
5
0 0 0 0 1
stdout
#include <iostream>
#include<map>
#include<vector>
#include<math.h>
using namespace std;
int main() {
    long long n;
    cin>>n;
    map<long long ,long long>mp;
    long long x;
    while(n--)
    {
        cin>>x;
        mp[x]++;
    }
    vector<long long>v;
    for(long long i=0;i<=32;i++)
    {
        v.push_back((long long )pow(2,i));
    }
    long long c=0;
 
 
            for(auto i:v)
            {
                for(auto k:mp)
                {
            if(mp.find(i-k.first)!=mp.end())
            {
                if(i-k.first==k.first &&mp[i-k.first]<=1){continue;}
                c+=mp[i-k.first];
                if(i-k.first!=k.first)
                {
                    auto it=i-k.first;
                    mp.erase(it);
 
                }
 
            }
        }
    }
    cout<<c;
    return 0;
}