QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578192 | #5464. Dice Game | xin11 | WA | 0ms | 3648kb | C++23 | 3.8kb | 2024-09-20 17:15:26 | 2024-09-20 17:15:27 |
Judging History
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 << "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: 0
Wrong Answer
time: 0ms
memory: 3648kb
input:
4 1 2 3 4
output:
249561089 765231376 776412276 2
result:
wrong answer 1st numbers differ - expected: '0', found: '249561089'