腾讯笔试题
请问红白花那个题
为什么爆超时啊?我是O(n)复杂度的
t,k = list(map(int,input().split())) dp = [1] * (100000+1) for i in range(k,100000+1): dp[i] = (dp[i-k] + dp[i-1])%(int(10e9+7)) for _ in range(t): a,b = list(map(int,input().split())) result = sum(dp[a:b+1])%(int(10e9+7)) print(result)
破案了,mod多了一个0
我真傻 真的
t,k = list(map(int,input().split())) dp = [1] * (100000+1) mod = int(1e9+7) for i in range(k,100000+1): dp[i] = (dp[i-k] + dp[i-1] + mod)%mod for i in range(1,100000+1): dp[i] += (dp[i-1]+mod) dp[i] = dp[i]%mod for _ in range(t): a,b = list(map(int,input().split())) result = (dp[b]-dp[a-1]+mod)%mod print(result)