結果

問題 No.2497 GCD of LCMs
ユーザー KudeKude
提出日時 2023-10-06 23:56:32
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 2,172 bytes
コンパイル時間 3,624 ms
コンパイル使用メモリ 283,732 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-06 23:56:37
合計ジャッジ時間 4,615 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 11 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 12 ms
4,376 KB
testcase_13 AC 13 ms
4,384 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 14 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

template<class T>
vector<pair<T, int>> factorize(T x) {
  vector<pair<T, int>> res;
  for(T p = 2; p * p <= x; p++) {
    int cnt = 0;
    while(x % p == 0) x /= p, cnt++;
    if (cnt) res.emplace_back(p, cnt);
  }
  if (x > 1) res.emplace_back(x, 1);
  return res;
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  VI a(n);
  rep(i, n) cin >> a[i];
  VVI to(n);
  rep(_, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
  }
  VI ps;
  rep(i, n) {
    for (auto [p, _] : factorize(a[i])) {
      ps.emplace_back(p);
    }
  }
  sort(all(ps));
  ps.erase(unique(all(ps)), ps.end());
  vector<mint> ans(n, 1);
  VI c(n);
  constexpr int INF = 1001001001;
  VI vs[30];
  for (int p : ps) {
    rep(i, n) {
      int cnt = 0;
      int now = a[i];
      while(now % p == 0) now /= p, cnt++;
      c[i] = cnt;
    }
    VI dist(n, INF);
    dist[0] = c[0];
    vs[c[0]].emplace_back(0);
    mint pd = 1;
    rep(d, 30) {
      while(vs[d].size()) {
        int u = vs[d].back(); vs[d].pop_back();
        if (dist[u] != d) continue;
        ans[u] *= pd;
        for (int v : to[u]) {
          int nd = max(d, c[v]);
          if (nd < dist[v]) {
            dist[v] = nd;
            vs[nd].emplace_back(v);
          }
        }
      }
      pd *= p;
    }
  }
  for (mint x : ans) cout << x.val() << '\n';
}
0