// clang-format off #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(auto i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif // clang-format on // clang-format off const int MOD = 1000000007; int add(const int& a, const int& b) { return (a + b >= MOD ? a + b - MOD : a + b); } int mul(const int& a, const int& b) { return 1LL * a * b % MOD; } int fast_pow(int base, int exp); // clang-format on // Global variables int nlength, nbase_perms, total_invs = 0; vector<vector<int>> perms; map<vector<int>, int> perm_to_id; vector<int> to_study; int count_inv(const vector<int>& perm); void add_perm(const vector<int>& perm); vector<int> apply_perm(const vector<int>& a, const vector<int>& b); int main() { cin.tie(0)->sync_with_stdio(0); cin >> nlength >> nbase_perms; vector<vector<int>> base_perms(nbase_perms, vector<int>(nlength)); REP (i, nbase_perms) { for (auto& x : base_perms[ssize(perms)]) { cin >> x; --x; } add_perm(base_perms[ssize(perms)]); } nbase_perms = ssize(perms); base_perms.resize(nbase_perms); debug(base_perms); // Generate all permutations in a DFS manner while (!to_study.empty()) { int id = to_study.back(); to_study.pop_back(); REP (i, nbase_perms) add_perm(apply_perm(perms[id], base_perms[i])); } int q = fast_pow(ssize(perms), MOD - 2); cout << mul(total_invs, q) << "\n"; return 0; } void add_perm(const vector<int>& perm) { if (perm_to_id.contains(perm)) return; perm_to_id[perm] = ssize(perms); to_study.emplace_back(ssize(perms)); perms.emplace_back(perm); const int& inv_cnt = count_inv(perm); total_invs = add(total_invs, inv_cnt); debug(perm, inv_cnt); } // Counts the number of permutation inversions O(n log n) int count_inv(const vector<int>& perm) { // Allocate a segment tree int base = 1; while (base <= ssize(perm)) base <<= 1; vector<int> sum(base << 1); // Calculate the number of inversions int res = 0; for (const auto& x : perm) { // Add to res the number of guys > x { int l = base + x, r = base + ssize(perm); while ((l >> 1) != (r >> 1)) { if (!(l & 1)) res += sum[l + 1]; if (r & 1) res += sum[r - 1]; l >>= 1, r >>= 1; } } // Add 1 to x for (int v = base + x; v >= 1; v >>= 1) sum[v] += 1; } return res; } int fast_pow(int base, int exp) { int res = 1; while (exp) { if (exp & 1) res = mul(res, base); base = mul(base, base); exp >>= 1; } return res; } vector<int> apply_perm(const vector<int>& a, const vector<int>& b) { vector<int> res(nlength); REP (i, nlength) res[i] = b[a[i]]; return res; }
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 | // clang-format off #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(auto i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif // clang-format on // clang-format off const int MOD = 1000000007; int add(const int& a, const int& b) { return (a + b >= MOD ? a + b - MOD : a + b); } int mul(const int& a, const int& b) { return 1LL * a * b % MOD; } int fast_pow(int base, int exp); // clang-format on // Global variables int nlength, nbase_perms, total_invs = 0; vector<vector<int>> perms; map<vector<int>, int> perm_to_id; vector<int> to_study; int count_inv(const vector<int>& perm); void add_perm(const vector<int>& perm); vector<int> apply_perm(const vector<int>& a, const vector<int>& b); int main() { cin.tie(0)->sync_with_stdio(0); cin >> nlength >> nbase_perms; vector<vector<int>> base_perms(nbase_perms, vector<int>(nlength)); REP (i, nbase_perms) { for (auto& x : base_perms[ssize(perms)]) { cin >> x; --x; } add_perm(base_perms[ssize(perms)]); } nbase_perms = ssize(perms); base_perms.resize(nbase_perms); debug(base_perms); // Generate all permutations in a DFS manner while (!to_study.empty()) { int id = to_study.back(); to_study.pop_back(); REP (i, nbase_perms) add_perm(apply_perm(perms[id], base_perms[i])); } int q = fast_pow(ssize(perms), MOD - 2); cout << mul(total_invs, q) << "\n"; return 0; } void add_perm(const vector<int>& perm) { if (perm_to_id.contains(perm)) return; perm_to_id[perm] = ssize(perms); to_study.emplace_back(ssize(perms)); perms.emplace_back(perm); const int& inv_cnt = count_inv(perm); total_invs = add(total_invs, inv_cnt); debug(perm, inv_cnt); } // Counts the number of permutation inversions O(n log n) int count_inv(const vector<int>& perm) { // Allocate a segment tree int base = 1; while (base <= ssize(perm)) base <<= 1; vector<int> sum(base << 1); // Calculate the number of inversions int res = 0; for (const auto& x : perm) { // Add to res the number of guys > x { int l = base + x, r = base + ssize(perm); while ((l >> 1) != (r >> 1)) { if (!(l & 1)) res += sum[l + 1]; if (r & 1) res += sum[r - 1]; l >>= 1, r >>= 1; } } // Add 1 to x for (int v = base + x; v >= 1; v >>= 1) sum[v] += 1; } return res; } int fast_pow(int base, int exp) { int res = 1; while (exp) { if (exp & 1) res = mul(res, base); base = mul(base, base); exp >>= 1; } return res; } vector<int> apply_perm(const vector<int>& a, const vector<int>& b) { vector<int> res(nlength); REP (i, nlength) res[i] = b[a[i]]; return res; } |