SDUT 2241-计算组合数C(n,m)(递推)

题目链接:点击打开链接

线性O(n) 计算组合数,递推法。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define maxn 1005
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 1<<40+10
#define pp pair<int,int>
#define ull unsigned long long
using namespace std;
ll C(ll n,ll m)
{
	if(m==0||n==m) return 1;
	ll sb=min(m,n-m);
	ll f=1,f1;
	for(ll i=1;i<=sb;i++)
	{
		f1=f*(n-i+1)/(i);
		f=f1;
	}
	return f1;
}
int main()
{
	int T;ll n,m;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		printf("%d\n",C(n,m));
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值