結果

問題 No.737 PopCount
ユーザー fine
提出日時 2018-09-29 00:14:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 2,362 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 171,052 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-12 07:45:04
合計ジャッジ時間 2,679 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0