h=Hash.new 0
gets
gets.chomp.chars.each_cons(2){|a|h[a]+=1}
puts h.max_by{|k,v|v}[0]*''


n=gets.to_i
s=gets
puts ('AA'..'ZZ').max_by{|x| (1...n).count{|i| s[i-1, 2]==x}}


n = gets.to_i
s = gets.chomp
cnt = Hash.new(0)
(n-1).times do |i|
    cnt[s[i, 2]] += 1
end

maxv = 0
ans = ""

cnt.each_key do |c|
    if cnt[c] > maxv
        ans = c
        maxv = cnt[c]
    end
end
puts ans


n = gets.chomp.to_i
a = gets.chomp.to_i
b = gets.chomp.to_i
c = gets.chomp.to_i

l = 0
r = n + 1
m = (l + r) / 2

while l < m && m < r
  if 0 <= n - m * b + (m - 1) * c
    l = m
  else
    r = m
  end
  m = (l + r) / 2
end

puts [n / a, m + (n - m * b + m * c) / a].max


n = gets.chomp.to_i
a = gets.chomp.to_i
b = gets.chomp.to_i
c = gets.chomp.to_i

if n < a && n < b
	puts 0
elsif b-c <= a && n > b

	tot = (n-b)/(b-c)
	rem=(n-b) % (b-c)+c
	puts 1 + tot + rem/a
else
	puts (n/a)+(n%a)/b
end


n=gets.to_i
a=gets.to_i
b=gets.to_i
c=gets.to_i

if a<=b-c||b>n then
    puts n/a
    exit(0)
end

n-=b
ans=n/(b-c)+1
n%=b-c
n+=c
ans+=n/a

puts ans


v=1000000000;
input=gets.split.map(&:to_i);
a=input[0];b=input[1];m=input[2];
if a==v then
        a-=1;
end
if b==v then
        b-=1;
end
v%=m;
if b>=m || v==0 then
        puts 2;
else
        el=[a,m].min;
        j=v;
        for i in 1..el
                if j+b>=m then

                else
                        printf "1 %09d\n",i;
                        exit;
                end
                j+=v;
                if j>m then
                        j-=m;
                end
        end
        puts 2;
end


def check(as, bs)
    ax = [0,2,4,6].map{|i|as[i]}
    ay = [1,3,5,7].map{|i|as[i]}
    bcx = [0,2,4,6].map{|i|bs[i]}.inject(:+)/4.to_f
    bcy = [1,3,5,7].map{|i|bs[i]}.inject(:+)/4.to_f
    [bs[0,2], bs[2,2], bs[4,2], bs[6,2], [bcx,bcy]].each do |bx, by|
        if bx.between?(ax.min, ax.max) && by.between?(ay.min, ay.max)
            puts 'YES'
            exit
        end
    end
end

as = gets.split.map(&:to_i)
bs = gets.split.map(&:to_i)

check(as, bs)
4.times do |i|
    as[2*i], as[2*i+1] = [as[2*i] + as[2*i+1], as[2*i] - as[2*i+1]]
    bs[2*i], bs[2*i+1] = [bs[2*i] + bs[2*i+1], bs[2*i] - bs[2*i+1]]
end
check(bs, as)
puts 'NO'


n = gets.to_i

a = gets.split.map(&:to_i).sort
b = []

while (0 < a.size)
  current = a.shift
  flag = false
  for i in (0..(b.size-1)) do
    if (b[i] <= current)
      b[i]+=1
      flag = true
      break
    end
  end
  b << 1 unless flag
end
puts b.size


sum = gets.to_i
cx = [0] * 101
ans = 0
gets.split.map(&:to_i).each { |x| cx[x] += 1 }

def check(len, cx)
  r = []
  dx = cx.clone
  (0...len).each do |i|
    ok = false
    (i..100).each do |j|
      next if dx[j].zero?
      dx[j] -= 1
      r << j
      ok = true
      break
    end
    return false unless ok
  end
  r
end

len = 100
while sum > 0
  break if len < 1
  r = check(len, cx)
  if r == false
    len -= 1
  else
    # p r
    sum -= r.size
    r.each { |x| cx[x] -= 1 }
    ans += 1
  end
end

p ans


n=gets.to_i
a=gets.split.map(&:to_i).sort
c=0
while a[0]
  x=-1
  a.delete_if{|y|y>x ?x+=1:nil}
  c+=1
end
p c


n, m = gets.chomp.split.map(&:to_i)
answer = \
  case m
  when 0,1
    1
  else
    [m, n-m].min
  end

puts answer


n, a, b=gets.split.map &:to_i
a, b=b, a if a>b
res=1
for k in 1..a
    x, y=a, b
    ok=true
    for i in 0...n
        if x>=k
            x-=k
            x=0 if x<k
        else
            y-=k
        end
        ok=false if y<0
    end
    res=k if ok
end
puts res


x,y = gets.split.map(&:to_i)
if x==y
  puts "YES"
  exit
end
bubun = []
if x[0] == 1
  bubun << x.to_s(2)
  bubun << x.to_s(2).reverse
else
  x1 = (x<<1) + 1
  bubun << x1.to_s(2)
  bubun << x1.to_s(2).reverse
  i = 0
  while x[i] == 0
    i += 1
  end
  x2 = x >> i
  bubun << x2.to_s(2)
  bubun << x2.to_s(2).reverse
end
y = y.to_s(2)
y_str = y.size
bubun.each do |str|
  len = str.size
  f = true
  i = /#{str}/ =~ y
  if i != nil
    0.upto(i-1) do |j|
      f = false if y[j] == "0"
    end
    (i+len).upto(y_str-1) do |j|
      f = false if y[j] == "0"
    end
    if f
      puts "YES"
      exit
    end
  end
end
puts "NO"


a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
cnt =0
(0..a.length - 1).each{|i|
    if a[i] == b[i]
        puts "YES"
        break
    else
        cnt += 1
    end
}
if cnt == a.length
    puts "NO"
end


a = gets.chomp.split(/ /).map!{|x|x.to_i}
b = gets.chomp.split(/ /).map!{|x|x.to_i}
ret = "NO"
for i in 0..2
	if a[i] == b[i]
		ret = "YES"
	end
end

puts ret


DBG = true
c1 = gets.to_i
c2 = gets.to_i
c3 = gets.to_i
c4 = gets.to_i
puts (c1 == c4 && (c3==0 || c1>0)) ? "1" : "0"


class Matrix2x2
  attr_accessor :a, :b, :c, :d
  def initialize(a=0,b=0,c=0,d=0)
    @a = a
    @b = b
    @c = c
    @d = d
  end
end

def mult(x, y)
  z = Matrix2x2.new
  z.a = (x.a*y.a + x.b*y.c)%1000000007
  z.b = (x.a*y.b + x.b*y.d)%1000000007
  z.c = (x.c*y.a + x.d*y.c)%1000000007
  z.d = (x.c*y.b + x.d*y.d)%1000000007
  z
end

def expt(a, p)
  if p == 1
    a.clone
  elsif p % 2 == 1
    t = expt(a, p-1)
    mult(a, t)
  else
    t = expt(a, p/2)
    mult(t, t)
  end
end

n = gets.to_i
a = Matrix2x2.new(0, 3, 1, 2)
an = expt(a, n)
puts an.a


require 'matrix'

MOD = 1000000007

def mul(a, b)
    (a * b).map{|_| _ % MOD}
end

a = Matrix.build(4, 4){|i, j| i == j ? 0 : 1}
b = Matrix.I(4)
n = gets.to_i
while n > 0
    b = mul(b, a) if n.odd?
    a = mul(a, a)
    n /= 2
end
p b[0, 0]


mod=1000000007

n=gets.to_i
ans=1

1.upto(n) do|i|
    ans=ans*i
    ans%=mod
end

power=1

1.upto(n-1) do|i|
    power=power*2
    power%=mod
end

ans=(ans-power)%mod

if ans<0
    ans+=mod
end

puts ans


def rec(x, now, matched, last)
  state=[now,matched,last]
  if @memo.key?(state)
    return @memo[state]
  end
  if now==x.length-1
    if !matched
      return 1
    else
      if x[x.size()-1].to_i>=last
        return 1
      end
      return 0
    end
  end
  if !matched
    return 10*rec(x,now+1,false,last)
  else
    ret=0
    for i in 0..(x[now].to_i-1)
      ret+=rec(x,now+1,false,last)
    end
    ret+=rec(x,now+1,true,last)
    return @memo[state]=ret
  end
end
# x以下の条件を満たす数の個数
def solve(x)
  ret=0
  if x<=9
    return x
  end
  if 1<=x.to_s.length-1
    ret+=9
  end
  for i in 2..(x.to_s.length-1)
    ret+=9*10**(i-2)
  end
  x=x.to_s
  @memo=Hash.new
  for i in 1..(x[0].to_i-1)
    ret+=rec(x,1,false,i)
  end
  ret+=rec(x,1,true,x[0].to_i)
  return ret
end

l,r=gets.split.map(&:to_i)
p solve(r)-solve(l-1)


n=gets.to_i
sq,x,y=0,[],[]
n.times do
  x1,y1,x2,y2=gets.split.map(&:to_i)
  sq+=(x2-x1)*(y2-y1)
  x.push x1,x2
  y.push y1,y2
end
if (x.max-x.min)==(y.max-y.min) and (y.max-y.min)**2==sq
  puts "YES"
else
  puts "NO"
end


n, a, b, c = gets.strip.split(' ').map(&:to_i)

def solve(n, a, b, c, k = 0)
  if k == 4
    return 10**11
  end
  if n % 4 == 0
    return 0
  end

  [a + solve(n + 1, a, b, c, k + 1), b + solve(n + 2, a, b, c, k + 1), c + solve(n + 3, a, b, c, k + 1)].min
end

printf("%d\n", solve(n, a, b, c))


inp = gets.chomp.split(' ')
n=inp[0].to_i
a=inp[1].to_i
b=inp[2].to_i
c=inp[3].to_i
k= 4-n%4 if(n%4!=0)

if (n%4==0)
    puts 0
    k=0
end

if(k==1)
    puts [a,b+c,3*c].min
elsif (k==2)
    puts [2*a,b,2*c].min
elsif (k==3)
    puts [3*a,a+b,c].min
end


require 'prime'

n, k = gets.chomp.split.map(&:to_i)
a = n.prime_division
sum = (a.map do | x | x[1] end).inject(0, :+)

if sum < k
  puts -1
else
  for x in a
    for i in 1..x[1]
      if k == 1
        break
      end
      print x[0], ' '
      k -= 1
      n /= x[0]
    end
  end
  puts n
end


