QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#269409#5424. NaN in a HeapjrjyyAC ✓1476ms3776kbC++204.1kb2023-11-29 16:45:102023-11-29 16:45:11

Judging History

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

  • [2023-11-29 16:45:11]
  • 评测
  • 测评结果:AC
  • 用时:1476ms
  • 内存:3776kb
  • [2023-11-29 16:45:10]
  • 提交

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;

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

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

    Z prod = 1, ans = 0;
    std::vector<Z> sz(m + 1);
    sz[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);
        prod *= f[p][0] * sz[i - 1];
    }
    assert(sz[1] == n);
    for (int i = m, x = n; x > 0; --i, x /= 2) {
        Z coef = sz[i] / prod;
        for (int j = 1; j < i; ++j) {
            coef /= (sz[j] - sz[i]) / sz[j];
        }
        ans += coef;
        if (x > 1) {
            int p = m - i + x % 2;
            for (int y = 1; y <= p; ++y) {
                Z coef = ((1 << y) - 1) / prod;
                for (int j = 1; j < i; ++j) {
                    coef /= (sz[j] - ((1 << y) - 1)) / sz[j];
                }
                coef /= f[p][y] * f[y][0] / f[p][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));
        }
    }

    int t;
    std::cin >> t;

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

    return 0;
}

详细

Test #1:

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

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: 52ms
memory: 3492kb

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: 1476ms
memory: 3604kb

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: 1337ms
memory: 3540kb

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