QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#709348#4056. 进制转换propane100 ✓1172ms178596kbC++204.3kb2024-11-04 14:12:212024-11-04 14:12:22

Judging History

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

  • [2024-11-04 14:12:22]
  • 评测
  • 测评结果:100
  • 用时:1172ms
  • 内存:178596kb
  • [2024-11-04 14:12:21]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#include<cassert>
using namespace std;
using LL = long long;

const int mod = 998244353;

void add(int &a, int b){
    a += b;
    if (a >= mod) a -= mod;
}

int mul(int a, int b){
    return 1LL * a * b % mod;
}

const int N = 3162278;
struct FastPow{
    int pow1[N + 1], pow2[N + 1];

    void init(int x) {
        pow1[0] = pow2[0] = 1;
        for(int i = 1; i <= N; i++){
            pow1[i] = mul(pow1[i - 1], x);
        }
        for(int i = 1; i <= N; i++){
            pow2[i] = mul(pow2[i - 1], pow1[N]);
        }
    }

    int operator[](LL x){
        return mul(pow1[x % N], pow2[x / N]);
    }

}powx;

int powy[70], powz[70];
LL pow3[70];
int pc3[5000005];
int a[70], len;
vector<int> f[15][2];

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    LL n; int x, y, z;
    cin >> n >> x >> y >> z;
    powx.init(x);
    powy[0] = powz[0] = 1;
    pow3[0] = 1;
    for(int i = 1; i <= 30; i++){
        pow3[i] = pow3[i - 1] * 3;
    }
    for(int i = 1; i <= 60; i++){
        powy[i] = mul(powy[i - 1], y);
        powz[i] = mul(powz[i - 1], z);
    }
    LL bk = n;
    while(n){
        a[++len] = n % 3;
        n /= 3;
    }

    auto dfs1 = [&](auto &&dfs1, int u, int val, int s) -> void {
        if (u == len / 2){
            pc3[val] = s;
            return;
        }
        for(int i = 0; i < 3; i++){
            dfs1(dfs1, u + 1, val * 3 + i, s + i);
        }
    };

    dfs1(dfs1, 0, 0, 0);

    for(int i = 0; i <= len / 2; i++){
        for(int j = 0; j < 2; j++){
            f[i][j].resize(1 << (i + 9));
        }
    }

    for(int val = 0; val < f[0][0].size(); val++){
        f[0][0][val] = powy[__builtin_popcount(val)];
    }
    for(int u = 1; u <= len / 2; u++){
        const int bit = u + 9;
        const int mask = (1 << (bit - 1)) - 1;
        for(int carry = 0; carry < 2; carry++){
            for(int val = 0; val < f[u][carry].size(); val++){
                int sum = 0;
                for(int i = 0; i < 3; i++){
                    int nval = val + i * pow3[u - 1];
                    if (nval >= (1 << bit)){
                        if (carry){
                            int cur_bit = (nval >> (bit - 1) & 1);
                            for(int j = 0; cur_bit + j < 2; j++){
                                add(sum, mul(mul(powy[cur_bit + j], powz[i]), mul(powx[i * pow3[u - 1]], f[u - 1][j][nval & mask])));
                            }
                        }
                    }
                    else{
                        int cur_bit = (nval >> (bit - 1) & 1);
                        for(int j = 0; j < 2; j++){
                            if ((j + cur_bit) / 2 == carry){
                                add(sum, mul(mul(powy[(cur_bit + j) % 2], powz[i]), mul(powx[i * pow3[u - 1]], f[u - 1][j][nval & mask])));
                            }
                        }
                    }
                }
                f[u][carry][val] = sum;
            }
        }
    }

    int ans = 0;

    auto dfs2 = [&](auto &&dfs2, int u, bool lim, int val, int s) -> void {
        if (u == len / 2){
            if (lim){
                int remain = bk - val * pow3[len / 2];
                for(int i = 0; i <= remain; i++){
                    LL cur = val * pow3[len / 2] + i;
                    add(ans, mul(mul(powx[cur], powz[s + pc3[i]]), powy[__builtin_popcountll(cur)]));
                }
                return;
            }
            const int mask = (1 << (u + 9)) - 1;
            for(int i = 0; i < 2; i++){
                LL nval = val * pow3[len / 2] + (i << (u + 9));
                int t = f[len / 2][i][nval & mask];
                add(ans, mul(mul(powx[val * pow3[len / 2]], powy[__builtin_popcountll(nval >> (u + 9))]), mul(powz[s], t)));
            }
            return;
        }
        int up = lim ? a[u] : 2;
        for(int i = 0; i <= up; i++){
            dfs2(dfs2, u - 1, lim and (i == up), val * 3 + i, s + i);
        }
    };

    dfs2(dfs2, len, true, 0, 0);
    cout << (ans - 1 + mod) % mod << '\n';

}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 34ms
memory: 30736kb

