結果

問題 No.3182 recurrence relation’s intersection sum
ユーザー Kude
提出日時 2025-06-13 22:50:20
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 7,030 bytes
コンパイル時間 3,641 ms
コンパイル使用メモリ 307,552 KB
実行使用メモリ 12,104 KB
最終ジャッジ日時 2025-06-13 22:50:28
合計ジャッジ時間 7,535 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:49:6: warning: ‘{anonymous}::mint {anonymous}::perm(int, int)’ defined but not used [-Wunused-function]
   49 | mint perm(int n, int k) {
      |      ^~~~
main.cpp:48:6: warning: ‘{anonymous}::mint {anonymous}::fact(int)’ defined but not used [-Wunused-function]
   48 | mint fact(int n) {return Fact[n];}
      |      ^~~~
main.cpp:44:6: warning: ‘{anonymous}::mint {anonymous}::icomb(int, int)’ defined but not used [-Wunused-function]
   44 | mint icomb(int n, int k) {
      |      ^~~~~
main.cpp:37:6: warning: ‘{anonymous}::mint {anonymous}::comb(int, int)’ defined but not used [-Wunused-function]
   37 | mint comb(int n, int k) {
      |      ^~~~

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

constexpr int FACT_SIZE = 1000000;
mint Fact[FACT_SIZE + 1];
mint iFact[FACT_SIZE + 1];
const auto fact_init = [] {
    Fact[0] = mint::raw(1);
    for(int i = 1; i <= FACT_SIZE; ++i) {
        Fact[i] = Fact[i-1] * i;
    }
    iFact[FACT_SIZE] = Fact[FACT_SIZE].inv();
    for(int i = FACT_SIZE; i; --i) {
        iFact[i-1] = iFact[i] * i;
    }
    return false;
}();

mint comb(int n, int k) {
    if (k == 0) return mint::raw(1);
    assert(n >= 0 && k >= 0);
    if (k > n) return mint::raw(0);
    return Fact[n] * iFact[n - k] * iFact[k];
}

mint icomb(int n, int k) {
    return iFact[n] * Fact[n - k] * Fact[k];
}

mint fact(int n) {return Fact[n];}
mint perm(int n, int k) {
    assert(0 <= n);
    return Fact[n] * iFact[n - k];
}



template<class T, int N>
struct Mat: std::array<std::array<T, N>, N> {
    friend Mat<T, N> operator*(const Mat& A, const Mat& B) {
        Mat<T, N> C = {};
        for(int i = 0; i < N; i++) {
            for(int k = 0; k < N; k++) {
                for(int j = 0; j < N; j++) {
                    C[i][j] += A[i][k] * B[k][j];
                }
            }
        }
        return C;
    }
    friend std::array<T, N> operator*(const Mat& A, const std::array<T, N>& v) {
        std::array<T, N> x = {};
        for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) x[i] += A[i][j] * v[j];
        return x;
    }
    friend Mat<T, N> operator+(const Mat& A, const Mat& B) {
        Mat<T, N> C;
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                C[i][j] = A[i][j] + B[i][j];
            }
        }
        return C;
    }
    Mat<T, N>& operator*=(const Mat& A) { return *this = *this * A; }
    Mat<T, N>& operator+=(const Mat& A) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                (*this)[i][j] += A[i][j];
            }
        }
    }
    static Mat<T, N> I() {
        Mat<T, N> X = {};
        for(int i = 0; i < N; i++) X[i][i] = 1;
        return X;
    }
    Mat<T, N> pow(long long k) const {
        assert(k >= 0);
        auto X = *this;
        auto Y = I();
        while(k) {
            if (k & 1) Y *= X;
            k >>= 1;
            if (k) X *= X;
        }
        return Y;
    }
    Mat<T, N> inv() const {
        auto X = *this;
        auto Y = I();
        for(int p = 0; p < N; p++) {
            bool ok = false;
            for(int i = p; i < N; i++) {
                if (X[i][p] != T()) {
                    ok = true;
                    if (i != p) {
                        // std::swap(X[i], X[p]);
                        for(int j = p; j < N; j++) std::swap(X[i][j], X[p][j]);
                        std::swap(Y[i], Y[p]);
                    }
                    break;
                }
            }
            assert(ok);
            T c = 1 / X[p][p];
            for(int j = p; j < N; j++) X[p][j] *= c;
            for(int j = 0; j < N; j++) Y[p][j] *= c;
            for(int i = 0; i < N; i++) if (i != p) {
                T c = X[i][p];
                for(int j = p; j < N; j++) X[i][j] -= c * X[p][j];
                for(int j = 0; j < N; j++) Y[i][j] -= c * Y[p][j];
            }
        }
        return Y;
    }

    T det() const {
        if (N == 0) return T();
        auto X = *this;
        bool flag = false;
        T res = 1;
        bool ok = false;
        for(int p = 0; p < N; p++) {
            for(int i = p; i < N; i++) {
                if (X[i][p] != T()) {
                    ok = true;
                    if (i != p) {
                        flag = !flag;
                        for(int j = p; j < N; j++) std::swap(X[i][j], X[p][j]);
                    }
                    break;
                }
            }
            if (!ok) return T();
            // T = modint
            for(int i = p + 1; i < N; i++) {
                T c0 = 1, c1 = 0, d0 = 0, d1 = 1;
                int a = X[p][p].val(), b = X[i][p].val();
                while(true) {
                    if (b == 0) {
                        break;
                    }
                    int q = a / b;
                    a -= q * b;
                    c0 -= q * d0;
                    c1 -= q * d1;
                    if (a == 0) {
                        flag = !flag;
                        for(int j = p; j < N; j++) std::swap(X[i][j], X[p][j]);
                        std::swap(c0, d1);
                        std::swap(c1, d0);
                        break;
                    }
                    q = b / a;
                    b -= q * a;
                    d0 -= q * c0;
                    d1 -= q * c1;
                }
                for(int j = p; j < N; j++) {
                    T a = X[p][j], b = X[i][j];
                    T x = c0 * a + c1 * b, y = d0 * a + d1 * b;
                    X[p][j] = x;
                    X[i][j] = y;
                }
            }
            res *= X[p][p];
        }
        if (flag) res = -res;
        return res;
    }
    friend std::ostream& operator<<(std::ostream& os, const Mat<T, N>& A) {
        for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) os << A[i][j] << " \n"[j + 1 == N];
        return os;
    }
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k;
  ll l, r;
  cin >> k >> l >> r;
  if (k == 1) {
    mint ans = mint(r - l + 1) * (mint(l) * l + mint(l) * r + l + mint(r) * r + 2 * r + 6) / 6;
    cout << ans.val() << endl;
    return 0;
  }
  constexpr int M = 220;
  static_assert(M % 2 == 0);
  vector<mint> a(M), s(M);
  a[0] = s[0] = 1;
  rep(n, M - 1) a[n+1] = k * a[n] + mint(n).pow(k) + mint(k).pow(n);
  rep(n, M - 1) s[n+1] = s[n] + a[n+1];
  using Mt = Mat<mint, M>;
  Mt A;
  vector<mint> b(M);
  rep(i, M) {
    rep(j, M / 2) A[i][j] = mint(i).pow(j);
    rep(j, M / 2) A[i][j+M/2] = mint(k).pow(i) * mint(i).pow(j);
    b[i] = s[i];
  }
  auto Ai = A.inv();
  vector<mint> c(M);
  rep(i, M) {
    mint v;
    rep(j, M) v += Ai[i][j] * b[j];
    c[i] = v;
  }
  l--;
  mint ans;
  rep(z, 2) {
    mint res;
    rep(j, M / 2) res += mint(r).pow(j) * c[j];
    rep(j, M / 2) res += mint(k).pow(r) * mint(r).pow(j) * c[j+M/2];
    ans += z == 0 ? res : -res;
    swap(l, r);
    if (r == -1) break;
  }
  cout << ans.val() << '\n';
}
0