結果

問題 No.737 PopCount
ユーザー kazumakazuma
提出日時 2018-09-28 21:44:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,390 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 210,152 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-02 10:55:24
合計ジャッジ時間 3,125 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<int MOD>
struct mod_int {
	static const int Mod = MOD;
	unsigned x;
	mod_int() : x(0) { }
	mod_int(int sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	mod_int(long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	mod_int &operator+=(mod_int that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator-=(mod_int that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator*=(mod_int that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	mod_int &operator/=(mod_int that) { return *this *= that.inverse(); }

	mod_int operator+(mod_int that) const { return mod_int(*this) += that; }
	mod_int operator-(mod_int that) const { return mod_int(*this) -= that; }
	mod_int operator*(mod_int that) const { return mod_int(*this) *= that; }
	mod_int operator/(mod_int that) const { return mod_int(*this) /= that; }

	mod_int inverse() const {
		long long a = x, b = MOD, u = 1, v = 0;
		while (b) {
			long long t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		return mod_int(u);
	}
};

template<int MOD>
istream& operator >> (istream& is, mod_int<MOD>& val) {
	long long x;
	is >> x; val = x;
	return is;
}

template<int MOD>
ostream& operator << (ostream& os, const mod_int<MOD>& val) {
	os << val.get();
	return os;
}

using mint = mod_int<1000000007>;

mint digit_dp(ll n) {
	vector<int> s;
	if (n == 0) s.push_back(0);
	while (n) s.push_back(n % 2), n /= 2;
	reverse(s.begin(), s.end());
	n = s.size();
	vector<vector<vector<mint>>> cnt(n + 1, vector<vector<mint>>(2, vector<mint>(61)));
	vector<vector<vector<mint>>> dp(n + 1, vector<vector<mint>>(2, vector<mint>(61)));
	cnt[0][0][0] = 1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 2; j++) {
			int lim = j ? 1 : s[i];
			for (int d = 0; d <= lim; d++) {
				for (int k = 0; k <= 60; k++) {
					if (k + d <= 60) {
						cnt[i + 1][j || d < lim][k + d] += cnt[i][j][k];
						dp[i + 1][j || d < lim][k + d] += dp[i][j][k] + cnt[i][j][k] * ((ll)d << (ll)(n - 1 - i));
					}
				}
			}
		}
	}
	mint res = 0;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j <= 60; j++) {
			res += dp[n][i][j] * j;
		}
	}
	return res;
}

int main()
{
	ll N;
	cin >> N;
	cout << digit_dp(N) << endl;
	return 0;
}
0