結果

問題 No.1529 Constant Lcm
ユーザー H3PO4
提出日時 2023-03-14 10:27:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,399 ms / 3,000 ms
コード長 1,882 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 87,176 KB
最終ジャッジ日時 2025-02-11 11:04:31
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <unordered_map>

const int MOD = 998244353;

long long pow(long long x, long long n) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) {
            res = res * x % MOD;
        }
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}

std::vector<std::unordered_map<int, int>> generate_factor_table(int &N) {
    std::vector<std::unordered_map<int, int>> table(N + 1);
    for (int i = 2; i < N + 1; i++) {
        if (table.at(i).empty()) {
            // i is prime
            for (int j = i; j < N + 1; j += i) {
                int x = j;
                int ct = 0;
                while (x % i == 0) {
                    x /= i;
                    ct += 1;
                }
                table.at(j).emplace(i, ct);
            }
        }
    }
    return table;
}

int main() {
    int N;
    std::cin >> N;

    auto table = generate_factor_table(N);
    std::unordered_map<int, int> lcm_dict;

    for (int x = 1; x < N; x++) {
        int y = N - x;
        std::unordered_map<int, int> ct;
        for (const auto &[k, v]: table.at(x)) {
            if (ct.find(k) == ct.end()) {
                ct.emplace(k, v);
            } else {
                ct.at(k) += v;
            }
        }
        for (const auto &[k, v]: table.at(y)) {
            if (ct.find(k) == ct.end()) {
                ct.emplace(k, v);
            } else {
                ct.at(k) += v;
            }
        }

        for (const auto &[k, v]: ct) {
            if (lcm_dict.find(k) == lcm_dict.end()) {
                lcm_dict.emplace(k, v);
            } else {
                if (lcm_dict.at(k) < v) { lcm_dict.at(k) = v; }
            }
        }
    }
    long long ans = 1;
    for (const auto &[k, v]: lcm_dict) {
        ans *= pow(k, v);
        ans %= MOD;
    }
    std::cout << ans << std::endl;
}
0