結果

問題 No.2497 GCD of LCMs
ユーザー KumaTachiRen
提出日時 2023-09-03 12:07:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,562 bytes
コンパイル時間 4,556 ms
コンパイル使用メモリ 261,248 KB
最終ジャッジ日時 2025-02-16 18:22:29
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

struct Fast {
  Fast() {
    std::cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(10);
  }
} fast;

#define rep(i, a, b) for (int(i) = (a); (i) < (int)(b); (i)++)

using ll = long long;
using mint = modint998244353;

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];
  vector<vector<int>> g(n, vector<int>());
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  set<ll> primes;
  rep(i, 0, n) {
    ll x = a[i];
    for (ll d = 2; d * d <= x; d += (d & 1) + 1) {
      if (x % d) continue;
      primes.insert(d);
      while (!(x % d)) x /= d;
    }
    if (x > 1) primes.insert(x);
  }

  ll amax = 0;
  rep(i, 0, n) amax = max(amax, a[i]);

  vector<mint> ans(n, 1);
  for (auto p : primes) {
    vector<bool> seen(n, false);
    for (ll pow = 1; pow <= amax; pow *= p) {
      if (a[0] % (pow * p) == 0) continue;
      vector<bool> reachable(n, false);
      stack<int> st;
      st.push(0);
      while (!st.empty()) {
        auto x = st.top();
        st.pop();
        reachable[x] = true;
        for (auto y : g[x]) {
          if (a[y] % (pow * p) && !reachable[y]) {
            reachable[y] = true;
            st.push(y);
          }
        }
      }
      rep(i, 0, n) if (reachable[i] && !seen[i]) {
        ans[i] *= pow;
        seen[i] = true;
      }
    }
  }

  rep(i, 0, n) cout << ans[i].val() << "\n";
}
0