/* * C++ Program to Compute Combinations using Matrix Multiplication */ #include #define ll long long using namespace std; // Matrix C;ass template class Matrix { public: int m, n; T *data; Matrix (int m, int n) ; Matrix (const Matrix &matrix); const Matrix &operator=(const Matrix &A); const Matrix operator*(const Matrix &A); const Matrix operator^(int P); ~Matrix(); }; //Constructor template Matrix ::Matrix(int m, int n) { this->m = m; this->n = n; data = new T[m * n]; } //Constructor template Matrix ::Matrix (const Matrix &A) { this->m = A.m; this->n = A.n; data = new T[m * n]; for (int i = 0; i < m * n; i++) data[i] = A.data[i]; } //Destructor template Matrix ::~Matrix() { delete [] data; } template const Matrix &Matrix ::operator=(const Matrix &A) { if (&A != this) { delete [] data; m = A.m; n = A.n; data = new T[m * n]; for (int i = 0; i < m * n; i++) data[i] = A.data[i]; } return *this; } template const Matrix Matrix ::operator*(const Matrix &A) { Matrix C (m, A.n); for (int i = 0; i < m; ++i) { for (int j = 0; j < A.n; ++j) { C.data[i * C.n + j] = 0; for (int k = 0; k < n; ++k) C.data[i * C.n + j] = C.data[i * C.n + j] + (data[i * n + k] * A.data[k * A.n + j]); } } return C; } template const Matrix Matrix ::operator^(int P) { if (P == 1) return (*this); if (P & 1) return (*this) * ((*this) ^ (P - 1)); Matrix B = (*this) ^ (P/2); return B * B; } //Compute Combinations ll C(int n, int r) { Matrix M(r + 1,r + 1); for (int i = 0; i < (r + 1) * (r + 1); i++) M.data[i] = 0; M.data[0] = 1; for (int i = 1; i < r + 1; i++) { M.data[i * (r + 1) + i - 1] = 1; M.data[i * (r + 1) + i] = 1; } return (M ^ n).data[r * (r + 1)]; } //Main int main() { int n, r, m; while (1) { cout<<"Enter total number of objects:(0 to exit) "; cin>>n; if (n == 0) break; cout<<"Enter number of objects to be chosen: "; cin>>r; cout<<"Number of Combinations: "<