require 'prime'
n,k=gets.split.map(&:to_i)
a=n.prime_division.flat_map{|n,p|[n]*p}
puts a.size<k ? -1 : (a[0,k-1]+[a[k-1..-1].reduce(:*)])*' '


n, k = gets.split.map(&:to_i)

data = []
for i in (2..n)
    while n % i == 0
        data.push(i)
        n /= i
    end
end

if data.length < k
    puts -1
else
    data[(k-1..-1)] = data[(k-1..-1)].reduce(:*)
    puts data.map(&:to_s).join(' ')
end


a, b, w, x, c = gets.split.map &:to_i

go = Array.new(w + 1, -1)
val = []

while go[b] == -1
  b = go[b] = if b >= x then val << 1; b - x else val << 0; b + w - x end
end

if c <= a
  puts 0
else
  x, y, z, ans = 0, 0, 0, 1e100
  val.each { |v| x += v }

  c -= a
  val.each { |v|
    y += v
    z += 1
    if (c - y) % x == 0
      tmp = (c - y) / x * val.size + z
      ans = tmp if tmp < ans
    end
  }
  puts ans
end


a,b,w,x,c=gets.split.map(&:to_i)
h={}
i=0
aa=a
while true
  if c<=a
    p i
    exit
  end
  if b>=x
    bb=b-x
  else
    aa=a-1
    bb=w-(x-b)
  end
  break if h[bb]!= nil
  b=bb
  a=aa
  h[b]=[i,a]
  i+=1
  c-=1
end
cl=i-h[bb][0]
da=h[bb][1]-aa
d=(c-a)/(cl-da)-1
a-=da*d
c-=d*cl
i+=d*cl
while c>a
  if b>=x
    b-=x
  else
    a-=1
    b=w-(x-b)
  end
  c-=1
  i+=1
end
p i


require "prime"
A = gets.to_i
ans = 1
A.prime_division.each{ |q,d| ans *= d+1 }
p ans


n, k, x = gets.split.map{|i| i.to_i}
arr = gets.split.map{|i| i.to_i}

ans = k * x

(n - k).times{|i| ans += arr[i]}

p ans


n, k, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 0
1.upto(n) do |i|
    if i <= k
        ans += x
    else
        ans += a[-i]
    end
end
puts ans


n,k,x,*a=$<.read.split.map &:to_i
k=n if k>n
p a[0...-k].reduce(0,:+)+k*x


w,h,k = gets.split.map(&:to_i)
ans = 0
k.times do
  ans += (w+h-2)*2
  w -= 4
  h -= 4
end

puts ans


def cnt(w, h)
  2*h + 2*(w-2)
end

w,h,k = gets.split.map(&:to_i)
ans = 0
k.times do |i|
  ans += cnt(w - (4*i), h - (4*i))
end
puts ans


def f(n)
	if n==1
		return X
	elsif n==2
		return X+[X,Y].min
	elsif n%2==0
		return f(n/2)+[Y,X*(n-n/2)].min
	else
		return X+[f(n+1),f(n-1)].min
	end
end
N,X,Y=gets.split.map(&:to_i)
p f(N)


N, X, Y = $stdin.gets.split.map(&:to_i)

$sep = Y / X

# n = N
# result = 0

def getResult(n)
  # temp = n>>1 * X < Y
  return X if n == 1
  if (n>>1) <= $sep
    return n * X
  end
  result = 0
  if n & 1 == 1
    minus = getResult(n-1)
    plus = getResult(n+1)
    result = X + (minus < plus ? minus : plus)
  else
    result = Y + getResult(n>>1)
  end
  # print("#{n} #{result}\n")
  result
end

puts getResult(N)


n = gets.to_i
p = gets.split.map(&:to_i).map{|x| x - 1}

if p.uniq.size != n
  puts(-1)
  exit
end

ls = []
n.times do |i|
  x = p[i]
  len = 1
  until x == i
    x = p[x]
    len += 1
  end
  ls << len
end

ans = 1
ls.each do |l|
  l /= 2 if l.even?
  ans = ans.lcm(l)
end
puts ans


MOD = 1000000009

def clamp(x)
	x < 0 ? (x + MOD) : x
end

n, m = gets.split.map(&:to_i)

pow2m = 1

m.times { |_| pow2m = (pow2m << 1) % MOD }

ans = 1

(1..n).each do |i|
	ans = (ans * clamp(pow2m - i)) % MOD
end

puts ans


n, k, m = gets.split.map{|x| x.to_i}
sum = 0
t = gets.split.map{|x| sum += x.to_i; x.to_i}.sort

ans = 0

(0..n).each do |i|
	rt = m - i * sum
	re = n - i
	cur = i * (k + 1)

	break if rt < 0

	t.each do |ti|
		re.times do
			cur += 1 if ti <= rt
			rt -= ti
		end

		break if rt <= 0
	end

	ans = cur if ans < cur
end

p ans


a = gets.split.map{|i| [i[0].ord - 97, i[1].ord - 49]}
b = Array.new(8){Array.new(8){false}}
c = Array.new(8){Array.new(8){false}}
a[0 .. 2].each{|x,y| b[x][y] = true}
a[0 .. 1].each do |x,y|
	x.pred.downto(0).each{|i| c[i][y] = true; break if b[i][y]}
	x.succ.upto(7).each{|i| c[i][y] = true; break if b[i][y]}
	y.pred.downto(0).each{|j| c[x][j] = true; break if b[x][j]}
	y.succ.upto(7).each{|j| c[x][j] = true; break if b[x][j]}
end
(a[2][0] - 1 .. a[2][0] + 1).each do |i|
	(a[2][1] - 1 .. a[2][1] + 1).each do |j|
		c[i][j] = true if (0 .. 7).include?(i) && (0 .. 7).include?(j)
	end
end
(a[3][0] - 1 .. a[3][0] + 1).each do |i|
	(a[3][1] - 1 .. a[3][1] + 1).each do |j|
		if (0 .. 7).include?(i) && (0 .. 7).include?(j) && !c[i][j] then
			puts :OTHER
			exit
		end
	end
end
puts :CHECKMATE


n = gets.to_i
s = gets.strip
d = Array.new(n + 1) { [Float::INFINITY] * 2 }
d[0][0] = 0
n.times do |i|
  2.times do |j|
    d[i + 1][j] = [d[i + 1][j], d[i][j] + 1].min
  end
  if i + i <= n && s[0, i] == s[i, i]
    d[i + i][1] = [d[i + i][1], d[i][0] + 1].min
  end
end
p d[-1].min


a = gets.split.map &:to_i
(0..15).each do |x|
  f, s = 0, 0
  (0..3).each { |i| x & 1 << i != 0 ? f += a[i] : s += a[i] }
  if f == s
    puts 'YES'
    exit
  end
end
puts 'NO'


as = gets.split.map(&:to_i)

(1..4**2).each do |x|
  sum = 0
  (0..3).each do |i|
    if x & (1<< i) == 0
      sum += as[i]
    end
  end
  if sum * 2 == as.inject(:+)
    puts 'YES'
    exit
  end
end
puts 'NO'


#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# b.rb

def main()
    v1, v2 = gets.chomp.split(" ").map{|s| next s.to_i }
    t, d = gets.chomp.split(" ").map{|s| next s.to_i }

    r = 0
    t.times do |tt|
        t1 = tt
        t2 = t - 1 - tt
        r += [v1 + d * t1, v2 + d * t2].min
    end
    puts r

end

if __FILE__ == $0 then
    main()
end


DEBUG = false

start_v, end_v = gets.strip.split(' ').map &:to_i
time, delta = gets.strip.split(' ').map &:to_i

start_v, end_v = end_v, start_v if start_v > end_v

sum = start_v
current_v = start_v
2.upto(time) do |current_time|
  max_v = end_v + (time - current_time) * delta
  current_v = [max_v, current_v + delta].min
  sum += current_v

  if DEBUG
    puts "#{current_time}: max_v = #{max_v}, possible_curent_v = #{current_v + delta}, max_v = #{max_v}, sum = #{sum}"
  end
end

puts sum


v1, v2 = gets.split.collect{|i| i.to_i}.minmax
t, d = gets.split.collect{|i| i.to_i}
if d.zero?
    puts v1*t
    exit
end
x = ((1.0*d*t+v2-v1)/(2*d)).round
ans = v1*x+d*(x-1)*x/2 + v2*(t-x)+d*(t-x-1)*(t-x)/2
puts ans


n=gets.to_i
k=n.to_s.size-1
p (n-('9'*k).to_i).to_s.chars.map(&:to_i).reduce(:+)+9*k


def calc2(n)
  # a, b
  # b, a
  return n * (n - 1) / 2 * 1
end

def calc3(n)
  # a, b, c
  # b, c, a
  # c, a, b
  return n * (n - 1) * (n - 2) / 6 * 2
end

def calc4(n)
  # a, b, c, d
  # b, a, d, c
  # b, c, d, a
  # b, d, a, c
  # c, a, d, b
  # c, d, a, b
  # c, d, b, a
  # d, a, b, c
  # d, c, a, b
  # d, c, b, a
  return n * (n - 1) * (n - 2) * (n - 3) / 24 * 9
end

n, m = gets.split
n = n.to_i
m = m.to_i
ans = 1
if m >= 2
  ans = ans + calc2(n)
end
if m >= 3
  ans = ans + calc3(n)
end
if m >= 4
  ans = ans + calc4(n)
end
p ans


gets.to_i.times { a=gets+gets;puts a.include?('1') ? (!a.include?('0') ? 2 : 1) : 0}


za=gets.chomp.to_i
za.times do
	a,b=gets.split.map(&:to_i)
	c,d=gets.split.map(&:to_i)
	if (a==1 and b==1 and c==1 and d==1)
		puts 2
	elsif (a==1 or b==1 or c==1 or d==1)
		puts 1
	else
		puts 0
	end
end


gets.to_i.times { a=(gets+gets).split(' ');puts a.include?('1') ? (!a.include?('0') ? 2 : 1) : 0}


n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
hoge=n
k.times do |i|
    tmp = a[i] / 4
    if hoge >= tmp
        hoge -= tmp
        a[i] %= 4
    else
        a[i] -= hoge * 4
        hoge = 0
    end
    if hoge == 0
        break
    end
end
lim = 2 * n + hoge
lim2 = hoge
k.times do |i|
    tmp = a[i] / 2
    if lim >= tmp
        lim -= tmp
        a[i] %= 2
    else
        a[i] -= 2 * lim
        lim2 -= a[i]
        a[i] = 0
    end
end
lim2 += lim
k.times do |i|
    lim2 -= a[i]
end
puts lim2 >= 0 ? "YES" : "NO"


