#include <cstdio> #include <vector> #include <queue> #include <set> #include <algorithm> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } const int N = 100 + 9; const int P = 25; int prime[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 }; vector<int> divx[N]; class big { static const int BASE = 10000; // be careful in 'void show();' vector<int> v; public: void set0() { v = vector<int>(1, 0); } void set1() { v = vector<int>(1, 1); } big &operator*=(int n) { for (int i=0; i<v.size(); ++i) v[i] *= n; for (int i=0; i+1<v.size(); ++i) { if (v[i] >= BASE) { v[i + 1] += v[i] / BASE; v[i] %= BASE; } } if (v.back() >= BASE) { int add = v.back() / BASE; v.back() %= BASE; v.push_back(add); } return *this; } big &operator+=(const big &oth) { if (v.size() < oth.v.size()) v.resize(oth.v.size(), 0); for (int i=0; i<oth.v.size(); ++i) { v[i] += oth.v[i]; if (v[i] >= BASE) { v[i] -= BASE; if (i + 1 >= v.size()) v.push_back(0); v[i + 1] += 1; } } return *this; } int operator%(int p) { int res = 0; for (int i=v.size()-1; i>=0; --i) { res = res * BASE + v[i]; res %= p; } return res; } big &operator/=(int p) { for (int i=v.size()-1; i>0; --i) { v[i - 1] += (v[i] % p) * BASE; v[i] /= p; } v[0] /= p; while (!v.empty() && v.back() == 0) v.pop_back(); return *this; } void show(const char *nl = "\n") { printf("%d", v.back()); for (int i=v.size()-2; i>=0; --i) { if (v[i] < 1000) printf("0"); if (v[i] < 100) printf("0"); if (v[i] < 10) printf("0"); printf("%d", v[i]); } printf("%s", nl); } }; class fact { big a; vector<int> b; public: void set0() { a.set0(); b.clear(); } void set1() { a.set1(); b.clear(); } fact &operator/=(int d) { b.push_back(d); return *this; } fact &operator+=(const fact &oth) { big a2(oth.a); vector<int> b2(oth.b); sort(b.begin(), b.end()); sort(b2.begin(), b2.end()); int i = 0; int bn = b.size(); int j = 0; int b2n = b2.size(); while (i < bn && j < b2n) { if (b[i] < b2[j]) { a2 *= b[i]; ++i; continue; } if (b[i] > b2[j]) { a *= b2[j]; b.push_back(b2[j]); ++j; continue; } ++i; ++j; } while (i < bn) { a2 *= b[i]; ++i; } while (j < b2n) { a *= b2[j]; b.push_back(b2[j]); ++j; } a += a2; return *this; } void show(const char *nl = "\n") { a.show("/"); sort(b.begin(), b.end()); const char *sp = " "; for (int i=0; i<b.size(); ++i) printf("%d%s", b[i], i+1<b.size() ? sp : nl); } void adjust(int *res) { for (int i=0; i<b.size(); ++i) { int d = b[i]; for (int j=0; j<divx[d].size(); ++j) { int p = divx[d][j]; if (a % p == 0) { a /= p; } else { ++res[p]; } } } } }; void init() { for (int d=2; d<=99; ++d) { int n = d; for (int i=0; i<P; ++i) { if (n % prime[i] == 0) { divx[d].push_back(prime[i]); n /= prime[i]; --i; } } } } int main() { init(); int n = nextInt(); vector<int> visited(n + 1, 0); vector<fact> p(n + 1); vector<int> final; visited[1] = 1; p[1].set1();; for (int i=2; i<=n; ++i) p[i].set0(); for (int i=1; i<=n; ++i) { int c = nextInt(); if (0 == visited[i]) { for (int k=0; k<c; ++k) nextInt(); continue; } if (0 == c) continue; if (c > 1) final.push_back(i); if (c > 1) p[i] /= c; for (int k=0; k<c; ++k) { int x = nextInt(); visited[x] = 1; p[x] += p[i]; } } int resArr[100]; for (int i=0; i<100; ++i) resArr[i] = 0; for (int i=0; i<final.size(); ++i) { int arr[100]; for (int i=0; i<100; ++i) arr[i] = 0; p[final[i]].adjust(arr); for (int i=0; i<100; ++i) if (arr[i] > resArr[i]) resArr[i] = arr[i]; } big res; res.set1(); for (int i=0; i<100; ++i) while (resArr[i]--) res *= i; res.show(); 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 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 | #include <cstdio> #include <vector> #include <queue> #include <set> #include <algorithm> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } const int N = 100 + 9; const int P = 25; int prime[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 }; vector<int> divx[N]; class big { static const int BASE = 10000; // be careful in 'void show();' vector<int> v; public: void set0() { v = vector<int>(1, 0); } void set1() { v = vector<int>(1, 1); } big &operator*=(int n) { for (int i=0; i<v.size(); ++i) v[i] *= n; for (int i=0; i+1<v.size(); ++i) { if (v[i] >= BASE) { v[i + 1] += v[i] / BASE; v[i] %= BASE; } } if (v.back() >= BASE) { int add = v.back() / BASE; v.back() %= BASE; v.push_back(add); } return *this; } big &operator+=(const big &oth) { if (v.size() < oth.v.size()) v.resize(oth.v.size(), 0); for (int i=0; i<oth.v.size(); ++i) { v[i] += oth.v[i]; if (v[i] >= BASE) { v[i] -= BASE; if (i + 1 >= v.size()) v.push_back(0); v[i + 1] += 1; } } return *this; } int operator%(int p) { int res = 0; for (int i=v.size()-1; i>=0; --i) { res = res * BASE + v[i]; res %= p; } return res; } big &operator/=(int p) { for (int i=v.size()-1; i>0; --i) { v[i - 1] += (v[i] % p) * BASE; v[i] /= p; } v[0] /= p; while (!v.empty() && v.back() == 0) v.pop_back(); return *this; } void show(const char *nl = "\n") { printf("%d", v.back()); for (int i=v.size()-2; i>=0; --i) { if (v[i] < 1000) printf("0"); if (v[i] < 100) printf("0"); if (v[i] < 10) printf("0"); printf("%d", v[i]); } printf("%s", nl); } }; class fact { big a; vector<int> b; public: void set0() { a.set0(); b.clear(); } void set1() { a.set1(); b.clear(); } fact &operator/=(int d) { b.push_back(d); return *this; } fact &operator+=(const fact &oth) { big a2(oth.a); vector<int> b2(oth.b); sort(b.begin(), b.end()); sort(b2.begin(), b2.end()); int i = 0; int bn = b.size(); int j = 0; int b2n = b2.size(); while (i < bn && j < b2n) { if (b[i] < b2[j]) { a2 *= b[i]; ++i; continue; } if (b[i] > b2[j]) { a *= b2[j]; b.push_back(b2[j]); ++j; continue; } ++i; ++j; } while (i < bn) { a2 *= b[i]; ++i; } while (j < b2n) { a *= b2[j]; b.push_back(b2[j]); ++j; } a += a2; return *this; } void show(const char *nl = "\n") { a.show("/"); sort(b.begin(), b.end()); const char *sp = " "; for (int i=0; i<b.size(); ++i) printf("%d%s", b[i], i+1<b.size() ? sp : nl); } void adjust(int *res) { for (int i=0; i<b.size(); ++i) { int d = b[i]; for (int j=0; j<divx[d].size(); ++j) { int p = divx[d][j]; if (a % p == 0) { a /= p; } else { ++res[p]; } } } } }; void init() { for (int d=2; d<=99; ++d) { int n = d; for (int i=0; i<P; ++i) { if (n % prime[i] == 0) { divx[d].push_back(prime[i]); n /= prime[i]; --i; } } } } int main() { init(); int n = nextInt(); vector<int> visited(n + 1, 0); vector<fact> p(n + 1); vector<int> final; visited[1] = 1; p[1].set1();; for (int i=2; i<=n; ++i) p[i].set0(); for (int i=1; i<=n; ++i) { int c = nextInt(); if (0 == visited[i]) { for (int k=0; k<c; ++k) nextInt(); continue; } if (0 == c) continue; if (c > 1) final.push_back(i); if (c > 1) p[i] /= c; for (int k=0; k<c; ++k) { int x = nextInt(); visited[x] = 1; p[x] += p[i]; } } int resArr[100]; for (int i=0; i<100; ++i) resArr[i] = 0; for (int i=0; i<final.size(); ++i) { int arr[100]; for (int i=0; i<100; ++i) arr[i] = 0; p[final[i]].adjust(arr); for (int i=0; i<100; ++i) if (arr[i] > resArr[i]) resArr[i] = arr[i]; } big res; res.set1(); for (int i=0; i<100; ++i) while (resArr[i]--) res *= i; res.show(); return 0; } |