#include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; int expo(int a, int n) { int ans = 1; while (n) { if (n & 1) { ans = ((long long) ans * a) % MOD; } n >>= 1; a = ((long long) a * a) % MOD; } return ans; } int revMod(int a) { return expo(a, MOD - 2); } int solve(int n, vector <vector<int>> &perm) { vector <int> comp(n * n), compSize(n * n, 1); vector <vector<int>> inComp(n * n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) { int v = n * i + j; comp[v] = v; inComp[v].push_back(v); } for (auto &p : perm) { for (int i = 0; i < n; i++) { int si = n * i, spi = n * p[i]; for (int j = 0; j < n; j++) if (i != j) { int x = comp[si + j], y = comp[spi + p[j]]; if (x != y) { if (compSize[x] > compSize[y]) { swap(x, y); } compSize[y] += compSize[x]; for (int v : inComp[x]) { comp[v] = y; inComp[y].push_back(v); } inComp[x] = vector <int> (); } } } } vector <int> numAll(n * n, 0), numOrd(n * n, 0); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) { int r = comp[n * i + j]; numAll[r]++; numOrd[r] += i < j; } int ans = 0; for (int r = 0; r < n * n; r++) if (numAll[r]) { int prod = ((long long) numOrd[r] * (numAll[r] - numOrd[r])) % MOD; ans = (ans + (long long) prod * revMod(numAll[r])) % MOD; } return ans; } int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector <vector<int>> perm(k, vector <int> (n)); for (int i = 0; i < k; i++) for (int j = 0; j < n; j++) { cin >> perm[i][j]; perm[i][j]--; } cout << solve(n, perm); return 0; }
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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; int expo(int a, int n) { int ans = 1; while (n) { if (n & 1) { ans = ((long long) ans * a) % MOD; } n >>= 1; a = ((long long) a * a) % MOD; } return ans; } int revMod(int a) { return expo(a, MOD - 2); } int solve(int n, vector <vector<int>> &perm) { vector <int> comp(n * n), compSize(n * n, 1); vector <vector<int>> inComp(n * n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) { int v = n * i + j; comp[v] = v; inComp[v].push_back(v); } for (auto &p : perm) { for (int i = 0; i < n; i++) { int si = n * i, spi = n * p[i]; for (int j = 0; j < n; j++) if (i != j) { int x = comp[si + j], y = comp[spi + p[j]]; if (x != y) { if (compSize[x] > compSize[y]) { swap(x, y); } compSize[y] += compSize[x]; for (int v : inComp[x]) { comp[v] = y; inComp[y].push_back(v); } inComp[x] = vector <int> (); } } } } vector <int> numAll(n * n, 0), numOrd(n * n, 0); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) { int r = comp[n * i + j]; numAll[r]++; numOrd[r] += i < j; } int ans = 0; for (int r = 0; r < n * n; r++) if (numAll[r]) { int prod = ((long long) numOrd[r] * (numAll[r] - numOrd[r])) % MOD; ans = (ans + (long long) prod * revMod(numAll[r])) % MOD; } return ans; } int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector <vector<int>> perm(k, vector <int> (n)); for (int i = 0; i < k; i++) for (int j = 0; j < n; j++) { cin >> perm[i][j]; perm[i][j]--; } cout << solve(n, perm); return 0; } |