QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#578199#5464. Dice Gamexin11WA 0ms3712kbC++233.9kb2024-09-20 17:16:182024-09-20 17:16:18

Judging History

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

  • [2024-09-20 17:16:18]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3712kb
  • [2024-09-20 17:16:18]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

template <unsigned M_> struct ModInt {
    static constexpr unsigned M = M_;
    unsigned x;
    constexpr ModInt() : x(0) {}
    constexpr ModInt(unsigned x_) : x(x_ % M) {}
    constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
    constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
    constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
    ModInt &operator+=(const ModInt &a) {
        x = ((x += a.x) >= M) ? (x - M) : x;
        return *this;
    }
    ModInt &operator-=(const ModInt &a) {
        x = ((x -= a.x) >= M) ? (x + M) : x;
        return *this;
    }
    ModInt &operator*=(const ModInt &a) {
        x = (static_cast<unsigned long long>(x) * a.x) % M;
        return *this;
    }
    ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
    ModInt pow(long long e) const {
        if (e < 0)
            return inv().pow(-e);
        ModInt a = *this, b = 1;
        for (; e; e >>= 1) {
            if (e & 1)
                b *= a;
            a *= a;
        }
        return b;
    }
    ModInt inv() const {
        unsigned a = M, b = x;
        int y = 0, z = 1;
        for (; b;) {
            const unsigned q = a / b;
            const unsigned c = a - q * b;
            a = b;
            b = c;
            const int w = y - static_cast<int>(q) * z;
            y = z;
            z = w;
        }
        assert(a == 1);
        return ModInt(y);
    }
    ModInt operator+() const { return *this; }
    ModInt operator-() const {
        ModInt a;
        a.x = x ? (M - x) : 0;
        return a;
    }
    ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
    ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
    ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
    ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
    template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
    template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
    template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
    template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
    explicit operator bool() const { return x; }
    bool operator==(const ModInt &a) const { return (x == a.x); }
    bool operator!=(const ModInt &a) const { return (x != a.x); }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
// using mint = ModInt<1000000007U>;
using mint = ModInt<998244353U>;

void solve() {
    ll n;
    cin >> n;
	if (n == 1) {
		cout << "0\n";
		return;
	}
    if (n == 2) {
        cout << "249561089\n";
        return;
    }
    const int LG = 62;
    int msb = 0;
    while ((1LL << (msb + 1)) < n)
        msb++;
    auto count_zeros = [&](ll n, int i) {
        ll pw = 1LL << i;
        ll count = n / (pw * 2) * pw;
        n %= pw * 2;
        count += min(pw, n);
        return count;
    };
    mint from = 1LL << msb;
    mint to = n - 1;
    mint ans = (from + to) * (to - from + 1) / 2 * n;
    mint kek = 1LL << (msb - 1);
    for (int i = 0; i < LG; i++) {
        mint cnt = n - count_zeros(n, i);
        if (i < msb)
            ans += n * kek * (1LL << i);
        else
            ans += cnt * kek * 2 * (1LL << i);
    }
    cout << ans * mint(n).pow(2).inv() << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    cin >> T;
    for (int tc = 1; tc <= T; tc++) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

4
1
2
3
4

output:

0
249561089
776412276
2

result:

ok 4 number(s): "0 249561089 776412276 2"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3712kb

input:

100
119
75
29
10
17
29
449
71
72
12
79
117
83
80
35
272
105
497
346
287
362
140
297
167
111
419
210
212
170
413
373
210
196
39
1
101
258
496
333
293
392
2
187
431
157
342
436
106
449
136
378
243
357
325
237
254
22
292
62
435
18
446
471
18
42
377
181
350
19
389
212
58
45
70
52
63
107
71
66
355
381
30...

output:

645006489
696197840
400009943
738700827
818629461
400009943
404386276
274858831
6162044
610038223
923548021
428933764
579616457
419262678
143421249
620016971
390515069
284396636
272483674
832999565
205386701
74562821
990356716
256209833
348466020
70467186
281907962
847743783
662295514
169987
2928173...

result:

wrong answer 2nd numbers differ - expected: '296012775', found: '696197840'