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
#include <string>
#include <iostream>
using namespace std;

// zmiana koncepcji:
//  - zamiast stringów liczby
//  - przejść cały cykl, żeby zobaczyć co się stanie w jednym cyklu i ile takich cykli jeszcze potrzeba

int belts[101][100];

string bigSum(string a, string b)
{
    string sum = "";
    int iSum, c = 0;
    int maxLength = (a.size() >= b.size()) ? a.size() : b.size();
    if (maxLength == 0)
        maxLength = 1;

    a.insert(0, maxLength - a.size(), '0');
    b.insert(0, maxLength - b.size(), '0');
    for (int i = maxLength - 1; i >= 0; i--)
    {
        iSum = int(a[i]) + int(b[i]) - 96 + c;
        sum.insert(0, 1, char(iSum % 10 + 48));
        c = iSum / 10;
    }
    if (c == 1)
        sum.insert(0, 1, char(c + 48));
    return sum;
}

void mul(int from, int n, int mno, int ile[], int steps[])
{
    for (int i = from; i <= n; i++)
    {
        if (belts[i][0] > 0)
        {
            steps[i] *= mno;
            ile[i] += steps[i] / belts[i][0];
            steps[i] = steps[i] % belts[i][0];
        }
    }
}

void sum(int actPlat, int steps[], int ile[])
{
    int i;
    for (int j = 1; j <= belts[actPlat][0]; j++)
    {
        i = belts[actPlat][j];
        if (belts[i][0] > 0)
        {
            steps[i] += ile[actPlat];
            ile[i] += steps[i] / belts[i][0];
            steps[i] = steps[i] % belts[i][0];
        }
    }
}

int nwd(int a, int b)
{
    if (b == 0)
        return a;
    nwd(b, a % b);
}

int main()
{
    int n, m;

    cin >> n;

    int steps[n + 1], ile[n + 1], all[n + 1], nextPlat, actPlat, count, mno, suitInt = 1;
    string suitcases = "1", allS;

    for (int i = 1; i <= n; i++)
    {
        cin >> m;
        belts[i][0] = m;
        for (int j = 1; j <= m; j++)
        {
            cin >> belts[i][j];
        }
        steps[i] = 0;
        ile[i] = 0;
        all[i - 1] = 0;
    }
    bool load = true;

    if (belts[1][0] != 0)
    {
        ile[1] = 1;
        sum(1, steps, ile);
        all[0]++;
        all[all[0]] = belts[1][0];
        count = belts[1][1];
        while (count < n)
        {
            if (steps[count] > 0)
            {
                if (steps[count] == 1)
                {
                    mno = belts[count][0];
                }
                else
                {
                    mno = belts[count][0] / nwd(belts[count][0], steps[count]);
                }
                mul(count, n, mno, ile, steps);
                sum(count, steps, ile);
                all[0]++;
                all[all[0]] = mno;
                // cout<< count<<" " << mno <<" " <<ile[i]<< endl;
            }
            else
                count++;
        }

        for (int i = 1; i <= all[0]; i++)
        {
            //cout << all[i] << " ";
            //suitInt *= all[i];
            allS = suitcases;
            //cout << allS << endl;
            for (int j = 1; j < all[i]; j++)
            {
                suitcases = bigSum(allS, suitcases);
            }
            //cout << "   " + suitcases + "  ";
        }
        //cout << suitInt << "   " + suitcases;
        cout << suitcases;
    }
    else
        cout << 1;
    return 0;
}