def gcd(x, y): if y > 0: return gcd(y, x % y) return x def lcm(x, y): return x // gcd(x, y) * y n = int(input()) G = [] for i in range(0, n): L = [int(x) for x in input().split()] for j in range(1, len(L)): L[j] -= 1 G.append(L) p = [0 for i in range(0, n)] q = [1 for i in range(0, n)] p[0] = 1 for i in range(0, n): tp = p[i] tq = q[i] * G[i][0] for j in range(1, len(G[i])): x = G[i][j] retp = tp * q[x] + p[x] * tq retq = tq * q[x] g = gcd(retp, retq) retp //= g retq //= g p[x] = retp q[x] = retq d = 1 for i in range(0, n): dd = gcd(p[i], q[i]) p[i] //= dd q[i] //= dd d = lcm(d, q[i]) print(d)
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 | def gcd(x, y): if y > 0: return gcd(y, x % y) return x def lcm(x, y): return x // gcd(x, y) * y n = int(input()) G = [] for i in range(0, n): L = [int(x) for x in input().split()] for j in range(1, len(L)): L[j] -= 1 G.append(L) p = [0 for i in range(0, n)] q = [1 for i in range(0, n)] p[0] = 1 for i in range(0, n): tp = p[i] tq = q[i] * G[i][0] for j in range(1, len(G[i])): x = G[i][j] retp = tp * q[x] + p[x] * tq retq = tq * q[x] g = gcd(retp, retq) retp //= g retq //= g p[x] = retp q[x] = retq d = 1 for i in range(0, n): dd = gcd(p[i], q[i]) p[i] //= dd q[i] //= dd d = lcm(d, q[i]) print(d) |