Submission #760108

# Submission time Handle Problem Language Result Execution time Memory
760108 2023-06-17T07:52:13 Z The_Samurai Party (INOI20_party) C++17
0 / 100
3000 ms 212 KB
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;

ll binpow(ll a, ll b, ll mod, bool dec = false) {
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans = ans * a % mod;
        }
        a = a * a % mod;
        b >>= 1;
    }
    if (dec) {
        ans = (ans - 1 + mod) % mod;
    }
    return ans;
}

bool isTwo(ll n) {
    return (1 << (63 - __builtin_clzll(n))) == n;
}

ll n, ans;

void calc(ll x, ll mul) {
    vector<tuple<ll, ll, bool>> g = {{x, 1, true}};
    ll others = n;
    for (int h = 0; h <= n; h++) {
        vector<tuple<ll, ll, bool>> nw;
        ll sum = 0;
        for (auto [v, t, change]: g) {
            sum += t;
            if (!change) {
                if (v * 2 <= n) {
                    nw.emplace_back(v * 2, t * 2, change);
                }
                continue;
            }
            if (x == v) {
                if (v * 2 <= n) {
                    nw.emplace_back(v * 2, t * 2, false);
                }
                if (v > 1) {
                    nw.emplace_back(v / 2, 1, true);
                }
                continue;
            }
            if (isTwo(x / v) and x % v == 0) {
                if (v * 2 + 1 <= n) {
                    nw.emplace_back(v * 2 + 1, 1, false);
                }
                if (v > 1) {
                    nw.emplace_back(v / 2, 1, true);
                }
                continue;
            }
        }
//        cout << x << ' ' << h << '\t';
//        for (auto [v, t, change]: g) {
//            cout << v << " -> " << t << "  ";
//        }
//        cout << endl;
        others -= sum;
//        cout << x << ' ' << h << ' ' << sum << ' ' << others << endl;
        ll k = binpow(2, sum % mod, mod, true) * binpow(2, others, mod) % mod;
//        cout << 1 << endl;
        ans = (ans + k * h % mod * mul) % mod;
        if (nw.empty()) {
            break;
        }
        g = nw;
    }
}

int main() {
    int q;
    cin >> q;
    for (int qq = 1; qq <= q; qq++) {
        cin >> n;
        ans = 0;
        for (ll x = 1; x < n; x *= 2) {
            calc(x, x);
        }
#ifdef sunnitov
        cout << n << ' ';
#endif
        cout << ans * binpow(binpow(2, n % mod, mod, true), mod - 2, mod) % mod << '\n';
    }
}

// 1 - 1 2 4 8
// 2 - 1 3 8 2 4
// 4 - 1 3 2 3 2 4
# Verdict Execution time Memory Grader output
1 Execution timed out 3065 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 120 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3057 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3065 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -