0%
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
| template<int MOD> class Combination { vector<vector<int>> _comb; public: Combination(int max_n) { _comb.resize(max_n+1, vector<int>()); for(int i=0;i<=max_n;i++) { _comb[i].resize(i/2+1); } _comb[0][0] = 1; for(int i=1;i<=max_n;i++) { _comb[i][0] = 1; for(int j=1;j<=i/2;j++) { _comb[i][j] = (_comb[i-1][j-1] + _comb[i-1][j<=(i-1)/2?j:i-1-j]) % MOD; } } }
int operator()(int n, int k) { if(k < 0 || n < 0 || k > n) return 0; if(k > n/2) return _comb[n][n-k]; else return _comb[n][k]; } };
template<> class Combination<0> { vector<vector<int>> _comb; public: Combination(int max_n) { _comb.resize(max_n+1, vector<int>()); for(int i=0;i<=max_n;i++) { _comb[i].resize(i/2+1); } _comb[0][0] = 1; for(int i=1;i<=max_n;i++) { _comb[i][0] = 1; for(int j=1;j<=i/2;j++) { _comb[i][j] = (_comb[i-1][j-1] + _comb[i-1][j<=(i-1)/2?j:i-1-j]); } } }
int operator()(int n, int k) { if(k < 0 || n < 0 || k > n) return 0; if(k > n/2) return _comb[n][n-k]; else return _comb[n][k]; } };
|