QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#132780 | #5464. Dice Game | ClHg2 | WA | 1ms | 3648kb | C++14 | 2.9kb | 2023-07-31 15:53:28 | 2023-07-31 15:53:30 |
Judging History
answer
#include <array>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
namespace {
using std::cin;
using std::cout;
using std::int64_t;
using std::size_t;
namespace base {
template <typename T, size_t... sizes>
struct NestedArray {};
template <typename T, size_t size, size_t... sizes>
struct NestedArray<T, size, sizes...> {
using Type = std::array<typename NestedArray<T, sizes...>::Type, size>;
};
template <typename T>
struct NestedArray<T> {
using Type = T;
};
template <typename T, size_t... sizes>
using Array = typename NestedArray<T, sizes...>::Type;
void OptimizeIO() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
}
void OptimizeIO(const std::string &input_file, const std::string &output_file) {
static std::ifstream input_stream(input_file);
static std::ofstream output_stream(output_file);
cin.rdbuf(input_stream.rdbuf()), cout.rdbuf(output_stream.rdbuf());
cin.tie(nullptr), cout.tie(nullptr);
}
} // namespace base
using base::Array;
const int kP = 998244353, kInv2 = (kP + 1) / 2;
namespace mod {
inline void Mul(int &a, int b) { a = int64_t{a} * b % kP; }
int Pow(int a, int b) {
int ans = 1, prod = a;
while (b) {
if (b & 1) Mul(ans, prod);
Mul(prod, prod), b >>= 1;
}
return ans;
}
inline int Inv(int a) { return Pow(a, kP - 2); }
} // namespace mod
const int kMaxLogN = 30;
int n;
int64_t ans;
Array<int64_t, kMaxLogN> c, sum;
inline int Calc(int n, int k) {
int q = (n - 1) >> (k + 1), r = n - (q << (k + 1));
return (q << k) + std::max(0, r - (1 << k));
}
void Dfs(int cur, bool f, int64_t sum) {
if (cur == -1) {
ans += std::max(sum % kP, int64_t{0});
return;
}
if (sum + ::sum[cur] <= 0) return;
if (sum - ::sum[cur] >= 0) {
if (f) {
int m = ((n - 1) & ((1 << (cur + 1)) - 1)) + 1;
ans += sum % kP * m % kP;
for (int i = 0; i <= cur; ++i) {
int x = Calc(m, i);
int val = (c[i] % kP * (m - 2 * x) % kP << i) % kP;
if (val < 0) val += kP;
ans += val;
}
} else {
ans += ((sum % kP) << (cur + 1)) % kP;
}
return;
}
if (f) {
if ((n - 1) >> cur & 1) {
Dfs(cur - 1, true, sum - c[cur]);
Dfs(cur - 1, false, sum + c[cur]);
} else {
Dfs(cur - 1, true, sum + c[cur]);
}
} else {
Dfs(cur - 1, false, sum - c[cur]);
Dfs(cur - 1, false, sum + c[cur]);
}
}
void Solve() {
cin >> n;
for (int i = 0; i < kMaxLogN; ++i) {
sum[i] = c[i] = int64_t{Calc(n, i)} << i;
if (i) sum[i] += sum[i - 1];
}
ans = 0, Dfs(kMaxLogN - 1, true, 0);
int inv = mod::Inv(n);
cout << (ans % kP * inv % kP * inv + int64_t{n - 1} * kInv2) % kP << "\n";
}
int Main() {
base::OptimizeIO();
int t;
cin >> t;
while (t--) Solve();
return 0;
}
} // namespace
int main() { return Main(); }
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3596kb
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: 1ms
memory: 3648kb
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 870114196 400009943 299473312 459399661 400009943 60548260 894281243 856518353 471393174 175624519 531171954 143020402 134763040 890678454 115713657 269095960 284396636 191400715 75308794 967774080 609132881 290921431 5941827 724073586 701650152 262576881 417830609 833275086 916357319 1435...
result:
wrong answer 2nd numbers differ - expected: '296012775', found: '870114196'