// // Created by piotr on 12.03.2024. // #include <array> #include <cassert> #include <cmath> #include <cstdio> #include <numeric> #include <set> #include <vector> #include <tuple> struct Ułamek { public: long long p; long long q; Ułamek(long long p=0, long long q=1) : p(p), q(q) { norm(); } Ułamek& operator+=(const Ułamek& inny) { long long d = std::gcd(q, inny.q); p = (p * inny.q + inny.p * q) / d; q *= inny.q / d; norm(); return *this; } Ułamek& operator-=(const Ułamek& inny) { long long d = std::gcd(q, inny.q); p = (p * inny.q - inny.p * q) / d; q *= inny.q / d; norm(); return *this; } private: void norm() { long long d = std::gcd(std::abs(p), q); p /= d; q /= d; } }; std::tuple<long long, long long, long long> extended_gcd(long long a, long long b) { if (b == 0) { return {1, 0, a}; } auto [x1, y1, gcd] = extended_gcd(b, a % b); return {y1, x1 - (a / b) * y1, gcd}; } long long mod_inverse(long long a, long long m) { long long x, y, gcd; std::tie(x, y, gcd) = extended_gcd(a, m); if (gcd != 1) { return -1; } return (x % m + m) % m; } using Perm = std::vector<int>; bool is_identity(const Perm& p) { const int N = p.size(); for (int i=0; i<N; ++i) { if (p[i] != i) { return false; } } return true; } const int BLOCKS = 50; const long long MODULO = 1000000007; long long count_inversions(std::vector<int>& counts, std::vector<int>& countsB, const Perm& p) { const int N = p.size(); const int maxB = N/BLOCKS; std::fill(counts.begin(), counts.end(), 0); std::fill(countsB.begin(), countsB.end(), 0); long long inversions = 0; for (int i=0; i<N; ++i) { // chcemy znaleźć liczbę elementów w counts pomiędzy p[i] a N-1 int piB = p[i] / BLOCKS; int endC = std::min(BLOCKS*(piB+1), N); for (int j=p[i]; j<endC; ++j) { inversions += counts[j]; } for (int j=piB+1; j<=maxB; ++j) { inversions += countsB[j]; } counts[p[i]] += 1; countsB[piB] += 1; } return inversions; } void combine(Perm& p, const Perm& q) { for (auto& index : p) { index = q[index]; } } void remove_duplicates(const Perm& p, std::set<Perm>& other_permutations) { Perm pn(p); other_permutations.erase(pn); do { combine(pn, p); other_permutations.erase(pn); } while (!is_identity(pn)); } Ułamek count_inversions_simple(int N, const Perm& p) { Perm pn(p); long long P=0, Q=0; std::vector<int> counts(N), countsB(N/BLOCKS+1); P = (P+count_inversions(counts, countsB, p))%MODULO; Q++; do { combine(pn, p); P = (P+count_inversions(counts, countsB, pn))%MODULO; Q++; } while (!is_identity(pn)); return { P, Q % MODULO }; } Ułamek count_inversions_smart(int N, const Perm& p) { Ułamek u; std::vector<int> cycle_number(N, -1); int C = 0; for (int i0=0; i0<N; ++i0) { if (cycle_number[i0] == -1) { int i = i0; do { cycle_number[i] = C; i = p[i]; } while (i != i0); ++C; } } std::vector<Ułamek> poj(C); for (int c=0; c<C; ++c) { Perm pc(N); for (int i=0; i<N; ++i) { pc[i] = (cycle_number[i] == c) ? p[i] : i; } poj[c] = count_inversions_simple(N, pc); u += poj[c]; } for (int c1=0; c1<C; ++c1) { for (int c2=c1+1; c2<C; ++c2) { Perm pc(N); for (int i=0; i<N; ++i) { pc[i] = (cycle_number[i] == c1 || cycle_number[i] == c2) ? p[i] : i; } u += count_inversions_simple(N, pc); u -= poj[c1]; u -= poj[c2]; } } return u; } int foo(int N, int K) { std::vector<Perm> permutations; permutations.reserve(K); for (int k = 0; k<K; ++k) { Perm p(N); for (int n = 0; n<N; ++n) { int x; assert(scanf("%d", &x)==1); p[n] = x-1; } if (!is_identity(p)) { permutations.push_back(p); } } if (permutations.empty()) { return 0; } K = permutations.size(); // wszystkie Id odrzucamy Perm first_permutation = permutations[0]; std::set<Perm> other_permutations(permutations.begin()+1, permutations.end()); remove_duplicates(first_permutation, other_permutations); if (!other_permutations.empty()) { return N; // tak, wiem, że to nie do końca tak } auto pair = count_inversions_smart(N, first_permutation); long long P = pair.p, Q = pair.q; long long PQ = std::gcd(P, Q); P /= PQ; Q /= PQ; Q = mod_inverse(Q, MODULO); return (P * Q) % MODULO; } int main() { int N, K; assert(scanf("%d%d", &N, &K) == 2); printf("%d\n", foo(N, K)); }
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 | // // Created by piotr on 12.03.2024. // #include <array> #include <cassert> #include <cmath> #include <cstdio> #include <numeric> #include <set> #include <vector> #include <tuple> struct Ułamek { public: long long p; long long q; Ułamek(long long p=0, long long q=1) : p(p), q(q) { norm(); } Ułamek& operator+=(const Ułamek& inny) { long long d = std::gcd(q, inny.q); p = (p * inny.q + inny.p * q) / d; q *= inny.q / d; norm(); return *this; } Ułamek& operator-=(const Ułamek& inny) { long long d = std::gcd(q, inny.q); p = (p * inny.q - inny.p * q) / d; q *= inny.q / d; norm(); return *this; } private: void norm() { long long d = std::gcd(std::abs(p), q); p /= d; q /= d; } }; std::tuple<long long, long long, long long> extended_gcd(long long a, long long b) { if (b == 0) { return {1, 0, a}; } auto [x1, y1, gcd] = extended_gcd(b, a % b); return {y1, x1 - (a / b) * y1, gcd}; } long long mod_inverse(long long a, long long m) { long long x, y, gcd; std::tie(x, y, gcd) = extended_gcd(a, m); if (gcd != 1) { return -1; } return (x % m + m) % m; } using Perm = std::vector<int>; bool is_identity(const Perm& p) { const int N = p.size(); for (int i=0; i<N; ++i) { if (p[i] != i) { return false; } } return true; } const int BLOCKS = 50; const long long MODULO = 1000000007; long long count_inversions(std::vector<int>& counts, std::vector<int>& countsB, const Perm& p) { const int N = p.size(); const int maxB = N/BLOCKS; std::fill(counts.begin(), counts.end(), 0); std::fill(countsB.begin(), countsB.end(), 0); long long inversions = 0; for (int i=0; i<N; ++i) { // chcemy znaleźć liczbę elementów w counts pomiędzy p[i] a N-1 int piB = p[i] / BLOCKS; int endC = std::min(BLOCKS*(piB+1), N); for (int j=p[i]; j<endC; ++j) { inversions += counts[j]; } for (int j=piB+1; j<=maxB; ++j) { inversions += countsB[j]; } counts[p[i]] += 1; countsB[piB] += 1; } return inversions; } void combine(Perm& p, const Perm& q) { for (auto& index : p) { index = q[index]; } } void remove_duplicates(const Perm& p, std::set<Perm>& other_permutations) { Perm pn(p); other_permutations.erase(pn); do { combine(pn, p); other_permutations.erase(pn); } while (!is_identity(pn)); } Ułamek count_inversions_simple(int N, const Perm& p) { Perm pn(p); long long P=0, Q=0; std::vector<int> counts(N), countsB(N/BLOCKS+1); P = (P+count_inversions(counts, countsB, p))%MODULO; Q++; do { combine(pn, p); P = (P+count_inversions(counts, countsB, pn))%MODULO; Q++; } while (!is_identity(pn)); return { P, Q % MODULO }; } Ułamek count_inversions_smart(int N, const Perm& p) { Ułamek u; std::vector<int> cycle_number(N, -1); int C = 0; for (int i0=0; i0<N; ++i0) { if (cycle_number[i0] == -1) { int i = i0; do { cycle_number[i] = C; i = p[i]; } while (i != i0); ++C; } } std::vector<Ułamek> poj(C); for (int c=0; c<C; ++c) { Perm pc(N); for (int i=0; i<N; ++i) { pc[i] = (cycle_number[i] == c) ? p[i] : i; } poj[c] = count_inversions_simple(N, pc); u += poj[c]; } for (int c1=0; c1<C; ++c1) { for (int c2=c1+1; c2<C; ++c2) { Perm pc(N); for (int i=0; i<N; ++i) { pc[i] = (cycle_number[i] == c1 || cycle_number[i] == c2) ? p[i] : i; } u += count_inversions_simple(N, pc); u -= poj[c1]; u -= poj[c2]; } } return u; } int foo(int N, int K) { std::vector<Perm> permutations; permutations.reserve(K); for (int k = 0; k<K; ++k) { Perm p(N); for (int n = 0; n<N; ++n) { int x; assert(scanf("%d", &x)==1); p[n] = x-1; } if (!is_identity(p)) { permutations.push_back(p); } } if (permutations.empty()) { return 0; } K = permutations.size(); // wszystkie Id odrzucamy Perm first_permutation = permutations[0]; std::set<Perm> other_permutations(permutations.begin()+1, permutations.end()); remove_duplicates(first_permutation, other_permutations); if (!other_permutations.empty()) { return N; // tak, wiem, że to nie do końca tak } auto pair = count_inversions_smart(N, first_permutation); long long P = pair.p, Q = pair.q; long long PQ = std::gcd(P, Q); P /= PQ; Q /= PQ; Q = mod_inverse(Q, MODULO); return (P * Q) % MODULO; } int main() { int N, K; assert(scanf("%d%d", &N, &K) == 2); printf("%d\n", foo(N, K)); } |