module mod_bitset
  implicit none
  integer, private, parameter :: address_bits_per_word = 6
  integer, private, parameter :: bits_per_word = lshift(1, address_bits_per_word)
  integer, private, parameter :: bit_index_mask = bits_per_word - 1
  integer(8), private, parameter :: word_mask = -1
  type t_bitset
    integer(8), private, allocatable :: words(:) ! 0-indexed, [start, end)
    integer, private :: words_in_use = 0
  contains
    procedure, private :: flip0
    procedure, private :: flip1
    generic :: flip => flip0, flip1
    procedure, private :: set0
    procedure, private :: set1
    procedure, private :: setval0
    procedure, private :: setval1
    generic :: set => set0, set1, setval0, setval1
    procedure, private :: clear0
    procedure, private :: clear1
    procedure, private :: clear2
    generic :: clear => clear0, clear1, clear2
    procedure, private :: get0
    procedure, private :: get1
    generic :: get => get0, get1
    procedure :: next_set_bit => next_set_bit
    procedure :: next_clear_bit => next_clear_bit
    procedure :: previous_set_bit => previous_set_bit
    procedure :: previous_clear_bit => previous_clear_bit
    procedure :: length => length
    procedure :: is_empty => is_empty
    procedure :: is_not_empty => is_not_empty
    procedure :: intersects => intersects
    procedure :: cardinality => cardinality
    procedure :: and => and0
    procedure :: or => or0
    procedure :: xor => xor0
    procedure :: and_not => and_not0
    procedure :: size => size0
  end type
  interface bitset
    module procedure :: newb0, newb3
  end interface bitset
  interface slshift
    module procedure :: slshift0, slshift1
  end interface slshift
  interface urshift
    module procedure :: urshift0, urshift1
  end interface urshift
