結果

問題 No.3717 GCD LCM GCD
コンテスト
ユーザー ponjuice
提出日時 2026-09-18 22:07:13
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 604 ms / 2,000 ms
+ 724µs
コード長 4,150 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,917 ms
コンパイル使用メモリ 361,556 KB
実行使用メモリ 9,776 KB
最終ジャッジ日時 2026-09-18 22:07:31
合計ジャッジ時間 4,521 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

//高速化 
struct ponjuice{ponjuice(){cin.tie(0);ios::sync_with_stdio(0);cout<<fixed<<setprecision(20);}}PonJuice;
#define endl '\n' //インタラクティブ問題の時は消す

//型
using ll = long long;
using ld = long double;

// for文
#define overload4(a, b, c, d, e, ...) e
#define rep1(n)             for(ll i = 0; i < n; i++)
#define rep2(i, n)          for(ll i = 0; i < n; i++)
#define rep3(i, a, b)       for(ll i = a; i < b; i++)
#define rep4(i, a, b, step) for(ll i = a; i < b; i+= step)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define per1(n)             for(ll i = n-1; i >= 0; i--)
#define per2(i, n)          for(ll i = n-1; i >= 0; i--)
#define per3(i, a, b)       for(ll i = b-1; i >= a; i--)
#define per4(i, a, b, step) for(ll i = b-1; i >= a; i-= step)
#define per(...) overload4(__VA_ARGS__, per4, per3, per2, per1)(__VA_ARGS__)

//関数
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class S, class T>inline bool chmax(S& a, T b){return a < b && ( a = b , true);}
template<class S, class T>inline bool chmin(S& a, T b){return a > b && ( a = b , true);}

//定数
constexpr ll mod = 998244353;
constexpr ll minf=-(1<<29);
constexpr ll inf=(1<<29);
constexpr ll MINF=-(1LL<<60);
constexpr ll INF=(1LL<<60);
const int dx[4] ={-1, 0, 1, 0};
const int dy[4] ={ 0, 1, 0,-1};
const int dx8[8] ={-1,-1,-1, 0, 1, 1, 1, 0};
const int dy8[8] ={-1, 0, 1, 1, 1, 0,-1,-1};


ll greedy(ll n, ll k, vector<ll> a) {
    vector<int> iot(n);
    iota(all(iot), 0);
    sort(all(iot));
    ll gcds = 0;
    do {
        ll lcms = 1;
        rep(i,0,n-k+1) {
            ll gd = 0;
            rep(j,0,k) {
                gd = gcd(gd, a[iot[i+j]]);
            }
            lcms = lcm(lcms, gd);
        }
        gcds = gcd(gcds, lcms);
    }while(next_permutation(all(iot)));
    return gcds % mod;
}

ll solve(ll n, ll k, vector<ll> a);

void test() {
    ll n = 5;
    int itr = 0;
    while(true) {
        itr++;
        if(itr % 100000 == 0) cout << itr << endl << flush;
        ll k = rand()%5+1;
        vector<ll> a(n);
        rep(i,0,n) a[i] = rand()%100;
        if(greedy(n,k,a) != solve(n,k,a)) {
            cout << n << " " << k << endl;
            for(auto x: a) cout << x << " "; cout << endl;
            cout << greedy(n,k,a) << " " << solve(n,k,a) << endl;
            return;
        }
    }
}

int main() {
    // test();
	int t = 1;
    // cin >> t;
    while(t--) {

        ll n,k;
        cin >> n >> k;
        vector<ll> a(n);
        rep(i,0,n) cin >> a[i];
        ll ans = solve(n,k,a);
        cout << ans << endl;
        // ans = greedy(n,k,a);
        // cout << ans << endl;
    }
}


vector<pair<ll,ll>> factorize(ll x){
    vector<pair<ll,ll>> res;
    for(ll i = 2; i*i <= x; i++){
        if(x % i == 0){
            if(res.size() && res.back().first == i){
                res.back().second++;
            }else{
                res.emplace_back(i,1);
            }
            x/=i;
            i--;
        }
    }
    if(x != 1){
        if(res.size() && res.back().first == x){
            res.back().second++;
        }else{
            res.emplace_back(x,1);
        }
    }
    return res;
}

ll powll(ll x, ll n) {
    ll res = 1;
    while(n > 0) {
        if(n & 1) {
            res = res * x % mod;
        }
        x = x*x % mod;
        n >>= 1;
    }
    return res;
}

ll solve(ll n, ll k, vector<ll> a){

    // 各素因数について そうでないものの の個数が n/k 未満 -> n/k 個目の大きさを見る
    map<int,int> ind;
    vector<vector<int>> vs;

    rep(i,0,n) {
        auto res = factorize(a[i]);
        for(auto [v, k]: res) {
            if(ind.count(v) == 0) {
                ind[v] = vs.size();
                vs.push_back({});
            }
            vs[ind[v]].push_back(k);
        }
    }

    rep(i,0,vs.size()) sort(rall(vs[i]));
    int x = n/k;

    ll ans = 1;
    for(auto [v, i]: ind) {
        if(vs[i].size() <= n-x) continue;
        ans = ans * powll(v, vs[i][n-x]) % mod;
    }

    return ans;
}
0