POJ题单 组合数

POJ题单 组合数

总结

组合数计算方法

  1. O(n)直接计算:ans = 1,然后乘分数
  2. 带有阶乘的公式
  3. n,m很小的时候可以杨辉三角打表C(n,m)=C(n-1,m)+C(n-1,m-1);
  4. n,m较大,p是素数,lucas定理
  5. n,m较大,p不是素数,拓展lucas
    (详情组合数取模方法总结
  6. 常用公式在这里插入图片描述

组合数更是一种解决方法,没啥好写的题目,题目里面更多时候思路是解题的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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值