N, K = gets.split.map &:to_i
A = gets.split.map &:to_i
one, two, four = 0, 2 * N, N
until A.empty? do
  A.sort!
  u = A[-1]
  A.pop
  if four > 0
    four -= 1
    one += 1 if u <= 2
    u -= 4
  elsif two > 0
    two -= 1
    u -= 2
  elsif one > 0
    one -= 1
    u -= 1
  else
    A.push u
    break
  end
  A.push(u) if u > 0
end
puts ["NO", "YES"][A.empty? ? 1 : 0]


gets
stack=[]
ans=0
gets.split.map(&:to_i).each do |i|
  while stack.size>=2&&stack[-1]<=stack[-2]&&stack[-1]<=i
    ans+=[stack[-2],i].min
    stack.pop
  end
  stack.push i
end
stack.sort!
2.times{stack.pop}
stack.each do |i|
  ans+=i
end
p ans


gets
s,ans=[],0
gets.split.map(&:to_i).each do |i|
  while s.size>1&&[s[-1],s[-2],i].min==s[-1]
    ans+=[s[-2],i].min
    s.pop
  end
  s.push i
end
s.sort!
2.times{s.pop}
s.each{|i| ans+=i}
p ans


gets
s,ans=[],0
gets.split.map(&:to_i).each do |i|
  while s[-2]&&(k=[s[-2],i].min)>=s[-1]
    ans+=k
    s.pop
  end
  s.push i
end
s.sort!
(0..s.size-3).each{|i| ans+=s[i]}
p ans


def getnum(day)
  case day
  when "monday"
    0
  when "tuesday"
    1
  when "wednesday"
    2
  when "thursday"
    3
  when "friday"
    4
  when "saturday"
    5
  when "sunday"
    6
  end
end

d1 = getnum(gets.strip)
d2 = getnum(gets.strip)
ans = "NO"
mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
0.upto(10).each do |i|
  ans = "YES" if (d1 + mdays[i])%7 == d2
end
puts ans


a = gets.chomp

b = gets.chomp

if a == "monday"
	n1 = 1
end
if (a == "tuesday")
	n1 = 2
end
if (a == "wednesday")
	n1 = 3
end
if (a == "thursday")
	n1 = 4
end
if (a == "friday")
	n1 = 5
end
if (a == "saturday")
	n1 = 6
end
if (a == "sunday")
	n1 = 7
end

if (b == "monday")
	n2 = 1
end
if b == "tuesday"
	n2 = 2
end
if (b == "wednesday")
	n2 = 3
end
if (b == "thursday")
	n2 = 4
end
if (b == "friday")
	n2 = 5
end
if (b == "saturday")
	n2 = 6
end
if (b == "sunday")
	n2 = 7
end

dif = n2 - n1

if (dif < 0)
f = dif + 7
else
f = dif
end

if (f == 3)
	puts "YES"
elsif (f == 0)
	puts "YES"
elsif (f == 2)
	puts "YES"
else
	puts "NO"
end


d1 = gets.strip
d2 = gets.strip
months = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
mods = months.map { |e| e % 7 }
ans = "NO"
(1..12).each do |i|
    d1_id = days.index d1
    d2_id = d1_id + mods[i - 1]
    d2_id -= 7 if d2_id >= 7
    if days[d2_id] == d2
        ans = "YES"
        break
    end
end
puts ans


n,a,b,c = gets.chomp.split(/ /).map!{|x| x.to_i}
maxa = a / 2
maxab = maxa + b
minc = [(n - maxab+1)/2, 0].max
maxc = [n/2, c].min

ret = 0
for i in minc..maxc
	forb = n - i*2
	minb = [forb - maxa, 0].max
	maxb = [forb, b].min
	ret += maxb - minb + 1
end
puts ret


N = gets.to_i
as = gets.split.map(&:to_i)
cnt = Array.new(6, 0)
as.each do |a|
    cnt[a] += 1
end

border = (N*9+1)/2
shortage = border - as.inject(:+)
ans = 0
(1..4).each do |i|
    break if shortage < 0
    num = [cnt[i], (shortage-1)/(5-i)+1].min
    ans += num
    shortage -= num * (5-i)
end
puts ans


n,*a=$<.read.split.map &:to_i
a=a.map{|e|e*2}.sort
i=0;s=a.reduce(:+)
while s<n*9
	s+=10-a[i]
	i+=1
end
p i


n, h, m = gets.split.map(&:to_i)
rest = Array.new(n, h)
m.times do
  l, r, x = gets.split.map(&:to_i)
  (l..r).each do |pos|
    rest[pos-1] = [rest[pos-1], x].min
  end
end

ans = 0
rest.each do |r|
  ans += r ** 2
end
puts ans


n,h,m = gets.split.map(&:to_i)
height = [h]*(n+1)
m.times do
  l,r,x = gets.split.map(&:to_i)

  l.upto(r) do |i|
    height[i] = x if height[i] > x
  end
end
ans = 0
1.upto(n) do |i|
  ans += height[i] ** 2
end
p ans


def nth_root_ceil(n, m)
  return n if m == 1
  i = 1
  j = n
  while j > i + 1 do
    mid = (i + j) / 2
    if mid ** m >= n
      j = mid
    else
      i = mid
    end
  end
  return j
end

class Solution
  def initialize(n)
    @n = n
    @x = []
    (1..n).each do |i|
      @x[i] = nth_root_ceil(n, i)
      break if @x[i] == 2
    end
  end

  def build_dp()
    @dp = Array.new(@x.size)
    (0...@x.size).each {|i| @dp[i] = Array.new(@x[2]+1, 1)}
    @dp[1][@x[2]] = 0 if ((@n - @x[2]) % 2 == 1)
    (3...@x.size).each do |m|
      (@x[m-1]-1).downto(@x[m]).each do |x|
        (m-1).downto(1).each do |y|
          @dp[y][x] = 0 if (@dp[y+1][x] == 1 and @dp[y][x+1] == 1)
        end
      end
    end
  end

  def solve_boundary_case(b)
    result = -1
    (b...@x.size).each do |i|
      if @dp[i][2] == 0
        result = (i - b) % 2 == 0 ? 1 : 0
        break
      end
    end
    return result
  end

  def solve(a, b)
    if (a > @x[2])
      result = (@n - a) % 2 == 1 ? 0 : 1
    elsif (a > 1)
      result = @dp[b][a]
    else
      result = solve_boundary_case(b)
    end
    return result
  end

  def print()
    (@dp.size-1).downto(1).each do |i|
      (2...@dp[i].size).each do |j|
        printf("%d ", @dp[i][j])
      end
      printf("\n")
    end
  end
end

a, b, n = gets.split.map(&:to_i)
# n = 2, a has to be 1, it's a draw
if n == 2
  puts "Missing"
  exit
end
solution = Solution.new(n)
solution.build_dp
answers = %w[Stas Masha Missing]
puts answers[solution.solve(a, b)]


$MAXN = 2222;
$P = Integer(1e9 + 7);

def powmod(a,b,c)
  r=1;
  while b != 0
    if( (b&1) == 1 )
      r = r * a % c;
    end
    a = a * a % c;
    b /= 2;
  end
  return r;
end

def inv(a, b )
  return powmod( a, b - 2 , b ) ;
end

def facmod(n)
  r=1;
  (1..n).each{ |i|
    r = r * i % $P;
  }
  return r;
end

def C(n,m)
  if(m == 0)
    return 1;
  end;
  if(m>n)
    return 0;
  end;
  return (facmod(n) * inv( facmod(m), $P) * inv( facmod(n-m), $P ) ) % $P ;
end

n, m, k = gets().split(" ").map(&:to_i);
printf "%d\n", (C(n-1,2*k) * C(m-1,2*k) )% $P;


def pow(a, b, mod)
  ret = 1
  while b > 0
    if (b & 1 == 1)
      ret = ret * a % mod
    end
    b = b / 2
    a = a * a % mod
  end
  return ret
end
(n, m, k) = gets.chomp.split.map(&:to_i)
mod = 10**9 + 7
ans = pow(pow(2, n - 1, mod), m - 1, mod)
if (n % 2 != m % 2 and k == -1)
  ans = 0
end
print ans


def power(x, y, z)
	number = 1
	while (y > 0) do
		if (y%2==1)
			number = number * x % z
		end
		y >>= 1
		x = x * x % z
	end
	return number
end

n, m, k = gets.split.map(&:to_i)
if((n + m) % 2 == 1 && k == -1)
    p 0
else
    md = 1000000007
    p power(2, (n-1)*(m-1), md)
end


n, k = gets.split.map(&:to_i)
s = gets.chomp

l = 0;

for i in 0..(n-1)
  c = 0;
  j = i;
  while j < n && s[j] == 'N' do
   c = c + 1;
   j = j + 1;
  end
  if (l < c)
  	l = c;
  end
end

ok = false

for i in 0..(n-1)
  if ((i == 0 || s[i - 1] != 'N') && (i + k) <= n)
  	oo = true;
  	for j in 0..(k-1)
  		if (s[i+j] == 'Y')
  			oo = false;
  		end
  	end
  	if (!(i + k == n || s[i + k] != 'N'))
  		oo = false;
  	end
  	if (oo)
  		ok = true;
  	end
  end
end

if (ok && l <= k)
	puts "YES"
else
	puts "NO"
end


# frozen_string_literal: true

def dist(i, j, n)
  if j == i
    n
  else
    j > i ? j - i : n - i + j
  end
end

n, m = readline.strip.split.map(&:to_i)
l = readline.strip.split.map { |c| c.to_i - 1 }

vis = Array.new(n) { false }
used = Array.new(n + 1) { false }

a = Array.new(n) { -1 }

ok = true
(0...m - 1).each do |i|
  if vis[l[i]]
    ok &&= dist(l[i], l[i + 1], n) == a[l[i]]
  else
    vis[l[i]] = true

    d = dist(l[i], l[i + 1], n)

    ok &&= !used[d]

    used[d] = true

    a[l[i]] = d
  end
end

i = 1
(0...n).each do |j|
  next unless a[j] < 0

  i += 1 while used[i]
  a[j] = i
  used[i] = true
  i += 1
end

if !ok
  puts -1
else
  puts a.map(&:to_s).join(' ')
end


def main
  w, m = gets.split.map(&:to_i)
  return true if w == 2
  ma = []
  while m > 0
    ma << (m % w)
    m /= w
  end
  i = 0
  while i < ma.size
    if ma[i] >= w - 1
      ma[i] = 0
      ma[i + 1] = 0 if ma[i + 1].nil?
      ma[i + 1] += 1
    elsif ma[i] >= 2
      return false
    end
    i += 1
  end
  true
end

