fork download
  1. #include <vector>
  2. #include <bitset>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 1024;
  8.  
  9. int GetRank(vector<bitset<N>> matrix) {
  10. const int n = matrix.size();
  11. int r = 0;
  12. vector<bool> used(n);
  13.  
  14. for (int col = 0; col < n; ++col) {
  15. int pivot = -1;
  16. for (int row = 0; row < n; ++row) {
  17. if (!used[row] && matrix[row][col]) {
  18. pivot = row; break;
  19. }
  20. }
  21.  
  22. if (pivot == -1) { continue; }
  23. used[pivot] = true;
  24. ++r;
  25.  
  26. for (int r = 0; r < n; ++r) {
  27. if (r == pivot) { continue; }
  28. if (matrix[r][col]) {
  29. matrix[r] ^= matrix[pivot];
  30. }
  31. }
  32. }
  33. return r;
  34. }
  35.  
  36. int main() {
  37. int n;
  38. cin >> n;
  39.  
  40. srand(time(NULL));
  41.  
  42. for (int i = 0; i < 40; ++i) {
  43. int m = n + i;
  44. vector<bitset<N>> matrix(m);
  45. for (int i = 0; i < m; ++i) {
  46. for (int j = 0; j < m; ++j) {
  47. matrix[i][j] = rand() % 2;
  48. }
  49. }
  50.  
  51. cout << m << " "<< GetRank(matrix) << "\n";
  52. }
  53. }
  54.  
Success #stdin #stdout 0.58s 15472KB
stdin
900
stdout
900 496
901 527
902 496
903 527
904 496
905 527
906 496
907 527
908 496
909 527
910 496
911 527
912 496
913 527
914 496
915 527
916 496
917 527
918 496
919 527
920 496
921 527
922 496
923 527
924 496
925 527
926 496
927 527
928 496
929 527
930 496
931 527
932 496
933 527
934 496
935 527
936 496
937 527
938 496
939 527