input:

9134097 778012792 263448561 839843856

output:

887680205

result:

ok 1 number(s): "887680205"

Test #2:

score: 5
Accepted
time: 32ms
memory: 30244kb

input:

9896386 2948513 263418583 271155379

output:

853292631

result:

ok 1 number(s): "853292631"

Test #3:

score: 5
Accepted
time: 36ms
memory: 29400kb

input:

9150910 827328107 842171962 39947937

output:

534921610

result:

ok 1 number(s): "534921610"

Test #4:

score: 5
Accepted
time: 1172ms
memory: 178480kb

input:

9586674634211 1 1 58301262

output:

13306748

result:

ok 1 number(s): "13306748"

Test #5:

score: 5
Accepted
time: 995ms
memory: 178480kb

input:

9774917720998 1 1 609549524

output:

825025220

result:

ok 1 number(s): "825025220"

Test #6:

score: 5
Accepted
time: 1019ms
memory: 178396kb

input:

9765239207265 422503033 1 719749187

output:

993518920

result:

ok 1 number(s): "993518920"

Test #7:

score: 5
Accepted
time: 991ms
memory: 178396kb

input:

9732354736444 277693641 1 501293609

output:

77844778

result:

ok 1 number(s): "77844778"

Test #8:

score: 5
Accepted
time: 89ms
memory: 37808kb

input:

9004409828 377918953 449219487 26422407

output:

110868569

result:

ok 1 number(s): "110868569"

Test #9:

score: 5
Accepted
time: 88ms
memory: 38352kb

input:

9579878149 820453354 218842704 133154415

output:

727248713

result:

ok 1 number(s): "727248713"

Test #10:

score: 5
Accepted
time: 85ms
memory: 36588kb

input:

9475807443 305433821 391589421 170059051

output:

372839725

result:

ok 1 number(s): "372839725"

Test #11:

score: 5
Accepted
time: 282ms
memory: 63676kb

input:

484758270277 372146623 410538257 35340632

output:

575284574

result:

ok 1 number(s): "575284574"

Test #12:

score: 5
Accepted
time: 276ms
memory: 65860kb

input:

473458173541 864158404 220259603 529747800

output:

610487662

result:

ok 1 number(s): "610487662"

Test #13:

score: 5
Accepted
time: 288ms
memory: 63724kb

input:

459992983903 359742981 983942229 552405867

output:

81366483

result:

ok 1 number(s): "81366483"

Test #14:

score: 5
Accepted
time: 292ms
memory: 65932kb

input:

462331701308 665849375 563297194 141092054

output:

774987426

result:

ok 1 number(s): "774987426"

Test #15:

score: 5
Accepted
time: 1021ms
memory: 178400kb

input:

9061635042931 746632077 302662913 559990819

output:

274721229

result:

ok 1 number(s): "274721229"

Test #16:

score: 5
Accepted
time: 1011ms
memory: 178516kb

input:

9653325901537 559549569 638292572 474780356

output:

418906016

result:

ok 1 number(s): "418906016"

Test #17:

score: 5
Accepted
time: 980ms
memory: 178380kb

input:

9640271229478 619740479 644097590 907038757

output:

936646026

result:

ok 1 number(s): "936646026"

Test #18:

score: 5
Accepted
time: 1009ms
memory: 178388kb

input:

9781711161203 988850684 154464719 995932763

output:

166841144

result:

ok 1 number(s): "166841144"

Test #19:

score: 5
Accepted
time: 1009ms
memory: 178596kb

input:

9156325004698 915375188 316066096 217969045

output:

306877956

result:

ok 1 number(s): "306877956"

Test #20:

score: 5
Accepted
time: 976ms
memory: 178596kb

input:

9042535293051 906265264 156788435 622201740

output:

397975092

result:

ok 1 number(s): "397975092"