Codeforces Round #166 (Div. 2)

http://www.codeforces.com/contest/263

A, B問題だけ。

A. Beautiful Year

require 'set'

def is_disinct_digts(digits)
  set = Set.new digits
  return true if set.length == digits.length
  
  false 
end

y = gets.chomp.to_i
ans = nil

for i in 1 .. 9000 do
  y2 = y + i
  if is_disinct_digts(y2.to_s.split(""))
    ans = y2
    break
  end
end

puts ans

B. Prime Matrix

n*mの行列が与えられる。

任意の要素を+1加算する操作ができる。

このとき、任意の行または列が素数のみで構成されるようにするために必要な操作の最小回数を求めよ。

require 'prime'

n, m = gets.chomp.split(" ").map{ |e| e.to_i }
matrix = 
n.times do
  matrix << gets.chomp.split(" ").map{ |e| e.to_i }
end
primes = Prime.each(100000 + 1000).to_a
min = 100000 * [n, m].max

memo = 
idx = 0
for i in 0 .. 100000 do
  if not i <= primes[idx]
    idx += 1
  end
  memo << primes[idx]
end

matrix.concat matrix.transpose

for i in 0 .. (n + m - 1) do
  col = matrix[i]
  
  num = 0
  col.each do |n|
    num += memo[n] - n
  end
  min = [min, num].min
end

puts min