source_code:
def solution(n, k, d)
  @memo = Array.new(2) { Array.new(n+1, false) }
  def solve(n, k, d, flag)
    return @memo[(flag ? 1 : 0)][n] if @memo[(flag ? 1 : 0)][n]
    return 1 if n == 0 && flag
    return 0 if n == 0 && !flag
    cnt = 0
    for i in 1..k do
      cnt += solve(n-i, k, d, flag || (i >= d)) if n-i >= 0
    end
    @memo[(flag ? 1 : 0)][n] = cnt
  end
  solve(n, k, d, false)
  @memo[0][n] % 1000000007
end

puts solution(*gets.split.map(&:to_i))

lang_cluster: Ruby

exec_outcome: PASSED


source_code:
n = gets.to_i
a = gets.split.map(&:to_i)

for i in 0 ... n
  for j in i ... n
    # increasing in [0, i]?
    ok = true
    for k in 0 ... i
      if a[k] >= a[k + 1]
        ok = false
        break
      end
    end
    if not ok
      next
    end
    for k in j ... n - 1
      if a[k] <= a[k + 1]
        ok = false
        break
      end
    end
    if not ok
      next
    end
    if a[i .. j].uniq.size != 1
      next
    end
    puts 'YES'
    exit
  end
end
puts 'NO'


source_code:
stones = gets.chomp.to_i
colors = gets.chomp
colors = colors.split("")

for i in (0..colors.size-1)
	if i < colors.size - 1
		while colors[i] == colors[i+1] && colors.size > 1
			colors.delete_at(i+1)
		end
	end
end

print stones - colors.size


source_code:
n,m = gets.split(" ").map(&:to_i)
arr = []

n.times do
	arr << gets.split("")
end
eaten = Array.new(n)
eaten.map! { |e| Array.new(m,false) }
count = 0
(0..(n-1)).each do |i|
	(0..(m-1)).each do |j|
		if arr[i][j] == 'W'
			if arr[i][j+1] && arr[i][j+1] == 'P' && !eaten[i][j+1]
				eaten[i][j+1] = true
				count += 1
			elsif j > 0 && arr[i][j-1] == 'P' && !eaten[i][j-1]
				eaten[i][j-1] = true
				count += 1
			elsif arr[i+1] && arr[i+1][j] == 'P' && !eaten[i+1][j]
				eaten[i+1][j] = true
				count += 1
			elsif i > 0 && arr[i-1][j] == 'P' && !eaten[i-1][j]
				eaten[i-1][j] = true
				count += 1
			end
		end
		# puts "#{i} #{j} #{arr[i][j]}"
		# puts "here #{count}"
	end
end
puts count


source_code:
a=gets.split.map{|x| x.to_i}
b=false
c=0
for i in 0..5
 if a.count(a[i])>=4
  b=true
  c=a[i]
  break
 end
end

s=0
for i in 0..5
 if a[i]==c
  a[i]=nil
  s+=1
 end
 break if s==4
end

if b
 a=a.compact
 puts "Elephant" if a[0]==a[1]
 puts "Bear" if a[0]!=a[1]
else
 puts "Alien"
end


source_code:
#!/usr/bin/ruby
s=gets.chomp.chars.uniq.join
puts s==[*'a'..'z'].join[0,s.size] ? :YES : :NO


source_code:
s = gets
t = gets
start = 0
t.each_char do |i|
    if s[start] == i
        start +=1
    end
end

puts start + 1


source_code:
recipe = gets
n = gets.split.map{|e| e.to_i}
p = gets.split.map{|e| e.to_i}
budget = gets.to_i

k = ["B", "S", "C"].map{|e| recipe.count e}

def non_neg(x)
    return x < 0 ? 0 : x
end

l = 0; r = budget + n.max; x = 0
while l <= r
    m = (l + r) / 2
    sum_cost = k.map.with_index{|e, i| e == 0 ? 0 : p[i] * non_neg(e * m - n[i])}.inject(:+)
    if sum_cost < budget
        l = m + 1
        x = m
    elsif sum_cost > budget
        r = m - 1
    else
        x = m
        break
    end
end
puts x


source_code:
cityNum, livingCity = gets.chomp.split(' ').map(&:to_i)
criminals = gets.chomp.split(' ').map(&:to_i)

maxDist = [(cityNum - livingCity), livingCity - 1].max
checkedDist = 0

sure = 0

until checkedDist > maxDist
	leftCity = livingCity - checkedDist
	rightCity = livingCity + checkedDist

	lackNumber = 0
	lackNumber += 1 if leftCity <= 0
	lackNumber += 1 if rightCity > cityNum
	if leftCity == rightCity
		sure += 1 if criminals[livingCity - 1] > 0
	else
		case lackNumber
		when 0 then sure += 2 if criminals[leftCity - 1] > 0 && criminals[rightCity - 1] > 0
		when 1 then	sure += 1 if (leftCity > 0 && criminals[leftCity - 1] > 0) || (rightCity <= cityNum && criminals[rightCity - 1] > 0)
		end
	end

	checkedDist += 1
end

puts sure


source_code:
w,h,k=gets.split.map &:to_i
p k*(2*(w+h)-8*k+4)


source_code:
def gao1(a, b)
	c = 0
	4.times{|i| c += 1 if a[i] == b[i]}
	c
end

def gao2(a, b)
	c = 0
	4.times{|i| c += 1 if b.index(a[i])}
	c
end

n = gets.to_i
x = 100.upto(9999).map{|_| '%04d' % _}
x.reject!{|_| _.bytes.sort.uniq.size < 4}
n.times do
	a, b, c = gets.split.map(&:to_i)
	a = '%04d' % a
	c += b
	x.reject!{|_| gao1(_, a) != b || gao2(_, a) != c}
end

if x.empty?
	puts 'Incorrect data'
elsif x.size > 1
	puts 'Need more data'
else
	puts x[0]
end


source_code:
w,m,k=gets.split.map(&:to_i)
a=Math.log10(m).to_i+1
v=l=0
while a*k*(c=(b=10**a-1)-m+1)+v <=w do
  v+=a*k*c
  l+=c
  m=b+1
  a+=1
end
l+=(w-v)/(a*k)
p l


source_code:
n = gets.chomp.to_i
interestings = gets.chomp.split(/ /).map(&:to_i)
watch = 0
prev = 0
(0...n).each do |i|
    curr = interestings[i]
    watch = curr
    if curr - prev > 15
        watch = prev + 15
        break
    end
    prev = curr
end
watch = prev + 15 if 90 - prev > 15
watch = 90 if watch > 90 || 90 - prev <= 15
puts watch


source_code:
gets
arr = gets.split

cnt1 = arr.count(?1)
ans = 0
cnt0 = 0

arr.each do |x|
	if x == ?1
		ans = [ans, cnt0 + cnt1].max
		cnt1 -= 1
	else
		cnt0 += 1
	end
end

p [ans, cnt0 + cnt1].max


source_code:
l, r = gets.split(' ').map(&:to_i)
cnt = 0
for x in 0..(Math.log(r, 2)+1)
	for y in 0..(Math.log(r,3)+1)
		a = (2**x)*(3**y)
		if a >= l and a <= r
			cnt += 1
		end
	end
end
print String(cnt)


source_code:
exhibit = gets.rstrip

arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

count = 0
current_index = 0
exhibit.split('').each do |char|
  index = arr.find_index do |l|
    l == char
  end

  distance_index = (index - current_index).abs

  if distance_index > 13
    if index < current_index
      count += index + 26 - current_index
    else
      count += current_index + 26 - index
    end
  else
    count += distance_index
  end
  current_index = index
end

puts count


source_code:
x,t,a,b,da,db=gets.split.map(&:to_i);
tag=0;
for i in 0...t
	for j in 0...t
		if a-i*da+b-j*db==x or a-i*da==x or b-j*db==x or x==0
			tag=1;
			break;
		end
	end
end

if tag==1
	puts "YES"
else
	puts "NO"
end


source_code:
a1, a2 = gets.chomp.split(" ").map(&:to_i)
n = 0
while true
  if a1 < a2 then
    a1 += 1
    a2 -= 2
  else
    a1 -= 2
    a2 += 1
  end
  if a1 > 0 and a2 > 0 then
    n += 1
  elsif a1 == 0 or a2 == 0 then
    n += 1
    puts n
    exit
  else
    puts n
    exit
  end
end


source_code:
s = gets.chomp.downcase
puts (s =~ /[^aeioun][^aeiou]/ || s=~ /[^aeioun]$/) ? "NO" : "YES"


source_code:
a,b,m,r = gets.split.map(&:to_i)

ar = Array.new(m)
ar[r]=0
i = 0
loop do
  i += 1
  r = (a*r+b)%m
  if ar[r]
    puts i - ar[r]
    break
  end
  ar[r] = i
end


source_code:
n, k = gets.chomp.split(" ").map{|e| e.to_i}

