結果

問題 No.368 LCM of K-products
ユーザー xuzijian629xuzijian629
提出日時 2018-10-31 21:35:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 708 ms / 2,000 ms
コード長 2,137 bytes
コンパイル時間 2,568 ms
コンパイル使用メモリ 222,612 KB
実行使用メモリ 133,100 KB
最終ジャッジ日時 2023-10-23 21:28:37
合計ジャッジ時間 8,172 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 260 ms
44,200 KB
testcase_01 AC 708 ms
133,100 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 196 ms
31,884 KB
testcase_04 AC 328 ms
47,972 KB
testcase_05 AC 268 ms
66,372 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 106 ms
25,732 KB
testcase_14 AC 341 ms
53,116 KB
testcase_15 AC 457 ms
63,560 KB
testcase_16 AC 307 ms
49,776 KB
testcase_17 AC 198 ms
37,624 KB
testcase_18 AC 511 ms
66,620 KB
testcase_19 AC 23 ms
8,580 KB
testcase_20 AC 157 ms
32,772 KB
testcase_21 AC 11 ms
5,740 KB
testcase_22 AC 436 ms
61,236 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 45 ms
14,372 KB
testcase_34 AC 40 ms
14,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr i64 MOD = 1e9 + 7;

i64 modpow(i64 a, i64 n, i64 mod) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        i64 t = modpow(a, n / 2, mod);
        return t * t % mod;
    }
    return a * modpow(a, n - 1, mod) % mod;
}

bool is_prime(i64 n, int k = 10) {
    if (n == 2) return true;
    if (n < 2 || n % 2 == 0) return false;
    i64 d = n - 1;
    while (d % 2 == 0) {
        d /= 2;
    }
    for (int i = 0; i < k; i++) {
        i64 a = rnd() % (n - 2) + 1;
        i64 t = d;
        i64 y = modpow(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = modpow(y, 2, n);
            t *= 2;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    int n, k;
    cin >> n >> k;
    vi as(n);
    for (int i = 0; i < n; i++) {
        cin >> as[i];
    }

    map<i64, int> cnt[n];
    set<i64> ps;
    for (int i = 0; i < n; i++) {
        i64 a = as[i];
        if (is_prime(a)) {
            cnt[i][a]++;
            ps.insert(a);
        } else {
            while (a > 1) {
                if (is_prime(a)) {
                    cnt[i][a]++;
                    ps.insert(a);
                    break;
                }
                for (int j = 2; j * j <= a; j++) {
                    int change = 0;
                    while (a % j == 0) {
                        change = 1;
                        cnt[i][j]++;
                        ps.insert(j);
                        a /= j;
                    }              
                    if (change) break;  
                }
            }
        }
    }

    i64 ans = 1;
    for (i64 p: ps) {
        vi vs;
        for (int i = 0; i < n; i++) {
            vs.push_back(cnt[i][p]);
        }
        sort(vs.begin(), vs.end(), greater<>());
        ans *= modpow(p, accumulate(vs.begin(), vs.begin() + k, 0), MOD);
        ans %= MOD;
    }

    cout << ans << endl;
}
0