結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー gosou513gosou513
提出日時 2024-02-15 22:57:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 12,523 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 176,888 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-09-28 19:07:47
合計ジャッジ時間 2,337 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 1 ms
6,820 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 1 ms
6,816 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 1 ms
6,816 KB
testcase_11 AC 1 ms
6,816 KB
testcase_12 AC 1 ms
6,820 KB
testcase_13 AC 2 ms
6,816 KB
testcase_14 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair< int, int >;
using pll = pair< ll, ll >;
template< class T > using vc = vector< T >;
template< class T > using vvc = vector< vector< T > >;
template< class T > using vvvc = vector< vector< vector< T > > >;
template< class T > using vvvvc = vector< vector< vector< vector< T > > > >;
template< class T > using pq = priority_queue< T >;
template< class T > using pqg = priority_queue< T, vector< T >, greater< T > >;
template< class T > using mset = multiset< T >;
template< class T > using uset = unordered_set< T >;
template< class T, class U > using umap = unordered_map< T, U >;
template< class T > struct Edge {
    int from, to;
    T cost;
    int id;
    Edge( int from_, int to_, T cost_ = 1, int id_ = -1 ) : from( from_ ), to( to_ ), cost( cost_ ), id( id_ ) {}
};
template< class T > struct Graph {
    vector< vector< Edge< T > > > G;
    int e;
    bool is_directed;

    Graph() {}
    Graph( int n, bool is_directed_ = false ) : G( n ), e( 0 ), is_directed( is_directed_ ) {}
    int size() const {
        return G.size();
    }
    void add_edge( int from, int to, T cost = 1 ) {
        G[ from ].emplace_back( from, to, cost, e );
        if ( !is_directed ) G[ to ].emplace_back( to, from, cost, e );
        e++;
    }
    void add_edge( int from, int to, T cost, int id ) {
        G[ from ].emplace_back( from, to, cost, id );
        if ( !is_directed ) G[ to ].emplace_back( to, from, cost, id );
        e++;
    }
    inline vector< Edge< T > > &operator[]( int i ) {
        return G[ i ];
    }
};
template< class T > using Edges = vector< Edge< T > >;

#define vec( type, name, ... ) vector< type > name( __VA_ARGS__ )
#define vv( type, name, h, ... ) vector name( h, vector< type >( __VA_ARGS__ ) )
#define vvv( type, name, h, w, ... ) vector name( h, vector( w, vector< type >( __VA_ARGS__ ) ) )
#define vvvv( type, name, a, b, c, ... ) vector< vector< vector< vector< type > > > > name( a, vector< vector< vector< type > > >( b, vector< vector< type > >( c, vector< type >( __VA_ARGS__ ) ) ) )

const int INF = 0x3fffffff;
const ll LINF = 0x1fffffffffffffff;
const ld DINF = numeric_limits< ld >::infinity();
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932;
const ll dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
const ll dy[] = { 1, 0, -1, 0, 1, 1, -1, -1 };

#define overload5( a, b, c, d, e, name, ... ) name
#define overload4( a, b, c, d, name, ... ) name
#define overload3( a, b, c, name, ... ) name
#define rep0( n ) for ( ll qwerty = 0; qwerty < n; qwerty++ )
#define rep1( i, n ) for ( ll i = 0; i < ( n ); i++ )
#define rep2( i, a, b ) for ( ll i = ( a ); i < ( b ); i++ )
#define rep3( i, a, b, c ) for ( ll i = ( a ); i < ( b ); i += ( c ) )
#define rep( ... ) overload4( __VA_ARGS__, rep3, rep2, rep1, rep0 )( __VA_ARGS__ )
#define rrep0( n ) for ( ll qwerty = (n)-1; qwerty >= 0; qwerty-- )
#define rrep1( i, n ) for ( ll i = (n)-1; i >= 0; i-- )
#define rrep2( i, a, b ) for ( ll i = (a)-1; i >= ( b ); i-- )
#define rrep3( i, a, b, c ) for ( ll i = (a)-1; i >= ( b ); i -= ( c ) )
#define rrep( ... ) overload4( __VA_ARGS__, rrep3, rrep2, rrep1, rrep0 )( __VA_ARGS__ )
#define each1( i, a ) for ( auto &&i : a )
#define each2( x, y, a ) for ( auto &&[ x, y ] : a )
#define each3( x, y, z, a ) for ( auto &&[ x, y, z ] : a )
#define each4( w, x, y, z, a ) for ( auto &&[ w, x, y, z ] : a )
#define each( ... ) overload5( __VA_ARGS__, each4, each3, each2, each1 )( __VA_ARGS__ )
#define all1( i ) begin( i ), end( i )
#define all2( i, a ) begin( i ), begin( i ) + a
#define all3( i, a, b ) begin( i ) + a, begin( i ) + b
#define all( ... ) overload3( __VA_ARGS__, all3, all2, all1 )( __VA_ARGS__ )
#define rall1( i ) rbegin( i ), rend( i )
#define rall2( i, a ) rbegin( i ), rbegin( i ) + a
#define rall3( i, a, b ) rbegin( i ) + a, rbegin( i ) + b
#define rall( ... ) overload3( __VA_ARGS__, rall3, rall2, rall1 )( __VA_ARGS__ )

#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define Endl '\n'
#define ifnot( x ) if ( !( x ) )
#define YesNo( x ) cout << ( x ? "Yes" : "No" ) << endl;

#define bit( i ) ( 1LL << i )
#define getbit( x, i ) ( ( ( x ) >> ( i ) ) & 1 )
#define topbit( x ) ( ( x ) == 0 ? -1 : 63 - __builtin_clzll( x ) )
#define lowbit( x ) ( ( x ) == 0 ? 64 : __builtin_ctzll( x ) )
#define popcount( x ) __builtin_popcountll( x )
#define mask( i ) ( ( 1LL << ( i ) ) - 1 )

template< class T > vector< T > &operator++( vector< T > &v ) {
    for ( auto e : v ) e++;
    return v;
}
template< class T > vector< T > operator++( vector< T > &v, int ) {
    auto ret = v;
    for ( auto e : v ) e++;
    return ret;
}
template< class T > vector< T > &operator--( vector< T > &v ) {
    for ( auto e : v ) e--;
    return v;
}
template< class T > vector< T > operator--( vector< T > &v, int ) {
    auto ret = v;
    for ( auto e : v ) e--;
    return ret;
}

#define INT( ... )   \
    int __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define INTd( ... )  \
    int __VA_ARGS__; \
    IN2( __VA_ARGS__ )
#define LL( ... )   \
    ll __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define LLd( ... )  \
    ll __VA_ARGS__; \
    IN2( __VA_ARGS__ )
#define STR( ... )      \
    string __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define CHR( ... )    \
    char __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define DBL( ... )      \
    double __VA_ARGS__; \
    IN( __VA_ARGS__ )
#define VEC( type, name, size )  \
    vector< type > name( size ); \
    IN( name )
#define VECd( type, name, size ) \
    vector< type > name( size ); \
    IN2( name )
#define VEC2( type, name1, name2, size )         \
    vector< type > name1( size ), name2( size ); \
    for ( int i = 0; i < size; i++ ) IN( name1[ i ], name2[ i ] )
#define VEC2d( type, name1, name2, size )        \
    vector< type > name1( size ), name2( size ); \
    for ( int i = 0; i < size; i++ ) IN2( name1[ i ], name2[ i ] )
#define VEC3( type, name1, name2, name3, size )                 \
    vector< type > name1( size ), name2( size ), name3( size ); \
    for ( int i = 0; i < size; i++ ) IN( name1[ i ], name2[ i ], name3[ i ] )
#define VEC3d( type, name1, name2, name3, size )                \
    vector< type > name1( size ), name2( size ), name3( size ); \
    for ( int i = 0; i < size; i++ ) IN2( name1[ i ], name2[ i ], name3[ i ] )
#define VEC4( type, name1, name2, name3, name4, size )                         \
    vector< type > name1( size ), name2( size ), name3( size ), name4( size ); \
    for ( int i = 0; i < size; i++ ) IN( name1[ i ], name2[ i ], name3[ i ], name4[ i ] );
#define VEC4d( type, name1, name2, name3, name4, size )                        \
    vector< type > name1( size ), name2( size ), name3( size ), name4( size ); \
    for ( int i = 0; i < size; i++ ) IN2( name1[ i ], name2[ i ], name3[ i ], name4[ i ] );
#define VV( type, name, h, w )                               \
    vector< vector< type > > name( h, vector< type >( w ) ); \
    IN( name )
#define VVd( type, name, h, w )                              \
    vector< vector< type > > name( h, vector< type >( w ) ); \
    IN2( name )
int scan() {
    return getchar();
}
void scan( int &a ) {
    cin >> a;
}
void scan( long long &a ) {
    cin >> a;
}
void scan( char &a ) {
    cin >> a;
}
void scan( double &a ) {
    cin >> a;
}
void scan( string &a ) {
    cin >> a;
}
template< class T, class S > void scan( pair< T, S > &p ) {
    scan( p.first ), scan( p.second );
}
template< class T > void scan( vector< T > & );
template< class T > void scan( vector< T > &a ) {
    for ( auto &i : a ) scan( i );
}
template< class T > void scan( T &a ) {
    cin >> a;
}
void IN() {}
void IN2() {}
template< class Head, class... Tail > void IN( Head &head, Tail &...tail ) {
    scan( head );
    IN( tail... );
}
template< class Head, class... Tail > void IN2( Head &head, Tail &...tail ) {
    scan( head );
    --head;
    IN2( tail... );
}

