From 8c18daa10ada7e38976c8eafb8db108167b45dfb Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Wed, 20 Nov 2024 23:11:56 +0800 Subject: [PATCH] update --- src/P7113/P7113.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/P7113/P7113.cpp diff --git a/src/P7113/P7113.cpp b/src/P7113/P7113.cpp new file mode 100644 index 0000000..5c56848 --- /dev/null +++ b/src/P7113/P7113.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; +using ll = int64_t; +using i128 = __int128; + +const ll maxn{ll(1e5+5)}; +ll n,m,ind[maxn]; +pair nds[maxn]; +vector adj[maxn]; + +i128 gcd128(i128 m,i128 n){ + while(n!=0){ + i128 t{m%n}; + m=n; + n=t; + } + return m; +} + +void sf(i128 &p,i128 &q){ + i128 gcda{gcd128(p,q)}; + p/=gcda; + q/=gcda; +} + +pair add(i128 p1,i128 q1,i128 p2,i128 q2){ + i128 pa,qa; + i128 lcm{q1*q2/gcd128(q1,q2)}; + qa=lcm; + p1=p1*(lcm/q1); + p2=p2*(lcm/q2); + pa=p1+p2; + sf(pa,qa); + return {pa,qa}; +} + +ostream &operator<<(ostream &os, i128 num){ + if(num < 0){ + os << '-'; + num = -num; + } + string s; + if(num == 0){ + s = "0"; + } + while(num > 0){ + s += '0' + (num % 10); + num /= 10; + } + reverse(s.begin(), s.end()); + os << s; + return os; +} + + +int main(){ + iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); + + cin>>n>>m; + for(ll i{1};i<=n;i++){ + if(i<=m){ + nds[i]={1,1}; + }else{ + nds[i]={0,1}; + } + ll nn; + cin>>nn; + for(ll j{1};j<=nn;j++){ + ll d; + cin>>d; + adj[i].emplace_back(d); + ind[d]++; + } + } + queue qu; + for(ll i{1};i<=n;i++){ + if(ind[i]==0){ + qu.emplace(i); + } + } + while(qu.size()){ + auto u {qu.front()}; + qu.pop(); + for(auto &v:adj[u]){ + nds[v]=add(nds[v].first,nds[v].second,nds[u].first,nds[u].second*adj[u].size()); + if(--ind[v]==0){ + qu.emplace(v); + } + } + } + for(ll i{1};i<=n;i++){ + if(adj[i].size()==0){ + cout<