#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; using ll = long long; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int mod = 1e9 + 7; ll bp(ll a, ll b) { ll r = 1; while(b) { if(b & 1) r = r * a % mod; b /= 2; a = a * a % mod; } return r; } int compute(int n, vector<int> a, vector<int> b) { int x = a.size(); int y = b.size(); ll r = 0; int g = gcd(x, y); int l = x * y / g; vector<int> elem(g); for(int diff = 0; diff < g; diff++) { for(int rep = 0; rep < l; rep++) { if(a[rep % x] > b[(diff + rep) % y]) elem[diff]++; } elem[diff] *= g; } for(int diff = 0; diff < y; diff++) { ll pos = 0; for(int i = 0; i < x; i++) { int j = (i + diff) % y; if(a[i] < b[j]) pos++; } r += pos * elem[diff % g]; r %= mod; } return r * bp(x * y, mod - 2) % mod; } void solve_one(int n) { vector<int> u(n); vector<int> v(n); for(int i = 0; i < n; i++) { cin >> v[i]; v[i]--; } vector<vector<int>> cyc; for(int i = 0; i < n; i++) { if(u[i]) continue; int j = i; vector<int> c; while(!u[j]) { c.push_back({v[j]}); u[j] = 1; j = v[j]; } cyc.push_back(c); } int ans = 0; for(int i = 0; i < cyc.size(); i++) { for(int j = i; j < cyc.size(); j++) { ll x = compute(n, cyc[i], cyc[j]); if(i < j) x = 2 * x % mod; ans = (ans + x) % mod; } } cout << ans << endl; } vector<int> compose(const vector<int>& a, const vector<int>& b) { vector<int> c(a.size()); for(int i = 0; i < a.size(); i++) c[i] = a[b[i]]; return c; } void solve_brute(int n, int k) { vector<vector<int>> v(k); for(int i = 0; i < k; i++) { v[i].resize(n); for(int j = 0; j < n; j++) { cin >> v[i][j]; v[i][j]--; } } set<vector<int>> st; vector<int> start(n); iota(start.begin(), start.end(), 0); queue<vector<int>> q; q.push(start); st.insert(start); ll ans = 0; ll cnt = 0; while(!q.empty()) { auto x = q.front(); q.pop(); cnt++; if(cnt >= mod) cnt -= mod; for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(x[i] > x[j]) ans++; } } if(ans >= mod) ans -= mod; for(int i = 0; i < k; i++) { auto y = compose(x, v[i]); if(!st.contains(y)) { st.insert(y); q.push(y); } } } ans = ans * bp(cnt, mod - 2) % mod; cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; if(k == 1) { solve_one(n); return 0; } solve_brute(n, k); 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 145 146 147 148 149 150 151 152 153 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; using ll = long long; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int mod = 1e9 + 7; ll bp(ll a, ll b) { ll r = 1; while(b) { if(b & 1) r = r * a % mod; b /= 2; a = a * a % mod; } return r; } int compute(int n, vector<int> a, vector<int> b) { int x = a.size(); int y = b.size(); ll r = 0; int g = gcd(x, y); int l = x * y / g; vector<int> elem(g); for(int diff = 0; diff < g; diff++) { for(int rep = 0; rep < l; rep++) { if(a[rep % x] > b[(diff + rep) % y]) elem[diff]++; } elem[diff] *= g; } for(int diff = 0; diff < y; diff++) { ll pos = 0; for(int i = 0; i < x; i++) { int j = (i + diff) % y; if(a[i] < b[j]) pos++; } r += pos * elem[diff % g]; r %= mod; } return r * bp(x * y, mod - 2) % mod; } void solve_one(int n) { vector<int> u(n); vector<int> v(n); for(int i = 0; i < n; i++) { cin >> v[i]; v[i]--; } vector<vector<int>> cyc; for(int i = 0; i < n; i++) { if(u[i]) continue; int j = i; vector<int> c; while(!u[j]) { c.push_back({v[j]}); u[j] = 1; j = v[j]; } cyc.push_back(c); } int ans = 0; for(int i = 0; i < cyc.size(); i++) { for(int j = i; j < cyc.size(); j++) { ll x = compute(n, cyc[i], cyc[j]); if(i < j) x = 2 * x % mod; ans = (ans + x) % mod; } } cout << ans << endl; } vector<int> compose(const vector<int>& a, const vector<int>& b) { vector<int> c(a.size()); for(int i = 0; i < a.size(); i++) c[i] = a[b[i]]; return c; } void solve_brute(int n, int k) { vector<vector<int>> v(k); for(int i = 0; i < k; i++) { v[i].resize(n); for(int j = 0; j < n; j++) { cin >> v[i][j]; v[i][j]--; } } set<vector<int>> st; vector<int> start(n); iota(start.begin(), start.end(), 0); queue<vector<int>> q; q.push(start); st.insert(start); ll ans = 0; ll cnt = 0; while(!q.empty()) { auto x = q.front(); q.pop(); cnt++; if(cnt >= mod) cnt -= mod; for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(x[i] > x[j]) ans++; } } if(ans >= mod) ans -= mod; for(int i = 0; i < k; i++) { auto y = compose(x, v[i]); if(!st.contains(y)) { st.insert(y); q.push(y); } } } ans = ans * bp(cnt, mod - 2) % mod; cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; if(k == 1) { solve_one(n); return 0; } solve_brute(n, k); return 0; } |