#include<iostream> #include<vector> #include<random> #include<algorithm> using namespace std; using ll = long long; constexpr ll mod = 1'000'000'007; constexpr int mn = 3'030; constexpr int pn = mn * mn; constexpr ll szpotmod(ll a, ll b) { if (b == 0) return 1; ll p = szpotmod(a, b / 2); p = (p * p) % mod; if (b & 1) { p = (p * a) % mod; } return p; } constexpr ll odw(ll a) { return szpotmod(a, mod - 2); } bool vis[mn][mn]; int pers[mn][mn]; int stackx[pn]; int stacky[pn]; int p = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, k; cin >> n >> k; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { vis[i][j] = false; } } for (int i = 0; i < k; i++) { for(int j = 1; j <= n; j++) { cin >> pers[j][i]; } } ll res = 0; ll sor = 0; ll inw = 0; ll pom = 0; bool sym = false; ll r; int x, y, u, v; for(int i = 1; i <= n; i++) { for(int j = i + 1; j <= n; j++) { if (vis[i][j] || vis[j][i]) continue; sor = 0; inw = 0; pom = 0; sym = false; vis[i][j] = true; stackx[p] = i; stacky[p] = j; p++; while (p) { p--; x = stackx[p]; y = stacky[p]; if (x < y) sor++; else inw++; pom++; for(int w = 0; w < k; w++) { u = pers[x][w]; v = pers[y][w]; if (!vis[u][v]) { if (vis[v][u]) { sym = true; continue; } vis[u][v] = true; stackx[p] = u; stacky[p] = v; p++; } } } if (sym) r = pom * odw(2); else r = 2 * ((sor * inw) % mod) * odw(sor + inw); res = (res + r) % mod; } } cout << res << "\n"; }
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 | #include<iostream> #include<vector> #include<random> #include<algorithm> using namespace std; using ll = long long; constexpr ll mod = 1'000'000'007; constexpr int mn = 3'030; constexpr int pn = mn * mn; constexpr ll szpotmod(ll a, ll b) { if (b == 0) return 1; ll p = szpotmod(a, b / 2); p = (p * p) % mod; if (b & 1) { p = (p * a) % mod; } return p; } constexpr ll odw(ll a) { return szpotmod(a, mod - 2); } bool vis[mn][mn]; int pers[mn][mn]; int stackx[pn]; int stacky[pn]; int p = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, k; cin >> n >> k; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { vis[i][j] = false; } } for (int i = 0; i < k; i++) { for(int j = 1; j <= n; j++) { cin >> pers[j][i]; } } ll res = 0; ll sor = 0; ll inw = 0; ll pom = 0; bool sym = false; ll r; int x, y, u, v; for(int i = 1; i <= n; i++) { for(int j = i + 1; j <= n; j++) { if (vis[i][j] || vis[j][i]) continue; sor = 0; inw = 0; pom = 0; sym = false; vis[i][j] = true; stackx[p] = i; stacky[p] = j; p++; while (p) { p--; x = stackx[p]; y = stacky[p]; if (x < y) sor++; else inw++; pom++; for(int w = 0; w < k; w++) { u = pers[x][w]; v = pers[y][w]; if (!vis[u][v]) { if (vis[v][u]) { sym = true; continue; } vis[u][v] = true; stackx[p] = u; stacky[p] = v; p++; } } } if (sym) r = pom * odw(2); else r = 2 * ((sor * inw) % mod) * odw(sor + inw); res = (res + r) % mod; } } cout << res << "\n"; } |