結果

問題 No.3182 recurrence relation’s intersection sum
ユーザー pitP
提出日時 2025-06-13 22:22:03
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 8,482 bytes
コンパイル時間 5,348 ms
コンパイル使用メモリ 335,808 KB
実行使用メモリ 8,672 KB
最終ジャッジ日時 2025-06-13 22:22:16
合計ジャッジ時間 12,652 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
istream &operator>>(istream &is, modint &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint &a) { return os << a.val(); }
istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); } 

typedef long long ll;
typedef vector<vector<int>> Graph;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define FOR(i,l,r) for (int i = l;i < (int)(r); i++)
#define rep(i,n) for (int i = 0;i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define my_sort(x) sort(x.begin(), x.end())
#define my_max(x) *max_element(all(x))
#define my_min(x) *min_element(all(x))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<62) - 1;
const double PI = acos(-1);
vector<int> di = {1,0,-1,0};
vector<int> dj = {0,1,0,-1};

#ifdef LOCAL
#  include <debug_print.hpp>
#  define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define debug(...) (static_cast<void>(0))
#endif

// https://youtu.be/ylWYSurx10A?t=2352
template<typename T>
struct Matrix{
    int h, w;
    vector<vector<T>> d;
    Matrix(): h(0), w(0) {}
    Matrix(int h, int w, T val=0): h(h), w(w), d(h, vector<T>(w,val)) {}
    Matrix(int n, T val=0): h(n), w(n), d(n, vector<T>(n,val)) {}

    Matrix& unit(){
        assert(h == w);
        rep(i,h) d[i][i] = 1;
        return *this;
    }

    const vector<T>& operator[](int i) const { return d[i];}
    vector<T>& operator[](int i) { return d[i];}

    Matrix operator+(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        Matrix r(h, w);
        rep(i,h)rep(j,w){
            r[i][j] = d[i][j] + a[i][j];
        }
        return r;
    }

    Matrix &operator+=(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        rep(i,h)rep(j,w){
            (*this)[i][j] += a[i][j];
        }
        return (*this);
    }

    Matrix operator-(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        Matrix r(h, w);
        rep(i,h)rep(j,w){
            r[i][j] = d[i][j] - a[i][j];
        }
        return r;
    }

    Matrix &operator-=(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        rep(i,h)rep(j,w){
            (*this)[i][j] -= a[i][j];
        }
        return (*this);
    }

    Matrix operator*(const Matrix& a) const {
        assert(w == a.h);
        Matrix r(h, a.w);
        rep(i,h)rep(k,w)rep(j,a.w){
            r[i][j] += d[i][k] * a[k][j];
        }
        return r;
    }

    Matrix &operator*=(const Matrix& a) const {
        assert(w == a.h);
        vector<vector<T>> nd(h, vector<T>(w));
        rep(i,h)rep(k,w)rep(j,a.w){
            nd[i][j] += (*this)[i][k] * a[k][j];
        }
        d = move(nd);
        return (*this);
    }

    vector<T> operator*(const vector<T> &a) const {
        // res[i] = sum{ M[i][j] * x[j] } (j = 0 ... h-1)
        assert(w == (int)a.size());
        vector<T> r(h);
        rep(i,h)rep(j,w){
            r[i] += (*this)[i][j] * a[j];
        }
        return r;
    }

    Matrix operator*(const T &a) const {
        Matrix r(h, w);
        rep(i,h)rep(j,w){
            r[i][j] = (*this)[i][j] * a;
        }
        return r;
    }

    Matrix &operator*=(const T &a) const {
        vector<vector<T>> nd(h, vector<T>(w));
        rep(i,h)rep(j,w){
            nd[i][j] = (*this)[i][j] * a;
        }
        d = move(nd);
        return (*this);
    }

    bool operator==(const Matrix &a){
        if(h != a.h || w != a.w) return false;
        rep(i,h)rep(j,w){
            if((*this)[i][j] != a[i][j]) return false;
        }
        return true;
    }

    friend ostream &operator<<(ostream &os, Matrix &a){
        rep(i,a.h){
            os << (i == 0 ? '[' : ' ');
            rep(j,a.w) os << (j == 0 ? '[' : ' ') << a[i][j] << (j == a.w - 1 ? "]" : ",");
            os << (i == a.h - 1 ? "]" : ",") << '\n';
        }
        return os;
    }


    pair<int, int> shape() {return {this->h, this->w};}

    static Matrix eye(int n){
        Matrix mat(n, n);
        mat.unit();
        return mat;
    }

