Submission #853024

# Submission time Handle Problem Language Result Execution time Memory
853024 2023-09-23T10:37:41 Z phathnv Party (INOI20_party) C++11
63 / 100
3000 ms 1252 KB
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

int pw[2][1 << 15];

void Add(int &x, const int &y) {
    x += y;
    if (x >= MOD)
        x -= MOD;
}

int Pow2(int k) {
    return 1ll * pw[0][k & ((1 << 15) - 1)] * pw[1][k >> 15] % MOD;
}

int Pow(int n, int k) {
    if (k == 0)
        return 1;
    int res = Pow(n, k >> 1);
    res = 1ll * res * res % MOD;
    if (k & 1)
        res = 1ll * res * n % MOD;
    return res;
}

void CalcCnt(long long n, vector<long long> &cnt) {
    int ptr = 0;
    for(long long x = 1; n > 0; x <<= 1) {
        cnt[ptr] = min(n, x);
        n -= cnt[ptr];
        ptr++;
    }
}

vector<long long> CalcNxtCnt(vector<long long> cnt, long long subtreeSize) {
    int sz = cnt.size();
    vector<long long> cntSubTree(cnt.size(), 0);
    CalcCnt(subtreeSize, cntSubTree);
    for(int i = 0; i < sz - 1; i++)
        cnt[i + 1] -= cntSubTree[i];
    for(int i = sz - 1; i >= 0; i--)
        cnt[i] = (i == 0 ? 0 : cnt[i - 1]);
    for(int i = 0; i < sz - 1; i++)
        cnt[i] += cntSubTree[i];
    return cnt;
}

int CalcContribute(vector<long long> cnt) {
    int val = 0, sz = cnt.size();
    vector<int> cntSubset(sz, 0);
    for(int i = sz - 2; i >= 0; i--)
        cnt[i] += cnt[i + 1];
    for(int i = 0; i < sz; i++) {
        cntSubset[i] = Pow2(cnt[i] % (MOD - 1));
        Add(cntSubset[i], MOD - 1);
    }
    for(int i = 0; i < sz - 1; i++)
        Add(val, 1ll * i * (cntSubset[i] - cntSubset[i + 1] + MOD) % MOD);
    return val;
}

int CalcComplete(long long n, vector<long long> cntRoot) {
    int k = 64 - __builtin_clzll(n);
    int sz = cntRoot.size();
    vector<vector<long long>> cnt(k, vector<long long>(sz, 0));
    cnt[0] = cntRoot;
    for(int i = 1; i < k; i++) {
        cnt[i] = cnt[i - 1];
        for(int j = i; j < k; j++)
            cnt[i][j - i + 1] -= (1ll << (j - i));
        for(int j = sz - 2; j >= 0; j--)
            cnt[i][j] = (j == 0? 0 : cnt[i][j - 1]);
        for(int j = i; j < k; j++)
            cnt[i][j - i] += (1ll << (j - i));
    }
    int sum = 0;
    for(int i = 0; i < k; i++)
        Add(sum, 1ll * CalcContribute(cnt[i]) * ((1ll << i) % MOD) % MOD);
    return sum;
}

