結果

問題 No.3250 最小公倍数
ユーザー hint908
提出日時 2025-08-29 21:58:03
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 10,389 bytes
コンパイル時間 4,136 ms
コンパイル使用メモリ 314,436 KB
実行使用メモリ 44,560 KB
最終ジャッジ日時 2025-08-29 21:58:47
合計ジャッジ時間 9,038 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 5 TLE * 13 -- * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "src.cpp"
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")


#include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = V<VV<T>>;
template<class T> using VVVV = VV<VV<T>>;
#define rep(i,n) for(ll i=0ll;(i)<(n);(i)++)
#define REP(i,a,n) for(ll i=(a);(i)<(n);(i)++)
#define rrep(i,n) for(ll i=(n)-1;(i)>=(0ll);(i)--)
#define RREP(i,a,n) for(ll i=(n)-1;(i)>=(a);(i)--)
const long long INF = (1LL << 60);
const long long mod99 = 998244353;
const long long mod107 = 1000000007;
const long long mod = mod99;
#define eb emplace_back
#define be(v) (v).begin(),(v).end()
#define all(v) (v).begin(),(v).end()
#define foa(i,v) for(auto& (i) : (v))
#define UQ(v) sort(be(v)), (v).erase(unique(be(v)), (v).end())
#define UQ2(v,cmp) sort(be(v)), (v).erase(unique(be(v),cmp), (v).end())
#define UQ3(v,cmp) sort(be(v),cmp), (v).erase(unique(be(v)), (v).end())
#define UQ4(v,cmp,cmp2) sort(be(v), cmp), (v).erase(unique(be(v),cmp2), (v).end())
#define LB(x,v) (lower_bound(be(v),(x))-(v).begin())
#define LB2(x,v,cmp) (lower_bound(be(v),(x),(cmp))-(v).begin())
#define UB(x,v) (upper_bound(be(v),(x))-(v).begin())
#define UB2(x,v,cmp) (upper_bound(be(v),(x),(cmp))-(v).begin())
#define dout()  cout << fixed << setprecision(20)
#define randinit() srand((unsigned)time(NULL))

template<class T, class U> bool chmin(T& t, const U& u) { if (t > u){ t = u; return 1;} return 0; }
template<class T, class U> bool chmax(T& t, const U& u) { if (t < u){ t = u; return 1;} return 0; }


ll Rnd(ll L=0, ll R=mod99){return rand()%(R-L)+L;}

#line 2 "tree/auxiliary-tree.hpp"

#line 6 "tree/auxiliary-tree.hpp"
using namespace std;

#line 2 "tree/heavy-light-decomposition.hpp"

#line 2 "graph/graph-template.hpp"

template <typename T>
struct edge {
  int src, to;
  T cost;

  edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
  edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;

// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
                      bool is_1origin = true) {
  UnweightedGraph g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    if (is_1origin) x--, y--;
    g[x].push_back(y);
    if (!is_directed) g[y].push_back(x);
  }
  return g;
}

// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
                        bool is_1origin = true) {
  WeightedGraph<T> g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    cin >> c;
    if (is_1origin) x--, y--;
    g[x].emplace_back(x, y, c);
    if (!is_directed) g[y].emplace_back(y, x, c);
  }
  return g;
}

// Input of Edges
template <typename T>
Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true,
                 bool is_1origin = true) {
  Edges<T> es;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    es.emplace_back(x, y, c);
  }
  return es;
}

// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
                           bool is_directed = false, bool is_1origin = true) {
  vector<vector<T>> d(N, vector<T>(N, INF));
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    d[x][y] = c;
    if (!is_directed) d[y][x] = c;
  }
  return d;
}

/**
 * @brief グラフテンプレート
 * @docs docs/graph/graph-template.md
 */
#line 4 "tree/heavy-light-decomposition.hpp"

template <typename G>
struct HeavyLightDecomposition {
 private:
  void dfs_sz(int cur) {
    size[cur] = 1;
    for (auto& dst : g[cur]) {
      if (dst == par[cur]) {
        if (g[cur].size() >= 2 && int(dst) == int(g[cur][0]))
          swap(g[cur][0], g[cur][1]);
        else
          continue;
      }
      depth[dst] = depth[cur] + 1;
      par[dst] = cur;
      dfs_sz(dst);
      size[cur] += size[dst];
      if (size[dst] > size[g[cur][0]]) {
        swap(dst, g[cur][0]);
      }
    }
  }

  void dfs_hld(int cur) {
    down[cur] = id++;
    for (auto dst : g[cur]) {
      if (dst == par[cur]) continue;
      nxt[dst] = (int(dst) == int(g[cur][0]) ? nxt[cur] : int(dst));
      dfs_hld(dst);
    }
    up[cur] = id;
  }

  // [u, v)
  vector<pair<int, int>> ascend(int u, int v) const {
    vector<pair<int, int>> res;
    while (nxt[u] != nxt[v]) {
      res.emplace_back(down[u], down[nxt[u]]);
      u = par[nxt[u]];
    }
    if (u != v) res.emplace_back(down[u], down[v] + 1);
    return res;
  }

  // (u, v]
  vector<pair<int, int>> descend(int u, int v) const {
    if (u == v) return {};
    if (nxt[u] == nxt[v]) return {{down[u] + 1, down[v]}};
    auto res = descend(u, par[nxt[v]]);
    res.emplace_back(down[nxt[v]], down[v]);
    return res;
  }

 public:
  G& g;
  int root, id;
  vector<int> size, depth, down, up, nxt, par;
  HeavyLightDecomposition(G& _g, int _root = 0)
      : g(_g),
        root(_root),
        id(0),
        size(g.size(), 0),
        depth(g.size(), 0),
        down(g.size(), -1),
        up(g.size(), -1),
        nxt(g.size(), root),
        par(g.size(), root) {
    dfs_sz(root);
    dfs_hld(root);
  }

