File tree 1 file changed +55
-0
lines changed
1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ class Judge :
3
+
4
+ def __init__ (self , A , B ):
5
+ self .A = A
6
+ self .B = B
7
+ self .total = 0
8
+
9
+ def __next__ (self ):
10
+ next (self .A )
11
+ next (self .B )
12
+ self .total += (self .A .first16 () == self .B .first16 ())
13
+
14
+ def find_matches (self , rounds_to_consider ):
15
+ for i in list (range (rounds_to_consider )):
16
+ if i % 1000000 == 0 :
17
+ print ("Considered " + str (i ) + " matches so far" , end = "\r " )
18
+ next (self )
19
+
20
+ class Generator :
21
+
22
+ def __init__ (self , value , factor ):
23
+ self .value = value
24
+ self .factor = factor
25
+
26
+ def __next__ (self ):
27
+ self .value = (self .value * self .factor ) % 2147483647
28
+ return self
29
+
30
+ def __repr__ (self ):
31
+ return str (self .value )
32
+
33
+ def first16 (self ):
34
+ return '' .join (list (reversed (bin (self .value )[2 :])))[:16 ]
35
+
36
+ if __name__ == '__main__' :
37
+ # Test input
38
+ A = Generator (65 , 16807 )
39
+ B = Generator (8921 , 48271 )
40
+ judge = Judge (A , B )
41
+ # The line below take several minutes to run when uncommented
42
+ # judge.find_matches(40000000)
43
+ print (judge .total )
44
+
45
+ # Real input
46
+ A = Generator (783 , 16807 )
47
+ B = Generator (325 , 48271 )
48
+ judge = Judge (A , B )
49
+ # The line below take several minutes to run
50
+ judge .find_matches (40000000 )
51
+ print (judge .total )
52
+
53
+
54
+
55
+
You can’t perform that action at this time.
0 commit comments