Created
January 29, 2013 11:08
-
-
Save arjandepooter/4663497 to your computer and use it in GitHub Desktop.
Facebook Hackercup 2013 - Find the min
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from blist import sortedlist | |
def rng(a, b, c, r, k): | |
m = [a] | |
for i in xrange(k - 1): | |
m.append((b*m[-1]+c) % r) | |
return m | |
def get_sol(m, n): | |
k = len(m) | |
to_add = sortedlist(xrange(k+1)) | |
i = (n-k-1) % (k+1) | |
for ix, p in enumerate(reversed(m)): | |
if p <= (k+1) and p in to_add: | |
to_add.remove(p) | |
else: | |
p = to_add.pop() | |
if k - ix == i: | |
return p | |
return to_add.pop() | |
def main(): | |
import sys | |
n = int(sys.stdin.readline()) | |
for i in xrange(1, n+1): | |
n, k = (int(x) for x in sys.stdin.readline().strip().split(' ')) | |
a, b, c, r = (int(x) for x in sys.stdin.readline().strip().split(' ')) | |
m = rng(a, b, c, r, k) | |
print "Case #%d: %d" % (i, get_sol(m, n)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment