結果

問題 No.737 PopCount
ユーザー @abcde
提出日時 2019-04-20 09:21:25
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 5,361 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 164,476 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-25 06:26:59
合計ジャッジ時間 2,264 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL MOD = 1e9 + 7;
const LL LIMIT = 61;
LL FAC[LIMIT + 1];
LL pow2[LIMIT + 1];
LL INV[LIMIT + 1];
// Use of dynamic bitset to convert decimal numbers.
// https://stackoverflow.com/questions/42759330/use-of-dynamic-bitset-to-convert-decimal-numbers
// 10 → 2.
// @param n: 210.
// @param d: 2.
// @return: 2.
string decimalToBinary(LL n, int d) {
// 1. .
string ret = "";
if(n == 0){
while(ret.size() < d) ret = "0" + ret;
return ret;
}
// 2. .
LL ln = abs(n);
do{
ret = string((ln & 1) ? "1" : "0") + ret;
}while((ln /= 2) > 0);
while(ret.size() < d) ret = "0" + ret;
// 3. .
if(n < 0) ret = "-" + ret;
return ret;
}
// Fermat's little theorem , .
// @param a: .
// @param b: .
// @return: (mod).
LL inverse(LL a, LL b){
LL t = 1;
while(b) {
if(b & 1) t = (t * a) % MOD;
a *= a;
a %= MOD;
b >>= 1;
}
return t % MOD;
}
// (nCk)(mod).
// ※FAC, INV , 使.
// @param P_: .
// @param Q_: .
// @return: (nCk).
LL comb(LL P_, LL Q_){
if(P_ < 0 || Q_ < 0 || Q_ > P_) return 0;
LL ret = FAC[P_] * INV[Q_] % MOD * INV[P_ - Q_] % MOD;
return ret;
}
// PopCount .
// @param b: 2.
// @param r: (※r).
// @param l: (※l).
// @return: PopCount .
LL subPopCount(string b, int r, int l) {
// 1. 1 .
int lOne = 0;
for(int i = 0; i < r; i++) if(b[i] == '1') lOne++;
// 2. base .
LL base = 0;
for(int i = 0; i < r; i++) base += pow2[i + l] * (b[r - 1 - i] - '0'), base %= MOD;
// 3. PopCount .
// ex.
// 1010XXX , XXX 1010000 , .
// 3-1. XXX .
LL lx = 0;
for(int i = 1; i <= l; i++) lx += comb(l - 1, i - 1) * (i + lOne), lx %= MOD;
lx *= (pow2[l] - 1);
lx %= MOD;
// 3-2. 1010000 .
LL rx = 0;
for(int i = 0; i <= l; i++) rx += comb(l, i) * (i + lOne), rx %= MOD;
rx *= base;
rx %= MOD;
// 3-3. .
LL res = lx + rx;
res %= MOD;
// cout << "r=" << r << " l=" << l << endl;
// cout << "base=" << base << " lOne=" << lOne << " lx=" << lx << " rx=" << rx << " res=" << res << endl;
return res;
}
int main() {
// 1. .
LL N;
cin >> N;
// 2. .
// 2-1. , .
FAC[0] = 1;
for(int i = 1; i < LIMIT; i++) FAC[i] = i * FAC[i - 1] % MOD;
for(int i = 0; i < LIMIT; i++) INV[i] = inverse(FAC[i], MOD - 2) % MOD;
// for(int i = 0; i < LIMIT; i++) cout << "FAC[" << i << "]=" << FAC[i] << " INV[" << i << "]=" << INV[i] << endl;
// 2-2. 2.
pow2[0] = 1;
for(int i = 1; i < LIMIT; i++) pow2[i] = 2 * pow2[i - 1], pow2[i] %= MOD;
// for(int i = 0; i < LIMIT; i++) cout << pow2[i] << endl;
// 2-3. 2.
string binN = decimalToBinary(N, LIMIT);
// cout << binN << endl;
// 3. V(1) V(N) .
// 1e18 , 110111100000101101101011001110100111011001000000000000000000 (60)
// , 61調.
//
// ex.
// V(80) V(87) .
// 1010000 (80) 1010001 (81) 1010011 (83) 1010111 (87)
// 1010010 (82) 1010101 (85)
// 1010100 (84) 1010110 (86)
// -> 1010XXX ,
// XXX , (2 3 - 1) * (2C0 * 3 + 2C1 * 4 + 2C2 * 5) = 7 * (3 + 8 + 5) = 112.
// 1010000 , (2 6 + 2 4) * (3C0 * 2 + 3C1 * 3 + 3C2 * 4 + 3C3 * 5) = 80 * (2 + 9 + 12 + 5)
// = 2240.
// -> 2352 , OK .
LL t1 = subPopCount("1010000", 4, 3); // base=80 lOne=2 lx=112 rx=2240 res=2352
LL t2 = subPopCount("01111", 1, 4); // base=0 lOne=0 lx=300 rx=0 res=300
LL t3 = subPopCount("10111", 2, 3); // base=16 lOne=1 lx=84 rx=320 res=404
// cout << t1 << " " << t2 << " " << t3 << endl;
// 3-1. binN (i = 0), 1(i = LIMIT - 2), .
LL ans = 0;
for(int i = 0; i < LIMIT - 1; i++){
if(binN[i] == '1'){
string tBin = binN;
tBin[i] = '0';
ans += subPopCount(tBin, i + 1, LIMIT - i - 1);
ans %= MOD;
}
}
// 3-2. binN (i = LIMIT - 1).
if(binN[LIMIT - 1] == '0') ans += subPopCount(binN, LIMIT, 0), ans %= MOD;
if(binN[LIMIT - 1] == '1') ans += subPopCount(binN, LIMIT - 1, 1), ans %= MOD;
// 4. .
cout << ans << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0