QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#277386#5424. NaN in a HeapjrjyyAC ✓25ms3880kbC++204.8kb2023-12-06 18:22:152023-12-06 18:22:15

Judging History

你现在查看的是最新测评结果

  • [2023-12-06 18:22:15]
  • 评测
  • 测评结果:AC
  • 用时:25ms
  • 内存:3880kb
  • [2023-12-06 18:22:15]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

template <typename T>
constexpr T power(T a, i64 b) {
    T c{1};
    while (b) {
        if (b % 2) {
            c *= a;
        }
        a *= a;
        b /= 2;
    }
    return c;
}

template <int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x_) : x{up(x_ % getMod())} {}

    static int Mod;
    constexpr static int getMod() {
        return P > 0 ? P : Mod;
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }

    constexpr int up(int x) const {
        if (x < 0) {
            x += getMod();
        }
        return x;
    }
    constexpr int down(int x) const {
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int norm(int x) const {
        return up(down(x));
    }

    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator+=(MInt rhs) {
        x = down(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) {
        x = up(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator*=(MInt rhs) {
        x = 1ll * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        return lhs += rhs;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        return lhs -= rhs;
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        return lhs *= rhs;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        return lhs /= rhs;
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 x = 0;
        is >> x;
        a = MInt(x);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
};

template <int P>
int MInt<P>::Mod = P;
template <>
int MInt<0>::Mod = 998244353;

template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 1000000007;
using Z = MInt<P>;

constexpr int N = 31;

std::array<std::array<Z, N>, N> f, invf;

void solve() {
    int n;
    std::cin >> n;

    const int m = std::__lg(n) + 1;

    Z prod = 1;
    std::vector<Z> sz(m + 1), invsz(m + 1);
    sz[m] = invsz[m] = 1;
    for (int i = m, x = n; x > 1; --i, x /= 2) {
        int p = m - i + x % 2;
        sz[i - 1] = sz[i] + (1 << p);
        invsz[i - 1] = sz[i - 1].inv();
        prod *= f[p][0] * sz[i - 1];
    }
    Z invprod = prod.inv();

    std::vector<std::vector<Z>> invg(m, std::vector<Z>(m + 1));
    invg[0].assign(m + 1, 1);
    for (int i = 1; i < m; ++i) {
        for (int j = 1; j <= m - i; ++j) {
            invg[i][j] = invg[i - 1][j] * (sz[i] - (1 << j) + 1) * invsz[i];
        }
    }
    for (int i = 1; i < m; ++i) {
        invg[m - i][i] = invg[m - i][i].inv();
    }
    for (int i = m - 2; i >= 1; --i) {
        for (int j = 1; j <= m - i - 1; ++j) {
            invg[i][j] = invg[i + 1][j] * (sz[i + 1] - (1 << j) + 1) * invsz[i + 1];
        }
    }

    Z ans = 0;
    for (int i = m, x = n; x > 0; --i, x /= 2) {
        Z u = sz[i], v = 1;
        for (int j = 1; j < i; ++j) {
            u *= sz[j];
            v *= sz[j] - sz[i];
        }
        ans += u / v * invprod;
        if (x > 1) {
            int p = m - i + x % 2;
            for (int y = 1; y <= p; ++y) {
                Z coef = ((1 << y) - 1) * f[p][0] * invprod;
                coef *= invg[i - 1][y] * invf[p][y] * invf[y][0];
                ans += coef * (1 << (p - y));
            }
        }
    }

    std::cout << ans / n << "\n";
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    f[0][0] = 1;
    for (int i = 1; i < N; ++i) {
        f[i][i] = 1;
        for (int j = 0; j < i; ++j) {
            f[i][j] = f[i - 1][j] * f[i - 1][0] * ((1 << i) - (1 << j));
        }
    }
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j <= i; ++j) {
            invf[i][j] = f[i][j].inv();
        }
    }

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3648kb

input:

5
1
3
7
10
20221218

output:

1
666666672
55555556
596445110
3197361

result:

ok 5 number(s): "1 666666672 55555556 596445110 3197361"

Test #2:

score: 0
Accepted
time: 6ms
memory: 3880kb

input:

1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101...

output:

1
1
666666672
375000003
633333338
97222223
55555556
822222228
123456791
596445110
229888169
878681664
673617560
436681157
699563287
68610901
902106812
308824953
904504598
779800169
693423362
328728506
466956451
68896808
88594095
156207594
533144330
758445723
92289701
206321076
267778621
266415260
48...

result:

ok 1000 numbers

Test #3:

score: 0
Accepted
time: 25ms
memory: 3764kb

input:

1000
1000000000
999999999
999999998
999999997
999999996
999999995
999999994
999999993
999999992
999999991
999999990
999999989
999999988
999999987
999999986
999999985
999999984
999999983
999999982
999999981
999999980
999999979
999999978
999999977
999999976
999999975
999999974
999999973
999999972
9999...

output:

191769339
839078655
63430702
488796230
588110810
163742647
964465260
961862258
425060141
344042065
747463620
143548999
281463738
797756640
382302841
365802993
511891059
902367958
70796927
495040138
33561329
728278059
879674300
992542203
309248753
251669085
188046077
672990625
635281273
113409431
972...

result:

ok 1000 numbers

Test #4:

score: 0
Accepted
time: 24ms
memory: 3664kb

input:

1000
524125987
923264237
374288891
535590429
751244358
124321145
232930851
266089174
543529670
773363571
319728747
580543238
582720391
468188689
490702144
598813561
138628383
284660056
733781508
155605777
931759705
245485733
723534730
257812292
794937524
596788519
188451996
981010588
14483682
592676...

output:

726166876
355333772
482635633
758157044
775831523
760255234
748551027
477359472
86466892
226497967
994190156
546377096
39059573
537362710
398171443
66921908
845526053
340839799
621258613
555318917
603009173
528685604
550082786
978381020
650853592
125984432
139054932
319259349
481246666
178000233
799...

result:

ok 1000 numbers