結果

問題 No.3399 One Two Three Two Three
コンテスト
ユーザー Shlok Goyal
提出日時 2026-11-26 13:40:56
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 33 ms / 4,000 ms
+ 566µs
コード長 6,309 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,418 ms
コンパイル使用メモリ 372,240 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2026-11-26 13:41:07
合計ジャッジ時間 7,207 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <cassert>
using namespace std;

constexpr int MOD = 998244353;
using i64 = long long;
using u64 = unsigned long long;

int normalize(i64 value) {
    value %= MOD;
    if (value < 0) value += MOD;
    return int(value);
}
int modPower(int a, int exponent) {
    int result = 1;
    for (; exponent; exponent >>= 1, a = i64(a) * a % MOD)
        if (exponent & 1) result = i64(result) * a % MOD;
    return result;
}

// Arithmetic modulo z^3+z^2+z-1: z^3=1-z-z^2 and z^4=2z-1.
struct Element {
    int a = 0, b = 0, c = 0;
};
Element operator+(Element x, Element y) {
    int a = x.a + y.a, b = x.b + y.b, c = x.c + y.c;
    if (a >= MOD) a -= MOD;
    if (b >= MOD) b -= MOD;
    if (c >= MOD) c -= MOD;
    return {a, b, c};
}
Element operator-(Element x, Element y) {
    int a = x.a - y.a, b = x.b - y.b, c = x.c - y.c;
    if (a < 0) a += MOD;
    if (b < 0) b += MOD;
    if (c < 0) c += MOD;
    return {a, b, c};
}
Element operator*(Element x, Element y) {
    i64 t0 = i64(x.a) * y.a;
    i64 t1 = i64(x.a) * y.b + i64(x.b) * y.a;
    i64 t2 = i64(x.a) * y.c + i64(x.b) * y.b + i64(x.c) * y.a;
    i64 t3 = i64(x.b) * y.c + i64(x.c) * y.b;
    i64 t4 = i64(x.c) * y.c;
    return {normalize(t0 + t3 - t4), normalize(t1 - t3 + 2 * t4), normalize(t2 - t3)};
}
Element square(Element x) {
    i64 t0 = i64(x.a) * x.a;
    i64 t1 = 2LL * x.a * x.b;
    i64 t2 = 2LL * x.a * x.c + i64(x.b) * x.b;
    i64 t3 = 2LL * x.b * x.c;
    i64 t4 = i64(x.c) * x.c;
    return {normalize(t0 + t3 - t4), normalize(t1 - t3 + 2 * t4), normalize(t2 - t3)};
}
Element inverse(Element x) {
    int a = x.a, b = x.b, c = x.c;
    int q0 = normalize((i64(a) - c) * (a - b) - (2LL * c - b) * (b - c));
    int q1 = normalize(i64(c) * (2LL * c - b) - i64(b) * (a - b));
    int q2 = normalize(i64(b) * (b - c) - i64(c) * (a - c));
    int determinant = normalize(i64(a) * q0 + i64(c) * q1 + (i64(b) - c) * q2);
    // All requested denominators are units for this modulus and N <= 10^18.
    assert(determinant != 0);
    int inv = determinant == 1 ? 1 : modPower(determinant, MOD - 2);
    return {int(i64(q0) * inv % MOD), int(i64(q1) * inv % MOD), int(i64(q2) * inv % MOD)};
}
Element evaluate(Element coefficients, Element z, Element z2) {
    return {normalize(coefficients.a + i64(coefficients.b) * z.a + i64(coefficients.c) * z2.a),
            normalize(i64(coefficients.b) * z.b + i64(coefficients.c) * z2.b),
            normalize(i64(coefficients.b) * z.c + i64(coefficients.c) * z2.c)};
}

struct Scale {
    u64 value;
    int parent = -1, factor = 0, twice = -1, thrice = -1;
    Element z, z2, inverseDenominator;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    u64 N;
    cin >> N;

