#include<bits/stdc++.h> #define pb push_back #define popb pop_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vii vector<pii> #define vl vector<ll> #define vvi vector<vi> #define vvii vector<vii> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define vvvb vector<vvb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define ALL(x) x.begin(), x.end() #define vs vector<string> #define LL(x) ((ll)x) using namespace std; const ll BASE = 1000000000; struct bignum { vl num; bignum(): num(vl(1,0)){} bignum(ll val) { num.pb(val%BASE); val/=BASE; if(val) num.pb(val); } void trim() { while(num.size() > 1 and !num.back()) num.popb(); } bignum &operator+=(const bignum &b) { while(this->num.size() < b.num.size()) this->num.pb(0); this->num.pb(0); for(int i = 0, pom = 0; i < this->num.size(); i++) { this->num[i] += pom; if(i < b.num.size()) this->num[i]+=b.num[i]; pom = this->num[i]/BASE; this->num[i]%=BASE; } this->trim(); return *this; } bignum operator+(const bignum &b) { bignum res = *this; return (res+=b); } bignum &operator-=(const bignum &b) { for(ll i = 0, bal = 0; i < this->num.size(); i++) { this->num[i] += bal; if(i < this->num.size()) this->num[i] - b.num[i]; if(this->num[i] < 0) { this->num[i]+=BASE; bal = -1; } else bal = 0; } this->trim(); return *this; } bignum operator*=(const bignum &b) { bignum res; res.num = vl(this->num.size() + b.num.size()); for(int i = 0; i < this->num.size(); ++i) if(this->num[i]) for(ll j = 0, pom = 0; j < b.num.size() or pom; j++) { res.num[i+j] += pom; if(j < b.num.size()) res.num[i+j] += this->num[i]*b.num[j]; pom = res.num[i+j]/BASE; res.num[i+j] %= BASE; } res.trim(); return res; } bignum operator*(const bignum &b) { return (*this)*=b; } bool operator<(int val) { if(num.size() > 1) return false; return num[0] < val; } bool operator>(int val) { return !((*this) < val); } bool operator<(const bignum &b) { if(num.size() != b.num.size()) return num.size() < b.num.size(); for(int i = num.size()-1; i >= 0; i--) if(num[i] != b.num[i]) return num[i] < b.num[i]; return false; } bool operator==(const bignum &b) { return num == b.num; } bool operator!=(const bignum &b) { return num != b.num; } bignum &operator*=(int val) { this->num.pb(0); for(ll i = 0, pom = 0; i < this->num.size(); i++) { num[i] = num[i]*(ll)val + pom; pom = num[i]/BASE; num[i]%=BASE; } trim(); return *this; } bignum operator*(int val) { bignum res = *this; return res*=val; } bignum &operator/=(int val) { for(int i = num.size() - 1, pom = 0; i >= 0; --i) { ll nw = num[i] + pom*BASE; num[i] = nw/val; pom = nw%val; } trim(); return *this; } bignum operator/(int val) { bignum res = *this; return res/=val; } int operator%(int v) { ll m = 0; for(int i = num.size() - 1; i >= 0; i--) m = (num[i] + m*BASE) % (ll)v; return (int)m; } friend ostream& operator<<(ostream &stream, const bignum &b) { stream << b.num.back(); for(int i = b.num.size() - 2; i >= 0; i--) stream << setw(9) << setfill('0') << b.num[i]; return stream; } }; void out(bignum b) { b.trim(); vl v = b.num; cout << "XD" << v.size() << endl; reverse(ALL(v)); for(auto it : v) cout << it; } bignum wyn(1); const int N = 110; vi v[N]; int ile[N]; vi fact[N]; int gcd(int i, bignum b) { int res = 1; for(auto it : fact[i]) if(b%it == 0) { b/=it; res*=it; } return res; } int main() { turbo int n; cin >> n; for(int i = 1; i <= n; i++) { int q; cin >> q; ile[i] = q; v[i] = vi(q); for(auto &it : v[i]) cin >> it; } for(int i = 1; i <= n; i++) { ll p = ile[i]; if(!p) continue; for(ll j = 2; j <= ile[i]; j++) while(p%j == 0) { fact[i].pb(j); p/=j; } } while(true) { vector<bignum> wal(N); wal[1] = wyn; bool senko = true; for(int i = 1; i <= n; i++) if(wal[i] != 0 and ile[i]) { if(wal[i]%ile[i]) { int mno = ile[i]/gcd(i, wal[i]); wyn *= mno; senko = false; break; } for(auto it : v[i]) wal[it] = wal[it] + wal[i]/ile[i]; } if(senko) break; } cout << wyn; }
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #include<bits/stdc++.h> #define pb push_back #define popb pop_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vii vector<pii> #define vl vector<ll> #define vvi vector<vi> #define vvii vector<vii> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define vvvb vector<vvb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define ALL(x) x.begin(), x.end() #define vs vector<string> #define LL(x) ((ll)x) using namespace std; const ll BASE = 1000000000; struct bignum { vl num; bignum(): num(vl(1,0)){} bignum(ll val) { num.pb(val%BASE); val/=BASE; if(val) num.pb(val); } void trim() { while(num.size() > 1 and !num.back()) num.popb(); } bignum &operator+=(const bignum &b) { while(this->num.size() < b.num.size()) this->num.pb(0); this->num.pb(0); for(int i = 0, pom = 0; i < this->num.size(); i++) { this->num[i] += pom; if(i < b.num.size()) this->num[i]+=b.num[i]; pom = this->num[i]/BASE; this->num[i]%=BASE; } this->trim(); return *this; } bignum operator+(const bignum &b) { bignum res = *this; return (res+=b); } bignum &operator-=(const bignum &b) { for(ll i = 0, bal = 0; i < this->num.size(); i++) { this->num[i] += bal; if(i < this->num.size()) this->num[i] - b.num[i]; if(this->num[i] < 0) { this->num[i]+=BASE; bal = -1; } else bal = 0; } this->trim(); return *this; } bignum operator*=(const bignum &b) { bignum res; res.num = vl(this->num.size() + b.num.size()); for(int i = 0; i < this->num.size(); ++i) if(this->num[i]) for(ll j = 0, pom = 0; j < b.num.size() or pom; j++) { res.num[i+j] += pom; if(j < b.num.size()) res.num[i+j] += this->num[i]*b.num[j]; pom = res.num[i+j]/BASE; res.num[i+j] %= BASE; } res.trim(); return res; } bignum operator*(const bignum &b) { return (*this)*=b; } bool operator<(int val) { if(num.size() > 1) return false; return num[0] < val; } bool operator>(int val) { return !((*this) < val); } bool operator<(const bignum &b) { if(num.size() != b.num.size()) return num.size() < b.num.size(); for(int i = num.size()-1; i >= 0; i--) if(num[i] != b.num[i]) return num[i] < b.num[i]; return false; } bool operator==(const bignum &b) { return num == b.num; } bool operator!=(const bignum &b) { return num != b.num; } bignum &operator*=(int val) { this->num.pb(0); for(ll i = 0, pom = 0; i < this->num.size(); i++) { num[i] = num[i]*(ll)val + pom; pom = num[i]/BASE; num[i]%=BASE; } trim(); return *this; } bignum operator*(int val) { bignum res = *this; return res*=val; } bignum &operator/=(int val) { for(int i = num.size() - 1, pom = 0; i >= 0; --i) { ll nw = num[i] + pom*BASE; num[i] = nw/val; pom = nw%val; } trim(); return *this; } bignum operator/(int val) { bignum res = *this; return res/=val; } int operator%(int v) { ll m = 0; for(int i = num.size() - 1; i >= 0; i--) m = (num[i] + m*BASE) % (ll)v; return (int)m; } friend ostream& operator<<(ostream &stream, const bignum &b) { stream << b.num.back(); for(int i = b.num.size() - 2; i >= 0; i--) stream << setw(9) << setfill('0') << b.num[i]; return stream; } }; void out(bignum b) { b.trim(); vl v = b.num; cout << "XD" << v.size() << endl; reverse(ALL(v)); for(auto it : v) cout << it; } bignum wyn(1); const int N = 110; vi v[N]; int ile[N]; vi fact[N]; int gcd(int i, bignum b) { int res = 1; for(auto it : fact[i]) if(b%it == 0) { b/=it; res*=it; } return res; } int main() { turbo int n; cin >> n; for(int i = 1; i <= n; i++) { int q; cin >> q; ile[i] = q; v[i] = vi(q); for(auto &it : v[i]) cin >> it; } for(int i = 1; i <= n; i++) { ll p = ile[i]; if(!p) continue; for(ll j = 2; j <= ile[i]; j++) while(p%j == 0) { fact[i].pb(j); p/=j; } } while(true) { vector<bignum> wal(N); wal[1] = wyn; bool senko = true; for(int i = 1; i <= n; i++) if(wal[i] != 0 and ile[i]) { if(wal[i]%ile[i]) { int mno = ile[i]/gcd(i, wal[i]); wyn *= mno; senko = false; break; } for(auto it : v[i]) wal[it] = wal[it] + wal[i]/ile[i]; } if(senko) break; } cout << wyn; } |