2894.cpp (422B)
1 // Brute force 2 class Solution { 3 public: 4 int differenceOfSums(int n, int m) const { 5 int res = 0; 6 7 for (int i = 1; i <= n; i++) { 8 if (i % m) 9 res += i; 10 else 11 res -= i; 12 } 13 14 return res; 15 } 16 }; 17 18 // Math 19 class Solution { 20 public: 21 int differenceOfSums(int n, int m) const { return n * (n + 1) / 2 - m * (n / m) * (n / m + 1); } 22 };