Please subscribe to the official Codeforces channel in Telegram via the link: https://t.me/codeforces_official.
×

# | User | Rating |
---|---|---|

1 | tourist | 3372 |

2 | Radewoosh | 3353 |

3 | OO0OOO00O0OOO0O0…O | 3264 |

4 | V--o_o--V | 3247 |

5 | scott_wu | 3168 |

6 | Benq | 3154 |

7 | mnbvmar | 3143 |

8 | Petr | 3139 |

8 | ksun48 | 3139 |

10 | LHiC | 3118 |

# | User | Contrib. |
---|---|---|

1 | Radewoosh | 195 |

2 | Errichto | 175 |

3 | rng_58 | 159 |

4 | Ashishgup | 157 |

5 | neal | 156 |

6 | tourist | 154 |

7 | PikMike | 151 |

8 | Petr | 150 |

8 | Um_nik | 150 |

10 | Swistakk | 147 |

10 | kostka | 147 |

10 | Vovuh | 147 |

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Dec/10/2018 02:06:42 (d1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Dear AmEr_Tinsley,

It seems that the MLE error is not due to the size of the global two-dimensional array, as the program has already passed 12 tests.

The more plausible reason is that with n = 10,000,000, the maximum value for n, the program exceeded the stack size limit due to the large number of recursive calls.

Please keep in mind that each function call allocates a new stack frame in the program memory space. The size of the stack frame should be large enough to at store all the local variables declared inside the function, the arguments passed by value, and the present processor register values that the function changes their contents. Optimizing compilers often use Abstract interpretation at compile time to compute the minimal size for the stack frame of each function.

Best wishes

Thank you :D

With pleasure.

The following is an iterative

O(n)version of your algorithm.35538919

The simple idea used to transform the recursive algorithm to an iterative algorithm is to reverse the direction of changing the variable (i), i.e. decrement (i) instead of increment it, provided that the final case dp[n][0] = 1 and dp[n][1] = 0 is known.

Only two variables are sufficient to compute the final value dp[0][0], instead of reserving a two-dimensional array, as dp[i][0] and dp[i][1] are computed in terms of dp[i+1][0] and dp[i+1][1], and are used only once in the subsequent iteration of the loop.

Best wishes,

Here is a nice video solution of this problem by rachitjain. Hope it helps.

Yeaa i got it now , sometimes bottom up approach is a must ;) , thank you :D

With n <= 1e7, you shouldn't use memoization top-down. This can make you overflow due to many recursive calls.

Instead you should build bottom-up, f[i][j] is the number of ways to get j after i steps, so f[i][j] = sigma f[i-1][k] with k != j.

P/s: If n <= 1e9, you can solve this problem using matrix multiplication.

By the way, it's good to test such solutions at CUSTOM TEST here on Codeforces to see the amount of memory the solution uses since the biggest input in this problem is simple and not hard to be written by ourselves counter to most of the graph problems input, for example.