Language: C++

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
int a[maxn];
int main(){
  int n;
  scanf("%d", &n);
  for(int e = 0; e < n; e++) scanf("%d", a + e);
  sort(a, a + n);
  set<pair<int, int> > b;
  for(int e = 0; e < n; e++) b.insert({a[e], e});
  int ans = 0;
  for(int e = n - 1; e >= 0; e--){
    auto me = b.lower_bound({a[e], e});
    if(me != b.end() && me->first == a[e] && me->second == e){
      long long lg = 1;
      while(a[e] >= lg) lg *= 2;
      int diff = (int)(lg - a[e]);
      auto nme = b.lower_bound({diff, -1});
      if(nme != b.end() && nme->first == diff && nme->second != e){
        b.erase(b.lower_bound({a[e], e}));
        b.erase(b.lower_bound({diff, -1}));
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


code 2


using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
 
int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
 
int main() {
    int n;
    cin >> n;
    map<ll, ll> mp;
    set<ll> st;
    REP(i,n){
        ll x = lin();
        mp[x]++;
        st.insert(x);
    }
 
    ll ans = 0;
    FORR(b,34,0){
        ll s = (1LL << b);
        for (ll x : st){
            if (2 * x > s) break;
            if (2 * x == s){
                ans += mp[x] / 2;
                mp[x] %= 2;
            }else{
                if (!mp.count(s - x)) continue;
                ll y = min(mp[x], mp[s - x]);
                ans += y;
                mp[x] -= y;
                mp[s - x] -= y;
            }
        }
    }
    cout << ans << endl;
 
    return 0;
}

code 3:


using namespace std;
int n, res;
multiset<int>Set;
int main() {
	int i, a;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &a);
		Set.insert(a);
	}
	while (!Set.empty()) {
		auto it = Set.end();
		it--;
		int x = *it;
		Set.erase(it);
		for (i = 1; i <= x; i *= 2);
		if (Set.find(i-x) != Set.end()) {
			Set.erase(Set.find(i - x));
			res++;
		}
	}
	printf("%d\n", res);
}

code 4:

#include <bits/stdc++.h>
using namespace std;
 
const int MAXN = 5e5;
int N;
multiset<int> A;
 
int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> N;
    for (int i = 0; i < N; i++) {
        int a; cin >> a; A.insert(a);
    }
    int res = 0;
    while (!A.empty()) {
        auto it = A.end();
        it--;
        int v = *it;
        A.erase(it);
        int i = 0;
        while (v >= (1ll << i)) i++;
        assert(v < (1ll << i));
        int o = int((1ll << i) - v);
        assert(o <= v);
        auto jt = A.find(o);
        if (jt == A.end()) continue;
        res++;
        A.erase(jt);
    }
    cout << res << '\n';
    return 0;
}

code 5


#include <bits/stdc++.h>
using namespace std;
 
const int Maxb = 31;
 
int n;
map <int, int> M;
int res;
 
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int a; scanf("%d", &a);
        M[a]++;
    }
    for (int i = Maxb - 1; i >= 0; i--)
        for (map <int, int>::iterator it = M.begin(); it != M.end() && 2 * it->first <= (1 << i); it++) {
            if (it->second == 0) continue;
            int nd = (1 << i) - it->first;
            if (nd == it->first) {
                int tk = it->second / 2;
                it->second -= 2 * tk;
                res += tk;
            } else {
                map <int, int>::iterator it2 = M.find(nd);
                if (it2 != M.end()) {
                    int tk = min(it->second, it2->second);
                    it->second -= tk; it2->second -= tk;
                    res += tk;
                }
            }
        }
    printf("%d\n", res);
    return 0;
}


Compiler output:
a.cc:32:1: error: 'code' does not name a type
   32 | code 2
      | ^~~~
a.cc:53:5: error: redefinition of 'int main()'
   53 | int main() {
      |     ^~~~
a.cc:5:5: note: 'int main()' previously defined here
    5 | int main(){
      |     ^~~~
a.cc:86:1: error: 'code' does not name a type
   86 | code 3:
      | ^~~~
a.cc:92:5: error: redefinition of 'int main()'
   92 | int main() {
      |     ^~~~
a.cc:5:5: note: 'int main()' previously defined here
    5 | int main(){
      |     ^~~~
a.cc:113:1: error: 'code' does not name a type
  113 | code 4:
      | ^~~~
a.cc:122:5: error: redefinition of 'int main()'
  122 | int main() {
      |     ^~~~
a.cc:5:5: note: 'int main()' previously defined here
    5 | int main(){
      |     ^~~~
a.cc:148:1: error: 'code' does not name a type
  148 | code 5
      | ^~~~
a.cc:156:5: error: redefinition of 'int n'
  156 | int n;
      |     ^
a.cc:90:5: note: 'int n' previously declared here
   90 | int n, res;
      |     ^
a.cc:158:5: error: redefinition of 'int res'
  158 | int res;
      |     ^~~
a.cc:90:8: note: 'int res' previously declared here
   90 | int n, res;
      |        ^~~
a.cc:160:5: error: redefinition of 'int main()'
  160 | int main()
      |     ^~~~
a.cc:5:5: note: 'int main()' previously defined here
    5 | int main(){
      |     ^~~~