結果
問題 | No.2497 GCD of LCMs |
ユーザー |
![]() |
提出日時 | 2024-11-12 14:42:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 48 ms / 2,000 ms |
コード長 | 2,472 bytes |
コンパイル時間 | 2,621 ms |
コンパイル使用メモリ | 222,128 KB |
最終ジャッジ日時 | 2025-02-25 03:59:23 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 14 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/modint>using namespace std;using namespace atcoder;using ll = long long;using mint = modint998244353;map<ll, ll> prime;void prime_factor(ll n){prime.clear();ll m = n;if (n % 2 == 0){while(n % 2 == 0){prime[2]++;n /= 2;}}for (ll i = 3; i*i <= m; i+=2){if (n % i == 0){while(n % i == 0){prime[i]++;n /= i;}}}if (n != 1){prime[n]++;}}template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;vector<ll> dijkstra(vector<vector<ll>> &E, vector<ll> &B){ll N = E.size();ll from, alt, d;pq<pair<ll, ll>> que;vector<ll> dist(N, 1e18);vector<bool> vst(N);dist[0] = B[0];que.push({B[0], 0});while(!que.empty()){tie(d, from) = que.top();que.pop();if (vst[from]) continue;vst[from] = 1;for (auto to : E[from]){alt = max(d, B[to]);if (alt < dist[to]){dist[to] = alt;que.push({dist[to], to});}}}return dist;}int main(){cin.tie(nullptr);ios_base::sync_with_stdio(false);/*Aの素因数pごとに考える。素因数pの指数eを考えると、LCMのGCD->MAXのMIN1->xに行く時のeの最大値の最小値でdist(x)を定める。p^dist(x)の積*/ll N, M, u, v;cin >> N >> M;//素因数の集合vector<ll> v2, A(N);vector<mint> ans(N, 1);for (int i=0; i<N; i++){cin >> A[i];prime_factor(A[i]);for (auto [p, e] : prime) v2.push_back(p);}sort(v2.begin(), v2.end());v2.erase(unique(v2.begin(), v2.end()), v2.end());vector<vector<ll>> E(N);for (int i=0; i<M; i++){cin >> u >> v; u--; v--;E[u].push_back(v);E[v].push_back(u);}for (auto p : v2){vector<ll> B(N);for (int i=0; i<N; i++){ll cnt=0, X;X = A[i];while(X % p == 0){X /= p;cnt++;}B[i] = cnt;}vector<ll> dist = dijkstra(E, B);for (int i=0; i<N; i++){ans[i] *= mint(p).pow(dist[i]);}}for (int i=0; i<N; i++) cout << ans[i].val() << endl;return 0;}