POJ题单 组合数
总结
组合数计算方法
- O(n)直接计算:ans = 1,然后乘分数
- 带有阶乘的公式
- n,m很小的时候可以杨辉三角打表C(n,m)=C(n-1,m)+C(n-1,m-1);
- n,m较大,p是素数,lucas定理
- n,m较大,p不是素数,拓展lucas
(详情组合数取模方法总结) - 常用公式
组合数更是一种解决方法,没啥好写的题目,题目里面更多时候思路是解题的key point
Paths on a Grid POJ - 1942
题目
思路
经典题了,C(n+m,m)
代码
#include <iostream>
#include <cstdio>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
//#include <unordered_map>
using namespace std;
#define dbg(x) cerr << #x " = " << x << endl;
typedef pair<int, int> P;
typedef long long ll;
#define FIN freopen("in.txt", "r", stdin);
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll a, b;
while(~scanf("%lld%lld", &a, &b))
{
if(a == 0 && b == 0) break;
double ans = 1;
ll _b = min(a, b);
a = a + b;
b = _b;
while(b >= 1){
ans *= a-- / double(b--);
}
printf("%.0lf\n", ans);
}
return 0;
}