結果

問題 No.2313 Product of Subsequence (hard)
ユーザー navel_tosnavel_tos
提出日時 2023-05-25 01:41:29
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,202 bytes
コンパイル時間 1,339 ms
コンパイル使用メモリ 86,720 KB
実行使用メモリ 837,708 KB
最終ジャッジ日時 2023-08-25 15:44:57
合計ジャッジ時間 10,573 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
71,284 KB
testcase_01 AC 53 ms
71,156 KB
testcase_02 AC 61 ms
75,968 KB
testcase_03 AC 108 ms
79,076 KB
testcase_04 AC 106 ms
78,616 KB
testcase_05 AC 129 ms
79,484 KB
testcase_06 AC 98 ms
79,240 KB
testcase_07 AC 107 ms
78,524 KB
testcase_08 AC 2,507 ms
205,528 KB
testcase_09 AC 318 ms
120,592 KB
testcase_10 AC 1,175 ms
173,824 KB
testcase_11 AC 373 ms
105,364 KB
testcase_12 AC 895 ms
169,240 KB
testcase_13 MLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder 2313 Product of Subsequence(Hard)

'''
制約: R<10^9 だが、この制約下での約数数は高々1344個。
 735134400=2^6*3^3*5^2*7^1*11^1*13^1*17^1
 931170240=2^6*3^2*5^1*7^1*11^1*13^1*17^1*19^1

なのでDPで解けそう。
ただ ABC300E Dice Product 3 と異なり、元となる素因数が事前に与えられないため、
DP[i][j][k]: 2^i * 3^j * 5^k を取る値の確率/場合の数
のようなDPは組めない。きっと約数列挙してdict管理がよいだろう。

遷移が難しいな。状態をハッシュで管理して、定期的に呼び出す感じかな。
意外と重実装。
'''
#素因数分解し、(素因数,次数)の順に格納したリストを返す
def Soinsu(CheckNumber):
    SoinsuList=[]
    for Soinsu in range(2,CheckNumber):
        if Soinsu*Soinsu>CheckNumber:break
        if CheckNumber%Soinsu!=0:continue
        SoinsuCount=0
        while CheckNumber%Soinsu==0:SoinsuCount+=1;CheckNumber//=Soinsu
        SoinsuList.append((Soinsu,SoinsuCount))
    if CheckNumber!=1:SoinsuList.append((CheckNumber,1))
    return SoinsuList


f=lambda:list(map(int,input().split()))

#入力受取り Kを素因数分解し、Kの素因数でAを割る
N,K=f(); A=f(); P=Soinsu(K); Aexp=[]; MOD=998244353
for num in A:
    fact=[0]*len(P)
    for pos,(prime,exp) in enumerate(P):
        while num%prime==0 and exp>0: num//=prime; fact[pos]+=1; exp-=1
    Aexp.append(fact)

#手動で冪乗数リストからハッシュに変換する関数を定義
base=[1]; E=[P[i][1] for i in range(len(P))]
for exp in E[:-1]: base.append(base[-1]*(exp+1))
hash=lambda T: sum(base[i]*T[i] for i in range(len(T)))
rev =lambda H: tuple([H%base[i]//base[i-1] for i in range(1,len(base))]+[H//base[-1]])
Max=base[-1]*(E[-1]+1); HtoT={i:rev(i) for i in range(Max)}

#DP[x][S]: A[i:x-1]まで考慮したとき、約数のハッシュ値がSとなる場合の数
DP=[[0]*Max for x in range(N+1)]; DP[0][0]=1
for x,i in enumerate(range(N),start=1):
    for S in range(Max):
        DP[x][S]+=DP[x-1][S]; DP[x][S]%=MOD
        T=HtoT[S]; U=hash([min(E[y],T[y]+Aexp[i][y]) for y in range(len(T))])
        DP[x][U]+=DP[x-1][S]; DP[x][U]%=MOD
print(DP[-1][-1])
0