#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MAXN = 100 + 5; inline int gcd(int a,int b){ return b? gcd(b,a%b): a;} const int base = 1e7; struct Bigint { vector<int> a; Bigint(ll x = 0) { while(x) a.emplace_back(x % base), x /= base; } Bigint operator + (const Bigint &oth) const { Bigint res; res.a.resize(max(a.size(), oth.a.size())); int c = 0; for(int i=0; i<(int)res.a.size(); ++i) { if(i<(int)a.size()) c += a[i]; if(i<(int)oth.a.size()) c += oth.a[i]; res.a[i] = c % base; c /= base; } if(c) res.a.emplace_back(c); return res; } Bigint operator * (int oth) const { if(oth == 0 || !a.size()) return Bigint(0); Bigint res; res.a.resize(a.size()); ll c = 0; for(int i=0; i<(int)res.a.size(); ++i) { c += (ll)a[i] * oth; res.a[i] = c % base; c /= base; } while(c) res.a.emplace_back(c % base), c /= base; return res; } pair<Bigint,int> div(int oth) const { Bigint res; res.a.resize(a.size()); ll c = 0; for(int i=(int)a.size()-1; i>=0; --i) { c = c * base + a[i]; res.a[i] = c / oth; c %= oth; } while(res.a.size() && !res.a.back()) res.a.pop_back(); return {res, c}; } Bigint operator / (int oth) const { return div(oth).first;} int operator % (int oth) const { return div(oth).second;} Bigint& operator += (const Bigint &oth){ return *this = *this + oth;} Bigint& operator *= (int oth){ return *this = *this * oth;} }; ostream& operator << (ostream &os,const Bigint &a) { if(!a.a.size()) return os << "0"; auto save = os.fill(); os << a.a.back() << setfill('0'); for(int i=(int)a.a.size()-2; i>=0; --i) os << setw(7) << a.a[i]; os << setfill(save); return os; } Bigint f[MAXN]; vector<int> g[MAXN]; int main(void) { ios :: sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i=1; i<=n; ++i) { int len; cin >> len; g[i].resize(len); for(int &t: g[i]) cin >> t; } if(!g[1].size()) { printf("1\n"); return 0; } for(int u: g[1]) f[u] += 1; Bigint res = (int)g[1].size(); for(int i=2; i<=n; ++i) { if(!g[i].size() || !f[i].a.size()) continue; int len = (int)g[i].size(); int t = len / gcd(f[i] % len, len); res *= t; for(int j=i; j<=n; ++j) f[j] *= t; Bigint cur = f[i] / len; for(int v: g[i]) f[v] += cur; } cout << res << endl; return 0; }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MAXN = 100 + 5; inline int gcd(int a,int b){ return b? gcd(b,a%b): a;} const int base = 1e7; struct Bigint { vector<int> a; Bigint(ll x = 0) { while(x) a.emplace_back(x % base), x /= base; } Bigint operator + (const Bigint &oth) const { Bigint res; res.a.resize(max(a.size(), oth.a.size())); int c = 0; for(int i=0; i<(int)res.a.size(); ++i) { if(i<(int)a.size()) c += a[i]; if(i<(int)oth.a.size()) c += oth.a[i]; res.a[i] = c % base; c /= base; } if(c) res.a.emplace_back(c); return res; } Bigint operator * (int oth) const { if(oth == 0 || !a.size()) return Bigint(0); Bigint res; res.a.resize(a.size()); ll c = 0; for(int i=0; i<(int)res.a.size(); ++i) { c += (ll)a[i] * oth; res.a[i] = c % base; c /= base; } while(c) res.a.emplace_back(c % base), c /= base; return res; } pair<Bigint,int> div(int oth) const { Bigint res; res.a.resize(a.size()); ll c = 0; for(int i=(int)a.size()-1; i>=0; --i) { c = c * base + a[i]; res.a[i] = c / oth; c %= oth; } while(res.a.size() && !res.a.back()) res.a.pop_back(); return {res, c}; } Bigint operator / (int oth) const { return div(oth).first;} int operator % (int oth) const { return div(oth).second;} Bigint& operator += (const Bigint &oth){ return *this = *this + oth;} Bigint& operator *= (int oth){ return *this = *this * oth;} }; ostream& operator << (ostream &os,const Bigint &a) { if(!a.a.size()) return os << "0"; auto save = os.fill(); os << a.a.back() << setfill('0'); for(int i=(int)a.a.size()-2; i>=0; --i) os << setw(7) << a.a[i]; os << setfill(save); return os; } Bigint f[MAXN]; vector<int> g[MAXN]; int main(void) { ios :: sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i=1; i<=n; ++i) { int len; cin >> len; g[i].resize(len); for(int &t: g[i]) cin >> t; } if(!g[1].size()) { printf("1\n"); return 0; } for(int u: g[1]) f[u] += 1; Bigint res = (int)g[1].size(); for(int i=2; i<=n; ++i) { if(!g[i].size() || !f[i].a.size()) continue; int len = (int)g[i].size(); int t = len / gcd(f[i] % len, len); res *= t; for(int j=i; j<=n; ++j) f[j] *= t; Bigint cur = f[i] / len; for(int v: g[i]) f[v] += cur; } cout << res << endl; return 0; } |