結果

問題 No.737 PopCount
ユーザー finefine
提出日時 2018-09-29 00:14:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 2,362 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 166,632 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 12:24:20
合計ジャッジ時間 2,182 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

ll modpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

ll f[70], fi[70];

void init(int sz) {
    f[0] = 1;
    for (int i = 1; i <= sz; i++) f[i] = f[i - 1] * i % MOD;
    fi[sz] = modpow(f[sz], MOD - 2);
    for (int i = sz; i > 0; i--) {
        fi[i - 1] = fi[i] * i % MOD;
    }
}

ll comb(ll r, ll c) {
    if (r < c) return 0;
    return f[r] * fi[c] % MOD * fi[r - c] % MOD;
}

ll calc1(int r, int c) {
    return comb(r, c) * c % MOD * modpow(r, MOD - 2) % MOD * (((1LL << r) - 1) % MOD) % MOD;
}

ll solve2(vector<int>& v, int cur) {
    if (cur == v.size()) return 1;
    return (comb(v[cur], (int)v.size() - cur) + solve2(v, cur + 1)) % MOD;
}

ll solve1(vector<int>& v, int cur) {
    if (cur == v.size()) return 0;
    return (calc1(v[cur], (int)v.size() - cur) + solve1(v, cur + 1) + solve2(v, cur + 1) * ((1LL << v[cur]) % MOD)) % MOD;
}

ll solve(ll n) {
    ll res = 0;
    for (ll i = 1; i <= n; i++) (res += i * __builtin_popcountll(i)) %= MOD;
    return res;
}

ll compute(ll n, int sz) {
    ll ans = 0;
    for (int i = 1; i <= sz; i++) {
        if ((1LL << i) - 1 > n) break;
        vector<int> v;
        ll tmp = 0;
        int prv = sz;
        for (int j = 1; j <= i; j++) {
            int cur = i - j;
            while (cur < prv - 1) {
                ll hoge = tmp + (1LL << cur) + ((1LL << (i - j)) - 1);
                if (hoge > n) {
                    cur--;
                    break;
                }
                cur++;
            }
            ll hoge = tmp + (1LL << cur) + ((1LL << (i - j)) - 1);
            if (hoge > n) cur--;
            prv = cur;
            //cout << i << " " << cur << endl;
            v.push_back(cur);
            tmp += (1LL << cur);
        }
        (ans += solve1(v, 0) * i % MOD) %= MOD;
        //cout << tmp << endl;
        //cout << tmp << " " << solve1(v, 0) << endl;
        //cout << ans << endl;
    }
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    int sz = 0;
    while (n >= (1LL << sz)) sz++;
    init(sz);

    ll ans = compute(n, sz);
    cout << ans << endl;
    return 0;
}
0