program sequential_operations_on_sequence
  implicit none
  integer :: n, q, i, u = 1, l, r, m, x
  integer(8) :: p(100000) = 0_8, k, t(100000) = 0_8, ans(100001) = 0_8
  read(*,*) n, q
  p(1) = int(n,8)
  do i = 1, q
    u = u+1
    read(*,*) p(u)
    do while (u > 1 .and. p(u-1) >= p(u))
      p(u-1) = p(u)
      u = u-1
    end do
  end do
  t(u) = 1_8
  do i = u, 1, -1
    k = p(i)
    do
      r = i
      l = 0
      do while (r-l > 1)
        m = (l+r)/2
        if (k < p(m)) then
          r = m
        else
          l = m
        end if
      end do
      if (l == 0) then
        x = int(k,4)
        ans(1) = ans(1)+t(i)
        ans(x+1) = ans(x+1)-t(i)
        exit
      else
        t(l) = t(l)+t(i)*(k/p(l))
        k = mod(k,p(l))
      end if
    end do
  end do
  write(*,'(i0)') ans(1)
  do i = 2, n
    ans(i) = ans(i)+ans(i-1)
    write(*,'(i0)') ans(i)
  end do
end program sequential_operations_on_sequence