QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#479918#8565. Basic Bloomsucup-team3519#AC ✓1308ms6896kbC++234.0kb2024-07-15 21:58:302024-07-15 21:58:30

Judging History

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

  • [2024-07-15 21:58:30]
  • 评测
  • 测评结果:AC
  • 用时:1308ms
  • 内存:6896kb
  • [2024-07-15 21:58:30]
  • 提交

answer

#include <bits/stdc++.h>

template <int m>
struct ModInt {
private:
    int raw_;
public:
    using mint = ModInt;
    using i64 = int64_t;

    ModInt() {
        raw_ = 0;
    }
    template <typename Tp>
    ModInt(const Tp &v) {
        raw_ = v % m;
    }

    int value() const {
        return (raw_ + m) % m;
    }

    mint &operator+=(const mint &rhs) {
        raw_ = (raw_ + rhs.raw_) % m;
        return *this;
    }
    mint &operator-=(const mint &rhs) {
        raw_ = (raw_ - rhs.raw_) % m;
        return *this;
    }
    mint &operator*=(const mint &rhs) {
        raw_ = (i64)raw_ * rhs.raw_ % m;
        return *this;
    }
    mint &operator/=(const mint &rhs) {
        raw_ = (i64)raw_ * qpow(rhs.raw_, m - 2) % m;
        return *this;
    }
    
    friend mint operator+(const mint &lhs, const mint &rhs) {
        return mint{lhs} += rhs;
    }
    friend mint operator-(const mint &lhs, const mint &rhs) {
        return mint{lhs} -= rhs;
    }
    friend mint operator*(const mint &lhs, const mint &rhs) {
        return mint{lhs} *= rhs;
    }
    friend mint operator/(const mint &lhs, const mint &rhs) {
        return mint{lhs} /= rhs;
    }

    static int qpow(int a, int b) {
        int res = 1;
        while (b) {
            if (b & 1) {
                res = (i64)res * a % m;
            }
            a = (i64)a * a % m, b >>= 1;
        }
        return res;
    }
};

constexpr int N = 1e6;
constexpr int MOD = 998244353, HELPER_MOD = 1004535989;
using mint = ModInt<MOD>;
using i64 = int64_t;
using i128 = __int128;
using real = long double;

template <typename Z>
constexpr Z qpow(Z a, int b) {
    Z res = 1;
    while (b) {
        if (b & 1) {
            res *= a;
        }
        a *= a, b >>= 1;
    }
    return res;
}

struct Numeric {
    ModInt<MOD> v;
    ModInt<HELPER_MOD> h1;
    real logarithm;
    i64 enig;
    Numeric(int base, int len, int d) {
        v = ModInt<MOD>{ModInt<MOD>::qpow(base, len) - 1} / (base - 1) * d;
        h1 = ModInt<HELPER_MOD>{ModInt<HELPER_MOD>::qpow(base, len) - 1} / (base - 1) * d;

        logarithm = logl(base) * len - logl(base - 1) + logl(d);
        enig = (qpow<i64>(base, len) - 1) / (base - 1) * d;
    }
    bool is_big() const {
        return logarithm > 30;
    }
    ModInt<MOD> value() const {
        return v;
    }
    friend bool operator==(const Numeric &lhs, const Numeric &rhs) {
        return lhs.v.value() == rhs.v.value() &&
            lhs.h1.value() == rhs.h1.value();
    }
    friend bool operator<(const Numeric &lhs, const Numeric &rhs) {
        if (!lhs.is_big() && !rhs.is_big()) {
            return lhs.enig < rhs.enig;
        }
        return lhs.logarithm < rhs.logarithm;
    }
};

struct Base {
    int base;
    int len, d;
    bool valid;
    Numeric num;
    explicit Base(int b) : base(b), len(1), d(1), valid(true), num(base, len, d) {
    }
    void advance() {
        if (d + 1 < base) {
            d += 1;
        } else {
            d = 1;
            len += 1;
        }
        valid = false;
    }
    Numeric get() {
        if (!valid) {
            valid = true;
            num = Numeric{base, len, d};
        }
        return num;
    }
};

int main() {
    std::vector<mint> s(N + 1);

    std::vector<Base> base;
    for (int i = 2; i <= 16; ++i) {
        base.emplace_back(i);
    }

    for (int i = 1; i <= N; ++i) {
        Numeric res{2, N + 1, 1};
        for (auto &b : base) {
            auto x = b.get();
            if (x < res) {
                res = x;
            }
        }
        for (auto &b : base) {
            if (b.get() == res) {
                b.advance();
            }
        }
        s[i] = s[i - 1] + res.value();
    }

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        int l, r;
        std::cin >> l >> r;
        std::cout << (s[r] - s[l - 1]).value() << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1164ms
memory: 6808kb

input:

3
1 2
1 10
15 2000

output:

3
55
736374621

result:

ok 3 number(s): "3 55 736374621"

Test #2:

score: 0
Accepted
time: 1172ms
memory: 6896kb

input:

100000
26 99975
57 99944
28 99973
62 99939
71 99930
25 99976
53 99948
60 99941
73 99928
72 99929
30 99971
7 99994
3 99998
35 99966
73 99928
68 99933
83 99918
37 99964
63 99938
17 99984
34 99967
74 99927
6 99995
3 99998
23 99978
91 99910
39 99962
85 99916
82 99919
17 99984
61 99940
31 99970
44 99957
...

output:

957904590
358359691
31524403
519690359
208321031
477204717
835715447
186583689
847423322
760952087
25753603
241428916
832623523
232679133
847423322
11425904
640652773
663756612
767901835
356898792
503593019
495288401
265039242
832623523
793754988
389398856
758928836
349243444
158978749
356898792
873...

result:

ok 100000 numbers

Test #3:

score: 0
Accepted
time: 1308ms
memory: 6804kb

input:

1000000
561662 731870
560627 798415
497930 613164
210084 556894
479283 902738
271881 288854
467622 971733
55854 157477
310152 415183
146385 874852
140599 526659
438420 629148
733746 924626
84146 436790
275793 457537
466464 541539
661070 696519
534866 688272
190259 412401
206392 354525
2344 217676
51...

output:

387682849
91353801
759238022
175113502
143631299
488887729
201615869
359127675
954541571
806609754
254074751
589282709
523407089
298821716
593042756
268635027
495659009
878948937
741148909
716887807
31798813
425888650
765930054
831198164
372500280
694558761
918178838
919393601
661100143
134966024
37...

result:

ok 1000000 numbers

Extra Test:

score: 0
Extra Test Passed