結果

問題 No.737 PopCount
ユーザー ei1333333ei1333333
提出日時 2018-05-02 22:31:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,009 bytes
コンパイル時間 1,983 ms
コンパイル使用メモリ 196,012 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 09:18:03
合計ジャッジ時間 2,500 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

int64 N;
int dp[64][2][64];
int dp2[64][2][64];

int ways(int idx, bool less, int popcount) {
  if(idx == -1) return popcount;
  if(~dp2[idx][less][popcount]) return dp2[idx][less][popcount];
  int ret = ways(idx - 1, less | ((N >> idx) & 1), popcount);
  if(less || ((N >> idx) & 1)) ret += ways(idx - 1, less, popcount + 1);
  return dp2[idx][less][popcount] = ret % mod;
}

int rec(int idx, bool less, int popcount) {
  if(idx == -1) return 0;
  if(~dp[idx][less][popcount]) return dp[idx][less][popcount];
  int64 ret = rec(idx - 1, less | ((N >> idx) & 1), popcount);
  if(less || ((N >> idx) & 1)) {
    ret += rec(idx - 1, less, popcount + 1);
    ret += 1LL * ways(idx - 1, less, popcount + 1) * ((1LL << idx) % mod) % mod;
  }
  return dp[idx][less][popcount] = ret % mod;
}

int main() {
  cin >> N;
  memset(dp, -1, sizeof(dp));
  memset(dp2, -1, sizeof(dp2));
  cout << rec(62, false, 0) << endl;
}
0