R4-每日一题.
今日甚是清醒.
目录
动态规划
记忆化搜索
python
class Solution:
def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int:
MOD=1_000_000_007
@cache
def dfs(i,j,k):
if i==0:
return 1 if k==1 and j<=limit else 0
if j==0:
return 1 if k==0 and i<=limit else 0
if k==0:
return (dfs(i-1,j,0)+dfs(i-1,j,1)-(dfs(i-limit-1,j,1) if i>limit else 0))% MOD
else:
return (dfs(i,j-1,0)+dfs(i,j-1,1)-(dfs(i,j-limit-1,0) if j>limit else 0))% MOD
ret=(dfs(zero,one,0)+dfs(zero,one,1)) % MOD
#防止爆内存
dfs.cache_clear()
return ret
递推方法
就是将记忆化翻译成3维数组!
python
class Solution:
def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int:
MOD=1_000_000_007
#直接上3维数组
f=[[[0,0] for _ in range(one+1)] for _ in range(zero+1)]
for i in range(1,min(limit,zero)+1):
f[i][0][0]=1
for j in range(1,min(limit,one)+1):
f[0][j][1]=1
for i in range(1,zero+1):
for j in range(1,one+1):
f[i][j][0]=(f[i-1][j][0]+f[i-1][j][1]-(f[i-limit-1][j][1] if i>limit else 0))%MOD
f[i][j][1]=(f[i][j-1][0]+f[i][j-1][1]-(f[i][j-limit-1][0] if j>limit else 0))%MOD
return sum(f[-1][-1])%MOD
组合数学
容斥原理+乘法原理
这是人能想出来的吗??!!
python
class Solution:
def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int:
MOD=1_000_000_007
MX=1001
#f[i]=i! 阶乘
fac=[0]*MX
fac[0]=1
for i in range(1,MX):
fac[i]=fac[i-1]*i%MOD
#inv_f[i]=i!^-1
inv_f=[0]*MX
inv_f[-1]=pow(fac[-1],-1,MOD)
for i in range(MX-1,0,-1):
inv_f[i-1]=inv_f[i]*i%MOD
#最终计算(n m)
def comb(n,m):
return fac[n]*inv_f[m]*inv_f[n-m]%MOD
if zero>one:
# 保证空间复杂度为 O(min(zero, one))
zero,one=one,zero
f0=[0]*(zero+3)
for i in range((zero-1)//limit+1,zero+1):
f0[i]=comb(zero-1,i-1)
for j in range(1,(zero-i)//limit+1):
f0[i]=(f0[i]+(-1 if j%2 else 1)*comb(i,j)*comb(zero-j*limit-1,i-1))%MOD
ret=0
for i in range((one-1)//limit+1,min(one,zero+1)+1):
f1=comb(one-1,i-1)
for j in range(1,(one-i)//limit+1):
f1=(f1+(-1 if j %2 else 1)*comb(i,j)*comb(one-j*limit-1,i-1))%MOD
ret=(ret+(f0[i-1]+f0[i]*2+f0[i+1])*f1)%MOD
return ret
灵神真的厉害!
ps:
1.
python
#防止整数溢出
MOD=1_000_000_007
%MOD
python
#记忆化搜索
@cache
python
#防止爆内存
dfs.cache_clear()
2.
3.
刷完后再润灵神题单(争取开学前干完?!)