IMohammedNasr

Untitled

Apr 22nd, 2022
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. void solve()
  2. {
  3.     ll a, b, c;
  4.     cin >> a >> b >> c;
  5.     if (a > b)
  6.         swap(a, b);
  7.     // sum from 1 to n without adding the number which are divisible by c
  8.     ll whole_sum = (((b * (b + 1)) / 2) - ((a * (a - 1)) / 2)) - (((((b / c) * ((b / c + 1))) / 2) - ((((a - 1) / c) * (((a - 1) / c) + 1)) / 2)) * c);
  9.     // the sum of the numbers which are divisible by c after dividing it by c
  10.     ll adding_sum = (((b / c) * ((b / c + 1))) / 2) - ((((a - 1) / c) * (((a - 1) / c) + 1)) / 2);
  11.     ll final_sum = whole_sum + adding_sum;
  12.     cout << final_sum << endl;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment