1466E - Apollo Versus Pan

Editorial is pretty cool.

Time complexity: $O(n \log{x_{max}})$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using namespace atcoder;
using ll = long long;
using vi = vector<int>;
using mint = modint1000000007;
int const BITSZ = 60;
int solve(vector<ll> a)
{
    int n = (int)(a).size();
    vi cnt(BITSZ, 0);
    mint ans;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < BITSZ; ++j)
            cnt[j] += (a[i] >> j) & 1;
    for (int j = 0; j < n; ++j)
    {
        mint sum_or = 0, sum_and = 0;
        for (int b = 0; b < BITSZ; ++b)
        {
            if ((a[j] >> b) & 1)
            {
                sum_or += mint(1LL << b) * n;
                sum_and += mint(1LL << b) * cnt[b];
            }
            else
                sum_or += mint(1LL << b) * cnt[b];
        }
        ans += sum_or * sum_and;
    }
    return ans.val();
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<ll> a(n);
        for (auto &x : a)
            cin >> x;
        cout << solve(a) << endl;
    }
    return 0;
}