#include <iostream> #include <vector> #include <ccomplex> using namespace std; long double pi = acos(-1); int carm = -1; class BigInteger { // class based on a piece of code from pa-2021 private : bool positiv = true; std::vector <int> digits; public : BigInteger() {}; BigInteger(const BigInteger &x) { positiv = x.positiv; digits = x.digits; } ~BigInteger() { digits.clear(); digits.push_back(0); positiv = true; } BigInteger(const std::string &x) { positiv = true; if (x.size() == 0) digits.push_back(0); else { for (int i = (int)x.size() - 1;i >= 0;i--) if (x[i] >= '0' && x[i] <= '9') digits.push_back((int)((x[i])-'0')); if (x[0] == '-') positiv = false; } } BigInteger (const int &x) { positiv = (x >= 0); int y = x; if (x < 0) y = -x; while (y > 0) { digits.push_back(y % 10); y/=10; } if (!digits.size()) {positiv = true;digits.push_back(0);} } bool operator== (const BigInteger &x) const{ if (positiv != x.positiv) return false; if (x.digits != digits) return false; return true; } bool operator< (const BigInteger &x) const{ if ((*this) == x) return false; if (positiv != x.positiv) return !positiv; if (!positiv) { BigInteger z1 = (*this); BigInteger z2 = x; z1 = z1.abs(); z2 = z2.abs(); return (!(z1 < z2)); } if (x.digits.size() != digits.size()) return (digits.size() < x.digits.size()); for (int i = (int)digits.size() - 1;i >= 0;i--) if (digits[i] != x.digits[i]) return (digits[i] < x.digits[i]); return false; } bool operator> (const BigInteger &x) const{ if (x == (*this)) return false; return (!((*this) < x)); } bool operator!= (const BigInteger &x) const{ return !(x == (*this)); } bool operator<= (const BigInteger &x) const { if (x == (*this)) return true; return ((*this) < x); } bool operator>= (const BigInteger &x) const { if (x == (*this)) return true; return ((*this) > x); } std::string toString() const{ std::string ans = ""; if (!positiv) ans+='-'; for (int i = (int)digits.size() - 1;i >= 0;i--) ans+=((char)((digits[i])+'0')); if (ans.size() == 0) ans = "0"; return ans; } explicit operator bool() { //std::cout<<digits.size()<<" "<<digits[0]<<" "<<"!"<<'\n'; if (digits[0] == 0 && digits.size() == 1) return false; else return true; } BigInteger &operator+=(const BigInteger& x) { if (x.positiv == positiv) { int carry = 0; for (int i = 0;i < (int)digits.size() || i < (int)x.digits.size() || carry;i++) { if (i == (int)digits.size()) digits.push_back(0); if (i < (int)x.digits.size()) { digits[i]+=(carry + x.digits[i]); } else digits[i]+=carry; carry = (digits[i] / 10); digits[i]%=10; } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (!digits.size()) digits.push_back(0); return (*this); } else { if (abs() > x.abs()) { raznost_(x); return (*this); } else { BigInteger a1 = (*this); BigInteger a2 = x; a2.raznost_(a1); return (*this = a2); } } } void raznost_(const BigInteger &x) { int carry = 0; for (int i = 0;i < (int)x.digits.size() || carry;i++) { if (i < (int)x.digits.size()) digits[i]-=(carry + x.digits[i]); else digits[i]-=carry; carry = (digits[i] < 0); if (carry != 0) digits[i]+=10; } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (!digits.size()) {positiv = true;digits.push_back(0);} } BigInteger abs() const{ BigInteger wow = (*this); wow.positiv = true; return wow; } BigInteger &operator= (const BigInteger &x) { positiv = x.positiv; digits = x.digits; return (*this); } BigInteger &operator-= (const BigInteger &x) { if (positiv == x.positiv) { if (abs() >= x.abs()) {raznost_(x); return (*this);} else { BigInteger a1 = (*this); BigInteger a2 = x; a2.raznost_(a1); a2.positiv = (!a2.positiv); return (*this = a2); } } else { int carry = 0; for (int i = 0;i < (int)digits.size() || i < (int)x.digits.size() || carry;i++) { if (i == (int)digits.size()) digits.push_back(0); if (i < (int)x.digits.size()) { digits[i]+=(carry + x.digits[i]); } else digits[i]+=carry; carry = (digits[i] / 10); digits[i]%=10; } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (!digits.size()) digits.push_back(0); return (*this); } } typedef std::complex <long double> ftype; //for FFT inline void FFT (std::vector <ftype> &p, bool inv) { int n = (int)p.size(); if (n == 1) return; std::vector <ftype> a(n / 2); std::vector <ftype> b(n / 2); for (int i = 0;i < n / 2;i++) { a[i] = p[i * 2]; b[i] = p[i * 2 + 1]; } FFT(a, inv); FFT(b, inv); long double h = 2 * pi / n; if (inv) h*=-1; ftype w(1),wn(cos(h),sin(h)); for (int i = 0;i < n / 2;i++) { p[i] = a[i] + w * b[i]; p[i + n / 2] = a[i] - w * b[i]; if (inv) { p[i]/=2; p[i + n / 2]/=2; } w*=wn; } } inline std::vector <int> multiply (std::vector <int> a,std::vector <int> b) { std::vector <ftype> x (a.begin(),a.end()); std::vector <ftype> y (b.begin(),b.end()); int m = 1; while (m < std::max((int)a.size(),(int)b.size())) m*=2; m*=2; x.resize(m); y.resize(m); FFT(x, 0); FFT(y, 0); for (int i = 0;i < m;i++) x[i]*=y[i]; FFT(x, 1); std::vector <int> ans(m); for (int i = 0;i < m;i++) ans[i] = (int)(x[i].real() + 0.5); return ans; } BigInteger &operator*= (const int& x) { if ((*this) == 0) return (*this); if (x == 0) { (*this) = x; return (*this); } int carry = 0; for (int i = 0;i < (int)digits.size() || carry;i++) { if (i == (int)digits.size()) digits.push_back(0); int cur = (carry + (digits[i] * x)); digits[i] = (cur % 10); carry = (cur / 10); } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (digits.size() == 0) digits.push_back(0); return (*this); } BigInteger &operator*= (const BigInteger& x) { if ((*this) == 0) return (*this); if (x == 0) { (*this) = x; return (*this); } if (digits.size() + x.digits.size() <= 1000) { std::vector <int> zj(digits.size() + x.digits.size()); int carry = 0; for (int i = 0;i < (int)digits.size();i++) { for (int j = 0;j < (int)x.digits.size() || carry;j++) { int cl = zj[i + j] + digits[i] * 1 * (j < (int)x.digits.size() ? x.digits[j] : 0) + carry; zj[i + j] = (int)(cl % 10); carry = (int)(cl / 10); } } while (zj.size() > 0 && zj.back() == 0) zj.pop_back(); positiv = (!(positiv ^ x.positiv)); if (zj.size() == 0) {positiv = true;zj.push_back(0);} digits = zj; return (*this); } std::vector <int> zj = multiply(digits,x.digits); int ptr = (int)zj.size() - 1; while (ptr >= 0 && zj[ptr] == 0) --ptr; int carry = 0; positiv = (!(positiv ^ x.positiv)); std::vector <int> ans; ans.resize(ptr + 1); for (int i = 0;i <= ptr || carry;i++) { if (i > ptr) ans.push_back(0); ans[i] = (zj[i] + carry); carry = (zj[i] + carry) / 10; ans[i]%=10; } digits = ans; return (*this); } BigInteger &operator/= (int v) { if ((*this) == 0) return (*this); BigInteger ans; ans.digits.resize(digits.size()); int cur = 0; int carry = 0; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur = (digits[i] + carry * 10); ans.digits[i] = cur / v; carry = (cur % v); } while (ans.digits.size() > 0 && ans.digits.back() == 0) ans.digits.pop_back(); if (ans.digits.size() == 0) ans.digits.push_back(0); (*this) = ans; carm = carry; return (*this); } BigInteger &operator%= (int v) { if ((*this) == 0) return (*this); int cur = 0; int carry = 0; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur = (digits[i] + carry * 10); carry = (cur % v); } (*this) = carry; return (*this); } BigInteger &operator/= (const BigInteger& x) { if ((*this) == 0) {return (*this);} BigInteger ans; ans.digits.resize(digits.size()); BigInteger cur; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur.digits.insert(cur.digits.begin(),0); while (cur.digits.size() > 0 && cur.digits.back() == 0) cur.digits.pop_back(); if (cur.digits.size() == 0) {cur.positiv = true;cur.digits.push_back(0);} cur.digits[0] = digits[i]; int nd = -1; int left = 0; int right = 9; while (left <= right) { int mid = (left + right) >> 1; if ((x.abs() * mid).abs() <= cur.abs()) { nd = mid; left = mid + 1; } else right = mid - 1; } cur-=x.abs() * nd; ans.digits[i] = nd; } if (positiv != x.positiv) ans.positiv = false; else ans.positiv = true; while (ans.digits.size() > 0 && ans.digits.back() == 0) ans.digits.pop_back(); if (ans.digits.size() == 0) {ans.positiv = true;ans.digits.push_back(0);} *this = ans; return (*this); } int back() { return digits[0]; } BigInteger &operator%= (const BigInteger& x) { BigInteger ans; ans.digits.resize(digits.size()); BigInteger cur; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur.digits.insert(cur.digits.begin(),0); while (cur.digits.size() > 0 && cur.digits.back() == 0) cur.digits.pop_back(); if (!cur.digits.size()) cur.digits.push_back(0); cur.digits[0] = digits[i]; int nd = -1; int left = 0; int right = 9; while (left <= right) { int mid = (left + right) >> 1; if ((x.abs() * mid).abs() <= cur.abs()) { nd = mid; left = mid + 1; } else right = mid - 1; } cur-=x.abs() * nd; ans.digits[i] = nd; } while (cur.digits.size() > 0 && cur.digits.back() == 0) cur.digits.pop_back(); cur.positiv = positiv; if (!cur.digits.size()) {cur.positiv = true;cur.digits.push_back(0);} *this = cur; return (*this); } friend BigInteger operator+ (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans+=y; return ans; } BigInteger operator- () { BigInteger ans; ans = (*this); if (ans == 0) return ans; ans.positiv = (!ans.positiv); return ans; } friend BigInteger operator- (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans-=y; return ans; } friend BigInteger operator* (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans*=y; return ans; } friend BigInteger operator/ (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans/=y; return ans; } friend BigInteger operator% (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans%=y; return ans; } BigInteger operator--() { *this-=1; return *this; } BigInteger operator++() { *this+=1; return *this; } BigInteger operator--(int) { BigInteger ans = (*this); --(*this); return ans; } BigInteger operator++(int) { BigInteger ans = (*this); ++(*this); return ans; } int toInt(){ int ret=0,p=1; for (int i : digits){ ret+=i*p; p*=10; } return ret; } friend std::istream& operator>>(std::istream &in, BigInteger &x) { std::string S; in >> S; x = BigInteger(S); return in; } friend std::ostream &operator<<(std::ostream &out, const BigInteger &x) { out << x.toString(); return out; } }; BigInteger gcd (BigInteger a,BigInteger b) { if (!b) return a; else return gcd(b, a % b); } long long a,b,c,d,e,f,k,p,w,n,l; BigInteger ile[101]; vector <long long> in[101],out[101]; int main(){ scanf("%lld", &n); for (a=1; a<=n; ++a){ scanf("%lld", &p); while (p--){ scanf("%lld", &k); in[k].emplace_back(a); out[a].emplace_back(k); } } ile[1]=1; for (a=2; a<=n; ++a){ ile[a]=0; for (long long i : in[a]) ile[a]+=ile[i]; if (ile[a]==0||out[a].size()<2) continue; BigInteger oo=(int)out[a].size(),g; if (ile[a]>=oo) g=gcd(ile[a], oo); else g=gcd(oo, ile[a]); l=out[a].size()/g.toInt(); for (b=1; b<=a; ++b) ile[b]*=(int)l; ile[a]/=(int)out[a].size(); } ile[1]*=max(1, (int)out[1].size()); cout<<ile[1]; }
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | #include <iostream> #include <vector> #include <ccomplex> using namespace std; long double pi = acos(-1); int carm = -1; class BigInteger { // class based on a piece of code from pa-2021 private : bool positiv = true; std::vector <int> digits; public : BigInteger() {}; BigInteger(const BigInteger &x) { positiv = x.positiv; digits = x.digits; } ~BigInteger() { digits.clear(); digits.push_back(0); positiv = true; } BigInteger(const std::string &x) { positiv = true; if (x.size() == 0) digits.push_back(0); else { for (int i = (int)x.size() - 1;i >= 0;i--) if (x[i] >= '0' && x[i] <= '9') digits.push_back((int)((x[i])-'0')); if (x[0] == '-') positiv = false; } } BigInteger (const int &x) { positiv = (x >= 0); int y = x; if (x < 0) y = -x; while (y > 0) { digits.push_back(y % 10); y/=10; } if (!digits.size()) {positiv = true;digits.push_back(0);} } bool operator== (const BigInteger &x) const{ if (positiv != x.positiv) return false; if (x.digits != digits) return false; return true; } bool operator< (const BigInteger &x) const{ if ((*this) == x) return false; if (positiv != x.positiv) return !positiv; if (!positiv) { BigInteger z1 = (*this); BigInteger z2 = x; z1 = z1.abs(); z2 = z2.abs(); return (!(z1 < z2)); } if (x.digits.size() != digits.size()) return (digits.size() < x.digits.size()); for (int i = (int)digits.size() - 1;i >= 0;i--) if (digits[i] != x.digits[i]) return (digits[i] < x.digits[i]); return false; } bool operator> (const BigInteger &x) const{ if (x == (*this)) return false; return (!((*this) < x)); } bool operator!= (const BigInteger &x) const{ return !(x == (*this)); } bool operator<= (const BigInteger &x) const { if (x == (*this)) return true; return ((*this) < x); } bool operator>= (const BigInteger &x) const { if (x == (*this)) return true; return ((*this) > x); } std::string toString() const{ std::string ans = ""; if (!positiv) ans+='-'; for (int i = (int)digits.size() - 1;i >= 0;i--) ans+=((char)((digits[i])+'0')); if (ans.size() == 0) ans = "0"; return ans; } explicit operator bool() { //std::cout<<digits.size()<<" "<<digits[0]<<" "<<"!"<<'\n'; if (digits[0] == 0 && digits.size() == 1) return false; else return true; } BigInteger &operator+=(const BigInteger& x) { if (x.positiv == positiv) { int carry = 0; for (int i = 0;i < (int)digits.size() || i < (int)x.digits.size() || carry;i++) { if (i == (int)digits.size()) digits.push_back(0); if (i < (int)x.digits.size()) { digits[i]+=(carry + x.digits[i]); } else digits[i]+=carry; carry = (digits[i] / 10); digits[i]%=10; } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (!digits.size()) digits.push_back(0); return (*this); } else { if (abs() > x.abs()) { raznost_(x); return (*this); } else { BigInteger a1 = (*this); BigInteger a2 = x; a2.raznost_(a1); return (*this = a2); } } } void raznost_(const BigInteger &x) { int carry = 0; for (int i = 0;i < (int)x.digits.size() || carry;i++) { if (i < (int)x.digits.size()) digits[i]-=(carry + x.digits[i]); else digits[i]-=carry; carry = (digits[i] < 0); if (carry != 0) digits[i]+=10; } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (!digits.size()) {positiv = true;digits.push_back(0);} } BigInteger abs() const{ BigInteger wow = (*this); wow.positiv = true; return wow; } BigInteger &operator= (const BigInteger &x) { positiv = x.positiv; digits = x.digits; return (*this); } BigInteger &operator-= (const BigInteger &x) { if (positiv == x.positiv) { if (abs() >= x.abs()) {raznost_(x); return (*this);} else { BigInteger a1 = (*this); BigInteger a2 = x; a2.raznost_(a1); a2.positiv = (!a2.positiv); return (*this = a2); } } else { int carry = 0; for (int i = 0;i < (int)digits.size() || i < (int)x.digits.size() || carry;i++) { if (i == (int)digits.size()) digits.push_back(0); if (i < (int)x.digits.size()) { digits[i]+=(carry + x.digits[i]); } else digits[i]+=carry; carry = (digits[i] / 10); digits[i]%=10; } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (!digits.size()) digits.push_back(0); return (*this); } } typedef std::complex <long double> ftype; //for FFT inline void FFT (std::vector <ftype> &p, bool inv) { int n = (int)p.size(); if (n == 1) return; std::vector <ftype> a(n / 2); std::vector <ftype> b(n / 2); for (int i = 0;i < n / 2;i++) { a[i] = p[i * 2]; b[i] = p[i * 2 + 1]; } FFT(a, inv); FFT(b, inv); long double h = 2 * pi / n; if (inv) h*=-1; ftype w(1),wn(cos(h),sin(h)); for (int i = 0;i < n / 2;i++) { p[i] = a[i] + w * b[i]; p[i + n / 2] = a[i] - w * b[i]; if (inv) { p[i]/=2; p[i + n / 2]/=2; } w*=wn; } } inline std::vector <int> multiply (std::vector <int> a,std::vector <int> b) { std::vector <ftype> x (a.begin(),a.end()); std::vector <ftype> y (b.begin(),b.end()); int m = 1; while (m < std::max((int)a.size(),(int)b.size())) m*=2; m*=2; x.resize(m); y.resize(m); FFT(x, 0); FFT(y, 0); for (int i = 0;i < m;i++) x[i]*=y[i]; FFT(x, 1); std::vector <int> ans(m); for (int i = 0;i < m;i++) ans[i] = (int)(x[i].real() + 0.5); return ans; } BigInteger &operator*= (const int& x) { if ((*this) == 0) return (*this); if (x == 0) { (*this) = x; return (*this); } int carry = 0; for (int i = 0;i < (int)digits.size() || carry;i++) { if (i == (int)digits.size()) digits.push_back(0); int cur = (carry + (digits[i] * x)); digits[i] = (cur % 10); carry = (cur / 10); } while (digits.size() > 0 && digits.back() == 0) digits.pop_back(); if (digits.size() == 0) digits.push_back(0); return (*this); } BigInteger &operator*= (const BigInteger& x) { if ((*this) == 0) return (*this); if (x == 0) { (*this) = x; return (*this); } if (digits.size() + x.digits.size() <= 1000) { std::vector <int> zj(digits.size() + x.digits.size()); int carry = 0; for (int i = 0;i < (int)digits.size();i++) { for (int j = 0;j < (int)x.digits.size() || carry;j++) { int cl = zj[i + j] + digits[i] * 1 * (j < (int)x.digits.size() ? x.digits[j] : 0) + carry; zj[i + j] = (int)(cl % 10); carry = (int)(cl / 10); } } while (zj.size() > 0 && zj.back() == 0) zj.pop_back(); positiv = (!(positiv ^ x.positiv)); if (zj.size() == 0) {positiv = true;zj.push_back(0);} digits = zj; return (*this); } std::vector <int> zj = multiply(digits,x.digits); int ptr = (int)zj.size() - 1; while (ptr >= 0 && zj[ptr] == 0) --ptr; int carry = 0; positiv = (!(positiv ^ x.positiv)); std::vector <int> ans; ans.resize(ptr + 1); for (int i = 0;i <= ptr || carry;i++) { if (i > ptr) ans.push_back(0); ans[i] = (zj[i] + carry); carry = (zj[i] + carry) / 10; ans[i]%=10; } digits = ans; return (*this); } BigInteger &operator/= (int v) { if ((*this) == 0) return (*this); BigInteger ans; ans.digits.resize(digits.size()); int cur = 0; int carry = 0; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur = (digits[i] + carry * 10); ans.digits[i] = cur / v; carry = (cur % v); } while (ans.digits.size() > 0 && ans.digits.back() == 0) ans.digits.pop_back(); if (ans.digits.size() == 0) ans.digits.push_back(0); (*this) = ans; carm = carry; return (*this); } BigInteger &operator%= (int v) { if ((*this) == 0) return (*this); int cur = 0; int carry = 0; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur = (digits[i] + carry * 10); carry = (cur % v); } (*this) = carry; return (*this); } BigInteger &operator/= (const BigInteger& x) { if ((*this) == 0) {return (*this);} BigInteger ans; ans.digits.resize(digits.size()); BigInteger cur; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur.digits.insert(cur.digits.begin(),0); while (cur.digits.size() > 0 && cur.digits.back() == 0) cur.digits.pop_back(); if (cur.digits.size() == 0) {cur.positiv = true;cur.digits.push_back(0);} cur.digits[0] = digits[i]; int nd = -1; int left = 0; int right = 9; while (left <= right) { int mid = (left + right) >> 1; if ((x.abs() * mid).abs() <= cur.abs()) { nd = mid; left = mid + 1; } else right = mid - 1; } cur-=x.abs() * nd; ans.digits[i] = nd; } if (positiv != x.positiv) ans.positiv = false; else ans.positiv = true; while (ans.digits.size() > 0 && ans.digits.back() == 0) ans.digits.pop_back(); if (ans.digits.size() == 0) {ans.positiv = true;ans.digits.push_back(0);} *this = ans; return (*this); } int back() { return digits[0]; } BigInteger &operator%= (const BigInteger& x) { BigInteger ans; ans.digits.resize(digits.size()); BigInteger cur; for (int i = (int)digits.size() - 1;i >= 0;i--) { cur.digits.insert(cur.digits.begin(),0); while (cur.digits.size() > 0 && cur.digits.back() == 0) cur.digits.pop_back(); if (!cur.digits.size()) cur.digits.push_back(0); cur.digits[0] = digits[i]; int nd = -1; int left = 0; int right = 9; while (left <= right) { int mid = (left + right) >> 1; if ((x.abs() * mid).abs() <= cur.abs()) { nd = mid; left = mid + 1; } else right = mid - 1; } cur-=x.abs() * nd; ans.digits[i] = nd; } while (cur.digits.size() > 0 && cur.digits.back() == 0) cur.digits.pop_back(); cur.positiv = positiv; if (!cur.digits.size()) {cur.positiv = true;cur.digits.push_back(0);} *this = cur; return (*this); } friend BigInteger operator+ (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans+=y; return ans; } BigInteger operator- () { BigInteger ans; ans = (*this); if (ans == 0) return ans; ans.positiv = (!ans.positiv); return ans; } friend BigInteger operator- (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans-=y; return ans; } friend BigInteger operator* (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans*=y; return ans; } friend BigInteger operator/ (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans/=y; return ans; } friend BigInteger operator% (const BigInteger &x, const BigInteger &y) { BigInteger ans; ans = x; ans%=y; return ans; } BigInteger operator--() { *this-=1; return *this; } BigInteger operator++() { *this+=1; return *this; } BigInteger operator--(int) { BigInteger ans = (*this); --(*this); return ans; } BigInteger operator++(int) { BigInteger ans = (*this); ++(*this); return ans; } int toInt(){ int ret=0,p=1; for (int i : digits){ ret+=i*p; p*=10; } return ret; } friend std::istream& operator>>(std::istream &in, BigInteger &x) { std::string S; in >> S; x = BigInteger(S); return in; } friend std::ostream &operator<<(std::ostream &out, const BigInteger &x) { out << x.toString(); return out; } }; BigInteger gcd (BigInteger a,BigInteger b) { if (!b) return a; else return gcd(b, a % b); } long long a,b,c,d,e,f,k,p,w,n,l; BigInteger ile[101]; vector <long long> in[101],out[101]; int main(){ scanf("%lld", &n); for (a=1; a<=n; ++a){ scanf("%lld", &p); while (p--){ scanf("%lld", &k); in[k].emplace_back(a); out[a].emplace_back(k); } } ile[1]=1; for (a=2; a<=n; ++a){ ile[a]=0; for (long long i : in[a]) ile[a]+=ile[i]; if (ile[a]==0||out[a].size()<2) continue; BigInteger oo=(int)out[a].size(),g; if (ile[a]>=oo) g=gcd(ile[a], oo); else g=gcd(oo, ile[a]); l=out[a].size()/g.toInt(); for (b=1; b<=a; ++b) ile[b]*=(int)l; ile[a]/=(int)out[a].size(); } ile[1]*=max(1, (int)out[1].size()); cout<<ile[1]; } |