prithivMLmods/Coder-Stat huggingface dataset:
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i, a) for (auto &i : a)

typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {

  int n;
  cin >> n;
  VLL a(n);
  REP(i, n) cin >> a[i];
  sort(ALL(a));
  ll ans = 0;

  vector<bool> b(n, true);

  for (int i = n - 1; i >= 0; i--) {
    if (!b[i])
      continue;
    b[i] = false;
    ll j = 1;
    while (j <= a[i])
      j *= 2;
    j -= a[i];

    int m = lower_bound(ALL(a), j) - a.begin();
    int l = upper_bound(ALL(a), j) - a.begin();
    for (int k = l - 1; k >= m; k--) {
      if (b[k]) {
        b[k] = false;
        ans++;
        break;
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i, a) for (auto &i : a)

typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {

  int n;
  cin >> n;
  VLL a(n);
  REP(i, n) cin >> a[i];
  sort(ALL(a));
  ll ans = 0;

  vector<bool> b(n, true);

  for (int i = n - 1; i >= 0; i--) {
    if (!b[i])
      continue;
    b[i] = false;
    ll j = 1;
    while (j <= a[i])
      j *= 2;
    j -= a[i];

    ll l = upper_bound(ALL(a), j) - a.begin();
    for (int k = min(l - 1, (ll)i);; k--) {
      if (a[k] != j)
        break;
      if (b[k]) {
        b[k] = false;
        ans++;
        break;
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int A[100000];
map<int, int> elem_occurence;

int closest_two_power(int n) {
  for (int i = 1; i < 32; i++) {
    if ((1 << (i - 1)) <= n && n < (1 << i)) {
      return (1 << i);
    }
  }
  return -1;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    if (elem_occurence.find(A[i]) == elem_occurence.end()) {
      elem_occurence[A[i]] = 1;
    } else {
      elem_occurence[A[i]]++;
    }
  }

  sort(A, A + N, greater<int>());

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (elem_occurence.find(A[i]) == elem_occurence.end()) {
      continue;
    }
    int target = closest_two_power(A[i]) - A[i];
    if (target != A[i]) {
      if (elem_occurence.find(target) != elem_occurence.end()) {
        ans++;
        elem_occurence[target]--;
        elem_occurence[A[i]]--;
        if (elem_occurence[target] == 0) {
          elem_occurence.erase(target);
        }
        if (elem_occurence[A[i]] == 0) {
          elem_occurence.erase(A[i]);
        }
      }
    } else {
      if (elem_occurence[target] >= 2) {
        ans++;
        elem_occurence[target] -= 2;
        if (elem_occurence[target] == 0) {
          elem_occurence.erase(target);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int A[200000];
map<int, int> elem_occurence;

int closest_two_power(int n) {
  for (int i = 1; i < 32; i++) {
    if ((1 << (i - 1)) <= n && n < (1 << i)) {
      return (1 << i);
    }
  }
  return -1;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    if (elem_occurence.find(A[i]) == elem_occurence.end()) {
      elem_occurence[A[i]] = 1;
    } else {
      elem_occurence[A[i]]++;
    }
  }

  sort(A, A + N, greater<int>());

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (elem_occurence.find(A[i]) == elem_occurence.end()) {
      continue;
    }
    int target = closest_two_power(A[i]) - A[i];
    if (target != A[i]) {
      if (elem_occurence.find(target) != elem_occurence.end()) {
        ans++;
        elem_occurence[target]--;
        elem_occurence[A[i]]--;
        if (elem_occurence[target] == 0) {
          elem_occurence.erase(target);
        }
        if (elem_occurence[A[i]] == 0) {
          elem_occurence.erase(A[i]);
        }
      }
    } else {
      if (elem_occurence[target] >= 2) {
        ans++;
        elem_occurence[target] -= 2;
        if (elem_occurence[target] == 0) {
          elem_occurence.erase(target);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
typedef long long int ll;
int N, a[200001];

void solve() {
  map<int, int> M;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    M[a[i]]++;
  }
  sort(a, a + N);
  int ret = 0;
  int pw = 1 << 30;
  for (int i = N - 1; i >= 0; i++) {
    if (M[a[i]]) {
      M[a[i]]--;
      while (pw >= 2 && 2 * a[i] < pw)
        pw >>= 1;
      if (M.count(pw - a[i]) && M[pw - a[i]]) {
        ret++;
        M[pw - a[i]]--;
      }
    }
  }
  cout << ret << endl;
}
int main() {
  while (cin >> N) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
typedef long long int ll;
int N, a[200001];

void solve() {
  map<int, int> M;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    M[a[i]]++;
  }
  sort(a, a + N);
  int ret = 0;
  int pw = 1 << 30;
  for (int i = N - 1; i >= 0; i--) {
    if (M[a[i]]) {
      M[a[i]]--;
      while (pw >= 2 && 2 * a[i] < pw)
        pw >>= 1;
      if (M.count(pw - a[i]) && M[pw - a[i]]) {
        ret++;
        M[pw - a[i]]--;
      }
    }
  }
  cout << ret << endl;
}
int main() {
  while (cin >> N) {
    solve();
  }
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define PF push_front
#define MP make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e5 + 5;

int a[maxn];
map<int, int> mp;

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a, a + n, greater<int>());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (mp[a[i]] != 0) {
      mp[a[i]]--;
      for (int j = 30; j >= 0; j--) {
        if (mp[(1 << j) - a[i]] > 0) {
          ans++;
          mp[(1 << j) - a[i]]--;
        }
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define PF push_front
#define MP make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 2e5 + 5;

int a[maxn];
map<int, int> mp;

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a, a + n, greater<int>());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (mp[a[i]] != 0) {
      mp[a[i]]--;
      for (int j = 30; j >= 0; j--) {
        if (mp[(1 << j) - a[i]] > 0) {
          ans++;
          mp[(1 << j) - a[i]]--;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 500005
using namespace std;
map<int, int> mp;
int n, ans, a[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), mp[a[i]]++;
  sort(a + 1, a + n + 1);
  for (int i = n; i; i--) {
    if (!mp[a[i]])
      continue;
    for (int j = 1; j <= (1 << 30); j *= 2) {
      if (!mp[a[i]])
        break;
      int x = j - a[i];
      // if (x<0) continue;
      if (mp[x]) {
        if (x == a[i]) {
          ans += mp[a[i]] / 2;
          mp[a[i]] %= 2;
        } else {
          int t = min(mp[a[i]], mp[x]);
          ans += t;
          mp[a[i]] -= t;
          mp[x] -= t;
        }
      }
      if (j == (1 << 30))
        break;
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 500005
using namespace std;
map<int, int> mp;
int n, ans, a[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), mp[a[i]]++;
  sort(a + 1, a + n + 1);
  for (int i = n; i; i--) {
    if (!mp[a[i]])
      continue;
    for (int j = 1; j <= (1 << 30); j *= 2) {
      if (!mp[a[i]])
        break;
      int x = j - a[i];
      if (x < 0)
        continue;
      if (mp[x]) {
        if (x == a[i]) {
          ans += mp[a[i]] / 2;
          mp[a[i]] %= 2;
        } else {
          int t = min(mp[a[i]], mp[x]);
          ans += t;
          mp[a[i]] -= t;
          mp[x] -= t;
        }
      }
      if (j == (1 << 30))
        break;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
//{{{
#include <bits/stdc++.h>
using namespace std;
#define repX(a, b, c, x, ...) x
#define repN(a) repX a
#define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__)
#define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__)
#define loop(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, begin, end)                                                    \
  for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i)
#define rrep2(i, n) rrep3(i, n, 0)
#define rrep3(i, begin, end)                                                   \
  for (int i = (int)(begin - 1), i##_end = (int)(end); i >= i##_end; --i)
#define foreach(x, a) for (auto &x : a)
using ll = long long;
const ll mod = (ll)1e9 + 7;
//}}}

int a[200000];
map<int, int> mp;

int target[32];

int f(int n) {
  rep(i, 32) {
    int num = target[i] - n;
    if (n != num && mp[n] && mp[num] || n == num && mp[n] >= 2) {
      // cout << n << " "<< num << endl;
      mp[n]--;
      mp[num]--;
      return 1;
    }
  }
  return 0;
}

int main() {
  int N;
  cin >> N;
  target[0] = 1;
  rep(i, 1, 32) { target[i] = target[i - 1] * 2; }
  rep(i, N) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a, a + N);
  reverse(a, a + N);
  ll ans = 0;
  rep(i, N) { ans += f(a[i]); }
  cout << ans << endl;
  return 0;
}


Fixed submission:
//{{{
#include <bits/stdc++.h>
using namespace std;
#define repX(a, b, c, x, ...) x
#define repN(a) repX a
#define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__)
#define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__)
#define loop(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, begin, end)                                                    \
  for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i)
#define rrep2(i, n) rrep3(i, n, 0)
#define rrep3(i, begin, end)                                                   \
  for (int i = (int)(begin - 1), i##_end = (int)(end); i >= i##_end; --i)
#define foreach(x, a) for (auto &x : a)
using ll = long long;
const ll mod = (ll)1e9 + 7;
//}}}

int a[200000];
map<int, int> mp;

int target[32];

int f(int n) {
  rep(i, 32) {
    int num = target[i] - n;
    if (num > n)
      continue;
    if (num < 0)
      continue;
    if (n != num && mp[n] && mp[num] || n == num && mp[n] >= 2) {
      // cout << n << " "<< num << endl;
      mp[n]--;
      mp[num]--;
      return 1;
    }
  }
  return 0;
}

int main() {
  int N;
  cin >> N;
  target[0] = 1;
  rep(i, 1, 32) { target[i] = target[i - 1] * 2; }
  rep(i, N) {
    cin >> a[i];
    mp[a[i]]++;
  }
  sort(a, a + N);
  reverse(a, a + N);
  ll ans = 0;
  rep(i, N) { ans += f(a[i]); }
  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
// #define int long long
int powint(int a, int x) {
  int res = 1;
  REP(i, x) { res = res * a; }
  return res;
}
#define vi vector<int>

using namespace std;

int target(int src) {
  int tmp = 1;
  while (tmp <= src) {
    tmp *= 2;
  }
  return tmp;
}

signed main() {
  int N;
  cin >> N;
  vi A(N);
  REP(i, N) { cin >> A[i]; }
  sort(A.begin(), A.end()); // 昇順
  // REP(i,A.size()) cout<<A[i]<<endl;
  int ans = 0;
  vector<bool> used(N, false);

  RREP(i, N) {
    int t = target(A[i]);
    int u = t - A[i];
    int l = lower_bound(A.begin(), A.begin() + i, u) - A.begin();
    int r = upper_bound(A.begin(), A.begin() + i, u) - A.begin();
    // debug(t); debug(u); debug(l); debug(r);
    for (auto j = r - 1; j >= l; --j) {
      if (!used[j] && !used[i]) {
        used[j] = true;
        used[i] = true;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
// #define int long long
int powint(int a, int x) {
  int res = 1;
  REP(i, x) { res = res * a; }
  return res;
}
#define vi vector<int>

using namespace std;

int target(int src) {
  int tmp = 1;
  while (tmp <= src) {
    tmp *= 2;
  }
  return tmp;
}

signed main() {
  int N;
  cin >> N;
  vi A(N);
  REP(i, N) { cin >> A[i]; }
  sort(A.begin(), A.end()); // 昇順
  // REP(i,A.size()) cout<<A[i]<<endl;
  int ans = 0;
  vector<bool> used(N, false);

  RREP(i, N) {
    if (used[i])
      continue;
    int t = target(A[i]);
    int u = t - A[i];
    int l = lower_bound(A.begin(), A.begin() + i, u) - A.begin();
    int r = upper_bound(A.begin(), A.begin() + i, u) - A.begin();
    // debug(t); debug(u); debug(l); debug(r);
    for (auto j = r - 1; j >= l; --j) {
      if (!used[j] && !used[i]) {
        used[j] = true;
        used[i] = true;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repeat(i, s, n) for (int(i) = s; (i) < (n); (i)++)
#define revrep(i, n) for (int(i) = (n)-1; i >= 0; i--)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(std::numeric_limits<float>::max_digits10);
  int n;
  cin >> n;
  multiset<ll> s;
  rep(i, n) {
    ll x;
    cin >> x;
    s.insert(x);
  }
  int ans = 0;
  while (!s.empty()) {
    auto m = max_element(s.begin(), s.end());
    s.erase(m);
    ll y = 1;
    while (y <= *m)
      y *= 2;
    if (s.find(y - *m) != s.end()) {
      s.erase(s.find(y - *m));
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repeat(i, s, n) for (int(i) = s; (i) < (n); (i)++)
#define revrep(i, n) for (int(i) = (n)-1; i >= 0; i--)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(std::numeric_limits<float>::max_digits10);
  int n;
  cin >> n;
  multiset<ll> s;
  rep(i, n) {
    ll x;
    cin >> x;
    s.insert(x);
  }
  int ans = 0;
  while (!s.empty()) {
    auto m = s.end();
    m--;
    s.erase(m);
    ll y = 1;
    while (y <= *m)
      y *= 2;
    if (s.find(y - *m) != s.end()) {
      s.erase(s.find(y - *m));
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int to[N], n, x, cnt, ans;
map<int, int> Map;
int cmp(int x, int y) { return x > y; }
int main() {
  scanf("%d", &n);
  while (n--) {
    scanf("%d", &x);
    if (!Map.count(x))
      to[++cnt] = x;
    Map[x]++;
  }
  sort(to + 1, to + cnt + 1, cmp);
  for (int i = 1; i <= cnt; i++)
    for (int j = 0; j <= 30; j++)
      while (Map[to[i]] > 0 && Map[(1 << j) - to[i]] > 0) {
        if (to[i] * 2 == (1 << j) && Map[to[i]] == 1)
          break;
        Map[to[i]]--, Map[(1 << j) - to[i]]--, ans++;
      }
  printf("%d\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int to[N], n, x, cnt, ans;
map<int, int> Map;
int cmp(int x, int y) { return x > y; }
int main() {
  scanf("%d", &n);
  while (n--) {
    scanf("%d", &x);
    if (!Map.count(x))
      to[++cnt] = x;
    Map[x]++;
  }
  sort(to + 1, to + cnt + 1, cmp);
  for (int i = 1; i <= cnt; i++)
    for (int j = 0; j <= 30; j++)
      if ((1 << j) > to[i])
        while (Map[to[i]] > 0 && Map[(1 << j) - to[i]] > 0) {
          if (to[i] * 2 == (1 << j) && Map[to[i]] == 1)
            break;
          Map[to[i]]--, Map[(1 << j) - to[i]]--, ans++;
        }
  printf("%d\n", ans);
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>

std::map<int, int> mp;

int n, a[200005];

void init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    mp[a[i]] ? (++mp[a[i]]) : mp[a[i]] = 1;
  }
  std::map<int, int>::iterator it = mp.end();
  --it;

  int ans = 0, nw;
  while (1) {
    if (it->second == 0) {
      goto loop;
    }
    nw = it->first;
    for (int i = 30; i >= 1; --i) {
      while (it->second >= 1 && mp[(1 << i) - nw] >= 1 &&
             (nw != (1 << (i - 1)))) {
        --mp[(1 << i) - nw];
        it->second -= 1;
        ++ans;
      }
      while (it->second >= 2 && nw == (1 << (i - 1))) {
        it->second -= 2;
        ++ans;
      }
    }
  loop:
    if (it == mp.begin()) {
      break;
    }
    --it;
  }

  printf("%d\n", ans);
}

int main() {
  init();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>

std::map<int, int> mp;

int n, a[200005];

void init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    mp[a[i]] ? (++mp[a[i]]) : mp[a[i]] = 1;
  }
  std::map<int, int>::iterator it = mp.end();
  --it;

  int ans = 0, nw;
  while (1) {
    if (it->second == 0) {
      goto loop;
    }
    nw = it->first;
    for (int i = 30; i >= 1; --i) {
      while (it->second >= 1 && (1 << i) - nw >= 0 && mp[(1 << i) - nw] >= 1 &&
             (nw != (1 << (i - 1)))) {
        --mp[(1 << i) - nw];
        it->second -= 1;
        ++ans;
      }
      while (it->second >= 2 && nw == (1 << (i - 1))) {
        it->second -= 2;
        ++ans;
      }
    }
  loop:
    if (it == mp.begin()) {
      break;
    }
    --it;
  }

  printf("%d\n", ans);
}

int main() {
  init();
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

int main() {
  LL N;
  in >> N;
  std::vector<LL> A(N);
  rep(i, N) in >> A[i];

  std::map<LL, LL> cnt;
  rep(i, N)++ cnt[A[i]];

  LL ans = 0;
  for (LL b = 31; b >= 0; --b) {
    LL num = (1LL << b);
    for (auto &p : cnt) {
      if (p.first > 0) {
        if (cnt[num - p.first] > 0) {
          if (num == p.first * 2) {
            ans += p.second / 2;
            p.second = 0;
          } else {
            LL res = std::min(p.second, cnt[num - p.first]);
            ans += res;
            p.second -= res;
            cnt[num - p.first] -= res;
          }
        }
      }
    }
  }

  out << ans << std::endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

int main() {
  LL N;
  in >> N;
  std::vector<LL> A(N);
  rep(i, N) in >> A[i];

  std::map<LL, LL> cnt;
  rep(i, N)++ cnt[A[i]];

  LL ans = 0;
  for (LL b = 31; b >= 0; --b) {
    LL num = (1LL << b);
    for (auto &p : cnt) {
      if (p.first > 0) {
        if (cnt.count(num - p.first) > 0) {
          if (num == p.first * 2) {
            ans += p.second / 2;
            p.second = 0;
          } else {
            LL res = std::min(p.second, cnt[num - p.first]);
            ans += res;
            p.second -= res;
            cnt[num - p.first] -= res;
          }
        }
      }
    }
  }

  out << ans << std::endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>

#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <list>
#include <utility>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <unordered_map>
#include <unordered_set>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> l4;
typedef pair<double, double> dd;
#define mp make_pair
#define pb push_back

#define debug(x) cerr << #x << " = " << x << " "

int main() {
  int n;
  scanf("%d", &n);
  multiset<int> st;
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    st.insert(x);
  }
  vector<int> pw2(1, 1);
  while (*st.rbegin() * 2 > pw2.back())
    pw2.pb(pw2.back() << 1);
  int ans = 0;
  while (!st.empty()) {
    auto it = --st.end();
    int cur = *it;
    st.erase(it);
    while (!pw2.empty() && 2 * cur < pw2.back())
      pw2.pop_back();
    assert(!pw2.empty());
    int tar = pw2.back() - cur;
    it = st.find(tar);
    if (it != st.end()) {
      ++ans;
      st.erase(it);
    }
  }
  printf("%d\n", ans);
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>

#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <list>
#include <utility>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <unordered_map>
#include <unordered_set>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> l4;
typedef pair<double, double> dd;
#define mp make_pair
#define pb push_back

#define debug(x) cerr << #x << " = " << x << " "

int main() {
  int n;
  scanf("%d", &n);
  multiset<int> st;
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    st.insert(x);
  }
  const int P = 31;
  vector<int> pw2(P);
  for (int i = 0; i < P; ++i)
    pw2[i] = 1 << i;

  int ans = 0;
  while (!st.empty()) {
    auto it = --st.end();
    int cur = *it;
    st.erase(it);
    while (!pw2.empty() && 2 * cur < pw2.back())
      pw2.pop_back();
    assert(!pw2.empty());
    int tar = pw2.back() - cur;
    it = st.find(tar);
    if (it != st.end()) {
      ++ans;
      st.erase(it);
    }
  }
  printf("%d\n", ans);
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;

map<int, int> mapa;
int n, a, f, i, j, res, tab[200005];

int main() {
  scanf("%d", &n);
  res = 0;
  for (i = 0; i < n; i++)
    scanf("%d", &tab[i]);
  mapa.clear();
  sort(tab, tab + n);
  for (j = n - 1; j >= 0; j--) {
    a = tab[j];
    f = 0;
    for (i = 1; i <= (1 << 30); i *= 2)
      if (i > a && mapa[i - a] > 0) {
        mapa[i - a]--;
        f = 1;
        break;
      }
    res += f;
    if (!f)
      mapa[a]++;
  }
  printf("%d\n", res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;

map<int, int> mapa;
int n, a, f, i, j, res, tab[200005];

int main() {
  scanf("%d", &n);
  res = 0;
  for (i = 0; i < n; i++)
    scanf("%d", &tab[i]);
  mapa.clear();
  sort(tab, tab + n);
  for (j = n - 1; j >= 0; j--) {
    a = tab[j];
    f = 0;
    for (i = 1; i <= (1 << 30); i *= 2)
      if (i > a && mapa[i - a] > 0) {
        mapa[i - a]--;
        f = 1;
        break;
      } else if (i == (1 << 30))
        break;
    res += f;
    if (!f)
      mapa[a]++;
  }
  printf("%d\n", res);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define brep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define cY cout << "Yes" << endl
#define cN cout << "No" << endl
#define ll long long

ll pow2(ll p, ll n) {
  long long num = 1;
  while (n) {
    if (n % 2 == 1) {
      num *= p;
    }
    n /= 2;
    p *= p;
  }
  return num;
}

int main(void) {
  ll two[30];
  rep(i, 30) two[i] = pow2(2, i + 1);
  ll n;
  cin >> n;
  ll a[n];
  ll ans = 0;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);
  brep(i, 30) {
    int num = 0;
    brep(j, n) {
      if (a[j] == -INT_MAX || a[j] >= two[i])
        continue;
      for (int k = num; k < j; k++) {
        if (a[k] == -INT_MAX)
          continue;
        if (a[j] + a[k] == two[i]) {
          a[j] = -INT_MAX;
          a[k] = -INT_MAX;
          ans++;
          num = k + 1;
          break;
        } else if (a[j] + a[k] > two[i]) {
          num = k;
          break;
        }
      }
    }
  }
  // rep(i,n)cout <<a[i]<< endl;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define brep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define cY cout << "Yes" << endl
#define cN cout << "No" << endl
#define ll long long

ll pow2(ll p, ll n) {
  long long num = 1;
  while (n) {
    if (n % 2 == 1) {
      num *= p;
    }
    n /= 2;
    p *= p;
  }
  return num;
}

int main(void) {
  ll two[30];
  rep(i, 30) two[i] = pow2(2, i + 1);
  ll n;
  cin >> n;
  ll a[n];
  ll ans = 0;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);
  brep(i, 30) {
    int num = 0;
    brep(j, n) {
      if (a[j] == -INT_MAX || a[j] >= two[i])
        continue;
      if (a[j] < (two[i] + 1) / 2)
        break;

      for (int k = num; k < j; k++) {
        if (a[k] == -INT_MAX)
          continue;
        if (a[j] + a[k] == two[i]) {
          a[j] = -INT_MAX;
          a[k] = -INT_MAX;
          ans++;
          num = k + 1;
          break;
        } else if (a[j] + a[k] > two[i]) {
          num = k;
          break;
        }
      }
    }
  }
  // rep(i,n)cout <<a[i]<< endl;
  cout << ans << endl;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long t, n, a[200000 + 10], ans;
multiset<long long> st;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    st.insert(a[i]);
  }
  for (int i = 0; i < n; i++) {
    t = 1;
    for (int j = 1; j < 30 && t < 1e9 + 10; j++) {
      t *= 2;
      if (a[i] > t && st.find(a[i] - t) != st.end()) {
        ans++;
        st.erase(st.find(a[i] - t));
        st.erase(st.find(a[i]));
        break;
      }
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long t, n, a[200000 + 10], ans;
multiset<long long> st;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    st.insert(a[i]);
  }
  sort(a, a + n);
  for (int i = n - 1; i >= 0; i--) {
    if (st.find(a[i]) != st.end()) {
      t = 1;
      st.erase(st.find(a[i]));
      for (int j = 0; j < 40; j++) {
        t *= 2;
        if (st.find(t - a[i]) != st.end()) {
          st.erase(st.find(t - a[i]));
          ans++;
          break;
        }
      }
    }
  }
  cout << ans;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, n) for (long long i = 0; i < (n); ++i)
#define pb push_back
#define int long long
#define debug(x) std::cout << #x << ": " << x << '\n';
typedef long long ll;
const int N = 2e5 + 10;
int a[N];

signed main() {
  std::ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  std::map<int, int> cnt;
  FOR(i, n) {
    std::cin >> a[i];
    cnt[a[i]]++;
  }
  int answ = 0;
  FOR(lg, 31) {
    ll x = (1ll << (31 - lg));
    FOR(i, n) {
      if (cnt[a[i]] != 0 && cnt.find(x - a[i]) != cnt.end()) {
        if (x - a[i] == a[i]) {
          if (cnt[x - a[i]] >= 2) {
            cnt[x - a[i]] -= 2;
            answ++;
          }
        } else if (cnt[x - a[i]] >= 1) {
          cnt[a[i]]--;
          cnt[x - a[i]]--;
          answ++;
        }
      }
    }
  }
  std::cout << answ;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, n) for (long long i = 0; i < (n); ++i)
#define pb push_back
#define int long long
#define debug(x) std::cout << #x << ": " << x << '\n';
typedef long long ll;
const int N = 2e5 + 10;
int a[N];

signed main() {
  std::ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  std::unordered_map<int, int> cnt;
  FOR(i, n) {
    std::cin >> a[i];
    cnt[a[i]]++;
  }
  int answ = 0;
  FOR(lg, 31) {
    ll x = (1ll << (31 - lg));
    FOR(i, n) {
      if (cnt[a[i]] != 0 && cnt.find(x - a[i]) != cnt.end()) {
        if (x - a[i] == a[i]) {
          if (cnt[x - a[i]] >= 2) {
            cnt[x - a[i]] -= 2;
            answ++;
          }
        } else if (cnt[x - a[i]] >= 1) {
          cnt[a[i]]--;
          cnt[x - a[i]]--;
          answ++;
        }
      }
    }
  }
  std::cout << answ;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define int long long
#define all(a) a.begin(), a.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  rep(i, 0, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  int ans = 0;
  for (int i = 30; i >= 1; i--) {
    for (auto it = mp.begin(); it != mp.end(); it++) {
      if (it->second == 0)
        continue;
      int op = (1LL << i) - it->first;
      if (mp[op] == 0)
        continue;
      if (op == it->first) {
        ans += mp[op] / 2;
        mp[op] %= 2;
      } else {
        int tmp = min(it->second, mp[op]);
        ans += tmp;
        it->second -= tmp;
        mp[op] -= tmp;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define int long long
#define all(a) a.begin(), a.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  rep(i, 0, n) {
    cin >> a[i];
    mp[a[i]]++;
  }
  int ans = 0;
  for (int i = 30; i >= 1; i--) {
    for (auto it = mp.begin(); it != mp.end(); it++) {
      if (it->second == 0)
        continue;
      int op = (1LL << i) - it->first;
      if (op < it->first)
        break;
      if (mp[op] == 0)
        continue;
      if (op == it->first) {
        ans += mp[op] / 2;
        mp[op] %= 2;
      } else {
        int tmp = min(it->second, mp[op]);
        ans += tmp;
        it->second -= tmp;
        mp[op] -= tmp;
      }
    }
  }
  cout << ans << endl;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, x, count = 0;
  long int t;
  vector<int> num;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    num.push_back(x);
  }
  sort(num.begin(), num.end());
  while (num.size() > 1) {
    t = 1;
    x = num[num.size() - 1];
    // cout << x << endl;
    num.pop_back();
    while (t <= x) {
      t *= 2;
    }
    auto it = lower_bound(num.begin(), num.end(), t - x);
    if (*it == t - x) {
      // cout << "found" << *it <<endl;
      num.erase(it);
      count++;
    } else {
      continue;
    }
  }
  cout << count << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, x, count = 0;
  long int t;
  vector<int> num;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    num.push_back(x);
  }
  sort(num.begin(), num.end());
  while (num.size() > 1) {
    t = 1;
    x = num[num.size() - 1];
    // cout << x << endl;
    num.pop_back();
    while (t <= x) {
      t *= 2;
    }
    auto it = lower_bound(num.begin(), num.end(), t - x);
    if (it == num.end())
      continue;
    if (*it == t - x) {
      // cout << "found" << *it <<endl;
      num.erase(it);
      count++;
    } else {
      continue;
    }
  }
  cout << count << endl;

  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

unordered_map<int, int> mp;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int A[200010];
  rep(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }
  int num = (1 << 30);
  int ans = 0;
  rep(k, 31) {
    rep(i, N) {
      int remain = num - A[i];
      if (remain != A[i] && mp[remain] > 0 && mp[A[i]] > 0) {
        ans++;
        mp[remain]--;
        mp[A[i]]--;
        // cout << remain << " " << A[i] << endl;
      } else if (remain == A[i] && mp[A[i]] > 1) {
        ans++;
        mp[A[i]] -= 2;
        // cout << remain << " " << A[i] << " " << mp[A[i]] << endl;
      }
    }
    num >>= 1;
    // cout << num << endl;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

unordered_map<int, int> mp;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int A[200010];
  rep(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }
  int num = (1 << 30);
  int ans = 0;
  rep(k, 31) {
    rep(i, N) {
      int remain = num - A[i];
      if (remain <= 0)
        continue;
      if (remain != A[i] && mp[remain] > 0 && mp[A[i]] > 0) {
        ans++;
        mp[remain]--;
        mp[A[i]]--;
        // cout << remain << " " << A[i] << endl;
      } else if (remain == A[i] && mp[A[i]] > 1) {
        ans++;
        mp[A[i]] -= 2;
        // cout << remain << " " << A[i] << " " << mp[A[i]] << endl;
      }
    }
    num >>= 1;
    // cout << num << endl;
  }
  cout << ans << endl;

  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
using namespace std;
int a[200010];
int main() {
  int n;
  scanf("%d", &n);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    m[a[i]]++;
  }
  vector<pair<int, int>> b(m.begin(), m.end());
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    int x = b[i].first;
    for (int j = 30; j >= 0; j--) {
      if ((1 << j) <= x)
        break;
      if (b[i].second <= 0)
        break;
      int l = lower_bound(b.begin(), b.begin() + i + 1,
                          make_pair((1 << j) - x, -1)) -
              b.begin();
      if (l > i || b[l].first + x != (1 << j))
        continue;
      if (l == i) {
        ans += b[i].second / 2;
        b[i].second %= 2;
      } else {
        int t = min(b[i].second, b[l].second);
        ans += t;
        b[i].second -= t;
        b[l].second -= t;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
using namespace std;
int a[200010];
int main() {
  int n;
  scanf("%d", &n);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    m[a[i]]++;
  }
  vector<pair<int, int>> b(m.begin(), m.end());
  int ans = 0;
  for (int i = (int)b.size() - 1; i >= 0; i--) {
    int x = b[i].first;
    for (int j = 30; j >= 0; j--) {
      if ((1 << j) <= x)
        break;
      if (b[i].second <= 0)
        break;
      int l = lower_bound(b.begin(), b.begin() + i + 1,
                          make_pair((1 << j) - x, -1)) -
              b.begin();
      if (l > i || b[l].first + x != (1 << j))
        continue;
      if (l == i) {
        ans += b[i].second / 2;
        b[i].second %= 2;
      } else {
        int t = min(b[i].second, b[l].second);
        ans += t;
        b[i].second -= t;
        b[l].second -= t;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 200010;

int main() {
  int N;
  int A[MAX];
  map<long long, long long> table;
  int ans = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (table.count(A[i]) == 0)
      table[A[i]] = 0;
    table[A[i]]++;
  }
  sort(A, A + N);

  for (int i = N - 1; i >= 0; i--) {
    long long n = 1;
    while (n <= A[i])
      n = n * n;
    if (A[i] == n * 2 && table[A[i]] >= 2) {
      ans++;
      table[A[i]] -= 2;
    } else if (table[A[i]] >= 1 && table[n - A[i]] >= 1) {
      ans++;
      table[A[i]]--;
      table[n - A[i]]--;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 200010;

int main() {
  int N;
  int A[MAX];
  map<long long, long long> table;
  int ans = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (table.count(A[i]) == 0)
      table[A[i]] = 0;
    table[A[i]]++;
  }
  sort(A, A + N);

  for (int i = N - 1; i >= 0; i--) {
    long long n = 1;
    while (n <= A[i])
      n <<= 1;
    if (A[i] * 2 == n) {
      if (table[A[i]] >= 2) {
        ans++;
        table[A[i]] -= 2;
      }
    } else if (table[A[i]] >= 1 && table[n - A[i]] >= 1) {
      ans++;
      table[A[i]]--;
      table[n - A[i]]--;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pint;
typedef pair<ll, int> P;
// typedef tuple<int,int,int> T;
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
ll MOD = 1000000007;
ll r(ll x) {
  ll xdash = x;
  int cnt = 0;
  while (xdash != 0) {
    cnt++;
    xdash /= 2;
  }
  return pow(2, cnt) - x;
}

int main(void) {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  A.push_back(INFL);
  sort(A.begin(), A.end());

  vector<pair<ll, int>> Apair(0);
  ll ad = A.at(0);
  int c = 1;
  for (int i = 1; i <= N; i++) {
    if (A.at(i) != ad) {
      Apair.push_back({ad, c});
      c = 1;
      ad = A.at(i);
    } else {
      c++;
    }
  }

  ll cnt = 0;
  for (int i = Apair.size() - 1; i >= 0; i--) {
    while (Apair.at(i).second != 0) {
      Apair.at(i).second--;
      ll now = Apair.at(i).first;
      ll R = r(now);

      int left = -1;
      int right = N;
      int mid;
      while (right - left > 1) {
        mid = (left + right) / 2;
        if (Apair.at(mid).first > R) {
          right = mid;
        } else {
          left = mid;
        }
      }
      if (left != -1 && Apair.at(left).first == R &&
          Apair.at(left).second != 0) {
        cnt++;
        Apair.at(left).second--;
      }
    }
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pint;
typedef pair<ll, int> P;
// typedef tuple<int,int,int> T;
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
ll MOD = 1000000007;
ll r(ll x) {
  ll xdash = x;
  int cnt = 0;
  while (xdash != 0) {
    cnt++;
    xdash /= 2;
  }
  return pow(2, cnt) - x;
}

int main(void) {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  A.push_back(INFL);
  sort(A.begin(), A.end());

  vector<pair<ll, int>> Apair(0);
  ll ad = A.at(0);
  int c = 1;
  for (int i = 1; i <= N; i++) {
    if (A.at(i) != ad) {
      Apair.push_back({ad, c});
      c = 1;
      ad = A.at(i);
    } else {
      c++;
    }
  }

  ll cnt = 0;
  for (int i = Apair.size() - 1; i >= 0; i--) {
    while (Apair.at(i).second != 0) {
      Apair.at(i).second--;
      ll now = Apair.at(i).first;
      ll R = r(now);

      int left = -1;
      int right = Apair.size();
      int mid;
      while (right - left > 1) {
        mid = (left + right) / 2;
        if (Apair.at(mid).first > R) {
          right = mid;
        } else {
          left = mid;
        }
      }
      if (left != -1 && Apair.at(left).first == R &&
          Apair.at(left).second != 0) {
        cnt++;
        Apair.at(left).second--;
      }
    }
  }
  cout << cnt << endl;
}
Problem p03201 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define F2(i, a, b) for (int i = a; i < (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define dF2(i, a, b) for (int i = a; i > (b); --i)
#define eF(i, u) for (int i = h[u]; i; i = nxt[i])
#define SZ(x) ((int)(x).size())
#define prts(x) return puts(x), 0
#define pirtf(x) return printf("%d\n", x), 0
#define re return
typedef long long LL;
const int INF = 0x3f3f3f3f;

map<int, int> mp;

int n, q, m, k, Ans;
int a[200005];

int main() {
  scanf("%d", &n);
  F(i, 1, n) scanf("%d", a + i), ++mp[a[i]];
  for (int j = 1 << 30; j; j >>= 1) {
    F(i, 1, n) {
      if (mp[a[i]] > 0 && mp[j - a[i]] > 0) {
        if (a[i] != j - a[i]) {
          ++Ans, --mp[a[i]], --mp[j - a[i]];
        } else {
          if (mp[a[i]] > 1) {
            ++Ans;
            mp[a[i]] -= 2;
          }
        }
      }
    }
  }
  printf("%d\n", Ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define F2(i, a, b) for (int i = a; i < (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define dF2(i, a, b) for (int i = a; i > (b); --i)
#define eF(i, u) for (int i = h[u]; i; i = nxt[i])
#define SZ(x) ((int)(x).size())
#define prts(x) return puts(x), 0
#define pirtf(x) return printf("%d\n", x), 0
#define re return
typedef long long LL;
const int INF = 0x3f3f3f3f;

map<int, int> mp;

int n, q, m, k, Ans;
int a[200005];

int main() {
  scanf("%d", &n);
  F(i, 1, n) scanf("%d", a + i), ++mp[a[i]];
  sort(a + 1, a + n + 1);
  dF(i, n, 1) {
    if (mp[a[i]] == 0)
      continue;
    dF(j, 30, 0) {
      int v = (1 << j) - a[i], o = 0;
      if (v < 0)
        break;
      if (v == a[i])
        o = 1;
      if (mp[v] > o) {
        ++Ans, --mp[v], --mp[a[i]];
        break;
      }
    }
  }
  printf("%d\n", Ans);
  return 0;
}