結果
問題 | No.907 Continuous Kadomatu |
ユーザー | vwxyz |
提出日時 | 2024-05-03 23:09:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 548 ms / 2,000 ms |
コード長 | 5,526 bytes |
コンパイル時間 | 349 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 78,740 KB |
最終ジャッジ日時 | 2024-11-25 04:39:20 |
合計ジャッジ時間 | 5,612 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,376 KB |
testcase_01 | AC | 43 ms
54,844 KB |
testcase_02 | AC | 42 ms
53,556 KB |
testcase_03 | AC | 41 ms
55,360 KB |
testcase_04 | AC | 41 ms
54,056 KB |
testcase_05 | AC | 124 ms
77,580 KB |
testcase_06 | AC | 56 ms
66,028 KB |
testcase_07 | AC | 111 ms
76,596 KB |
testcase_08 | AC | 124 ms
76,648 KB |
testcase_09 | AC | 114 ms
77,336 KB |
testcase_10 | AC | 151 ms
78,384 KB |
testcase_11 | AC | 167 ms
77,444 KB |
testcase_12 | AC | 245 ms
77,984 KB |
testcase_13 | AC | 251 ms
77,840 KB |
testcase_14 | AC | 275 ms
78,580 KB |
testcase_15 | AC | 168 ms
78,436 KB |
testcase_16 | AC | 262 ms
78,248 KB |
testcase_17 | AC | 166 ms
78,224 KB |
testcase_18 | AC | 260 ms
78,740 KB |
testcase_19 | AC | 279 ms
78,432 KB |
testcase_20 | AC | 104 ms
74,192 KB |
testcase_21 | AC | 88 ms
68,728 KB |
testcase_22 | AC | 87 ms
69,756 KB |
testcase_23 | AC | 337 ms
77,976 KB |
testcase_24 | AC | 548 ms
77,720 KB |
testcase_25 | AC | 41 ms
53,816 KB |
testcase_26 | AC | 41 ms
53,940 KB |
testcase_27 | AC | 41 ms
54,020 KB |
testcase_28 | AC | 41 ms
54,320 KB |
testcase_29 | AC | 43 ms
54,692 KB |
ソースコード
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: break 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)