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;
}

 

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