2894. 分类求和并作差 - 力扣(LeetCode)
一、Java
class Solution {public int differenceOfSums(int n, int m) {return (1+n)*n/2-n/m*(m+n/m*m)/2;}
}
二、C++
class Solution {
public:int differenceOfSums(int n, int m) {return (1+n)*n/2-n/m*(m+n/m*m);}
};
三、Python
class Solution:def differenceOfSums(self, n: int, m: int) -> int:return (1+n)*n//2-n//m*(m+n//m*m)
四、JavaScript
var differenceOfSums = function(n, m) {let k=Math.floor(n/m);return (1+n)*n/2-(m+m*k)*k;
};
五、Go
func differenceOfSums(n int, m int) int {return (1+n)*n/2-n/m*(m+n/m*m)
}