### piyushgoyal443's blog

By piyushgoyal443, history, 5 years ago,

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;
}




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

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

• 0

 » 5 years ago, # |   0 Auto comment: topic has been updated by piyushgoyal443 (previous revision, new revision, compare).
 » 5 years ago, # |   +3 Please provide a link to the problem.
 » 5 years ago, # |   +3 There is no var called 'mod' in your code
 » 5 years ago, # |   0 Auto comment: topic has been updated by piyushgoyal443 (previous revision, new revision, compare).
 » 5 years ago, # | ← Rev. 4 →   0 (sorry for my english) Given: a = q1 * b + r1; (1) q1 + r1 = q2 * b + r2; (2) ... q(n - 1) + r(n - 1) = qn * b + rn; (n) where qn = 0 ans = a + sum(i = 1 ... n - 1){qi} Let s = sum(i = 1 ... n — 1){qi}So we need to find s. From (1) we get r1: r1 = a - q1 * b From this and (2) we get r2: r2 = a - q1 * (b - 1) - q2 * b And so we get r3: r3 = a - (q1 + q2) * (b - 1) - q3 * b And so on: rn = a - s * (b - 1) - qn * b = a - s * (b - 1) Then s = (a - rn) / (b - 1) (s) 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 1 <= rn < b Then we need to subtract at least 1 from a in (3): s = (a - 1) / (b - 1) answer = a + s = a + (a - 1) / (b - 1) = (a * b - 1) / (b - 1) `
•  » » 5 years ago, # ^ |   0 thanks
•  » » 2 months ago, # ^ |   0 Which mathematical theory you used here?