Hello @dhayden, I'm not sure whether you agree with me or not.
Your example is indeed what you would get for K=2.
The code given in my Fortran and Python examples is for K=3.
For K up to 5 you can run it online:
https://rextester.com/SZDQY78041
719
S(1) = 15
S(2) = 85
S(3) = 225
S(4) = 274
S(5) = 120 |
So,
S(1) = 15 (as you worked out)
S(2) = 85 (as you worked out)
S(3) = 225 (this means that S(1) + S(2) + S(3) = 325 in the original question)
@sawingboat, I think you should be able to convert those array sections to a loop (though you must loop downwards to get the update correct, or the update of k-1 will contaminate the update of k).
I'm pretty sure this isn't a codechef live competition, but you are sufficiently demanding that I suspect it is live homework or competition somewhere.
sawingboat12 wrote: |
---|
@lastChance a working example for that.
Let's take A = [1, 2, 3] & k = 2. |
Answer is 6 + 11 = 17
S(1) = 1 + 2 + 3 = 6
S(2) = 2 * 3 + 1 * 3 + 1 * 2 = 11
So S(1) + S(2) = 17
-----
The way my code would do it:
Start with S(0)=1, S(1) = S(2) = 0
Include value 1: then
S(0) = 1,
S(1) = 0 + 1 * 1 = 1
S(2) = 0 + 1 * 0 = 0
Include value 2: then
S(0) = 1
S(1) = 1 + 2 * 1 = 3
S(2) = 0 + 2 * 1 = 2
Include value 3: then
S(0) = 1
S(1) = 3 + 3 * 1 = 6
S(2) = 2 + 3 * 3 = 11
At the end, S(1) = 6, S(2) = 11 and S(1) + S(2) = 17