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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Arti1990, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,K,FUN)             ({auto SSS=P, PPP = P-1, KKK=(K)+1; while(PPP+1!=KKK) {SSS = (PPP+(KKK-PPP)/2); if(FUN(SSS)) KKK = SSS; else PPP = SSS;} PPP;})
#define testy()                     int _tests; cin>>_tests; FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define ff                          first
#define ss                          second
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007LL
#define P 25

using namespace std;

int p[P] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
int n, k, a;
list <int> l[1007];
vector <int> mian[1007], licz[1007], res;

long long nww(long long a, long long b)
{
    return a/__gcd(a, b)*b;
}

string wypisz(vector <int> &l)
{
    string res;
    FORD(i, l.size(), 1)
    {
        char c = l[i-1] + '0';
        res += c;
    }
    return res;
}

long long modulo_li(vector <int> &l, long long x)
{
    //cout << "liczy " << wypisz(l) << " modulo " << x<< '\n';
    long long res = 0;
    FORD(i, l.size(), 1)
        res = (10*res + l[i-1]) % x;
    //cout << wypisz(l) << " modulo " << x << " dalo " << res << '\n';
    return res;
}

void podziel_li(vector <int> &l, long long x)
{
    //cout << wypisz(l) << " podzelone przez " << x << " dalo ";
    FORD(i, l.size(), 1)
    {
        int nr = i-1;
        if(nr > 0)
            l[nr-1] += l[nr]%x * 10;
        l[nr] /= x;
    }
    //cout << wypisz(l) << '\n';
}

void skroc_lm(vector <int> &l, vector <int> &m)
{
    //cout << "skraca " << wypisz(l) << '\n';
    forr(i, P)
        while(m[i] > 0 && modulo_li(l, p[i]) == 0)
        {
            podziel_li(l, p[i]);
            m[i]--;
        }
}

void pomnoz_mi(vector <int> &mian, long long x)
{
    //cout << "mnozy mianowniki razy " << x << '\n';
    forr(i, P)
        while(x%p[i] == 0)
        {
            mian[i]++;
            x /= p[i];
        }
}

vector <int> licz_nww_mm(vector <int> &a, vector <int> &b)
{
    vector <int> res;
    forr(i, P)
        res.PB(max(a[i], b[i]));
    return res;
}

void dodaj_ll(vector <int> &a, vector <int> &b)
{
    //cout << wypisz(a) << " + " << wypisz(b);
    int dl = max(a.size(), b.size());
    a.resize(dl);
    forr(i, a.size())
    {
        if(i < b.size())
            a[i] += b[i];
        //cout << a[i] << '\n';
        if(a[i] >= 10 && i+1 == a.size())
            a.PB(0);
        a[i+1] += a[i]/10;
        a[i] %= 10;
    }
    //cout << " = " << wypisz(a) << '\n';
}

vector <int> podziel_mm(vector <int>  &a, vector <int>  &b)
{
    vector <int> res;
    forr(i, P)
        res.PB(a[i] - b[i]);
    return res;
}

void pomnoz_li(vector <int> &l, long long x)
{
    //cout << wypisz(l) << " pomnozone razy " << x << " daje ";
    forr(i, l.size())
        l[i] *= x;
    forr(i, l.size())
    {
        if(l[i] >= 10 && i+1 == l.size())
            l.PB(0);
        l[i+1] += l[i]/10;
        l[i] %= 10;
    }
    //cout << wypisz(l) << '\n';
}

vector <int> pomnoz_lm(vector <int> &a, vector <int> &b)
{
    vector <int> res = a;
    forr(i, P)
        forr(j, b[i])
            pomnoz_li(res, p[i]);
    return res;
}

int solve()
{
    cin >> n;
    FOR(i, 1, n)
    {
        cin >> k;
        forr(j, k)
        {
            cin >> a;
            l[i].PB(a);
        }
        mian[i].resize(P, 0);
    }
    licz[1].PB(1);
    res.resize(P);
    FOR(i, 1, n)
    {
        if(l[i].size() != 0)
        {
            //cout << "i: " << i << '\n';
            pomnoz_mi(mian[i], l[i].size());
            skroc_lm(licz[i], mian[i]);
            res = licz_nww_mm(res, mian[i]);

            //cout << i << " ma licznik " << wypisz(licz[i]) << '\n';
            //cout << "mianownik: ";
            //forr(j, P)
            //    cout << mian[i][j] << " ";
            //cout << '\n';

            if(licz[i].size() > 0)
            {
                for(auto el : l[i])
                {
                    vector <int> x = licz_nww_mm(mian[i], mian[el]);
                    vector <int> x1 = podziel_mm(x, mian[el]);
                    licz[el] = pomnoz_lm(licz[el], x1);
                    mian[el] = x;
                    x1 = podziel_mm(x, mian[i]);
                    vector <int> l = pomnoz_lm(licz[i], x1);
                    dodaj_ll(licz[el], l);
                }
            }
        }
    }
    vector <int> r;
    r.PB(1);
    r = pomnoz_lm(r, res);
    cout << wypisz(r) << '\n';

    return 0;
}

int main()
{
    /*FOR(i, 2, 100)
    {
        bool ok = 1;
        FOR(j, 2, i-1)
            if(i%j == 0)
                ok = 0;
        if(ok)
            cout << i << ", ";
    }*/
    //std::ios_base::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);

    solve();

    return 0;
}