結果

問題 No.368 LCM of K-products
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-06 01:56:41
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,936 bytes
コンパイル時間 825 ms
コンパイル使用メモリ 79,216 KB
実行使用メモリ 1,359,460 KB
最終ジャッジ日時 2024-04-20 22:07:12
合計ジャッジ時間 7,356 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <set>
#include <map>
using namespace std;

typedef map<int, int> Number;
typedef long long ll;

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

int N, K;

set<Number> dp[kMAX_N][kMAX_K];

int a[kMAX_N];

Number PrimeFactor(int n) {
    Number 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() {
    Number n;
    n[1] = 1;

    dp[0][0].insert(n);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= K; j++) {
            if (dp[i][j].empty()) continue;

            for (set<Number>::iterator it = dp[i][j].begin(); it != dp[i][j].end(); it++) {
                Number new_num = *it, a_factors = PrimeFactor(a[i]);
                // かけないで使う
                dp[i + 1][j].insert(*it);
                if (j + 1 > K) continue;
                // かけて使う
                for (Number::iterator n_it = a_factors.begin(); n_it != a_factors.end(); n_it++) {
                    new_num[n_it->first] += n_it->second;
                }
                dp[i + 1][j + 1].insert(new_num);
            }
        }
    }
    Number result;
    for (set<Number>::iterator it = dp[N][K].begin(); it != dp[N][K].end(); it++) {
        Number num = *it;
        for (Number::iterator n_it = num.begin(); n_it != num.end(); n_it++) {
            result[n_it->first] = max(result[n_it->first], n_it->second);
        }
    }
    ll answer = 1;
    for (Number::iterator n_it = result.begin(); n_it != result.end(); n_it++) {
        for (int i = 0; i < n_it->second; i++) {
            answer *= n_it->first;
            answer %= kMOD;
        }
    }
    cout << answer << endl;
}

int main() {
    cin >> N >> K;

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

    Solve();

    return 0;
}
0