結果

問題 No.2497 GCD of LCMs
ユーザー tnakao0123
提出日時 2024-06-21 09:28:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,610 bytes
コンパイル時間 1,118 ms
コンパイル使用メモリ 72,452 KB
最終ジャッジ日時 2025-02-21 23:39:58
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:88:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   88 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:89:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   89 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~
main.cpp:93:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   93 |     scanf("%d%d", &u, &v);
      |     ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2497.cc:  No.2497 GCD of LCMs - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 250;
const int MOD = 998244353;
const int MAX_P = 32000;
const int INF = 1 << 30;

/* typedef */

using ll = long long;
using vb = vector<bool>;
using vi = vector<int>;
using pii = pair<int,int>;
using vpii = vector<pii>;

/* global variables */

vb primes;
vi pnums;
int as[MAX_N], ups[MAX_N * 15];
vi nbrs[MAX_N];
int es[MAX_N], ds[MAX_N], gs[MAX_N];

/* subroutines */

int gen_primes(int maxp) {
  primes.assign(maxp + 1, true);
  primes[0] = primes[1] = false;

  int p;
  for (p = 2; p * p <= maxp; p++)
    if (primes[p]) {
      pnums.push_back(p);
      for (int q = p * p; q <= maxp; q += p) primes[q] = false;
    }
  for (; p <= maxp; p++)
    if (primes[p]) pnums.push_back(p);
  return (int)pnums.size();
}

bool prime_decomp(int n, vpii& pds) {
  pds.clear();

  for (auto pi: pnums) {
    if (pi * pi > n) {
      if (n > 1) pds.push_back(pii(n, 1));
      return true;
    }

    if (n % pi == 0) {
      int fi = 0;
      while (n % pi == 0) n /= pi, fi++;
      pds.push_back(pii(pi, fi));
    }
  }
  return false;
}

int powmod(int a, int b) {
  int p = 1;
  while (b > 0) {
    if (b & 1) p = (ll)p * a % MOD;
    a = (ll)a * a % MOD;
    b >>= 1;
  }
  return p;
}

/* main */

int main() {
  gen_primes(MAX_P);
  
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    nbrs[u].push_back(v);
    nbrs[v].push_back(u);
  }

  int k = 0;
  for (int i = 0; i < n; i++) {
    vpii pds;
    prime_decomp(as[i], pds);
    for (auto [p, d]: pds) ups[k++] = p;
  }
  sort(ups, ups + k);
  k = unique(ups, ups + k) - ups;
  //printf(" ups=%d\n", k);

  fill(gs, gs + n, 1);
  
  for (int i = 0; i < k; i++) {
    int p = ups[i];

    for (int i = 0; i < n; i++) {
      es[i] = 0;
      for (int ai = as[i]; ai % p == 0; ai /= p) es[i]++;
    }

    fill(ds, ds + n, INF);
    ds[0] = es[0];
    priority_queue<pii> q;
    q.push({-es[0], 0});

    while (! q.empty()) {
      auto [ud, u] = q.top(); q.pop();
      ud = -ud;
      if (ds[u] != ud) continue;

      for (auto v: nbrs[u]) {
	int vd = max(ud, es[v]);
	if (ds[v] > vd) {
	  ds[v] = vd;
	  q.push({-vd, v});
	}
      }
    }

    for (int i = 0; i < n; i++)
      gs[i] = (ll)gs[i] * powmod(p, ds[i]) % MOD;
  }

  for (int i = 0; i < n; i++) printf("%d\n", gs[i]);
  
  return 0;
}

0