結果

問題 No.2313 Product of Subsequence (hard)
ユーザー navel_tosnavel_tos
提出日時 2023-05-25 01:47:13
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,291 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 227,328 KB
最終ジャッジ日時 2023-08-25 15:47:12
合計ジャッジ時間 12,542 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
227,328 KB
testcase_01 AC 54 ms
71,468 KB
testcase_02 AC 62 ms
76,104 KB
testcase_03 AC 99 ms
78,640 KB
testcase_04 AC 105 ms
78,256 KB
testcase_05 AC 139 ms
78,240 KB
testcase_06 AC 98 ms
78,456 KB
testcase_07 AC 136 ms
78,588 KB
testcase_08 AC 2,400 ms
133,964 KB
testcase_09 AC 282 ms
110,332 KB
testcase_10 AC 1,150 ms
134,596 KB
testcase_11 AC 358 ms
95,684 KB
testcase_12 AC 816 ms
133,048 KB
testcase_13 TLE -
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となる場合の数
#...だとTLEしたので、DP[S]とnDP[S]の2つを交互に持ち替える方式で再実装
DP=[1]+[0]*(Max-1)
for x,i in enumerate(range(N),start=1):
    nDP=DP[::1]  #A[i]を使わない遷移
    for S in range(Max):  #A[i]を使う遷移
        T=HtoT[S]; U=hash([min(E[y],T[y]+Aexp[i][y]) for y in range(len(T))])
        nDP[U]+=DP[S]; nDP[U]%=MOD
    DP=nDP
print(DP[-1])
0