QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#281191#5464. Dice GameForever_YoungWA 2ms3952kbC++142.3kb2023-12-09 23:00:282023-12-09 23:00:28

Judging History

你现在查看的是最新测评结果

  • [2023-12-09 23:00:28]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3952kb
  • [2023-12-09 23:00:28]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;
using LL = long long;
int n;

// [0, x)
int calc(int x, int w) {
    int v1 = 1 << w;
    int rep = v1 * 2;
    int ret = x / rep * v1;
    int rem = x % rep;
    if (rem > v1) {
        ret += rem - v1;
    }
    return ret;
}

int calc2(int x) {
    int ret = 0;
    //printf("x = %d, n = %d\n",x,n);
    for (int w = 0; w < 30; w++) {
        int v1_1 = calc(x + 1, w);
        int v1_0 = x + 1 - v1_1;
        int v2_1 = calc(n, w);
        int v2_0 = n - v2_1;
        //printf("%d %d %d %d\n",v1_1,v1_0,v2_1,v2_0);
        ret = (ret + (1LL * v1_1 * v2_0 + 1LL * v1_0 * v2_1) % MOD * (1LL << w) % MOD) % MOD;
    }
    return ret;
}

// a \in [l, r], b \in [0, n), \sum(a ^ b)
int getsum(int l, int r) {
    return (calc2(r) - calc2(l - 1) + MOD) % MOD;
}

int quick_pow(int a, int x) {
    int ret = 1;
    while(x) {
        if (x & 1) {
            ret = 1LL * ret * a % MOD;
        }
        a = 1LL * a * a % MOD;
        x >>= 1;
    }
    return ret;
}

int seqsum(int l, int r) {
    return 1LL * (r + l) * (r - l + 1) / 2 % MOD;
}

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        int ans = calc2(0);
        for (int w = 0; w < 30; w++) {
            // hight bit = 2^w
            if ((1 << w) >= n) {
                break;
            }
            int L = (1 << w) - 1, R = (1 << (w + 1));
            if ((1 << (w + 1)) >= n) {
                R = n;
            }
            // need to xor
            auto check = [&](int x) {
                LL v1 = 1LL * x * n;
                LL v2 = getsum(x, x);
                return v2 >= v1;
            };
            while(L < R - 1) {
                int mid = (L + R) >> 1;
                if (check(mid)) {
                    L = mid;
                } else {
                    R = mid;
                }
            }
            // printf("w = %d, L = %d, R = %d\n", w, L, R);
            ans = (ans + 1LL * seqsum(R, min((1 << (w + 1)) - 1, n - 1)) * n) % MOD;
            ans = (ans + getsum(1 << w, L)) % MOD;
            // printf("ans = %d\n", ans);
        }
        ans = 1LL * ans * quick_pow(n, MOD - 2) % MOD;
        ans = 1LL * ans * quick_pow(n, MOD - 2) % MOD;
        printf("%d\n", ans);
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3952kb

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: 2ms
memory: 3848kb

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'