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