-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompute Combinations using Matrix Multiplication.cpp
116 lines (109 loc) · 2.44 KB
/
Compute Combinations using Matrix Multiplication.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#define ll long long
using namespace std;
// Matrix C;ass
template <class T>
class Matrix
{
public:
int m, n;
T *data;
Matrix (int m, int n) ;
Matrix (const Matrix <T> &matrix);
const Matrix <T> &operator=(const Matrix<T> &A);
const Matrix <T> operator*(const Matrix<T> &A);
const Matrix <T> operator^(int P);
~Matrix();
};
//Constructor
template <class T>
Matrix <T>::Matrix(int m, int n)
{
this->m = m;
this->n = n;
data = new T[m * n];
}
//Constructor
template <class T>
Matrix <T>::Matrix (const Matrix <T> &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 <class T>
Matrix <T>::~Matrix()
{
delete [] data;
}
template <class T>
const Matrix <T> &Matrix <T>::operator=(const Matrix <T> &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 <class T>
const Matrix <T> Matrix <T>::operator*(const Matrix <T> &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 <class T>
const Matrix <T> Matrix <T>::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 <ll> 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: "<<C(n, min(r, n - r))<<endl;
}
}