#include #include #include #include using namespace std; #define CIN( LL , A ) LL A; cin >> A #define TYPE_OF( VAR ) remove_const::type >::type #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 #include #define MAIN main int MAIN() { CIN( int , B_0 ); assert( 1 <= B_0 && B_0 <= 100 ); CIN( int , C_0 ); assert( -100 <= C_0 && C_0 <= 100 ); C_0 = ( C_0 >= 0 ? C_0 % B_0 : ( B_0 - ( - C_0 ) % B_0 ) % B_0 ); CIN( int , B_1 ); assert( 1 <= B_1 && B_1 <= 100 ); CIN( int , C_1 ); assert( -100 <= C_1 && C_1 <= 100 ); C_1 = ( C_1 >= 0 ? C_1 % B_1 : ( B_1 - ( - C_1 ) % B_1 ) % B_1 ); int product = B_0 * B_1; FOR( n , 0 , product ){ if( n % B_0 == C_0 && n % B_1 == C_1 ){ RETURN( n ); } } RETURN( "NaN" ); }