puts (main ? "YES" : "NO")


def pred(x, n, m)
    (n-m+x*m)-((m+x)*(m+x+1)-m*(m+1))/2 <= 0;
end
input = gets.chomp.split(' ').map(&:to_i)
n = input[0];
m = input[1];
if m > n
  puts n;
else
  lo = 0
  hi = n;
  while lo < hi
	mid = lo+(hi-lo)/2;
	if pred(mid, n, m)
	  hi = mid;
	else
	  lo = mid+1
	end
  end
  puts m+hi;
end


n, m = gets.split.map(&:to_i)
# p gets.split.map(&:to_i)
def f(day, n, m)
  minus = (m + 1 + day) * (day - m) / 2
  plus = (day - m - 1) * m
  return n - minus + plus <= 0
end

if(m >= n)
  puts n
else

  l = m
  r = 1000000000000000000
  while(r - l > 1)
    mid = (r + l) / 2
    if(f(mid, n, m))
      r = mid
    else
      l = mid
    end

  end

  while(r >= l)
    if(f(r, n, m))
      r -= 1
    else
      r += 1
      break
    end
  end

  puts(r)
end


n, s = gets.split.map{|x| x.to_i}
ncoins = 0
n.downto(1) do |i|
    ncoins += s/i
    s %= i
end
puts ncoins


n, s = gets.split.map(&:to_f)
puts (s / n).ceil.to_i


# Created by Alex
n, k, x = gets.split.map {|i| i.to_i}
a = gets.split.map {|i| i.to_i}
max = 0
maxi = -1
for i in 0...n - 1
  #print a[i].to_s + ' '
  if a[i] != x || a[i + 1] != x
    next
  end
  l = i - 1
  r = i + 2
  cnt = 3
  while l >= 0 && r < n && a[l] == a[r]
    p1 = l
    while p1 > 0 && a[p1 - 1] == a[r]
      p1 -= 1
    end
    p2 = r
    while p2 < n - 1 && a[p2 + 1] == a[l]
      p2 += 1
    end
    if l - p1 + p2 - r + 2 < 3
      break
    end
    cnt += l - p1 + p2 - r + 2
    l = p1 - 1
    r = p2 + 1
  end
  if cnt >= max
    #print ' (' + cnt.to_s + ' ' + p1.to_s + ' ' + p2.to_s + ') '
    max = cnt
    maxi = i
  end
end
if max < 3
  max = 1
end
puts max - 1


n, k, x = $stdin.gets.chomp.split(' ').map { |v| v.to_i }
a = $stdin.gets.chomp.split(' ').map { |v| v.to_i }

class Solver
  def initialize(n, k, x, a)
    @n = n
    @k = k
    @x = x
    @a = a
  end

  def cnt(l, r)
    ans = 0
    flag = false
    while true do
      sum = 0
      while l>-1 && @a[l] == @a[r]
        l -= 1
        sum += 1
      end
      l += 1
      while r < @n && @a[l] == @a[r]
        r += 1
        sum += 1
      end
      r -= 1
      return ans if flag && sum < 3
      flag = 1
      ans = [ans, r - l + 1].max
      l -= 1
      r += 1
      return ans if l <0 || r >= @n || @a[l] != @a[r]
    end
  end
end

solver = Solver.new(n, k, x, a)
ans = 0
for i in 0..n-1
  ans = [ans, solver.cnt(i, i+1)].max if a[i] == a[i+1] && a[i] == x
end

puts ans


N = gets.to_i
puts (1..((N+1)/2)).bsearch{|k|
    n = N
    sum1 = 0
    sum2 = 0
    while n > 0
        d1 = k < n ? k : n
        sum1 += d1
        n -= d1
        d2 = n/10
        sum2 += d2
        n -= d2
    end
    sum1 * 2 >= N
}


n = gets.strip.to_i
# n, k = gets.strip.split.map(&:to_i)
atleast_half = n/2
atleast_half += (n%2)

def get_eaten(n, k)
  eaten = 0
  rem = n
  # puts rem if @d1
  # puts "---" if @d1
  # puts eaten
  while rem > 0 do
    # puts rem if @dbg
    if rem >= k
      eaten += k
      rem -= k
    else
      eaten += rem
      rem = 0
    end
    # puts rem if @d1
    # rem -= (0.1 * rem).floor
    rem -= (rem/10)
    # puts rem if @d1
    # puts eaten if @d1
    # puts "eod" if @d1
    # puts eaten
  end
  eaten
end

# puts get_eaten(n, k)

lo = 1
hi = n
k = (lo  + hi)/2
while lo+1 < hi do
  k = (lo + hi) >> 1
  # puts "#{lo}:#{k}:#{hi}"
  if get_eaten(n, k) >= atleast_half
    hi = k
  else
    lo = k
  end
end
# while lo <= hi do
#   k = (lo  + hi)/2
#   # puts "#{lo}:#{k}:#{hi}"
#   if get_eaten(n, k) >= (n/2.0).ceil
#     hi = k-1
#   else
#     lo = k+1
#   end
# end
# puts k
# puts "..."
mk = [1, k-1000].max
(mk).upto(k+1000).each do |ki|
  # puts ki
  # @dbg = true
  if get_eaten(n, ki) >= atleast_half
    # puts get_eaten(n, ki)
    puts ki
    break
  end
end
# puts k

# # puts "---"
# 1.upto(n).each do |ki|
#   if get_eaten(n, ki) >= (n/2.0).ceil
#     # @d1 = true
#     # puts get_eaten(n, ki)
#     puts ki
#     break
#   end
# end


module Memoizer
  def memoize(methodname)
    raise ArgumentError, "statefull method [#{methodname}]should not be memoized" if methodname.match(/\!\z/)
    unbound_method = instance_method(methodname)
    raise ArgumentError, "method [#{methodname}] with argument cannot be memoized" if unbound_method.arity > 0
    memoized_object_name = "@_memoized_#{methodname}"
    define_method(methodname) do
      if(instance_variable_get(memoized_object_name).nil?)
        instance_variable_set(memoized_object_name, unbound_method.bind(self).call)
      end
      instance_variable_get(memoized_object_name)
    end
  end
end

class Problem
  attr_reader(:e, :o)
  def initialize(e, o)
    @e = e
    @o = o
  end

  def ans
    return false if (e == 0 and o == 0)
    (e - o).abs <= 1
  end

  def out
    return "YES" if ans
    return "NO"
  end
end

e, o = gets.split(" ").map(&:to_i)
puts Problem.new(e, o).out


a, b = gets.strip.split.map(&:to_i)
if (a > 0 || b > 0) && (a == b || (a - b).abs == 1)
    puts "YES"
else
    puts "NO"
end


# 4
#0123
# 5
class Dice
    def initialize(arr)
        @arr = arr.dup
    end

    def rot_x
        @arr[0], @arr[1..3] = @arr[3], @arr[0..2]
    end

    def rot_y
        @arr[4], @arr[2], @arr[5], @arr[0] = @arr[2], @arr[5], @arr[0], @arr[4]
    end

    def rot_z
        @arr[4], @arr[1], @arr[5], @arr[3] = @arr[1], @arr[5], @arr[3], @arr[4]
    end

    def to_a
        @arr.dup
    end
end

arr = gets.chomp.each_char.sort
dict = []
cnt = 0
arr.permutation do |perm|
    dice = Dice.new(perm)
    ok = true
    catch(:check) {
        6.times do |t|
            4.times do
                if dict.index(dice.to_a)
                    ok = false
                    throw :check
                end
                dice.rot_x
            end
            if t == 3
                dice.rot_y
                dice.rot_z
            elsif t == 4
                dice.rot_z
                dice.rot_z
            else
                dice.rot_y
            end
        end
    }
    if ok
        cnt += 1
        dict << dice.to_a
    end
end

p cnt


#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.chomp end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end
############################################################

n = gi

if n == 0
    puts "0 0"
    exit
end

m = n - 1

k = 9 + 4 * 3 * m
d = (- 3 + (k ** 0.5)) / 6
d = d.floor

while d * (d + 1) * 3 < n
    d += 1
end

f = d * (d - 1) * 3

case
when (n - f) <= d
    e = n - f
    x = d * 2 - e
    y = e * 2
when (n - f) <= 2 * d
    e = (n - f) - d
    x = d - e * 2
    y = d * 2
when (n - f) <= 3 * d
    e = (n - f) - d * 2
    x = -d - e
    y = d * 2 - e * 2
when (n - f) <= 4 * d
    e = (n - f) - d * 3
    x = - d * 2 + e
    y = -e * 2
when (n - f) <= 5 * d
    e = (n - f) - d * 4
    x = -d + 2 * e
    y = -d * 2
else
    e = (n - f) - d * 5
    x = d + e
    y = -d * 2 + e * 2
end

puts "%d %d" % [x, y]


def sum(x)
    x.split('').map(&:to_i).inject(0) { |sum, x| sum + x }
end

x = gets.to_i

sx = x.to_s

ans = x
sans = sx

sx.length.times do |i|
    sy = sx[0...i] + (sx[i].to_i - 1).to_s + '9' * (sx.length - i - 1)

    y = sy.to_i
    sy = y.to_s

    if (sum(sy) > sum(sans) || (sum(sy) == sum(sans) && y > ans))
        ans = y
        sans = sy
    end
end

puts sans


x,y = gets.split.map(&:to_i)
prime = []
k = (y**0.5).to_i
k = x if k > x
a = Array.new(k+1,1)
i = 2
while i <= k
  if a[i] == 1
    prime << i
    (k/i).times do |j|
      a[i*j] = 0
    end
  end
  i += 1
end
isprime = false
while !isprime && x < y
  isprime = true
  prime.length.times do |i|
    isprime = false if y % prime[i] == 0
  end
  if isprime
    ans = y
  end
  y -= 1
end
ans = -1 if !isprime
puts ans


Mod = (10**9)+7

def solve n,m,k
  if n == k
    ans = 1
    for i in 0 ... ((n+1)/2)
      ans = ans * m
      ans = ans%Mod
    end
    ans
  elsif k == 1 || n < k
    ans = 1
    for i in 0 ... n
      ans = ans * m
      ans = ans % Mod
    end
    ans
  elsif k%2 == 1
    (m*m)%Mod
  elsif k%2 == 0
    m
  end
end

while line = gets
  a = line.split
  puts solve(a[0].to_i,a[1].to_i,a[2].to_i)
end


def invalid?(s)
  (s[0] == "0" and s.size > 1) or s.to_i > 10**6
end

ARGF.each_line do |line|
  max = -1
  digit = line.chomp!.size
  1.upto(digit) do |i|
    1.upto(digit - i - 1) do |j|
      k = digit - i - j

      a = line[0...i]
      b = line[i...(i + j)]
      c = line[(i + j)...digit]
      if invalid?(a) or invalid?(b) or invalid?(c)
        next
      end
      point = a.to_i + b.to_i + c.to_i
      max = point if max < point
    end
  end
  puts max
end


def bad(ss)
    return true if ss[0] == '0' && ss.length > 1
    if ss.length > 6
      return true if ss.to_i > 1000000
    end
    return false
end

s = gets.chomp
n = s.length-2
wkmax = -1
for i in 0..n
  s1 = s.slice(0..i)
  next if bad(s1)
  s1n = s1.to_i
  for j in i+1..n
    s2 = s.slice(i+1..j)
    next if bad(s2)
    s3 = s.slice(j+1..n+1)
    next if bad(s3)
    wk = s1n + s2.to_i + s3.to_i
    if wk > wkmax
      wkmax = wk
    end
  end
end

puts wkmax


s = gets.chomp
ans = -1
s.size.times do |i|
  (i + 1).upto(s.size - 2) do |j|
    a, b, c = s[0..i], s[(i + 1)..j], s[(j + 1)..-1]
    next if a.size > 1 and a[0] == ?0
    next if b.size > 1 and b[0] == ?0
    next if c.size > 1 and c[0] == ?0
    next if a.to_i > 1e6.to_i
    next if b.to_i > 1e6.to_i
    next if c.to_i > 1e6.to_i
    ans = [ans, a.to_i + b.to_i + c.to_i].max
  end
end
puts ans


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
tie=gi
sca=gi
vest=gi
jack=gi
p1=gi
p2=gi

c1=0
c2=0
if p1<p2
  c2 = [sca,vest,jack].min
  jack-=c2
  c1=min(tie,jack)
else
  c1 = [tie,jack].min
  jack-=c1
  c2=[sca,vest,jack].min
end
puts c1*p1 + c2*p2


a, b, c = gets.split.map &:to_i

def solve(a,b,c)
  ar = Array.new(1000001) { 0 }
  ans = 0
  mod = 1073741824
  for i in 1..a do
    for j in 1..b do
      for k in 1..c do
        d = i*j*k
        ans += ret_div(ar, d, mod)
      end
    end
  end
  puts ans
end

def ret_div(ar, el, mod)
  return ar[el] if ar[el]>0
  j = 1
  div = 0
  while j*j<=el do
    if el%j == 0
      div+=2
    end
    j+=1

  end
  div-=1 if (j-1)*(j-1) == el
  ar[el] = div % mod
  ar[el]
end

solve a, b ,c


n=gets.to_i
s=gets.chomp
a, b, c=[], 0, -1
s.chars do |e|
  if e.to_i==0
    if c==0
      a<< 0
    else
      a<< b
      b=0
    end
  else
    b+=1
  end
  c=e.to_i
end

puts (a<<b).join


#!/usr/bin/ruby
gets
prev='0'
x=0
gets.chomp.chars{|c|
	if c=='0'
		if prev=='0'
			print 0
		else
			print x
			x=0
		end
	else
		x+=1
	end
	prev=c
}
print x


a, b = STDIN.readline.split.map {|s| s.to_i }
puts ((a*a + a)*b/2 + a)*(b*b-b)/2 % (10**9 + 7)


#!/usr/bin/ruby
def f(m)
	r=1
	r*=2 while r*2<=m
	r
end
N,B,P=gets.split.map(&:to_i)
r=0
n=N
while n>1
	k=f(n)
	r+=k*B+k/2
	n=n-k/2
end
puts '%d %d'%[r,N*P]


n=gets.to_i
ac=Array.new(n,0)
now=0
for i in 0..2*n
        now=(now+i)%n
        ac[now]=1
end
k=1
for i in 0..n-1
        k&=ac[i]
end
if k==1 then
        puts "YES"
else
        puts "NO"
end


n=gets.to_i
checked=Array.new(n,false);
s=0
n.times do |t|
  if checked[s] then puts "NO"; exit; end
  checked[s]=true;
  s=(s+t+1)%n
end
puts "YES"


n = gets.to_i
while n % 2 == 0 do n /= 2 end
puts n == 1 ? "YES" : "NO"


arr = gets.split.map &:to_i
$n, x0, y0, dx, dy, t = arr

def mod(a)
  (a - 1) % $n + 1
end

dx, dy = mod(dx), mod(dy)

def zmul(a, b)
  ans = 0
  6.times do |i|
    ans += mod(a[i] * b[i])
    ans -= $n if ans > $n
  end
  ans
end

class Matrix
  def initialize(n)
    @mat = Array.new(n) do |i|
      Array.new(n) { |j| i == j ? 1 : 0 }
    end
  end

  def [](index)
    @mat[index]
  end

  def []=(index, val)
    @mat[index] = val
  end

  def mul(rhs)
    ans = Matrix.new(6)
    6.times do |i|
      6.times do |j|
        ans[i][j] = 0
        6.times do |k|
          ans[i][j] += mod(@mat[i][k] * rhs[k][j])
          ans[i][j] -= $n if ans[i][j] > $n
        end
      end
    end
    ans
  end

  def pow(e)
    ans = Matrix.new(6)
    tmp = Matrix.new(6)
    6.times { |i| tmp[i] = Array.new(6) { |j| @mat[i][j] } }
    while e > 0
      ans = ans.mul(tmp) if (e & 1) == 1
      tmp = tmp.mul(tmp) if (e >>= 1) > 0
    end
    ans
  end
end

mat = Matrix.new(6)
mat[0] = [2, 1, 1, 0, 1, 0]
mat[1] = [1, 2, 0, 1, 1, 0]
mat[2] = [1, 1, 1, 0, 1, 0]
mat[3] = [1, 1, 0, 1, 1, 0]
mat[4] = [0, 0, 0, 0, 1, 1]
mat[5] = [0, 0, 0, 0, 0, 1]

mat = mat.pow(t)

arr.shift
arr[-1] = 0
arr << 1
puts "#{zmul(mat[0], arr)} #{zmul(mat[1], arr)}"


#!/usr/bin/ruby

x, y, z = STDIN.readline.split.map {|s| s.to_i }
x1, y1, z1 = STDIN.readline.split.map {|s| s.to_i }
a1, a2, a3, a4, a5, a6 = STDIN.readline.split.map {|s| s.to_i }
sum = 0
if z < 0
	sum += a3
elsif z > z1
	sum += a4
end
if x < 0
	sum += a5
elsif x > x1
	sum += a6
end
if y < 0
	sum += a1
elsif y > y1
	sum += a2
end
puts sum


#!/usr/bin/env ruby

first_line = gets.split(" ")
vasya = {"x" => first_line[0].to_i, "y" => first_line[1].to_i, "z" => first_line[2].to_i}

second_line = gets.split(" ")
box = {"x" => second_line[0].to_i, "y" => second_line[1].to_i, "z" => second_line[2].to_i}

array = gets.split(" ")

array[5] = 0 if vasya["x"] <= box["x"]
array[4] = 0 if vasya["x"] >= 0

array[1] = 0 if vasya["y"] <= box["y"]
array[0] = 0 if vasya["y"] >= 0

array[3] = 0 if vasya["z"] <= box["z"]
array[2] = 0 if vasya["z"] >= 0

sum = 0
array.each do |el|
	sum += el.to_i
end

puts sum


class Integer
  def divisers
    n=self
    res = []
    res << 1
    i=1
    while i*i <= n
      if n%i==0
        res << i
        res << n/i if i*i!=n
      end
      i+=1
    end
    res.sort!
  end
end

def solve n,m,k
  ds = n.divisers
  if n-1+m-1<k
    return -1
  end
  ans=0
  for w1 in ds
    k1=n/w1-1
    k2=k-k1
    if 0<=k1 && k1<=k && 0<=k2 && k2<=k
      w2=m/(k2+1)
      ans = [ans, w1*w2].max
    end
  end
  ans
end

n,m,k = gets.chomp.split.map{|i|i.to_i}
puts [solve(n,m,k),solve(m,n,k)].max


#!/usr/bin/ruby

def solve(h, w, k)
	return -1 if k > h+w-2
	strips = [h, k+1].min
	across = [1, k-strips+2].max
	#puts 'h = %d, w = %d, k = %d, strips = %d, across = %d' % [h, w, k,
	#		strips, across]
	return (h/strips).floor * (w/across).floor
end

n, m, k = STDIN.readline.split.collect {|s| s.to_i }
a, b = [n, m].max, [n, m].min
puts [solve(a, b, k), solve(b, a, k)].max


n, m, k = gets.split.collect{|i| i.to_i}
n, m = m, n if n > m
if k < n
    ans = [(n/1)*(m/(k+1)), (n/(k+1))*(m/1)].max
elsif k >= n and k < m
    ans = (n/1)*(m/(k+1))
elsif k >= m and k <= m+n-2
    ans = (n/(k+2-m))*(m/m)
else
    ans = -1
end
puts ans


mas = gets.split(" ")
n = mas[0].to_i
x1 = mas[1].to_i
y1 = mas[2].to_i
x2 = mas[3].to_i
y2 = mas[4].to_i

if x1 == x2 && (x1 == 0 || x1 == n)
  puts (y2-y1).abs
  exit
end
if y1 == y2 && (y1 == 0 || y1 == n)
  puts (x2-x1).abs
  exit
end

if (x1-x2).abs == n
  puts [(n-y1).abs+(n-y2).abs, (y1+y2).abs].min+n
  exit
end
if (y1-y2).abs == n
  puts [(n-x1).abs+(n-x2).abs, (x1+x2).abs].min+n
  exit
end

puts ((x2-x1).abs+(y2-y1).abs).to_s


b = gets.to_i
g = gets.to_i
n = gets.to_i

ans = 0
(0..n).each do |bb|
  gg = n - bb
  ans += 1 if bb <= b && gg <= g
end
puts ans


b = gets.to_i
g = gets.to_i
n = gets.to_i

cnt=0

if b>=n && g>=n
  cnt = n+1
elsif b<n && g<n
  diff = n-[b,g].max
  cnt = [b,g].min-diff+1
  cnt = 0 if cnt<0
else
  cnt = [b,g].min+1
end

puts cnt


k, n = gets.chomp.split.map{|i| i.to_i}
m, ans, offset, flag = 2 ** k, 0, 0, false
while m != 1
  m /= 2
  if flag ^ (offset + m >= n)
    ans += 1
    flag = !flag
    offset += m unless flag
  else
    ans += 2 * m
    offset += m unless flag
  end
end
puts ans


h, n = gets.split.map &:to_i

nn = 2**h - 1 + n
$turn = [0] * h
levell = h - 1
while levell >= 0
    $turn[levell] = nn.odd? ? 1 : 0
    nn /= 2
    levell -= 1
end

def treesize(h)
    return 2**(h + 1) - 1
end

def func(level, k, symb, h)
    # symb == 0 means 'L', 1 means 'R'

    #puts "in #{k}"

    return 0 if level == h

    rec = func(level + 1, k * 2 + $turn[level], 1 - $turn[level], h)

    if symb == $turn[level]

        return  1 + rec
    else

        return 1 + rec + treesize(h - level - 1)
    end
end

p func(0, 1, 0, h)


def stop
	puts "stop"
	sleep(2)
end
def readarr
	gets.split.map{|i| i.to_i}
end
def write(foo)
	if foo.is_a?(Array)
		puts foo.map{|i| i.to_s}.join(" ")
	else
		puts foo
	end
end
h,n = readarr
ans = 0
while(h>0)
	a = 1<<(h-1)
	if (n>a)
		n-=a
		ans+=a<<1
	else
		n = a+1-n
		ans+=1
	end
	h-=1
end
puts ans
#sleep(300)


def count0(x)
	ans, mul = 0, 5
	ans, mul = ans + x / mul, mul * 5 while mul <= x
	ans
end

n = gets.strip.to_i
beg = 0
beg += 1 while count0(beg) < n
ans = []
while count0(beg) == n
	ans.push(beg)
	beg += 1
end
puts ans.length
puts ans.map(&:to_s).join(' ')


#!/usr/bin/ruby
N=gets.to_i
A=Enumerator.new{|y|
	s=0
	(1..1/0.0).each{|n|
		i=n
		r=0
		while i%5==0
			r+=1
			i/=5
		end
		s+=r
		y<<[s,n]
	}
}.lazy.drop_while{|e,n|e<N}.take_while{|e,n|e==N}.map(&:last).to_a
p A.size
puts A*' '


l,d,v,g,r = gets.split(" ").map(&:to_i)

till_a = d/v.to_f
# puts "#{till_a}"
total_interval = g + r
# puts "total_interval#{till_a % total_interval}"
extra_time = (till_a % total_interval) >= g ? (total_interval - (till_a % total_interval)) : 0
# puts "#{extra_time}"
a_to_b = (l-d) / v.to_f
# puts "#{a_to_b}"

puts till_a + extra_time + a_to_b


#!/usr/bin/env ruby

n = gets.to_i

l, r = 0, n
while l != r
  m = (l + r + 1) / 2
  need = 3 * m * (m + 1) / 2 - m
  if need <= n
    l = m
  else
    r = m - 1
  end
end

l -= 1 while l > 0 && [0, 2, 1][l % 3] != n % 3
puts (l + 2) / 3


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

p = q = 0
a.each_with_index do |v, i|
  dp = (v - (i*2+1)).abs
  dq = (v - (i*2+2)).abs
  p += dp
  q += dq

end

puts p < q ? p : q


n=gets.to_i
a=gets.split.map(&:to_i).sort
p [1,2].map{|s|a.zip(s.step(n,2).to_a).map{|x,i|(x-i).abs}.reduce :+}.min


#!/usr/bin/ruby
def zeller(_y,m,d)
	m+=1
	if m<4 then _y-=1;m+=12 end
	y=_y/100;z=_y%100
	(5*y+z+y/4+z/4+13*m/5+d-1)%7
end
def leap(y)
	return true if y%400==0
	return false if y%100==0
	y%4==0
end
n=gets.to_i
a=[zeller(n,1,1),leap(n)]
p (n+1..1/0.0).find{|i|a==[zeller(i,1,1),leap(i)]}


def subseq(a, b, i, j)
  return true if i.zero?
  return false if j.zero?

  return subseq(a, b, i - 1, j - 1) if a[i-1] == b[j-1]
  return subseq(a, b, i, j - 1)
end

def substrs(s)
  len = s.size
  (0..len).map { |l| (0..(len-l)).map { |x| (x..(x + l)) } }.flatten(1)
end

def solve(s, t)
  subs = substrs(s)
  mx = 0
  subs.each do |sub|
    ss = s.dup
    ss[sub] = ''
    next if ss.size.zero? or sub.any? { |r| r >= s.size }
    mx = [mx, sub.size].max if subseq(t, ss, t.size, ss.size)
  end
  mx
end

s = gets.strip
t = gets.strip
puts solve(s, t)


s = gets
t = gets
a = [0]
n = s.size
(0...n).each do |i|
  (i...n).each do |j|
    cur = 0
    (0...n).each do |k|
      next if i <= k and k <= j
      cur += 1 if cur < t.size and t[cur] == s[k]
    end
    a << j - i + 1 if cur == t.size
  end
end
puts a.max


n = gets.to_i
ret = 0
loop do
  break if n == 0
  max_digit = 0
  temp = n
  while temp != 0
    max_digit = [temp % 10, max_digit].max
    temp /= 10
  end
  n -= max_digit
  ret += 1
end
puts ret


Teams = ["BERLAND"]
def teamn(name)
  Teams << name if Teams.index(name).nil?
  Teams.index(name)
end
game = [1,0,0,0]
point = [3,0,0,0]
diff = [0,0,0,0]
earn = [0,0,0,0]
5.times do |i|
  input = gets.split(" ")
  teama = teamn(input[0])
  teamb = teamn(input[1])
  score = input[2].split(":").map{|x| x.to_i}
  if score[0]>score[1]
    point[teama] += 3
  elsif score[0]==score[1]
    point[teama] += 1
    point[teamb] += 1
  else
    point[teamb] += 3
  end
  game[teama] += 1
  game[teamb] += 1
  diff[teama] += score[0] - score[1]
  diff[teamb] += score[1] - score[0]
  earn[teama] += score[0]
  earn[teamb] += score[1]
end
opponent = game.index(2)
found = false
fi = nil
fj = nil
1.upto(50) do |i|
  diff[0] += i
  diff[opponent] -= i
  i.upto(50) do |j|
    earn[0] += j
    earn[opponent] += j-i
    rank = (0..3).sort{|a,b| (point[a]<=>point[b])*8+(diff[a]<=>diff[b])*4+(earn[a]<=>earn[b])*2-(Teams[a]<=>Teams[b])}.index(0)
    if rank > 1
      found = true
      fj = j
      break
    end
    earn[0] -= j
    earn[opponent] -= j-i
  end
  if found
    fi = i
    break
  end
  diff[0] -= i
  diff[opponent] += i
end
if found
  puts fj.to_s+":"+(fj-fi).to_s
else
  puts "IMPOSSIBLE"
end


#ruby 2.4.1

require "prime"
t = gets.to_i
t.times do |a, b|
	flag = false
	nums = ""
	nums = gets
	a1, b1 = nums.split(' ')
	a = a1.to_i
	b = b1.to_i
	if Prime.prime?(a + b) == true && (a + b) * (a - b) == (a + b)
		puts "YES"
	else
		puts "NO"
	end
end


# https://codeforces.com/problemset/problem/1242/A

require 'set'

n = gets.to_i

dividers = Set.new []

i = 2
size = n

while i * i <= n
  if n % i == 0
    dividers.add(i)
    n /= i
    i -= 1
  end
  i += 1
end

dividers.add(n) if n > 1

arr = []
dividers.each { |num| arr << num }

if arr[0] == size
  puts size
elsif arr.size == 1
  puts arr[0]
else
  puts 1
end


#!/usr/bin/ruby
k,a,b=gets.split.map(&:to_i)
if a<k&&b<k
	p -1
elsif (0<a&&a%k==0)||(0<b&&b%k==0)
	p a/k+b/k
elsif (a%k!=0&&b<k) || (b%k!=0&&a<k)
	p -1
else
	p a/k+b/k
end


k, a, b = gets.strip.split.map(&:to_i)
sets = (a / k) + (b / k)
sets = -1 if a / k == 0 && b % k != 0
sets = -1 if b / k == 0 && a % k != 0
puts sets


#很多来回：赢了一个来回的1分，输了没有分！
#赢了k分就赢了1局
#最大的局数
#第一局：如果misha赢，则可能的比分是
#k:0
#k:1
#...
#k:k-1
#如果vanya赢，
#0:k
#1:k
#...
#k-1:k
#a/k表示misha最多可以赢多少局！
#b/k表示vanya最多可以赢多少局！
kab=gets.chomp.split.map{ |x| x.to_i }
k=kab[0]
a=kab[1]
b=kab[2]
win_a=a/k
win_b=b/k
max_b=win_a*(k-1)+k*win_b
max_a=win_b*(k-1)+k*win_a
if a/k+b/k==0
	puts -1
elsif a>max_a || b>max_b
	puts -1
else
	puts win_a+win_b
end
#情况探讨是：如果某个人赢了x场比赛，则另一个人的得分是0～x*(k-1)
#
#win_a=a/k
#win_a_max_b=(a/k)*(k-1)
#win_b=b/k
#win_b_max_a=(b/k)*(k-1)


nm = gets.chomp.split(" ").map(&:to_i)
n = nm[0]
m = nm[1]

def findfor(x,y,k)
  count = 0
  (1..x).each do |a|
    (1..y).each do |b|
      if (a**2+b**2)%k==0
        count += 1
      end
    end
  end
  return count
end

if n<m
  puts findfor(n,n,m)
  exit
else
  form = findfor(m,m,m)
  nbym = n/m
  nmodm = n%m

  ans = form*(nbym**2)
  ans += 2*nbym*findfor(m,nmodm,m)
  ans += findfor(nmodm,nmodm,m)
end
puts ans


class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(number1, number2)
    if vertices[number1].nil? then
      vertices[number1] = List.new(number2, nil)
    else
      vertices[number1] = List.new(number2, vertices[number1])
    end
  end

  def print_edges
    vertices.each_with_index do |vertex, index|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{index} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

end

def dfs(graph, number, visited)
  visited[number] = true
  next_vert = graph.vertices[number]
  # puts "Processing number: #{number}"
  # p next_vert
  stack = []
  # return [number] if next_vert.nil?
  while !next_vert.nil? do
    # puts "#{next_vert.element} is not nil. Process it"
    if !visited[next_vert.element]
      # puts "#{next_vert.element}'s is not visited. Processing it"
      new_number = dfs(graph, next_vert.element, visited)
      # puts new_number
      stack = new_number + stack
    end
    next_vert = next_vert.next_elem
  end
  # p [number] + stack
  return [number] + stack
end

def reverse_graph(graph)
  reversed_graph = Graph.new([])
  graph.vertices.each_with_index do |vertex, index|
    next_vertex = vertex
    while !next_vertex.nil? do
      reversed_graph.add_edge(next_vertex.element, index)
      next_vertex = next_vertex.next_elem
    end
  end
  reversed_graph
