#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 105; string div(string a, int b){ string res=""; bool wasNonZero=0; int carry=0; for(int i=0; i<a.size(); ++i){ int num = a[i]-'0'+carry*10; if(num/b>0 || wasNonZero){ res += char(num/b+'0'); wasNonZero=1; } carry = num%b; } if(!wasNonZero) return "0"; return res; } int mod(string a, int b){ int rest=0; for(int i=0; i<a.size(); ++i){ rest = rest*10 + a[i]-'0'; rest %= b; } return rest; } int gcd(string a, int b){ for(int i=b; i>=1; --i){ if(b%i==0 && mod(a,i)==0){ return i; } } return 0; } string add(string a, string b){ if(a.size() < b.size()) swap(a,b); string buf = ""; for(int i=0; i<a.size()-b.size(); ++i) buf+="0"; b = buf+b; if(a[0]!='0' || b[0]!='0'){ a="0"+a; b="0"+b; } for(int i=a.size()-1; i>=0; --i){ a[i] += b[i]-'0'; if(a[i]>'9'){ a[i]-=10; a[i-1]++; } } return a; } string mul(string a, int b){ if(b==0) return "0"; string res="0"; while(b>0){ if(b%2==1){ res = add(res,a); } a=add(a,a); b/=2; } return res; } vector<vector<int>> g(N); //ll cnt[N]; string cnt[N]; int main(){ ios_base::sync_with_stdio(0); int n; // ll res=1; string res="1"; cin >> n; for(int i=1; i<=n; ++i){ int r; cin >> r; while(r--){ int a; cin >> a; g[i].push_back(a); } } g[0].push_back(1); bool repeat=1; while(repeat){ repeat=0; for(int i=0; i<N; ++i){ cnt[i] = "0"; } cnt[0] = res; // cout << "NEW:" << res << endl; for(int v=0; v<n; ++v){ // cout << "v:" << v << " cnt[v]:" << cnt[v] << endl; ll siz = g[v].size(); if(siz==0) continue; // cout << "cnt[v]:" << cnt[v] << " siz:" << siz << " mod:"<<mod(cnt[v],siz) <<endl; if(mod(cnt[v],siz) != 0){ // res *= siz/ll(__gcd(cnt[v], siz)); ll G = gcd(cnt[v], siz); ll toMul = siz/G; res = mul(res,toMul); // cout << "G:" << G << " toMul:" << toMul << " res:" << res << endl; repeat=1; break; }else{ for(auto &u : g[v]){ cnt[u] = add(cnt[u], div(cnt[v],siz)); } } } } while(res.size()>1 && res[0]=='0') res.erase(res.begin()); if(res.empty()) cout << "0"; else cout << res; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 105; string div(string a, int b){ string res=""; bool wasNonZero=0; int carry=0; for(int i=0; i<a.size(); ++i){ int num = a[i]-'0'+carry*10; if(num/b>0 || wasNonZero){ res += char(num/b+'0'); wasNonZero=1; } carry = num%b; } if(!wasNonZero) return "0"; return res; } int mod(string a, int b){ int rest=0; for(int i=0; i<a.size(); ++i){ rest = rest*10 + a[i]-'0'; rest %= b; } return rest; } int gcd(string a, int b){ for(int i=b; i>=1; --i){ if(b%i==0 && mod(a,i)==0){ return i; } } return 0; } string add(string a, string b){ if(a.size() < b.size()) swap(a,b); string buf = ""; for(int i=0; i<a.size()-b.size(); ++i) buf+="0"; b = buf+b; if(a[0]!='0' || b[0]!='0'){ a="0"+a; b="0"+b; } for(int i=a.size()-1; i>=0; --i){ a[i] += b[i]-'0'; if(a[i]>'9'){ a[i]-=10; a[i-1]++; } } return a; } string mul(string a, int b){ if(b==0) return "0"; string res="0"; while(b>0){ if(b%2==1){ res = add(res,a); } a=add(a,a); b/=2; } return res; } vector<vector<int>> g(N); //ll cnt[N]; string cnt[N]; int main(){ ios_base::sync_with_stdio(0); int n; // ll res=1; string res="1"; cin >> n; for(int i=1; i<=n; ++i){ int r; cin >> r; while(r--){ int a; cin >> a; g[i].push_back(a); } } g[0].push_back(1); bool repeat=1; while(repeat){ repeat=0; for(int i=0; i<N; ++i){ cnt[i] = "0"; } cnt[0] = res; // cout << "NEW:" << res << endl; for(int v=0; v<n; ++v){ // cout << "v:" << v << " cnt[v]:" << cnt[v] << endl; ll siz = g[v].size(); if(siz==0) continue; // cout << "cnt[v]:" << cnt[v] << " siz:" << siz << " mod:"<<mod(cnt[v],siz) <<endl; if(mod(cnt[v],siz) != 0){ // res *= siz/ll(__gcd(cnt[v], siz)); ll G = gcd(cnt[v], siz); ll toMul = siz/G; res = mul(res,toMul); // cout << "G:" << G << " toMul:" << toMul << " res:" << res << endl; repeat=1; break; }else{ for(auto &u : g[v]){ cnt[u] = add(cnt[u], div(cnt[v],siz)); } } } } while(res.size()>1 && res[0]=='0') res.erase(res.begin()); if(res.empty()) cout << "0"; else cout << res; return 0; } |