#!/usr/bin/python3 from math import gcd #def gcd(a, b): # assert(0 <= a) # assert(0 <= b) # assert(0 < a or 0 < b) # if a == 0: # return b # return gcd(b % a, a) def nww(a, b): return a * b // gcd(a, b) def skroc(u): (a, b) = u assert b != 0 g = gcd(a, b) return (a // g, b // g) def dodaj(u1, u2): (u1a, u1b) = u1 (u2a, u2b) = u2 return skroc((u1a * u2b + u2a * u1b, u1b * u2b)) def podziel(u, co): (a, b) = u return skroc((a, b * co)) n = int(input()) ile = [(0, 1) for i in range(n + 1)] stopy = [0 for i in range(n + 1)] ile[1] = (1, 1) for i in range(1, n + 1): ciag = [int(s) for s in input().split()] assert len(ciag) >= 1 l = ciag[0] stopy[i] = l ciag = ciag[1:] assert len(ciag) == l for j in ciag: assert i < j <= n ile[j] = dodaj(ile[j], podziel(ile[i], l)) out = 1 for i in range(1, n + 1): (a, b) = ile[i] #print(i, a, b, ile[i], stopy[i]) if a > 0: if stopy[i] == 0: out = nww(out, b) else: out = nww(out, stopy[i] * b // gcd(stopy[i] * b, a)) print(out)
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 | #!/usr/bin/python3 from math import gcd #def gcd(a, b): # assert(0 <= a) # assert(0 <= b) # assert(0 < a or 0 < b) # if a == 0: # return b # return gcd(b % a, a) def nww(a, b): return a * b // gcd(a, b) def skroc(u): (a, b) = u assert b != 0 g = gcd(a, b) return (a // g, b // g) def dodaj(u1, u2): (u1a, u1b) = u1 (u2a, u2b) = u2 return skroc((u1a * u2b + u2a * u1b, u1b * u2b)) def podziel(u, co): (a, b) = u return skroc((a, b * co)) n = int(input()) ile = [(0, 1) for i in range(n + 1)] stopy = [0 for i in range(n + 1)] ile[1] = (1, 1) for i in range(1, n + 1): ciag = [int(s) for s in input().split()] assert len(ciag) >= 1 l = ciag[0] stopy[i] = l ciag = ciag[1:] assert len(ciag) == l for j in ciag: assert i < j <= n ile[j] = dodaj(ile[j], podziel(ile[i], l)) out = 1 for i in range(1, n + 1): (a, b) = ile[i] #print(i, a, b, ile[i], stopy[i]) if a > 0: if stopy[i] == 0: out = nww(out, b) else: out = nww(out, stopy[i] * b // gcd(stopy[i] * b, a)) print(out) |