编写函数,参数是两个非负整数n和m,返回组合数,其中m≤n≤25。例如,n=25,m=12时答案为5200300。
#include <iostream>
using namespace std;
typedef long long ll;
ll fun(int m, int n){
if(m < n-m) m = n-m;//this is the key
ll ans = 1;
for(int i = n; i >= m+1; i--)
ans *= i;
for(int i = n-m; i > 0; i--)
ans /= i;
return ans;
}
int main() {
int m = 1;
int n = 21;
cout << fun(m, n);
return 0;
}