Skip to content

Instantly share code, notes, and snippets.

@methane
Forked from sinya8282/bt.py
Created April 28, 2011 00:56
Show Gist options
  • Save methane/945584 to your computer and use it in GitHub Desktop.
Save methane/945584 to your computer and use it in GitHub Desktop.
#http://googledevjp.blogspot.com/2011/04/google-code-jam-2011.html
def solve(rest, bords, counts):
if bords == []:
return rest == 0
bord = bords[-1]
count = rest / bord
rest = rest % bord
while count >= 0:
if solve(rest, bords[0:-1], counts):
counts.append(count)
return True
count -= 1
rest += bord
return False
def solver(length, bords, counts):
if solve(length, bords, counts):
answer = " + ".join(["%d*%d" % tuple(x) for x in zip(counts, bords)])
print(answer + " = " + str(eval(answer)))
print("Boards must have at least %d." % reduce(lambda x,y:x+y, counts))
if __name__ == "__main__":
length = 10**10
bords = sorted([233,456,787])
counts = list()
solver(length, bords, counts)
# big(?) scale problem :) 500 bords(about ten thousands meters)
import random
bords = set()
while len(bords) != 500:
bords.add(int((100000-1)*random.random())+1)
bords = sorted(list(bords))
counts = list()
solver(length, bords, counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment