#include #include #include #include #include using namespace std; using uint = unsigned int; using ll = long long; #define TYPE_OF( VAR ) remove_const::type >::type #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define CEXPR( LL , BOUND , VALUE ) constexpr const LL BOUND = VALUE #define CIN( LL , A ) LL A; cin >> A #define ASSERT( A , MIN , MAX ) assert( MIN <= A && A <= MAX ) #define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define QUIT return 0 #define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; QUIT #define POWER( ANSWER , ARGUMENT , EXPONENT ) \ TYPE_OF( ARGUMENT ) ANSWER{ 1 }; \ { \ TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #include #define MAIN main inline CEXPR( uint , bound_L , 100 ); class Polynomial { public: vector m_f; static ll g_M; static uint g_L; static ll g_B; inline Polynomial() : m_f( g_L ) {}; inline Polynomial( const ll& c ) : m_f( g_L ) { m_f[0] = c; }; inline Polynomial( const Polynomial& g ) : m_f( g.m_f ) {}; inline Polynomial& operator*=( const Polynomial& g ); }; ll Polynomial::g_M = 1; uint Polynomial::g_L = 1; ll Polynomial::g_B = 1; inline Polynomial& Polynomial::operator*=( const Polynomial& g ) { vector answer( g_L * 2 ); FOR( i , 0 , g_L ){ const ll& fi = m_f[i]; FOR( j , 0 , g_L ){ ( answer[i + j] += fi * g.m_f[j] ) %= g_B; if( answer[i + j] < 0 ){ cout << "here" << endl; } } } FOR( k , 0 , g_L ){ ( answer[k] += answer[ k + g_L ] * g_M ) %= g_B; if( answer[k] < 0 ){ cout << "here" << endl; } } m_f = move( answer ); return *this; } inline Polynomial operator*( const Polynomial& f , const Polynomial& g ) { return Polynomial( f ).operator*=( g ); } int MAIN() { UNTIE; CEXPR( ll , bound_N , 1000000000000000000 ); CIN_ASSERT( N , 1 , bound_N ); CIN_ASSERT( M , 1 , bound_N ); CEXPR( uint , bound_L , 1000 ); CIN_ASSERT( L , 1 , bound_L ); Polynomial::g_L = L; CIN_ASSERT( K , 0 , L - 1 ); CEXPR( ll , bound_B , 1000000000 ); CIN_ASSERT( B , 1 , bound_B ); Polynomial::g_B = B; Polynomial::g_M = M % B; Polynomial f{}; f.m_f[0] = 1; if( L == 1 ){ f.m_f[0] += Polynomial::g_M; } else { f.m_f[1] = 1; } POWER( answer , f , N ); RETURN( answer.m_f[K] ); }