#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define vec vector #define pb push_back #define f first #define s second #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define pint pair<int, int> using namespace std; const ll M = 1e9+7; ll inv(ll a) { return a <= 1 ? a : M - (long long)(M/a) * inv(M % a) % M; } const int N = 4000; int n; int parent[N*N]; int rnk[N*N]; ll card[N*N]; ll iv[N*N]; // how many pairs of first > second int get_parent(int x) { if (parent[x] == x) return x; parent[x] = get_parent(parent[x]); return parent[x]; } void mrg(int x, int y){ x = get_parent(x); y = get_parent(y); if(x==y) return; if (rnk[x] < rnk[y]) swap(x, y); if(rnk[x] == rnk[y]) rnk[x] ++; card[x] += card[y]; iv[x] += iv[y]; parent[y] = x; } inline int int_from_pair(int x, int y){ return x*n+y; } inline pint pair_from_int(int x){ return {x/n, x%n}; } int perms[N][N]; int perm[N]; const int TRIES = 10; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int k; cin >> n >> k; foru(i, k){ foru(j, n){ cin >> perms[i][j]; perms[i][j]--; } } random_shuffle(perms, perms+k); foru(i, n*n){ parent[i]=i; rnk[i]=1; card[i]=1; pint p = pair_from_int(i); iv[i] = (p.f > p.s); } foru (k_, k) { foru(i, n) perm[i]=perms[k_][i]; vec<int> non_stable; foru(i, n) if(perm[i] != i) non_stable.pb(i); bool is_new = true; if(k<=30) goto out; is_new = false; foru(i, n) foru(_i, TRIES){ int j = rand()%n; if(get_parent(int_from_pair(i, j)) != get_parent(int_from_pair(perm[i], perm[j]))){ is_new = true; goto out; } } out: int idx = 0; if(!is_new) goto skip; foru(i, n) { if(idx != non_stable.size() && i == non_stable[idx]) { idx++; foru(j, n) { int a = int_from_pair(i, j); int b = int_from_pair(perm[i], perm[j]); mrg(a, b); } } else { for(auto j : non_stable){ int a = int_from_pair(i, j); int b = int_from_pair(perm[i], perm[j]); mrg(a, b); } } } skip:; } ll ans = 0; foru(i, n) fors(j, n, i+1) { int idx = int_from_pair(i, j); idx = get_parent(idx); ans += iv[idx]*inv(card[idx]); ans = ans%M; } cout << ans; 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define vec vector #define pb push_back #define f first #define s second #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define pint pair<int, int> using namespace std; const ll M = 1e9+7; ll inv(ll a) { return a <= 1 ? a : M - (long long)(M/a) * inv(M % a) % M; } const int N = 4000; int n; int parent[N*N]; int rnk[N*N]; ll card[N*N]; ll iv[N*N]; // how many pairs of first > second int get_parent(int x) { if (parent[x] == x) return x; parent[x] = get_parent(parent[x]); return parent[x]; } void mrg(int x, int y){ x = get_parent(x); y = get_parent(y); if(x==y) return; if (rnk[x] < rnk[y]) swap(x, y); if(rnk[x] == rnk[y]) rnk[x] ++; card[x] += card[y]; iv[x] += iv[y]; parent[y] = x; } inline int int_from_pair(int x, int y){ return x*n+y; } inline pint pair_from_int(int x){ return {x/n, x%n}; } int perms[N][N]; int perm[N]; const int TRIES = 10; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int k; cin >> n >> k; foru(i, k){ foru(j, n){ cin >> perms[i][j]; perms[i][j]--; } } random_shuffle(perms, perms+k); foru(i, n*n){ parent[i]=i; rnk[i]=1; card[i]=1; pint p = pair_from_int(i); iv[i] = (p.f > p.s); } foru (k_, k) { foru(i, n) perm[i]=perms[k_][i]; vec<int> non_stable; foru(i, n) if(perm[i] != i) non_stable.pb(i); bool is_new = true; if(k<=30) goto out; is_new = false; foru(i, n) foru(_i, TRIES){ int j = rand()%n; if(get_parent(int_from_pair(i, j)) != get_parent(int_from_pair(perm[i], perm[j]))){ is_new = true; goto out; } } out: int idx = 0; if(!is_new) goto skip; foru(i, n) { if(idx != non_stable.size() && i == non_stable[idx]) { idx++; foru(j, n) { int a = int_from_pair(i, j); int b = int_from_pair(perm[i], perm[j]); mrg(a, b); } } else { for(auto j : non_stable){ int a = int_from_pair(i, j); int b = int_from_pair(perm[i], perm[j]); mrg(a, b); } } } skip:; } ll ans = 0; foru(i, n) fors(j, n, i+1) { int idx = int_from_pair(i, j); idx = get_parent(idx); ans += iv[idx]*inv(card[idx]); ans = ans%M; } cout << ans; return 0; } |