Last Updated: 13 Oct, 2025

Divisible Sum Difference

Easy

Problem statement

You are given two positive integers, n and m. Your task is to find the sum of all integers in the range from 1 to m (inclusive) that are divisible by n, and the sum of all integers in the same range that are not divisible by n.


Let sum_divisible be the sum of numbers from 1 to m that are divisible by n.

Let sumnotdivisible be the sum of numbers from 1 to m that are not divisible by n.


You must calculate and return the difference: sumnotdivisible - sum_divisible.


Input Format:
The first and only line of input contains two space-separated integers, n and m.


Output Format:
Print a single integer representing the calculated difference.


Note:
A brute-force loop that iterates from 1 to m will be too slow for the given constraints. The problem can be solved in O(1) time by using mathematical formulas for arithmetic series. The sums can become very large, so be sure to use a 64-bit integer type (like long in Java or long long in C++) for your calculations.