contains
  integer function slshift0(i, n) result(res)
    integer, intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 31)
    res = lshift(i, m)
  end
  integer(8) function slshift1(i, n) result(res)
    integer(8), intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 63)
    res = lshift(i, m)
  end
  integer function urshift0(i, n) result(res)
    integer, intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 31)
    if (m == 0) then
      res = i
    else
      res = rshift(ibclr(rshift(i, 1), 31), m - 1)
    end if
  end
  integer(8) function urshift1(i, n) result(res)
    integer(8), intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 63)
    if (m == 0) then
      res = i
    else
      res = rshift(ibclr(rshift(i, 1), 63), m - 1)
    end if
  end
  integer function number_of_leading_zeros(i) result(res)
    integer(8), intent(in) :: i
    integer :: x
    if (i == 0) then
      res = 64
      return
    end if
    res = 1
    x = int(urshift(i, 32))
    if (x == 0) then
      res = res + 32
      x = int(i)
    end if
    if (urshift(x, 16) == 0) then
      res = res + 16
      x = lshift(x, 16)
    end if
    if (urshift(x, 24) == 0) then
      res = res + 8
      x = lshift(x, 8)
    end if
    if (urshift(x, 28) == 0) then
      res = res + 4
      x = lshift(x, 4)
    end if
    if (urshift(x, 30) == 0) then
      res = res + 2
      x = lshift(x, 2)
    end if
    res = res - urshift(x, 31)
  end
  integer function number_of_trailing_zeros(i) result(res)
    integer(8), intent(in) :: i
    integer :: x, y
    if (i == 0) then
      res = 64
      return
    end if
    res = 63
    y = int(i)
    if (y /= 0) then
      res = res - 32
      x = y
    else
      x = int(urshift(i, 32))
    end if
    y = lshift(x, 16)
    if (y /= 0) then
      res = res - 16
      x = y
    end if
    if (y /= 0) then
      res = res - 8
      x = y
    end if
    if (y /= 0) then
      res = res - 4
      x = y
    end if
    if (y /= 0) then
      res = res - 2
      x = y
    end if
    res = res - urshift(lshift(x, 1), 31)
  end
  integer function words_index(bit_index) result(res)
    integer, intent(in) :: bit_index
    res = rshift(bit_index, address_bits_per_word)
  end
  subroutine assert(flag, message)
    logical, intent(in) :: flag
    character(*), intent(in), optional :: message
    if (flag) return
    write(*,*) "*** Error occurred ***"
    if (present(message)) write(*,'(a)') message
    stop
  end
  subroutine check_invariants(this)
    type(t_bitset), intent(in) :: this
    integer :: u, n
    u = this%words_in_use
    n = size(this%words)
    if (u /= 0) call assert(this%words(u - 1) /= 0, "At check_invariants() (1)")
    call assert(u >= 0 .and. u <= n, "At check_invariants() (2)")
    if (u /= n) call assert(this%words(u) == 0, "At check_invariants() (3)")
  end
  subroutine recalculate_words_in_use(this)
    type(t_bitset), intent(inout) :: this
    integer :: i
    do i = this%words_in_use - 1, 0, -1
      if (this%words(i) /= 0) then
        this%words_in_use = i + 1
        return
      end if
    end do
    this%words_in_use = 0
  end
  subroutine init_words(this, nbits)
    type(t_bitset), intent(inout) :: this
    integer, intent(in) :: nbits
    if (allocated(this%words)) deallocate(this%words)
    allocate(this%words(0:words_index(nbits - 1)))
    this%words = 0
  end
  type(t_bitset) function newb0() result(res)
    call init_words(res, bits_per_word)
  end
  type(t_bitset) function newb1(nbits) result(res)
    integer, intent(in) :: nbits
    call assert(nbits >= 0, "nbits < 0 at newb1()")
    call init_words(res, nbits)
  end
  type(t_bitset) function newb2(words) result(res)
    integer(8), intent(in) :: words(:)
    allocate(res%words(0:size(words) - 1))
    res%words = words
    res%words_in_use = size(words)
    call check_invariants(res)
  end
  type(t_bitset) function newb3(longs) result(res)
    integer(8), intent(in) :: longs(:)
    integer :: i
    do i = size(longs), 1, -1
      if (longs(i) /= 0) then
        res = newb2(longs(1:i))
        return
      end if
    end do
    res = newb0()
  end
  subroutine ensure_capacity(this, words_required)
    type(t_bitset), intent(inout) :: this
    integer, intent(in) :: words_required
    integer :: request
    integer(8), allocatable :: copy(:)
    if (size(this%words) >= words_required) return
    request = max(2 * size(this%words), words_required)
    allocate(copy(size(this%words)))
    copy = this%words
    deallocate(this%words)
    allocate(this%words(0:request - 1))
    this%words = 0
    this%words(0:size(copy) - 1) = copy
  end
  subroutine expand_to(this, word_index)
    type(t_bitset), intent(inout) :: this
    integer, intent(in) :: word_index
    integer :: words_required
    words_required = word_index + 1
    if (this%words_in_use < words_required) then
      call ensure_capacity(this, words_required)
      this%words_in_use = words_required
    end if
  end
  subroutine check_range(from_index, to_index)
    integer, intent(in) :: from_index, to_index
    call assert(0 <= from_index .and. from_index <= to_index, "At check_range()")
  end
  subroutine flip0(this, bit_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at flip0()")
    word_index = words_index(bit_index)
    call expand_to(this, word_index)
    this%words(word_index) = xor(this%words(word_index), slshift(1_8, bit_index))
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine flip1(this, from_index, to_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index
    integer :: start_word_index, end_word_index, idx
    integer(8) :: first_word_mask, last_word_mask
    call check_range(from_index, to_index)
    if (from_index == to_index) return
    start_word_index = words_index(from_index)
    end_word_index = words_index(to_index - 1)
    call expand_to(this, end_word_index)
    first_word_mask = slshift(word_mask, from_index)
    last_word_mask = urshift(word_mask, -to_index)
    if (start_word_index == end_word_index) then
      idx = start_word_index
      this%words(idx) = xor(this%words(idx), and(first_word_mask, last_word_mask))
    else
      idx = start_word_index
      this%words(idx) = xor(this%words(idx), first_word_mask)
      do idx = start_word_index + 1, end_word_index - 1
        this%words(idx) = xor(this%words(idx), word_mask)
      end do
      idx = end_word_index
      this%words(idx) = xor(this%words(idx), last_word_mask)
    end if
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine set0(this, bit_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at set0()")
    word_index = words_index(bit_index)
    call expand_to(this, word_index)
    this%words(word_index) = or(this%words(word_index), slshift(1_8, bit_index))
    call check_invariants(this)
  end
  subroutine clear0(this, bit_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at clear0()")
    word_index = words_index(bit_index)
    if (word_index >= this%words_in_use) return
    this%words(word_index) = and(this%words(word_index), not(slshift(1_8, bit_index)))
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine setval0(this, bit_index, val)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    logical, intent(in) :: val
    if (val) then
      call set0(this, bit_index)
    else
      call clear0(this, bit_index)
    end if
  end
  subroutine set1(this, from_index, to_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index
    integer :: start_word_index, end_word_index, idx
    integer(8) :: first_word_mask, last_word_mask
    call check_range(from_index, to_index)
    if (from_index == to_index) return
    start_word_index = words_index(from_index)
    end_word_index = words_index(to_index - 1)
    call expand_to(this, end_word_index)
    first_word_mask = slshift(word_mask, from_index)
    last_word_mask = urshift(word_mask, -to_index)
    if (start_word_index == end_word_index) then
      idx = start_word_index
      this%words(idx) = or(this%words(idx), and(first_word_mask, last_word_mask))
    else
      idx = start_word_index
      this%words(idx) = or(this%words(idx), first_word_mask)
      do idx = start_word_index + 1, end_word_index - 1
        this%words(idx) = word_mask
      end do
      idx = end_word_index
      this%words(idx) = or(this%words(idx), last_word_mask)
    end if
    call check_invariants(this)
  end
  subroutine clear1(this, from_index, to_index_)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index_
    integer :: to_index
    integer :: start_word_index, end_word_index, idx
    integer(8) :: first_word_mask, last_word_mask
    to_index = to_index_
    call check_range(from_index, to_index)
    if (from_index == to_index) return
    start_word_index = words_index(from_index)
    if (start_word_index >= this%words_in_use) return
    end_word_index = words_index(to_index - 1)
    if (end_word_index >= this%words_in_use) then
      to_index = length(this)
      end_word_index = this%words_in_use - 1
    end if
    first_word_mask = slshift(word_mask, from_index)
    last_word_mask = urshift(word_mask, -to_index)
    if (start_word_index == end_word_index) then
      idx = start_word_index
      this%words(idx) = and(this%words(idx), not(and(first_word_mask, last_word_mask)))
    else
      idx = start_word_index
      this%words(idx) = and(this%words(idx), not(first_word_mask))
      do idx = start_word_index + 1, end_word_index - 1
        this%words(idx) = 0
      end do
      idx = end_word_index
      this%words(idx) = and(this%words(idx), not(last_word_mask))
    end if
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine setval1(this, from_index, to_index, val)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index
    logical, intent(in) :: val
    if (val) then
      call set1(this, from_index, to_index)
    else
      call clear1(this, from_index, to_index)
    end if
  end
  subroutine clear2(this)
    class(t_bitset), intent(out) :: this
    this%words(0:this%words_in_use - 1) = 0
    this%words_in_use = 0
  end
  logical function get0(this, bit_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at get0()")
    call check_invariants(this)
    word_index = words_index(bit_index)
    if (word_index >= this%words_in_use) then
      res = .false.
      return
    end if
    res = and(this%words(word_index), slshift(1_8, bit_index)) /= 0
  end
  type(t_bitset) function get1(this, from_index, to_index_) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index, to_index_
    integer :: to_index, len
    integer :: target_words, source_index, idx
    logical :: word_aligned
    integer(8) :: last_word_mask
    to_index = to_index_
    call check_range(from_index, to_index)
    call check_invariants(this)
    len = length(this)
    if (len <= from_index .or. from_index == to_index) then
      res = newb0()
      return
    end if
    if (to_index > len) to_index = len
    res = newb1(to_index - from_index)
    target_words = words_index(to_index - from_index - 1) + 1
    source_index = words_index(from_index)
    word_aligned = and(from_index, bit_index_mask) == 0
    do idx = 0, target_words - 2
      if (word_aligned) then
        res%words(idx) = this%words(source_index)
      else
        res%words(idx) = or(urshift(this%words(source_index), from_index), slshift(this%words(source_index + 1), -from_index))
      end if
      source_index = source_index + 1
    end do
    last_word_mask = urshift(word_mask, -to_index)
    if (and(to_index - 1, bit_index_mask) < and(from_index, bit_index_mask)) then
      res%words(target_words - 1) = or(urshift(this%words(source_index), from_index), &
        slshift(and(this%words(source_index + 1), last_word_mask), -from_index))
    else
      res%words(target_words - 1) = urshift(and(this%words(source_index), last_word_mask), from_index)
    end if
    res%words_in_use = target_words
    call recalculate_words_in_use(res)
    call check_invariants(res)
  end
  integer function next_set_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    call assert(from_index >= 0, "from_index < 0 at next_set_bit()")
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = -1
      return
    end if
    word = and(this%words(u), slshift(word_mask, from_index))
    do
      if (word /= 0) then
        res = (u * bits_per_word) + number_of_trailing_zeros(word)
        return
      end if
      u = u + 1
      if (u == this%words_in_use) then
        res = -1
        return
      end if
      word = this%words(u)
    end do
  end
  integer function next_clear_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    call assert(from_index >= 0, "from_index < 0 at next_clear_bit()")
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = from_index
      return
    end if
    word = and(not(this%words(u)), slshift(word_mask, from_index))
    do
      if (word /= 0) then
        res = (u * bits_per_word) + number_of_trailing_zeros(word)
        return
      end if
      u = u + 1
      if (u == this%words_in_use) then
        res = this%words_in_use * bits_per_word
        return
      end if
      word = not(this%words(u))
    end do
  end
  integer function previous_set_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    if (from_index < 0) then
      call assert(from_index == -1, "from_index < -1 at previous_set_bit()")
      res = -1
      return
    end if
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = length(this) - 1
      return
    end if
    word = and(this%words(u), urshift(word_mask, -(from_index + 1)))
    do
      if (word /= 0) then
        res = (u + 1) * bits_per_word - 1 - number_of_leading_zeros(word)
        return
      end if
      if (u == 0) then
        res = -1
        return
      end if
      u = u - 1
      word = this%words(u)
    end do
  end
  integer function previous_clear_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    if (from_index < 0) then
      call assert(from_index == -1, "from_index < -1 at previous_clear_bit()")
      res = -1
      return
    end if
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = from_index
      return
    end if
    word = and(not(this%words(u)), urshift(word_mask, -(from_index + 1)))
    do
      if (word /= 0) then
        res = (u + 1) * bits_per_word - 1 - number_of_leading_zeros(word)
        return
      end if
      if (u == 0) then
        res = -1
        return
      end if
      u = u - 1
      word = not(this%words(u))
    end do
  end
  integer function length(this) result(res)
    class(t_bitset), intent(in) :: this
    integer :: u, n
    u = this%words_in_use
    if (u == 0) then
      res = 0
    else
      n = number_of_leading_zeros(this%words(u - 1))
      res = bits_per_word * (u - 1) + (bits_per_word - n)
    end if
  end
  logical function is_empty(this) result(res)
    class(t_bitset), intent(in) :: this
    res = this%words_in_use == 0
  end
  logical function is_not_empty(this) result(res)
    class(t_bitset), intent(in) :: this
    res = this%words_in_use > 0
  end
  logical function intersects(this, other) result(res)
    class(t_bitset), intent(in) :: this
    type(t_bitset), intent(in) :: other
    integer :: m, i
    m = min(this%words_in_use, other%words_in_use)
    do i = m, 0, -1
      if (and(this%words(i), other%words(i)) /= 0) then
        res = .true.
      end if
    end do
    res = .false.
  end
  integer function cardinality(this) result(res)
    class(t_bitset), intent(in) :: this
    res = sum(popcnt(this%words(0:this%words_in_use - 1)))
  end
  subroutine and0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: i
    do while (this%words_in_use > other%words_in_use)
      this%words_in_use = this%words_in_use - 1
      this%words(this%words_in_use) = 0
    end do
    do i = 0, this%words_in_use - 1
      this%words(i) = and(this%words(i), other%words(i))
    end do
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine or0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: u, i
    u = min(this%words_in_use, other%words_in_use)
    do i = 0, u - 1
      this%words(i) = or(this%words(i), other%words(i))
    end do
    if (u < other%words_in_use) then
      call ensure_capacity(this, other%words_in_use)
      this%words_in_use = other%words_in_use
      this%words(u:this%words_in_use - 1) = other%words(u:other%words_in_use - 1)
    end if
    call check_invariants(this)
  end
  subroutine xor0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: u, i
    u = min(this%words_in_use, other%words_in_use)
    do i = 0, u - 1
      this%words(i) = xor(this%words(i), other%words(i))
    end do
    if (u < other%words_in_use) then
      call ensure_capacity(this, other%words_in_use)
      this%words_in_use = other%words_in_use
      this%words(u:this%words_in_use - 1) = other%words(u:other%words_in_use - 1)
    end if
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine and_not0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: u, i
    u = min(this%words_in_use, other%words_in_use)
    do i = 0, u - 1
      this%words(i) = and(this%words(i), not(other%words(i)))
    end do
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  integer function size0(this) result(res)
    class(t_bitset), intent(in) :: this
    res = size(this%words) * bits_per_word
  end
end module mod_bitset
program balanced_path
  use mod_bitset
  implicit none
  integer :: h, w, a(80, 80) = 0, b(80) = 0, i, j, k
  type(t_bitset) :: dp(80, 80)
  read(*,*) h, w
  do i = 1, h
    read(*,*) a(i, 1:w)
  end do
  do i = 1, h
    read(*,*) b(1:w)
    a(i, 1:w) = abs(a(i, 1:w)-b(1:w))
  end do
  do i = 1, h
    do j = 1, w
      dp(i, j) = bitset()
    end do
  end do
  call dp(1, 1)%set(a(1, 1))
  do i = 1, h
    do j = 1, w
      if (i > 1) then
        do k = 0, 12800
          if (dp(i - 1, j)%get(k)) then
            call dp(i, j)%set(abs(k - a(i, j)))
            call dp(i, j)%set(k + a(i, j))
          end if
        end do
      end if
      if (j > 1) then
        do k = 0, 12800
          if (dp(i, j - 1)%get(k)) then
            call dp(i, j)%set(abs(k - a(i, j)))
            call dp(i, j)%set(k + a(i, j))
          end if
        end do
      end if
    end do
  end do
  do k = 0, 12800
    if (dp(h, w)%get(k)) then
      write(*,'(i0)') k
      stop
    end if
  end do
end program balanced_path

Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.

The square (i, j) has two numbers A_{ij} and B_{ij} written on it.

First, for each square, Takahashi paints one of the written numbers red and the other blue.

Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.

Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).

Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.

Find the minimum unbalancedness possible.

Constraints

2 \leq H \leq 80

2 \leq W \leq 80

0 \leq A_{ij} \leq 80

0 \leq B_{ij} \leq 80

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

Output

Print the minimum unbalancedness possible.

Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 1

0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2


program sample
	integer ::a,b,c,d
    read(*,*) a,b
    read(*,*) c,d
    if (d==1) then
    	write(*,*) 1
    else
    	write(*,*) 0
    end if
    stop
end program sample

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score: 100 points

Problem Statement

In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.

Integers M_1, D_1, M_2, and D_2 will be given as input.

It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.

Determine whether the date 2019-M_1-D_1 is the last day of a month.

Constraints

Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.

The date 2019-M_2-D_2 follows 2019-M_1-D_1.

Input

Input is given from Standard Input in the following format:

M_1 D_1
M_2 D_2

Output

If the date 2019-M_1-D_1 is the last day of a month, print 1; otherwise, print 0.

Sample Input 1

11 16
11 17

Sample Output 1

0

November 16 is not the last day of a month.

Sample Input 2

11 30
12 1

Sample Output 2

1

November 30 is the last day of November.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


program sample
    implicit none
    real(8)::x,y,z
    integer(8) :: a,b,c,i,j,m,n,k,a1,b1,c1

    read(*,*) n

    if (mod(n*100,108)==0)then
        write(*,*) n*100/108
    else
        a=1+int(n/1.08)
        if (a<(n+1)/1.08)then
            write(*,*)a
        else
            write(*,*)':('
        end if
    end if
    stop
end program sample

Score: 200 points

Problem Statement

Takahashi bought a piece of apple pie at ABC Confiserie. According to his memory, he paid N yen (the currency of Japan) for it.

The consumption tax rate for foods in this shop is 8 percent. That is, to buy an apple pie priced at X yen before tax, you have to pay X \times 1.08 yen (rounded down to the nearest integer).

Takahashi forgot the price of his apple pie before tax, X, and wants to know it again. Write a program that takes N as input and finds X. We assume X is an integer.

If there are multiple possible values for X, find any one of them. Also, Takahashi's memory of N, the amount he paid, may be incorrect. If no value could be X, report that fact.

Constraints

1 \leq N \leq 50000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If there are values that could be X, the price of the apple pie before tax, print any one of them.

If there are multiple such values, printing any one of them will be accepted.

If no value could be X, print :(.

Sample Input 1

432

Sample Output 1

400

If the apple pie is priced at 400 yen before tax, you have to pay 400 \times 1.08 = 432 yen to buy one.

Otherwise, the amount you have to pay will not be 432 yen.

Sample Input 2

1079

Sample Output 2

:(

There is no possible price before tax for which you have to pay 1079 yen with tax.

Sample Input 3

1001

Sample Output 3

927

If the apple pie is priced 927 yen before tax, by rounding down 927 \times 1.08 = 1001.16, you have to pay 1001 yen.


program colorful_hats_2
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, a(100000) = 0, i, c(3) = 0, j
  integer(8) :: ans = 1_8, m
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    m = 0_8
    do j = 1, 3
      if (a(i) == c(j)) m = m+1_8
    end do
    ans = mod(ans*m,md)
    do j = 1, 3
      if (a(i) == c(j)) then
        c(j) = c(j)+1
        exit
      end if
    end do
  end do
  write(*,'(i0)') ans
end program colorful_hats_2

Score: 500 points

Problem Statement

N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green.

The person numbered i says:

"In front of me, exactly A_i people are wearing hats with the same color as mine."

Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats.

Since the count can be enormous, compute it modulo 1000000007.

Constraints

1 \leq N \leq 100000

0 \leq A_i \leq N-1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print the number of possible combinations of colors of the N people's hats, modulo 1000000007.

Sample Input 1

6
0 1 2 3 4 5

Sample Output 1

3

We have three possible combinations, as follows:

Red, Red, Red, Red, Red, Red

Blue, Blue, Blue, Blue, Blue, Blue

Green, Green, Green, Green, Green, Green

Sample Input 2

3
0 0 0

Sample Output 2

6

Sample Input 3

54
0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17

Sample Output 3

115295190


      PROGRAM run
      IMPLICIT NONE
      INTEGER(16) :: a1,a2,b1,b2,t1,t2
      INTEGER(16) :: iter,sub,len1,len2,s,l,pos1,pos2
      INTEGER(16) :: piyo,i,ans

      read*,t1,t2
      read*,a1,a2
      read*,b1,b2

      len1 = t1*a1 + t2*a2
      len2 = t1*b1 + t2*b2
      s = min( len1,len2 )
      l = max( len1,len2 )

      sub = l - s

      if( sub==0 )then
        print*,'infinity'
        stop
      end if
      iter = s / sub + 1

      pos1 = 0;pos2 = 0;ans = 0
      piyo = 3
      do i = 1,iter
        pos1 = pos1 + t1*a1
        pos2 = pos2 + t1*b1
        if( pos2 > pos1 )then
          if(piyo==1) ans = ans + 1
          piyo = 2
        else
          if(piyo==2) ans = ans + 1
          piyo = 1
        end if

        pos1 = pos1 + t2*a2
        pos2 = pos2 + t2*b2
        if( pos2 > pos1 )then
          if(piyo==1) ans = ans + 1
          piyo = 2
        else
          if(piyo==2) ans = ans + 1
          piyo = 1
        end if
      end do

      print*,ans

      END PROGRAM

Score: 600 points

Problem Statement

Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.

They start simultaneously at the same point and moves as follows towards the east:

Takahashi runs A_1 meters per minute for the first T_1 minutes, then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

Aoki runs B_1 meters per minute for the first T_1 minutes, then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.

Constraints

1 \leq T_i \leq 100000

1 \leq A_i \leq 10^{10}

1 \leq B_i \leq 10^{10}

A_1 \neq B_1

A_2 \neq B_2

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T_1 T_2
A_1 A_2
B_1 B_2

Output

Print the number of times Takahashi and Aoki will meet each other.

If they meet infinitely many times, print infinity instead.

Sample Input 1

1 2
10 10
12 4

Sample Output 1

1

They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start.

Sample Input 2

100 1
101 101
102 1

Sample Output 2

infinity

They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.

Sample Input 3

12000 15700
3390000000 3810000000
5550000000 2130000000

Sample Output 3

113

The values in input may not fit into a 32-bit integer type.


program sample
	implicit none
    character(len=3) :: a

    read(*,*) a

    if (a=='SUN') then
    	write(*,*) 7
    else if (a=='MON') then
        write(*,*) 6
    else if (a=='TUE') then
        write(*,*) 5
    else if (a=='WED') then
        write(*,*) 4
    else if (a=='THU') then
        write(*,*) 3
    else if (a=='FRI') then
        write(*,*) 2
    else
    	write(*,*) 1
    end if
    stop
end program sample

Score : 100 points

Problem Statement

Given is a string S representing the day of the week today.

S is SUN, MON, TUE, WED, THU, FRI, or SAT, for Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, respectively.

After how many days is the next Sunday (tomorrow or later)?

Constraints

S is SUN, MON, TUE, WED, THU, FRI, or SAT.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of days before the next Sunday.

Sample Input 1

SAT

Sample Output 1

1

It is Saturday today, and tomorrow will be Sunday.

Sample Input 2

SUN

Sample Output 2

7

It is Sunday today, and seven days later, it will be Sunday again.


program b
    implicit none
    integer :: N, l, i
    character(10000) :: S

    read(*, *) N
    read(*, *) S

    l = len_trim(S)

    do i = 1, l
        S(i:i) = achar(mod(ichar(S(i:i)) - 65 + N, 26) + 65)
    end do

    write(*, '(a)') trim(S)

end program b

Score : 200 points

Problem Statement

We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.

Shift each character of S by N in alphabetical order (see below), and print the resulting string.

We assume that A follows Z. For example, shifting A by 2 results in C (A \to B \to C), and shifting Y by 3 results in B (Y \to Z \to A \to B).

Constraints

0 \leq N \leq 26

1 \leq |S| \leq 10^4

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the string resulting from shifting each character of S by N in alphabetical order.

Sample Input 1

2
ABCXYZ

Sample Output 1

CDEZAB

Note that A follows Z.

Sample Input 2

0
ABCXYZ

Sample Output 2

ABCXYZ

Sample Input 3

13
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output 3

NOPQRSTUVWXYZABCDEFGHIJKLM


program main
  integer::n,i,j,k,ans,le
  !integer,allocatable::
  character(10000)::s
read(*,*)n

read(*,*)s
le=len(s)
!write(*,*) le
do i=1,n
  do j=1,le
    select case(s(j:j))
    case('A')
      s(j:j)='B'
    case('B')
      s(j:j)='C'
    case('C')
      s(j:j)='D'
    case('D')
      s(j:j)='E'
    case('E')
      s(j:j)='F'
    case('F')
      s(j:j)='G'
    case('G')
      s(j:j)='H'
    case('H')
      s(j:j)='I'
    case('I')
      s(j:j)='J'
    case('J')
      s(j:j)='K'
    case('K')
      s(j:j)='L'
    case('L')
      s(j:j)='M'
    case('M')
      s(j:j)='N'
    case('N')
      s(j:j)='O'
    case('O')
      s(j:j)='P'
    case('P')
      s(j:j)='Q'
    case('Q')
      s(j:j)='R'
    case('R')
      s(j:j)='S'
    case('S')
      s(j:j)='T'
    case('T')
      s(j:j)='U'
    case('U')
      s(j:j)='V'
    case('V')
      s(j:j)='W'
    case('W')
      s(j:j)='X'
    case('X')
      s(j:j)='Y'
    case('Y')
      s(j:j)='Z'
    case('Z')
      s(j:j)='A'
    end select
  end do
end do
s=trim(s(1:le))
write(*,'(a)') s
stop
end program


program atcoder
	implicit none
	integer :: i,n,ans=0,k,j
	integer,allocatable,dimension(:) :: a
	read*,n,k
	allocate(a(n))
	read(*,*)(a(i),i=1,n)
	do i = 1,n
		do j=0,n-i
			if( mod(sum(a(i:i+j)),k) == j+1 )then
				ans = ans+1
			endif
		end do
	end do
	write(*,*)ans
	stop
end program atcoder

Score : 500 points

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \ldots, A_N, and a positive integer K.

Find the number of non-empty contiguous subsequences in A such that the remainder when dividing the sum of its elements by K is equal to the number of its elements. We consider two subsequences different if they are taken from different positions, even if they are equal sequences.

Constraints

All values in input are integers.

1 \leq N \leq 2\times 10^5

1 \leq K \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print the number of subsequences that satisfy the condition.

Sample Input 1

5 4
1 4 2 3 5

Sample Output 1

4

Four sequences satisfy the condition: (1), (4,2), (1,4,2), and (5).

Sample Input 2

8 4
4 2 4 2 4 2 4 2

Sample Output 2

7

(4,2) is counted four times, and (2,4) is counted three times.

Sample Input 3

10 7
14 15 92 65 35 89 79 32 38 46

Sample Output 3

8


module mod_segment_tree
  implicit none
  type t_segment_tree
    private
    integer :: deflt = 1000000000
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_segtree
    procedure :: release => release_segtree
    procedure :: update => update
    procedure :: query => query
    procedure :: default => set_default
    procedure :: change => change_default
  end type
  interface segment_tree
    module procedure :: newst
  end interface segment_tree
contains
  integer function op(x,y)
    integer, intent(in) :: x, y
    op = min(x,y)
  end
  function newst(n) result(ret)
    integer, intent(in) :: n
    type(t_segment_tree) :: ret
    integer :: p
    p = 1
    do while (p < n)
      p = 2*p
    end do
    ret%n = n
    ret%p = p
    allocate(ret%arr(2*p-1))
    ret%arr = ret%deflt
  end
  subroutine init_segtree(this,n)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    do while (p < n)
      p = 2*p
    end do
    this%n = n
    this%p = p
    allocate(this%arr(2*p-1))
    this%arr = this%deflt
  end
  subroutine release_segtree(this)
    class(t_segment_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine set_default(this,i)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer :: x
    x = i+this%p-1
    this%arr(x) = this%deflt
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  subroutine change_default(this,deflt)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: deflt
    this%deflt = deflt
  end
  subroutine update(this,i,v)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i+this%p-1
    this%arr(x) = v
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  integer function query(this,a,b)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: a, b
    integer :: l, r
    query = this%deflt
    l = a+this%p-1
    r = b+this%p-1
    do while (l <= r)
      if (mod(l,2) == 1) then
        query = op(query,this%arr(l))
        l = l+1
      end if
      if (mod(r,2) == 0) then
        query = op(query,this%arr(r))
        r = r-1
      end if
      l = l/2
      r = r/2
    end do
  end
end module mod_segment_tree
program Sugoroku
  use mod_segment_tree
  implicit none
  integer :: n, m, i, x, y, a(0:100001) = 0
  character(100001) :: s
  type(t_segment_tree) :: st
  read(*,*) n, m
  if (n <= m) then
    write(*,'(i0)') n
    stop
  end if
  read(*,*) s
  st = segment_tree(n+1)
  call st%update(1,0)
  do i = 2, n+1
    if (s(i:i) == "1") cycle
    x = st%query(max(1,i-m),i-1)
    if (x > n) then
      write(*,'(i0)') -1
      stop
    end if
    call st%update(i,x+1)
  end do
  y = st%query(n+1,n+1)
  a(y) = n+1
  do i = n+1, 1, -1
    if (s(i:i) == "1") cycle
    x = st%query(i,i)
    if (x == y) cycle
    if (i < a(x+1)-m) cycle
    a(x) = i
  end do
  write(*,'(i0)',advance='no') a(1)-a(0)
  do i = 1, y-1
    write(*,'(x,i0)',advance='no') a(i+1)-a(i)
  end do
  write(*,*)
end program Sugoroku

Score : 600 points

Problem Statement

Takahashi is playing a board game called Sugoroku.

On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at Square 0, and he has to stop exactly at Square N to win the game.

The game uses a roulette with the M numbers from 1 to M. In each turn, Takahashi spins the roulette. If the number x comes up when he is at Square s, he moves to Square s+x. If this makes him go beyond Square N, he loses the game.

Additionally, some of the squares are Game Over Squares. He also loses the game if he stops at one of those squares. You are given a string S of length N + 1, representing which squares are Game Over Squares. For each i (0 \leq i \leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.

Find the sequence of numbers coming up in the roulette in which Takahashi can win the game in the fewest number of turns possible. If there are multiple such sequences, find the lexicographically smallest such sequence. If Takahashi cannot win the game, print -1.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

|S| = N + 1

S consists of 0 and 1.

S[0] = 0

S[N] = 0

Input

Input is given from Standard Input in the following format:

N M
S

Output

If Takahashi can win the game, print the lexicographically smallest sequence among the shortest sequences of numbers coming up in the roulette in which Takahashi can win the game, with spaces in between.

If Takahashi cannot win the game, print -1.

Sample Input 1

9 3
0001000100

Sample Output 1

1 3 2 3

If the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9 via Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and this is the lexicographically smallest sequence in which he reaches Square 9 in four turns.

Sample Input 2

5 4
011110

Sample Output 2

-1

Takahashi cannot reach Square 5.

Sample Input 3

6 6
0101010

Sample Output 3

6


module mod_segment_tree
  implicit none
  type t_segment_tree
    private
    integer :: deflt = 1000000000
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_segtree
    procedure :: release => release_segtree
    procedure :: update => update
    procedure :: query => query
    procedure :: default => set_default
    procedure :: change => change_default
  end type
  interface segment_tree
    module procedure :: newst
  end interface segment_tree
contains
  integer function op(x,y)
    integer, intent(in) :: x, y
    op = min(x,y)
  end
  function newst(n) result(ret)
    integer, intent(in) :: n
    type(t_segment_tree) :: ret
    integer :: p
    p = 1
    do while (p < n)
      p = 2*p
    end do
    ret%n = n
    ret%p = p
    allocate(ret%arr(2*p-1))
    ret%arr = ret%deflt
  end
  subroutine init_segtree(this,n)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    do while (p < n)
      p = 2*p
    end do
    this%n = n
    this%p = p
    allocate(this%arr(2*p-1))
    this%arr = this%deflt
  end
  subroutine release_segtree(this)
    class(t_segment_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine set_default(this,i)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer :: x
    x = i+this%p-1
    this%arr(x) = this%deflt
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  subroutine change_default(this,deflt)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: deflt
    this%deflt = deflt
  end
  subroutine update(this,i,v)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i+this%p-1
    this%arr(x) = v
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  integer function query(this,a,b)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: a, b
    integer :: l, r
    query = this%deflt
    l = a+this%p-1
    r = b+this%p-1
    do while (l <= r)
      if (mod(l,2) == 1) then
        query = op(query,this%arr(l))
        l = l+1
      end if
      if (mod(r,2) == 0) then
        query = op(query,this%arr(r))
        r = r-1
      end if
      l = l/2
      r = r/2
    end do
  end
end module mod_segment_tree
program Sugoroku
  use mod_segment_tree
  implicit none
  integer :: n, m, i, x, y, a(0:100001) = 0
  character(100001) :: s
  type(t_segment_tree) :: st
  read(*,*) n, m
  if (n <= m) then
    write(*,'(i0)') n
    stop
  end if
  read(*,*) s
  st = segment_tree(n+1)
  call st%update(1,0)
  do i = 2, n+1
    if (s(i:i) == "1") cycle
    x = st%query(max(1,i-m),i-1)
    if (x > n) then
      write(*,'(i0)') -1
      stop
    end if
    call st%update(i,x+1)
  end do
  y = st%query(n+1,n+1)
  a(y) = n+1
  do i = n+1, 1, -1
    if (s(i:i) == "1") cycle
    x = st%query(i,i)
    if (x == y) cycle
    a(x) = max(i,a(x+1)-m)
  end do
  write(*,'(i0)',advance='no') a(1)-a(0)
  do i = 1, y-1
    write(*,'(x,i0)',advance='no') a(i+1)-a(i)
  end do
  write(*,*)
end program Sugoroku


      program ans1
      implicit none
      integer :: X,Y
      integer :: XR,YR
      read(*,*) X,Y
      XR = 0
      YR = 0
      if(X == 1) XR = 300000
      if(X == 2) XR = 200000
      if(X == 3) XR = 100000
      if(Y == 1) YR = 300000
      if(Y == 2) YR = 200000
      if(Y == 3) YR = 100000
      if(X == 1 .and. Y == 1) XR = XR + 400000
      write(*,*) XR + YR
      end program ans1

Score: 100 points

Problem Statement

We held two competitions: Coding Contest and Robot Maneuver.

In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places receive 100000, 200000, and 300000 yen (the currency of Japan), respectively. Furthermore, a contestant taking the first place in both competitions receives an additional 400000 yen.

DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot Maneuver.
Find the total amount of money he earned.

Constraints

1 \leq X \leq 205

1 \leq Y \leq 205

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the amount of money DISCO-Kun earned, as an integer.

Sample Input 1

1 1

Sample Output 1

1000000

In this case, he earned 300000 yen in Coding Contest and another 300000 yen in Robot Maneuver. Furthermore, as he won both competitions, he got an additional 400000 yen.
In total, he made 300000 + 300000 + 400000 = 1000000 yen.

Sample Input 2

3 101

Sample Output 2

100000

In this case, he earned 100000 yen in Coding Contest.

Sample Input 3

4 4

Sample Output 3

0

In this case, unfortunately, he was the highest-ranked contestant without prize money in both competitions.


program DDCC2020B
    implicit none
    integer(8)::N,i,diff
    integer(8)::sum_half=0
    integer(8),allocatable,dimension(:)::A

    read(5,*)N
    allocate(A(N))
    read(5,*)(A(i),i=1,N)

    diff=sum(A)

    do i=1,N
        if(abs(sum(A)-2*(sum_half+A(i)))<diff)then
            diff=abs(sum(A)-2*(sum_half+A(i)))
            sum_half=sum_half+A(i)
        end if
    end do

    print'(i0)',diff

end program DDCC2020B

Score: 200 points

Problem Statement

Takahashi, who works at DISCO, is standing before an iron bar.
The bar has N-1 notches, which divide the bar into N sections. The i-th section from the left has a length of A_i millimeters.

Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.
However, this may not be possible as is, so he will do the following operations some number of times before he does the cut:

Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).

Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.

Find the minimum amount of money needed before cutting the bar into two parts with the same length.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 2020202020

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.

Sample Input 1

3
2 4 3

Sample Output 1

3

The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:

Shrink the second section from the left. The lengths of the sections are now [2, 3, 3].

Shrink the first section from the left. The lengths of the sections are now [1, 3, 3].

Shrink the second section from the left. The lengths of the sections are now [1, 2, 3], and we can cut the bar at the second notch from the left into two parts of length 3 each.

Sample Input 2

12
100 104 102 105 103 103 101 105 104 102 104 101

Sample Output 2

0


program DISCO2020yC
integer::H,W,K
integer,allocatable::ans(:,:)
character(300)reader
integer ichigonumber
read*,H,W,K
allocate(ans(H,W))
ans=-1
ichigonumber=1
do i=1,H
    read*,reader
    do j=1,W
        if(reader(j:j)=="#")then
            ans(i,j)=ichigonumber
            ichigonumber=ichigonumber+1
        endif
    end do
end do
do i=1,H
    do j=1,W
        if(ans(i,j)==-1)then
            if(j==1)cycle
            if(ans(i,j-1)>0)then
                ans(i,j)=ans(i,j-1)
            endif
        endif
    end do
    do j=W,1,-1
        if(ans(i,j)==-1)then
            if(j==W)cycle
            if(ans(i,j+1)>0)then
                ans(i,j)=ans(i,j+1)
            endif
        endif
    end do
end do
do i=1,H
    if(ans(i,1)<0)then
        if(i==1)cycle
        if(ans(i-1,1)>0)then
            ans(i,:)=ans(i-1,:)
        endif
    endif
end do
do i=H,1,-1
    if(ans(i,1)<0)then
        if(i==H)cycle
        if(ans(i+1,1)>0)then
            ans(i,:)=ans(i+1,:)
        endif
    endif
end do
do i=1,H
    write(*,"(i0)",advance="no") ans(i,1)
    do j=2,W
        write(*,"(A,i0)",advance="no")" ",ans(i,j)
    end do
    write(*,*)
end do
end

Score: 400 points

Problem Statement

Chokudai made a rectangular cake for contestants in DDCC 2020 Finals.

The cake has H - 1 horizontal notches and W - 1 vertical notches, which divide the cake into H \times W equal sections. K of these sections has a strawberry on top of each of them.

The positions of the strawberries are given to you as H \times W characters s_{i, j} (1 \leq i \leq H, 1 \leq j \leq W). If s_{i, j} is #, the section at the i-th row from the top and the j-th column from the left contains a strawberry; if s_{i, j} is ., the section does not contain one. There are exactly K occurrences of #s.

Takahashi wants to cut this cake into K pieces and serve them to the contestants. Each of these pieces must satisfy the following conditions:

Has a rectangular shape.

Contains exactly one strawberry.

One possible way to cut the cake is shown below:

Find one way to cut the cake and satisfy the condition. We can show that this is always possible, regardless of the number and positions of the strawberries.

Constraints

1 \leq H \leq 300

1 \leq W \leq 300

1 \leq K \leq H \times W

s_{i, j} is # or ..

There are exactly K occurrences of # in s.

Input

Input is given from Standard Input in the following format:

H W K
s_{1, 1} s_{1, 2} \cdots s_{1, W}
s_{2, 1} s_{2, 2} \cdots s_{2, W}
:
s_{H, 1} s_{H, 2} \cdots s_{H, W}

Output

Assign the numbers 1, 2, 3, \dots, K to the K pieces obtained after the cut, in any order. Then, let a_{i, j} be the number representing the piece containing the section at the i-th row from the top and the j-th column from the left of the cake. Output should be in the following format:

a_{1, 1} \ a_{1, 2} \ \cdots \ a_{1, W}
a_{2, 1} \ a_{2, 2} \ \cdots \ a_{2, W}
:
a_{H, 1} \ a_{H, 2} \ \cdots \ a_{H, W}

If multiple solutions exist, any of them will be accepted.

Sample Input 1

3 3 5
#.#
.#.
#.#

Sample Output 1

1 2 2
1 3 4
5 5 4

One way to cut this cake is shown below:

Sample Input 2

3 7 7
#...#.#
..#...#
.#..#..

Sample Output 2

1 1 2 2 3 4 4
6 6 2 2 3 5 5
6 6 7 7 7 7 7

One way to cut this cake is shown below:

Sample Input 3

13 21 106
.....................
.####.####.####.####.
..#.#..#.#.#....#....
..#.#..#.#.#....#....
..#.#..#.#.#....#....
.####.####.####.####.
.....................
.####.####.####.####.
....#.#..#....#.#..#.
.####.#..#.####.#..#.
.#....#..#.#....#..#.
.####.####.####.####.
.....................

Sample Output 3

12 12 23 34 45 45 60 71 82 93 93 2 13 24 35 35 17 28 39 50 50
12 12 23 34 45 45 60 71 82 93 93 2 13 24 35 35 17 28 39 50 50
12 12 56 89 89 89 60 104 82 31 31 46 13 24 35 35 61 61 39 50 50
12 12 67 67 100 100 60 9 9 42 42 57 13 24 6 72 72 72 72 72 72
12 12 78 5 5 5 20 20 20 53 68 68 90 24 6 83 83 83 83 83 83
16 16 27 38 49 49 64 75 86 97 79 79 90 101 6 94 94 105 10 21 21
16 16 27 38 49 49 64 75 86 97 79 79 90 101 6 94 94 105 10 21 21
32 32 43 54 65 65 80 11 106 95 22 22 33 44 55 55 70 1 96 85 85
32 32 43 54 76 76 91 11 106 84 84 4 99 66 66 66 81 1 96 74 74
14 14 3 98 87 87 102 11 73 73 73 4 99 88 77 77 92 92 63 63 63
25 25 3 98 87 87 7 29 62 62 62 15 99 88 77 77 103 19 30 52 52
36 36 47 58 69 69 18 29 40 51 51 26 37 48 59 59 8 19 30 41 41
36 36 47 58 69 69 18 29 40 51 51 26 37 48 59 59 8 19 30 41 41


program DISCO2020yD
integer::M
integer(16)::d,c
integer(16)::bonus
integer(16)::ans
read*,M
ans=0
bonus=0
do i=1,M
  read*,d,c
  bonus=bonus+d*c
  ans=ans+c
end do
print"(i0)",ans+(bonus-1_16)/9_16 -1_16
end

Score: 500 points

Problem Statement

N programmers are going to participate in the preliminary stage of DDCC 20XX. Due to the size of the venue, however, at most 9 contestants can participate in the finals.

The preliminary stage consists of several rounds, which will take place as follows:

All the N contestants will participate in the first round.

When X contestants participate in some round, the number of contestants advancing to the next round will be decided as follows:

The organizer will choose two consecutive digits in the decimal notation of X, and replace them with the sum of these digits. The number resulted will be the number of contestants advancing to the next round.

For example, when X = 2378, the number of contestants advancing to the next round will be 578 (if 2 and 3 are chosen), 2108 (if 3 and 7 are chosen), or 2315 (if 7 and 8 are chosen).

When X = 100, the number of contestants advancing to the next round will be 10, no matter which two digits are chosen.

The preliminary stage ends when 9 or fewer contestants remain.

Ringo, the chief organizer, wants to hold as many rounds as possible.
Find the maximum possible number of rounds in the preliminary stage.

Since the number of contestants, N, can be enormous, it is given to you as two integer sequences d_1, \ldots, d_M and c_1, \ldots, c_M, which means the following: the decimal notation of N consists of c_1 + c_2 + \ldots + c_M digits, whose first c_1 digits are all d_1, the following c_2 digits are all d_2, \ldots, and the last c_M digits are all d_M.

Constraints

1 \leq M \leq 200000

0 \leq d_i \leq 9

d_1 \neq 0

d_i \neq d_{i+1}

c_i \geq 1

2 \leq c_1 + \ldots + c_M \leq 10^{15}

Input

Input is given from Standard Input in the following format:

M
d_1 c_1
d_2 c_2
:
d_M c_M

Output

Print the maximum possible number of rounds in the preliminary stage.

Sample Input 1

2
2 2
9 1

Sample Output 1

3

In this case, N = 229 contestants will participate in the first round. One possible progression of the preliminary stage is as follows:

229 contestants participate in Round 1, 49 contestants participate in Round 2, 13 contestants participate in Round 3, and 4 contestants advance to the finals.

Here, three rounds take place in the preliminary stage, which is the maximum possible number.

Sample Input 2

3
1 1
0 8
7 1

Sample Output 2

9

In this case, 1000000007 will participate in the first round.


integer(16)::mo = 10**9 + 7
integer(16) h, w, t
integer(16) gh,gw,ans
read*,h,w,t
gh = gcd(h, t)
gw = gcd(w, t)
h =h/ gh
w =w/ gw
ans = mod((power(2_16, h) + power(2_16, w) - 1 + mo),mo)
ans = mod((ans + power(2_16,gcd(h, w)) - 2 + mo),mo)
print"(i0)",power(ans, gh * gw)
contains
integer(16) recursive function power(x,y)result(res)
    integer(16) x,y
    res=1
    if (y==0)then
        return
    endif
    res=mod(power(x,y-1)*x,mo)
end function
integer(16) recursive function gcd(a, b)result(res)
  integer(16), intent(in)::a, b
  res=merge(b, gcd(b, mod(a, b)),mod(a, b)==0)
end function
end

Score: 900 points

Problem Statement

In 2937, DISCO creates a new universe called DISCOSMOS to celebrate its 1000-th anniversary.

DISCOSMOS can be described as an H \times W grid. Let (i, j) (1 \leq i \leq H, 1 \leq j \leq W) denote the square at the i-th row from the top and the j-th column from the left.

At time 0, one robot will be placed onto each square. Each robot is one of the following three types:

Type-H: Does not move at all.

Type-R: If a robot of this type is in (i, j) at time t, it will be in (i, j+1) at time t+1. If it is in (i, W) at time t, however, it will be instead in (i, 1) at time t+1. (The robots do not collide with each other.)

Type-D: If a robot of this type is in (i, j) at time t, it will be in (i+1, j) at time t+1. If it is in (H, j) at time t, however, it will be instead in (1, j) at time t+1.

There are 3^{H \times W} possible ways to place these robots. In how many of them will every square be occupied by one robot at times 0, T, 2T, 3T, 4T, and all subsequent multiples of T?

Since the count can be enormous, compute it modulo (10^9 + 7).

Constraints

1 \leq H \leq 10^9

1 \leq W \leq 10^9

1 \leq T \leq 10^9

H, W, T are all integers.

Input

Input is given from Standard Input in the following format:

H W T

Output

Print the number of ways to place the robots that satisfy the condition, modulo (10^9 + 7).

Sample Input 1

2 2 1

Sample Output 1

9

Shown below are some of the ways to place the robots that satisfy the condition, where ., >, and v stand for Type-H, Type-R, and Type-D, respectively:

>> .. vv
.. .. vv

Sample Input 2

869 120 1001

Sample Output 2

672919729


program main
 integer r
 read(*,*) r
 write(*,*) r*r
end program main

Score : 100 points

Problem Statement

Given is an integer r.

How many times is the area of a circle of radius r larger than the area of a circle of radius 1?

It can be proved that the answer is always an integer under the constraints given.

Constraints

1 \leq r \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r

Output

Print the area of a circle of radius r, divided by the area of a circle of radius 1, as an integer.

Sample Input 1

2

Sample Output 1

4

The area of a circle of radius 2 is 4 times larger than the area of a circle of radius 1.

Note that output must be an integer - for example, 4.0 will not be accepted.

Sample Input 2

100

Sample Output 2

10000


      PROGRAM circle
      IMPLICIT NONE
      INTEGER(8) :: r
      INTEGER(8) :: ans

      read*,r

      ans = r*r

      print*,ans

      END PROGRAM


program ABC145_B
integer N
character(200)S
logical check
read*,N
read*,S

check=.true.
if(mod(N,2)==1)check=.false.
if(S(1:N/2)/=S(N/2+1:N))check=.false.

if(check)then
  print"(A)","Yes"
else
  print"(A)","No"
endif
end program

Score : 200 points

Problem Statement

Given are a positive integer N and a string S of length N consisting of lowercase English letters.

Determine whether the string is a concatenation of two copies of some string.
That is, determine whether there is a string T such that S = T + T.

Constraints

1 \leq N \leq 100

S consists of lowercase English letters.

|S| = N

Input

Input is given from Standard Input in the following format:

N
S

Output

If S is a concatenation of two copies of some string, print Yes; otherwise, print No.

Sample Input 1

6
abcabc

Sample Output 1

Yes

Let T = abc, and S = T + T.

Sample Input 2

6
abcadc

Sample Output 2

No

Sample Input 3

1
z

Sample Output 3

No


program main

  implicit none
  integer :: i,j,n
  integer, allocatable :: x(:), y(:)
  real :: dis, tmp

  read(*,*) n
  allocate( x(n) )
  allocate( y(n) )

  do i = 1, n
    read(*,*) x(i), y(i)
  end do

  dis = 0e0
  do i = 1, n
    do j = i, n
      if ( j == i ) cycle
      tmp = (x(i) - x(j))**2e0 + (y(i) - y(j))**2e0
      dis = dis + sqrt( tmp )
    end do
  end do
  do i = 1, n
    dis = dis / real( i )
  end do
  print*, dis

  deallocate( x )
  deallocate( y )
end program main

Score : 300 points

Problem Statement

There are N towns in a coordinate plane. Town i is located at coordinates (x_i, y_i). The distance between Town i and Town j is \sqrt{\left(x_i-x_j\right)^2+\left(y_i-y_j\right)^2}.

There are N! possible paths to visit all of these towns once. Let the length of a path be the distance covered when we start at the first town in the path, visit the second, third, \dots, towns, and arrive at the last town (assume that we travel in a straight line from a town to another). Compute the average length of these N! paths.

Constraints

2 \leq N \leq 8

-1000 \leq x_i \leq 1000

-1000 \leq y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) (if i \neq j)

(Added 21:12 JST) All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the average length of the paths.
Your output will be judges as correct when the absolute difference from the judge's output is at most 10^{-6}.

Sample Input 1

3
0 0
1 0
0 1

Sample Output 1

2.2761423749

There are six paths to visit the towns: 1 → 2 → 3, 1 → 3 → 2, 2 → 1 → 3, 2 → 3 → 1, 3 → 1 → 2, and 3 → 2 → 1.

The length of the path 1 → 2 → 3 is \sqrt{\left(0-1\right)^2+\left(0-0\right)^2} + \sqrt{\left(1-0\right)^2+\left(0-1\right)^2} = 1+\sqrt{2}.

By calculating the lengths of the other paths in this way, we see that the average length of all routes is:

\frac{\left(1+\sqrt{2}\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)}{6} = 2.276142...

Sample Input 2

2
-879 981
-866 890

Sample Output 2

91.9238815543

There are two paths to visit the towns: 1 → 2 and 2 → 1. These paths have the same length.

Sample Input 3

8
-406 10
512 859
494 362
-955 -475
128 553
-986 -885
763 77
449 310

Sample Output 3

7641.9817824387


program ABC145_B
type xy
  real(16)::x,y
end type
integer N
type(xy),allocatable::town(:)
real(16) average
read*,N
allocate(town(N))
do i=1,N
    read*,town(i)%x,town(i)%y
end do
average=0
do i=1,N-1
    do j=i+1,N
        average=average+dist(town(i),town(j))
    end do
end do
average=average/real(N*(N-1)/2)
print"(f0.14)",average*(N-1)
contains
pure real(16) function dist(a,b)
    type(xy),intent(in)::a,b
    dist=sqrt((a%x-b%x)**2+(a%y-b%y)**2)
end function
end program


program main
  implicit none
  integer(8) :: x, y, z, m
  read (*, *) x, y
  if (mod(x + y, 3_8) /= 0) then
    write (*, "(i0)") 0
    return
  end if
  z = (x + y) / 3
  if (x < z .or. y < z) then
    write (*, "(i0)") 0
    return
  end if

  m = 1000000007
  write(*, "(i0)") combi(z, x - z, m)
  contains

  function combi(n, k, m)
    implicit none
    integer(8), intent(in) :: n, k, m
    integer(8) :: combi, numera, denomi
    numera = perm(n, k, m)
    denomi = mod(fact(k, m), m)
    combi = mod(numera * inv(denomi, m), m)
  end function combi

  function perm(n, k, m)
    integer(8), intent(in) :: n, k, m
    integer(8) :: i, perm
    perm = 1
    do i = n, n - k + 1, -1
      perm = mod(perm * i, m)
    end do
  end function perm

  function fact(n, m)
    implicit none
    integer(8), intent(in) :: n, m
    integer(8) :: i, fact
    fact = 1
    do i = 1, n
      fact = mod(fact * i, m)
    end do
  end function fact

  function inv(n, m)
    implicit none
    integer(8), intent(in) :: n, m
    integer(8) :: inv
    inv = pow(n, m - 2, m)
  end function inv

  recursive function pow(n, p, m) result(res)
    implicit none
    integer(8), intent(in) :: n, p, m
    integer(8) :: res
    if (p == 0) then
      res = 1
    else if (mod(p, 2_8) == 0) then
      res = mod(pow(n, p / 2, m) ** 2, m)
    else
      res = mod(pow(n, p - 1, m) * n, m)
    end if
  end function pow
end program main

Score : 400 points

Problem Statement

There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.

When the knight is at the square (i, j), it can be moved to either (i+1,j+2) or (i+2, j+1).

In how many ways can the knight reach the square (X, Y)?

Find the number of ways modulo 10^9 + 7.

Constraints

1 \leq X \leq 10^6

1 \leq Y \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the number of ways for the knight to reach (X, Y) from (0, 0), modulo 10^9 + 7.

Sample Input 1

3 3

Sample Output 1

2

There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3).

Sample Input 2

2 2

Sample Output 2

0

The knight cannot reach (2,2).

Sample Input 3

999999 999999

Sample Output 3

151840682

Print the number of ways modulo 10^9 + 7.


program kadai
    implicit none
integer(8)  ::  x,y,i
integer(8)  ::  newx,newy
integer(8),allocatable  ::  ue(:),shita1(:),shita2(:)
integer(8)  ::  ans

read(*,*) x,y

newx=-x+2*y
newy=2*x-y

if (mod(newx,3) /= 0 .or. mod(newy,3) /=0 .or. newx<0 .or. newy<0) then
    write(*,*) 0
    stop
end if
newx=newx/3
newy=newy/3

allocate(ue(newx+newy))
allocate(shita1(10**9+7))
allocate(shita2(newy+newy))

ue(1)=1
shita1(1)=1
shita2(1)=1

do i=2,newx+newy
    ue(i)=mod(ue(i-1)*i,10**9+7)
    shita1(i)=10**9+7-mod(shita1(mod(10**9+7,i)) * ((10**9+7)/i) , 10**9+7)
    shita2(i)=mod(shita2(i-1)*shita1(i),10**9+7)
end do

!do i=1,10**9+5
!    ans=mod(ans*shita1*shita2,10**9+7)
!end do

ans=mod(ue(newx+newy)*mod(shita2(newx)*shita2(newy),10**9+7),10**9+7)
write(*,*) ans

stop
contains
end program kadai


program abc145e
type data
  integer::A,B
end type
integer N,T
type(data),allocatable::menu(:)
integer,allocatable::DP1(:,:),DP2(:,:)
integer ans
read*,N,T
allocate(DP1(0:N+1,0:T),DP2(0:N+1,0:T))
allocate(menu(N))
do i=1,N
    read*,menu(i)%A,menu(i)%B
end do

DP1=0;DP2=0
do i = 1,N
	do j = 0,T
    	if (j >= menu(i)%A)then
        	dp1(i+1,j) = max(dp1(i,J-menu(i)%A) +menu(i)%B, dp1(i,j))
        else
            dp1(i+1,j) = dp1(i,j)
        endif
    end do
end do
do i = N,1,-1
	do j = 0,T
    	if (j >= menu(i)%A)then
        	dp2(i-1,j) = max(dp2(i,J-menu(i)%A) +menu(i)%B, dp2(i,j))
        else
            dp2(i-1,j) = dp2(i,j)
        endif
    end do
end do
ans=0
do j=1,N
  do i=0,T-1
    ans=max(ans,DP1(j,i)+DP2(j,T-1-i)+menu(j)%B)
  end do
end do
print"(i0)",ans
end program

Score : 500 points

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

The restaurant has the following rules:

You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.

You cannot order the same kind of dish more than once.

Until you finish eating the dish already served, you cannot order a new dish.

After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

What is the maximum possible happiness achieved by making optimal choices?

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145


program laminate
  implicit none
  integer(8), parameter :: inf = 10000000000000000_8
  integer :: n, k, i, j, l
  integer(8) :: h(0:300) = 0_8, dp(0:300,0:300) = inf
  read(*,*) n, k
  read(*,*) h(1:n)
  dp(:,0) = 0_8
  do i = 1, n
    do j = 1, min(i,n-k)
      do l = 0, i-1
        dp(i,j) = min(dp(i,j),dp(l,j-1)+max(0_8,h(i)-h(l)))
      end do
    end do
  end do
  write(*,'(i0)') minval(dp(1:n,n-k))
end program laminate

Score : 600 points

Problem Statement

We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.

The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.

Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).

Different values can be chosen for different columns.

Then, you will create the modified artwork by repeating the following operation:

Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)

Find the minimum number of times you need to perform this operation.

Constraints

1 \leq N \leq 300

0 \leq K \leq N

0 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 H_2 ... H_N

Output

Print the minimum number of operations required.

Sample Input 1

4 1
2 3 4 1

Sample Output 1

3

For example, by changing the value of H_3 to 2, you can create the modified artwork by the following three operations:

Paint black the 1-st through 4-th squares from the left in the 1-st row from the bottom.

Paint black the 1-st through 3-rd squares from the left in the 2-nd row from the bottom.

Paint black the 2-nd square from the left in the 3-rd row from the bottom.

Sample Input 2

6 2
8 6 9 1 2 1

Sample Output 2

7

Sample Input 3

10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000

Sample Output 3

4999999996


Score : 700 points

Problem Statement

Given are positive integers N and K.

Determine if the 3N integers K, K+1, ..., K+3N-1 can be partitioned into N triples (a_1,b_1,c_1), ..., (a_N,b_N,c_N) so that the condition below is satisfied. Any of the integers K, K+1, ..., K+3N-1 must appear in exactly one of those triples.

For every integer i from 1 to N, a_i + b_i \leq c_i holds.

If the answer is yes, construct one such partition.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq 10^9

Input

Input is given from Standard Input in the following format:

N K

Output

If it is impossible to partition the integers satisfying the condition, print -1. If it is possible, print N triples in the following format:

a_1 b_1 c_1
:
a_N b_N c_N

Sample Input 1

1 1

Sample Output 1

1 2 3

Sample Input 2

3 3

Sample Output 2

-1


program non_triangular_triplets
  implicit none
  integer(8) :: n, k, i
  read(*,*) n, k
  if (2_8*k+2_8*n-1_8 <= k+2_8*n) then
    do i = 0_8, n-1_8
      write(*,'(i0,x,i0,x,i0)') k+i, k+2_8*n-1_8-i, k+2_8*n+i
    end do
  else
    write(*,'(i0)') -1
  end if
end program non_triangular_triplets


program main
  implicit none
  character(5*10**5) :: s
  integer ::  n, i, k, j, l, m, ans
  integer, allocatable :: a(:)

  read(*,*) s
  n = len_trim(s) + 1

  ans = 0
  do i = 1, n-1
     if(s(i:i) == '<') then
        k = i
     do j = 1, n-1
        if (s(j:j) == '>') then
           l = j
           exit
        end if
     end do

     do j = l, n-1
        if(s(j:j) == '<') then
           m = i
           exit
        end if
     end do

     if (l >= m) then
        ans = ans + l*(l+1)/2 + m*(m-1)/2
     else
        ans = ans + m*(m+1)/2 + l*(l-1)/2
     end if

  end if
end do

write(*,*) ans

stop
end program main

Score : 300 points

Problem Statement

Given is a string S of length N-1.
Each character in S is < or >.

A sequence of N non-negative integers, a_1,a_2,\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \leq i \leq N-1):

If S_i= <: a_i<a_{i+1}

If S_i= >: a_i>a_{i+1}

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Constraints

2 \leq N \leq 5 \times 10^5

S is a string of length N-1 consisting of < and >.

Input

Input is given from Standard Input in the following format:

S

Output

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

Sample Input 1

<>>

Sample Output 1

3

a=(0,2,1,0) is a good sequence whose sum is 3.
There is no good sequence whose sum is less than 3.

Sample Input 2

<>>><<><<<<<>>><

Sample Output 2

28


program abc144b
   implicit none
   integer :: N, i

   read(*,*) N

   do i = 1, 9
      if (mod(N,i) == 0) then
         if (N/i <= 9) then
            write(*,*) "Yes"
            stop
         end if
      end if
   end do

   write(*,*) "No"

end program abc144b

Score : 200 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.

Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No.

Sample Input 1

10

Sample Output 1

Yes

10 can be represented as, for example, 2 \times 5.

Sample Input 2

50

Sample Output 2

No

50 cannot be represented as the product of two integers between 1 and 9.

Sample Input 3

81

Sample Output 3

Yes


program ABC144B
    implicit none
    integer::N,i
    integer::result=0

    read*,N

    do i=1,9
        if(mod(N,i)==0 .and. N/i<=9) then
            result=1
        endif
    end do

    if(result==0) then
        print'(A)',"No"
    else
        print'(A)',"Yes"
    end if
end program ABC144B


program main
  implicit none

  integer(16) :: i,j,n,m,l,p=0,q=0

  read(*,*)n
  m = int(sqrt(real(n)))
  do i = 1, m
    l = n/i
    if (i*l == n) then
      p = i
      q = l
    end if
  end do
  write(*,'(i0)')p+q-2
end program main

Score : 300 points

Problem Statement

Takahashi is standing on a multiplication table with infinitely many rows and columns.

The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).

In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).

Given an integer N, find the minimum number of moves needed to reach a square that contains N.

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum number of moves needed to reach a square that contains the integer N.

Sample Input 1

10

Sample Output 1

5

(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.

Sample Input 2

50

Sample Output 2

13

(5, 10) can be reached in 13 moves.

Sample Input 3

10000000019

Sample Output 3

10000000018

Both input and output may be enormous.


program prob4
    implicit none
    integer::a,b,x
    real(8)::aa,bb,xx
    real(8),parameter::pi=3.141592653589793_8
    read(*,*) a,b,x
    aa = dble(a)
    bb = dble(b)
    xx = dble(x)
    if(2*x < a*a*b) then
        write(*,*) 90.0_8 - atan(2.0_8*xx / (aa*bb*bb))*180.0_8/pi
    else
        write(*,*) atan((2.0_8*bb - 2.0_8*xx/aa**2) / aa)*180.0_8/pi
    end if
    stop
end program

Score : 400 points

Problem Statement

Takahashi has a water bottle with the shape of a rectangular prism whose base is a square of side a~\mathrm{cm} and whose height is b~\mathrm{cm}. (The thickness of the bottle can be ignored.)

We will pour x~\mathrm{cm}^3 of water into the bottle, and gradually tilt the bottle around one of the sides of the base.

When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.

Constraints

All values in input are integers.

1 \leq a \leq 100

1 \leq b \leq 100

1 \leq x \leq a^2b

Input

Input is given from Standard Input in the following format:

a b x

Output

Print the maximum angle in which we can tilt the bottle without spilling any water, in degrees.
Your output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

2 2 4

Sample Output 1

45.0000000000

This bottle has a cubic shape, and it is half-full. The water gets spilled when we tilt the bottle more than 45 degrees.

Sample Input 2

12 21 10

Sample Output 2

89.7834636934

This bottle is almost empty. When the water gets spilled, the bottle is nearly horizontal.

Sample Input 3

3 1 8

Sample Output 3

4.2363947991

This bottle is almost full. When the water gets spilled, the bottle is still nearly vertical.


program problemE
  implicit none

  integer, parameter     :: double = selected_real_kind ( 9 )
  integer, parameter     ::     i8 = selected_int_kind  ( 18 )
  real ( kind = double ), parameter :: pi = 3.141592653589790_double

  integer, allocatable, dimension ( : ) :: mxl
  integer ( kind = i8 )                               :: N, K, i, j, tmp, ans, min
  integer ( kind = i8 ), allocatable, dimension ( : ) :: A, F, AF, sA, sF

  read *, N, K

  allocate ( mxl ( 1:1 ) )
  allocate ( A   ( 1:N ) )
  allocate ( F   ( 1:N ) )
  allocate ( AF  ( 1:N ) )
  allocate ( sA  ( 1:N ) )
  allocate ( sF  ( 1:N ) )

  read *, A ( 1:N )
  read *, F ( 1:N )

  call sort ( N, A, sA, F, sF )
  AF ( 1:N ) = sA ( 1:N ) * sF ( 1:N )

  do i = 1, K
     mxl ( 1:1 ) = maxloc ( AF ( 1:N ) )
     if ( AF ( mxl ( 1 ) ) == 0 ) then
        exit
     end if
     sA ( mxl ( 1 ) ) = sA ( mxl ( 1 ) ) - 1_i8
     AF ( mxl ( 1 ) ) = sA ( mxl ( 1 ) ) * sF ( mxl ( 1 ) )
  end do
  AF ( 1:N ) = sA ( 1:N ) * sF ( 1:N )

  ans = maxval ( AF ( 1:N ) )
  print *, ans

contains

  subroutine sort ( n, lagsrc, lagrtn, smasrc, smartn )
    implicit none

!!! parameter ==================================================

    integer, parameter :: i8 = selected_int_kind  ( 18 )
    integer ( kind = i8 ) :: i, max, min, imax, imin
    integer ( kind = i8 ),                               intent ( in )  :: n
    integer ( kind = i8 ), allocatable, dimension ( : ), intent ( in )  :: lagsrc, smasrc
    integer ( kind = i8 ), allocatable, dimension ( : ), intent ( out ) :: lagrtn, smartn
    integer ( kind = i8 ), allocatable, dimension ( : )                 :: lagtmp, smatmp

!!! allocate ==================================================

    allocate ( lagrtn ( n ) )
    allocate ( lagtmp ( n ) )
    allocate ( smartn ( n ) )
    allocate ( smatmp ( n ) )

    max  = maxval ( lagsrc ( 1:n ) )
    imax = maxval ( lagsrc ( 1:n ) )
    min  = minval ( lagsrc ( 1:n ) )
    imin = minval ( lagsrc ( 1:n ) )

!!! sort ==================================================

    max  = maxval ( lagsrc ( 1:n ) )
    lagtmp = pack ( lagsrc, lagsrc == max )
    lagrtn = [ lagtmp ( : ) ]

    min  = minval ( smasrc ( 1:n ) )
    smatmp = pack ( smasrc, smasrc == min )
    smartn = [ smatmp ( : ) ]

    do i = imin, imax
       max = max - 1
       lagtmp = pack ( lagsrc, lagsrc == max )
       lagrtn = [ lagrtn, lagtmp ]

       min = min + 1
       smatmp = pack ( smasrc, smasrc == min )
       smartn = [ smartn, smatmp ]
    end do

!!! ==================================================

  end subroutine sort

end program problemE

Score: 500 points

Problem Statement

Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.

In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:

A team should assign one member to each food, and should not assign the same member to multiple foods.

It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.

The score of a team is the longest time it takes for an individual member to finish the food.

Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.

What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

0 \leq K \leq 10^{18}

1 \leq A_i \leq 10^6\ (1 \leq i \leq N)

1 \leq F_i \leq 10^6\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N

Output

Print the minimum possible score of the team.

Sample Input 1

3 5
4 2 1
2 3 1

Sample Output 1

2

They can achieve the score of 2, as follows:

Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.

Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.

Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.

They cannot achieve a score of less than 2, so the answer is 2.

Sample Input 2

3 8
4 2 1
2 3 1

Sample Output 2

0

They can choose not to do exactly K sets of training.

Sample Input 3

11 14
3 1 4 1 5 9 2 6 5 3 5
8 9 7 9 3 2 3 8 4 6 2

Sample Output 3

12


program problemE
  implicit none

  integer, parameter     :: double = selected_real_kind ( 9 )
  integer, parameter     :: i8 = selected_int_kind  ( 18 )
  real ( kind = double ), parameter :: pi = 3.141592653589790_double

  integer ( kind = i8 )                               :: N, K, i, j, tmp, ans, min
  integer ( kind = i8 ), allocatable, dimension ( : ) :: A, F, AF, sA, sF

  read *, N, K

  allocate ( A  ( 1:N ) )
  allocate ( F  ( 1:N ) )
  allocate ( AF ( 1:N ) )
  allocate ( sA ( 1:N ) )
  allocate ( sF ( 1:N ) )

  read *, A ( 1:N )
  read *, F ( 1:N )

  call sort ( N, A, sA, F, sF )

  do i = 1, K
     AF ( 1:N ) = sA ( 1:N ) * sF ( 1:N )
     if ( sum ( AF ( 1:N ) ) == 0 ) then
        exit
     end if
     sA ( maxloc ( AF ( 1:N ) ) ) = sA ( maxloc ( AF ( 1:N ) ) ) - 1
  end do
  AF ( 1:N ) = sA ( 1:N ) * sF ( 1:N )

  ans = maxval ( AF ( 1:N ) )
  print *, ans

contains

  subroutine sort ( n, lagsrc, lagrtn, smasrc, smartn )
    implicit none

!!! parameter ==================================================

    integer, parameter :: i8 = selected_int_kind  ( 18 )
    integer ( kind = i8 ),                               intent ( in )  :: n
    integer ( kind = i8 ), allocatable, dimension ( : ), intent ( in )  :: lagsrc, smasrc
    integer ( kind = i8 ), allocatable, dimension ( : ), intent ( out ) :: lagrtn, smartn
    integer ( kind = i8 ), allocatable, dimension ( : ) :: lagtmp, smatmp
    integer ( kind = i8 ) :: i, max, min, imax, imin

!!! allocate ==================================================

    allocate ( lagrtn ( n ) )
    allocate ( lagtmp ( n ) )
    allocate ( smartn ( n ) )
    allocate ( smatmp ( n ) )

    max  = maxval ( lagsrc ( 1:n ) )
    imax = maxval ( lagsrc ( 1:n ) )
    min  = minval ( lagsrc ( 1:n ) )
    imin = minval ( lagsrc ( 1:n ) )

!!! sort ==================================================

    max  = maxval ( lagsrc ( 1:n ) )
    lagtmp = pack ( lagsrc, lagsrc == max )
    lagrtn = [ lagtmp ( : ) ]

    min  = minval ( smasrc ( 1:n ) )
    smatmp = pack ( smasrc, smasrc == min )
    smartn = [ smatmp ( : ) ]

    do i = imin, imax
       max = max - 1
       lagtmp = pack ( lagsrc, lagsrc == max )
       lagrtn = [ lagrtn, lagtmp ]

       min = min + 1
       smatmp = pack ( smasrc, smasrc == min )
       smartn = [ smartn, smatmp ]
    end do

!!! ==================================================

  end subroutine sort

end program problemE


program bbb
implicit none

integer :: n, i, j, res
integer,allocatable,dimension(:) :: d

read*, n
allocate(d(n))
read*, d

res=0

do i=1,n
 do j=i+1,n
 res=res+d(i)*d(j)
 end do
end do

write(*,'(i0)') res

end program

Score : 200 points

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!

This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.

As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.

There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.

Constraints

All values in input are integers.

2 \leq N \leq 50

0 \leq d_i \leq 100

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.

Sample Input 1

3
3 1 2

Sample Output 1

11

There are three possible choices:

Eat the first and second takoyaki. You will restore 3 health points.

Eat the second and third takoyaki. You will restore 2 health points.

Eat the first and third takoyaki. You will restore 6 health points.

The sum of these values is 11.

Sample Input 2

7
5 0 7 8 3 3 2

Sample Output 2

312


program TAKOYAKI_FESTIVAL_2019
  implicit none
  integer :: n, d(50) = 0
  read(*,*) n
  read(*,*) d(1:n)
  write(*,'(i0)') (sum(d)**2-sum(d**2))/2
end program TAKOYAKI_FESTIVAL_2019


program ccc
implicit none

integer :: n, i, res
character(100000) :: s
character(1) :: che

read*, n
read*, s

che=s(1:1)
res=1

do i=2, n
	if(s(i:i)/=che) then
		res=res+1
	end if
	che=s(i:i)
end do

write(*,'(i0)') res

end program

Score : 300 points

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

Ultimately, how many slimes will be there?

Constraints

1 \leq N \leq 10^5

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final number of slimes.

Sample Input 1

10
aabbbbaaca

Sample Output 1

5

Ultimately, these slimes will fuse into abaca.

Sample Input 2

5
aaaaa

Sample Output 2

1

All the slimes will fuse into one.

Sample Input 3

20
xxzaffeeeeddfkkkkllq

Sample Output 3

10


program incenters
  implicit none
  real(8), parameter :: pi = 3.14159265358979d0
  integer :: n, l, t, i, j, k
  real(8) :: v(3000,2) = 0.d0, c(2) = 0.d0, s(0:3000,2) = 0.d0
  real(8) :: di, dj, dk
  character(48) :: sx, sy
  read(*,*) n, l
  do i = 1, n
    read(*,*) t
    v(i,1) = cos(2.d0*pi*real(t,8)/real(l,8))
    v(i,2) = sin(2.d0*pi*real(t,8)/real(l,8))
    s(i,:) = s(i-1,:)+v(i,:)
  end do
  do i = 1, n-2
    do j = i+1, n-1
      do k = j+1, n
        di = d(v(j,:),v(k,:))
        dj = d(v(k,:),v(i,:))
        dk = d(v(i,:),v(j,:))
        c = c+(di*v(i,:)+dj*v(j,:)+dk*v(k,:))/(di+dj+dk)
      end do
    end do
  end do
  c = 6.d0*c/(real(n,8)*real(n-1,8)*real(n-2,8))
  write(sx,'(f32.16)') c(1)
  write(sy,'(f32.16)') c(2)
  write(*,'(a,x,a)') trim(adjustl(sx)), trim(adjustl(sy))
contains
  real(8) function d(v1,v2)
    real(8), intent(in) :: v1(2), v2(2)
    d = sqrt(dot_product(v1-v2,v1-v2))
  end
end program incenters

Score : 1000 points

Problem Statement

Given are N points on the circumference of a circle centered at (0,0) in an xy-plane.
The coordinates of the i-th point are (\cos(\frac{2\pi T_i}{L}),\sin(\frac{2\pi T_i}{L})).

Three distinct points will be chosen uniformly at random from these N points.
Find the expected x- and y-coordinates of the center of the circle inscribed in the triangle formed by the chosen points.

Constraints

3 \leq N \leq 3000

N \leq L \leq 10^9

0 \leq T_i \leq L-1

T_i<T_{i+1}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L
T_1
:
T_N

Output

Print the expected x- and y-coordinates of the center of the circle inscribed in the triangle formed by the chosen points.
Your output will be considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 4
0
1
3

Sample Output 1

0.414213562373095 -0.000000000000000

The three points have the coordinates (1,0), (0,1), and (0,-1). The center of the circle inscribed in the triangle formed by these points is (\sqrt{2}-1,0).

Sample Input 2

4 8
1
3
5
6

Sample Output 2

-0.229401949926902 -0.153281482438188

Sample Input 3

10 100
2
11
35
42
54
69
89
91
93
99

Sample Output 3

0.352886583546338 -0.109065017701873


program main
 implicit none
 integer N,k
 real ans
 read(*,*) N
 k = (N+1) / 2
 ans = real(k) / real(N)
 write(*,*) ans
end program main

Score : 100 points

Problem Statement

Given is an integer N.

Takahashi chooses an integer a from the positive integers not greater than N with equal probability.

Find the probability that a is odd.

Constraints

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the probability that a is odd.
Your output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-6}.

Sample Input 1

4

Sample Output 1

0.5000000000

There are four positive integers not greater than 4: 1, 2, 3, and 4. Among them, we have two odd numbers: 1 and 3. Thus, the answer is \frac{2}{4} = 0.5.

Sample Input 2

5

Sample Output 2

0.6000000000

Sample Input 3

1

Sample Output 3

1.0000000000


program bbb

implicit none
integer :: n, k, i, co
integer, allocatable, dimension(:) :: h

read*, n, k
allocate(h(n))
read*, h

co = 0

do i=1,n
if (h(i)>=k) co=co+1
end do

write(*,'(i0)') co

end program

Score : 200 points

Problem Statement

N friends of Takahashi has come to a theme park.

To ride the most popular roller coaster in the park, you must be at least K centimeters tall.

The i-th friend is h_i centimeters tall.

How many of the Takahashi's friends can ride the roller coaster?

Constraints

1 \le N \le 10^5

1 \le K \le 500

1 \le h_i \le 500

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the number of people among the Takahashi's friends who can ride the roller coaster.

Sample Input 1

4 150
150 140 100 200

Sample Output 1

2

Two of them can ride the roller coaster: the first and fourth friends.

Sample Input 2

1 500
499

Sample Output 2

0

Sample Input 3

5 1
100 200 300 400 500

Sample Output 3

5


program roller_coaster
  implicit none
  integer :: n, k, h(100000) = 0, i
  read(*,*) n, k
  read(*,*) h(1:n)
  write(*,'(i0)') sum([(1,i=1,n)],mask=h(1:n)>=k)
end program roller_coaster


program go_to_school
    implicit none
    integer n, i
    integer a(100000), b(100000)
    read *, n
    read *, a(1:n)
    do i = 1, n
        b(a(i)) = i
    end do
    write(*,*) b(1:n)
end program

Score : 300 points

Problem Statement

Takahashi is a teacher responsible for a class of N students.

The students are given distinct student numbers from 1 to N.

Today, all the students entered the classroom at different times.

According to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i).

From these records, reconstruct the order in which the students entered the classroom.

Constraints

1 \le N \le 10^5

1 \le A_i \le N

A_i \neq A_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the student numbers of the students in the order the students entered the classroom.

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

First, student number 3 entered the classroom.

Then, student number 1 entered the classroom.

Finally, student number 2 entered the classroom.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

8 2 4 5 6 7 3 1


program main
  implicit none

  integer :: n, a(100000), i, b(100000), j

  read(*,*)n
  read(*,*)(a(i),i = 1, n)

  do i = 1, n
    do j = 1, n
      if (a(j) < i) then
        cycle
      end if
      if (a(j) == i) then
        b(i) = j
        exit
      end if
    end do
  end do
  write(*,*)(b(i),i = 1, n)
end program main


      PROGRAM goToSchool
      IMPLICIT NONE
      INTEGER :: N
      INTEGER,ALLOCATABLE :: A(:),ans(:)

      INTEGER :: i, buffer
      LOGICAL :: flag

      READ*,N
      ALLOCATE( A(N),ans(N) )
      READ*,A(:)

      ans = (/ (i, i = 1,N) /)

      outer:DO while( .true. )
      flag = .true.
        DO i = 1,N-1
          IF( A(i)>A(i+1) )THEN
            buffer = A(i+1)
            A(i+1) = A(i)
            A(i)   = buffer

            buffer   = ans(i+1)
            ans(i+1) = ans(i)
            ans(i)   = buffer

            flag = .false.
          END IF
        END DO
        IF(flag) EXIT outer
      END DO outer

      print*,ans(:)

      END PROGRAM


program sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n,ans
    integer(8)::a,b,c,d
    integer(8),allocatable::y(:)

    read(*,*) a,b
    c=min(a,b)
    d=max(a,b)
    a=c
    b=d

    do i=1,1000
        c=mod(b,a)
        b=a
        a=c
        if(a==0)then
            exit
        end if
    end do
    a=b
    if(a==1)then
        write(*,*)1
        stop
    end if

    c=0
    ans=1
    d=int(sqrt(real(b)))
    do i=2,d+1
        c=0
        do j=1,100
            if(mod(a,i)==0)then
                a=a/i
                if(c==0)then
                    c=1
                    ans=ans+1
                end if
            else
                exit
            end if
        end do
        if(a==1)then
            exit
        end if
    end do
    write(*,*)ans

    stop
end program sample

Score : 400 points

Problem Statement

Given are positive integers A and B.

Let us choose some number of positive common divisors of A and B.

Here, any two of the chosen divisors must be coprime.

At most, how many divisors can we choose?

Definition of common divisor

An integer d is said to be a common divisor of integers x and y when d divides both x and y.

Definition of being coprime

Integers x and y are said to be coprime when x and y have no positive common divisors other than 1.

Definition of dividing

An integer x is said to divide another integer y when there exists an integer \alpha such that y = \alpha x.

Constraints

All values in input are integers.

1 \leq A, B \leq 10^{12}

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of divisors that can be chosen to satisfy the condition.

Sample Input 1

12 18

Sample Output 1

3

12 and 18 have the following positive common divisors: 1, 2, 3, and 6.

1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result.

Sample Input 2

420 660

Sample Output 2

4

Sample Input 3

1 2019

Sample Output 3

1

1 and 2019 have no positive common divisors other than 1.


program main
    integer::p,ans
    integer(8):: a,b,i, sosu
    read*, a,b
    sosu = 0
    i = 1
    do while(real(i) <= min(sqrt(real(a)),sqrt(real(b))))
        i=i+1
        if (.not.((mod(a,i) == 0) .and. (mod(b,i) == 0))) cycle
        sosu=sosu+1

        do while((mod(a,i) == 0) .and. (mod(b,i) == 0))
            a = a/(i)
            b = b/(i)
        end do

    end do
    print*, sosu+1

end program


program main
    integer::p,ans
    integer(8):: a,b,i, sosu
    read*, a,b
    sosu = 0
    i = 1
    do while(i <= min(a,b))
        i=i+1
        if (.not.((mod(a,i) == 0) .and. (mod(b,i) == 0))) cycle
        sosu=sosu+1

        do while((mod(a,i) == 0) .and. (mod(b,i) == 0))
            a = a/(i)
            b = b/(i)
        end do

    end do
    print*, sosu+1

end program


program main
    integer::p,ans
    integer(8):: a,b,i, sosu
    integer(8), pointer:: cd(:)
    read*, a,b
    allocate(cd(1))
    sosu = 0
    i = 1
    do while(i <= min(a,b))
        i=i+1
        if (.not.((mod(a,i) == 0) .and. (mod(b,i) == 0))) cycle
        sosu=sosu+1
        do while((mod(a,i*i) == 0) .and. (mod(b,i*i) == 0))
            a = a/(i*i)
            b = b/(i*i)
        end do
        do while((mod(a,i) == 0) .and. (mod(b,i) == 0))
            a = a/(i)
            b = b/(i)
        end do

    end do
    print*, sosu+1

end program