#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define INF 1000000000 #define INFl 1000000000000000000 #define all(x) x.begin(), x.end() #define sajz(x) (int)x.size() #define pb push_back #define se second #define fi first #define yes puts("YES") #define no puts("NO") #define erase_duplicates(x) {sort(all(x));(x).resize(distance((x).begin(), unique(all(x))));} using namespace std; using namespace __gnu_pbds; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif //#define int ll typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef set<int> si; typedef multiset<int> msi; typedef long double ld; typedef cc_hash_table<size_t, bool, hash<size_t>> ht; const int mod = 1e9 + 7; // Schreier–Sims algorithm uzywany z : https://github.com/ShahjalalShohag/code-library/blob/main/Miscellaneous/Schreier–Sims%20algorithm.cpp vi inv(vi& p){ vi ret(p.size()); for (int i = 0; i < (int)p.size(); i++) ret[p[i]] = i; return ret; } vi operator * (vi& a, vi& b){ vi ret(a.size()); for (int i = 0 ; i < (int)a.size(); i++) ret[i] = b[a[i]]; return ret; } // a group contains all subset products of generators struct Group { int n, m; vector<vi> lookup; vector<vector<vi>> buckets, ibuckets; int yo(vi p, bool add_to_group = 1){ n = buckets.size(); for (int i = 0; i < n ; i++){ int res = lookup[i][p[i]]; if (res == -1 ){ if (add_to_group){ buckets[i].push_back(p); ibuckets[i].push_back(inv(p)); lookup[i][p[i]] = buckets[i].size() - 1; } return i; } p = p * ibuckets[i][res]; } return -1; } ll size() { ll ret = 1; for (int i = 0; i < n; i++) ret *= buckets[i].size(); return ret; } bool in_group(vi g) { return yo(g, false) == -1; } Group(vector<vi> &gen, int _n){ n = _n, m = gen.size(); // m permutations of size n, 0 indexed lookup.resize(n); buckets.resize(n); ibuckets.resize(n); for (int i = 0; i < n ; i++){ lookup[i].resize(n); fill(lookup[i].begin(), lookup[i].end(), -1); } vi id(n); for (int i = 0; i < n ; i++) id[i] = i; for (int i = 0; i < n ; i++){ buckets[i].push_back(id); ibuckets[i].push_back(id); lookup[i][i] = 0; } for (int i = 0; i < m ; i++) yo(gen[i]); queue<pair<pair<int, int>,pair<int, int>>> q; for (int i = 0; i < n ; i++) { for (int j = i; j < n ; j++) { for (int k = 0; k < (int)buckets[i].size(); k++) { for (int l = 0; l < (int)buckets[j].size(); l++) { q.push({pair<int, int>(i, k), pair<int, int>(j, l)}); } } } } while(!q.empty()) { pair<int, int> a = q.front().first; pair<int, int> b = q.front().second; q.pop(); int res = yo(buckets[a.first][a.second] * buckets[b.first][b.second]); if (res == -1) continue; pair<int, int> cur(res, (int)buckets[res].size() - 1); for (int i = 0; i < n; i ++) { for (int j = 0; j < (int)buckets[i].size(); ++j){ if (i <= res) q.push(make_pair(pair<int, int>(i , j), cur)); if (res <= i) q.push(make_pair(cur, pair<int, int>(i, j))); } } } } }; // koniec kodu z https://github.com/ShahjalalShohag/code-library/blob/main/Miscellaneous/Schreier–Sims%20algorithm.cpp int bin_exp(int a, int b) { if (b == 0) { return 1; } if (b % 2) { return 1LL * bin_exp(a, b - 1) * a % mod; } int res = bin_exp(a, b / 2); return 1LL * res * res % mod; } int Inv(int a) { return bin_exp(a, mod - 2); } //int inver(vi &a) { //int n = sajz(a); //int ile = 0; //for (int i = 0; i < n; i ++) { //for (int j = i + 1; j < n; j ++) { //ile += (a[i] > a[j]); //} //} //return ile; //} const int MX = (1 << 13); int pods; int t[MX*2]; void add(int v, int x) { t[v += pods] = x; while (v/2) { v/=2; t[v] = t[v*2] + t[v*2+1]; } } int suma(int l, int r) { int res = 0; for (l += pods, r += pods; l < r; l >>= 1, r >>= 1) { if (l&1) res += t[l++]; if (r&1) res += t[--r]; } return res; } int inver(vi &a) { for (int i = 0; i <= 2*pods; i ++) t[i] = 0; int n = sajz(a); int res = 0; for (int i = 0; i < n; i ++) { res += suma(a[i]+1, n); add(a[i], 1); } return res; } // kod z : https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector struct Vec { vi vec; size_t operator()() { size_t seed = vec.size(); for(auto& i : vec) { seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2); } return seed; } }; int32_t main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, k; cin >> n >> k; pods = 1; while (pods <= n) pods*=2; if (k == 1) { Vec a; for (int i = 0; i < n; i ++) { int x; cin >> x; x --; a.vec.pb(x); } vi b = a.vec; ll sum = 0, cnt = 0; //map<size_t, bool> M; ht M; while (!M[a()]) { M[a()] = true; cnt ++; sum += inver(a.vec); sum %= mod; a.vec = a.vec * b; } ll up = sum % mod; ll down = Inv(cnt) % mod; ll ans = (up * down) % mod; // 625, 721 zamula //debug(cnt); cout << ans << '\n'; } else { vector<vi> base; while (k--) { vi v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x - 1); } base.push_back(v); } Group g(base, n); int Group_size = g.size(); //debug(Group_size); vi perm(n); for (int i = 0; i < n; i ++) perm[i] = i; ll nom = 0, denom = 0; do { if (denom == Group_size) break; if (g.in_group(perm)) { // jest w grupie denom ++; nom += inver(perm); nom %= mod; } } while (next_permutation(all(perm))); ll ans = (nom * Inv(denom)) % mod; cout << ans << '\n'; } 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 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define INF 1000000000 #define INFl 1000000000000000000 #define all(x) x.begin(), x.end() #define sajz(x) (int)x.size() #define pb push_back #define se second #define fi first #define yes puts("YES") #define no puts("NO") #define erase_duplicates(x) {sort(all(x));(x).resize(distance((x).begin(), unique(all(x))));} using namespace std; using namespace __gnu_pbds; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif //#define int ll typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef set<int> si; typedef multiset<int> msi; typedef long double ld; typedef cc_hash_table<size_t, bool, hash<size_t>> ht; const int mod = 1e9 + 7; // Schreier–Sims algorithm uzywany z : https://github.com/ShahjalalShohag/code-library/blob/main/Miscellaneous/Schreier–Sims%20algorithm.cpp vi inv(vi& p){ vi ret(p.size()); for (int i = 0; i < (int)p.size(); i++) ret[p[i]] = i; return ret; } vi operator * (vi& a, vi& b){ vi ret(a.size()); for (int i = 0 ; i < (int)a.size(); i++) ret[i] = b[a[i]]; return ret; } // a group contains all subset products of generators struct Group { int n, m; vector<vi> lookup; vector<vector<vi>> buckets, ibuckets; int yo(vi p, bool add_to_group = 1){ n = buckets.size(); for (int i = 0; i < n ; i++){ int res = lookup[i][p[i]]; if (res == -1 ){ if (add_to_group){ buckets[i].push_back(p); ibuckets[i].push_back(inv(p)); lookup[i][p[i]] = buckets[i].size() - 1; } return i; } p = p * ibuckets[i][res]; } return -1; } ll size() { ll ret = 1; for (int i = 0; i < n; i++) ret *= buckets[i].size(); return ret; } bool in_group(vi g) { return yo(g, false) == -1; } Group(vector<vi> &gen, int _n){ n = _n, m = gen.size(); // m permutations of size n, 0 indexed lookup.resize(n); buckets.resize(n); ibuckets.resize(n); for (int i = 0; i < n ; i++){ lookup[i].resize(n); fill(lookup[i].begin(), lookup[i].end(), -1); } vi id(n); for (int i = 0; i < n ; i++) id[i] = i; for (int i = 0; i < n ; i++){ buckets[i].push_back(id); ibuckets[i].push_back(id); lookup[i][i] = 0; } for (int i = 0; i < m ; i++) yo(gen[i]); queue<pair<pair<int, int>,pair<int, int>>> q; for (int i = 0; i < n ; i++) { for (int j = i; j < n ; j++) { for (int k = 0; k < (int)buckets[i].size(); k++) { for (int l = 0; l < (int)buckets[j].size(); l++) { q.push({pair<int, int>(i, k), pair<int, int>(j, l)}); } } } } while(!q.empty()) { pair<int, int> a = q.front().first; pair<int, int> b = q.front().second; q.pop(); int res = yo(buckets[a.first][a.second] * buckets[b.first][b.second]); if (res == -1) continue; pair<int, int> cur(res, (int)buckets[res].size() - 1); for (int i = 0; i < n; i ++) { for (int j = 0; j < (int)buckets[i].size(); ++j){ if (i <= res) q.push(make_pair(pair<int, int>(i , j), cur)); if (res <= i) q.push(make_pair(cur, pair<int, int>(i, j))); } } } } }; // koniec kodu z https://github.com/ShahjalalShohag/code-library/blob/main/Miscellaneous/Schreier–Sims%20algorithm.cpp int bin_exp(int a, int b) { if (b == 0) { return 1; } if (b % 2) { return 1LL * bin_exp(a, b - 1) * a % mod; } int res = bin_exp(a, b / 2); return 1LL * res * res % mod; } int Inv(int a) { return bin_exp(a, mod - 2); } //int inver(vi &a) { //int n = sajz(a); //int ile = 0; //for (int i = 0; i < n; i ++) { //for (int j = i + 1; j < n; j ++) { //ile += (a[i] > a[j]); //} //} //return ile; //} const int MX = (1 << 13); int pods; int t[MX*2]; void add(int v, int x) { t[v += pods] = x; while (v/2) { v/=2; t[v] = t[v*2] + t[v*2+1]; } } int suma(int l, int r) { int res = 0; for (l += pods, r += pods; l < r; l >>= 1, r >>= 1) { if (l&1) res += t[l++]; if (r&1) res += t[--r]; } return res; } int inver(vi &a) { for (int i = 0; i <= 2*pods; i ++) t[i] = 0; int n = sajz(a); int res = 0; for (int i = 0; i < n; i ++) { res += suma(a[i]+1, n); add(a[i], 1); } return res; } // kod z : https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector struct Vec { vi vec; size_t operator()() { size_t seed = vec.size(); for(auto& i : vec) { seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2); } return seed; } }; int32_t main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, k; cin >> n >> k; pods = 1; while (pods <= n) pods*=2; if (k == 1) { Vec a; for (int i = 0; i < n; i ++) { int x; cin >> x; x --; a.vec.pb(x); } vi b = a.vec; ll sum = 0, cnt = 0; //map<size_t, bool> M; ht M; while (!M[a()]) { M[a()] = true; cnt ++; sum += inver(a.vec); sum %= mod; a.vec = a.vec * b; } ll up = sum % mod; ll down = Inv(cnt) % mod; ll ans = (up * down) % mod; // 625, 721 zamula //debug(cnt); cout << ans << '\n'; } else { vector<vi> base; while (k--) { vi v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x - 1); } base.push_back(v); } Group g(base, n); int Group_size = g.size(); //debug(Group_size); vi perm(n); for (int i = 0; i < n; i ++) perm[i] = i; ll nom = 0, denom = 0; do { if (denom == Group_size) break; if (g.in_group(perm)) { // jest w grupie denom ++; nom += inver(perm); nom %= mod; } } while (next_permutation(all(perm))); ll ans = (nom * Inv(denom)) % mod; cout << ans << '\n'; } return 0; } |