#include #include #include #include #include #include using namespace std; using ll = long long; // 自分のライブラリ(https://github.com/p-adic/cpp)よりソースコードをコピーして編集している。 template using VLArray = list; template INT Residue( const INT& M , const INT& n ) noexcept; template INT Residue( const INT& M , const INT& n ) noexcept { if( M == 0 ){ return 0; } const INT M_abs = ( M > 0 ? M : -M ); if( n < 0 ){ const INT n_abs = -n; const INT res = n_abs % M_abs; return res == 0 ? res : M_abs - res; } return n % M_abs; } using INT_TYPE_FOR_ADIC_INT = long long int; template class AdicInt { private: VLArray m_expansion; INT_TYPE_FOR_ADIC_INT m_n; public: inline AdicInt( const INT_TYPE_FOR_ADIC_INT& n ) noexcept; inline const VLArray& GetExpansion() const noexcept; inline const INT_TYPE_FOR_ADIC_INT& GetValue() const noexcept; static const VLArray& Expand( const INT_TYPE_FOR_ADIC_INT& n ) noexcept; }; template inline AdicInt::AdicInt( const INT_TYPE_FOR_ADIC_INT& n ) noexcept : m_expansion( Expand( n ) ) , m_n( n ) {} template inline const VLArray& AdicInt::GetExpansion() const noexcept { return m_expansion; } template inline const INT_TYPE_FOR_ADIC_INT& AdicInt::GetValue() const noexcept { return m_n; } template const VLArray& AdicInt::Expand( const INT_TYPE_FOR_ADIC_INT& n ) noexcept { static VLArray memory_n{}; static VLArray > memory_answer{}; if( P == 0 ){ // ダミー return memory_n; } auto itr_n = memory_n.begin() , end_n = memory_n.end(); auto itr_answer = memory_answer.begin(); while( itr_n != end_n ){ if( *itr_n == n ){ return *itr_answer; } itr_n++; itr_answer++; } INT_TYPE_FOR_ADIC_INT n_copy = n; VLArray answer{}; if( LENGTH == 0 ){ for( INT_TYPE_FOR_ADIC_INT i = 0 ; n_copy != 0 ; i++ ){ const INT_TYPE_FOR_ADIC_INT d = Residue( P , n_copy ); answer.push_back( d ); n_copy -= d; n_copy /= P; } } else { for( INT_TYPE_FOR_ADIC_INT i = 0 ; i < LENGTH && n_copy != 0 ; i++ ){ const INT_TYPE_FOR_ADIC_INT d = Residue( P , n_copy ); answer.push_back( d ); n_copy -= d; n_copy /= P; } } memory_n.push_back( n ); memory_answer.push_back( answer ); return memory_answer.back(); } // init * ( t ^ num ) template T Power( const T& t , const UINT& num , const T& init = 1 , const bool& for_right_multiplication = true , const string& method = "normal" ); template inline T PowerNormalMethod( const T& t , const UINT& num , const T& init = 1 , const bool& for_right_multiplication = true ); template T PowerBinaryMethod( const T& t , const UINT& num , const T& init = 1 , const bool& for_right_multiplication = true ); // 単なる2乗だが、T次第ではオーバーロードしてより高速なものに置き換える template inline T Square( const T& t ); // PowerBinaryMetodの呼び出しは部分特殊化ではなくオーバーロードで処理できるようにするためにPowerBinaryMethodとはしない。 template inline T Power( const T& t , const UINT& num , const T& init , const bool& for_right_multiplication , const string& method ) { return method == "binary" ? PowerBinaryMethod( t , num , init , for_right_multiplication ) : PowerNormalMethod( t , num , init , for_right_multiplication ); } template inline T PowerNormalMethod( const T& t , const UINT& num , const T& init , const bool& for_right_multiplication ) { return num == 0 ? init : ( for_right_multiplication ? PowerNormalMethod( t , num - 1 , init ) * t : t * PowerNormalMethod( t , num - 1 , init ) ); } template T PowerBinaryMethod( const T& t , const UINT& num , const T& init , const bool& for_right_multiplication ) { const VLArray& num_binary = AdicInt<2>::Expand( num ); T answer = init; T power = t; for( auto itr = num_binary.begin() , end = num_binary.end() ; itr != end ; itr++ ){ if( *itr == 1 ){ answer = for_right_multiplication ? answer * power : power * answer; } // 部分特殊化ではなくオーバーロードで処理できるようにするためにSquareとしない。 power = Square( power ); } return answer; } template inline T Square( const T& t ) { return t * t; } using INT_TYPE_FOR_MOD = long long int; // ここをtempate などにしてしまうとoperator+などを呼び出す際に型推論に失敗する。整数型を変えたい時はINT_TYPE_FOR_MODの型エイリアスを変更する。 template class Mod { protected: INT_TYPE_FOR_MOD m_n; INT_TYPE_FOR_MOD m_inv; public: inline Mod() noexcept; inline Mod( const INT_TYPE_FOR_MOD& n ) noexcept; inline Mod( const Mod& n ) noexcept; inline Mod& operator=( const INT_TYPE_FOR_MOD& n ) noexcept; Mod& operator=( const Mod& n ) noexcept; Mod& operator+=( const INT_TYPE_FOR_MOD& n ) noexcept; inline Mod& operator+=( const Mod& n ) noexcept; inline Mod& operator-=( const INT_TYPE_FOR_MOD& n ) noexcept; inline Mod& operator-=( const Mod& n ) noexcept; Mod& operator*=( const INT_TYPE_FOR_MOD& n ) noexcept; Mod& operator*=( const Mod& n ) noexcept; // INT_TYPE_FOR_MODでの割り算ではないことに注意 virtual Mod& operator/=( const INT_TYPE_FOR_MOD& n ); virtual Mod& operator/=( const Mod& n ); Mod& operator%=( const INT_TYPE_FOR_MOD& n ); inline Mod& operator%=( const Mod& n ); // 前置++/--を使うつもりがないので後置++/--と同じものとして定義する inline Mod& operator++() noexcept; inline Mod& operator++( int ) noexcept; inline Mod& operator--() noexcept; inline Mod& operator--( int ) noexcept; inline const INT_TYPE_FOR_MOD& Represent() const noexcept; void Invert() noexcept; bool CheckInvertible() noexcept; bool IsSmallerThan( const INT_TYPE_FOR_MOD& n ) const noexcept; bool IsBiggerThan( const INT_TYPE_FOR_MOD& n ) const noexcept; }; template inline bool operator==( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template inline bool operator==( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template inline bool operator==( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator==( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator!=( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template inline bool operator!=( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template inline bool operator!=( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator!=( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator<( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template inline bool operator<( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template inline bool operator<( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator<=( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template inline bool operator<=( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template inline bool operator<=( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator<=( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator>( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template inline bool operator>( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template inline bool operator>( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator>( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator>=( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template inline bool operator>=( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template inline bool operator>=( const Mod& n0 , const Mod& n1 ) noexcept; template inline bool operator>=( const Mod& n0 , const Mod& n1 ) noexcept; template Mod operator+( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template Mod operator+( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template Mod operator+( const Mod& n0 , const Mod& n1 ) noexcept; template inline Mod operator-( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template Mod operator-( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template Mod operator-( const Mod& n0 , const Mod& n1 ) noexcept; template Mod operator*( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept; template Mod operator*( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept; template Mod operator*( const Mod& n0 , const Mod& n1 ) noexcept; template Mod operator/( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ); template Mod operator/( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ); template Mod operator/( const Mod& n0 , const Mod& n1 ); template Mod operator%( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ); template inline Mod operator%( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ); template inline Mod operator%( const Mod& n0 , const Mod& n1 ); template Mod Inverse( const Mod& n ); template Mod Power( const Mod& n , const INT_TYPE_FOR_MOD& p , const string& method = "normal" ); template <> inline Mod<2> Power( const Mod<2>& n , const INT_TYPE_FOR_MOD& p , const string& method ); // M乗が1になるよう定義されていることに注意 template inline Mod Power( const Mod& n , const Mod& p , const string& method = "normal" ); template <> inline Mod<2> Power( const Mod<2>& n , const Mod<2>& p , const string& method ); // ../Power/a_Body.hppにて定義 template inline T Square( const T& t ); template <> inline Mod<2> Square >( const Mod<2>& t ); void LazyEvaluationOfModularInverse( const INT_TYPE_FOR_MOD& M , const INT_TYPE_FOR_MOD& n , INT_TYPE_FOR_MOD& m ); template inline Mod::Mod() noexcept : m_n( 0 ) , m_inv( M ){} template inline Mod::Mod( const INT_TYPE_FOR_MOD& n ) noexcept : m_n( Residue( M , n ) ) , m_inv( 0 ){} template inline Mod::Mod( const Mod& n ) noexcept : m_n( n.m_n ) , m_inv( 0 ){} template inline Mod& Mod::operator=( const INT_TYPE_FOR_MOD& n ) noexcept { return operator=( Mod( n ) ); } template Mod& Mod::operator=( const Mod& n ) noexcept { m_n = n.m_n; m_inv = n.m_inv; return *this; } template Mod& Mod::operator+=( const INT_TYPE_FOR_MOD& n ) noexcept { m_n = Residue( M , m_n + n ); m_inv = 0; return *this; } template inline Mod& Mod::operator+=( const Mod& n ) noexcept { return operator+=( n.m_n ); }; template inline Mod& Mod::operator-=( const INT_TYPE_FOR_MOD& n ) noexcept { return operator+=( -n ); } template inline Mod& Mod::operator-=( const Mod& n ) noexcept { return operator-=( n.m_n ); } template Mod& Mod::operator*=( const INT_TYPE_FOR_MOD& n ) noexcept { m_n = Residue( M , m_n * n ); m_inv = 0; return *this; } template Mod& Mod::operator*=( const Mod& n ) noexcept { m_n = Residue( M , m_n * n.m_n ); if( m_inv == 0 || n.m_inv == 0 ){ m_inv = 0; } else if( m_inv == M || n.m_inv == M ){ m_inv = M; } else { Residue( M , m_inv * n.m_inv ); } return *this; } // 仮想関数なのでinline指定しない。 template Mod& Mod::operator/=( const INT_TYPE_FOR_MOD& n ) { return operator/=( Mod( n ) ); } template Mod& Mod::operator/=( const Mod& n ) { return operator*=( Inverse( n ) ); } template Mod& Mod::operator%=( const INT_TYPE_FOR_MOD& n ) { m_n %= Residue( M , n ); m_inv = 0; return *this; } template inline Mod& Mod::operator%=( const Mod& n ) { return operator%=( n.m_n ); } template inline Mod& Mod::operator++() noexcept { return operator+=( 1 ); } template inline Mod& Mod::operator++( int ) noexcept { return operator++(); } template inline Mod& Mod::operator--() noexcept { return operator-=( 1 ); } template inline Mod& Mod::operator--( int ) noexcept { return operator-=(); } template inline const INT_TYPE_FOR_MOD& Mod::Represent() const noexcept { return m_n; } template void Mod::Invert() noexcept { if( CheckInvertible() ){ INT_TYPE_FOR_MOD i = m_inv; m_inv = m_n; m_n = i; } else { // m_nがMになるのはここの処理に限るのでRepresent()の値を参照することで例外処理可能 m_n = M; m_inv = M; } return; } template bool Mod::CheckInvertible() noexcept { if( m_inv == 0 ){ LazyEvaluationOfModularInverse( M , m_n , m_inv ); } return m_inv != M; } template inline bool Mod::IsSmallerThan( const INT_TYPE_FOR_MOD& n ) const noexcept { return m_n < Residue( M , n ); } template inline bool Mod::IsBiggerThan( const INT_TYPE_FOR_MOD& n ) const noexcept { return m_n > Residue( M , n ); } template inline bool operator==( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { return n0 == Mod( n1 ); } template inline bool operator==( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return Mod( n0 ) == n0; } template inline bool operator==( const Mod& n0 , const Mod& n1 ) noexcept { return n0.Represent() == n1.Represent(); } template inline bool operator!=( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { return !( n0 == n1 ); } template inline bool operator!=( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return !( n0 == n1 ); } template inline bool operator!=( const Mod& n0 , const Mod& n1 ) noexcept { return !( n0 == n1 ); } template inline bool operator<( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { return n0.IsSmallerThan( n1 ); } template inline bool operator<( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return n1.IsBiggerThan( n0 ); } template inline bool operator<( const Mod& n0 , const Mod& n1 ) noexcept { return n0.Represent() < n1.Represent(); } template inline bool operator<=( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { return !( n1 < n0 ); } template inline bool operator<=( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return !( n1 < n0 ); } template inline bool operator<=( const Mod& n0 , const Mod& n1 ) noexcept { return !( n1 < n0 ); } template inline bool operator>( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { return n1 < n0; } template inline bool operator>( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return n1 < n0; } template inline bool operator>( const Mod& n0 , const Mod& n1 ) noexcept { return n1 < n0; } template inline bool operator>=( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { return !( n0 < n1 ); } template inline bool operator>=( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return !( n0 < n1 ); } template inline bool operator>=( const Mod& n0 , const Mod& n1 ) noexcept { return !( n0 < n1 ); } template Mod operator+( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { auto n = n0; n += n1; return n; } template inline Mod operator+( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return n1 + n0; } template inline Mod operator+( const Mod& n0 , const Mod& n1 ) noexcept { return n0 + n1.Represent(); } template inline Mod operator-( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { return n0 + ( -n1 ); } template inline Mod operator-( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return Mod( n0 - n1.Represent() ); } template inline Mod operator-( const Mod& n0 , const Mod& n1 ) noexcept { return n0 - n1.Represent(); } template Mod operator*( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) noexcept { auto n = n0; n *= n1; return n; } template inline Mod operator*( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) noexcept { return n1 * n0; } template Mod operator*( const Mod& n0 , const Mod& n1 ) noexcept { auto n = n0; n *= n1; return n; } template inline Mod operator/( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) { return n0 / Mod( n1 ); } template inline Mod operator/( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) { return Mod( n0 ) / n1; } template Mod operator/( const Mod& n0 , const Mod& n1 ) { auto n = n0; n /= n1; return n; } template Mod operator%( const Mod& n0 , const INT_TYPE_FOR_MOD& n1 ) { auto n = n0; n %= n1; return n; } template inline Mod operator%( const INT_TYPE_FOR_MOD& n0 , const Mod& n1 ) { return Mod( n0 ) % n1.Represent(); } template inline Mod operator%( const Mod& n0 , const Mod& n1 ) { return n0 % n1.Represent(); } template Mod Inverse( const Mod& n ) { auto n_copy = n; n_copy.Invert(); return n_copy; } template Mod Power( const Mod& n , const INT_TYPE_FOR_MOD& p , const string& method ) { if( p >= 0 ){ return Power,INT_TYPE_FOR_MOD>( n , p , 1 , true , true , method ); } return Inverse( Power( n , -p , method ) ); } template <> inline Mod<2> Power( const Mod<2>& n , const INT_TYPE_FOR_MOD& p , const string& method ) { return p == 0 ? 1 : n; } template inline Mod Power( const Mod& n , const Mod& p , const string& method ) { return Power,INT_TYPE_FOR_MOD>( n , p.Represent() , method ); } template <> inline Mod<2> Power( const Mod<2>& n , const Mod<2>& p , const string& method ) { return p == 0 ? 1 : n; } template <> inline Mod<2> Square >( const Mod<2>& t ) { return t; } void LazyEvaluationOfModularInverse( const INT_TYPE_FOR_MOD& M , const INT_TYPE_FOR_MOD& n , INT_TYPE_FOR_MOD& m ) { static VLArray memory_M{}; // vectorでなくVLArrayだと引数が小さい順に呼び出した時の計算量がO(1)からO(n)に跳ね上がってしまう。 static VLArray > memory_inverse{}; auto itr_M = memory_M.begin() , end_M = memory_M.end(); auto itr_inverse = memory_inverse.begin(); vector* p_inverse = nullptr; while( itr_M != end_M && p_inverse == nullptr ){ if( *itr_M == M ){ p_inverse = &( *itr_inverse ); } itr_M++; itr_inverse++; } if( p_inverse == nullptr ){ memory_M.push_front( M ); memory_inverse.push_front( vector() ); p_inverse = &( memory_inverse.front() ); p_inverse->push_back( M ); } const INT_TYPE_FOR_MOD size = p_inverse->size(); for( INT_TYPE_FOR_MOD i = size ; i <= n ; i++ ){ p_inverse->push_back( 0 ); } INT_TYPE_FOR_MOD& n_inv = ( *p_inverse )[n]; if( n_inv != 0 ){ m = n_inv; return; } const INT_TYPE_FOR_MOD M_abs = M >= 0 ? M : -M; const INT_TYPE_FOR_MOD n_sub = M_abs % n; INT_TYPE_FOR_MOD n_sub_inv = ( *p_inverse )[n_sub]; if( n_sub_inv == 0 ){ LazyEvaluationOfModularInverse( M , n_sub , n_sub_inv ); } if( n_sub_inv != M ){ n_inv = M_abs - ( ( n_sub_inv * ( M_abs / n ) ) % M_abs ); m = n_inv; return; } for( INT_TYPE_FOR_MOD i = 1 ; i < M_abs ; i++ ){ if( ( n * i ) % M_abs == 1 ){ n_inv = i; m = n_inv; return; } } n_inv = M; m = n_inv; return; } template using LineTypeForMatrix = vector; template using TableTypeForMatrix = LineTypeForMatrix >; using SizeTypeForMatrix = ll; template class Matrix { private: TableTypeForMatrix m_M; public: // argsの長さがXYでなくてもコンパイルエラーとならないがサポート外である。 template Matrix( const Args&... args ) noexcept; inline Matrix( const Matrix& mat ) noexcept; // ( X , Y )行列でないものも引数に取れるがサポート外である。 template inline Matrix( const TableTypeForMatrix& M ) noexcept; Matrix& operator=( const Matrix& mat ) noexcept; Matrix& operator+=( const Matrix& mat ); Matrix& operator-=( const Matrix& mat ); Matrix& operator*=( const T& scalar ) noexcept; // 行や列の長さを変更可能だがサポート外である。 inline TableTypeForMatrix& RefTable() noexcept; inline const TableTypeForMatrix& GetTable() const noexcept; static inline const Matrix& Unit() noexcept; private: static inline void ConstructTable( TableTypeForMatrix& M , LineTypeForMatrix& vec ) noexcept; template static void ConstructTable( TableTypeForMatrix& M , LineTypeForMatrix& vec , const Arg& arg , const Args&... args ) noexcept; static Matrix Unit_Body() noexcept; }; template inline Matrix operator==( const Matrix& mat1 , const Matrix& mat2 ) noexcept; template inline Matrix operator!=( const Matrix& mat1 , const Matrix& mat2 ) noexcept; template Matrix operator+( const Matrix& mat1 , const Matrix& mat2 ); template Matrix operator-( const Matrix& mat1 , const Matrix& mat2 ); template Matrix operator*( const Matrix& mat1 , const Matrix& mat2 ); template Matrix operator*( const T& scalar , const Matrix& mat ); template Matrix Transpose( const Matrix& mat ); template T Trace( const Matrix& mat ); // ../Arithmetic/Power/a_Body.hppにて定義 // template // T PowerBinaryMethod( const T& t , const UINT& num , const T& init , const bool& for_right_multiplication ); template Matrix<2,2,T> PowerBinaryMethod( const Matrix<2,2,T>& mat , const UINT& num , const Matrix<2,2,T>& init_dummy , const bool& for_right_multiplication_dummy ); template class TwoByTwoMatrix { private: T m_M00; T m_M01; T m_M10; T m_M11; public: inline TwoByTwoMatrix( const T& M00 , const T& M01 , const T& M10 , const T& M11 ) noexcept; TwoByTwoMatrix( const Matrix<2,2,T>& mat ); TwoByTwoMatrix& operator=( const TwoByTwoMatrix& mat ) noexcept; inline Matrix<2,2,T> GetMatrix22() const noexcept; static inline TwoByTwoMatrix Multiply( const TwoByTwoMatrix& mat1 , const TwoByTwoMatrix& mat2 ); static inline TwoByTwoMatrix Square( const TwoByTwoMatrix& mat ); }; template inline TwoByTwoMatrix operator*( const TwoByTwoMatrix& mat1 , const TwoByTwoMatrix& mat2 ); // ../../Arithmetic/Power/a_Body.hppにて定義 // template inline T Square( const T& t ); template inline TwoByTwoMatrix Square( const TwoByTwoMatrix& mat ); template template Matrix::Matrix( const Args&... args ) noexcept : m_M() { TableTypeForMatrix M{}; LineTypeForMatrix vec{}; ConstructTable( M , vec , args... ); m_M = M; } template inline Matrix::Matrix( const Matrix& mat ) noexcept : m_M( mat.m_M ) {} template template inline Matrix::Matrix( const TableTypeForMatrix& M ) noexcept : m_M( M ) {} template Matrix& Matrix::operator=( const Matrix& mat ) noexcept { m_M = mat.m_M; return *this; } template Matrix& Matrix::operator+=( const Matrix& mat ) { auto itr1y = m_M.begin() , end1y = m_M.end(); auto itr2y = mat.m_M.begin(); while( itr1y != end1y ){ auto itr1xy = itr1y->begin() , end1xy = itr1y->end(); auto itr2xy = itr2y->begin(); while( itr1xy != end1xy ){ *itr1xy += *itr2xy; itr1xy++; itr2xy++; } itr1y++; itr2y++; } return *this; } template Matrix& Matrix::operator-=( const Matrix& mat ) { auto itr1y = m_M.begin() , end1y = m_M.end(); auto itr2y = mat.m_M.begin(); while( itr1y != end1y ){ auto itr1xy = itr1y->begin() , end1xy = itr1y->end(); auto itr2xy = itr2y->begin(); while( itr1xy != end1xy ){ *itr1xy -= *itr2xy; itr1xy++; itr2xy++; } itr1y++; itr2y++; } return *this; } template Matrix& Matrix::operator*=( const T& scalar ) noexcept { for( auto itry = m_M.begin() , endy = m_M.end() ; itry != endy ; itry++ ){ for( auto itrxy = itry->begin() , endxy = itry->end() ; itrxy != endxy ; itrxy++ ){ *itrxy *= scalar; } } return *this; } template inline TableTypeForMatrix& Matrix::RefTable() noexcept { return m_M; } template inline const TableTypeForMatrix& Matrix::GetTable() const noexcept { return m_M; } template inline const Matrix& Matrix::Unit() noexcept { static const Matrix unit = Unit_Body(); return unit; } template Matrix Matrix::Unit_Body() noexcept { TableTypeForMatrix M{}; for( SizeTypeForMatrix y = 0 ; y < Y ; y++ ){ LineTypeForMatrix vec{}; for( SizeTypeForMatrix x = 0 ; x < X ; x++ ){ vec.push_back( x == y ? 1 : 0 ); } M.push_back( vec ); } return Matrix( M ); } template inline void Matrix::ConstructTable( TableTypeForMatrix& M , LineTypeForMatrix& vec ) noexcept { M.push_back( vec ); vec.clear(); } template template void Matrix::ConstructTable( TableTypeForMatrix& M , LineTypeForMatrix& vec , const Arg& arg , const Args&... args ) noexcept { vec.push_back( arg ); if( vec.size() == X ){ ConstructTable( M , vec ); } if( M.size() < Y ){ ConstructTable( M , vec , args... ); } return; } template inline Matrix operator==( const Matrix& mat1 , const Matrix& mat2 ) noexcept { return mat1.GetTable() == mat2.GetTable(); } template inline Matrix operator!=( const Matrix& mat1 , const Matrix& mat2 ) noexcept { return !( mat1 == mat2 ); } template Matrix operator+( const Matrix& mat1 , const Matrix& mat2 ) { Matrix mat1_copy = mat1; mat1_copy += mat2; return mat1_copy; } template Matrix operator-( const Matrix& mat1 , const Matrix& mat2 ) { Matrix mat1_copy = mat1; mat1_copy -= mat2; return mat1_copy; } template inline Matrix operator*( const Matrix& mat1 , const Matrix& mat2 ) { const TableTypeForMatrix& M1 = mat1.GetTable(); const TableTypeForMatrix& M2 = mat2.GetTable(); TableTypeForMatrix M_prod{}; auto begin2x = M2.begin(); for( auto itr1y = M1.begin() , end1y = M1.end() ; itr1y != end1y ; itr1y++ ){ LineTypeForMatrix vec{}; auto begin1yx = itr1y->begin() , end1yx = itr1y->end(); for( SizeTypeForMatrix z = 0 ; z < Z ; z++ ){ auto itr1yx = begin1yx; auto itr2x = begin2x; T inner_product = 0; while( itr1yx != end1yx ){ inner_product += ( *itr1yx ) * ( *itr2x )[z]; itr1yx++; itr2x++; } vec.push_back( inner_product ); } M_prod.push_back( vec ); } return Matrix( M_prod ); } template Matrix operator*( const T& scalar , const Matrix& mat ) { Matrix mat_copy = mat; mat_copy *= scalar; return mat_copy; } template Matrix Transpose( const Matrix& mat ) { const TableTypeForMatrix& M = mat.GetTable(); TableTypeForMatrix M_t{}; auto beginy = M.begin(); for( auto itr1x = beginy->begin() , end1x = beginy->end() ; itr1x != end1x ; itr1x++ ){ M_t.push_back( LineTypeForMatrix() ); } for( auto itry = beginy , endy = M.end() ; itry != endy ; itry++ ){ auto itryx = itry->begin() , endyx = itry->end(); auto itr_ty = M_t.begin(); while( itryx != endyx ){ itr_ty->push_back( *itryx ); itryx++; itr_ty++; } } return Matrix( M_t ); } template T Trace( const Matrix& mat ) { int i = 0; T answer =0; const TableTypeForMatrix& M = mat.GetTable(); for( auto itry = M.begin() , endy = M.end() ; itry != endy ; itry++ ){ answer += ( *itry )[i]; i++; } return answer; } template inline Matrix<2,2,T> PowerBinaryMethod( const Matrix<2,2,T>& mat , const UINT& num , const Matrix<2,2,T>& init_dummy , const bool& for_right_multiplication_dummy ) { return PowerBinaryMethod( TwoByTwoMatrix( mat ) , num , TwoByTwoMatrix( init_dummy ) , for_right_multiplication_dummy ).GetMatrix22(); } template inline TwoByTwoMatrix::TwoByTwoMatrix( const T& M00 , const T& M01 , const T& M10 , const T& M11 ) noexcept : m_M00( M00 ) , m_M01( M01 ) , m_M10( M10 ) , m_M11( M11 ) {} template TwoByTwoMatrix::TwoByTwoMatrix( const Matrix<2,2,T>& mat ) : m_M00() , m_M01() , m_M10() , m_M11() { const TableTypeForMatrix& M = mat.GetTable(); const LineTypeForMatrix& M0 = M[0]; const LineTypeForMatrix& M1 = M[1]; m_M00 = M0[0]; m_M01 = M0[1]; m_M10 = M1[0]; m_M11 = M1[1]; } template TwoByTwoMatrix& TwoByTwoMatrix::operator=( const TwoByTwoMatrix& mat ) noexcept { if( &mat != this ){ m_M00 = mat.m_M00; m_M01 = mat.m_M01; m_M10 = mat.m_M10; m_M11 = mat.m_M11; } return *this; } template inline Matrix<2,2,T> TwoByTwoMatrix::GetMatrix22() const noexcept { return Matrix<2,2,T>( m_M00 , m_M01 , m_M10 , m_M11 ); } template inline TwoByTwoMatrix TwoByTwoMatrix::Multiply( const TwoByTwoMatrix& mat1 , const TwoByTwoMatrix& mat2 ) { return TwoByTwoMatrix( mat1.m_M00 * mat2.m_M00 + mat1.m_M01 * mat2.m_M10 , mat1.m_M00 * mat2.m_M01 + mat1.m_M01 * mat2.m_M11 , mat1.m_M10 * mat2.m_M00 + mat1.m_M11 * mat2.m_M10 , mat1.m_M10 * mat2.m_M01 + mat1.m_M11 * mat2.m_M11 ); } template inline TwoByTwoMatrix TwoByTwoMatrix::Square( const TwoByTwoMatrix& mat ) { return TwoByTwoMatrix( mat.m_M00 * mat.m_M00 + mat.m_M01 * mat.m_M10 , ( mat.m_M00 + mat.m_M11 ) * mat.m_M01 , mat.m_M10 * ( mat.m_M00 + mat.m_M11 ) , mat.m_M10 * mat.m_M01 + mat.m_M11 * mat.m_M11 ); } template inline TwoByTwoMatrix operator*( const TwoByTwoMatrix& mat1 , const TwoByTwoMatrix& mat2 ) { return TwoByTwoMatrix::Multiply( mat1 , mat2 ); } template inline TwoByTwoMatrix Square( const TwoByTwoMatrix& mat ) { return TwoByTwoMatrix::Square( mat ); } #define MODULO 10000 using MOD = Mod< MODULO >; void Solve(); int main() { Solve(); return 0; } void Shuffle( MOD& n , const ll& power ); void ShuffleMemorise( MOD& n , const ll& power , const ll& I ); void Solve() { string S; ll P; ll I; cin >> S; cin >> P; cin >> I; MOD answer{ stoll( S ) }; if( P == 0 ){ if( I != 0 ){ answer = 0; } } else { // 素数pと正整数dに対するGL( 2 , p ^ d )の位数 // = ( p ^ d - p ^ ( d - 1 ) ) ^ 2 * ( p ^ d ) ^ 2 + p ^ ( d - 1 ) * ( p ^ d - p ^ ( d - 1 ) ) ^ 2 * p ^ d // = p ^ ( 4 * d - 2 ) * ( p - 1 ) ^ 2 + p ^ ( 4 * d - 3 ) * ( p - 1 ) ^ 2 // = p ^ ( 4 * d - 3 ) * ( p - 1 ) ^ 2 * ( p + 1 ) // GL( 2 , 2 ^ 4 )の位数 = 2 ^ ( 4 * 4 - 3 ) * ( 2 - 1 ) ^ 2 * ( 2 + 1 ) = 2 ^ 13 * 3 // GL( 2 , 5 ^ 4 )の位数 = 5 ^ ( 4 * 4 - 3 ) * ( 5 - 1 ) ^ 2 * ( 5 + 1 ) = 5 ^ 13 * 4 ^ 2 * 6 = 2 ^ 5 * 3 * 5 ^ 13 // lcm( GL( 2 , 2 ^ 4 )の位数 , GL( 2 , 5 ^ 4 )の位数 ) = 2 ^ 13 * 3 * 5 ^ 13 = 3 * 10 ^ 13; const ll power = ( P - 1 ) % 30000000000000; if( I < MODULO ){ for( ll i = 0 ; i < I ; i++ ){ Shuffle( answer , power ); } } else { ShuffleMemorise( answer , power , I ); } } const string answer_str = to_string( MODULO + answer.Represent() ).substr( 1 ); cout << answer_str << endl; return; } void Shuffle( MOD& n , const ll& power ) { if( n == 0 ){ return; } static const Matrix<1,2,MOD> A{ 1 , 0 }; const Matrix<2,2,MOD> B { n , 1 , 1 , 0 }; static const Matrix<2,1,MOD> C{ 1 , 0 }; static const Matrix<2,2,MOD> E { 1 , 0 , 0 , 1 }; static const string method = "binary"; MOD answer = Trace<1,MOD>( A * Power,ll>( B , power , E , true , method ) * C ); if( power % 2 == 0 ){ answer -= 1; } n = answer; return; } void ShuffleMemorise( MOD& n , const ll& power , const ll& I ) { VLArray answer_memory{}; for( ll i = 0 ; i < MODULO ; i++ ){ answer_memory.push_back( n ); Shuffle( n , power ); } const MOD& last_input = answer_memory.back(); answer_memory.push_back( n ); bool not_found = true; ll num; auto itr = answer_memory.begin(); for( ll i = 0 ; i <= MODULO && not_found ; i++ ){ if( *itr == last_input ){ not_found = false; num = i; } else { itr++; } } const ll period = MODULO - num - 1; const ll d = ( I - num ) % period; for( ll i = 0 ; i < d ; i++ ){ itr++; } n = *itr; return; }