combinatorial number

编写函数,参数是两个非负整数n和m,返回组合数,其中m≤n≤25。例如,n=25,m=12时答案为5200300。

 

技巧:化简,避免溢出

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--)//notice this is m+1 not m
    		ans *= i;
    for(int i = n-m; i > 0; i--)
   		ans /= i;
    return ans;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章