fork download
  1. import sys
  2. input_stream = sys.stdin
  3. cases = int(input_stream.readline())
  4. """
  5. Disclosure: I was struggling with the indexing idea..
  6. and I got influenced by the solution in a stackoverflow post to improve my implementation.
  7. It still reads like that solution instead of mine..
  8. I was out of time anyway after submitting my naiver solution, so didn't get to take advantage of sneaking on the interwebs
  9. """
  10.  
  11. def next_pointer(lst, start):
  12. idx = start
  13. # 0 indexed
  14. out = start - 1
  15. while idx < len(lst) and lst[idx]:
  16. out = idx
  17. idx += 1
  18. return out
  19.  
  20.  
  21.  
  22. def line_list(input_stream):
  23. return map(int,input_stream.readline().strip().split())
  24.  
  25.  
  26. for t in xrange(cases):
  27. n,k = line_list(input_stream)
  28. a,b,c,r = line_list(input_stream)
  29. # Storing 10^9 is expensive, do it in chunks
  30. # 4 worked for the input from facebook
  31. m = [0] * (4*k)
  32. # This is the lookup set guaranteed to not be > len(k)
  33. # Lookup is used more like a dictionary..
  34. # Each index stores the number of occurences of the value
  35. lookup = [0] * (k+1)
  36. m[0] = a
  37. if m[0] <= k:
  38. lookup[m[0]] = 1
  39. for i in xrange(1, k):
  40. # Linear Congruential Generator
  41. m[i] = (b * m[i-1] + c) % r
  42. if m[i] < k+1:
  43. lookup[m[i]] += 1
  44.  
  45. pointer = next_pointer(lookup, 0)
  46. m[k] = pointer + 1
  47. pointer = next_pointer(lookup, pointer+2)
  48.  
  49. for i in xrange(k+1, n):
  50. # Is the number > the index.. and have we seen it before?
  51. if m[i-k-1] > pointer or lookup[m[i-k-1]] > 1:
  52. m[i] = pointer + 1
  53. # If the item isn't in the range we're tracking,
  54. # decrement the counter at that index..
  55. if m[i-k-1] <= k:
  56. lookup[m[i-k-1]] -= 1
  57. lookup[m[i]] += 1
  58. pointer = next_pointer(lookup, pointer+2)
  59. else:
  60. m[i] = m[i-k-1]
  61. if pointer == k:
  62. break
  63.  
  64. if pointer != k:
  65. out = m[n-1]
  66. else:
  67. out = m[i-k + (n-i+k+k) % (k+1)]
  68. print 'Case #%d: %d' % (t+1, out)
Success #stdin #stdout 2.27s 10896KB
stdin
20
1000000000 100000
1 1 0 2
1000000000 100000
999999999 1 999999999 1000000000
714311129 39521
9 5 9 999998192
220 88
1 8 3 58265
177 73
7 7 5 56401
131 74
1 9 10 78736
45068754 29153
2 9 5 999904402
73 26
5 8 4 54214
640834505 28785
3 9 1 999946125
186 75
68 16 539 186
1000000000 1
12 7 74 12
137 49
48 17 461 137
46 18
7 11 9 46
66 39
35 2 589 66
1000000000 100000
100000 1 0 1000000000
249718282 93729
1 5 6 999917908
497151700 96511
9 7 6 999919625
218492221 46085
3 7 1 999969453
1000000000 100000
99999 1 99999 100000
82 81
58 56 739 82
stdout
Case #1: 90002
Case #2: 90001
Case #3: 30023
Case #4: 42
Case #5: 29
Case #6: 58
Case #7: 25824
Case #8: 19
Case #9: 573
Case #10: 38
Case #11: 0
Case #12: 41
Case #13: 12
Case #14: 30
Case #15: 90001
Case #16: 21563
Case #17: 18389
Case #18: 44581
Case #19: 9999
Case #20: 0