#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int Q = 1'000'000'007; void dod(int &x, int y) { x += y; if (x > Q) x -= Q; } int mn(long long x, int y) { return (x * y) % Q; } int odw(int x) { int n = Q-2; int ret = 1; while (n) { if (n % 2) ret = mn(ret, x); x = mn(x, x); n /= 2; } return ret; } const int N = 3'007; int n, k; int p[N][N]; bool c[N][N]; pair<int, int> e[N*N]; void szuk(int i, int j, pair<int, int>& ret) { c[i][j] = 1; e[0] = {i, j}; for (int r = 0, q = 1; r < q; r++) { i = e[r].first; j = e[r].second; if (i < j) ret.first++; else ret.second++; for (int x = 0; x < k; x++) { int a = p[i][x], b = p[j][x]; if (!c[a][b]) { c[a][b] = 1; e[q++] = {a, b}; } } } } int main() { scanf("%d %d", &n, &k); for (int i = 0; i < k; i++) for (int j = 1; j <= n; j++) scanf("%d", &p[j][i]); int ret = 0; for (int i = 1; i <= n; i++) for(int j = i+1; j <= n; j++) { if (!c[i][j] && !c[j][i]) { pair<int, int> a; szuk(i, j, a); dod(ret, mn(mn(a.first, a.second), odw(a.first + a.second))); if (!c[j][i]) { dod(ret, mn(mn(a.first, a.second), odw(a.first + a.second))); } } } printf("%d\n", ret); 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int Q = 1'000'000'007; void dod(int &x, int y) { x += y; if (x > Q) x -= Q; } int mn(long long x, int y) { return (x * y) % Q; } int odw(int x) { int n = Q-2; int ret = 1; while (n) { if (n % 2) ret = mn(ret, x); x = mn(x, x); n /= 2; } return ret; } const int N = 3'007; int n, k; int p[N][N]; bool c[N][N]; pair<int, int> e[N*N]; void szuk(int i, int j, pair<int, int>& ret) { c[i][j] = 1; e[0] = {i, j}; for (int r = 0, q = 1; r < q; r++) { i = e[r].first; j = e[r].second; if (i < j) ret.first++; else ret.second++; for (int x = 0; x < k; x++) { int a = p[i][x], b = p[j][x]; if (!c[a][b]) { c[a][b] = 1; e[q++] = {a, b}; } } } } int main() { scanf("%d %d", &n, &k); for (int i = 0; i < k; i++) for (int j = 1; j <= n; j++) scanf("%d", &p[j][i]); int ret = 0; for (int i = 1; i <= n; i++) for(int j = i+1; j <= n; j++) { if (!c[i][j] && !c[j][i]) { pair<int, int> a; szuk(i, j, a); dod(ret, mn(mn(a.first, a.second), odw(a.first + a.second))); if (!c[j][i]) { dod(ret, mn(mn(a.first, a.second), odw(a.first + a.second))); } } } printf("%d\n", ret); return 0; } |