/* 1) k = 1 -> O(n^2 log n) 2) k > 1 -> O(n! k) */ #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; inline int sub(int a, int b) { return a -= b, a < 0 ? a + mod : a; } inline int mul(int a, int b) { return int(a * 1LL * b % mod); } inline int pot(int a, int b) { int r = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) r = mul(r, a); return r; } inline uint64_t get_hash(const vector<int> &p) { uint64_t h = 0; for (int x : p) { h *= 4127; h += x + 1; } return h; } void brute(int n, int k) { vector ps(k, vector<int>(n, 0)); for (auto &p : ps) for (int &x : p) cin >> x, x--; vector<int> s(n); iota(s.begin(), s.end(), 0); set<uint64_t> vis; queue<vector<int>> q; q.push(s); vis.insert(get_hash(s)); long long sum = 0; while (q.size()) { vector<int> u = q.front(); q.pop(); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (u[i] > u[j]) sum++; for (auto &&p : ps) { vector<int> v(n, 0); for (int j = 0; j < n; j++) v[j] = u[p[j]]; uint64_t h = get_hash(v); if (vis.count(h) == 0) { vis.insert(h); q.push(v); } } } cerr << sum << ' ' << (int) vis.size() << '\n'; cout << mul(int(sum % mod), pot((int) vis.size(), mod - 2)) << '\n'; } const int L = 24; // 2*log2(N) const int N = 3000; int dp[L][N][N]; int kth[L][N]; int pot2[L]; int value(int i, int j, int k) { int a = i, b = j; int res = 0; for (int l = L - 1; l >= 0; l--) { if (k & (1 << l)) { if ((a < b) == (i < j)) res += dp[l][a][b]; else res += (1 << l) - dp[l][a][b]; a = kth[l][a]; b = kth[l][b]; } } return res; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int n, k; cin >> n >> k; if (k > 1) { brute(n, k); return 0; } assert(k == 1); vector<int> p(n); for (int &x : p) { cin >> x; x--; } vector<int> gdzie(n); // dokad tupta noca jez for (int i = 0; i < n; i++) { gdzie[p[i]] = i; } vector<int> len(n); vector<bool> vis(n); for (int i = 0; i < n; i++) { if (vis[i]) continue; vector<int> cycle; int u = i; while (vis[u] == false) { vis[u] = true; cycle.push_back(u); u = gdzie[u]; } for (int j : cycle) len[j] = (int) cycle.size(); } for (int i = 0; i < n; i++) kth[0][i] = gdzie[i]; for (int i = 1; i < L; i++) for (int j = 0; j < n; j++) kth[i][j] = kth[i - 1][kth[i - 1][j]]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dp[0][i][j] = 1; for (int pot = 1; pot < L; pot++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) if (i != j) { int a = kth[pot - 1][i]; int b = kth[pot - 1][j]; dp[pot][i][j] = dp[pot - 1][i][j]; if ((a < b) == (i < j)) dp[pot][i][j] += dp[pot - 1][a][b]; else dp[pot][i][j] += (1 << (pot - 1)) - dp[pot - 1][a][b]; } } } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int l = len[i] * len[j] / gcd(len[i], len[j]); if (p[i] > p[j]) (ans += mul(value(i, j, l - 1), pot(l, mod - 2))) %= mod; else (ans += mul(sub(l - 1, value(i, j, l - 1)), pot(l, mod - 2))) %= mod; } } cout << ans << '\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 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | /* 1) k = 1 -> O(n^2 log n) 2) k > 1 -> O(n! k) */ #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; inline int sub(int a, int b) { return a -= b, a < 0 ? a + mod : a; } inline int mul(int a, int b) { return int(a * 1LL * b % mod); } inline int pot(int a, int b) { int r = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) r = mul(r, a); return r; } inline uint64_t get_hash(const vector<int> &p) { uint64_t h = 0; for (int x : p) { h *= 4127; h += x + 1; } return h; } void brute(int n, int k) { vector ps(k, vector<int>(n, 0)); for (auto &p : ps) for (int &x : p) cin >> x, x--; vector<int> s(n); iota(s.begin(), s.end(), 0); set<uint64_t> vis; queue<vector<int>> q; q.push(s); vis.insert(get_hash(s)); long long sum = 0; while (q.size()) { vector<int> u = q.front(); q.pop(); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (u[i] > u[j]) sum++; for (auto &&p : ps) { vector<int> v(n, 0); for (int j = 0; j < n; j++) v[j] = u[p[j]]; uint64_t h = get_hash(v); if (vis.count(h) == 0) { vis.insert(h); q.push(v); } } } cerr << sum << ' ' << (int) vis.size() << '\n'; cout << mul(int(sum % mod), pot((int) vis.size(), mod - 2)) << '\n'; } const int L = 24; // 2*log2(N) const int N = 3000; int dp[L][N][N]; int kth[L][N]; int pot2[L]; int value(int i, int j, int k) { int a = i, b = j; int res = 0; for (int l = L - 1; l >= 0; l--) { if (k & (1 << l)) { if ((a < b) == (i < j)) res += dp[l][a][b]; else res += (1 << l) - dp[l][a][b]; a = kth[l][a]; b = kth[l][b]; } } return res; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int n, k; cin >> n >> k; if (k > 1) { brute(n, k); return 0; } assert(k == 1); vector<int> p(n); for (int &x : p) { cin >> x; x--; } vector<int> gdzie(n); // dokad tupta noca jez for (int i = 0; i < n; i++) { gdzie[p[i]] = i; } vector<int> len(n); vector<bool> vis(n); for (int i = 0; i < n; i++) { if (vis[i]) continue; vector<int> cycle; int u = i; while (vis[u] == false) { vis[u] = true; cycle.push_back(u); u = gdzie[u]; } for (int j : cycle) len[j] = (int) cycle.size(); } for (int i = 0; i < n; i++) kth[0][i] = gdzie[i]; for (int i = 1; i < L; i++) for (int j = 0; j < n; j++) kth[i][j] = kth[i - 1][kth[i - 1][j]]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dp[0][i][j] = 1; for (int pot = 1; pot < L; pot++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) if (i != j) { int a = kth[pot - 1][i]; int b = kth[pot - 1][j]; dp[pot][i][j] = dp[pot - 1][i][j]; if ((a < b) == (i < j)) dp[pot][i][j] += dp[pot - 1][a][b]; else dp[pot][i][j] += (1 << (pot - 1)) - dp[pot - 1][a][b]; } } } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int l = len[i] * len[j] / gcd(len[i], len[j]); if (p[i] > p[j]) (ans += mul(value(i, j, l - 1), pot(l, mod - 2))) %= mod; else (ans += mul(sub(l - 1, value(i, j, l - 1)), pot(l, mod - 2))) %= mod; } } cout << ans << '\n'; } |