@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) 99.times do t = g if hs == h(t) puts [s,t] break end end end