Submission #633721

# Submission time Handle Problem Language Result Execution time Memory
633721 2022-08-23T07:05:57 Z 406 Party (INOI20_party) C++17
100 / 100
2773 ms 1018244 KB
#include <bits/stdc++.h>
using ll = long long;
using namespace std;

#define forn(a, b) for (int a = 0; a < (b); a++)
#define rofn(a, b) for (int a = (b) - 1; a >= 0; a--)
#define LOG(a) (63 - __builtin_clzll(a))
#define gc() (pt1==pt2&&(pt2=(pt1=buf)+fread(buf,1,1<<21,stdin),pt1==pt2)?EOF:*pt1++)
char buf[1<<21],*pt1,*pt2;

inline ll read() {
	ll x = 0;char c;
	while(!isdigit(c = gc()));
	while(isdigit(c)) x = (x<<3)+(x<<1)+(c^48), c = gc();
	return x;
}

const ll INFL = 1ll << 60;
const int INF = 1 << 29;
const int N = 61;
const int M = 2 * N;
const int MOD = 1e9 + 7;
const int X = 2.6e8;

ll d[2][M][M];
ll n;
int ptr, ans, q;
int pw2[X + 1];
unordered_map<ll, int> calced;

inline ll pw(ll a, int b) {
    ll re = 1;
    while (b) {
        if (b & 1)
            re = (re * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return re;
}

inline int p2(ll b) {
    b %= (MOD - 1);
    ll a = pw(pw2[X], b / X) * pw2[b % X];
    return a % MOD;
}

void calc_dist(ll v) {
    for (int k = LOG(v) + 1; k < N; k++) {
        int x = k - LOG(v) - 1;
        if ((v << x) > n)
            d[1][ptr][x] = 0;
        else
            d[1][ptr][x] = min(n - (v << x) + 1, ((ll)1 << x));
    }
    if (v > 1) {
        for (int k = 2; k < N; k++)
            d[0][ptr][k] -= d[1][ptr][k - 2];
    }
}

inline void pass() {
    for (int k = 1; k < M; k++)
        d[0][ptr + 1][k] = d[0][ptr][k - 1] + d[1][ptr][k - 1];
    ptr++;
}

void Complete(ll v) {
    if (v > n) {
        ptr--;
        return;
    }
    //cout << "C: " << v << ' ' << sub << " PTR: " << ptr << '\n';
    calc_dist(v);

    if ((v << 1) <= n) {
        pass();
        Complete(v << 1);
        q <<= 1;
        if (q >= MOD)
            q -= MOD;
    }

    ll s = 0;
    for (int k = 0; s != n; k++) {
        s += d[0][ptr][k] + d[1][ptr][k];
        q += p2(n - s) - 1;
        if (q >= MOD)
            q -= MOD;
    }
    ptr--;
}

inline int solve(ll a, ll b) {
    int x = LOG(b) - LOG(a);
    a <<= x;
    return x + (a <= b);
}

void F(ll v, ll sub) {
    if (v > n) {
        ptr--;
        return;
    }

    if (((sub) & (sub + 1)) == 0) {
        q = 0;
        Complete(v);
        ans += q;
        if (ans >= MOD)
            ans -= MOD;
        return;
    }

    //cout << "F: " << v << ' ' << sub << " PTR: " << ptr << '\n';
    calc_dist(v);

    ll r = (v << 1) | 1;
    ll l = (v << 1);
    int cntr = solve(r, n), cntl = solve(l, n);

    if (cntr == cntl) {
        q = 0;
        if ((v << 1) <= n) {
            pass();
            Complete(v << 1);
            ans += q;
            ans %= MOD;
        }

        if (((v << 1) | 1) <= n) {
            pass();
            F((v << 1) | 1, sub - (1ll << cntl));
        }
    }
    else {
        if (((v << 1) | 1) <= n) {
            q = 0;
            pass();
            Complete((v << 1) | 1);
            ans += q;
            if (ans >= MOD)
                ans -= MOD;
        }
        if ((v << 1) <= n) {
            pass();
            F((v << 1), sub - (1ll << cntr));
        }
    }
    

    ll s = 0;
    for (int k = 0; s != n; k++) {
        s += d[0][ptr][k] + d[1][ptr][k];
        ans += p2(n - s) - 1;
        if (ans >= MOD)
            ans -= MOD;
    }
    ptr--;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);

    pw2[0] = 1;
    for (int i = 1; i <= X; i++) {
        pw2[i] = pw2[i - 1] * 2;
        if (pw2[i] >= MOD)
            pw2[i] -= MOD;
    }

    int query = read();
    //cin >> query;
    while (query--) {
        //cin >> n;
        n = read();

        if (calced.count(n)) {
            cout << calced[n] << '\n';
            continue;
        }
        F(1, n);
        ll re = p2(n) - 1;

        ans = (ans * pw(re, MOD - 2)) % MOD;
        cout << ans << '\n';
        calced[n] = ans;

        ans = q = ptr = 0;
    }
    return 0;
}
//assert
//yaddasht
//nim saat dar ye vaziat
//checkpoint
//f ha ro benvisim
# Verdict Execution time Memory Grader output
1 Correct 1347 ms 1017948 KB Output is correct
2 Correct 1342 ms 1017932 KB Output is correct
3 Correct 1357 ms 1017896 KB Output is correct
4 Correct 1327 ms 1017888 KB Output is correct
5 Correct 1332 ms 1017896 KB Output is correct
6 Correct 1346 ms 1017828 KB Output is correct
7 Correct 1403 ms 1017896 KB Output is correct
8 Correct 1336 ms 1017928 KB Output is correct
9 Correct 1331 ms 1017948 KB Output is correct
10 Correct 1329 ms 1017872 KB Output is correct
11 Correct 1324 ms 1018080 KB Output is correct
12 Correct 1356 ms 1017880 KB Output is correct
13 Correct 1328 ms 1017936 KB Output is correct
14 Correct 1732 ms 1017872 KB Output is correct
15 Correct 1335 ms 1017940 KB Output is correct
16 Correct 1357 ms 1017888 KB Output is correct
17 Correct 1324 ms 1017956 KB Output is correct
18 Correct 1353 ms 1017952 KB Output is correct
19 Correct 1339 ms 1017852 KB Output is correct
20 Correct 1337 ms 1018140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1329 ms 1018092 KB Output is correct
2 Correct 1380 ms 1017936 KB Output is correct
3 Correct 1343 ms 1017972 KB Output is correct
4 Correct 1341 ms 1018040 KB Output is correct
5 Correct 1329 ms 1017988 KB Output is correct
6 Correct 1347 ms 1018064 KB Output is correct
7 Correct 1333 ms 1018040 KB Output is correct
8 Correct 1335 ms 1018024 KB Output is correct
9 Correct 1358 ms 1017976 KB Output is correct
10 Correct 1387 ms 1017948 KB Output is correct
11 Correct 1397 ms 1018032 KB Output is correct
12 Correct 1368 ms 1017984 KB Output is correct
13 Correct 1326 ms 1018036 KB Output is correct
14 Correct 1350 ms 1018004 KB Output is correct
15 Correct 1408 ms 1018056 KB Output is correct
16 Correct 1440 ms 1018076 KB Output is correct
17 Correct 1491 ms 1018004 KB Output is correct
18 Correct 1481 ms 1017984 KB Output is correct
19 Correct 1415 ms 1017996 KB Output is correct
20 Correct 1440 ms 1018044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1536 ms 1017964 KB Output is correct
2 Correct 1552 ms 1018004 KB Output is correct
3 Correct 1662 ms 1017984 KB Output is correct
4 Correct 1566 ms 1018072 KB Output is correct
5 Correct 1461 ms 1017936 KB Output is correct
6 Correct 1466 ms 1017956 KB Output is correct
7 Correct 1484 ms 1018000 KB Output is correct
8 Correct 1447 ms 1018000 KB Output is correct
9 Correct 1481 ms 1018004 KB Output is correct
10 Correct 1411 ms 1018020 KB Output is correct
11 Correct 1415 ms 1018024 KB Output is correct
12 Correct 1438 ms 1017952 KB Output is correct
13 Correct 1520 ms 1018000 KB Output is correct
14 Correct 1413 ms 1018044 KB Output is correct
15 Correct 1411 ms 1018008 KB Output is correct
16 Correct 1433 ms 1018012 KB Output is correct
17 Correct 1476 ms 1018036 KB Output is correct
18 Correct 1544 ms 1017936 KB Output is correct
19 Correct 1727 ms 1018032 KB Output is correct
20 Correct 1378 ms 1018036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1347 ms 1017948 KB Output is correct
2 Correct 1342 ms 1017932 KB Output is correct
3 Correct 1357 ms 1017896 KB Output is correct
4 Correct 1327 ms 1017888 KB Output is correct
5 Correct 1332 ms 1017896 KB Output is correct
6 Correct 1346 ms 1017828 KB Output is correct
7 Correct 1403 ms 1017896 KB Output is correct
8 Correct 1336 ms 1017928 KB Output is correct
9 Correct 1331 ms 1017948 KB Output is correct
10 Correct 1329 ms 1017872 KB Output is correct
11 Correct 1324 ms 1018080 KB Output is correct
12 Correct 1356 ms 1017880 KB Output is correct
13 Correct 1328 ms 1017936 KB Output is correct
14 Correct 1732 ms 1017872 KB Output is correct
15 Correct 1335 ms 1017940 KB Output is correct
16 Correct 1357 ms 1017888 KB Output is correct
17 Correct 1324 ms 1017956 KB Output is correct
18 Correct 1353 ms 1017952 KB Output is correct
19 Correct 1339 ms 1017852 KB Output is correct
20 Correct 1337 ms 1018140 KB Output is correct
21 Correct 1329 ms 1018092 KB Output is correct
22 Correct 1380 ms 1017936 KB Output is correct
23 Correct 1343 ms 1017972 KB Output is correct
24 Correct 1341 ms 1018040 KB Output is correct
25 Correct 1329 ms 1017988 KB Output is correct
26 Correct 1347 ms 1018064 KB Output is correct
27 Correct 1333 ms 1018040 KB Output is correct
28 Correct 1335 ms 1018024 KB Output is correct
29 Correct 1358 ms 1017976 KB Output is correct
30 Correct 1387 ms 1017948 KB Output is correct
31 Correct 1397 ms 1018032 KB Output is correct
32 Correct 1368 ms 1017984 KB Output is correct
33 Correct 1326 ms 1018036 KB Output is correct
34 Correct 1350 ms 1018004 KB Output is correct
35 Correct 1408 ms 1018056 KB Output is correct
36 Correct 1440 ms 1018076 KB Output is correct
37 Correct 1491 ms 1018004 KB Output is correct
38 Correct 1481 ms 1017984 KB Output is correct
39 Correct 1415 ms 1017996 KB Output is correct
40 Correct 1440 ms 1018044 KB Output is correct
41 Correct 1536 ms 1017964 KB Output is correct
42 Correct 1552 ms 1018004 KB Output is correct
43 Correct 1662 ms 1017984 KB Output is correct
44 Correct 1566 ms 1018072 KB Output is correct
45 Correct 1461 ms 1017936 KB Output is correct
46 Correct 1466 ms 1017956 KB Output is correct
47 Correct 1484 ms 1018000 KB Output is correct
48 Correct 1447 ms 1018000 KB Output is correct
49 Correct 1481 ms 1018004 KB Output is correct
50 Correct 1411 ms 1018020 KB Output is correct
51 Correct 1415 ms 1018024 KB Output is correct
52 Correct 1438 ms 1017952 KB Output is correct
53 Correct 1520 ms 1018000 KB Output is correct
54 Correct 1413 ms 1018044 KB Output is correct
55 Correct 1411 ms 1018008 KB Output is correct
56 Correct 1433 ms 1018012 KB Output is correct
57 Correct 1476 ms 1018036 KB Output is correct
58 Correct 1544 ms 1017936 KB Output is correct
59 Correct 1727 ms 1018032 KB Output is correct
60 Correct 1378 ms 1018036 KB Output is correct
61 Correct 1333 ms 1017896 KB Output is correct
62 Correct 1376 ms 1018016 KB Output is correct
63 Correct 2773 ms 1018244 KB Output is correct
64 Correct 2594 ms 1018112 KB Output is correct
65 Correct 2450 ms 1018152 KB Output is correct
66 Correct 2436 ms 1018064 KB Output is correct
67 Correct 1305 ms 1017992 KB Output is correct
68 Correct 1309 ms 1018228 KB Output is correct
69 Correct 1303 ms 1018020 KB Output is correct
70 Correct 1308 ms 1017984 KB Output is correct
71 Correct 1273 ms 1018056 KB Output is correct
72 Correct 1286 ms 1018024 KB Output is correct
73 Correct 1275 ms 1017984 KB Output is correct
74 Correct 1292 ms 1018112 KB Output is correct
75 Correct 1274 ms 1018112 KB Output is correct
76 Correct 1283 ms 1018176 KB Output is correct
77 Correct 1281 ms 1018064 KB Output is correct
78 Correct 1268 ms 1018120 KB Output is correct
79 Correct 1360 ms 1018096 KB Output is correct
80 Correct 1281 ms 1018132 KB Output is correct
81 Correct 1271 ms 1018144 KB Output is correct
82 Correct 1275 ms 1018136 KB Output is correct