    Matrix pow(long long t) const {
        debug(t);
        assert(h == w);
        if(t == 0) Matrix(h, h).unit();
        if(t == 1) return *this;
        Matrix r = pow(t >> 1);
        r = r * r;
        if(t & 1) r = r * (*this);
        return r;
    }

    T det(){
        assert(h == w);
        vector<vector<T>> dc = d;
        T res = 1;
        rep(k,h){
            for(int i = k; i < h; i++){
                if(dc[i][k] == 0) continue;
                if(i != k){
                    swap(dc[i], dc[k]);
                    res = -res;
                }
            }
            if(dc[k][k] == 0) return 0;
            res *= dc[k][k];
            T inv = T(1) / dc[k][k];
            rep(j,h) dc[k][j] *= inv;
            for(int i = k + 1; i < h; i++){
                T c = dc[i][k];
                for(int j = k; j < h; j++) dc[i][j] -= dc[k][j] * c;
            }
        }
        return res;
    }

    Matrix inv(){
        assert(h == w);
        int n = h;
        // (A E) -> (E, invA)
        Matrix AE(n, 2 * n);
        rep(i,n){
            rep(j,n) AE[i][j] = d[i][j];
            AE[i][i + n] = 1;
        }

        rep(r, n){
            int pivot = -1;
            for(int i = r; i < n; i++){
                if(AE[i][r] != 0){
                    pivot = i;
                    break;
                }
            }
            // 逆行列が存在しない
            if(pivot == -1) return Matrix();
            swap(AE[r], AE[pivot]);

            for(int j = r + 1; j < 2 * n; j++) AE[r][j] /= AE[r][r];
            rep(i,n)if(i != r){
                for(int j = r + 1; j < 2 * n; j++){
                    AE[i][j] -= AE[i][r] * AE[r][j];
                }
            }
        }

        Matrix r(n, n);
        rep(i,n)rep(j,n) r[i][j] = AE[i][j + n];
        return r;
    }

    // !! not verify !!
    vector<T> linear_equation(vector<T> b){
        Matrix invA = (*this).inv();
        if(invA.h == 0) return {};
        return invA * b;
    }
};

// Matrix<T> mat(h, w);
// Matrix<T> identity = Matrix<T>::eye(n);
// pii hw = mat.shape();
// T determinant = A.det();
// Matrix<T> powA = A.pow(K);
// Matrix<T> invA = A.pow(K);
// vector<T> x = A.linear_equation(b);

using mint = modint998244353;

//https://drken1215.hatenablog.com/entry/2018/06/08/210000
//COMinit()を忘れない!!!
const ll NMAX = 202020;
const ll MOD = 998244353;
//const int MOD = 1e9+7;

ll fac[NMAX],finv[NMAX],inv[NMAX];

void COMinit(){
    fac[0] = fac[1] = 1LL;
    finv[0] = finv[1] = 1LL;
    inv[1] = 1LL;
    for (int i=2;i<NMAX;i++){
        fac[i] = fac[i-1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
        finv[i] = finv[i-1] * inv[i] % MOD;
    }
}

ll nCr(int n,int k){
    if (n<k) return 0LL;
    if (n < 0 || k < 0) return 0LL;
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
}

ll nPr(int n,int k){
    if (n<k) return 0LL;
    if (n < 0 || k < 0) return 0LL;
    return fac[n] * finv[n-k] % MOD;
}

ll nHr(int n,int r){
    return nCr(n+r-1,r);
}


int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    COMinit();

    ll K, L, R;
    cin >> K >> L >> R;

    Matrix<mint> M(K + 4, K + 4);
    M[0][0] = 1;
    M[0][1] = 1;

    M[1][1] = K;
    M[1][2] = 1;
    M[1][3] = 1;

    M[2][2] = K;

    int row = 3;
    for(int x = K; x >= 0; x--){
        for(int i = 0; i <= x; i++){
            M[row][i + row] = nCr(x, i);
        }
        row++;
    }

    // debug(M.d);

    Matrix<mint> MR = M.pow(R + 1);
    Matrix<mint> ML = (L > 0 ? M.pow(L) : M.unit());

    debug(M.d, MR.d, ML.d);

    vector<mint> B(K + 4);
    B[0] = 0;
    B[1] = 1;
    B[2] = 1;
    B[K + 3] = 1;

    mint Sl = 0;
    rep(i, K + 4) Sl += ML[0][i] * B[i];

    mint Sr = 0;
    rep(i, K + 4) Sr += MR[0][i] * B[i];

    cout << Sr - Sl + (L == 0) << endl;
}
0