#include #include #include #include #include using namespace std; using ll = long long; #define MODULO 10000 void Solve(); int main() { Solve(); return 0; } list AdicExp( ll& power ); void Shuffle( ll& n , const list& adic_exp ); void ShuffleMemorise( ll& n , const list& adic_exp , const ll& I ); void Solve() { string S; ll P; ll I; cin >> S; cin >> P; cin >> I; ll 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; ll power = ( P - 1 ) % 30000000000000; const list adic_exp = AdicExp( power ); if( I < MODULO ){ for( ll i = 0 ; i < I ; i++ ){ Shuffle( answer , adic_exp ); } } else { ShuffleMemorise( answer , adic_exp , I ); } } const string answer_str = to_string( MODULO + answer ).substr( 1 ); cout << answer_str << endl; return; } list AdicExp( ll& power ) { list a{}; while( power != 0 ){ a.push_back( power % 2 ); power /= 2; } return a; } void Shuffle( ll& n , const list& adic_exp ) { if( n == 0 ){ return; } ll M00 = 1 , M01 = 0; ll M10 = 0 , M11 = 1; ll P00 = n , P01 = 1; ll P10 = 1 , P11 = 0; for( auto itr = adic_exp.begin() , end = adic_exp.end() ; itr != end ; itr++ ){ if( *itr == 1 ){ const ll N00 = M00 , N01 = M01; const ll N10 = M10 , N11 = M11; M00 = ( P00 * N00 + P01 * N10 ) % MODULO , M01 = ( P00 * N01 + P01 * N11 ) % MODULO; M10 = ( P10 * N00 + P11 * N10 ) % MODULO , M11 = ( P10 * N01 + P11 * N11 ) % MODULO; } const ll Q00 = P00 , Q01 = P01; const ll Q10 = P10 , Q11 = P11; P00 = ( Q00 * Q00 + Q01 * Q10 ) % MODULO , P01 = ( ( Q00 + Q11 ) * Q01 ) % MODULO; P10 = ( Q10 * ( Q00 + Q11 ) ) % MODULO , P11 = ( Q10 * Q01 + Q11 * Q11 ) % MODULO; } if( adic_exp.empty() ? true : adic_exp.front() == 0 ){ if( M00 == 0 ){ M00 = MODULO - 1; } else { M00 -= 1; } } n = M00; return; } void ShuffleMemorise( ll& n , const list& adic_exp , const ll& I ) { list answer_memory{}; for( ll i = 0 ; i < MODULO ; i++ ){ answer_memory.push_back( n ); Shuffle( n , adic_exp ); } const ll& 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; }