Problem:
Problem B: Eight Princes

Once upon a time in a kingdom far, far away, there lived eight princes. Sadly they were on very
bad terms so they began to quarrel every time they met.

One day, the princes needed to seat at the same round table as a party was held. Since they
were always in bad mood, a quarrel would begin whenever:

A prince took the seat next to another prince.

A prince took the seat opposite to that of another prince (this happens only when the
table has an even number of seats), since they would give malignant looks each other.

Therefore the seat each prince would seat was needed to be carefully determined in order to
avoid their quarrels. You are required to, given the number of the seats, count the number of
ways to have all eight princes seat in peace.

Input

Each line in the input contains single integer value N , which represents the number of seats on
the round table. A single zero terminates the input.

Output

For each input N , output the number of possible ways to have all princes take their seat without
causing any quarrels. Mirrored or rotated placements must be counted as different.

You may assume that the output value does not exceed 1014.

Sample Input

8
16
17
0

Output for the Sample Input

0
0
685440

Solution in Ruby (judge verdict: Memory Limit Exceeded)

module Memoizable
	def memoize(name)
		original = "__original_#{name}__"
		([Class, Module].include?(self.class) ? self : self.class).class_eval {
			alias_method(original, name)
			private(original)
			cache = {}
			define_method(name){|*args|
				cache[args] ||= send(original, *args)
			}
		}
	end 
end

include Memoizable

def search(n, bits, k, i)
	return 1 if k == 0
	sum = 0
	(i..n-1).each{|j|
		next if bits[j-i] == 1
		copy = bits
		copy |= (1 << j-i+1) if j < n-1
		copy |= (1 << j-i+n/2) if n.even? && j < n/2
		sum += search(n, copy>>(j-i)+2, k-1, j+2)
	}
	sum
end

memoize :search

loop {
	n = gets.to_i
	break if n == 0
	bits = 0
	bits |= 2
	bits |= (1 << n-1)
	bits |= (1 << n/2) if n.even?
	p search(n, bits>>2, 7, 2) * n * 5040
}