結果

問題 No.2497 GCD of LCMs
ユーザー myaumyau
提出日時 2023-10-06 22:30:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,547 bytes
コンパイル時間 4,612 ms
コンパイル使用メモリ 278,500 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-06 22:30:38
合計ジャッジ時間 5,698 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

//only for atcoder
#include<atcoder/all>
using namespace atcoder;

#define rep(i,l,r) for(ll i=(l); i<(r); i++)
#define rrep(i,l,r) for(ll i=(r)-1; i>=(l); i--)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define REV(c) reverse(ALL(c))
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MINV(c) *min_element(ALL(c))
#define MAXV(c) *max_element(ALL(c))

template <typename TYPE>
void print(TYPE vec){
  rep(i,0,vec.size()){
    cout << vec[i];
    if(i == vec.size()-1){
      cout << endl;
    }
    else{
      cout << " ";
    }
  }
}

using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VVS = vector<VS>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VC = vector<char>;
using VVC = vector<VC>;
using VD = vector<ld>;
using VVD = vector<VD>;
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
const ll LINF = 2e18;
const int INF = 2e9;
using mint = modint998244353;

//素因数分解(関数ver)改
map<long long,long long> prime_fact(long long n){
  long long a = 2;
  map<long long,long long> mp;
  while(n >= a*a){
    if(n %a == 0){
      mp[a]++;
      n /= a;
    }
    else{
      a++;
    }
  }
  if(n != 1){
    mp[n]++;
  }
  return mp;
}

int main(){
  int N, M;
  cin >> N >> M;
  VI vec(N);
  rep(i,0,N){
    cin >> vec[i];
  }
  set<int> S;
  rep(i,0,N){
    map<ll,ll> mp = prime_fact(vec[i]);
    for(auto itr = mp.begin(); itr != mp.end(); itr++){
      S.insert(itr->first);
    }
  }
  
  VVI G(N,VI(0));
  rep(i,0,M){
    int a, b;
    cin >> a >> b;
    a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  vector<mint> ans(N,1);
  for(auto itr = S.begin(); itr != S.end(); itr++){
    int x = (*itr);
    VI cnt(N);
    rep(i,0,N){
      int y = vec[i];
      while(y % x == 0){
        cnt[i]++;
        y /= x;
      }
    }
    
    
    priority_queue<P, vector<P>, greater<P>> PQ;
    PQ.push(P(cnt[0], 0));
    VI dp(N,INF);
    dp[0] = cnt[0];
    
    while(PQ.size()){
      auto[c, p] = PQ.top();
      PQ.pop();
      for(auto e : G[p]){
        if(dp[e] > max(dp[p], cnt[e])){
          dp[e] = max(dp[p] , cnt[e]);
          PQ.push(P(dp[e], e));
        }
      }
    }
    
    mint t = x;
    rep(i,0,N){
      ans[i] *= t.pow(dp[i]);
    }
    //cout << x << endl;
    //print<VI>(dp);
  }
  
  rep(i,0,N){
    cout << ans[i].val() << endl;
  }
}
0