QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#181629 | #5464. Dice Game | realIyxiang | WA | 0ms | 3620kb | C++14 | 2.6kb | 2023-09-16 21:36:10 | 2023-09-16 21:36:11 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
typedef long long ll;
const int N = 30 + 5;
const int P = 998244353;
int T, n, m = 29, ans, Inv2, b[N], c[N];
long long pre[N];
int inc (int a, int b) { return (a += b) >= P ? a - P : a; }
int dec (int a, int b) { return (a -= b) < 0 ? a + P : a; }
int mul (int a, int b) { return 1ll * a * b % P; }
int fpow (int a, int b) { int ans = 1; for ( ; b; a = mul(a, a), b >>= 1) if(b & 1) ans = mul(ans, a); return ans; }
int main () {
ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> T;
Inv2 = fpow(2, P - 2);
while (T--) {
cin >> n, --n, ans = 0;
auto calc = [&](int n, int i) {
int b = ((n >> i) & 1), ans = 0;
if(b) ans = ((n & ((1 << i) - 1)) + 1);
ans += ((n & (((1 << (m + 1)) - 1) ^ ((1 << (i + 1)) - 1))) >> 1);
return ans;
} ;
rep(i, 0, m) b[i] = ((n >> i) & 1), c[i] = calc(n, i);
// rep(i, 0, m) cout << i << ' ' << c[i] << '\n';
pre[0] = c[0];
rep(i, 1, m) pre[i] = pre[i - 1] + 1ll * (1ll << i) * c[i];
auto chk = [&](int u, bool lim) {
int cur = (!lim ? ((1 << (u + 1)) - 1) : (n & ((1 << (u + 1)) - 1)));
// cout << "chk : " << u << ' ' << lim << ' ' << cur << '\n';
rep(i, 0, u) {
int cnt = calc(cur, i);
// cout << i << ' ' << cnt << '\n';
ans = dec(ans, mul(cnt, mul(c[i], 1 << i)));
ans = inc(ans, mul(cur + 1 - cnt, mul(c[i], 1 << i)));
}
} ;
function <void(int, int, ll)> dfs = [&](int u, bool lim, ll w) {
if(w <= 0 && w + pre[u] <= 0) return ;
// cout << u << ' ' << lim << ' ' << w << '\n';
if(u == -1) return ans = inc(ans, w), void();
if(w - pre[u] >= 0) {
ans = inc(ans, mul(w % P, 1 << (u + 1)));
return chk(u, lim), void();
}
dfs(u - 1, lim & (!b[u]), w + 1ll * c[u] * (1 << u));
if(!lim || (b[u] == 1)) dfs(u - 1, lim & (b[u]), w - 1ll * c[u] * (1 << u));
} ;
dfs(m, 1, 0);
/*rep(i, 0, n) {
ll res = 0;
rep(j, 0, m) {
if((i >> j) & 1) res -= 1ll * c[j] * (1ll << j);
else res += 1ll * c[j] * (1ll << j);
}
if(res > 0) ans = inc(ans, res % P);
// cout << i << ' ' << res << '\n';
}*/
// cout << ans << '\n';
ans = inc(ans, mul(n, mul(n + 1, mul(n + 1, Inv2))));
// cout << ans << '\n';
int Inv = fpow(n + 1, P - 2);
ans = mul(ans, mul(Inv, Inv));
cout << ans << '\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3616kb
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: 3620kb
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 390069577 400009943 299473312 801358800 400009943 60548260 963193965 449826208 471393174 175624519 531171954 143020402 134763040 128753173 343686395 269095960 284396636 191400715 404162575 967774080 15890517 26300464 5941827 724073586 701650152 262576881 417830609 833275086 916357319 14352...
result:
wrong answer 2nd numbers differ - expected: '296012775', found: '390069577'