题目链接:点击打开链接
线性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;
}