fork(1) download
  1. def solution(N, K):
  2. MOD = 10**9 + 7
  3.  
  4. # Initialize DP table
  5. dp = [[0] * 2 for _ in range(N + 1)]
  6.  
  7. # Base cases
  8. dp[0][0] = 1
  9.  
  10. for i in range(1, N + 1):
  11. # Color the object with color 0
  12. dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % MOD
  13.  
  14. # Color the object with color 1
  15. dp[i][1] = dp[i - 1][0] # Always color the object with color 1 if possible
  16.  
  17. # If distance between previous object colored with 1 and current object <= K
  18. if i >= K + 1:
  19. dp[i][1] = (dp[i][1] + dp[i - K - 1][1]) % MOD
  20.  
  21. # Calculate total ways
  22. total_ways = (dp[N][0] + dp[N][1]) % MOD
  23.  
  24. return total_ways
  25.  
  26. # Taking input from the user
  27. T = int(input())
  28.  
  29. for _ in range(T):
  30. N, K = map(int, input().split())
  31. print(solution(N, K))
  32.  
Success #stdin #stdout 0.03s 9808KB
stdin
1
5 2
stdout
15