結果

問題 No.3250 最小公倍数
ユーザー jiangxinyang
提出日時 2025-08-04 21:58:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,480 bytes
コンパイル時間 2,145 ms
コンパイル使用メモリ 208,340 KB
実行使用メモリ 42,700 KB
最終ジャッジ日時 2025-08-29 20:51:02
合計ジャッジ時間 7,371 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 11 RE * 10
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:43:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 |     freopen("lcm.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:44:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |     freopen("lcm.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:46:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
main.cpp:47:39: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   47 |     for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
      |                                  ~~~~~^~~~~~~~~~~~~~~
main.cpp:51:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   51 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353;
const int N = 500005;
const int INF = 0x3f3f3f3f;
ll a[N];
map<ll, ll> mp[N], m1, m2;
ll powmod(ll x, ll y, ll mod) {
    ll res = 1;
    while (y) {
        if (y & 1) res = res * x % mod;
        y >>= 1;
        x = x * x % mod;
    }
    return res;
}
void get_factor(ll n, map<ll, ll> &mp) {
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            while (n % i == 0) {
                n /= i;
                mp[i]++;
            }
        }
    }
    if (n != 1) mp[n]++;
}
vector<int> G[N];
int fa[N];
void dfs(int u) {
    for (auto v : G[u]) {
        if (v == fa[u]) continue;
        fa[v] = u;
        dfs(v);
        m1 = mp[u], m2 = mp[v];
        if ((int)m1.size() > (int)m2.size()) swap(m1, m2);
        for (auto [x, y] : m2) m1[x] = max(m1[x], y);
        mp[u] = m1;
    }
}
int main() {
    freopen("lcm.in", "r", stdin);
    freopen("lcm.out", "w", stdout);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (int i = 1; i <= n; i++) get_factor(a[i], mp[i]);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1);
    for (int i = 1; i <= n; i++) {
        ll res = 1;
        for (auto [x, y] : mp[i]) res = res * powmod(x, 1ll * y, mod) % mod;
        printf("%lld\n", res);
    }
    return 0;
}
0