program powers_of_two
  implicit none
  integer(8)              :: n, i, j, m, t, p
  integer(8), allocatable :: a(:)
  real(8), parameter      :: loge2 = log(2.d0)
  read(*,*) n
  if (n .eq. 1) then
     write(*,'(i0)') 0
     stop
  end if
  allocate(a(n))
  read(*,*) a(:)
  call merge_sort(n,a)
  m = 0
  do i = 1, n-1
     if (a(i) < 0) cycle
     t = int(log(real(a(i),8))/loge2,8) + 1
     p = 2**t - a(i)
     do j = i+1, n
        if (a(j) < 0) cycle
        if (a(j) < p) exit
        if (a(j) .eq. p) then
           a(j) = -1
           m = m + 1
           exit
        end if
     end do
  end do
  deallocate(a)
  write(*,'(i0)') m
  stop
contains
  subroutine merge_sort(n,c)
    implicit none
    integer(8), intent(in)    :: n
    integer(8), intent(inout) :: c(n)
    integer(8)                :: number, length, i, ubound
    number = n
    length = 1
    do while (number .gt. 1)
       do i = 1, number/2
          ubound = min(2*i*length,n)
          call merge(c(2*(i-1)*length+1:(2*i-1)*length),c((2*i-1)*length+1:ubound))
       end do
       length = 2*length
       number = (number+1)/2
    end do
    return
  end subroutine merge_sort

  subroutine merge(c1,c2)
    implicit none
    integer(8), intent(inout) :: c1(:), c2(:)
    integer(8)                :: c(size(c1)+size(c2))
    integer(8)                :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1)
    n2 = size(c2)
    do while (i1 .le. n1 .and. i2 .le. n2)
       if (c1(i1) .ge. c2(i2)) then
          c(i1+i2-1) = c1(i1)
          i1 = i1+1
       else
          c(i1+i2-1) = c2(i2)
          i2 = i2+1
       end if
    end do
    if (i1 .le. n1) then
       c(i1+i2-1:n1+n2) = c1(i1:n1)
    else if (i2 .le. n2) then
       c(i1+i2-1:n1+n2) = c2(i2:n2)
    end if
    c1 = c(1:n1)
    c2 = c(n1+1:n1+n2)
    return
  end subroutine merge
end program powers_of_two