@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 = g hs = h(s) t = g if hs == h(t) puts [s,t] exit end end