結果

問題 No.2497 GCD of LCMs
ユーザー SSRSSSRS
提出日時 2023-10-06 21:41:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 407 ms / 2,000 ms
コード長 1,864 bytes
コンパイル時間 2,567 ms
コンパイル使用メモリ 218,976 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-10-06 21:41:29
合計ジャッジ時間 5,083 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,500 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 19 ms
4,376 KB
testcase_08 AC 141 ms
4,376 KB
testcase_09 AC 166 ms
4,376 KB
testcase_10 AC 187 ms
4,376 KB
testcase_11 AC 78 ms
4,380 KB
testcase_12 AC 300 ms
4,380 KB
testcase_13 AC 348 ms
4,380 KB
testcase_14 AC 19 ms
4,376 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 407 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<vector<int>> E(N);
  for (int i = 0; i < M; i++){
    int U, V;
    cin >> U >> V;
    U--;
    V--;
    E[U].push_back(V);
    E[V].push_back(U);
  }
  vector<vector<pair<int, int>>> F(N);
  for (int i = 0; i < N; i++){
    for (int j = 2; j * j <= A[i]; j++){
      if (A[i] % j == 0){
        int cnt = 0;
        while (A[i] % j == 0){
          A[i] /= j;
          cnt++;
        }
        F[i].push_back(make_pair(j, cnt));
      }
    }
    if (A[i] > 1){
      F[i].push_back(make_pair(A[i], 1));
    }
  }
  vector<int> P;
  for (int i = 0; i < N; i++){
    for (pair<int, int> pe : F[i]){
      P.push_back(pe.first);
    }
  }
  sort(P.begin(), P.end());
  P.erase(unique(P.begin(), P.end()), P.end());
  int cnt = P.size();
  vector<vector<int>> e(N, vector<int>(cnt, 0));
  for (int i = 0; i < N; i++){
    for (pair<int, int> pe : F[i]){
      int idx = lower_bound(P.begin(), P.end(), pe.first) - P.begin();
      e[i][idx] = pe.second;
    }
  }
  vector<long long> ans(N, 1);
  for (int i = 0; i < cnt; i++){
    vector<int> d(N, -1);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push(make_pair(e[0][i], 0));
    while (!pq.empty()){
      int c = pq.top().first;
      int v = pq.top().second;
      pq.pop();
      if (d[v] == -1){
        d[v] = c;
        for (int w : E[v]){
          if (d[w] == -1){
            pq.push(make_pair(max(c, e[w][i]), w));
          }
        }
      }
    }
    for (int j = 0; j < N; j++){
      for (int k = 0; k < d[j]; k++){
        ans[j] *= P[i];
        ans[j] %= MOD;
      }
    }
  }
  for (int i = 0; i < N; i++){
    cout << ans[i] << endl;
  }
}
0