Submission #633818

# Submission time Handle Problem Language Result Execution time Memory
633818 2022-08-23T09:28:30 Z 406 Party (INOI20_party) C++17
63 / 100
3000 ms 196284 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 = 5e7;

ll d0[M][M], d1[M][M];
ll n;
int ptr, ans, q, cnt;
int pw2[X], hwt[N];

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 = 1;
    while (b > X) {
        a = (a * pw2[X]) % MOD;
        b -= X;
    }
    return a * pw2[b] % MOD;
    */
    return 1ll * hwt[b / X] * pw2[b % X] % 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) {

    //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), cin.tie(0);

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

    int query = read();
    //cin >> query;
    while (query--) {
        //cin >> n;
        n = read();
        if (n & (n + 1))
            F(1, n);
        else {
            q = 0;
            Complete(1);
            ans = q;
        }
        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

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:156:16: warning: iteration 49999999 invokes undefined behavior [-Waggressive-loop-optimizations]
  156 |         pw2[i] = pw2[i - 1] << 1;
      |         ~~~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:155:23: note: within this loop
  155 |     for (int i = 1; i <= X; i++) {
      |                     ~~^~~~
Main.cpp:162:43: warning: array subscript 50000000 is above array bounds of 'int [50000000]' [-Warray-bounds]
  162 |         hwt[i] = (1ll * hwt[i - 1] * pw2[X]) % MOD;
      |                                      ~~~~~^
Main.cpp:27:5: note: while referencing 'pw2'
   27 | int pw2[X], hwt[N];
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 266 ms 195972 KB Output is correct
2 Correct 266 ms 196004 KB Output is correct
3 Correct 276 ms 195940 KB Output is correct
4 Correct 271 ms 196116 KB Output is correct
5 Correct 280 ms 196036 KB Output is correct
6 Correct 253 ms 196004 KB Output is correct
7 Correct 266 ms 196088 KB Output is correct
8 Correct 297 ms 195912 KB Output is correct
9 Correct 271 ms 196096 KB Output is correct
10 Correct 271 ms 196020 KB Output is correct
11 Correct 303 ms 195996 KB Output is correct
12 Correct 312 ms 196000 KB Output is correct
13 Correct 305 ms 196004 KB Output is correct
14 Correct 297 ms 196016 KB Output is correct
15 Correct 312 ms 195940 KB Output is correct
16 Correct 290 ms 196044 KB Output is correct
17 Correct 309 ms 196060 KB Output is correct
18 Correct 323 ms 195992 KB Output is correct
19 Correct 298 ms 196048 KB Output is correct
20 Correct 287 ms 196056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 270 ms 196040 KB Output is correct
2 Correct 278 ms 196156 KB Output is correct
3 Correct 294 ms 196084 KB Output is correct
4 Correct 293 ms 196048 KB Output is correct
5 Correct 267 ms 196012 KB Output is correct
6 Correct 285 ms 196096 KB Output is correct
7 Correct 279 ms 196060 KB Output is correct
8 Correct 262 ms 196100 KB Output is correct
9 Correct 279 ms 196124 KB Output is correct
10 Correct 270 ms 196128 KB Output is correct
11 Correct 368 ms 196184 KB Output is correct
12 Correct 373 ms 196136 KB Output is correct
13 Correct 410 ms 196180 KB Output is correct
14 Correct 404 ms 196176 KB Output is correct
15 Correct 418 ms 196180 KB Output is correct
16 Correct 413 ms 196184 KB Output is correct
17 Correct 390 ms 196060 KB Output is correct
18 Correct 392 ms 196128 KB Output is correct
19 Correct 405 ms 196184 KB Output is correct
20 Correct 445 ms 196180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 438 ms 196096 KB Output is correct
2 Correct 403 ms 196156 KB Output is correct
3 Correct 428 ms 196100 KB Output is correct
4 Correct 362 ms 196132 KB Output is correct
5 Correct 487 ms 196128 KB Output is correct
6 Correct 372 ms 196284 KB Output is correct
7 Correct 439 ms 196132 KB Output is correct
8 Correct 395 ms 196232 KB Output is correct
9 Correct 282 ms 196244 KB Output is correct
10 Correct 306 ms 196148 KB Output is correct
11 Correct 311 ms 196124 KB Output is correct
12 Correct 278 ms 196020 KB Output is correct
13 Correct 619 ms 196132 KB Output is correct
14 Correct 614 ms 196128 KB Output is correct
15 Correct 741 ms 196132 KB Output is correct
16 Correct 618 ms 196132 KB Output is correct
17 Correct 697 ms 196136 KB Output is correct
18 Correct 626 ms 196164 KB Output is correct
19 Correct 626 ms 196136 KB Output is correct
20 Correct 601 ms 196136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 266 ms 195972 KB Output is correct
2 Correct 266 ms 196004 KB Output is correct
3 Correct 276 ms 195940 KB Output is correct
4 Correct 271 ms 196116 KB Output is correct
5 Correct 280 ms 196036 KB Output is correct
6 Correct 253 ms 196004 KB Output is correct
7 Correct 266 ms 196088 KB Output is correct
8 Correct 297 ms 195912 KB Output is correct
9 Correct 271 ms 196096 KB Output is correct
10 Correct 271 ms 196020 KB Output is correct
11 Correct 303 ms 195996 KB Output is correct
12 Correct 312 ms 196000 KB Output is correct
13 Correct 305 ms 196004 KB Output is correct
14 Correct 297 ms 196016 KB Output is correct
15 Correct 312 ms 195940 KB Output is correct
16 Correct 290 ms 196044 KB Output is correct
17 Correct 309 ms 196060 KB Output is correct
18 Correct 323 ms 195992 KB Output is correct
19 Correct 298 ms 196048 KB Output is correct
20 Correct 287 ms 196056 KB Output is correct
21 Correct 270 ms 196040 KB Output is correct
22 Correct 278 ms 196156 KB Output is correct
23 Correct 294 ms 196084 KB Output is correct
24 Correct 293 ms 196048 KB Output is correct
25 Correct 267 ms 196012 KB Output is correct
26 Correct 285 ms 196096 KB Output is correct
27 Correct 279 ms 196060 KB Output is correct
28 Correct 262 ms 196100 KB Output is correct
29 Correct 279 ms 196124 KB Output is correct
30 Correct 270 ms 196128 KB Output is correct
31 Correct 368 ms 196184 KB Output is correct
32 Correct 373 ms 196136 KB Output is correct
33 Correct 410 ms 196180 KB Output is correct
34 Correct 404 ms 196176 KB Output is correct
35 Correct 418 ms 196180 KB Output is correct
36 Correct 413 ms 196184 KB Output is correct
37 Correct 390 ms 196060 KB Output is correct
38 Correct 392 ms 196128 KB Output is correct
39 Correct 405 ms 196184 KB Output is correct
40 Correct 445 ms 196180 KB Output is correct
41 Correct 438 ms 196096 KB Output is correct
42 Correct 403 ms 196156 KB Output is correct
43 Correct 428 ms 196100 KB Output is correct
44 Correct 362 ms 196132 KB Output is correct
45 Correct 487 ms 196128 KB Output is correct
46 Correct 372 ms 196284 KB Output is correct
47 Correct 439 ms 196132 KB Output is correct
48 Correct 395 ms 196232 KB Output is correct
49 Correct 282 ms 196244 KB Output is correct
50 Correct 306 ms 196148 KB Output is correct
51 Correct 311 ms 196124 KB Output is correct
52 Correct 278 ms 196020 KB Output is correct
53 Correct 619 ms 196132 KB Output is correct
54 Correct 614 ms 196128 KB Output is correct
55 Correct 741 ms 196132 KB Output is correct
56 Correct 618 ms 196132 KB Output is correct
57 Correct 697 ms 196136 KB Output is correct
58 Correct 626 ms 196164 KB Output is correct
59 Correct 626 ms 196136 KB Output is correct
60 Correct 601 ms 196136 KB Output is correct
61 Correct 279 ms 196000 KB Output is correct
62 Correct 281 ms 196076 KB Output is correct
63 Correct 1253 ms 196100 KB Output is correct
64 Correct 1393 ms 196164 KB Output is correct
65 Correct 1364 ms 196156 KB Output is correct
66 Correct 1522 ms 196160 KB Output is correct
67 Correct 1501 ms 196104 KB Output is correct
68 Correct 1369 ms 196148 KB Output is correct
69 Correct 1419 ms 196156 KB Output is correct
70 Correct 1338 ms 196152 KB Output is correct
71 Correct 329 ms 196084 KB Output is correct
72 Correct 393 ms 196152 KB Output is correct
73 Correct 436 ms 196184 KB Output is correct
74 Correct 478 ms 196244 KB Output is correct
75 Execution timed out 3078 ms 196164 KB Time limit exceeded
76 Halted 0 ms 0 KB -