Commit 0a0bd415 authored by Taddeus Kroes's avatar Taddeus Kroes

Added bruteforce attempts for problems 243 and 364.

parent 4540b70b
*.pyc
*.swp
*~
def is_resilient(n, d):
if n == 1:
return True
for div in xrange(2, min(n, d) + 1):
if not n % div and not d % div:
return False
return True
def resilience(d):
r = 0
for n in xrange(1, d):
if is_resilient(n, d):
r += 1
return r / (d - 1.)
smallest = 15499. / 94744
d = 2
while True:
if resilience(d) < smallest:
print d
break
d += 1
from copy import copy
from sys import argv, exit, setrecursionlimit
setrecursionlimit(10000000)
def T(N, seats=None):
if not seats:
seats = [False] * N
elif all(seats):
return 1
t = 0
seated = False
# 1. If there is any seat whose adjacent seat(s) are not occupied, take
# such a seat
for i, taken in enumerate(seats):
if not taken and (not i or not seats[i - 1]) \
and (i == N - 1 or not seats[i + 1]):
new_seats = copy(seats)
new_seats[i] = True
t += T(N, new_seats)
seated = True
# 2. If there is no such seat and there is any seat for which only one
# adjacent seat is occupied take such a seat
if not seated:
for i, taken in enumerate(seats):
if not taken and ((not i and seats[1]) \
or (i == N - 1 and seats[N - 2])
or (seats[i - 1] ^ seats[i + 1])):
new_seats = copy(seats)
new_seats[i] = True
t += T(N, new_seats)
seated = True
# 3.Otherwise take one of the remaining available seats
if not seated:
for i, taken in enumerate(seats):
if not taken:
new_seats = copy(seats)
new_seats[i] = True
t += T(N, new_seats)
del seats
return t
if len(argv) < 2:
print 'Usage: python %s N' % argv[0]
exit(1)
print T(int(argv[1])) % 100000007
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment