Can someone please explain this ?
I donât understand why you need to:
1.) check for separate cases if(k==1) or(n==2) or(n>=k), you could do in one way.
2.) if you got the number of terms, then you could calculate the sum of AP, why do you need to calculate the last term and then use a different formula.
I tried to modify your code. Might help you. I donât know much python, there maybe be some more lines you could remove from the code, but youâll understand it.
https://www.codechef.com/viewsolution/24960184
P.S: Tried to copy paste the python code here, it was a nightmare.
Thank you for your reply. Can you please explain how are you calculating t and why do you multiply y with t?
here it goes:
We know the first term is (k-1), We know the difference is -(n-1), Now We know our AP is something like this, first_term,first_term-diff,âŚ0/anyotherNumber.
So, We know firt term=k-1, we know diff=n-1, We also know, that we need our last term>=0. So simply
last term=firstTerm+(t-1)*(-diff) >=0, Thus simplify this, we get
t<=(last term+diff)/diff. hence t=(lastTerm+diff)/diff, rounded off.
Thatâs how you get t(or number of terms).
Now, we are using the formula sumAP=(t*(2*firstTerm-(t-1)*diff))/2, remeber I used -(t-1)*diff, because we know diff is negative i.e (-(n-1)).
Now we check which part is even and divide it by 2, thats why I had y=firstPart ofAP, and multiplied with t.
hence ans=y*t.
I guess itâs clarified now.
Its clarified. Thanks a lot for the explanations.
Just suggesting, when you write a code, declaring 100000007 globally helps.
@sikh_coder Thank You for your explanation but I didnât undertand if last_term = first_term - (t-1)d;
Then shoulldnât t = (first_term - last_term + d)/2 ??
I think youâre mixing it up. t is the number of terms.
I am not able to understand what the editorial is saying. Which topics on Math do I need to know to solve this problem?
@neel19
Prerequisites: Modulo Division(10^9 + 7) and Arithmetic Progression.
Have a Look at this link for modulo division(10^9 + 7) click me here
Can someone explain me the setterâs solution? How is last = first - total*(n-1) ? And the if statement ans%2 == 0? @rishup_nitdgp
Hello,
I have understood that this problem is solvable by the sum of arithmatic progression formula given above. I am a newbie in modular arithmatic. I can only apply modular arithmatic for +,- & *. I mean I do not understand modular division. Without understanding modular divison can I solve this problem using above formula ? If so then how ? Otherwise help me understanding modular division in respect of this formula ?