Guest User

Untitled

a guest
Apr 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=100001;
  4. #define mod 1000000007
  5. #define ll long long
  6. ll r[N],parent[N],n,prime[N]={0},p,m1[N]={0},m2[N]={0};
  7. void calculate(ll a,ll b, ll flag){
  8.     ll curr=prime[a];
  9.     ll cnt=1;  
  10.     while (a>1) {
  11.         a/=prime[a];
  12.         if (curr==prime[a]){
  13.             cnt++;
  14.             continue;
  15.         }
  16.         if(flag==1)
  17.         m1[curr]+=b*cnt;
  18.         else m2[curr]+=b*cnt;
  19.         curr=prime[a];
  20.         cnt=1;
  21.     }
  22. }
  23. ll power(ll a,ll b){
  24.     ll res=1;
  25.     while(b){
  26.         if(b%2) res=(res*a)%mod;
  27.         a=(a*a)%mod;
  28.         b/=2;
  29.     }
  30.     return res;
  31. }
  32. void makeset(){
  33.     for(int i=1;i<=n;i++){
  34.          parent[i]=i;
  35.          r[i]=1;
  36.     }
  37. }
  38. ll find(ll x){
  39.     if(parent[x]!=x){
  40.         parent[x]=find(parent[x]);
  41.     }
  42.     return parent[x];
  43. }
  44. void unionset(ll x,ll y){
  45.     ll xp=find(x);
  46.     ll yp=find(y);
  47.    
  48.      if(xp==yp) return;
  49.         if(r[xp]>r[yp]){
  50.         parent[yp]=xp;
  51.         r[xp]+=r[yp];    
  52.     }
  53.     else{
  54.         parent[xp]=yp;
  55.         r[yp]+=r[xp];
  56.     }
  57. }
  58. int main(){
  59.     ios::sync_with_stdio(false);
  60.     cin.tie(0);
  61. //    #ifndef ONLINE_JUDGE
  62. //    freopen("in01.txt","r",stdin);
  63. //  freopen("out01.txt","w",stdout);
  64. //  #endif
  65.    
  66.     prime[1]=1;
  67.     for(int i=2;i<=N;i++) prime[i]=i;
  68.     for(int i=2;i*i<=N;i++){
  69.         if(prime[i]==i){
  70.             for(int j=i*i;j<=N;j+=i)
  71.                 prime[j]=i;
  72.         }
  73.     }
  74.    
  75.     cin>>n;
  76.         makeset();
  77.         vector<pair<int,pair<int,int > > > v;
  78.         ll x,y,k,ans=1,m=0;
  79.         int c=0;
  80.         for(int i=0;i<n-1;i++){
  81.             cin>>x>>y>>k;
  82.             v.push_back(make_pair(k,make_pair(x,y)));
  83.             if(k==1) c++;
  84.         }
  85.         sort(v.begin(),v.end());
  86.          vector<pair<int,pair<int,int > > > :: iterator it,itr,itr2;
  87.          it=v.begin();
  88.         for( it;it!=v.end();it++){
  89.              p=it->first;
  90.             ll q=r[find(it->second.first)]*r[find(it->second.second)];
  91.             ans=(ans*power(p,q))%mod;
  92.             calculate(p,q,1);
  93.             unionset(it->second.first,it->second.second);
  94.         }
  95.         cout<<ans<<"\n";
  96.         makeset();
  97.         ans=1;
  98.         itr=v.end();
  99.        itr--;
  100.         itr2=v.begin();
  101.        itr2--;
  102.         for( it=itr;it!=itr2;it--){
  103.             ll p=it->first;
  104.             ll q=r[find(it->second.first)]*r[find(it->second.second)];
  105.             ans=(ans*power(p,q))%mod;
  106.             calculate(p,q,2);
  107.             unionset(it->second.first,it->second.second);
  108.         }
  109.         cout<<ans<<"\n";
  110.        
  111.         ll res=INT_MAX;
  112.         for(int i=1;i<N;i++){
  113.          if(prime[i]!=i) continue;
  114.          ll x=m1[i];
  115.          ll y=m2[i];
  116.          if(y==0) continue;
  117.          ll z=x/y;
  118.          res=min(res,z);
  119.          if(y>x) {res=0;break;}
  120.      }
  121.     if(res==INT_MAX && c!=n-1) cout<<"0\n";
  122.     else if(c==n-1) cout<<"1\n";
  123.     else cout<<res;
  124.    
  125. }
Add Comment
Please, Sign In to add comment