結果

問題 No.907 Continuous Kadomatu
ユーザー vwxyzvwxyz
提出日時 2024-05-03 23:06:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 5,523 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 13,568 KB
実行使用メモリ 24,192 KB
最終ジャッジ日時 2024-11-25 04:36:40
合計ジャッジ時間 44,384 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
18,468 KB
testcase_01 AC 33 ms
18,344 KB
testcase_02 AC 30 ms
18,336 KB
testcase_03 AC 29 ms
23,680 KB
testcase_04 AC 28 ms
18,208 KB
testcase_05 AC 1,828 ms
23,936 KB
testcase_06 AC 54 ms
18,208 KB
testcase_07 AC 544 ms
24,064 KB
testcase_08 AC 1,253 ms
18,592 KB
testcase_09 AC 723 ms
23,808 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 AC 842 ms
18,468 KB
testcase_21 AC 879 ms
24,192 KB
testcase_22 AC 884 ms
18,472 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 AC 29 ms
11,776 KB
testcase_26 AC 27 ms
11,520 KB
testcase_27 AC 27 ms
11,520 KB
testcase_28 AC 28 ms
11,520 KB
testcase_29 AC 30 ms
24,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Compress(lst):
    decomp=sorted(list(set(lst)))
    comp={x:i for i,x in enumerate(decomp)}
    return comp,decomp

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
class Cumsum:
    def __init__(self,lst,mod=0):
        self.N=len(lst)
        self.mod=mod
        self.cumsum=[0]*(self.N+1)
        self.cumsum[0]=0
        for i in range(1,self.N+1):
            self.cumsum[i]=self.cumsum[i-1]+lst[i-1]
            if self.mod:
                self.cumsum[i]%=self.mod

    def __getitem__(self,i):
        if type(i)==int:
            if 0<=i<self.N:
                a,b=i,i+1
            elif -self.N<=i<0:
                a,b=i+self.N,i+self.N+1
            else:
                raise IndexError('list index out of range')
        else:
            a,b=i.start,i.stop
            if a==None or a<-self.N:
                a=0
            elif self.N<=a:
                a=self.N
            elif a<0:
                a+=self.N
            if b==None or self.N<=b:
                b=self.N
            elif b<-self.N:
                b=0
            elif b<0:
                b+=self.N
        s=self.cumsum[b]-self.cumsum[a]
        if self.mod:
            s%=self.mod
        return s

    def __setitem__(self,i,x):
        if -self.N<=i<0:
            i+=self.N
        elif not 0<=i<self.N:
            raise IndexError('list index out of range')
        self.cumsum[i+1]=self.cumsum[i]+x
        if self.mod:
            self.cumsum[i+1]%=self.mod

    def __len__(self):
        return self.N

    def __str__(self):
        lst=[self.cumsum[i+1]-self.cumsum[i] for i in range(self.N)]
        if self.mod:
            for i in range(self.N):
                lst[i]%=self.mod
        return "["+", ".join(map(str,lst))+"]"

N=int(input())
A,B=[],[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
mod=10**9+7
MD=MOD(mod)
MD.Build_Fact(N)
dp=[1]
cnt=[1]
for le in range(1,N+1):
    cnt.append(sum(dp)%mod)
    prev=dp
    dp=[0]*(1+le)
    for i in range(le):
        for j in range(le+1):
            if le%2 and j<=i or le%2==0 and i<j:
                dp[j]+=prev[i]
                dp[j]%=mod
inf=1<<30
comp,decomp=Compress(A+B+[inf])
le=len(comp)-1
dp=[[0]*le for i in range(N+1)]
dp[0][le-1]=1
dp[0]=Cumsum(dp[0],mod=mod)
BA=[b-a for a,b in zip(A,B)]
BA_inve=[MD.Pow(b-a,-1) for a,b in zip(A,B)]
for r in range(1,N+1):
    for x in range(le-1):
        p=1
        for l in range(r-1,-1,-1):
            d=decomp[x+1]-decomp[x]
            if max(decomp[x],A[l])<min(decomp[x+1],B[l]):
                p*=BA_inve[l]*d
                p%=mod
            else:
                p=0
            if l%2:
                dp[r][x]+=dp[l][:x]*cnt[r-l]%mod*MD.Fact_Inve(r-l)%mod*p%mod
            else:
                dp[r][x]+=dp[l][x+1:]*cnt[r-l]%mod*MD.Fact_Inve(r-l)%mod*p%mod
            dp[r][x]%=mod
    dp[r]=Cumsum(dp[r],mod=mod)
ans=dp[-1][:]
print(ans)
0