結果

問題 No.2313 Product of Subsequence (hard)
ユーザー karinohitokarinohito
提出日時 2023-05-24 22:37:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 408 ms / 4,000 ms
コード長 1,789 bytes
コンパイル時間 4,923 ms
コンパイル使用メモリ 281,964 KB
実行使用メモリ 31,712 KB
最終ジャッジ日時 2023-08-25 14:18:55
合計ジャッジ時間 11,717 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 184 ms
8,572 KB
testcase_09 AC 103 ms
6,232 KB
testcase_10 AC 213 ms
9,544 KB
testcase_11 AC 81 ms
5,380 KB
testcase_12 AC 221 ms
9,988 KB
testcase_13 AC 398 ms
31,476 KB
testcase_14 AC 401 ms
31,712 KB
testcase_15 AC 408 ms
31,524 KB
testcase_16 AC 391 ms
31,600 KB
testcase_17 AC 381 ms
29,592 KB
testcase_18 AC 382 ms
29,756 KB
testcase_19 AC 383 ms
29,480 KB
testcase_20 AC 384 ms
29,484 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 190 ms
9,616 KB
testcase_26 AC 115 ms
7,040 KB
testcase_27 AC 267 ms
31,540 KB
testcase_28 AC 215 ms
10,280 KB
testcase_29 AC 226 ms
10,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using mint = modint998244353;
using vm = vector<mint>;
using vvm = vector<vm>;
vector<mint> fact, factinv, inv;
const ll mod = 998244353;

void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact[0] = fact[1] = factinv[0] = factinv[1] = inv[1] = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact[i] = (fact[i - 1] * i);
        inv[i] = (mod - ((inv[mod % i] * (mod / i))));
        factinv[i] = (factinv[i - 1] * inv[i]);
    }
}
mint nCk(ll n, ll k) {
    if (n < k || k < 0) return 0;
    return (fact[n] * ((factinv[k] * factinv[n - k])));
}

int main() {
    ll N,K,k=0;
    cin>>N>>K;
    vll A(N);
    rep(i,N){
        cin>>A[i];
        A[i]=gcd(A[i],K);
    }
    map<ll,ll> M;
    for(ll d=1;d*d<=K;d++)if(K%d==0)M[d]=M[K/d]=1;
    for(auto m:M)M[m.first]=k,k++;
    vll D(k,0),KP(k);
    for(auto m:M)KP[m.second]=m.first;
    rep(i,N)D[M[A[i]]]++;
    vvm DP(k+1,vm(k,0));
    prenCkModp(N+1);
    DP[0][0]=1;
    unordered_map<ll,ll> MM;
    for(auto m:M)MM[m.first]=m.second;
    vvll nx(k,vll(k));
    rep(i,k)rep(j,k)nx[i][j]=MM[gcd(K,KP[i]*KP[j])];
    vector<mint> p2(N+1,1);
    rep(i,N)p2[i+1]=p2[i]*2;
    rep(i,k)rep(j,k){
        if(DP[i][j].val()==0)continue;
        ll r=j;
        mint us=p2[D[i]];
        rep(d,D[i]+1){
            mint multi=nCk(D[i],d);
            us-=multi;
            DP[i+1][r]+=DP[i][j]*multi;
            if(r==nx[r][i])break;
            r=nx[r][i];
        }
        DP[i+1][r]+=DP[i][j]*us;
    }
    if(K==1)DP[k][k-1]-=1;
    cout<<DP[k][k-1].val()<<endl;
}
0