You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
504 B
22 lines
504 B
class RecookMath {
|
|
///组合数算法
|
|
///
|
|
///传统算法会出现溢出
|
|
///
|
|
///
|
|
///[Stackoverflow](https://stackoverflow.com/questions/12130094/optimizing-calculating-combination-and-avoiding-overflows)
|
|
///
|
|
///[blog](https://blog.plover.com/math/choose.html)
|
|
static int combination(int k, int n) {
|
|
if (k <= 0 || n <= 0) return 0;
|
|
int r = 1;
|
|
int d;
|
|
if (k > n) return 0;
|
|
for (d = 1; d <= k; d++) {
|
|
r *= n--;
|
|
r = (r / d).floor();
|
|
}
|
|
return r;
|
|
}
|
|
}
|