from math import gcd def reduced(f): a, b = f g = gcd(a, b) return (a // g, b // g) def add(x, y): xa, xb = x ya, yb = y return reduced((xa * yb + ya * xb, xb * yb)) def mult(x, y): xa, xb = x ya, yb = y return reduced((xa * ya, xb * yb)) def lcm(a, b): return a // gcd(a, b) * b n = int(input()) F = [(0, 1) for _ in range(n + 1)] F[1] = (1, 1) ans = 1 for i in range(1, n + 1): lst = input().split(' ') l = int(lst[0]) if l != 0: f = (1, l) ff = mult(F[i], f) for j in range(1, l + 1): x = int(lst[j]) F[x] = add(F[x], ff) _, ffb = ff ans = lcm(ans, ffb) print(ans)
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 | from math import gcd def reduced(f): a, b = f g = gcd(a, b) return (a // g, b // g) def add(x, y): xa, xb = x ya, yb = y return reduced((xa * yb + ya * xb, xb * yb)) def mult(x, y): xa, xb = x ya, yb = y return reduced((xa * ya, xb * yb)) def lcm(a, b): return a // gcd(a, b) * b n = int(input()) F = [(0, 1) for _ in range(n + 1)] F[1] = (1, 1) ans = 1 for i in range(1, n + 1): lst = input().split(' ') l = int(lst[0]) if l != 0: f = (1, l) ff = mult(F[i], f) for j in range(1, l + 1): x = int(lst[j]) F[x] = add(F[x], ff) _, ffb = ff ans = lcm(ans, ffb) print(ans) |