#include <iostream> #include <vector> #include <cassert> #include <unordered_map> using namespace std; const long long modd = 1'000'000'007; long long mpow(long long x, long long exp) { if(exp == 0) return 1; long long result = mpow(x, exp / 2); result = result * result % modd; if(exp & 1) result = result * x % modd; return result; } const long long mod = 1021043935909; unordered_map<long long, bool>m; vector<vector<int>>perms; vector<int> compose(vector<int>a, vector<int>b) { int n = a.size(); vector<int>c(n); for(int i = 0; i < n; i++) c[i] = a[b[i]]; return c; } int inv(vector<int>&v) { int n = v.size(), result = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { result += v[i] < v[j]; } } return result; } long long h(vector<int>&v) { int n = v.size(); long long result = 0; for(int x : v) result = (result * n + x) % mod; return result; } long long total_inv = 0; long long count = 0; void rec(vector<int>v) { long long hh = h(v); if(m[hh] == true) return; count++; total_inv += inv(v); m[hh] = true; for(auto p : perms) rec(compose(v, p)); } int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; perms.resize(k); for(int i = 0; i < k; i++) { perms[i].resize(n); for(int &x : perms[i]) { cin >> x; x--; } } vector<int>id(n); for(int i = 0; i < n; i++) id[i] = i; rec(id); cout << total_inv * mpow(count, modd - 2) % modd << "\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 | #include <iostream> #include <vector> #include <cassert> #include <unordered_map> using namespace std; const long long modd = 1'000'000'007; long long mpow(long long x, long long exp) { if(exp == 0) return 1; long long result = mpow(x, exp / 2); result = result * result % modd; if(exp & 1) result = result * x % modd; return result; } const long long mod = 1021043935909; unordered_map<long long, bool>m; vector<vector<int>>perms; vector<int> compose(vector<int>a, vector<int>b) { int n = a.size(); vector<int>c(n); for(int i = 0; i < n; i++) c[i] = a[b[i]]; return c; } int inv(vector<int>&v) { int n = v.size(), result = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++) { result += v[i] < v[j]; } } return result; } long long h(vector<int>&v) { int n = v.size(); long long result = 0; for(int x : v) result = (result * n + x) % mod; return result; } long long total_inv = 0; long long count = 0; void rec(vector<int>v) { long long hh = h(v); if(m[hh] == true) return; count++; total_inv += inv(v); m[hh] = true; for(auto p : perms) rec(compose(v, p)); } int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; perms.resize(k); for(int i = 0; i < k; i++) { perms[i].resize(n); for(int &x : perms[i]) { cin >> x; x--; } } vector<int>id(n); for(int i = 0; i < n; i++) id[i] = i; rec(id); cout << total_inv * mpow(count, modd - 2) % modd << "\n"; } |