cosenza987

Untitled

Apr 20th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. const int mod = 1e9 + 7, N = 1e7 + 1;
  8.  
  9. struct LCA {
  10.     //one indexed shit
  11.     int n, l;
  12.     vector<vector<int>> adj, up;
  13.     vector<int> h;
  14.     LCA (int n_) {
  15.         n = n_;
  16.         l = 31 - __builtin_clz(n);
  17.         adj.resize(n + 1);
  18.         up.assign(n + 1, vector<int>(l + 1));
  19.         h.resize(n + 1);
  20.     }
  21.     void add(int a, int b) {
  22.         adj[a].push_back(b);
  23.         adj[b].push_back(a);
  24.     }
  25.     void dfs(int v = 1, int p = 0, int hh = 0) {
  26.         up[v][0] = p;
  27.         for(int i = 1; i <= l; i++) {
  28.             up[v][i] = up[up[v][i - 1]][i - 1];
  29.         }
  30.         for(auto u : adj[v]) {
  31.             if(u != p) {
  32.                 dfs(u, v, hh + 1);
  33.             }
  34.         }
  35.         h[v] = hh;
  36.     }
  37.     int lca(int a, int b) {
  38.         if(h[a] > h[b]) {
  39.             swap(a, b);
  40.         }
  41.         int dff = h[b] - h[a], cnt = 0;
  42.         while(dff) {
  43.             if(dff & 1) {
  44.                 b = up[b][cnt];
  45.             }
  46.             cnt++;
  47.             dff >>= 1;
  48.         }
  49.         if(a == b) {
  50.             return a;
  51.         }
  52.         for(int i = l; i >= 0; i--) {
  53.             if(up[a][i] != up[b][i]) {
  54.                 a = up[a][i];
  55.                 b = up[b][i];
  56.             }
  57.         }
  58.         return up[a][0];
  59.     }
  60.     int raise(int node, int dist) {
  61.         //if it returns 0, then it fucked up
  62.         int cnt = 0;
  63.         while(dist) {
  64.             if(dist & 1) {
  65.                 node = up[node][cnt];
  66.             }
  67.             cnt++;
  68.             dist >>= 1;
  69.         }
  70.         return node;
  71.     }
  72.     int dist(int a, int b) {
  73.         int z = lca(a, b);
  74.         return h[a] + h[b] - 2 * h[z];
  75.     }
  76. };
  77.  
  78. long long binexp(long long a, long long n) {
  79.     long long res = 1;
  80.     while(n) {
  81.         if(n & 1) {
  82.             res = (res * a) % mod;
  83.         }
  84.         a = (a * a) % mod;
  85.         n >>= 1;
  86.     }
  87.     return res;
  88. }
  89.  
  90. int lp[N];
  91. bool comp[N];
  92.  
  93. int main() {
  94.     ios_base::sync_with_stdio(false);
  95.     cin.tie(nullptr);
  96.     for(long long i = 2; i < N; i++) {
  97.         if(!comp[i]) {
  98.             lp[i] = i;
  99.             for(long long j = i * i; j < N; j += i) {
  100.                 lp[j] = i;
  101.                 comp[j] = true;
  102.             }
  103.         }
  104.     }
  105.     int n, q;
  106.     cin >> n >> q;
  107.     map<int, int> mp[n + 1];
  108.     for(int i = 1; i <= n; i++) {
  109.         int x;
  110.         cin >> x;
  111.         while(x > 1) {
  112.             mp[i][lp[x]]++;
  113.             x /= lp[x];
  114.         }
  115.     }
  116.     LCA lca(n);
  117.     for(int i = 1; i < n; i++) {
  118.         int a, b;
  119.         cin >> a >> b;
  120.         lca.add(a, b);
  121.     }
  122.     lca.dfs();
  123.     while(q--) {
  124.         int t, a, b;
  125.         cin >> t >> a >> b;
  126.         if(t == 1) {
  127.             int c = lca.lca(a, b);
  128.             map<int, int> ans;
  129.             while(a != c) {
  130.                 for(auto e : mp[a]) {
  131.                     ans[e.first] = max(ans[e.first], e.second);
  132.                 }
  133.                 a = lca.raise(a, 1);
  134.             }
  135.             a = b;
  136.             while(a != c) {
  137.                 for(auto e : mp[a]) {
  138.                     ans[e.first] = max(ans[e.first], e.second);
  139.                 }
  140.                 a = lca.raise(a, 1);
  141.             }
  142.             for(auto e : mp[c]) {
  143.                 ans[e.first] = max(ans[e.first], e.second);
  144.             }
  145.             long long res = 1;
  146.             for(auto e : ans) {
  147.                 res = (res * binexp(e.first, e.second)) % mod;
  148.             }
  149.             cout << res << "\n";
  150.         } else {
  151.             while(b > 1) {
  152.                 mp[a][lp[b]]++;
  153.                 b /= lp[b];
  154.             }
  155.         }
  156.     }
  157.     return 0;
  158. }
Add Comment
Please, Sign In to add comment