結果

問題 No.907 Continuous Kadomatu
ユーザー vwxyzvwxyz
提出日時 2024-05-03 23:03:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 5,507 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 83,932 KB
最終ジャッジ日時 2024-05-03 23:03:39
合計ジャッジ時間 9,116 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
59,136 KB
testcase_01 AC 44 ms
54,144 KB
testcase_02 AC 40 ms
53,504 KB
testcase_03 AC 39 ms
53,760 KB
testcase_04 AC 41 ms
54,016 KB
testcase_05 AC 228 ms
78,244 KB
testcase_06 AC 116 ms
76,340 KB
testcase_07 AC 148 ms
77,968 KB
testcase_08 AC 652 ms
77,684 KB
testcase_09 AC 183 ms
77,904 KB
testcase_10 AC 1,380 ms
77,784 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 -- -
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 #

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]*MD.Fact_Inve(r-l)*p%mod
            else:
                dp[r][x]+=dp[l][x+1:]*cnt[r-l]*MD.Fact_Inve(r-l)*p%mod
            dp[r][x]%=mod
    dp[r]=Cumsum(dp[r],mod=mod)
ans=dp[-1][:]
print(ans)
0