結果

問題 No.2497 GCD of LCMs
ユーザー nono00nono00
提出日時 2023-10-06 23:14:21
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 4,786 bytes
コンパイル時間 3,427 ms
コンパイル使用メモリ 267,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-06 23:14:26
合計ジャッジ時間 4,767 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#include <iostream>

namespace nono {

namespace internal {

constexpr bool is_prime(unsigned long long n) {
    for (unsigned long long i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

}  //  namespace internal

template <unsigned long long MOD = 998244353>
class Modint {
  public:
    constexpr Modint(unsigned long long value = 0): value_(value % MOD) {}

    constexpr Modint pow(long long exp) const {
        Modint result(1);
        Modint base(*this);
        while (exp > 0) {
            if (exp & 1) {
                result *= base;
            }
            base *= base;
            exp >>= 1;
        }
        return result;
    }

    constexpr Modint inv() const {
        return pow(MOD - 2);
    }

    void set(unsigned long long value) {
        if (value >= MOD) value %= MOD;
        value_ = value;
    }

    unsigned long long get() const {
        return value_;
    }

    constexpr Modint& operator+=(const Modint other) {
        this->value_ += other.value_;
        if (this->value_ >= MOD) this->value_ -= MOD;
        return *this;
    }

    constexpr Modint& operator-=(const Modint other) {
        this->value_ += MOD - other.value_;
        if (this->value_ >= MOD) this->value_ -= MOD;
        return *this;
    }

    constexpr Modint& operator*=(const Modint other) {
        this->value_ *= other.value_;
        if (this->value_ >= MOD) this->value_ %= MOD;
        return *this;
    }

    constexpr Modint& operator/=(const Modint other) {
        this->value_ *= other.inv();
        if (this->value_ >= MOD) this->value_ %= MOD;
        return *this;
    }

    constexpr friend Modint operator+(const Modint lhs, const Modint rhs) {
        return Modint(lhs) += rhs;
    }
    constexpr friend Modint operator-(const Modint lhs, const Modint rhs) {
        return Modint(lhs) -= rhs;
    }
    constexpr friend Modint operator*(const Modint lhs, const Modint rhs) {
        return Modint(lhs) *= rhs;
    }
    constexpr friend Modint operator/(const Modint lhs, const Modint rhs) {
        return Modint(lhs) /= rhs;
    }

    friend std::istream& operator>>(std::istream& stream, Modint& mint) {
        unsigned long long value;
        stream >> value;
        mint.set(value);
        return stream;
    }

    friend std::ostream& operator<<(std::ostream& stream, Modint mint) {
        stream << mint.get();
        return stream;
    }

  private:
    unsigned long long value_;
};

}  //  namespace nono

namespace nono {

std::vector<std::pair<int, int>> factorize(long long n) {
    std::vector<std::pair<int, int>> result;
    for (long long i = 2; i * i <= n; i++) {
        int count = 0;
        while (n % i == 0) {
            count++;
            n /= i;
        }
        if (count > 0) {
            result.emplace_back(i, count);
        }
    }
    if (n != 1) {
        result.emplace_back(n, 1);
    }
    return result;
}

void solve() {
    using Mint = Modint<998244353>;
    int n, m;
    std::cin >> n >> m;
    std::vector<long long> a(n);
    std::set<int> s;
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        for (auto [k, v]: factorize(a[i])) {
            s.insert(k);
        }
    }
    std::vector graph(n, std::vector<int>());
    for (int i = 0; i < m; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    std::vector<Mint> ans(n, 1);
    for (auto k: s) {
        std::vector<int> max(n, 1e9);
        using P = std::pair<int, int>;
        std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
        max[0] = 0;
        {
            long long temp = a[0];
            while (temp % k == 0) {
                max[0]++;
                temp /= k;
            }
        }
        pq.emplace(0, max[0]);
        while (!pq.empty()) {
            auto [u, c] = pq.top();
            pq.pop();
            if (max[u] != c) continue;
            for (auto w: graph[u]) {
                int g = 0;
                long long temp = a[w];
                while (temp % k == 0) {
                    g++;
                    temp /= k;
                }
                g = std::max(max[u], g);
                if (max[w] > g) {
                    max[w] = g;
                    pq.emplace(w, g);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            ans[i] *= Mint(k).pow(max[i]);
        }
    }
    for (int i = 0; i < n; i++) {
        std::cout << ans[i] << std::endl;
    }
}

}  //  namespace nono

int main() {
    std::cin.tie(0)->sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);

    int t = 1;

    while (t--) nono::solve();
}
0