#include using namespace std; unsigned long long HASH ( const string S, const unsigned long long a, const unsigned long long b ) { int i; int iSize; unsigned long long h; h = 0; iSize = S.size(); for( i = 0; i < iSize; i++ ) { h = ( h * a + S[ i ] ) % b; } return h; } bool NextStr( string &S ) { int i; int iSize; i = 0; iSize = S.size(); while( i < iSize ) { S[ i ]++; if( S[ i ] > 'z' ) { S[ i ] = 'a'; i++; } else return true; } return false; } bool CheckHash ( const int a, const int b, const int iDigit ) { string S( iDigit, 'a' ); unsigned long long h; map m; map::iterator it; while( true ) { h = HASH( S, a, b ); it = m.find( h ); if( it != m.end() ) { cout << m[ h ] << endl; cout << S << endl; return true; } else m[ h ] = S; if( ! NextStr( S ) ) return false; } return false; } bool CheckHash( const int a, const int b ) { int iDigit; for( iDigit = 2; iDigit < 10; iDigit++ ) { if( CheckHash( a, b, iDigit ) ) return true; } return false; } int main() { int a; int b; cin >> a; cin >> b; CheckHash( a, b ); return 0; }