結果

問題 No.829 成長関数インフレ中
ユーザー vwxyzvwxyz
提出日時 2024-04-14 08:37:57
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 6,190 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 278,396 KB
最終ジャッジ日時 2024-10-03 05:44:35
合計ジャッジ時間 6,870 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
63,180 KB
testcase_01 AC 42 ms
56,692 KB
testcase_02 AC 41 ms
56,280 KB
testcase_03 AC 42 ms
55,828 KB
testcase_04 AC 42 ms
56,500 KB
testcase_05 AC 42 ms
56,408 KB
testcase_06 AC 42 ms
56,264 KB
testcase_07 AC 41 ms
56,428 KB
testcase_08 AC 44 ms
55,572 KB
testcase_09 AC 41 ms
55,360 KB
testcase_10 AC 42 ms
55,888 KB
testcase_11 AC 41 ms
56,540 KB
testcase_12 AC 107 ms
112,128 KB
testcase_13 AC 51 ms
67,732 KB
testcase_14 AC 78 ms
91,744 KB
testcase_15 TLE -
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import math

def Extended_Euclid(n,m):
    stack=[]
    while m:
        stack.append((n,m))
        n,m=m,n%m
    if n>=0:
        x,y=1,0
    else:
        x,y=-1,0
    for i in range(len(stack)-1,-1,-1):
        n,m=stack[i]
        x,y=y,x-(n//m)*y
    return x,y

class MOD:
    def __init__(self,p,e=None):
        self.p=p
        self.e=e
        if self.e==None:
            self.mod=self.p
        else:
            self.mod=self.p**self.e

    def Pow(self,a,n):
        a%=self.mod
        if n>=0:
            return pow(a,n,self.mod)
        else:
            #assert math.gcd(a,self.mod)==1
            x=Extended_Euclid(a,self.mod)[0]
            return pow(x,-n,self.mod)

    def Build_Fact(self,N):
        assert N>=0
        self.factorial=[1]
        if self.e==None:
            for i in range(1,N+1):
                self.factorial.append(self.factorial[-1]*i%self.mod)
        else:
            self.cnt=[0]*(N+1)
            for i in range(1,N+1):
                self.cnt[i]=self.cnt[i-1]
                ii=i
                while ii%self.p==0:
                    ii//=self.p
                    self.cnt[i]+=1
                self.factorial.append(self.factorial[-1]*ii%self.mod)
        self.factorial_inve=[None]*(N+1)
        self.factorial_inve[-1]=self.Pow(self.factorial[-1],-1)
        for i in range(N-1,-1,-1):
            ii=i+1
            while ii%self.p==0:
                ii//=self.p
            self.factorial_inve[i]=(self.factorial_inve[i+1]*ii)%self.mod

    def Build_Inverse(self,N):
        self.inverse=[None]*(N+1)
        assert self.p>N
        self.inverse[1]=1
        for n in range(2,N+1):
            if n%self.p==0:
                continue
            a,b=divmod(self.mod,n)
            self.inverse[n]=(-a*self.inverse[b])%self.mod

    def Inverse(self,n):
        return self.inverse[n]

    def Fact(self,N):
        if N<0:
            return 0
        retu=self.factorial[N]
        if self.e!=None and self.cnt[N]:
            retu*=pow(self.p,self.cnt[N],self.mod)%self.mod
            retu%=self.mod
        return retu

    def Fact_Inve(self,N):
        if self.e!=None and self.cnt[N]:
            return None
        return self.factorial_inve[N]

    def Comb(self,N,K,divisible_count=False):
        if K<0 or K>N:
            return 0
        retu=self.factorial[N]*self.factorial_inve[K]%self.mod*self.factorial_inve[N-K]%self.mod
        if self.e!=None:
            cnt=self.cnt[N]-self.cnt[N-K]-self.cnt[K]
            if divisible_count:
                return retu,cnt
            else:
                retu*=pow(self.p,cnt,self.mod)
                retu%=self.mod
        return retu

def FFT(polynomial0,polynomial1,digit=10**5,round_to_int=True):
    assert digit==0 or round_to_int
    if len(polynomial0)*len(polynomial1)<=60:
        polynomial=[0]*(len(polynomial0)+len(polynomial1)-1)
        for i in range(len(polynomial0)):
            for j in range(len(polynomial1)):
                polynomial[i+j]+=polynomial0[i]*polynomial1[j]
        return polynomial
    def DFT(polynomial,n,inverse=False):
        if inverse:
            primitive_root=[math.cos(-i*2*math.pi/(1<<n))+math.sin(-i*2*math.pi/(1<<n))*1j for i in range(1<<n)]
        else:
            primitive_root=[math.cos(i*2*math.pi/(1<<n))+math.sin(i*2*math.pi/(1<<n))*1j for i in range(1<<n)]
        if inverse:
            for bit in range(1,n+1):
                a=1<<bit-1
                for i in range(1<<n-bit):
                    for j in range(a):
                        s=i*2*a+j
                        t=s+a
                        polynomial[s],polynomial[t]=polynomial[s]+polynomial[t]*primitive_root[j<<n-bit],polynomial[s]-polynomial[t]*primitive_root[j<<n-bit]
        else:
            for bit in range(n,0,-1):
                a=1<<bit-1
                for i in range(1<<n-bit):
                    for j in range(a):
                        s=i*2*a+j
                        t=s+a
                        polynomial[s],polynomial[t]=polynomial[s]+polynomial[t],primitive_root[j<<n-bit]*(polynomial[s]-polynomial[t])

    def FFT_(polynomial0,polynomial1,round_to_int=True):
        N0=len(polynomial0)
        N1=len(polynomial1)
        N=N0+N1-1
        n=(N-1).bit_length()
        polynomial0=polynomial0+[0]*((1<<n)-N0)
        polynomial1=polynomial1+[0]*((1<<n)-N1)
        DFT(polynomial0,n)
        DFT(polynomial1,n)
        fft=[x*y for x,y in zip(polynomial0,polynomial1)]
        DFT(fft,n,inverse=True)
        if round_to_int:
            fft=[round((fft[i]/(1<<n)).real) for i in range(N)]
        else:
            fft=[(fft[i]/(1<<n)).real for i in range(N)]
        return fft

    if digit:
        N0=len(polynomial0)
        N1=len(polynomial1)
        N=N0+N1-1
        polynomial00,polynomial01=[None]*N0,[None]*N0
        polynomial10,polynomial11=[None]*N1,[None]*N1
        for i in range(N0):
            polynomial00[i],polynomial01[i]=divmod(polynomial0[i],digit)
        for i in range(N1):
            polynomial10[i],polynomial11[i]=divmod(polynomial1[i],digit)
        polynomial=[0]*N
        for i,x in enumerate(FFT_(polynomial00,polynomial10)):
            polynomial[i]+=x*(digit**2-digit)
        for i,x in enumerate(FFT_(polynomial01,polynomial11)):
            polynomial[i]-=x*(digit-1)
        for i,x in enumerate(FFT_([x1+x2 for x1,x2 in zip(polynomial00,polynomial01)],[x1+x2 for x1,x2 in zip(polynomial10,polynomial11)])):
            polynomial[i]+=x*digit
    else:
        polynomial=FFT_(polynomial0,polynomial1,round_to_int=round_to_int)
    return polynomial

N,B=map(int,input().split())
mod=10**9+7
MD=MOD(mod)
MD.Build_Fact(N)
C=[0]*N
for s in map(int,input().split()):
    C[s]+=1
cnt=0
queue=[]
for s in range(N-1,-1,-1):
    if C[s]:
        if queue:
            c=MD.Fact(cnt+C[s]-1)*MD.Fact_Inve(cnt)%mod
            queue.append([c*cnt%mod,c*C[s]%mod])
        else:
            queue.append([0,MD.Fact(C[s])])
        cnt+=C[s]
queue=deque(queue)
while len(queue)>=2:
    queue.append([x%mod for x in FFT(queue.popleft(),queue.popleft())])
ans=sum(c*s*pow(B,s,mod)for s,c in enumerate(queue[0]))%mod
print(ans)
0