Skip to content

Instantly share code, notes, and snippets.

@guilhermebr
Last active December 11, 2015 21:19
Show Gist options
  • Save guilhermebr/4661787 to your computer and use it in GitHub Desktop.
Save guilhermebr/4661787 to your computer and use it in GitHub Desktop.
20
66 39
35 2 589 66
1000000000 100000
99999 1 99999 100000
186 75
68 16 539 186
1000000000 100000
1 1 1 1000000000
977365070 59489
8 5 9 999966210
1000000000 100000
100000 1 0 1000000000
1000000000 100000
999999999 1 999999999 1000000000
198 81
8 5 7 83495
840698758 13331
8 7 10 999955808
232959116 56689
4 9 1 999903057
45068754 29153
2 9 5 999904402
1000000000 1
12 7 74 12
640834505 28785
3 9 1 999946125
177 73
7 7 5 56401
97 39
34 37 656 97
59 26
14 19 681 59
73 26
5 8 4 54214
254 99
1 8 9 74990
22 21
1 4 869 22
137 49
48 17 461 137

Find the Min

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is not contained in the previous k values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given non-negative integers a, b, c and positive integer r, the known values of m can be calculated as follows:

m[0] = a

m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

The first line contains an integer T (T <= 20), the number of test cases.

This is followed by T test cases, consisting of 2 lines each.

The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 10^5, k < n <= 10^9).

The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 10^9, 1 <= r <= 10^9).

Output

For each test case, output a single line containing the case number and the nth element of m.

Example input

5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46

Example output

Case #1: 8
Case #2: 38
Case #3: 41
Case #4: 40
Case #5: 12
# Find the Min for Facebook Hackercup 2013
# fb.com/gbrezende
# Guilherme Rezende <guilhermebr@gmail.com>
def calcMatriz(n, k, a, b, c, r):
m = []
m.append(a)
for i in range(1, k):
m.append((b * m[i-1] +c) % r)
return m
def getNelement(m, n, k):
i = k
minimum = 0
while i < n:
if minimum in m:
minimum += 1
else:
m.append(minimum)
m.remove(m[0])
i += 1
minimum = 0
return m[k-1]
#__main__
try:
inFile = open('find_the_mintxt.txt', 'r')
outFile = open('output3.txt', 'w+')
numTests = inFile.readline()
numTests = int(numTests.strip('\n'))
for test in range(1, numTests+1):
n, k = (int(x) for x in inFile.readline().split())
a, b, c, r = (int(x) for x in inFile.readline().split())
m = calcMatriz(n, k, a, b, c, r)
nth = getNelement(m, n, k)
print 'Case #%d: %d' % (test, nth)
outFile.write('Case #%d: %d\n' % (test, nth))
finally:
inFile.close()
outFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment