def gcd(x: int, y: int): if y == 0: return x return gcd(y, x%y) class F: def __init__(self, x, y): self.n = x self.d = y self.opt() def opt(self): x = gcd(self.n, self.d) self.n //= x self.d //= x def divide(f: F, x: int): return F(f.n, f.d*x) def add(a: F, b: F): return F(a.n*b.d + b.n*a.d, a.d*b.d) if __name__ == '__main__': n = int(input()) t = n*[[]] for i in range(n): s = input().split() t[i] = [ int(x)-1 for x in s[1:] ] result = n*[F(0,1)] result[0] = F(1,1) for i in range(n): if len(t[i]) > 0: x = divide(result[i], len(t[i])) for j in t[i]: result[j] = add(result[j], x) R = 1 for f in result: R = (R * f.d) // gcd(R, f.d) print(R)
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 | def gcd(x: int, y: int): if y == 0: return x return gcd(y, x%y) class F: def __init__(self, x, y): self.n = x self.d = y self.opt() def opt(self): x = gcd(self.n, self.d) self.n //= x self.d //= x def divide(f: F, x: int): return F(f.n, f.d*x) def add(a: F, b: F): return F(a.n*b.d + b.n*a.d, a.d*b.d) if __name__ == '__main__': n = int(input()) t = n*[[]] for i in range(n): s = input().split() t[i] = [ int(x)-1 for x in s[1:] ] result = n*[F(0,1)] result[0] = F(1,1) for i in range(n): if len(t[i]) > 0: x = divide(result[i], len(t[i])) for j in t[i]: result[j] = add(result[j], x) R = 1 for f in result: R = (R * f.d) // gcd(R, f.d) print(R) |