program main
  implicit none
  integer, parameter :: nbit = 6
  integer :: nb
  integer, parameter :: num = 2**nbit
  integer :: nmax
  integer, dimension(0:num - 1) :: fld
  integer(kind=8) :: count
  logical(kind=1), dimension(0:num-1) :: set
  do nmax=3,num-1,2
     nb = log2u(nmax)
     fld = 0
     set(0) = .true.
     set(1:) = .false.
     count = 0
     call chk_level(1)
     print *,nb,(nmax+1)/2,count
  end do
contains
  recursive subroutine chk_level(i)
    integer, value :: i
    integer :: mask, val
    integer :: k
    do k=0,nb-1
       mask = ishft(1,k)
       val = ieor(fld(i-1), mask)
       if (set(val)) cycle
       fld(i) = val
       if (i < nmax) then
          set(val) = .true.
          call chk_level(i+1)
          set(val) = .false.
       else
          if (popcnt(val) /= 1) cycle
!          write (*,'(*(G0:", "))',advance="no") fld(0:i)
!          write (*,'(A)',advance="no") '; '
          count = count + 1
       end if
    end do
  end subroutine chk_level
  
  integer function log2u (n)
    integer, intent(in) :: n
    log2u = bit_size(n) - leadz(n-1)
  end function log2u
  
end program main