classSolution { public: staticconstexprint MOD = 1e9 + 7; inlinelonglongksm(longlong a, int b){ longlong res = 1; for (; b; a = a * a, b >>= 1) { if (b & 1) res = res * a; } return res; }
intcountPairs(vector<int>& d){ int n = d.size(); vector<longlong> target; for (int i = 0; i <= 21; i++) { target.push_back(ksm(2, i)); } int mx = -1; for (int i = 0; i < n; i++) mx = max(mx, d[i]); unordered_map<longlong, int> hash; // expected - cnt longlong res = 0; for (int i = 0; i < n; i++) { if (hash.count(d[i])) { res += hash[d[i]]; } //cout << d[i] << "---" << endl; for (int j = 0; j <= 21; j++) { if (target[j] - d[i] > mx) break; if (target[j] - d[i] >= 0) { hash[target[j] - d[i]]++; //cout << target[j] - d[i] << '-' << hash[target[j] - d[i]] << endl; } } } return res % MOD; } };