Language: C++

#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<ld> vld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)


int main(int argc, const char * argv[]) {
    ios_base::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<ll> vll(N);
    for (int i = 0; i < N; ++i) {
        cin >> vll[i];
    }
    size_t L = vll.size();
    sort(vll.begin(), vll.end());
    
    vector<vi> G(N);
    for (int l = 0; l < L; ++l) {
        for (int t = 1; t <= 30; ++t) {
            auto it = find(vll.begin(), vll.end(), (1 << t) - vll[l]);
            if (it != vll.end()) {
                int m = (int)(it - vll.begin());
                if (m == l) break;
                G[l].push_back(m);
            }
        }
    }

    vi color(N, -1);
    function<void(int, int)> dfsC = [&](int v, int c)->void
    {
        for (int u: G[v]) {
            if (color[u] == -1) {
                color[u] = c;
                dfsC(u, c);
            }
        }
    };
    int C = 0;
    vi rep;
    for (int n = 0; n < N; ++n) {
        if (color[n] == -1) {
            color[n] = C;
            rep.push_back(n);
            dfsC(n, C);
            ++C;
        }
    }
    
    int res = 0;
    for (int c = 0; c < C; ++c) {
        int a = rep[c];
        int max = 0;
        for (int b : G[a]) {
            vi us(N, 0);
            int tmp = 0;
            
            function<void(pii)> dfs = [&](pii e)
            {
                ++tmp;
                int v = e.first;
                for (int u : G[v]) {
                    if (us[u]) continue;
                    us[u] = 1;
                    for (int x : G[u]) {
                        if (us[x]) continue;
                        us[x] = 1;
                        dfs({u, x});
                    }
                }
                v = e.second;
                for (int u : G[v]) {
                    if (us[u]) continue;
                    us[u] = 1;
                    for (int x : G[u]) {
                        if (us[x]) continue;
                        us[x] = 1;
                        dfs({u, x});
                    }
                }
            };
            
            us[a] = us[b] = 1;
            dfs({a, b});
            
            if (tmp > max) max = tmp;
        }
        res += max;
    }
    
    cout << res << endl;
    
    return 0;
}


Compiler output:
a.cc: In function 'int main(int, const char**)':
a.cc:52:5: error: 'function' was not declared in this scope
   52 |     function<void(int, int)> dfsC = [&](int v, int c)->void
      |     ^~~~~~~~
a.cc:10:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    9 | #include <map>
  +++ |+#include <functional>
   10 | 
a.cc:52:27: error: expression list treated as compound expression in functional cast [-fpermissive]
   52 |     function<void(int, int)> dfsC = [&](int v, int c)->void
      |                           ^
a.cc:52:14: error: expected primary-expression before 'void'
   52 |     function<void(int, int)> dfsC = [&](int v, int c)->void
      |              ^~~~
a.cc:67:13: error: 'dfsC' was not declared in this scope
   67 |             dfsC(n, C);
      |             ^~~~
a.cc:80:22: error: expected primary-expression before 'void'
   80 |             function<void(pii)> dfs = [&](pii e)
      |                      ^~~~
a.cc:106:13: error: 'dfs' was not declared in this scope
  106 |             dfs({a, b});
      |             ^~~