You are on page 1of 2

int**getCofactor(int** a, int p, int q, int n)

{
int** temp = new int* [n];
for (int i = 0; i < n; i++) temp[i] = new int[n];
if (n == 1) {
temp[0][0] = 1;
return 0;
}
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
temp[i][j++] = a[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}

return temp;
}

int** nghichdao(int** ka, int n)


{

int det = tinh_dinhthuc(n, ka);


int det_inv=0;
if (det <= 0) det_inv = 26 + det % 26;
else
{
for (int i = 1; i < 26; i++)
{
if ((det * i) % 26 == 1)
{
det_inv = i;
break;
}
}
}

int** a = new int* [n];


for (int i = 0; i < n; i++) a[i] = new int[i];
int** b;
int sign = 1;
if (n == 1) {
a[0][0] = 1;
return 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
b= getCofactor(ka, i, j, n);
sign = ((i + j) % 2 == 0) ? 1 : -1;
a[j][i] = (sign) * (tinh_dinhthuc(n - 1,b));
if(a[j][i]<0) a[j][i]=26+a[j][i]%26;
else if(a[j][i]>26) a[j][i]=a[j][i]%26;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[j][i] = (a[j][i]*det_inv)%26;

}
return a;
}

You might also like