  pair<int, int> idx(int i) const { return make_pair(down[i], up[i]); }

  template <typename F>
  void path_query(int u, int v, bool vertex, const F& f) {
    int l = lca(u, v);
    for (auto&& [a, b] : ascend(u, l)) {
      int s = a + 1, t = b;
      s > t ? f(t, s) : f(s, t);
    }
    if (vertex) f(down[l], down[l] + 1);
    for (auto&& [a, b] : descend(l, v)) {
      int s = a, t = b + 1;
      s > t ? f(t, s) : f(s, t);
    }
  }

  template <typename F>
  void path_noncommutative_query(int u, int v, bool vertex, const F& f) {
    int l = lca(u, v);
    for (auto&& [a, b] : ascend(u, l)) f(a + 1, b);
    if (vertex) f(down[l], down[l] + 1);
    for (auto&& [a, b] : descend(l, v)) f(a, b + 1);
  }

  template <typename F>
  void subtree_query(int u, bool vertex, const F& f) {
    f(down[u] + int(!vertex), up[u]);
  }

  int lca(int a, int b) {
    while (nxt[a] != nxt[b]) {
      if (down[a] < down[b]) swap(a, b);
      a = par[nxt[a]];
    }
    return depth[a] < depth[b] ? a : b;
  }

  int dist(int a, int b) { return depth[a] + depth[b] - depth[lca(a, b)] * 2; }
};

/**
 * @brief Heavy Light Decomposition(重軽分解)
 * @docs docs/tree/heavy-light-decomposition.md
 */
#line 9 "tree/auxiliary-tree.hpp"

template <typename G>
struct AuxiliaryTree {
  G g;
  HeavyLightDecomposition<G> hld;
  AuxiliaryTree(const G& _g, int root = 0) : g(_g), hld(g, root) {}

  // ps : 頂点集合
  // 返り値 : (aux tree, aux tree の頂点と g の頂点の対応表)
  // aux tree は 親->子 の向きの辺のみ含まれる
  // ps が空の場合は空のグラフを返す
  pair<vector<vector<int>>, vector<int>> get(vector<int> ps) {
    if (ps.empty()) return {};
    auto comp = [&](int i, int j) { return hld.down[i] < hld.down[j]; };
    sort(begin(ps), end(ps), comp);
    for (int i = 0, ie = ps.size(); i + 1 < ie; i++) {
      ps.push_back(hld.lca(ps[i], ps[i + 1]));
    }
    sort(begin(ps), end(ps), comp);
    ps.erase(unique(begin(ps), end(ps)), end(ps));

    vector<vector<int>> aux(ps.size());
    vector<int> rs;
    rs.push_back(0);
    for (int i = 1; i < (int)ps.size(); i++) {
      int l = hld.lca(ps[rs.back()], ps[i]);
      while (ps[rs.back()] != l) rs.pop_back();
      aux[rs.back()].push_back(i);
      rs.push_back(i);
    }
    return make_pair(aux, ps);
  }
};

/**
 * @brief Auxiliary Tree
 */
#line 46 "src.cpp"

#include<atcoder/modint>
using namespace atcoder;
using mint = modint998244353;

void solve(){
    ll n;
    cin >> n;
    V<ll> v(n);
    rep(i,n) cin >> v[i];
    auto G = graph(n, n-1);
    
    AuxiliaryTree auxgen(G, 0);
    
    V<mint> ans(n, mint::raw(1));
    V<ll> prime;
    V<ll> vv(n, 1);
    for(int i=2; i<=1000; i++){
        ll ok = 1;
        for(auto x:prime) if(i%x == 0) ok = 0;
        if(!ok) continue; 
        V<int> node;
        rep(j, n) if(v[j]%i == 0){
            vv[j] = 1;
            while(v[j]%i == 0){
                v[j] /= i;
                vv[j] *= i;
            }
            node.eb(j);
        }
        if(node.empty()) continue;
        auto[aux, mp] = auxgen.get(node);
        
        auto dfs = [&](auto&&dfs, ll nd) -> ll {
            ll num = mp[nd];
            if(vv[num]%i != 0) vv[num] = 1;
            ll ret = vv[num];
            mint val = 1;
            for(auto nx:aux[nd]){
                ll a = dfs(dfs, nx);
                chmax(ret, a);
                val *= a;
            }
            val = ret / val;
            ans[num] *= val;
            return ret;
        };
        dfs(dfs, 0);
    }
    
    map<ll, V<int>> mm;
    rep(i, n) if(v[i] != 1){
        mm[v[i]].eb(i);
    }
    
    for(auto [x,node]:mm){
        auto[aux, mp] = auxgen.get(node);
        auto dfs = [&](auto&&dfs, ll nd) -> ll {
            ll num = mp[nd];
            ll ret = 1;
            if(v[num] == x) ret = x;
            mint val = 1;
            for(auto nx:aux[nd]){
                ll a = dfs(dfs, nx);
                chmax(ret, a);
                val *= a;
            }
            val = ret / val;
            ans[num] *= val;
            return ret;
        };
        dfs(dfs, 0);
    }
    
    auto dfs = [&](auto&&dfs, ll nd, ll par) -> void {
        for(auto nx:G[nd]) if(nx != par){
            dfs(dfs, nx, nd);
            ans[nd] *= ans[nx];
        }
    };
    dfs(dfs, 0, -1);
    rep(i, n) cout << ans[i].val() << "\n";
    
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t=1;
    // cin >> t;
    rep(i,t) solve();
}
0