結果

問題 No.737 PopCount
ユーザー nebukuro09nebukuro09
提出日時 2018-09-28 23:17:08
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,452 bytes
コンパイル時間 634 ms
コンパイル使用メモリ 101,524 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 21:01:01
合計ジャッジ時間 1,553 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long MOD = 10^^9 + 7;
immutable int MAX = 100;
auto F = new long[](MAX);
auto G = new long[](MAX);
auto P = new long[](MAX);

void main() {
    F[0] = 1, G[0] = 1, P[0] = 1;
    foreach (i; 1..MAX) F[i] = F[i-1] * i % MOD;
    foreach (i; 1..MAX) G[i] = powmod(F[i], MOD-2, MOD);
    foreach (i; 1..MAX) P[i] = P[i-1] * 2 % MOD;

    auto N = readln.chomp.to!long;
    long[] dp = new long[](MAX);

    for (long n = N, popcnt = 0, s = 0; n > 0; n = n - (1L << bsr(n)), popcnt += 1) {
        long b = 1L << bsr(n);
        long m = n - b;
        dp[popcnt + 1] += s + b;
        // 先頭が0のとき
        foreach (i; 1..bsr(n)+1) {
            long c = comb(bsr(n)-1, i-1);
            (dp[popcnt+i] += comb(bsr(n), i) * s % MOD) %= MOD;
            foreach (j; 0..bsr(n)) {
                (dp[popcnt+i] += c * P[j] % MOD) %= MOD;
            }
        }
        s += b;
        s %= MOD;
    }

    long ans = 0;
    foreach (i; 0..MAX) (ans += dp[i] * i % MOD) %= MOD;
    ans.writeln;
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

long comb(long n, long r) {
    return F[n] * G[n-r] % MOD * G[r] % MOD;
}
0