int Calc(long long n, vector<long long> cntRoot) {
    if (n == 0)
        return 0;
    int k = 64 - __builtin_clzll(n);
    if (n == (1ll << k) - 1)
        return CalcComplete(n, cntRoot);
    long long lastLayer = n - ((1ll << (k - 1)) - 1);
    long long res = CalcContribute(cntRoot);
    long long numLeft, numRight;
    numLeft = numRight = (1ll << (k - 2)) - 1;
    numLeft += min(1ll << (k - 2), lastLayer);
    numRight += max(0ll, lastLayer - (1ll << (k - 2)));
    res += Calc(numLeft, CalcNxtCnt(cntRoot, numLeft));
    res += Calc(numRight, CalcNxtCnt(cntRoot, numRight));
    return res % MOD;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    pw[0][0] = 1;
    for(int i = 1; i < (1 << 15); i++) {
        Add(pw[0][i], pw[0][i - 1]);
        Add(pw[0][i], pw[0][i - 1]);
    }
    for(int i = 0; i < (1 << 15); i++)
        pw[1][i] = Pow(pw[0][i], 1 << 15);
    int q;
    cin >> q;
    while (q--) {
        long long n;
        cin >> n;
        int k = 64 - __builtin_clzll(n);
        vector<long long> cntRoot(2 * k, 0);
        CalcCnt(n, cntRoot);
        cout << 1ll * Calc(n, cntRoot) * Pow(Pow2(n % (MOD - 1)) - 1 + MOD, MOD - 2) % MOD << '\n';
    }
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 600 KB Output is correct
2 Correct 4 ms 604 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 716 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 600 KB Output is correct
11 Correct 6 ms 604 KB Output is correct
12 Correct 6 ms 628 KB Output is correct
13 Correct 15 ms 604 KB Output is correct
14 Correct 15 ms 604 KB Output is correct
15 Correct 15 ms 744 KB Output is correct
16 Correct 17 ms 604 KB Output is correct
17 Correct 9 ms 740 KB Output is correct
18 Correct 9 ms 604 KB Output is correct
19 Correct 8 ms 604 KB Output is correct
20 Correct 9 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 860 KB Output is correct
2 Correct 9 ms 860 KB Output is correct
3 Correct 8 ms 868 KB Output is correct
4 Correct 8 ms 1224 KB Output is correct
5 Correct 9 ms 980 KB Output is correct
6 Correct 9 ms 860 KB Output is correct
7 Correct 8 ms 856 KB Output is correct
8 Correct 8 ms 860 KB Output is correct
9 Correct 9 ms 860 KB Output is correct
10 Correct 8 ms 860 KB Output is correct
11 Correct 160 ms 792 KB Output is correct
12 Correct 172 ms 800 KB Output is correct
13 Correct 165 ms 824 KB Output is correct
14 Correct 156 ms 600 KB Output is correct
15 Correct 176 ms 600 KB Output is correct
16 Correct 170 ms 808 KB Output is correct
17 Correct 161 ms 808 KB Output is correct
18 Correct 154 ms 796 KB Output is correct
19 Correct 156 ms 808 KB Output is correct
20 Correct 163 ms 808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 1216 KB Output is correct
2 Correct 131 ms 972 KB Output is correct
3 Correct 120 ms 1204 KB Output is correct
4 Correct 118 ms 1112 KB Output is correct
5 Correct 133 ms 928 KB Output is correct
6 Correct 131 ms 856 KB Output is correct
7 Correct 133 ms 1160 KB Output is correct
8 Correct 133 ms 856 KB Output is correct
9 Correct 8 ms 860 KB Output is correct
10 Correct 8 ms 860 KB Output is correct
11 Correct 17 ms 604 KB Output is correct
12 Correct 17 ms 604 KB Output is correct
13 Correct 451 ms 852 KB Output is correct
14 Correct 472 ms 788 KB Output is correct
15 Correct 451 ms 788 KB Output is correct
16 Correct 454 ms 848 KB Output is correct
17 Correct 39 ms 604 KB Output is correct
18 Correct 32 ms 788 KB Output is correct
19 Correct 33 ms 600 KB Output is correct
20 Correct 32 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 600 KB Output is correct
2 Correct 4 ms 604 KB Output is correct
3 Correct 4 ms 604 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 3 ms 716 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 3 ms 600 KB Output is correct
11 Correct 6 ms 604 KB Output is correct
12 Correct 6 ms 628 KB Output is correct
13 Correct 15 ms 604 KB Output is correct
14 Correct 15 ms 604 KB Output is correct
15 Correct 15 ms 744 KB Output is correct
16 Correct 17 ms 604 KB Output is correct
17 Correct 9 ms 740 KB Output is correct
18 Correct 9 ms 604 KB Output is correct
19 Correct 8 ms 604 KB Output is correct
20 Correct 9 ms 720 KB Output is correct
21 Correct 8 ms 860 KB Output is correct
22 Correct 9 ms 860 KB Output is correct
23 Correct 8 ms 868 KB Output is correct
24 Correct 8 ms 1224 KB Output is correct
25 Correct 9 ms 980 KB Output is correct
26 Correct 9 ms 860 KB Output is correct
27 Correct 8 ms 856 KB Output is correct
28 Correct 8 ms 860 KB Output is correct
29 Correct 9 ms 860 KB Output is correct
30 Correct 8 ms 860 KB Output is correct
31 Correct 160 ms 792 KB Output is correct
32 Correct 172 ms 800 KB Output is correct
33 Correct 165 ms 824 KB Output is correct
34 Correct 156 ms 600 KB Output is correct
35 Correct 176 ms 600 KB Output is correct
36 Correct 170 ms 808 KB Output is correct
37 Correct 161 ms 808 KB Output is correct
38 Correct 154 ms 796 KB Output is correct
39 Correct 156 ms 808 KB Output is correct
40 Correct 163 ms 808 KB Output is correct
41 Correct 120 ms 1216 KB Output is correct
42 Correct 131 ms 972 KB Output is correct
43 Correct 120 ms 1204 KB Output is correct
44 Correct 118 ms 1112 KB Output is correct
45 Correct 133 ms 928 KB Output is correct
46 Correct 131 ms 856 KB Output is correct
47 Correct 133 ms 1160 KB Output is correct
48 Correct 133 ms 856 KB Output is correct
49 Correct 8 ms 860 KB Output is correct
50 Correct 8 ms 860 KB Output is correct
51 Correct 17 ms 604 KB Output is correct
52 Correct 17 ms 604 KB Output is correct
53 Correct 451 ms 852 KB Output is correct
54 Correct 472 ms 788 KB Output is correct
55 Correct 451 ms 788 KB Output is correct
56 Correct 454 ms 848 KB Output is correct
57 Correct 39 ms 604 KB Output is correct
58 Correct 32 ms 788 KB Output is correct
59 Correct 33 ms 600 KB Output is correct
60 Correct 32 ms 604 KB Output is correct
61 Correct 3 ms 604 KB Output is correct
62 Correct 16 ms 804 KB Output is correct
63 Correct 1215 ms 1252 KB Output is correct
64 Correct 1195 ms 1064 KB Output is correct
65 Correct 1223 ms 1224 KB Output is correct
66 Correct 1209 ms 1168 KB Output is correct
67 Correct 1342 ms 1136 KB Output is correct
68 Correct 1282 ms 1184 KB Output is correct
69 Correct 1251 ms 1108 KB Output is correct
70 Correct 1299 ms 1044 KB Output is correct
71 Correct 57 ms 856 KB Output is correct
72 Correct 58 ms 860 KB Output is correct
73 Correct 154 ms 776 KB Output is correct
74 Correct 155 ms 796 KB Output is correct
75 Execution timed out 3034 ms 1052 KB Time limit exceeded
76 Halted 0 ms 0 KB -