import math
n=int(input())
fz=[]
fm=[]
for i in range(n+1):
fz.append((int)(0))
fm.append((int)(1))
fz[1]=1;
for i in range(1,n+1):
xl=input().split(" ")
for j in range(1,(int)(xl[0])+1):
y=(int)(xl[j])
x1=(int)(fz[i])
y1=(int)(fm[i])*(int)(xl[0])
x2=(int)(fz[y])
y2=(int)(fm[y])
sth=math.lcm((int)(y1),(int)(y2))
fm[y]=sth
fz[y]=x1*(sth//y1)+x2*(sth//y2)
sth=math.gcd(fz[y],fm[y])
fz[y]//=sth
fm[y]//=sth
ans=1
for i in range(1,n+1):
ans=math.lcm(ans,fm[i])
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 | import math n=int(input()) fz=[] fm=[] for i in range(n+1): fz.append((int)(0)) fm.append((int)(1)) fz[1]=1; for i in range(1,n+1): xl=input().split(" ") for j in range(1,(int)(xl[0])+1): y=(int)(xl[j]) x1=(int)(fz[i]) y1=(int)(fm[i])*(int)(xl[0]) x2=(int)(fz[y]) y2=(int)(fm[y]) sth=math.lcm((int)(y1),(int)(y2)) fm[y]=sth fz[y]=x1*(sth//y1)+x2*(sth//y2) sth=math.gcd(fz[y],fm[y]) fz[y]//=sth fm[y]//=sth ans=1 for i in range(1,n+1): ans=math.lcm(ans,fm[i]) print(ans) |
English