QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#281765 | #5464. Dice Game | samnever | WA | 1ms | 3980kb | C++14 | 2.9kb | 2023-12-10 18:16:17 | 2023-12-10 18:16:18 |
Judging History
answer
#include<bits/stdc++.h>
#define fs(i,x,y) for(int i=(x),_=(y);i<=_;++i)
#define fn(i,x,y) for(int i=(x),_=(y);i>=_;--i)
#define int ll
using namespace std;
typedef long long ll;
typedef double db;
const int N = 1e5 + 7, P = 998244353, iv2 = (P + 1) / 2;
int n;
int cnt[N];
//zhuyi bao longlong
int qw(int a, int b) {
int ans = 1;
for (;b;a = (ll)a * a % P, b >>= 1)if (b & 1)ans = (ll)ans * a % P;
return ans;
}
void pre() {
int lim = 2;
fs(i, 0, 30) {
int tp = n % lim;
cnt[i] = (n - tp) / 2;
if ((tp >> i) & 1) {
cnt[i] += (n % (lim >> 1)) + 1;
}
// cout << i << ' ' << cnt[i] << endl;
cnt[i] *= (lim >> 1);
lim <<= 1;
}
// fs(i, 0, 30)cout << cnt[i] << ' ';
// cout << endl;
}
bool calc(int x) {
ll tp = 0;
fn(i, 30, 0) {
if ((x >> i) & 1) {
tp -= cnt[i];
}
else tp += cnt[i];
}
return tp > 0;
}
int qry(int l, int r) {
int ans = l - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (calc(mid))ans = mid, l = mid + 1;
else r = mid - 1;
}
return ans;
}
int sol(int dig, int l, int pos) {
if (pos < l)return 0;
int ans = 0;
fs(i, dig + 1, 30) {
ans = (ans + cnt[i] % P * (pos - l + 1) % P) % P;
}
ans = (ans - cnt[dig] % P * (pos - l + 1) % P + P) % P;
int lim = 2;
fs(i, 0, dig - 1) {
if ((pos >> i) & 1) {
ans = (ans + cnt[i] % P * ((lim >> 1) - 1 - (pos % (lim >> 1))) % P) % P;
// cout << ((lim >> 1) - 1 - (pos % (lim >> 1))) << " " << "jishu" << "///";
}
else {
ans = (ans + cnt[i] % P * ((pos % (lim >> 1)) + 1) % P) % P;
// cout << pos % (lim >> 1) + 1 << ' ' << "oushu" << "///";
}
lim <<= 1;
}
return ans;
}
void solve() {
scanf("%lld", &n);
--n;
pre();
// fs(i, 0, n) {
// cout << calc(i) << ' ' << i << endl;
// }
int ans = n * (n + 1) % P * (n + 2) % P * iv2 % P;
// cout << ans << endl;
// cout << "HHH" << endl;
for (int i = 0;;++i) {
int l = (1ll << i), r = min((1ll << (i + 1)) - 1, n);
if (l > n)break;
// cout << "HHH" << endl;
int pos = qry(l, r);
// cout << pos << endl;
// cout << "///";
// cout << pos << ' ' << sol(i, l, pos) << endl;
ans += sol(i, l, pos);
// cout << endl;
// ans += (l + r) * (r - l + 1) % P * iv2 % P * n % P;
}
// cout << ans << endl;
// cout << "abs" << endl;
printf("%lld\n", ans * qw(n + 1, P - 2) % P * qw(n + 1, P - 2) % P);
}
signed main() {
// freopen("in", "r", stdin);
// freopen("hhout", "w", stdout);
// cout << 6 << endl;
// fs(i, 5000, 5005)cout << i << endl;
int T = 0;
scanf("%lld", &T);
while (T--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3980kb
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: 3932kb
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 723451295 531171954 143020402 389315350 560646647 43176836 269095960 284396636 191400715 269215551 967774080 745220060 584864173 5941827 724073586 701650152 262576881 417830609 833275086 916357319 1435...
result:
wrong answer 11th numbers differ - expected: '175624519', found: '723451295'