Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void solve()
- {
- ll a, b, c;
- cin >> a >> b >> c;
- if (a > b)
- swap(a, b);
- // sum from 1 to n without adding the number which are divisible by c
- 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);
- // the sum of the numbers which are divisible by c after dividing it by c
- ll adding_sum = (((b / c) * ((b / c + 1))) / 2) - ((((a - 1) / c) * (((a - 1) / c) + 1)) / 2);
- ll final_sum = whole_sum + adding_sum;
- cout << final_sum << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment