Deriving the DP formula in problem "Leaf and Limelight Attack" of HackerEarth

Revision en1, by jsaita96, 2017-01-10 13:10:07

While practicing DP problems on various sites, I came across this problem:- https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/practice-problems/algorithm/leaf-and-limelight-attack-circuit/description/ on HackerEarth.

According to the editorial, we pre-process the values in the solution. Queries are divided into even and odd. DP[i] gives the solution for ith query. So, DP[1] = 1, DP[3] = 25, DP[5] = 101.

So, they have derived a formula which is :-

DP[i] = DP[i-2] + (i-2)2 + (i-1) + (i-2)2 + 2(i-1) + (i-2)2 + 3(i-1) + (i-2)2 + 4(i-1) which gets reduced to

DP[i] = DP[i-2] + 4i2 — 6*(i-1)

So my question is, how was this formula derived? What was the approach in deriving this ?

Tags dp

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English jsaita96 2017-01-10 13:10:07 846 Initial revision (published)