QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#709311#4056. 进制转换propane100 ✓1654ms178624kbC++204.0kb2024-11-04 13:55:502024-11-04 13:55:51

Judging History

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

  • [2024-11-04 13:55:51]
  • 评测
  • 测评结果:100
  • 用时:1654ms
  • 内存:178624kb
  • [2024-11-04 13:55:50]
  • 提交

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 dfs(int u, bool carry, int val){
    if (u == 0){
        if (carry) return 0;
        return powy[__builtin_popcount(val)];
    }
    if (f[u][carry][val] != -1){
        return f[u][carry][val];
    }
    int bit = u + 9;
    const int mask = (1 << (bit - 1)) - 1;
    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]], dfs(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]], dfs(u - 1, j, nval & mask))));
                }
            }
        }
    }
    return f[u][carry][val] = sum;
}

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 == 14){
            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 = 1; i <= len / 2; i++){
        for(int j = 0; j < 2; j++){
            f[i][j].assign(1 << (i + 9), -1);
        }
    }

    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 = dfs(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: 43ms
memory: 48392kb

input:

9134097 778012792 263448561 839843856

output:

887680205

result:

ok 1 number(s): "887680205"

Test #2:

score: 5
Accepted
time: 38ms
memory: 48600kb

input:

9896386 2948513 263418583 271155379

output:

853292631

result:

ok 1 number(s): "853292631"

Test #3:

score: 5
Accepted
time: 35ms
memory: 48476kb

input:

9150910 827328107 842171962 39947937

output:

534921610

result:

ok 1 number(s): "534921610"

Test #4:

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

input:

9586674634211 1 1 58301262

output:

13306748

result:

ok 1 number(s): "13306748"

Test #5:

score: 5
Accepted
time: 1625ms
memory: 178572kb

input:

9774917720998 1 1 609549524

output:

825025220

result:

ok 1 number(s): "825025220"

Test #6:

score: 5
Accepted
time: 1638ms
memory: 178448kb

input:

9765239207265 422503033 1 719749187

output:

993518920

result:

ok 1 number(s): "993518920"

Test #7:

score: 5
Accepted
time: 1632ms
memory: 178456kb

input:

9732354736444 277693641 1 501293609

output:

77844778

result:

ok 1 number(s): "77844778"

Test #8:

score: 5
Accepted
time: 109ms
memory: 55460kb

input:

9004409828 377918953 449219487 26422407

output:

110868569

result:

ok 1 number(s): "110868569"

Test #9:

score: 5
Accepted
time: 115ms
memory: 55792kb

input:

9579878149 820453354 218842704 133154415

output:

727248713

result:

ok 1 number(s): "727248713"

Test #10:

score: 5
Accepted
time: 111ms
memory: 55552kb

input:

9475807443 305433821 391589421 170059051

output:

372839725

result:

ok 1 number(s): "372839725"

Test #11:

score: 5
Accepted
time: 461ms
memory: 80336kb

input:

484758270277 372146623 410538257 35340632

output:

575284574

result:

ok 1 number(s): "575284574"

Test #12:

score: 5
Accepted
time: 475ms
memory: 80328kb

input:

473458173541 864158404 220259603 529747800

output:

610487662

result:

ok 1 number(s): "610487662"

Test #13:

score: 5
Accepted
time: 468ms
memory: 80224kb

input:

459992983903 359742981 983942229 552405867

output:

81366483

result:

ok 1 number(s): "81366483"

Test #14:

score: 5
Accepted
time: 482ms
memory: 80200kb

input:

462331701308 665849375 563297194 141092054

output:

774987426

result:

ok 1 number(s): "774987426"

Test #15:

score: 5
Accepted
time: 1583ms
memory: 178584kb

input:

9061635042931 746632077 302662913 559990819

output:

274721229

result:

ok 1 number(s): "274721229"

Test #16:

score: 5
Accepted
time: 1654ms
memory: 178424kb

input:

9653325901537 559549569 638292572 474780356

output:

418906016

result:

ok 1 number(s): "418906016"

Test #17:

score: 5
Accepted
time: 1600ms
memory: 178440kb

input:

9640271229478 619740479 644097590 907038757

output:

936646026

result:

ok 1 number(s): "936646026"

Test #18:

score: 5
Accepted
time: 1638ms
memory: 178372kb

input:

9781711161203 988850684 154464719 995932763

output:

166841144

result:

ok 1 number(s): "166841144"

Test #19:

score: 5
Accepted
time: 1597ms
memory: 178448kb

input:

9156325004698 915375188 316066096 217969045

output:

306877956

result:

ok 1 number(s): "306877956"

Test #20:

score: 5
Accepted
time: 1577ms
memory: 178624kb

input:

9042535293051 906265264 156788435 622201740

output:

397975092

result:

ok 1 number(s): "397975092"