#include #include bool rowInX(int M, int N, int l, int k, const int *a, int row); bool columnInX(int M, int N, int l, int k, const int *a, int column); int main() { int M, N, l, k; // Task parameters int *a0 = nullptr; // Initial matrix int *a1 = nullptr; // Resulting matrix int *xRows = nullptr; // Indices of X-minor rows int numRows = 0; // Number of rows in the X-minor int *xColumns = nullptr; // Indices of X-minor columns int numColumns = 0; // Number of columns in the X-minor FILE *fin = fopen("data.txt", "r"); FILE *fout = fopen("res.txt", "w"); if (fin == NULL) { if (fout != NULL) { fprintf(fout, "ERROR\n"); fclose(fout); } return (-1); } if (fout == NULL) { fclose(fout); return (-1); } // Read the task parameters if (fscanf(fin, "%d%d%d%d", &M, &N, &l, &k) < 4) { fprintf(fout, "ERROR\n"); fclose(fin); fclose(fout); return (-1); } a0 = new int[l*k]; // Allocate a memory for the initial matrix and a1 = new int[l*k]; // for the resulting matrix for (int i = 0; i < l; ++i) { for (int j = 0; j < k; ++j) { // Read the matrix element if (fscanf(fin, "%d", a0 + i*k + j) < 1) { fprintf(fout, "ERROR\n"); fclose(fin); fclose(fout); delete[] a0; delete[] a1; return (-1); } // Copy the element from the initial matrix to the resulting a1[i*k + j] = a0[i*k + j]; } } fclose(fin); // Compute the set of rows of X-minor xRows = new int[l]; numRows = 0; for (int i = 0; i < l; ++i) { if (rowInX(M, N, l, k, a0, i)) { xRows[numRows] = i; ++numRows; } } // Compute the set of columns of X-minor xColumns = new int[k]; numColumns = 0; for (int j = 0; j < k; ++j) { if (columnInX(M, N, l, k, a0, j)) { xColumns[numColumns] = j; ++numColumns; } } // Replace elements of the X-minor in the resulting matrix a1 for (int ix = 0; ix < numRows; ++ix) { int i = xRows[ix]; assert(0 <= i && i < l); for (int jx = 0; jx < numColumns; ++jx) { int j = xColumns[jx]; assert(0 <= j && j < k); int elem = a0[i*k + j]; // Check whether it is the first such element in i-th row bool found = false; for (int jx1 = 0; jx1 < jx; ++jx1) { int j1 = xColumns[jx1]; if (a0[i*k + j1] == elem) { found = true; break; } } if (found) { a1[i*k + j] = 1; // It is NOT the first such element } else { a1[i*k + j] = 0; // It is the first such element } } } // Write the results fprintf(fout, "%d %d %d %d\n", M, N, l, k); for (int i = 0; i < l; ++i) { for (int j = 0; j < k; ++j) { if (j > 0) fprintf(fout, " "); // Write the delimiter fprintf(fout, "%d", a1[i*k + j]); } fprintf(fout, "\n"); } fclose(fout); delete[] a0; delete[] a1; delete[] xRows; delete[] xColumns; return 0; } bool rowInX(int M, int N, int l, int k, const int *a, int row) { assert(0 <= row && row < l); for (int j = 0; j < k; ++j) { if (a[row*k + j]%M == N) { return true; } } return false; } bool columnInX(int M, int N, int l, int k, const int *a, int column) { assert(0 <= column && column < k); for (int i = 0; i < l; ++i) { if (a[i*k + column]%M == N) { return true; } } return false; }