Advertisement
a53

perspic

a53
Aug 29th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin( "perspic.in" );
  8. ofstream fout( "perspic.out" );
  9.  
  10. const int MaxN = 101;
  11. const int Mod = 13007;
  12. const int MaxNr = MaxN * MaxN;
  13.  
  14. int M[MaxN][MaxN], ok[MaxN][MaxN], lin[MaxNr], col[MaxN * MaxN];
  15. int maxp[MaxNr], mx;
  16.  
  17. static inline int quickExp( int a, int n ) {
  18. int p = 1;
  19.  
  20. while ( n > 0 ) {
  21. if ( n & 1 ) {
  22. p = (p * a) % Mod;
  23. }
  24. a = (a * a) % Mod;
  25. n >>= 1;
  26. }
  27. return p;
  28. }
  29. static inline void fact( int nr ) {
  30. int d = 2, p;
  31.  
  32. while ( d * d <= nr ) {
  33. p = 0;
  34. while ( nr % d == 0 ) {
  35. nr /= d;
  36. ++p;
  37. }
  38. if ( p > 0 ) {
  39. if ( d > mx ) {
  40. mx = d;
  41. }
  42. }
  43. maxp[d] = max( p, maxp[d] );
  44. ++d;
  45. }
  46. if ( nr > 1 ) {
  47. if ( nr > mx ) {
  48. mx = nr;
  49. }
  50. maxp[nr] = max( 1, maxp[nr] );
  51. }
  52. }
  53.  
  54. int main() {
  55. int n, m, i, j, nr, s, l, c, l1, c1, l2, c2, res;
  56. char ch;
  57.  
  58. fin >> n >> m;
  59. nr = 1;
  60. for ( i = 0; i < n; ++i ) {
  61. for ( j = 0; j < n; ++j ) {
  62. lin[nr] = i;
  63. col[nr] = j;
  64. M[i][j] = nr++;
  65. }
  66. }
  67. while ( isspace( ch = fin.get() ) );
  68. for ( i = 0; i < m; ++i ) {
  69. switch ( ch ) {
  70. case 'C':
  71. fin >> c1 >> c2;
  72. for ( j = 0; j < n; ++j ) {
  73. swap( M[j][c1 - 1], M[j][c2 - 1] );
  74. }
  75. break;
  76. case 'R':
  77. fin >> l1 >> l2;
  78. for ( j = 0; j < n; ++j ) {
  79. swap( M[l1 - 1][j], M[l2 - 1][j] );
  80. }
  81. break;
  82. case 'E':
  83. fin >> l1 >> c1 >> l2 >> c2;
  84. swap( M[l1 - 1][c1 - 1], M[l2 - 1][c2 - 1] );
  85. break;
  86. }
  87. while ( isspace( ch = fin.get() ) );
  88. }
  89. nr = 1;
  90. for ( i = 0; i < n; ++i ) {
  91. for ( j = 0; j < n; ++j ) {
  92. l = i;
  93. c = j;
  94. s = 0;
  95. while ( !ok[l][c] ) {
  96. l1 = l;
  97. ok[l][c] = 1;
  98. l = lin[M[l][c]];
  99. c = col[M[l1][c]];
  100. ++s;
  101. }
  102. if ( s ) {
  103. fact( s );
  104. }
  105. ++nr;
  106. }
  107. }
  108. res = 1;
  109. for ( i = 1; i <= mx; ++i ) {
  110. res = (res * quickExp( i, maxp[i] )) % Mod;
  111. }
  112. fout << res;
  113. fin.close();
  114. fout.close();
  115. return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement