Submission #635361

# Submission time Handle Problem Language Result Execution time Memory
635361 2022-08-26T06:45:29 Z Cyanmond Party (INOI20_party) C++17
23 / 100
3000 ms 372 KB
#include <bits/stdc++.h>

using i64 = long long;

constexpr i64 mod = 1000000007;

i64 nmr(i64 a) {
    a %= mod;
    if (a < 0) a += mod;
    return a;
}

i64 add(i64 a, i64 b) {
    a += b;
    if (a > mod) a -= mod;
    return a;
}
i64 mul(i64 a, i64 b) { return a * b % mod; }
void add_m(i64 &a, i64 b) { a = add(a, b); }
void mul_m(i64 &a, i64 b) { a = mul(a, b); }

i64 inv(i64 x) {
    i64 a = x, b = mod, u = 1, v = 0, t = 0;
    while (b > 0) {
        t = a / b;
        a -= t * b;
        u -= t * v;
        std::swap(a, b);
        std::swap(u, v);
    }
    return nmr(u);
}

i64 pow(i64 a, i64 n) {
    i64 ret = 1;
    while (n != 0) {
        if (n & 1) mul_m(ret, a);
        mul_m(a, a);
        n >>= 1;
    }
    return ret;
}

std::unordered_map<i64, i64> memo;

void solve() {
    i64 N;
    std::cin >> N;
    if (memo.find(N) != memo.end()) {
        std::cout << memo[N] << std::endl;
        return;
    }
    // N = 2 ^ k - 1
    i64 k = 0;
    while ((1ll << k) - 1 < N) ++k;
    // assert((1ll << k) - 1 == N);

    i64 res = 0;
    for (i64 d = 0; d < k; ++d) {
        i64 ms = nmr(1ll << d);
        i64 sum = 0;
        for (i64 u = 0; u <= 2 * k; ++u) {
            i64 count_v = 0;
            for (i64 p = 0; p <= u; ++p) {
                const i64 now_d = d - p;
                if (now_d < 0) continue;
                const i64 t = u - p;
                if (now_d + t >= k) continue;
                i64 c = 1ll << t;
                if (p != 0 and t != 0) c /= 2;
                count_v += c;
            }
            add_m(res, mul(mul(ms, u), add(pow(2, N - sum), mod - pow(2, N - sum - count_v))));
            sum += count_v;
        }
    }

    mul_m(res, inv(add(pow(2, N), mod - 1)));
    std::cout << res << std::endl;
    memo[N] = res;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int Q;
    std::cin >> Q;
    while (Q--) solve();
}
# Verdict Execution time Memory Grader output
1 Execution timed out 3081 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 308 KB Output is correct
2 Correct 50 ms 316 KB Output is correct
3 Correct 50 ms 320 KB Output is correct
4 Correct 51 ms 320 KB Output is correct
5 Correct 51 ms 212 KB Output is correct
6 Correct 51 ms 312 KB Output is correct
7 Correct 52 ms 212 KB Output is correct
8 Correct 51 ms 212 KB Output is correct
9 Correct 50 ms 212 KB Output is correct
10 Correct 49 ms 212 KB Output is correct
11 Correct 7 ms 340 KB Output is correct
12 Correct 7 ms 372 KB Output is correct
13 Correct 7 ms 340 KB Output is correct
14 Correct 6 ms 340 KB Output is correct
15 Correct 6 ms 336 KB Output is correct
16 Correct 6 ms 332 KB Output is correct
17 Correct 7 ms 340 KB Output is correct
18 Correct 6 ms 372 KB Output is correct
19 Correct 7 ms 340 KB Output is correct
20 Correct 6 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3062 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3081 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -