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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>
 
using namespace std;
 
//#define endl '\n'
#define L long long
#define MP make_pair
#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, a, b) for(int i = b - 1; i >= a; --i)
#define EB emplace_back
#define ST first
#define ND second
#define S size
#define RS resize
 
template<class T> using P = pair<T, T>;
template<class T> using V = vector<T>;

int to_int(char c) {
    return c - '0';
}

char to_char(int d) {
    return (char)d + '0';
}

string add(string& a, string& b) {
    int r1 = (int)a.size();
    int r2 = (int)b.size();
    int p = 0;
    string s;
    REP(i, max(r1, r2)) {
        int k = p;
        if (i < r1) k += to_int(a[i]);
        if (i < r2) k += to_int(b[i]);
        s += to_string(k % 10);
        p = k / 10;
    }
    if (p > 0) s += to_string(p);
    return s;
}

string mult_digit(string& a, int d) {
    int r = (int)a.size();
    int p = 0;
    string s;
    REP(i, r) {
        int k = to_int(a[i]) * d + p;
        s += to_char(k % 10);
        p = k / 10;
    }
    if (p > 0) s += to_string(p);
    return s;
}

string mult(string& a, int b) {
    if (b == 0) return "0";
    if (b < 10) return mult_digit(a, b);
    string a1 = mult_digit(a, b % 10);
    string a2 = '0' + mult_digit(a, b / 10);
    return add(a1, a2);
}

pair<string, int> div_and_mod(string a, int b) {
    reverse(a.begin(), a.end());
    int r = (int)a.size();
    int p = 0;
    string s;
    REP(i, r) {
        p = 10 * p + to_int(a[i]);
        if (p >= b) {
            int k = p / b;
            s += to_char(k);
            p -= k * b;
        }
        else if (s != "") {
            s += '0';
        }
    }
    reverse(s.begin(), s.end());
    return MP(s, p);
}

string div(string& a, int b) {
    auto res = div_and_mod(a, b);
    return res.ST;
}

int mod(string& a, int b) {
    auto res = div_and_mod(a, b);
    return res.ND;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    V<int> r(n);
    V<V<int>> l(n);
    V<V<int>> lr(n);
    REP(i, n) {
        cin >> r[i];
        REP(j, r[i]) {
            int x;
            cin >> x;
            l[i].EB(x - 1);
            lr[x - 1].EB(i);
        }
    }

    if (r[0] == 0) {
        cout << "1\n";
        return 0;
    }

    V<string> p(n, "0");
    string w;
    p[0] = w = to_string(r[0]);
    reverse(p[0].begin(), p[0].end());
    reverse(w.begin(), w.end());
    FOR(i, 1, n) {
        string c = "0";
        for (int x : lr[i]) {
            string d = div(p[x], r[x]);
            c = add(c, d);
        }
        p[i] = c;
        if (r[i] == 0) continue;
        int m = mod(c, r[i]);
        if (m != 0) {
            int multiplicator = r[i] / gcd(m, r[i]);
            w = mult(w, multiplicator);
            REP(j, n - 1) {
                p[j] = mult(p[j], multiplicator);
            }
        }
    }
    while (w.back() == '0') {
        w.pop_back();
    }
    reverse(w.begin(), w.end());
    cout << w << '\n';
}

// g++ -std=c++17 -Wall -Wextra -Wshadow -Wconversion -D_GLIBCXX_DEBUG -O3 wal.cpp -o wal