結果

問題 No.368 LCM of K-products
ユーザー PachicobuePachicobue
提出日時 2017-12-31 22:25:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 1,782 bytes
コンパイル時間 3,280 ms
コンパイル使用メモリ 217,240 KB
実行使用メモリ 17,264 KB
最終ジャッジ日時 2023-10-23 21:25:16
合計ジャッジ時間 6,462 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
5,640 KB
testcase_01 AC 142 ms
17,264 KB
testcase_02 AC 150 ms
17,264 KB
testcase_03 AC 154 ms
17,264 KB
testcase_04 AC 156 ms
17,264 KB
testcase_05 AC 154 ms
17,264 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 71 ms
7,348 KB
testcase_14 AC 151 ms
10,400 KB
testcase_15 AC 162 ms
17,264 KB
testcase_16 AC 106 ms
7,356 KB
testcase_17 AC 70 ms
5,636 KB
testcase_18 AC 173 ms
10,764 KB
testcase_19 AC 28 ms
5,120 KB
testcase_20 AC 79 ms
7,352 KB
testcase_21 AC 18 ms
4,776 KB
testcase_22 AC 163 ms
10,764 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 4 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 4 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 4 ms
4,348 KB
testcase_33 AC 28 ms
4,348 KB
testcase_34 AC 166 ms
10,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
ll power(const ll p, const int n)
{
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return (power(p, n - 1) * p) % MOD;
    } else {
        const ll pp = power(p, n / 2);
        return (pp * pp) % MOD;
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    constexpr ll SQRT = 31623;
    vector<bool> isprime(SQRT, true);
    vector<ll> prime;
    for (ll i = 2; i < SQRT; i++) {
        if (isprime[i]) {
            for (ll j = 2; i * j < SQRT; j++) {
                isprime[i * j] = false;
            }
        }
    }
    for (ll i = 2; i < SQRT; i++) {
        if (isprime[i]) {
            prime.push_back(i);
        }
    }
    const int size = prime.size();
    vector<priority_queue<int, vector<int>, greater<int>>> ind(size);
    map<ll, int> rest;
    for (int i = 0; i < N; i++) {
        ll a;
        cin >> a;
        for (int j = 0; j < size; j++) {
            const ll p = prime[j];
            int cnt = 0;
            while (a % p == 0) {
                a /= p;
                cnt++;
            }
            ind[j].push(cnt);
            if (ind[j].size() == K + 1) {
                ind[j].pop();
            }
        }
        if (a > 1) {
            rest[a]++;
        }
    }

    ll ans = 1;
    for (int i = 0; i < size; i++) {
        int sum = 0;
        while (not ind[i].empty()) {
            sum += ind[i].top();
            ind[i].pop();
        }
        ans *= power(prime[i], sum);
        ans %= MOD;
    }
    for (const auto& e : rest) {
        ans *= power(e.first, min(K, e.second));
        ans %= MOD;
    }
    cout << ans << endl;

    return 0;
}

0