Setting up the recurrence is essential for every problem
Problem 2225: Sum Decomposition
Problem 13398: Consecutive Sum 2
Problem 2666: Moving Closet Doors
Problem 2410: Sum of Powers of 2
Ezreal goes mid first gg - 20500
Problem 20500: Ezreal goes mid first gg
You can store the number itself instead of the remainder.
But it's probably better to solve it by storing the remainder.
Handling the inverse operation of the remainder can be important in this problem.
let FIVE = 1, ONE = 0, MOD = 1_000_000_007 let N = Int(readLine()!)! var dp = [[[Int]]](repeating:[[Int]](repeating:[Int](repeating:0,count:2),count:3),count:N+1) dp[1][1][ONE] = 1 dp[1][2][FIVE] = 1 for i in 1..<N { for j in 0...2 { dp[i+1][(j+1)%3][ONE] = (dp[i+1][(j+1)%3][ONE] + dp[i][j][ONE]) % MOD dp[i+1][(j+1)%3][ONE] = (dp[i+1][(j+1)%3][ONE] + dp[i][j][FIVE]) % MOD dp[i+1][(j+5)%3][FIVE] = (dp[i+1][(j+5)%3][FIVE] + dp[i][j][ONE]) % MOD dp[i+1][(j+5)%3][FIVE] = (dp[i+1][(j+5)%3][FIVE] + dp[i][j][FIVE]) % MOD } } print(dp[N][0][FIVE])