結果

問題 No.368 LCM of K-products
ユーザー ふーらくたる
提出日時 2016-07-06 02:04:57
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,113 bytes
コンパイル時間 648 ms
コンパイル使用メモリ 67,964 KB
実行使用メモリ 357,828 KB
最終ジャッジ日時 2024-10-12 20:25:00
合計ジャッジ時間 7,084 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other TLE * 1 -- * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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<ll> dp[kMAX_N][kMAX_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);
}

void Solve() {
    dp[0][0].insert(1);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= K; j++) {
            if (dp[i][j].empty()) continue;
            for (set<ll>::iterator it = dp[i][j].begin(); it != dp[i][j].end(); it++) {
                dp[i + 1][j].insert(*it);
                if (j + 1 > K) continue;
                dp[i + 1][j + 1].insert((*it * a[i]) % kMOD);
            }
        }
    }
    ll answer = 1;
    for (set<ll>::iterator it = dp[N][K].begin(); it != dp[N][K].end(); it++) {
        answer = LCM(answer, *it);
        answer %= kMOD;
    }
    cout << answer << endl;
}

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

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

    Solve();

    return 0;
}
0