QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#305930 | #5464. Dice Game | ckiseki | TL | 54ms | 4808kb | C++20 | 3.0kb | 2024-01-16 06:12:12 | 2024-01-16 06:12:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int modpow(int64_t e, int p) {
int64_t r = 1;
while (p) {
if (p & 1) r = r * e % mod;
e = e * e % mod;
p >>= 1;
}
return r;
}
vector<pair<int64_t,int>> gen(int64_t h[]) {
vector<pair<int64_t,int>> ret, u, v;
ret.reserve(1 << 15);
u.reserve(1 << 15);
v.reserve(1 << 15);
ret.emplace_back(0, 0);
for (int i = 15 - 1; i >= 0; i--) {
u.clear();
v.clear();
for (size_t j = 0; j < ret.size(); j++) {
u.emplace_back(ret[j].first - h[i], ret[j].second + (1 << i));
v.emplace_back(ret[j].first + h[i], ret[j].second);
}
ret.resize(u.size() + v.size());
merge(u.begin(), u.end(), v.begin(), v.end(), ret.begin());
}
return ret;
}
void solve(int n) {
int64_t h[30];
for (int i = 0; i < 30; i++) {
int L = (1 << i);
// h[i] = ((n - 1) / L + 1) / 2;
h[i] = (n / L) / 2 * L +
((n / L) % 2 == 1 ? n % L : 0);
}
// for (int i = 0; i < 30; i++) {
// int cnt = 0;
// for (int j = 0; j < n; j++) {
// cnt += (j >> i & 1);
// }
// // h[i] = cnt;
// cerr<<cnt<<','<<h[i]<<' '<<n<<endl;
// assert (cnt == h[i]);
// }
// return;
for (int i = 0; i < 30; i++)
h[i] <<= i;
auto lo = gen(h);
auto hi = gen(h + 15);
// cerr << "|lo| = " << lo.size() << endl;
// cerr << "|hi| = " << hi.size() << endl;
reverse(lo.begin(), lo.end());
const int L = (n >> 15);
size_t it = 0;
int64_t sum = 0;
int64_t cnt = 0;
int64_t ans = 0;
// for (auto [s1, k1]: hi) {
// if (k1 <= 1) {
// for (auto [s2, k2]: lo) {
// if (s1 + s2 > 0 && (k1 << 15) + k2 < n) {
// ans += s1 + s2;
// }
// // if ((k1 << 15) + k2 < n)
// // cerr << s1 + s2 << ' ' << k1 << ' ' << k2 << endl;
// }
// }
// }
for (auto [s1, k1]: hi) {
while (it < lo.size() && lo[it].first + s1 > 0) {
sum += lo[it].first;
cnt += 1;
++it;
}
if (k1 < L) {
ans = (ans + s1 * cnt + sum) % mod;
} else if (k1 == L) {
for (auto [s2, k2]: lo) {
if (s1 + s2 > 0 && (k1 << 15) + k2 < n) {
ans = (ans + s1 + s2) % mod;
}
}
}
}
// cerr << "n = " << n << endl;
// cerr << "ans = " << ans << endl;
ans = 1LL * ans * modpow(1LL * n * n, mod - 2) % mod;
ans = (ans + 1LL * (n - 1) * modpow(2, mod - 2)) % mod;
cout << ans << '\n';
}
signed main() {
cin.tie(nullptr) -> sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
solve(n);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4808kb
input:
4 1 2 3 4
output:
0 249561089 776412276 2
result:
ok 4 number(s): "0 249561089 776412276 2"
Test #2:
score: 0
Accepted
time: 54ms
memory: 4732kb
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 296012775 400009943 299473312 221064504 400009943 60548260 896063466 573066250 471393174 175624519 531171954 143020402 134763040 560646647 43176836 269095960 284396636 191400715 895243672 967774080 745220060 584864173 5941827 724073586 701650152 262576881 417830609 833275086 916357319 1435...
result:
ok 100 numbers
Test #3:
score: 0
Accepted
time: 10ms
memory: 4724kb
input:
10 321 4212 2977 3438 679 2518 3359 2348 861 1853
output:
115992677 210903174 216412050 780704799 956649209 200804004 267658252 194263892 880330717 328032438
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 7ms
memory: 4728kb
input:
12 3324 3167 3780 2887 1541 494 4374 3416 4036 3637 3888 4630
output:
458228110 29999980 333283438 544178470 753289945 257353919 515561689 628492255 799560584 924311157 17047279 109086825
result:
ok 12 numbers
Test #5:
score: -100
Time Limit Exceeded
input:
10000 11424730 231352165 835903988 101946284 358678826 610737477 100503274 909725926 510714513 407317433 668332866 442951503 582321912 117772499 180141789 570384258 375362779 724696666 959258227 178470161 245672142 629464136 515375633 58812672 119047157 272587596 993112981 678095944 67996298 2717673...
output:
684037877 621784086 775115168 592320243 465168348 812154766 695423679 812830487 599736098 722561844 301372415 163406712 803501689 705975922 877364585 293106070 328629743 894941470 506248280 672648134 768749560 149447356 82631461 473124021 722052172 778328876 747239770 212335591 854308646 143332855 6...