    vector<u64> divisors;
    for (u64 two = 1; two <= N; two *= 2)
        for (u64 d = two; d <= N; d *= 3) divisors.push_back(d);
    sort(divisors.begin(), divisors.end());
    int count = int(divisors.size());
    auto scaleIndex = [&](u64 d) {
        return int(lower_bound(divisors.begin(), divisors.end(), d) - divisors.begin());
    };
    const Element ONE{1, 0, 0}, Z{0, 1, 0};
    vector<Scale> scales(count);
    for (int j = 0; j < count; ++j) {
        Scale& scale = scales[j];
        scale.value = divisors[j];
        if (j == 0) {
            scale.z = Z;
        } else {
            scale.factor = divisors[j] % 2 == 0 ? 2 : 3;
            scale.parent = scaleIndex(divisors[j] / scale.factor);
            Element parent = scales[scale.parent].z;
            scale.z = square(parent);
            if (scale.factor == 3) scale.z = scale.z * parent;
        }
        scale.z2 = square(scale.z);
        if (j) scale.inverseDenominator = inverse(ONE - scale.z - scale.z2 - scale.z2 * scale.z);
        if (divisors[j] <= N / 2) scale.twice = scaleIndex(divisors[j] * 2);
        if (divisors[j] <= N / 3) scale.thrice = scaleIndex(divisors[j] * 3);
    }

    vector<u64> sizes;
    for (u64 d : divisors) sizes.push_back(N / d);
    sort(sizes.begin(), sizes.end());
    sizes.erase(unique(sizes.begin(), sizes.end()), sizes.end());
    int rows = int(sizes.size());
    vector<int> offset(rows + 1), length(rows), half(rows), third(rows);
    auto rowIndex = [&](u64 size) {
        return size == 0 ? -1 : int(lower_bound(sizes.begin(), sizes.end(), size) - sizes.begin());
    };
    for (int i = 0; i < rows; ++i) {
        length[i] = int(upper_bound(divisors.begin(), divisors.end(), N / sizes[i]) - divisors.begin());
        offset[i + 1] = offset[i] + length[i];
        half[i] = rowIndex(sizes[i] / 2);
        third[i] = rowIndex(sizes[i] / 3);
    }
    // prefix[offset[i]+j] = sum_(k=1)^sizes[i] f(k) z^(divisors[j]*k).
    // Scale 1 is singular and is never used as a prefix-sum state.
    vector<Element> prefix(offset.back());
    array<Element, 64> binaryPower;
    binaryPower[0] = Z;
    for (int i = 1; i < 64; ++i) binaryPower[i] = square(binaryPower[i - 1]);
    auto powerZ = [&](u64 exponent) {
        Element result = ONE;
        for (int bit = 0; exponent; exponent >>= 1, ++bit)
            if (exponent & 1) result = result * binaryPower[bit];
        return result;
    };
    auto get = [&](int row, int scale) -> Element {
        if (row < 0) return {};
        assert(0 <= scale && scale < length[row]);
        return prefix[offset[row] + scale];
    };

    int answer = 0;
    for (int i = 0; i < rows; ++i) {
        vector<Element> powers(length[i]);
        powers[0] = powerZ(sizes[i] + 1);
        Element boundary = inverse(powers[0]) *
            (Z + get(half[i], scales[0].twice) + get(third[i], scales[0].thrice));
        // boundary = (f(n)+f(n-1)+f(n-2)) +
        //            (f(n)+f(n-1))*z + f(n)*z^2.
        answer = boundary.c;
        for (int j = 1; j < length[i]; ++j) {
            const Scale& scale = scales[j];
            Element parentPower = powers[scale.parent];
            powers[j] = square(parentPower);
            if (scale.factor == 3) powers[j] = powers[j] * parentPower;
            Element numerator = scale.z + get(half[i], scale.twice) + get(third[i], scale.thrice)
                - powers[j] * evaluate(boundary, scale.z, scale.z2);
            prefix[offset[i] + j] = numerator * scale.inverseDenominator;
        }
    }
    cout << answer << '\n';
}
0