gets a, *b = gets.split.map &:to_i gets c, *d = gets.split.map &:to_i numerator = ([a] + d).inject(:*) denominator = ([c] + b).inject(:*) gcd = numerator.gcd denominator sign = numerator * denominator < 0 ? -1 : 1 puts"#{sign * (numerator / gcd).abs} #{(denominator / gcd).abs}"