結果

問題 No.737 PopCount
ユーザー ikdikd
提出日時 2018-10-02 18:41:46
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 1,324 bytes
コンパイル時間 609 ms
コンパイル使用メモリ 84,112 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 21:01:37
合計ジャッジ時間 1,541 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  long n;
  rd(n);
  const long mod = 10 ^^ 9 + 7;

  int get(long nn) {
    int ret = 0;
    foreach (i; 0 .. 62) {
      if (nn & (1L << i))
        ret = i;
    }
    return ret + 1;
  }

  int len = get(n);
  auto dp = new long[][][](len + 1, len + 1, 2);
  auto o = new long[][][](len + 1, len + 1, 2);
  dp[0][0][0] = 1; // ??
  foreach (i; 0 .. len) {
    for (int j = 0; j <= len; j++) {
      foreach (f; 0 .. 2) {
        foreach (b; 0 .. 2) {
          auto x = (n >> (len - i - 1)) & 1;
          if (f == 0 && x == 0 && b == 1) {
            continue;
          }
          auto nj = j + b, nf = f | (b < x);
          if (nj <= len) {
            (dp[i + 1][nj][nf] += dp[i][j][f]) %= mod;
            (o[i + 1][nj][nf] += o[i][j][f] * 2) %= mod;
            if (b) {
              (o[i + 1][nj][nf] += dp[i][j][f]) %= mod;
            }
          }
        }
      }
    }
  }
  long s = 0;
  for (int j = 1; j <= len; j++) {
    foreach (f; 0 .. 2) {
      (s += o[len][j][f] * j) %= mod;
    }
  }
  writeln(s);
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}
0