結果

問題 No.737 PopCount
ユーザー startcppstartcpp
提出日時 2018-12-29 18:23:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 915 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 56,744 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 20:20:45
合計ジャッジ時間 1,190 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#define int long long
using namespace std;

int mod = 1000000007;
int inv2 = 500000004;
int g[60];	//popcount(0)~popcount(2^n - 1)の和
int f[60];	//V(0)~V(2^n - 1)の和。ただしV(i) = i * popcount(i)

//l + … + r - 1
int wa(int l, int r) {
	int n = r - l;
	n %= mod;
	l %= mod;
	return (n * (n - 1) % mod * inv2 + l * n % mod) % mod;
}

signed main() {
	int i;
	
	g[0] = 0;
	for (i = 1; i < 60; i++) {
		g[i] = 2 * g[i - 1] + (1LL << (i - 1)) % mod;
		g[i] %= mod;
	}
	
	f[0] = 0;
	for (i = 1; i < 60; i++) {
		f[i] = 2 * f[i - 1] + (1LL << (i - 1)) % mod * g[i - 1] + wa(1LL << (i - 1), 1LL << i);
		f[i] %= mod;
	}
	
	int n;
	cin >> n;
	n++;
	
	int l = 0, cnt = 0;
	int ans = 0;
	for (i = 59; i >= 0; i--) {
		if ((n >> i) & 1) {
			ans += f[i] + l % mod * g[i] + cnt * wa(l, l + (1LL << i));
			ans %= mod;
			cnt++;
			l += (1LL << i);
		}
	}
	
	cout << ans << endl;
	return 0;
}
0