@a,@b = gets.split.map(&:to_i) def h(s) s.bytes.reduce(0){|ha,c|(ha*@a+c)%@b} end def g 9.times.map{rand(97..122)}.pack("c*") end loop do s,t = g,g if h(s) == h(t) puts [s,t] break end end