結果

問題 No.1731 Product of Subsequence
ユーザー 👑 KazunKazun
提出日時 2021-11-05 22:15:58
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,699 ms / 2,000 ms
コード長 2,530 bytes
コンパイル時間 1,130 ms
コンパイル使用メモリ 86,828 KB
実行使用メモリ 78,752 KB
最終ジャッジ日時 2023-08-07 23:12:59
合計ジャッジ時間 23,634 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,670 ms
78,068 KB
testcase_01 AC 70 ms
70,816 KB
testcase_02 AC 72 ms
70,964 KB
testcase_03 AC 75 ms
75,248 KB
testcase_04 AC 74 ms
71,176 KB
testcase_05 AC 92 ms
76,784 KB
testcase_06 AC 87 ms
76,452 KB
testcase_07 AC 76 ms
74,988 KB
testcase_08 AC 174 ms
77,588 KB
testcase_09 AC 74 ms
75,672 KB
testcase_10 AC 1,500 ms
78,592 KB
testcase_11 AC 1,390 ms
78,112 KB
testcase_12 AC 98 ms
77,316 KB
testcase_13 AC 120 ms
77,212 KB
testcase_14 AC 1,392 ms
77,832 KB
testcase_15 AC 72 ms
70,960 KB
testcase_16 AC 72 ms
71,068 KB
testcase_17 AC 71 ms
71,008 KB
testcase_18 AC 1,607 ms
78,200 KB
testcase_19 AC 117 ms
77,348 KB
testcase_20 AC 1,310 ms
78,112 KB
testcase_21 AC 1,699 ms
77,712 KB
testcase_22 AC 1,632 ms
78,304 KB
testcase_23 AC 73 ms
75,360 KB
testcase_24 AC 138 ms
77,568 KB
testcase_25 AC 118 ms
77,532 KB
testcase_26 AC 340 ms
77,528 KB
testcase_27 AC 450 ms
77,448 KB
testcase_28 AC 869 ms
77,968 KB
testcase_29 AC 368 ms
77,524 KB
testcase_30 AC 1,640 ms
78,752 KB
testcase_31 AC 1,557 ms
77,628 KB
testcase_32 AC 74 ms
75,316 KB
testcase_33 AC 163 ms
77,264 KB
testcase_34 AC 188 ms
77,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#素因数分解
def Prime_Factorization(N):
    if N<0:
        R=[[-1,1]]
    else:
        R=[]

    N=abs(N)

    if N&1==0:
        C=0
        while N&1==0:
            N>>=1
            C+=1
        R.append([2,C])

    if N%3==0:
        C=0
        while N%3==0:
            N//=3
            C+=1
        R.append([3,C])

    k=5
    Flag=0
    while k*k<=N:
        if N%k==0:
            C=0
            while N%k==0:
                C+=1
                N//=k
            R.append([k,C])
        k+=2+2*Flag
        Flag^=1

    if N!=1:
        R.append([N,1])

    return R

class Grid:
    def __init__(self,*F):
        self.F=tuple(F)
        self.dim=len(self.F)

        R=[1]
        for a in self.F[::-1]:
            R.append(R[-1]*a)

        self.volume,*self.partition=R[::-1]

    def number_to_position(self,N):
        assert 0<=N<self.volume

        pos=[0]*self.dim
        for i in range(self.dim):
            pos[i],N=divmod(N,self.partition[i])
        return pos

    def position_to_number(self,*pos):
        assert len(pos)==self.dim

        N=0
        for i in range(self.dim):
            assert 0<=pos[i]<self.F[i]
            N+=self.partition[i]*pos[i]
        return N

    def number_neighborhood_yielder(self,N):
        r=N
        for i in range(self.dim):
            q,r=divmod(r,self.partition[i])

            if 0<q:
                yield N-self.partition[i]

            if q<self.F[i]-1:
                yield N+self.partition[i]

    def position_neighborhood_yielder(self,*pos):
        assert self.dim==len(pos)
        pos=list(pos)

        for i in range(self.dim):
            if 0<pos[i]:
                pos[i]-=1
                yield pos
                pos[i]+=1

            if pos[i]<self.F[i]-1:
                pos[i]+=1
                yield pos
                pos[i]-=1

#==================================================
N,K=map(int,input().split())
A=list(map(int,input().split()))

Mod=10**9+7

if K==1:
    exit(print((pow(2,N,Mod)-1)%Mod))

P=[(p,e) for p,e in Prime_Factorization(K)]

tau=[e+1 for p,e in P]
m=len(P)
G=Grid(*tau)

k=1
for t in tau:
    k*=t

DP=[0]*k; DP[0]=1
q=[0]*m
for a in A:
    f=[0]*m
    for i in range(m):
        p,_=P[i]
        while a%p==0:
            f[i]+=1
            a//=p

    for j in range(k-1,-1,-1):
        p=G.number_to_position(j)
        for i in range(m):
            q[i]=min(p[i]+f[i],P[i][1])

        DP[G.position_to_number(*q)]+=DP[j]

    for j in range(k):
        DP[j]%=Mod

print(DP[-1])
0