We need to count number of pairs (i<j) such that f(ai) = f(aj). f is defined recursively: f(0)=0; f(2x)=f(x); f(2x+1)=f(x)+1.
We need to compute f(x) for each ai. The function f(x) counts the number of 1s in the binary representation of x? Let's test: f(0)=0. For x>0, we can see that f(x) = number of 1 bits (popcount) because: when x is even, binary representation ends with 0, shifting right (divide by 2) doesn't change popcount; when odd, ends with 1, shifting right gives popcount = f(x) = f(floor(x/2)) + 1. Indeed, recursion: f(x) = popcount(x). Let's confirm: for x=1 (binary 1): f(1)=f(0)+1=1. x=2 (10): f(2)=f(1)=1. x=3 (11): f(3)=f(1)+1=2. x=4 (100): f(4)=f(2)=f(1)=1. Works. So f(ai) is simply the count of 1 bits in ai.
Thus we need to count pairs of indices with equal popcount.
Given n up to 1e5, ai up to 1e9 (max bits 30). Popcount values are in range 0 to at most 30 (since 1e9 < 2^30). Actually 2^30 ≈ 1.07e9, so up to 30 bits, max popcount 30. So we can compute popcount for each ai, and then count frequencies of each popcount value. Then for each popcount v, number of pairs = C(freq[v],2) = freq[v]*(freq[v]-1)/2. Sum over v.
That solves it.
Complexities: O(n * log(ai)) or O(n) if using builtin popcount. But simple loop: while x>0: cnt+=x&1; x>>=1; that's O(bits) per number, bits ~30, fine.
Edge cases: n=1 => answer 0.
Examples: first: 1,2,4 -> popcounts: 1 (1),1 (2),1 (4) => freq[1]=3 => pairs=3. Output 3.
Second: 5 (101 popcount 2), 3 (11 popcount 2), 1 (1 popcount 1). freq[2]=2 => pairs=1. Output 1.
So implement.
But careful: The function f defined also for x=0, but ai positive, so no zero.
Thus code: read n, read array. For each a, compute popcount (e.g., __builtin_popcount for gcc, but for portability we can implement). Since numbers are up to 1e9, popcount can be done with while.
Then use map or array of size maybe 31 or 32 for frequencies (0..31). For safety, popcount could be up to 31 if ai = 2^31-1 ~ 2.147e9 but ai <= 1e9, so max 30 bits? Actually 1e9 approx 2^30 (1,073,741,824) so numbers up to 1e9 could be as high as 1,073,741,823? But constraints say 1 ≤ ai ≤ 10^9, exactly 1,000,000,000. So maximum popcount: binary of 10^9? Let's compute: 10^9 = 111011100110101100101000000000? Actually we can compute quickly but it doesn't matter; popcount at most 30 because 2^30 > 1e9, so max bits needed 30, but popcount can be up to 30 (if number is 2^30-1 ~ 1,073,741,823 but that's >1e9? Actually 2^30-1 = 1,073,741,823 > 10^9, so numbers up to 1e9 might have popcount up to? Consider 999,999,999? We can approximate: max popcount for numbers <= 1e9 is when number has as many ones as possible while <=1e9. The maximum popcount maybe 28? Actually 2^30 -1 is >1e9, but 2^30-2^0? That's still >1e9. The largest number with 30 ones would be 2^30 -1, which is >1e9, so not allowed. Let's find largest number ≤1e9 with many ones: e.g., 101111111111111111111111111111? But that's >1e9 maybe. We can just allocate array size 31 or 64 to be safe. Since popcount for 32-bit numbers can be up to 32, but with ai ≤ 1e9 < 2^30, popcount ≤ 30, but maybe 30? For example, 536870911 = 2^29 -1 has popcount 29. 2^30 - 1 is >1e9 so not allowed. So max popcount 29? Actually 2^29 = 536,870,912. So numbers up to 1e9 can have bits up to 29 (since 2^29 < 1e9, 2^30 > 1e9). So maximum bit index is 29 (0-indexed). So max popcount could be 30? Wait: if we use 30 bits, the highest bit index is 29 (since 2^29 is about 5.37e8). 2^30 = 1.07e9, which is >1e9, so the 30th bit (index 30) cannot be set because that would be 2^30 = 1.07e9 > 10^9, unless the number is exactly 2^30? But 2^30 > 10^9, so not allowed. So the maximum bit index that can be set is 29. So numbers have at most 30 bits? Actually bits from 0 to 29 inclusive, that's 30 bits. But the highest value if all bits 0..29 are set would be 2^30 - 1 = 1,073,741,823 > 1e9. So you cannot have all 30 bits set. So maximum popcount is less than 30. Possibly 29? For example, set bits 0..28 inclusive: that sum is 2^29 - 1 = 536,870,911 (popcount 29). That's allowed. You can also set bits 0..29 except some to stay <=1e9. For instance, set bit 29 and many lower bits: 2^29 = 536,870,912. Then we can add up to 463,129,088 to reach 1e9. The maximum popcount might be something like 28? Let's compute the maximum popcount for numbers ≤1e9: you want as many 1s as possible. The strategy: use the largest number less than or equal to 1e9 with many ones. That would be something like 1e9 in binary? Let's compute quickly: 1e9 decimal = 111011100110101100101000000000 (binary). That has some popcount. But maybe we can get a higher popcount by using a number with more ones, e.g., 2^30 - 1 - something? But 2^30 -1 is too large; we can subtract some amount to bring below 1e9. If we subtract a large power of two, we lose many ones? Actually subtracting can reduce popcount drastically. Alternative: consider the number composed of many low bits set: the sum of the first k bits (0..k-1) is 2^k - 1. For k=30, sum = 1,073,741,823 > 1e9, so we can't have all 30 bits set. For k=29, sum = 536,870,911, which is below 1e9 and has popcount 29. Could we have a number with popcount 30 that is ≤1e9? That would require 30 ones, meaning the sum of 30 distinct powers of two. The minimum sum for 30 distinct powers of two is the sum of the 30 smallest powers: 2^0 + 2^1 + ... + 2^29 = 2^30 - 1 = 1,073,741,823 > 1e9. So any number with 30 ones is at least that sum. So impossible. Therefore max popcount is 29. So we can safely allocate array of size 31 (0..30). Or just use map.
But easier: we can use unordered_map or just vector freq(32,0). Compute popcount for each a, increment freq[pc]. Then iterate v from 0 to 31, sum += (long long)freq[v]*(freq[v]-1)/2.
Implementation details: n up to 1e5, answer up to C(n,2) ≈ 5e9, fits in 64-bit. Use long long.
Check example.
We need to adhere to input/output spec: "don't use %lld, use cin,cout or %I64d". In C++ we can use long long and cout.
Thus solution straightforward.
But wait: The problem defines f recursively as given. It's clearly popcount. So answer is count pairs with same popcount.
Let's provide solution.
Now, maybe there is a twist: The function f is defined with recurrence for all nonnegative integers. But that's exactly the binary ones count.
Thus final code:
#include
#include
using namespace std;
int popcount(int x) {
int cnt = 0;
while (x) {
cnt += x & 1;
x >>= 1;
}
return cnt;
}
int main() {
int n;
cin >> n;
vector freq(32, 0);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
int pc = popcount(a);
freq[pc]++;
}
long long ans = 0;
for (int i = 0; i < 32; ++i) {
long long f = freq[i];
ans += f * (f - 1) / 2;
}
cout << ans << endl;
return 0;
}
That should be correct.
But we can also use __builtin_popcount for speed.
One more note: ai up to 1e9, int is fine.
Thus answer.
I'll produce final answer with explanation. The function (f(x)) defined by the recurrence
- (f(0) = 0)
- (f(2x) = f(x))
- (f(2x + 1) = f(x) + 1)
is exactly the number of 1-bits in the binary representation of (x) (the popcount).
Therefore, for each number (a_i) we need to compute its popcount, and then count the number of index pairs ((i, j)) with (i < j) and equal popcounts.
Since all (a_i \le 10^9), the popcount is at most 29 (actually at most 30, but we can safely use size 32).
Compute the frequency of each popcount value, and for each value (v) add (\binom{freq[v]}{2} = \frac{freq[v] \cdot (freq[v] - 1)}{2}) to the answer.
The total number of pairs can be up to (\binom{10^5}{2} \approx 5 \times 10^9), which fits in a 64‑bit integer.
Time complexity: (O(n \log a_i)) or (O(n)) with built‑in popcount, which is easily within limits.
#include <iostream>
#include <vector>
using namespace std;
int popcount(int x) {
int cnt = 0;
while (x) {
cnt += x & 1;
x >>= 1;
}
return cnt;
}
int main() {
int n;
cin >> n;
vector<int> freq(32, 0);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
++freq[popcount(a)];
}
long long ans = 0;
for (int i = 0; i < 32; ++i) {
long long f = freq[i];
ans += f * (f - 1) / 2;
}
cout << ans << endl;
return 0;
}