結果

問題 No.737 PopCount
ユーザー potoooooooopotoooooooo
提出日時 2018-09-29 00:08:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,068 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 165,596 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 12:21:15
合計ジャッジ時間 2,039 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007

long long modpow(int n, int r) {
  long long ret = 1; long long tmp = (long long) n;
  while (r != 0) {
    if (r % 2) ret *= tmp;
    tmp *= tmp; tmp %= mod; ret %= mod;
    r /= 2;
  }
  return ret;
}

int main(){
  long long n; cin >> n;
  long long ans = 0; long long k = 0;
  long long inv2 = modpow(2, mod-2);
  long long c = n;
  while (c > 0) k++, c /= 2;
  for (int digit = 0; digit < k; digit++) {
    int di = digit + 1;
    if (n & (1LL << digit)) {
      long long han = (n >> digit) << digit;
      long long tmp = ((n+han)%mod) * ((n-han+1)%mod);
      ans += ((tmp%mod)*inv2) % mod; ans %= mod;
    }
    long long u = (n >> di); long long v = (1LL << digit);
    long long ini = (((v%mod)*((v-1)%mod))%mod)*inv2%mod;
    long long diff = (v%mod*2) * (v%mod) % mod;
    ans += (u % mod) * (((v%mod) * (v%mod)) % mod);
    ans %= mod;
    ans += ((ini%mod) * (u%mod)) % mod + ((diff%mod)*(u%mod)%mod)*((u-1)%mod)%mod*inv2%mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
0