#include <bits/stdc++.h> using namespace std; // loops #define FOR_3(i, a, b) for (int i = (a), bb##i = (b); i <= bb##i; ++i) #define FORR_3(i, a, b) for (int i = (b), aa##i = (a); i >= aa##i; --i) #define FOR_2(i, n) FOR_3(i, 0, (n)-1) #define FORR_2(i, n) FORR_3(i, 0, (n)-1) #define FOR_1(n) FOR_2(_i##__LINE__, n) #define FOR_0() for (;;) #define OVERLOAD(_0, _1, _2, _3, NAME, ...) NAME #define FOR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FOR_3, FOR_2, FOR_1, FOR_0) \ (__VA_ARGS__) #define FORR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FORR_3, FORR_2, FORR_1, FOR_0) \ (__VA_ARGS__) // in - expr style template <class T> T read() { T x; cin >> x; return x; } #define II read<int>() #define ILL read<ll>() #define ISTR read<string>() // in - macro style void _I() {} template <class ARG, class... ARGS> void _I(ARG &&arg, ARGS &&...args) { cin >> arg; _I(forward<ARGS>(args)...); } #define I(...) _I(__VA_ARGS__); // out template <class T> void _print(T x, ostream &os = cout) { os << x; bool space = true; if constexpr (is_same_v<T, char>) if (x == '\n') space = false; if (space) os << " "; } // template <class T> void _print(const vector<T> &d, ostream &os = cout) { // os << '['; // for (auto &e : d) // os << e << " "; // os << ']'; // } void _O() {} template <class ARG, class... ARGS> void _O(ARG &&arg, ARGS &&...args) { _print(arg); _O(forward<ARGS>(args)...); } #define O(...) _O(__VA_ARGS__ __VA_OPT__(, ) '\n'); #define OO(...) _O(__VA_ARGS__); // err const int _indentSize = 2; string _indent; void _E() { cerr << endl; } template <class ARG, class... ARGS> void _E(ARG &&arg, ARGS &&...args) { _print(forward<ARG>(arg), cerr); _E(std::forward<ARGS>(args)...); } #define ERR(...) \ { \ cerr << _indent << "[" << __LINE__ << "] "; \ _E(__VA_ARGS__); \ } #define _V5(x) #x, "==", (x) #define _V4(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V5(__VA_ARGS__)) #define _V3(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V4(__VA_ARGS__)) #define _V2(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V3(__VA_ARGS__)) #define _V1(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V2(__VA_ARGS__)) #define ERRV(...) ERR(_V1(__VA_ARGS__)) #define INDENT auto _indenter = Indenter(); struct Indenter { Indenter() { FOR(_indentSize) _indent.push_back(' '); } ~Indenter() { FOR(_indentSize) _indent.pop_back(); } }; #ifdef DEBUG #define FAIL(str) \ { \ cerr << "ASSERT FAILED " << str << " " << __FILE__ << ':' << __LINE__ \ << endl; \ abort(); \ } #else #define FAIL(x) \ {} #endif // asserts #define CHECK(cond) \ if (!(cond)) \ FAIL(#cond) #define CHECK_OP(op, a, b, res) \ if (!(res)) \ FAIL(#a " " op " " #b " (" + to_string(a) + " vs " + to_string(b) + ")") #define CHECK_EQ(a, b) CHECK_OP("==", a, b, (a) == (b)) #define CHECK_NE(a, b) CHECK_OP("!=", a, b, (a) != (b)) #define CHECK_LE(a, b) CHECK_OP("<=", a, b, (a) <= (b)) #define CHECK_GE(a, b) CHECK_OP(">=", a, b, (a) >= (b)) #define CHECK_LT(a, b) CHECK_OP("<", a, b, (a) < (b)) #define CHECK_GT(a, b) CHECK_OP(">", a, b, (a) > (b)) #define CHECK_RANGE(x, a, b) \ if (!(a <= x && x < b)) \ FAIL(#x " in [" #a "," #b ")") // #define SZ(v) ((int)(v).size()) using pii = pair<int, int>; //////////////////////////////////////////////////////////////////////////////// // snippets/modulo.hpp // template <class T, class E> T fpow(T a, E exp) { T r = 1; while (exp) { if (exp & 1) { r *= a; --exp; } else { exp >>= 1; a *= a; } } return r; } template <int MOD> class Modulo { public: using H = int; H h = 0; public: Modulo() = default; Modulo(H x) : h((x + MOD) % MOD){CHECK_GE(x + MOD, 0)} Modulo(const Modulo &) = default; public: Modulo &operator+=(const Modulo &o) { h += o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator-=(const Modulo &o) { h = MOD + h - o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator*=(const Modulo &o) { h = (1ULL * h * o.h) % MOD; return *this; } Modulo &operator/=(const Modulo &o) { const auto TOTIENT = MOD - 1; *this *= fpow(o, TOTIENT - 1); return *this; } bool operator==(const Modulo &o) const { return h == o.h; } bool operator!=(const Modulo &o) const { return h != o.h; } Modulo operator+(const Modulo &o) const { return Modulo(*this) += o; } Modulo operator-(const Modulo &o) const { return Modulo(*this) -= o; } Modulo operator*(const Modulo &o) const { return Modulo(*this) *= o; } Modulo operator/(const Modulo &o) const { return Modulo(*this) /= o; } Modulo &operator--() { if (h == 0) h = MOD; --h; return *this; } Modulo &operator++() { ++h; if (h == MOD) h = 0; return *this; } Modulo operator--(int) { auto m = *this; --(*this); return m; } Modulo operator++(int) { auto m = *this; ++(*this); return m; } explicit operator H() const { return h; } }; using F = Modulo<int(1e9) + 7>; ostream &operator<<(ostream &os, const F &modulo) { return os << modulo.h; } // snippets/modulo.hpp //////////////////////////////////////////////////////////////////////////////// // using F = Modulo<998'244'353>; // time complexity : O(n^2 log^3 |G| + t n log |G|) // memory complexity : O(n^2 log |G| + tn) // t : number of generators // |G| : group size, obviously <= (n!) vector<int> inv(vector<int> &p) { vector<int> ret(p.size()); for (int i = 0; i < (int)p.size(); i++) ret[p[i]] = i; return ret; } vector<int> operator*(vector<int> &a, vector<int> &b) { vector<int> 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<vector<int>> lookup; vector<vector<vector<int>>> buckets, ibuckets; int yo(vector<int> 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; } F size() { F ret = 1; for (int i = 0; i < n; i++) ret *= buckets[i].size(); return ret; } bool in_group(vector<int> g) { return yo(g, false) == -1; } Group(vector<vector<int>> &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); } vector<int> 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))); } } } } }; int merge(vector<int> &arr, int temp[], int left, int mid, int right) { int i, j, k; int inv_count = 0; i = left; // i is index for left subarray j = mid; // j is index for right subarray k = left; // k is index for resultant merged subarray while ((i <= mid - 1) && (j <= right)) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; inv_count = inv_count + (mid - i); } } // Copy the remaining elements of left subarray (if there are any) to temp while (i <= mid - 1) { temp[k++] = arr[i++]; } // Copy the remaining elements of right subarray (if there are any) to temp while (j <= right) { temp[k++] = arr[j++]; } // Copy back the merged elements to original array for (i = left; i <= right; i++) { arr[i] = temp[i]; } return inv_count; } int mergeSort(vector<int> &arr, int temp[], int left, int right) { int mid, inv_count = 0; if (right > left) { // Divide the array into two parts and call _mergeSortAndCountInv() // for each of the parts mid = (right + left) / 2; // Inversion count will be sum of inversions in left-part, right-part // and number of inversions in merging inv_count += mergeSort(arr, temp, left, mid); inv_count += mergeSort(arr, temp, mid + 1, right); // Merge the two parts inv_count += merge(arr, temp, left, mid + 1, right); } return inv_count; } int count_inversions(vector<int> &perm) { vector<int> temp(perm.size()); return mergeSort(perm, temp.data(), 0, perm.size() - 1); } ostream &operator<<(ostream &os, const vector<int> &v) { // os << '['; for (auto &e : v) os << e << " "; // os << ']'; return os; } // set<vector<int>> all_perms; auto get_all(Group &g, int idx, vector<int> &perm) { if (idx == g.n) { // cerr << "perm " << perm << endl; // all_perms.insert(perm); return (F)count_inversions(perm); } F res = 0; for (int i = 0; i < (int)g.buckets[idx].size(); i++) { auto &p = g.buckets[idx][i]; auto next = p * perm; res += get_all(g, idx + 1, next); } return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int k, n; cin >> n >> k; vector<vector<int>> a; while (k--) { vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x - 1); } a.push_back(v); } Group g(a, n); auto sz = g.size(); cerr << "size " << sz << endl; vector<int> id(n); for (int i = 0; i < n; i++) id[i] = i; auto sum = get_all(g, 0, id); auto result = sum / sz; // cerr << "all perms size " << SZ(all_perms) << endl; O(result); return 0; } // https://codeforces.com/blog/entry/21335?locale=ru#comment-260437 // http://opencup.ru/files/ocg/gp5/problems1-e.pdf // problem H
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | #include <bits/stdc++.h> using namespace std; // loops #define FOR_3(i, a, b) for (int i = (a), bb##i = (b); i <= bb##i; ++i) #define FORR_3(i, a, b) for (int i = (b), aa##i = (a); i >= aa##i; --i) #define FOR_2(i, n) FOR_3(i, 0, (n)-1) #define FORR_2(i, n) FORR_3(i, 0, (n)-1) #define FOR_1(n) FOR_2(_i##__LINE__, n) #define FOR_0() for (;;) #define OVERLOAD(_0, _1, _2, _3, NAME, ...) NAME #define FOR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FOR_3, FOR_2, FOR_1, FOR_0) \ (__VA_ARGS__) #define FORR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FORR_3, FORR_2, FORR_1, FOR_0) \ (__VA_ARGS__) // in - expr style template <class T> T read() { T x; cin >> x; return x; } #define II read<int>() #define ILL read<ll>() #define ISTR read<string>() // in - macro style void _I() {} template <class ARG, class... ARGS> void _I(ARG &&arg, ARGS &&...args) { cin >> arg; _I(forward<ARGS>(args)...); } #define I(...) _I(__VA_ARGS__); // out template <class T> void _print(T x, ostream &os = cout) { os << x; bool space = true; if constexpr (is_same_v<T, char>) if (x == '\n') space = false; if (space) os << " "; } // template <class T> void _print(const vector<T> &d, ostream &os = cout) { // os << '['; // for (auto &e : d) // os << e << " "; // os << ']'; // } void _O() {} template <class ARG, class... ARGS> void _O(ARG &&arg, ARGS &&...args) { _print(arg); _O(forward<ARGS>(args)...); } #define O(...) _O(__VA_ARGS__ __VA_OPT__(, ) '\n'); #define OO(...) _O(__VA_ARGS__); // err const int _indentSize = 2; string _indent; void _E() { cerr << endl; } template <class ARG, class... ARGS> void _E(ARG &&arg, ARGS &&...args) { _print(forward<ARG>(arg), cerr); _E(std::forward<ARGS>(args)...); } #define ERR(...) \ { \ cerr << _indent << "[" << __LINE__ << "] "; \ _E(__VA_ARGS__); \ } #define _V5(x) #x, "==", (x) #define _V4(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V5(__VA_ARGS__)) #define _V3(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V4(__VA_ARGS__)) #define _V2(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V3(__VA_ARGS__)) #define _V1(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V2(__VA_ARGS__)) #define ERRV(...) ERR(_V1(__VA_ARGS__)) #define INDENT auto _indenter = Indenter(); struct Indenter { Indenter() { FOR(_indentSize) _indent.push_back(' '); } ~Indenter() { FOR(_indentSize) _indent.pop_back(); } }; #ifdef DEBUG #define FAIL(str) \ { \ cerr << "ASSERT FAILED " << str << " " << __FILE__ << ':' << __LINE__ \ << endl; \ abort(); \ } #else #define FAIL(x) \ {} #endif // asserts #define CHECK(cond) \ if (!(cond)) \ FAIL(#cond) #define CHECK_OP(op, a, b, res) \ if (!(res)) \ FAIL(#a " " op " " #b " (" + to_string(a) + " vs " + to_string(b) + ")") #define CHECK_EQ(a, b) CHECK_OP("==", a, b, (a) == (b)) #define CHECK_NE(a, b) CHECK_OP("!=", a, b, (a) != (b)) #define CHECK_LE(a, b) CHECK_OP("<=", a, b, (a) <= (b)) #define CHECK_GE(a, b) CHECK_OP(">=", a, b, (a) >= (b)) #define CHECK_LT(a, b) CHECK_OP("<", a, b, (a) < (b)) #define CHECK_GT(a, b) CHECK_OP(">", a, b, (a) > (b)) #define CHECK_RANGE(x, a, b) \ if (!(a <= x && x < b)) \ FAIL(#x " in [" #a "," #b ")") // #define SZ(v) ((int)(v).size()) using pii = pair<int, int>; //////////////////////////////////////////////////////////////////////////////// // snippets/modulo.hpp // template <class T, class E> T fpow(T a, E exp) { T r = 1; while (exp) { if (exp & 1) { r *= a; --exp; } else { exp >>= 1; a *= a; } } return r; } template <int MOD> class Modulo { public: using H = int; H h = 0; public: Modulo() = default; Modulo(H x) : h((x + MOD) % MOD){CHECK_GE(x + MOD, 0)} Modulo(const Modulo &) = default; public: Modulo &operator+=(const Modulo &o) { h += o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator-=(const Modulo &o) { h = MOD + h - o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator*=(const Modulo &o) { h = (1ULL * h * o.h) % MOD; return *this; } Modulo &operator/=(const Modulo &o) { const auto TOTIENT = MOD - 1; *this *= fpow(o, TOTIENT - 1); return *this; } bool operator==(const Modulo &o) const { return h == o.h; } bool operator!=(const Modulo &o) const { return h != o.h; } Modulo operator+(const Modulo &o) const { return Modulo(*this) += o; } Modulo operator-(const Modulo &o) const { return Modulo(*this) -= o; } Modulo operator*(const Modulo &o) const { return Modulo(*this) *= o; } Modulo operator/(const Modulo &o) const { return Modulo(*this) /= o; } Modulo &operator--() { if (h == 0) h = MOD; --h; return *this; } Modulo &operator++() { ++h; if (h == MOD) h = 0; return *this; } Modulo operator--(int) { auto m = *this; --(*this); return m; } Modulo operator++(int) { auto m = *this; ++(*this); return m; } explicit operator H() const { return h; } }; using F = Modulo<int(1e9) + 7>; ostream &operator<<(ostream &os, const F &modulo) { return os << modulo.h; } // snippets/modulo.hpp //////////////////////////////////////////////////////////////////////////////// // using F = Modulo<998'244'353>; // time complexity : O(n^2 log^3 |G| + t n log |G|) // memory complexity : O(n^2 log |G| + tn) // t : number of generators // |G| : group size, obviously <= (n!) vector<int> inv(vector<int> &p) { vector<int> ret(p.size()); for (int i = 0; i < (int)p.size(); i++) ret[p[i]] = i; return ret; } vector<int> operator*(vector<int> &a, vector<int> &b) { vector<int> 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<vector<int>> lookup; vector<vector<vector<int>>> buckets, ibuckets; int yo(vector<int> 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; } F size() { F ret = 1; for (int i = 0; i < n; i++) ret *= buckets[i].size(); return ret; } bool in_group(vector<int> g) { return yo(g, false) == -1; } Group(vector<vector<int>> &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); } vector<int> 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))); } } } } }; int merge(vector<int> &arr, int temp[], int left, int mid, int right) { int i, j, k; int inv_count = 0; i = left; // i is index for left subarray j = mid; // j is index for right subarray k = left; // k is index for resultant merged subarray while ((i <= mid - 1) && (j <= right)) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; inv_count = inv_count + (mid - i); } } // Copy the remaining elements of left subarray (if there are any) to temp while (i <= mid - 1) { temp[k++] = arr[i++]; } // Copy the remaining elements of right subarray (if there are any) to temp while (j <= right) { temp[k++] = arr[j++]; } // Copy back the merged elements to original array for (i = left; i <= right; i++) { arr[i] = temp[i]; } return inv_count; } int mergeSort(vector<int> &arr, int temp[], int left, int right) { int mid, inv_count = 0; if (right > left) { // Divide the array into two parts and call _mergeSortAndCountInv() // for each of the parts mid = (right + left) / 2; // Inversion count will be sum of inversions in left-part, right-part // and number of inversions in merging inv_count += mergeSort(arr, temp, left, mid); inv_count += mergeSort(arr, temp, mid + 1, right); // Merge the two parts inv_count += merge(arr, temp, left, mid + 1, right); } return inv_count; } int count_inversions(vector<int> &perm) { vector<int> temp(perm.size()); return mergeSort(perm, temp.data(), 0, perm.size() - 1); } ostream &operator<<(ostream &os, const vector<int> &v) { // os << '['; for (auto &e : v) os << e << " "; // os << ']'; return os; } // set<vector<int>> all_perms; auto get_all(Group &g, int idx, vector<int> &perm) { if (idx == g.n) { // cerr << "perm " << perm << endl; // all_perms.insert(perm); return (F)count_inversions(perm); } F res = 0; for (int i = 0; i < (int)g.buckets[idx].size(); i++) { auto &p = g.buckets[idx][i]; auto next = p * perm; res += get_all(g, idx + 1, next); } return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int k, n; cin >> n >> k; vector<vector<int>> a; while (k--) { vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x - 1); } a.push_back(v); } Group g(a, n); auto sz = g.size(); cerr << "size " << sz << endl; vector<int> id(n); for (int i = 0; i < n; i++) id[i] = i; auto sum = get_all(g, 0, id); auto result = sum / sz; // cerr << "all perms size " << SZ(all_perms) << endl; O(result); return 0; } // https://codeforces.com/blog/entry/21335?locale=ru#comment-260437 // http://opencup.ru/files/ocg/gp5/problems1-e.pdf // problem H |