結果

問題 No.368 LCM of K-products
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-06 08:28:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 2,175 bytes
コンパイル時間 808 ms
コンパイル使用メモリ 82,368 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 21:19:12
合計ジャッジ時間 2,136 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
4,348 KB
testcase_01 AC 35 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 13 ms
4,348 KB
testcase_04 AC 5 ms
4,348 KB
testcase_05 AC 121 ms
4,348 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 1 ms
4,348 KB
testcase_13 AC 9 ms
4,348 KB
testcase_14 AC 14 ms
4,348 KB
testcase_15 AC 15 ms
4,348 KB
testcase_16 AC 13 ms
4,348 KB
testcase_17 AC 12 ms
4,348 KB
testcase_18 AC 16 ms
4,348 KB
testcase_19 AC 5 ms
4,348 KB
testcase_20 AC 11 ms
4,348 KB
testcase_21 AC 4 ms
4,348 KB
testcase_22 AC 15 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 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 7 ms
4,348 KB
testcase_34 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef priority_queue<int, vector<int>, greater<int> > Queue;
typedef long long ll;

const ll kMOD = 1000 * 1000 * 1000 + 7;
const int kMAX_N = 1010;
const int kMAX_K = 1010;

int N, K;
ll a[kMAX_N];

ll GCD(ll a, ll b) {
    if (b == 0) return a;
    else return GCD(b, a % b);
}

ll LCM(ll a, ll b) {
    return a * b / GCD(a, b);
}

map<int, int> PrimeFactors(int n) {
    map<int, int> res;
    for (int i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            res[i]++;
            n /= i;
        }
    }
    if (n != 1) res[n] = 1;
    return res;
}

void Solve() {
    map<int, Queue> biggest_factors;    // 各素因数の最大の指数を記録する
    // 各素因数について, 素因数分解したときの最大の指数を貪欲にK個まで集める
    for (int i = 0; i < N; i++) {
        map<int, int> factors = PrimeFactors(a[i]);

        for (map<int, int>::iterator it = factors.begin(); it != factors.end(); it++) {
            if (biggest_factors.find(it->first) == biggest_factors.end()) {
                biggest_factors[it->first] = Queue();
            }
            if (biggest_factors[it->first].size() < K) {
                biggest_factors[it->first].push(it->second);
            } else if (biggest_factors[it->first].top() < it->second) {
                biggest_factors[it->first].pop();
                biggest_factors[it->first].push(it->second);
            }
        }
    }
    // 答えを計算
    ll answer = 1;
    for (map<int, Queue>::iterator it = biggest_factors.begin();
                                   it != biggest_factors.end(); it++) {
        while (!it->second.empty()) {
            int exponent = it->second.top();
            it->second.pop();
            for (int i = 0; i < exponent; i++) {
                answer *= it->first;
                answer %= kMOD;
            }
        }
    }
    cout << answer << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin >> N >> K;

    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }

    Solve();

    return 0;
}
0