-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcandy_patterns.py
71 lines (52 loc) · 1.62 KB
/
candy_patterns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from collections import defaultdict
from fractions import Fraction
from functools import reduce
from typing import Tuple, Dict
def gcd(a, b):
"""
Compute the greatest common divisor of a and b
URL: https://gist.github.com/endolith/114336/eff2dc13535f139d0d6a2db68597fad2826b53c3
"""
while b > 0:
a, b = b, a % b
return a
def lcm(a, b):
"""
Compute the lowest common multiple of a and b
URL: https://gist.github.com/endolith/114336/eff2dc13535f139d0d6a2db68597fad2826b53c3
"""
return a * b // gcd(a, b)
def is_valid(counter: Dict[int, int]) -> bool:
for k, v in counter.items():
if v % k != 0:
return False
return True
def fix_values(counter: Dict[int, int]) -> Dict[int, int]:
inc = reduce(lcm, counter)
i = 0
counter_copy = counter
while not is_valid(counter_copy):
counter_copy = counter.copy()
i += inc
for k in counter_copy.keys():
counter_copy[k] *= i
return counter_copy
def solve_case() -> Tuple[int, int]:
n = int(input())
mapping = map(int, input().split())
counter = defaultdict(int)
for v in mapping:
counter[v] += 1
numerator, denominator = 0, 0
for k, v in fix_values(counter).items():
numerator += v
denominator += v // k
fraction = Fraction(numerator, denominator)
return fraction.numerator, fraction.denominator
def main():
c = int(input())
mapping = map(lambda i: (solve_case(), i + 1), range(c))
for result, i in mapping:
print(f'Case #{i}: {result[0]}/{result[1]}')
if __name__ == '__main__':
main()