0%

AcWing 885. 求组合数 I

思路讲解

image

这个递推法其实就是把两种情况分开来考虑了(即选一个人的情况+不选一个人的情况)

AC代码

https://www.acwing.com/problem/content/submission/code_detail/40911169/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Problem: 求组合数 I
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/887/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
typedef pair<ll,bool> plb;
constexpr ll MAXN=static_cast<ll>(2e3)+15,INF=static_cast<ll>(5e18)+3;
constexpr ll mod=static_cast<ll>(1e9)+7;

ll N,M,T,C[MAXN][MAXN];

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
N=2010;
C[1][1]=1;
C[0][1]=1;
FOR(i,1,N){
C[0][i]=1;
}
FOR(i,2,N){
FOR(j,1,i){
C[j][i]=(C[j-1][i-1]+C[j][i-1])%mod;
}
}
cin>>T;
FOR(i,1,T){
ll a,b;
cin>>a>>b;
cout<<C[b][a]<<"\n";
}
return 0;
}
/*
AC
https://www.acwing.com/problem/content/submission/code_detail/40911169/
*/

心路历程(WA,TLE,MLE……)