left = 1
right = n
ans = nil
while (left <= right) do
  mid = (left + right) / 2
  tmp = mid
  sum = 0

  while (tmp > 0) do
    sum += tmp
    tmp /= k
  end

  if sum < n
    left = mid + 1
  else
    right = mid - 1
    ans = mid
  end
end

puts ans


source_code:
#!/Users/mike/.rvm/rubies/ruby-1.9.3-p327/bin/ruby

redNum, blueNum = STDIN.readline.split.collect {|s| s.to_i}

def simulate(red, redCount, blueCount, petyaMove)
	petya, vasya = 0, 0
	while redCount+blueCount > 0
		wasRed = red
		if petyaMove and red and redCount > 0
			red = true
		elsif petyaMove and !red and blueCount > 0
			red = false
		elsif !petyaMove and red and blueCount > 0
			red = false
		elsif !petyaMove and !red and redCount > 0
			red = true
		elsif redCount > 0
			red = true
		else
			red = false
		end
		if red
			redCount -= 1
			petya += 1 if wasRed
			vasya += 1 if !wasRed
		else
			blueCount -= 1
			vasya += 1 if wasRed
			petya += 1 if !wasRed
		end
		petyaMove = !petyaMove
	end
	return [petya, vasya]
end

petya0, vasya0 = simulate(true, redNum-1, blueNum, false)
petya1, vasya1 = simulate(false, redNum, blueNum-1, false)

if (petya0 > petya1)
	puts '%s %s' % [petya0, vasya0]
else (petya1 > petya0)
	puts '%s %s' % [petya1, vasya1]
end


source_code:
k,r=gets.split.map &:to_i
(1..10).each{|i|t=k*i%10;(p i;exit)if t==0||t==r}


source_code:
def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

require 'prime'
n = inp[0]

d = Prime.prime_division(n)
if(d.size == 1 )
  p d[0][0]
else
  p 1
end


source_code:
n = gets.to_i
puts n - gets.chomp.scan(/RU|UR/).size


source_code:
def step(a,op)
	a.permutation.map{|v| ( ( [ (op=="+") ? v[0]+v[1] : v[0]*v[1] ] ) + ( (v.drop(2)) ? v.drop(2) : [] ) ).sort }.uniq
end

a=gets.split.map{|v| v.to_i}
oper=gets.split
res=[]
step(a,oper[0]).each do |v|
  step(v,oper[1]).each do |t|
	res+=step(t,oper[2])
  end
end
puts res.min


source_code:
X,Y,x,y,a,b=$<.read.split.map &:to_i
x-=X
y-=Y
puts x%a==0&&y%b==0&&x/a%2==y/b%2 ? :YES : :NO


source_code:
n = gets.to_i
s = gets.chomp.split ''
c = 0
s.each do |x|
  c += x == '+'  ? 1 : -1
  c = [c, 0].max
end
puts c


source_code:
MOD = 10**9 + 7
def modpow(x,n)
  ret = 1
  while n > 0
    ret = ret*x % MOD if n&1 == 1
    x = x*x % MOD
    n >>= 1
  end
  return ret
end

n,m = gets.split.map(&:to_i)
mx = [n,m].max
dp = Array.new(mx+1){[0,0]}
dp[1] = [2,0]
1.upto(mx-1) do |i|
  dp[i+1][0] = (dp[i][0] + dp[i][1]) % MOD
  dp[i+1][1] = (dp[i][0]) % MOD
end
sumn = (dp[n][0] + dp[n][1]) % MOD
summ = (dp[m][0] + dp[m][1]) % MOD
ans = (sumn + summ - 2) % MOD
p ans


source_code:
def gao(n)
    unless $h[n]
        t = 1
        d = 2
        while d * d <= n
            if n % d == 0
                t = [t, gao(d), gao(n / d)].max
            end
            d += 1
        end
        $h[n] = n + t
    end
    return $h[n]
end

$h = {1 => 1}
n = gets.to_i
p gao(n)


source_code:
a = []
3.times do
  a.push(gets.split.map(&:to_i))
end
a.map!{|b| b.map!{|c| c = c % 2}}
ans = []
5.times do
  ans.push([1,1,1,1,1])
end
3.times do |i|
  3.times do |j|
    if a[i][j] == 1
      ans[i+1][j+1] = 1 - ans[i+1][j+1]
      ans[i][j+1] = 1 - ans[i][j+1]
      ans[i+2][j+1] = 1 - ans[i+2][j+1]
      ans[i+1][j] = 1 - ans[i+1][j]
      ans[i+1][j+2] = 1 - ans[i+1][j+2]
    end
  end
end
3.times do |i|
  3.times do |j|
    print ans[i+1][j+1]
  end
  print "\n"
end