답안 #633710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
633710 2022-08-23T06:28:32 Z 406 Party (INOI20_party) C++17
63 / 100
3000 ms 1018328 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 chmx(a, b) a = max(a, b)
#define chmn(a, b) a = min(a, b)
#define pb push_back
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1,*p2;

/*inline int read() {
	ll x = 0, f = 1; char c;
	while(!isdigit(c = gc())) if(c=='-') f=-1;
	while(isdigit(c)) x = (x<<3)+(x<<1)+(c^48), c = gc();
	return x * f;
}
*/
const ll INFL = 1ll << 60;
const int INF = 1 << 29;
const int N = 62;
const int M = 2 * N;
const int MOD = 1e9 + 7;
const int X = 2.6e8;
//const int X = 1e7;

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


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

ll p2(ll b) {
    b %= (MOD - 1);
    ll a = 1;
    while (b > X) {
        a = (a * pw2[X]) % MOD;
        b -= X;
    }
    a *= pw2[b];
    return a % MOD;
}

void calc_dist(ll v, ll sub, int lg) {
    for (int k = lg; k < 62; k++) {
        int x = k - lg;
        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];
    }
}

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, ll sub, int lg) {
    if (v > n) {
        ptr--;
        return;
    }
    //cout << "C: " << v << ' ' << sub << " PTR: " << ptr << '\n';
    calc_dist(v, sub, lg);

    pass();
    Complete(v << 1, sub >> 1, lg + 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--;
}

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

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

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

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

    if (cntr == cntl) {
        q = 0;
        pass();
        Complete(v << 1, (1ll << cntl) - 1, lg + 1);
        ans += q;
        ans %= MOD;

        pass();
        F((v << 1) | 1, sub - (1ll << cntl), lg + 1);
    }
    else {
        q = 0;
        pass();
        Complete((v << 1) | 1, (1ll << cntr) - 1,lg + 1);

        ans += q;
        if (ans >= MOD)
            ans -= MOD;

        pass();
        F((v << 1), sub - (1ll << cntr), lg + 1);
    }
    

    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;
    cin >> query;
    while (query--) {
        cin >> n;

        F(1, n, 1);
        ll re = pw(2, 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 1580 ms 1017940 KB Output is correct
2 Correct 1503 ms 1017852 KB Output is correct
3 Correct 1441 ms 1017880 KB Output is correct
4 Correct 1373 ms 1017992 KB Output is correct
5 Correct 1407 ms 1017828 KB Output is correct
6 Correct 1381 ms 1017980 KB Output is correct
7 Correct 1340 ms 1017904 KB Output is correct
8 Correct 1350 ms 1017940 KB Output is correct
9 Correct 1331 ms 1017852 KB Output is correct
10 Correct 1328 ms 1017828 KB Output is correct
11 Correct 1349 ms 1017836 KB Output is correct
12 Correct 1338 ms 1017852 KB Output is correct
13 Correct 1371 ms 1017868 KB Output is correct
14 Correct 1345 ms 1017992 KB Output is correct
15 Correct 1371 ms 1017912 KB Output is correct
16 Correct 1506 ms 1017948 KB Output is correct
17 Correct 1447 ms 1017892 KB Output is correct
18 Correct 1456 ms 1017936 KB Output is correct
19 Correct 1564 ms 1017964 KB Output is correct
20 Correct 1428 ms 1017908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1675 ms 1018072 KB Output is correct
2 Correct 1493 ms 1017968 KB Output is correct
3 Correct 1442 ms 1017972 KB Output is correct
4 Correct 1475 ms 1017956 KB Output is correct
5 Correct 1562 ms 1017940 KB Output is correct
6 Correct 1533 ms 1017936 KB Output is correct
7 Correct 1403 ms 1018052 KB Output is correct
8 Correct 1421 ms 1018008 KB Output is correct
9 Correct 1435 ms 1018040 KB Output is correct
10 Correct 1407 ms 1018044 KB Output is correct
11 Correct 1620 ms 1018064 KB Output is correct
12 Correct 1605 ms 1018056 KB Output is correct
13 Correct 1639 ms 1018164 KB Output is correct
14 Correct 1557 ms 1018096 KB Output is correct
15 Correct 1592 ms 1018168 KB Output is correct
16 Correct 1535 ms 1018096 KB Output is correct
17 Correct 1745 ms 1018020 KB Output is correct
18 Correct 1531 ms 1018088 KB Output is correct
19 Correct 1718 ms 1018060 KB Output is correct
20 Correct 1537 ms 1017948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1508 ms 1017972 KB Output is correct
2 Correct 1580 ms 1018040 KB Output is correct
3 Correct 1515 ms 1018028 KB Output is correct
4 Correct 1513 ms 1018028 KB Output is correct
5 Correct 1539 ms 1017968 KB Output is correct
6 Correct 1571 ms 1017944 KB Output is correct
7 Correct 1515 ms 1018032 KB Output is correct
8 Correct 1566 ms 1017980 KB Output is correct
9 Correct 1442 ms 1017996 KB Output is correct
10 Correct 1413 ms 1017972 KB Output is correct
11 Correct 1541 ms 1018016 KB Output is correct
12 Correct 1406 ms 1018004 KB Output is correct
13 Correct 1850 ms 1018012 KB Output is correct
14 Correct 1865 ms 1018040 KB Output is correct
15 Correct 1832 ms 1018032 KB Output is correct
16 Correct 2039 ms 1018036 KB Output is correct
17 Correct 1442 ms 1018044 KB Output is correct
18 Correct 1417 ms 1017944 KB Output is correct
19 Correct 1479 ms 1017960 KB Output is correct
20 Correct 1445 ms 1018020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1580 ms 1017940 KB Output is correct
2 Correct 1503 ms 1017852 KB Output is correct
3 Correct 1441 ms 1017880 KB Output is correct
4 Correct 1373 ms 1017992 KB Output is correct
5 Correct 1407 ms 1017828 KB Output is correct
6 Correct 1381 ms 1017980 KB Output is correct
7 Correct 1340 ms 1017904 KB Output is correct
8 Correct 1350 ms 1017940 KB Output is correct
9 Correct 1331 ms 1017852 KB Output is correct
10 Correct 1328 ms 1017828 KB Output is correct
11 Correct 1349 ms 1017836 KB Output is correct
12 Correct 1338 ms 1017852 KB Output is correct
13 Correct 1371 ms 1017868 KB Output is correct
14 Correct 1345 ms 1017992 KB Output is correct
15 Correct 1371 ms 1017912 KB Output is correct
16 Correct 1506 ms 1017948 KB Output is correct
17 Correct 1447 ms 1017892 KB Output is correct
18 Correct 1456 ms 1017936 KB Output is correct
19 Correct 1564 ms 1017964 KB Output is correct
20 Correct 1428 ms 1017908 KB Output is correct
21 Correct 1675 ms 1018072 KB Output is correct
22 Correct 1493 ms 1017968 KB Output is correct
23 Correct 1442 ms 1017972 KB Output is correct
24 Correct 1475 ms 1017956 KB Output is correct
25 Correct 1562 ms 1017940 KB Output is correct
26 Correct 1533 ms 1017936 KB Output is correct
27 Correct 1403 ms 1018052 KB Output is correct
28 Correct 1421 ms 1018008 KB Output is correct
29 Correct 1435 ms 1018040 KB Output is correct
30 Correct 1407 ms 1018044 KB Output is correct
31 Correct 1620 ms 1018064 KB Output is correct
32 Correct 1605 ms 1018056 KB Output is correct
33 Correct 1639 ms 1018164 KB Output is correct
34 Correct 1557 ms 1018096 KB Output is correct
35 Correct 1592 ms 1018168 KB Output is correct
36 Correct 1535 ms 1018096 KB Output is correct
37 Correct 1745 ms 1018020 KB Output is correct
38 Correct 1531 ms 1018088 KB Output is correct
39 Correct 1718 ms 1018060 KB Output is correct
40 Correct 1537 ms 1017948 KB Output is correct
41 Correct 1508 ms 1017972 KB Output is correct
42 Correct 1580 ms 1018040 KB Output is correct
43 Correct 1515 ms 1018028 KB Output is correct
44 Correct 1513 ms 1018028 KB Output is correct
45 Correct 1539 ms 1017968 KB Output is correct
46 Correct 1571 ms 1017944 KB Output is correct
47 Correct 1515 ms 1018032 KB Output is correct
48 Correct 1566 ms 1017980 KB Output is correct
49 Correct 1442 ms 1017996 KB Output is correct
50 Correct 1413 ms 1017972 KB Output is correct
51 Correct 1541 ms 1018016 KB Output is correct
52 Correct 1406 ms 1018004 KB Output is correct
53 Correct 1850 ms 1018012 KB Output is correct
54 Correct 1865 ms 1018040 KB Output is correct
55 Correct 1832 ms 1018032 KB Output is correct
56 Correct 2039 ms 1018036 KB Output is correct
57 Correct 1442 ms 1018044 KB Output is correct
58 Correct 1417 ms 1017944 KB Output is correct
59 Correct 1479 ms 1017960 KB Output is correct
60 Correct 1445 ms 1018020 KB Output is correct
61 Correct 1410 ms 1017828 KB Output is correct
62 Correct 1401 ms 1018056 KB Output is correct
63 Correct 2481 ms 1018160 KB Output is correct
64 Correct 2483 ms 1018164 KB Output is correct
65 Correct 2416 ms 1018164 KB Output is correct
66 Correct 2463 ms 1018052 KB Output is correct
67 Correct 2798 ms 1017972 KB Output is correct
68 Correct 2764 ms 1018328 KB Output is correct
69 Correct 2408 ms 1018072 KB Output is correct
70 Correct 2505 ms 1018036 KB Output is correct
71 Correct 1392 ms 1018032 KB Output is correct
72 Correct 1399 ms 1018048 KB Output is correct
73 Correct 1469 ms 1018056 KB Output is correct
74 Correct 1477 ms 1018048 KB Output is correct
75 Execution timed out 3114 ms 1018036 KB Time limit exceeded
76 Halted 0 ms 0 KB -