답안 #633840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
633840 2022-08-23T09:51:29 Z 406 Party (INOI20_party) C++17
100 / 100
2707 ms 1432 KB
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#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 X1 = 1e5;
const int X2 = 1e5;

ll d0[M][M], d1[M][M];
ll n;
int ptr, ans, q, cnt;
int pw2[X1 + 1], hwt[X2 + 1];

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);
    return 1ll * hwt[b / X1] * pw2[b % X1] % 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)
            d1[ptr][x] = 0;
        else 
            d1[ptr][x] = min(n - (v << x) + 1, ((ll)1 << x));
    }
    if (v > 1) {
        for (int k = 2; k < N; k++)
            d0[ptr][k] -= d1[ptr][k - 2];
    }
}

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

void Complete(ll v) {
    //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 += d0[ptr][k] + d1[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 ((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;
            if (ans >= MOD)
                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 += d0[ptr][k] + d1[ptr][k];
        ans += p2(n - s) - 1;
        if (ans >= MOD)
            ans -= MOD;
    }
    ptr--;
}

signed main() {
    ios::sync_with_stdio(0);

    pw2[0] = 1;
    for (int i = 1; i <= X1; i++) {
        pw2[i] = pw2[i - 1] << 1;
        if (pw2[i] >= MOD)
            pw2[i] -= MOD;
    }
    hwt[0] = 1;
    for (int i = 1; i <= X2; i++) {
        hwt[i] = (1ll * hwt[i - 1] * pw2[X1]) % MOD;
    }

    int query = read();
    //cin >> query;
    while (query--) {
        //cin >> n;
        n = read();
        F(1, n);
        ll re = p2(n) - 1;

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

        ans = q = ptr = 0;
    }
    return 0;
}
//assert
//yaddasht
//nim saat dar ye vaziat
//checkpoint
//f ha ro benvisim
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1108 KB Output is correct
2 Correct 2 ms 1120 KB Output is correct
3 Correct 2 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 3 ms 1140 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 2 ms 1108 KB Output is correct
8 Correct 2 ms 1108 KB Output is correct
9 Correct 2 ms 1108 KB Output is correct
10 Correct 2 ms 1108 KB Output is correct
11 Correct 4 ms 1108 KB Output is correct
12 Correct 5 ms 1152 KB Output is correct
13 Correct 12 ms 1160 KB Output is correct
14 Correct 11 ms 1108 KB Output is correct
15 Correct 18 ms 1108 KB Output is correct
16 Correct 13 ms 1108 KB Output is correct
17 Correct 10 ms 1108 KB Output is correct
18 Correct 10 ms 1108 KB Output is correct
19 Correct 6 ms 1108 KB Output is correct
20 Correct 8 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1236 KB Output is correct
2 Correct 8 ms 1240 KB Output is correct
3 Correct 7 ms 1236 KB Output is correct
4 Correct 5 ms 1236 KB Output is correct
5 Correct 6 ms 1236 KB Output is correct
6 Correct 4 ms 1236 KB Output is correct
7 Correct 4 ms 1236 KB Output is correct
8 Correct 5 ms 1236 KB Output is correct
9 Correct 4 ms 1236 KB Output is correct
10 Correct 5 ms 1236 KB Output is correct
11 Correct 87 ms 1272 KB Output is correct
12 Correct 87 ms 1268 KB Output is correct
13 Correct 87 ms 1268 KB Output is correct
14 Correct 105 ms 1256 KB Output is correct
15 Correct 95 ms 1264 KB Output is correct
16 Correct 88 ms 1236 KB Output is correct
17 Correct 125 ms 1280 KB Output is correct
18 Correct 94 ms 1388 KB Output is correct
19 Correct 114 ms 1264 KB Output is correct
20 Correct 94 ms 1276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 1236 KB Output is correct
2 Correct 121 ms 1216 KB Output is correct
3 Correct 74 ms 1236 KB Output is correct
4 Correct 70 ms 1236 KB Output is correct
5 Correct 82 ms 1236 KB Output is correct
6 Correct 84 ms 1236 KB Output is correct
7 Correct 75 ms 1244 KB Output is correct
8 Correct 94 ms 1228 KB Output is correct
9 Correct 5 ms 1108 KB Output is correct
10 Correct 5 ms 1236 KB Output is correct
11 Correct 10 ms 1236 KB Output is correct
12 Correct 10 ms 1236 KB Output is correct
13 Correct 272 ms 1236 KB Output is correct
14 Correct 272 ms 1236 KB Output is correct
15 Correct 283 ms 1236 KB Output is correct
16 Correct 288 ms 1236 KB Output is correct
17 Correct 17 ms 1240 KB Output is correct
18 Correct 21 ms 1236 KB Output is correct
19 Correct 22 ms 1364 KB Output is correct
20 Correct 22 ms 1244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1108 KB Output is correct
2 Correct 2 ms 1120 KB Output is correct
3 Correct 2 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 3 ms 1140 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 2 ms 1108 KB Output is correct
8 Correct 2 ms 1108 KB Output is correct
9 Correct 2 ms 1108 KB Output is correct
10 Correct 2 ms 1108 KB Output is correct
11 Correct 4 ms 1108 KB Output is correct
12 Correct 5 ms 1152 KB Output is correct
13 Correct 12 ms 1160 KB Output is correct
14 Correct 11 ms 1108 KB Output is correct
15 Correct 18 ms 1108 KB Output is correct
16 Correct 13 ms 1108 KB Output is correct
17 Correct 10 ms 1108 KB Output is correct
18 Correct 10 ms 1108 KB Output is correct
19 Correct 6 ms 1108 KB Output is correct
20 Correct 8 ms 1108 KB Output is correct
21 Correct 7 ms 1236 KB Output is correct
22 Correct 8 ms 1240 KB Output is correct
23 Correct 7 ms 1236 KB Output is correct
24 Correct 5 ms 1236 KB Output is correct
25 Correct 6 ms 1236 KB Output is correct
26 Correct 4 ms 1236 KB Output is correct
27 Correct 4 ms 1236 KB Output is correct
28 Correct 5 ms 1236 KB Output is correct
29 Correct 4 ms 1236 KB Output is correct
30 Correct 5 ms 1236 KB Output is correct
31 Correct 87 ms 1272 KB Output is correct
32 Correct 87 ms 1268 KB Output is correct
33 Correct 87 ms 1268 KB Output is correct
34 Correct 105 ms 1256 KB Output is correct
35 Correct 95 ms 1264 KB Output is correct
36 Correct 88 ms 1236 KB Output is correct
37 Correct 125 ms 1280 KB Output is correct
38 Correct 94 ms 1388 KB Output is correct
39 Correct 114 ms 1264 KB Output is correct
40 Correct 94 ms 1276 KB Output is correct
41 Correct 93 ms 1236 KB Output is correct
42 Correct 121 ms 1216 KB Output is correct
43 Correct 74 ms 1236 KB Output is correct
44 Correct 70 ms 1236 KB Output is correct
45 Correct 82 ms 1236 KB Output is correct
46 Correct 84 ms 1236 KB Output is correct
47 Correct 75 ms 1244 KB Output is correct
48 Correct 94 ms 1228 KB Output is correct
49 Correct 5 ms 1108 KB Output is correct
50 Correct 5 ms 1236 KB Output is correct
51 Correct 10 ms 1236 KB Output is correct
52 Correct 10 ms 1236 KB Output is correct
53 Correct 272 ms 1236 KB Output is correct
54 Correct 272 ms 1236 KB Output is correct
55 Correct 283 ms 1236 KB Output is correct
56 Correct 288 ms 1236 KB Output is correct
57 Correct 17 ms 1240 KB Output is correct
58 Correct 21 ms 1236 KB Output is correct
59 Correct 22 ms 1364 KB Output is correct
60 Correct 22 ms 1244 KB Output is correct
61 Correct 1 ms 1108 KB Output is correct
62 Correct 8 ms 1236 KB Output is correct
63 Correct 709 ms 1252 KB Output is correct
64 Correct 713 ms 1376 KB Output is correct
65 Correct 737 ms 1248 KB Output is correct
66 Correct 716 ms 1356 KB Output is correct
67 Correct 780 ms 1368 KB Output is correct
68 Correct 768 ms 1264 KB Output is correct
69 Correct 742 ms 1368 KB Output is correct
70 Correct 750 ms 1260 KB Output is correct
71 Correct 33 ms 1236 KB Output is correct
72 Correct 34 ms 1260 KB Output is correct
73 Correct 88 ms 1276 KB Output is correct
74 Correct 84 ms 1268 KB Output is correct
75 Correct 2698 ms 1268 KB Output is correct
76 Correct 2675 ms 1316 KB Output is correct
77 Correct 2707 ms 1432 KB Output is correct
78 Correct 2698 ms 1312 KB Output is correct
79 Correct 168 ms 1236 KB Output is correct
80 Correct 191 ms 1320 KB Output is correct
81 Correct 176 ms 1300 KB Output is correct
82 Correct 180 ms 1312 KB Output is correct