The thing about "time limit exceeded" on these kinds of problems is not to write the code for the first thing that pops into your head.
Mostly, they're an exercise in mathematics for you to understand the real essence of the problem and come up with a direct answer.
If you were given the problem, and only a pen and paper, you'd be looking for smarter ways to solve the problem than just try everything. It's that approach written as a program is what you're supposed to be submitting.
That's why they have TLE, to weed out the chancers.
What salem says gets to the heart of the matter.
TLE usually means that you need a rethink and redesign, not some little tweak.
Think about the pattern in the problem and take advantage of it.
In this problem you were adding up two numbers, call them a and b, like this:
a + a + ... + a + b + a + a + ... + a + b + ...
where there are k a's in each group of a's and the total number of values is m.
So obviously that's something we can calculate without the loop.
It's a basic div/mod job.
(m / (k + 1) * (a * k + b)) + (m % (k + 1) * a)
m / (k + 1) gives the number of times the complete a+a+...+a+b pattern can fit.
m % (k + 1) gives the number of a's in the partial pattern at the end
They want problem solving/logical thinking. You can either beat the crap out of someone until they give you the answers, or you can persuade them. These problems want you to really think about what you're actually doing, look at the inner workings, and find a better implementation. As salem c put it, they put those barriers up so that you don't just code the first thing that comes to mind.
Couldn't help much since I'm busy with my own assignments.
> how the hell do they expect me a newbie to figure these things out
First of all, codechef and the like are very niche problems. Mostly they're based on problems such as factorisation, combinatorics, permutations and the like.
These have quick and dumb answers which typically result in TLE or M(emory)LE, or they have smarter answers - because you did the math on paper in the first instance to analyse the actual problem. The code at the end is somewhat incidental, and is only a means of communicating your 'smartness' to the test site.
They're also easy problems for automated testing; just feed in some numbers, output some numbers and compare.
So when you're given things like 2 <= m,k <= 2*109, you practice your solution on paper, first at 2*100, and then at 2*101. If you find yourself doing 10 times the work, then you have the wrong approach.
However, what isn't niche is the actual act of solving problems. As a working programmer, you will spend most of your time analysing and solving problems. Just knowing what a for loop is, and where the curly brackets go is the tip of the iceberg.
I'm pretty certain these online code challenges are not intended for newbies.
the same way experts figure it out: code it is the last thing you do.
solve it by hand, figure out the 'trick' / math / algorithm.
once you know exactly what the steps to solve the thing are, coding these types of problems is usually simple; most are < 1 page of code, and most can be done in a main monolith program with nothing too fancy (but c++ gives you all kinds of shortcuts on some of these problems with its containers and algorithms).
And that is what I would love to see from people doing them: this is how I want to solve it, here is my code, but I can't quite express my solution in code, a little help with the syntax/c++ side of the problem. The math part is on YOU. The c++ is too, but that, you can get help after showing a good try.