結果
問題 |
No.3250 最小公倍数
|
ユーザー |
|
提出日時 | 2025-08-30 03:51:19 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,869 ms / 2,000 ms |
コード長 | 2,811 bytes |
コンパイル時間 | 5,243 ms |
コンパイル使用メモリ | 335,828 KB |
実行使用メモリ | 44,992 KB |
最終ジャッジ日時 | 2025-08-30 03:51:43 |
合計ジャッジ時間 | 22,372 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 21 |
ソースコード
#include <bits/stdc++.h> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; using mint = modint998244353; // using mint = modint1000000007; // using mint = double; #endif //define using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long , long long>; using vi = vector<int>; using vll = vector<long long>; #define rep(i,l,r) for (int i = (int)(l); i < (int)(r); i++) #define rrep(i,l,r) for (int i = (int)(r-1); i >= (int)(l); i--) #define len(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define elif else if #define pb push_back #define eb emplace_back const int inf = 1e9; const long long infl = 1LL<<60; const int mod = 998244353; ll pow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; } template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; } template <class T> using spq = priority_queue<T, vector<T>, greater<T>>; template<class T>istream& operator>>(istream& i, vector<T>& v) {for(int j = 0; j < (int)(v).size(); j++) i >> v[j]; return i;} struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); } } iosetup; void solve(){ int n; cin >> n; vi a(n); cin >> a; vector<vi> e(n); rep(i, 0, n-1){ int u, v; cin >> u >> v; u--; v--; e[u].pb(v); e[v].pb(u); } int lim = 1e6 + 10; vi mip(lim, -1); rep(p, 2, lim){ if (mip[p] != -1) continue; for (int q=p; q<lim; q+=p){ if (mip[q] == -1) mip[q] = p; } } vll ans(n); auto dfs = [&](auto dfs, int u, int f) -> map<int, int>{ map<int, int> ndp; for (auto v : e[u]){ if (v != f){ auto dp = dfs(dfs, v, u); if (len(dp) > len(ndp)){ swap(dp, ndp); } for (auto [p, c] : dp){ chmax(ndp[p], c); } } } while(a[u] != 1){ int p = mip[a[u]]; int c = 0; while (p == mip[a[u]]){ c++; a[u] /= p; } chmax(ndp[p], c); } mint r = 1; for (auto [p, c] : ndp){ r *= pow(p, c, mod); } ans[u] = r.val(); return ndp; }; dfs(dfs, 0, -1); rep(i, 0, n){ cout << ans[i] << endl; } return ; } int main(){ int t; // cin >> t; t = 1; while(t--) solve(); return 0; }