Code Library Of Others/sgtlaugh/Berlekamp Massey + Matrix Expo.cpp
long long interpolate(vector <int>& recurrence, long long n){ /// hash = 377697
        int len = recurrence.size();
        if (n < len) return recurrence[n];

        vector <int> polynomial;
        int l = berlekamp_massey(recurrence, polynomial);
        while (l && polynomial[l] == 0){
            l--;
            polynomial.pop_back();
        }
        reverse(polynomial.begin(), polynomial.begin() + l + 1);

        struct Matrix mat = Matrix(l);
        for (int i = 1; i < mat.n; i++) mat.ar[i][i - 1] = 1;
        for (int i = 0; i < mat.n; i++) mat.ar[0][i] = MOD - polynomial[l - i - 1];

        long long res = 0;
        mat = mat ^ (n - len + 1);
        for (int i = 0; i < mat.n; i++) res = (res + (long long)mat.ar[0][i] * recurrence[len - i - 1]) % MOD;
        return res;
    }
/// find's the n'th term of the recurrence from a continuous sequence of recurrence values
/// recurrence and n both follow 0 based indexing
find's the n'th term of the recurrence from a continuous sequence of recurrence values
recurrence and n both follow 0 based indexing
find ' s the n ' th term of the recurrence from a continuous sequence of recurrence values recurrence and n both follow 0 based indexing
long long interpolate(vector <int>& recurrence, long long n){ 
        int len = recurrence.size();
        if (n < len) return recurrence[n];
        vector <int> polynomial;
        int l = berlekamp_massey(recurrence, polynomial);
        while (l && polynomial[l] == 0){
            l--;
            polynomial.pop_back();
        }
        reverse(polynomial.begin(), polynomial.begin() + l + 1);
        struct Matrix mat = Matrix(l);
        for (int i = 1; i < mat.n; i++) mat.ar[i][i - 1] = 1;
        for (int i = 0; i < mat.n; i++) mat.ar[0][i] = MOD - polynomial[l - i - 1];
        long long res = 0;
        mat = mat ^ (n - len + 1);
        for (int i = 0; i < mat.n; i++) res = (res + (long long)mat.ar[0][i] * recurrence[len - i - 1]) % MOD;
        return res;
    }
long long interpolate ( vector < int > & recurrence , long long n ) { int len = recurrence . size ( ) ; if ( n < len ) return recurrence [ n ] ; vector < int > polynomial ; int l = berlekamp_massey ( recurrence , polynomial ) ; while ( l && polynomial [ l ] == 0 ) { l -- ; polynomial . pop_back ( ) ; } reverse ( polynomial . begin ( ) , polynomial . begin ( ) + l + 1 ) ; struct Matrix mat = Matrix ( l ) ; for ( int i = 1 ; i < mat . n ; i ++ ) mat . ar [ i ] [ i - 1 ] = 1 ; for ( int i = 0 ; i < mat . n ; i ++ ) mat . ar [ 0 ] [ i ] = MOD - polynomial [ l - i - 1 ] ; long long res = 0 ; mat = mat ^ ( n - len + 1 ) ; for ( int i = 0 ; i < mat . n ; i ++ ) res = ( res + ( long long ) mat . ar [ 0 ] [ i ] * recurrence [ len - i - 1 ] ) % MOD ; return res ; }
find's the n'th term of the recurrence from a continuous sequence of recurrence values
recurrence and n both follow 0 based indexing
find ' s the n ' th term of the recurrence from a continuous sequence of recurrence values recurrence and n both follow 0 based indexing
/// hash = 377697