end

intersections = Graph.new([])
n, m = gets.strip.split(" ").map(&:to_i)
horizontal = gets.strip
vertical = gets.strip
visited = Array.new(m*n, false)

n.times do |i|
  # puts i
  # puts horizontal[i]
  if horizontal[i] == ">"
    (m - 1).times do |j|
      # puts j
      intersections.add_edge(i*m + j, i*m + j + 1)
    end
  else
    (m - 1).times do |j|
      intersections.add_edge(i*m + j + 1, i*m + j)
    end
  end
end
m.times do |i|
  if vertical[i] == "v"
    (n - 1).times do |j|
      intersections.add_edge(j*m + i, (j+1)*m + i)
    end
  else
    (n - 1).times do |j|
      intersections.add_edge((j+1)*m + i, j*m + i)
    end
  end
end
# intersections.print_edges
stack = []
(n*m).times do |i|
  if !visited[i]
    stack = dfs(intersections, i, visited) + stack
    # p stack
  end
end
# p intersections.vertices.count
answer = 'YES'
reversed_graph = reverse_graph(intersections)
# reversed_graph.print_edges
# p visited
visited = Array.new(m*n, false)
# (m*n).times do |i|
#   visited[i] = false
# end
# p visited
# p stack
# p intersections.vertices[stack.first]
dfs(reversed_graph, stack.first, visited)
# p visited
if visited.any?{|visited| !visited}
  answer = 'NO'
end
puts answer


n, m = gets.chomp.split.map(&:to_i)

def fromdec(n)
  return '0' if n == 0
  rtn = ""
  while 0 < n.to_i
    rtn = (n.to_i % 7).to_s + rtn
    n = (n.to_i / 7).to_s
  end
  return rtn
end

def todec(n)
  rtn = 0
  (0...n.size).each do |i|
    rtn += n[i].to_i * 7 ** (n.size - i - 1)
  end
  return rtn
end

ln = fromdec(n - 1).size
lm = fromdec(m - 1).size

ans = 0
(0...7).to_a.permutation(ln + lm) do |ps|
  pn = (0...ln).inject('') { |str , i| str + ps[i].to_s }
  pm = (ln...ln + lm).inject('') { |str , i| str + ps[i].to_s }
  if todec(pn) < n && todec(pm) < m
    ans = ans + 1
  end
end

puts ans


def cmmdc a,b
  return [a,b].max if a*b==0
  return cmmdc([a,b].min,[a,b].max % [a,b].min)
end

n,m,x,y,a,b = gets.chomp.split(" ").collect {|x| x.to_i}

cmm = cmmdc(a,b)
a /= cmm
b /= cmm

p = n/a
q = m/b

min = [p,q].min

p = min * a
q = min * b

delta_center_x = p/2.0
delta_center_y = q/2.0

c1x =  delta_center_x
c1y =  delta_center_y

c2x = n - delta_center_x
c2y = m - delta_center_y

rx = 0
ry = 0

if x<c1x and y<c1y
  rx = c1x
  ry = c1y
end

if x<c1x and y>c2y
  rx = c1x
  ry = c2y
end

if x>c2x and y<c1y
  rx = c2x
  ry = c1y
end

if x>c2x and y>c2y
  rx = c2x
  ry = c2y
end

if x>=c1x and x<=c2x and y<c1y
  ry = c1y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if x>=c1x and x<=c2x and y>c2y
  ry = c2y
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
end

if y>=c1y and y<=c2y and x<c1x
  rx = c1x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>c2x
  rx = c2x
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

if y>=c1y and y<=c2y and x>=c1x and x<=c2x
  if (c1x-x).ceil == (c1x-x).floor
    rx = x
  else
    rx = x - 0.5
  end
  if (c1y-y).ceil == (c1y-y).floor
    ry = y
  else
    ry = y - 0.5
  end
end

puts [rx-delta_center_x,ry-delta_center_y,rx+delta_center_x,ry+delta_center_y].map {|x| x.to_i}.join(' ')


a,b=gets.chomp.split(' ').map(&:to_i)
tmp=a.gcd(b)
a/=tmp
b/=tmp
ctr=0
while a%2==0
	a/=2
	ctr+=1
end
while b%2==0
	b/=2
	ctr+=1
end
while a%3==0
	a/=3
	ctr+=1
end
while b%3==0
	b/=3
	ctr+=1
end
while a%5==0
	a/=5
	ctr+=1
end
while b%5==0
	b/=5
	ctr+=1
end
if a+b > 2
	puts "-1"
else
	puts ctr
end


/(?<a>[\d]+) (?<b>[\d]+)/ =~ gets.chomp
a = a.to_i
b = b.to_i

def bite(size, mults=[2, 3, 5])	# => [b*2, b*3, b*5, leftovers || 1]
	bitesizes = []
	mults.each do |mult|
		bite = 0
		bite, size = (bite+1), (size/mult) while (size%mult)==0
		bitesizes << bite
	end
	bitesizes << size
	return bitesizes
end

*bitesA, leftA = bite(a)
*bitesB, leftB = bite(b)

if (leftA != leftB)
	puts -1
else
	p (bitesA.zip(bitesB).map{ |bA, bB|
		(bA-bB).abs
	}.reduce(&:+))
end


require 'set'

t = gets
(1..t.to_i).each do |ind|
    n = gets
    i = 1
    n = n.to_i
    st = Set[]
    while i*i <= n
        st << i*i
        i += 1
    end
    i = 1
    while i*i*i <= n
        st << i*i*i
        i += 1
    end
    puts st.size
end


def run target, left, right, result
  if target[0] == 1
    print (target[1] - 1), 'B'
    puts
    return true
  end
  if target[1] == 1
    print (target[0] - 1), 'A'
    puts
    return true
  end
  if target[0].gcd(target[1]) > 1
    puts "Impossible"
    return false
  end
  if (target[0] - target[1]).abs == 1
    result = []
    num  = target[0] > target[1] ? target[1] : target[0]
    char = target[0] > target[1] ? 'B' : 'A'
    print 1, (char == 'A' ? 'B' : 'A')
    if num > 1
      print (num - 1), char
    end
    puts
    return true
  end
  while true
    median = [left[0] + right[0], left[1] + right[1]]

    if fract_compare(median, target) == 0
      if median[0] != target[0]
        puts "Impossible"
        return false
      end
      puts string_compress result
      return true
    end

    if median[0] > target[0] || median[1] > target[1]
      puts "Impossible"
      return false
    end

    if fract_compare(median, target) > 0
      result << 'B'
      right[0] = median[0]
      right[1] = median[1]
    else
      result << 'A'
      left[0] = median[0]
      left[1] = median[1]
    end
  end
end

def fract_compare a, b
  return a[0] * b[1] - a[1] * b[0]
end

def string_compress s
  current_total = 0
  prev = ""
  size = s.size
  result = []
  s.each_with_index do |cur, i|
    if prev == cur
      current_total += 1
    else
      if prev != ""
        result << current_total
        result << prev
      end
      current_total = 1
      prev = cur
    end
    if i == (size - 1)
      result << current_total
      result << prev
    end
  end
  return result.join
end

target = gets.chomp.split(" ").map{ |i| i.to_i }
left = [0, 1]
right = [1, 0]
result = []
run target, left, right, result


n, m, k = gets.split.map(&:to_i)
if k < n
    puts "#{k + 1} 1"
else
    hoge = (m - 1) * 2
    k -= n
    r = n - k / hoge * 2
    tmp = k % hoge
    c = 0
    if tmp < m - 1
        c = tmp + 2
    else
        c = 2 * (m - 1) - tmp + 1
        r -= 1
    end
    puts "#{r} #{c}"
end


MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end
def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
n,m=gsmi

def pow(a,b)
  base = a
  ans=1
  while b>0 do
    d = b/10
    m = b%10
    ans*=(base**m)
    ans%=MOD
    base = (base**10)%MOD
    b=d
  end
  ans
end

p1 = pow(2,m) - 1
ans = pow(p1,n)%MOD

puts ans


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)

p modpow((modpow(2,m) - 1),n)


a,b = gets.chomp.split(' ').map{|x|x.to_i}
h = 0
if b > a
	puts 3*b
	exit
end

if a > b+1
	c = [(a-b)/2, b].min
	h+= c*6
	a = a - 3*c
	b = b - c
end

if b % 2 == 0
	h+=(b/2)*6
	a = a-b
	b = 0
else
	h+=(b/2)*6
	h+=3
	a = a - b
	b = 0
end

if a > 0
	if h%2 ==1
		h+=1
		a-=1
	end
end

h+=2*a

puts h


def read_next_line
  gets.chomp.split.map(&:to_i)
end

def read(range)
  g = []
  range.each {
    g << read_next_line
  }
  g
end

matrix_size = 5

r = (0...matrix_size)
g = read(r)
max = 0
r.to_a.permutation.each { |p|
  happ = 0
  line = r.to_a
  while line.size > 1
    n = line.size
    n -= n % 2
    (0...n).step(2) { |i|
      happ += g[p[line[i]]][p[line[i + 1]]] + g[p[line[i + 1]]][p[line[i]]]
    }
    line.shift
  end
  max = happ if happ > max
}
puts max


n = gets.to_i
ary = gets.strip.split('').map(&:to_i)

if ary.uniq == [0]
  puts "YES"
  exit 0
end

# lim = (n * 9) + 10
lim = ary.inject(:+) - 1

def poss?(ary, ss)
  i=0
  sum = 0
  sz = ary.size
  while i<sz do
    sum += ary[i]
    if sum == ss
      sum = 0
    elsif sum > ss
      return false
    end
    i += 1
  end
  sum == 0
end

1.upto(lim).each do |ss|
  if poss?(ary, ss)
    # puts ss
    puts "YES"
    exit 0
  end
end

puts "NO"


a, b = gets.strip.split.map{|s|s.to_i}
if a == b
  puts 'Equal'
  exit
end
d = a * b / a.gcd(b)
fa = d / a
fb = d / b
a < b ? fa -= 1 : fb -= 1
if fa > fb
  puts 'Dasha'
elsif fa < fb
  puts 'Masha'
else
  puts 'Equal'
end


#!/usr/bin/env ruby

n, l, r = gets.split.map(&:to_i)
#                  0:  1:&1 2:size 3:1_c
one_map = { 1 => [ [1], 1, 1, 1 ],
			0 => [ [0], 0, 1, 0 ] }

stack = []

c = n
while c > 1
  stack << c
  d = c >> 1
  one_map[c] = [d, c&1]
  c = d
