this is my solution and it works

```
#include < iostream >
using namespace std;
int main()
{
int a, b, div, rem;
cin >> a >> b;
int sum = a;
while(a >= b)
{
div = a / b;
sum += div;
rem = a % b;
a = div + rem;
}
cout << sum << endl;
return 0;
}
[link to the problem] (http://codeforces.com/problemset/problem/379/A)
`
```

but I saw other people solutions and they have used a formula that calculates answer without any loop

` ans = (a * b — 1)/(b — 1) `

Can someone please help me how to get this formula?

Auto comment: topic has been updated by piyushgoyal443 (previous revision, new revision, compare).Please provide a link to the problem.

There is no var called 'mod' in your code

Auto comment: topic has been updated by piyushgoyal443 (previous revision, new revision, compare).(sorry for my english) Given:

Let s = sum(i = 1 ... n — 1){qi}

So we need to find s. From (1) we get r1:

From this and (2) we get r2:

And so we get r3:

And so on:

Then

if rn equals 0 then q(n — 1) + r(n — 1) = 0 too. And then q(n — 1) = 0 and n = n — 1 what is wrong. Then

Then we need to subtract at least 1 from a in (3):

thanks

Which mathematical theory you used here?