template< class T, class S > bool inc( const T &x, const S &l, const S &r ) {
    return l <= x && x < r;
}
template< typename T > int si( const T &x ) {
    return x.size();
}
template< class T, class S > inline bool chmax( T &a, const S &b ) {
    return ( a < b ? a = b, 1 : 0 );
}
template< class T, class S > inline bool chmin( T &a, const S &b ) {
    return ( a > b ? a = b, 1 : 0 );
}
vector< int > iota( int n, int s = 0 ) {
    vector< int > ret( n );
    iota( ret.begin(), ret.end(), s );
    return ret;
}
template< class T > void sort( T &v ) {
    sort( v.begin(), v.end() );
}
template< class T > void rsort( T &v ) {
    sort( v.rbegin(), v.rend() );
}
template< class T > void reverse( T &v ) {
    reverse( v.begin(), v.end() );
}
template< class T > auto max( const T &a ) {
    return *max_element( a.begin(), a.end() );
}
template< class T > auto min( const T &a ) {
    return *min_element( a.begin(), a.end() );
}
template< class T > int max_id( const T &a ) {
    return max_element( a.begin(), a.end() ) - a.begin();
}
template< class T > int min_id( const T &a ) {
    return min_element( a.begin(), a.end() ) - a.begin();
}
template< class T, class S > int lb( vector< T > &v, S a ) {
    return lower_bound( v.begin(), v.end(), (T)a ) - v.begin();
}
template< class T, class S > int ub( vector< T > &v, S a ) {
    return upper_bound( v.begin(), v.end(), (T)a ) - v.begin();
}
template< typename T = ll, typename S > T sum( const S &v ) {
    return accumulate( v.begin(), v.end(), T( 0 ) );
}
template< class T > vector< T > uniq( vector< T > v ) {
    sort( v.begin(), v.end() );
    v.erase( unique( v.begin(), v.end() ), v.end() );
    return v;
}

long long max( int x, long long y ) {
    return max( (long long)x, y );
}
long long max( long long x, int y ) {
    return max( x, (long long)y );
}
long long min( int x, long long y ) {
    return min( (long long)x, y );
}
long long min( long long x, int y ) {
    return min( x, (long long)y );
}
template< class T, class S > T floor( T x, S y ) {
    if ( y < 0 ) x = -x, y = -y;
    return ( x >= 0 ? x / y : ( x - y + 1 ) / y );
}
template< class T, class S > T ceil( T x, S y ) {
    if ( y < 0 ) x = -x, y = -y;
    return ( x >= 0 ? ( x + y - 1 ) / y : x / y );
}

template< class T > class Matrix {
    int h, w;
    int mmod;
    vector< vector< T > > mat;

   public:
    Matrix() {}
    Matrix( int h ) {
        Matrix( h, h, 0 );
    }
    Matrix( int h, int w, int mmod, T val = 0 ) : h( h ), w( w ), mmod( mmod ), mat( h, vector< T >( w, val ) ) {}
    template< class S > Matrix( vector< vector< S > > &a ) {
        h = a.size();
        w = a[ 0 ].size();
        mat = vector< vector< T > >( h, vector< T >( w, 0 ) );
        for ( int i = 0; i < h; i++ ) {
            for ( int j = 0; j < w; j++ ) mat[ i ][ j ] = (T)a[ i ][ j ];
        }
    }
    Matrix &unit() {
        assert( h == w );
        for ( int i = 0; i < h; i++ ) mat[ i ][ i ] = 1;
        return *this;
    }

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

    Matrix operator*( const Matrix &a ) const {
        assert( w == a.h );
        Matrix ret( h, a.w, mmod );
        for ( int i = 0; i < h; i++ ) {
            for ( int k = 0; k < w; k++ ) {
                for ( int j = 0; j < a.w; j++ ) {
                    ret[ i ][ j ] += mat[ i ][ k ] * a.mat[ k ][ j ] % mmod;
                    ret[ i ][ j ] %= mmod;
                }
            }
        }
        return ret;
    }
    Matrix operator+( const Matrix &a ) const {
        assert( h == a.h && w == a.w );
        Matrix ret( h, w );
        for ( int i = 0; i < h; i++ ) {
            for ( int j = 0; j < w; j++ ) {
                ret[ i ][ j ] = mat[ i ][ j ] + a.mat[ i ][ j ];
                ret[ i ][ j ] %= mmod;
            }
        }
        return ret;
    }
    Matrix pow( long long t ) const {
        assert( h == w );
        if ( t == 0 ) return Matrix( h, h, mmod ).unit();
        if ( t == 1 ) return *this;
        Matrix ret = pow( t >> 1 );
        ret = ret * ret;
        if ( t & 1 ) {
            ret = ret * ( *this );
        }
        return ret;
    }
};

int main() {
    cin.tie( 0 );
    ios::sync_with_stdio( false );
    cout << fixed << setprecision( 15 );

    INT( N, M );

    Matrix< ll > m( 2, 2, M, 0 );
    m[ 0 ][ 0 ] = 1;
    m[ 0 ][ 1 ] = 1;
    m[ 1 ][ 0 ] = 1;
    m = m.pow( N - 2 );
    Matrix< ll > f0( 2, 1, M, 0 );
    f0[ 0 ][ 0 ] = 1;
    auto fN = m * f0;

    cout << fN[ 0 ][ 0 ] << endl;
}
0