end

#p stack

last = one_map[c]
while d = stack.pop
  cur = one_map[d]

  # size
  cur[2] = last[2] * 2 + 1
  # 1_c
  cur[3] = last[3] * 2 + cur[1]

  last = cur
end

c_start = l - 1
c_end = r - 1

def give_one_count(n, c_s, c_e, one_map)
  #p "n:#{n} c_s:#{c_s} c_e:#{c_e} one_map:#{one_map}"
  n_map = one_map[n]

  return n_map[3] if c_s == 0 && (c_e + 1) == n_map[2]

  mid = n >> 1
  mid_size = n_map[2] >> 1

  count = 0

  if c_s < mid_size
	#p "#{n} a"
    count += give_one_count(mid, c_s, [c_e, mid_size - 1].min, one_map)
  end

  if mid_size >= c_s && mid_size <= c_e
	#p "#{n} m"
	count += n_map[1]
  end

  if c_e > mid_size
	#p "#{n} b"
    count += give_one_count(mid, [c_s - 1 - mid_size, 0].max , c_e - 1 - mid_size, one_map)
  end

  count
end

puts give_one_count(n, c_start, c_end, one_map)


a,b=gets.split
idx=(1...a.size).find{|i|a[i]>=b[0]}||a.size
puts a[0...idx]+b[0]


a, b=gets.split
a+=b
i=1
i+=1 while a[i] && a[i]<b[0]
puts a[0..i-1]+b[0]


w,b=gets.split.map(&:to_i)
$dp=Array.new(1002){Array.new(1002){Array.new(2,-1)}}
#t=0 姫 t=1 ドラゴン
def solve(w,b,t)
        return 0 if b<0 || w<0
        return 0 if w+b==0
        return $dp[w][b][t] if $dp[w][b][t]>=0
        res=0.0
        if t==0 then
                res+=w.to_f/(w+b)
                res+=b.to_f/(w+b)*solve(w,b-1,1-t)
        else
                #Wを取る
                res=0
                #Bを取る
                tb=b-1
                tw=w
                if tb+tw==0 then
                        res=0
                else
                        res+=b.to_f/(w+b)*tb.to_f/(tb+tw)*solve(w,b-2,1-t)
                        res+=b.to_f/(w+b)*tw.to_f/(tb+tw)*solve(w-1,b-1,1-t)
                end
        end
        return $dp[w][b][t]=res
end
p solve(w,b,0)


c = gets.chomp.split(" ").map{|i| i.to_i}

a = c[0]
b = c[1]
k = c[2]

L = []

(1..b).each do |i|
    L[i] = 1
end
L[1] = 0

(2..b).each do |i|
    kq = 2*i
    if L[i] == 1
        while kq <= b do
            L[kq] = 0
            kq += i
        end
    end
end

dem = 0
P = []
max = 0

(a..b).each do |i|
  if L[i] == 1
    dem += 1
    P[dem] = i
  end
end

if dem < k
    max = -1
else
    max = b - P[dem-k+1] + 1
    if max < (P[k] - a + 1)
        max = P[k] -a + 1
    end
    j = dem - k
    (1..j).each do |i|
        if (P[i+k] - P[i]+1) > max
            max = P[i+k] - P[i]
        end
    end
end

puts max


low=[]
st=[]
cur=1989
step=10
(1..10).each{|i|
	len=cur.to_s.length
	st<<cur.to_s[len-i..len-1]
	low<<cur
	cur+=step
	step*=10
}
gets.to_i.times{
	x=gets[4..99].strip
	dig=x.length
	delta=x.to_i-st[dig-1].to_i
	delta+=10**dig if delta<0
	puts low[dig-1]+delta
}


def isprime(x,prime)
  if x <= prime[-1]
    flag = false
    prime.length.times do |i|
      flag = true if x == prime[i]
    end
  else
    flag = true
    prime.length.times do |i|
      flag = false if x % prime[i] == 0
    end
  end
  return flag
end

def solve(x,prime)
  if isprime(x,prime)
    return x
  else
    flag = false
    i = 2
    while !flag
      if x % i == 0
        y = x/i
        flag = isprime(y,prime)
        xn = x-y+1 if flag
      end
      i += 1
    end
    return xn
  end
end

c = gets.to_i
prime = []
k = (c**0.5).to_i
a = Array.new(k+1,1)
i = 2
while i <= k
  if a[i] == 1
    prime << i
    (k/i).times do |j|
      a[i*(j+1)] = 0
    end
  end
  i += 1
end

x1 = solve(c,prime)
ans = c
max = (x1+k) < c ? x1+k : c
x1.upto(max) do |i|
  t = solve(i,prime)
  ans = t if t < ans
end
puts ans


a = gets.chomp.to_i
b = gets.chomp.to_s

# check if valid
COLORS = ['C', 'M', 'Y']
$valid = true
def valids(b)
	prev = nil
	b.split("").each do |char|
		if !(char == '?' || char != prev)
			return false
		end
		prev = char
	end
	return true
end
ll = b.length
def permute(b, cols, i, str, out)
	if i == b.length
		out.push(str)
	elsif out.length < 4
		prevc = nil
		prevc = str[i-1] if i > 0
		nextc = nil
		while str[i] != '?' && i < b.length
			prevc = str[i]
			i += 1
		end
		nextc = b[i+1]
		if i < b.length
			cola = cols - [prevc, nextc]
			cola.each do |col|
				str[i] = col
				out = permute(b, cols, i+1, str.dup, out)
				str[i] = '?'
			end
		else
			out.push(str)
		end
	end
	return out
end
if valids(b)
	out = permute(b, COLORS, 0, b.dup, [])
	if out.length >= 2
		puts "Yes"

	else
		puts "No"
	end
else
	puts "No"
end


s = gets.chomp
t = gets.chomp
divs = []
divt = []
(s.length-1).times do |i|
    divs.push(i) if s[i] != s[i+1]
end
divs.push(s.length-1)
(t.length-1).times do |i|
    divt.push(i) if t[i] != t[i+1]
end
divt.push(t.length-1)

ans = []

if s[0] == t[0] && divs.length > 1 && divt.length > 1
    ps, pt = nil, nil
    diff = divt.length - divs.length
    idx = diff.abs/4*2+1
    if diff >= 2
        ps = divs[0]+1
        pt = divt[idx]+1
    elsif diff <= -2
        ps = divs[idx]+1
        pt = divt[0]+1
    end
    if ps
        ans.push([ps, pt])
        s, t = [
            t[0, pt] + s[ps, s.length],
            s[0, ps] + t[pt, t.length]
        ]
        divs = []
        divt = []
        (s.length-1).times do |i|
            divs.push(i) if s[i] != s[i+1]
        end
        divs.push(s.length-1)
        (t.length-1).times do |i|
            divt.push(i) if t[i] != t[i+1]
        end
        divt.push(t.length-1)
    end
end

if divs.length == 1 && divt.length == 1
    puts 0
    exit
end

if s[0] == t[0]
    if divs.length < divt.length
        ns = 0
        nt = (divt.length - divs.length)/2
        nt += 1 if nt % 2 == 0
    else
        nt = 0
        ns = (divs.length - divt.length)/2
        ns += 1 if ns % 2 == 0
    end
else
    if divs.length < divt.length
        ns = 1
        nt = (divt.length - 1 - divs.length)/2 + 1
        nt += 1 if nt % 2 == 0
    else
        nt = 1
        ns = (divs.length - 1 - divt.length)/2 + 1
        ns += 1 if ns % 2 == 0
    end
end

ps = ns == 0 ? 0 : divs[ns-1]+1
pt = nt == 0 ? 0 : divt[nt-1]+1
ans.push([ps, pt])
s, t = [
    t[0, pt] + s[ps, s.length],
    s[0, ps] + t[pt, t.length]
]

divs = []
divt = []
(s.length-1).times do |i|
    divs.push(i) if s[i] != s[i+1]
end
divs.push(s.length-1)
(t.length-1).times do |i|
    divt.push(i) if t[i] != t[i+1]
end
divt.push(t.length-1)

max = [divs.length, divt.length].max
[max-2, max-1].each do |i|
    next if i < 0
    divs[i] ||= divs[-1]
    divt[i] ||= divt[-1]
end

ps, pt = 0, 0
(max-1).times do |i|
    ps += divs[i] - (i > 0 ? divs[i-1] : -1)
    pt += divt[i] - (i > 0 ? divt[i-1] : -1)
    ans.push([ps, pt])
    ps, pt = pt, ps
end

puts ans.length
ans.each do |arr|
    puts arr.join(' ')
end


require 'prime'

def f(a,b)
  c=0
  x = Prime.prime_division(a)
  while b>0 do
    tmp = b
    x.each do |ar|
      tmp = [tmp, b%ar[0]].min
    end
    c += tmp
    b -= tmp
    x.each do |ar|
      while b%ar[0]==0
        b/=ar[0]
        ar[1]-=1
        if ar[1] == 0
          x.delete(ar)
          break
        end
      end
    end
  end
  return c
end

input = gets.chomp
a = input.split(" ")[0].to_i
b = input.split(" ")[1].to_i
puts f(a,b)


p, x, y = gets.chomp.split(" ").map(&:to_i)

def tshirt(s)
  ret = []
  i = (s / 50) % 475
  25.times do
    i = (i * 96 + 42) % 475
    ret << (26 + i)
  end
  return ret
end

if x > y then
  d = ((x - y) / 50.0).floor
  xx = x - (d * 50)
  while true
    if tshirt(xx).include?(p) then
      puts 0
      exit
    elsif xx >= x then
      break
    end
    xx += 50
  end
end

d = ([0, y - x].max / 50.0).ceil

x += (d * 50)

while true
  break if tshirt(x).include?(p)
  x += 50
  d += 1
end

if d.even?
  puts d / 2
else
  puts d / 2 + 1
end


def ascan; gets.split.map(&:to_i); end
require 'prime'

def funcd(n, div)
    total = 0
    while n >= div
        total += n/div
        n /= div
    end
    total
end

n,b = ascan

p b.prime_division.map{|pr,nm| funcd(n,pr)/nm}.min


n = gets.to_i
a = gets.split.map(&:to_i)
ans = 10**9
for i in 0..n
    ret = 0
    for j in 0..n-1
        ret += ((j-i).abs+j+i)*a[j]*2
    end
    ans = ret if ans > ret
end
puts ans


n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort.reverse

puts (1..n).bsearch { |day|
		minus, cnt = 0, 0
		(0...n).sum { |i|
			res = [0, a[i] - minus].max
			cnt += 1
			if cnt == day
				minus += 1
				cnt = 0
			end
			res
		} >= m
     } || -1