QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121440 | #5464. Dice Game | savacska | WA | 1ms | 3508kb | C++23 | 3.2kb | 2023-07-08 06:50:55 | 2023-07-08 06:50:59 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long ll;
const int MOD = 998244353;
vector <pair <int, int> > segments;
ll koef[35];
int cnt_ones(int n, int bit)
{
if (n <= 0) return 0;
int a = (n >> (bit + 1));
int ans = (a << bit);
if (n >= (a << (bit + 1)) + (1 << bit)) ans += n - (a << (bit + 1)) - (1 << bit) + 1;
return ans;
}
int cnt_ones(int lef, int rig, int bit)
{
return cnt_ones(rig, bit) - cnt_ones(lef - 1, bit);
}
void gener(int x, int n)
{
if (x >= n)
{
segments.back().y = n - 1;
return;
}
ll sum = 0;
for (int bit = 0; bit <= 29; bit++)
if ((x >> bit) & 1) sum -= koef[bit];
else sum += koef[bit];
if (sum >= 0)
{
for (int bit = 0; bit <= 29; bit++)
{
if ((x >> bit) & 1) continue;
ll poten = 0;
for (int i = 29; i > bit; i--)
if ((x >> i) & 1) poten -= koef[i];
else poten += koef[i];
poten -= koef[bit];
for (int i = bit - 1; i >= 0; i--) poten -= koef[i];
if (poten >= 0) continue;
int new_x = ((x >> (bit + 1)) << (bit + 1)) + (1 << (bit + 1)) - 1;
for (int i = bit - 1; i >= 0; i--)
{
if (poten + 2 * koef[i] < 0)
{
poten += 2 * koef[i];
new_x -= (1 << i);
}
}
segments.pb(mp(x, new_x - 1));
gener(new_x, n);
return;
}
segments.pb(mp(x, n - 1));
}
else
{
for (int bit = 0; bit <= 29; bit++)
{
if ((x >> bit) & 1) continue;
ll poten = 0;
for (int i = 29; i > bit; i--)
if ((x >> i) & 1) poten -= koef[i];
else poten += koef[i];
poten -= koef[bit];
for (int i = bit - 1; i >= 0; i--) poten += koef[i];
if (poten < 0) continue;
int new_x = ((x >> (bit + 1)) << (bit + 1)) + (1 << bit);
segments.pb(mp(x, new_x - 1));
gener(new_x, n);
return;
}
segments.pb(mp(x, n - 1));
}
return;
}
int binpow(int x, int step)
{
int res = 1;
while (step > 0)
{
if (step & 1) res = (ll) res * x % MOD;
x = (ll) x * x % MOD;
step /= 2;
}
return res;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int test;
cin >> test;
for (int rep = 1; rep <= test; rep++)
{
int n;
cin >> n;
if (n == 1) {cout << "0\n"; continue;}
for (int i = 0; i <= 29; i++) koef[i] = (1ll << i) * cnt_ones(0, n - 1, i);
segments.clear();
gener(0, n);
int rev_n = binpow(n, MOD - 2);
int rev_2 = binpow(2, MOD - 2);
int ans = (ll) (n - 1) * rev_2 % MOD;
int sum = 0;
for (int i = 0; i < (int) segments.size(); i++)
{
if (i & 1) continue;
for (int bit = 0; bit <= 29; bit++)
{
int s = (1ll << bit) * cnt_ones(0, n - 1, bit) % MOD;
s = (ll) s * (segments[i].y - segments[i].x + 1 - 2 * cnt_ones(segments[i].x, segments[i].y, bit)) % MOD;
sum = (sum + s) % MOD;
}
}
ans = (ans + (ll) sum * rev_n % MOD * rev_n) % MOD;
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3508kb
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: 1ms
memory: 3356kb
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: -100
Wrong Answer
time: 1ms
memory: 3420kb
input:
10 321 4212 2977 3438 679 2518 3359 2348 861 1853
output:
115992677 210903174 216412050 -217539554 956649209 -797440349 267658252 194263892 880330717 328032438
result:
wrong answer 4th numbers differ - expected: '780704799', found: '-217539554'