#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } template<typename T, typename V> T constexpr myPow(T base, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans *= base; base *= base; exp >>= 1; } return ans; } template<typename T, typename V> T constexpr myPowMod(T base, T mod, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans = (ans * base) % mod; base = (base * base) % mod; exp >>= 1; } return ans; } template<typename T> inline constexpr T myMod(T val, T mod) {val %= mod; if (val < 0) val += mod; return val; } template<typename T> pair<T, T> extEuclidean(T a, T b) { pair<T, T> s = {1, 0}; pair<T, T> r = {a, b}; while (r.second) { T q = r.first / r.second; r = {r.second, r.first - q * r.second}; s = {s.second, s.first - q * s.second}; } return {s.first, b ? (r.first - s.first * a) / b : 0}; } template<typename T, T modulo> struct modBase { T val; public: modBase() : val(0) {} modBase(T v) { if (0 <= v && v < modulo) val = v; else val = myMod(v, modulo); } using baseType = T; static T modValue() {return modulo; } T value() const {return val; } // modBase inverse() const {/*assert(gcd(modulo, val) == 1);*/ return extEuclidean(modulo, val).second; } modBase inverse() const {return myPow(*this, modulo - 2); } modBase operator-() const {modBase ret; if (val) ret.val = modulo - val; return ret; } modBase operator+() const {return *this; } modBase& operator+=(modBase const& x) {val += x.val; if (val >= modulo) val -= modulo; return *this; } modBase& operator-=(modBase const& x) {val -= x.val; if (val < 0) val += modulo; return *this; } modBase& operator*=(modBase const& x) {val = myMod(val * x.val, modulo); return *this; } modBase& operator/=(modBase const& x) {return (*this) *= x.inverse(); } friend modBase operator+(const modBase& a, const modBase& b) {modBase ret(a); return ret += b; } friend modBase operator-(const modBase& a, const modBase& b) {modBase ret(a); return ret -= b; } friend modBase operator*(const modBase& a, const modBase& b) {modBase ret(a); return ret *= b; } friend modBase operator/(const modBase& a, const modBase& b) {modBase ret(a); return ret /= b; } modBase& operator++() {return (*this) += 1; } modBase operator++(int) {modBase tmp(*this); operator++(); return tmp; } modBase& operator--() {return (*this) -= 1; } modBase operator--(int) {modBase tmp(*this); operator--(); return tmp; } template<typename X> static modBase factorial(X v) {modBase ret(1); for (modBase i(1); --v >= 0 ; ++i) ret *= i; return ret; } friend ostream& operator<<(ostream &os, const modBase& m) {return os << m.val; } friend bool operator<(const modBase& a, const modBase& b) {return a.val < b.val; } friend bool operator==(const modBase& a, const modBase& b) {return a.val == b.val; } }; template<ll M> using modInteger = modBase<ll, M>; using mod1e9p7 = modInteger<ll(1e9 + 7)>; using mod1e9p9 = modInteger<ll(1e9 + 9)>; using mod998s = modInteger<998244353LL>; using mods = pair<mod1e9p7, mod1e9p9>; template<typename F> class matrix { int n, m; vector<vector<F>> ent; public: // -- Misc. 1 int get_row_count() const {return n; } int get_col_count() const {return m; } int get_n() const {return n; } int get_m() const {return m; } auto begin() {return ent.begin(); } auto end() {return ent.end(); } auto rbegin() {return ent.rbegin(); } auto rend() {return ent.rend(); } auto begin() const {return ent.begin(); } auto end() const {return ent.end(); } auto rbegin() const {return ent.rbegin(); } auto rend() const {return ent.rend(); } auto cbegin() const {return ent.begin(); } auto cend() const {return ent.end(); } auto crbegin() const {return ent.rbegin(); } auto crend() const {return ent.rend(); } vector<F>& operator[](int i) {return ent[i]; } const vector<F>& operator[](int i) const {return ent[i]; } friend bool add_able(const matrix &A, const matrix &B) {return A.n == B.n && A.m == B.m; } friend bool mul_able(const matrix &A, const matrix &B) {return A.m == B.n; } bool is_square() {return n == m; } // -- Constructors & etc matrix(int _n = 1, int _m = 1, F f = 0) : n(_n), m(_m), ent(n, vector<F>(m, f)) { } matrix(const matrix &A) : n(A.n), m(A.m), ent(A.ent) {} matrix(matrix &&A) : n(A.n), m(A.m), ent(move(A.ent)) {} ~matrix() { } matrix& operator=(const matrix& M) { n = M.n; m = M.m; ent = M.ent; return *this; } matrix& operator=(matrix&& M) { n = M.n; m = M.m; ent = move(M.ent); return *this; } template<typename functor> static matrix from_functor(int n, int m, functor f) { matrix A(n, m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) A[i][j] = f(i, j); return A; } template<typename G> static matrix convert(int n, int m, const G &A) { return from_functor(n, m, [&A](int i, int j){return A[i][j]; }); } template<typename G> static matrix convert(const G &A) { return from_functor(A.get_row_count(), A.get_col_count(), [&A](int i, int j){return A[i][j]; }); } static matrix identity(int n) { return from_functor(n, n, [](int i, int j){return i == j; }); } // -- Oper. friend ostream& operator<<(ostream& os, const matrix &A) { for (int i = 0; i < A.n; ++i) for (int j = 0; j < A.m; ++j) os << A[i][j] << " \n"[j+1 == A.m]; return os; } friend istream& operator>>(istream& is, matrix &A) { for (int i = 0; i < A.n; ++i) for (int j = 0; j < A.m; ++j) is >> A[i][j]; return is; } friend bool operator==(const matrix &A, const matrix &B) { if (!add_able(A, B)) return false; for (int i = 0; i < A.get_row_count(); ++i) if (A[i] != B[i]) return false; return true; } friend bool operator<(const matrix &A, const matrix &B) { assert(add_able(A, B)); for (int i = 0; i < A.get_row_count(); ++i) for (int j = 0; j < A.get_col_count(); ++j) { if (A[i][j] == B[i][j]) continue; else return A[i][j] < B[i][j]; } return false; } // -- Misc. 2 void clear() { ent.clear(); n = m = 0; } void fill(F f = 0) { for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) ent[i][j] = f; } matrix& change_row_count(int k, F f = 0) { if (k <= n) ent.resize(k); else ent.resize(k, vector<F>(m, f)); n = k; return *this; } matrix& change_col_count(int k, F f = 0) { for (vector<F>& v : ent) v.resize(k, f); m = k; return *this; } matrix& resize(int kn, int km, F f = 0) { change_row_count(kn, f); change_col_count(km, f); } matrix& add_row(vector<F> &&v) { assert((int)v.size() == m); ++n; ent.push_back(v); return *this; } matrix& add_row(const vector<F> &v) { vector<F> w(v); return add_row(move(w)); } matrix& add_col(const vector<F> &v) { assert(v.size() == n); ++m; for (int i = 0; i < n; ++i) ent[i].push_back(v[i]); return *this; } // -- Echelon Form template<bool det_only> F det_echelon(); matrix& make_echelon() { det_echelon<false>(); return *this; } matrix get_echelon() { matrix A(*this); return A.make_echelon(); } friend matrix get_echelon(const matrix &A) { return A.get_echelon(); } friend matrix get_echelon(matrix &&A) { return A.make_echelon(); } // -- Inverse bool try_to_invert(); matrix& invert() { assert(try_to_invert()); return *this; } matrix inverse() const { matrix A(*this); return A.invert(); } friend matrix inverse(const matrix &A) { return A.inverse(); } friend matrix inverse(matrix &&A) { return A.invert(); } // -- Determinant F det_destroy() { assert(is_square()); return det_echelon<true>(); } F det() const { matrix A(*this); return A.det_destroy(); } friend F det(const matrix &A) { return A.det(); } friend F det(matrix &&A) { return A.det_destroy(); } // -- Alg oper. 1 friend matrix operator+(const matrix &A) {return A; } friend matrix operator-(const matrix &A) { return from_functor(A.n, A.m, [&A](int i, int j){return -A[i][j]; }); } friend matrix operator+(const matrix &A, const matrix &B) { assert(add_able(A, B)); return from_functor(A.n, A.m, [&A, &B](int i, int j){return A[i][j] + B[i][j]; }); } friend matrix operator-(const matrix &A, const matrix &B) { assert(add_able(A, B)); return from_functor(A.n, A.m, [&A, &B](int i, int j){return A[i][j] - B[i][j]; }); } friend matrix operator*(const matrix &A, const matrix &B) { assert(mul_able(A, B)); return from_functor(A.n, B.m, [&A, &B](int i, int j){ F res = 0; for (int k = 0; k < A.m; ++k) res += A[i][k] * B[k][j]; return res; }); } friend matrix operator/(const matrix &A, const matrix &B) { return A * B.inverse(); } // -- Alg oper. 1 matrix& operator+=(const matrix &A) { assert(add_able(*this, A)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) ent[i][j] += A[i][j]; return *this; } matrix& operator-=(const matrix &A) { assert(add_able(*this, A)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) ent[i][j] -= A[i][j]; return *this; } matrix& operator*=(const matrix &A) { assert(mul_able(*this, A)); return *this = *this * A; } matrix& operator/=(const matrix &A) { assert(mul_able(*this, A)); return *this = *this / A; } }; template<typename F> template<bool det_only> F matrix<F>::det_echelon() { if constexpr (det_only) if (n != m) return 0; F d = (n == m ? 1 : 0); int row = 0; for (int col = 0; col < m; ++col) { int non_zero = -1; for (int i = row; i < n; ++i) { if (ent[i][col].val != 0) { non_zero = i; break; } } if (non_zero == -1) { if constexpr (det_only) return 0; d = 0; continue; } if (non_zero != row) { d = -d; swap(ent[non_zero], ent[row]); } if (ent[row][col].val != 1) { d *= ent[row][col]; F inverse = 1 / ent[row][col]; for (F &f : ent[row]) f *= inverse; } if constexpr (!det_only) { for (int i = 0; i < row; ++i) { if (ent[i][col] == 0) continue; F mul = ent[i][col]; for (int j = 0; j < m; ++j) ent[i][j] -= mul * ent[row][j]; } } { for (int i = row + 1; i < n; ++i) { if (ent[i][col].val == 0) continue; F mul = ent[i][col]; for (int j = 0; j < m; ++j) ent[i][j] -= mul * ent[row][j]; } } ++row; } return d; } template<typename F> bool matrix<F>::try_to_invert() { assert(is_square()); m = 2 * n; for (int i = 0; i < n; ++i) { ent[i].resize(2 * n); ent[i][n + i] = 1; } make_echelon(); if (ent[n-1][n-1] != 1) { clear(); return false; } m = n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) ent[i][j] = ent[i][n + j]; ent[i].resize(n); } return true; } void solve() { int n; cin >> n; vi a(n); for (int &i : a) cin >> i; if (n == 1) { cout << "1\n"; return; } auto m = matrix<mod1e9p7>::from_functor(n-1, n-1, [&](int i, int j) { mod1e9p7 x = -__gcd(a[i], a[j]); for (int k = 0; k < n && i == j; ++k) x += __gcd(a[i], a[k]); return x; }); mod1e9p7 ans = det(m); cout << ans << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); 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 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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } template<typename T, typename V> T constexpr myPow(T base, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans *= base; base *= base; exp >>= 1; } return ans; } template<typename T, typename V> T constexpr myPowMod(T base, T mod, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans = (ans * base) % mod; base = (base * base) % mod; exp >>= 1; } return ans; } template<typename T> inline constexpr T myMod(T val, T mod) {val %= mod; if (val < 0) val += mod; return val; } template<typename T> pair<T, T> extEuclidean(T a, T b) { pair<T, T> s = {1, 0}; pair<T, T> r = {a, b}; while (r.second) { T q = r.first / r.second; r = {r.second, r.first - q * r.second}; s = {s.second, s.first - q * s.second}; } return {s.first, b ? (r.first - s.first * a) / b : 0}; } template<typename T, T modulo> struct modBase { T val; public: modBase() : val(0) {} modBase(T v) { if (0 <= v && v < modulo) val = v; else val = myMod(v, modulo); } using baseType = T; static T modValue() {return modulo; } T value() const {return val; } // modBase inverse() const {/*assert(gcd(modulo, val) == 1);*/ return extEuclidean(modulo, val).second; } modBase inverse() const {return myPow(*this, modulo - 2); } modBase operator-() const {modBase ret; if (val) ret.val = modulo - val; return ret; } modBase operator+() const {return *this; } modBase& operator+=(modBase const& x) {val += x.val; if (val >= modulo) val -= modulo; return *this; } modBase& operator-=(modBase const& x) {val -= x.val; if (val < 0) val += modulo; return *this; } modBase& operator*=(modBase const& x) {val = myMod(val * x.val, modulo); return *this; } modBase& operator/=(modBase const& x) {return (*this) *= x.inverse(); } friend modBase operator+(const modBase& a, const modBase& b) {modBase ret(a); return ret += b; } friend modBase operator-(const modBase& a, const modBase& b) {modBase ret(a); return ret -= b; } friend modBase operator*(const modBase& a, const modBase& b) {modBase ret(a); return ret *= b; } friend modBase operator/(const modBase& a, const modBase& b) {modBase ret(a); return ret /= b; } modBase& operator++() {return (*this) += 1; } modBase operator++(int) {modBase tmp(*this); operator++(); return tmp; } modBase& operator--() {return (*this) -= 1; } modBase operator--(int) {modBase tmp(*this); operator--(); return tmp; } template<typename X> static modBase factorial(X v) {modBase ret(1); for (modBase i(1); --v >= 0 ; ++i) ret *= i; return ret; } friend ostream& operator<<(ostream &os, const modBase& m) {return os << m.val; } friend bool operator<(const modBase& a, const modBase& b) {return a.val < b.val; } friend bool operator==(const modBase& a, const modBase& b) {return a.val == b.val; } }; template<ll M> using modInteger = modBase<ll, M>; using mod1e9p7 = modInteger<ll(1e9 + 7)>; using mod1e9p9 = modInteger<ll(1e9 + 9)>; using mod998s = modInteger<998244353LL>; using mods = pair<mod1e9p7, mod1e9p9>; template<typename F> class matrix { int n, m; vector<vector<F>> ent; public: // -- Misc. 1 int get_row_count() const {return n; } int get_col_count() const {return m; } int get_n() const {return n; } int get_m() const {return m; } auto begin() {return ent.begin(); } auto end() {return ent.end(); } auto rbegin() {return ent.rbegin(); } auto rend() {return ent.rend(); } auto begin() const {return ent.begin(); } auto end() const {return ent.end(); } auto rbegin() const {return ent.rbegin(); } auto rend() const {return ent.rend(); } auto cbegin() const {return ent.begin(); } auto cend() const {return ent.end(); } auto crbegin() const {return ent.rbegin(); } auto crend() const {return ent.rend(); } vector<F>& operator[](int i) {return ent[i]; } const vector<F>& operator[](int i) const {return ent[i]; } friend bool add_able(const matrix &A, const matrix &B) {return A.n == B.n && A.m == B.m; } friend bool mul_able(const matrix &A, const matrix &B) {return A.m == B.n; } bool is_square() {return n == m; } // -- Constructors & etc matrix(int _n = 1, int _m = 1, F f = 0) : n(_n), m(_m), ent(n, vector<F>(m, f)) { } matrix(const matrix &A) : n(A.n), m(A.m), ent(A.ent) {} matrix(matrix &&A) : n(A.n), m(A.m), ent(move(A.ent)) {} ~matrix() { } matrix& operator=(const matrix& M) { n = M.n; m = M.m; ent = M.ent; return *this; } matrix& operator=(matrix&& M) { n = M.n; m = M.m; ent = move(M.ent); return *this; } template<typename functor> static matrix from_functor(int n, int m, functor f) { matrix A(n, m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) A[i][j] = f(i, j); return A; } template<typename G> static matrix convert(int n, int m, const G &A) { return from_functor(n, m, [&A](int i, int j){return A[i][j]; }); } template<typename G> static matrix convert(const G &A) { return from_functor(A.get_row_count(), A.get_col_count(), [&A](int i, int j){return A[i][j]; }); } static matrix identity(int n) { return from_functor(n, n, [](int i, int j){return i == j; }); } // -- Oper. friend ostream& operator<<(ostream& os, const matrix &A) { for (int i = 0; i < A.n; ++i) for (int j = 0; j < A.m; ++j) os << A[i][j] << " \n"[j+1 == A.m]; return os; } friend istream& operator>>(istream& is, matrix &A) { for (int i = 0; i < A.n; ++i) for (int j = 0; j < A.m; ++j) is >> A[i][j]; return is; } friend bool operator==(const matrix &A, const matrix &B) { if (!add_able(A, B)) return false; for (int i = 0; i < A.get_row_count(); ++i) if (A[i] != B[i]) return false; return true; } friend bool operator<(const matrix &A, const matrix &B) { assert(add_able(A, B)); for (int i = 0; i < A.get_row_count(); ++i) for (int j = 0; j < A.get_col_count(); ++j) { if (A[i][j] == B[i][j]) continue; else return A[i][j] < B[i][j]; } return false; } // -- Misc. 2 void clear() { ent.clear(); n = m = 0; } void fill(F f = 0) { for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) ent[i][j] = f; } matrix& change_row_count(int k, F f = 0) { if (k <= n) ent.resize(k); else ent.resize(k, vector<F>(m, f)); n = k; return *this; } matrix& change_col_count(int k, F f = 0) { for (vector<F>& v : ent) v.resize(k, f); m = k; return *this; } matrix& resize(int kn, int km, F f = 0) { change_row_count(kn, f); change_col_count(km, f); } matrix& add_row(vector<F> &&v) { assert((int)v.size() == m); ++n; ent.push_back(v); return *this; } matrix& add_row(const vector<F> &v) { vector<F> w(v); return add_row(move(w)); } matrix& add_col(const vector<F> &v) { assert(v.size() == n); ++m; for (int i = 0; i < n; ++i) ent[i].push_back(v[i]); return *this; } // -- Echelon Form template<bool det_only> F det_echelon(); matrix& make_echelon() { det_echelon<false>(); return *this; } matrix get_echelon() { matrix A(*this); return A.make_echelon(); } friend matrix get_echelon(const matrix &A) { return A.get_echelon(); } friend matrix get_echelon(matrix &&A) { return A.make_echelon(); } // -- Inverse bool try_to_invert(); matrix& invert() { assert(try_to_invert()); return *this; } matrix inverse() const { matrix A(*this); return A.invert(); } friend matrix inverse(const matrix &A) { return A.inverse(); } friend matrix inverse(matrix &&A) { return A.invert(); } // -- Determinant F det_destroy() { assert(is_square()); return det_echelon<true>(); } F det() const { matrix A(*this); return A.det_destroy(); } friend F det(const matrix &A) { return A.det(); } friend F det(matrix &&A) { return A.det_destroy(); } // -- Alg oper. 1 friend matrix operator+(const matrix &A) {return A; } friend matrix operator-(const matrix &A) { return from_functor(A.n, A.m, [&A](int i, int j){return -A[i][j]; }); } friend matrix operator+(const matrix &A, const matrix &B) { assert(add_able(A, B)); return from_functor(A.n, A.m, [&A, &B](int i, int j){return A[i][j] + B[i][j]; }); } friend matrix operator-(const matrix &A, const matrix &B) { assert(add_able(A, B)); return from_functor(A.n, A.m, [&A, &B](int i, int j){return A[i][j] - B[i][j]; }); } friend matrix operator*(const matrix &A, const matrix &B) { assert(mul_able(A, B)); return from_functor(A.n, B.m, [&A, &B](int i, int j){ F res = 0; for (int k = 0; k < A.m; ++k) res += A[i][k] * B[k][j]; return res; }); } friend matrix operator/(const matrix &A, const matrix &B) { return A * B.inverse(); } // -- Alg oper. 1 matrix& operator+=(const matrix &A) { assert(add_able(*this, A)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) ent[i][j] += A[i][j]; return *this; } matrix& operator-=(const matrix &A) { assert(add_able(*this, A)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) ent[i][j] -= A[i][j]; return *this; } matrix& operator*=(const matrix &A) { assert(mul_able(*this, A)); return *this = *this * A; } matrix& operator/=(const matrix &A) { assert(mul_able(*this, A)); return *this = *this / A; } }; template<typename F> template<bool det_only> F matrix<F>::det_echelon() { if constexpr (det_only) if (n != m) return 0; F d = (n == m ? 1 : 0); int row = 0; for (int col = 0; col < m; ++col) { int non_zero = -1; for (int i = row; i < n; ++i) { if (ent[i][col].val != 0) { non_zero = i; break; } } if (non_zero == -1) { if constexpr (det_only) return 0; d = 0; continue; } if (non_zero != row) { d = -d; swap(ent[non_zero], ent[row]); } if (ent[row][col].val != 1) { d *= ent[row][col]; F inverse = 1 / ent[row][col]; for (F &f : ent[row]) f *= inverse; } if constexpr (!det_only) { for (int i = 0; i < row; ++i) { if (ent[i][col] == 0) continue; F mul = ent[i][col]; for (int j = 0; j < m; ++j) ent[i][j] -= mul * ent[row][j]; } } { for (int i = row + 1; i < n; ++i) { if (ent[i][col].val == 0) continue; F mul = ent[i][col]; for (int j = 0; j < m; ++j) ent[i][j] -= mul * ent[row][j]; } } ++row; } return d; } template<typename F> bool matrix<F>::try_to_invert() { assert(is_square()); m = 2 * n; for (int i = 0; i < n; ++i) { ent[i].resize(2 * n); ent[i][n + i] = 1; } make_echelon(); if (ent[n-1][n-1] != 1) { clear(); return false; } m = n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) ent[i][j] = ent[i][n + j]; ent[i].resize(n); } return true; } void solve() { int n; cin >> n; vi a(n); for (int &i : a) cin >> i; if (n == 1) { cout << "1\n"; return; } auto m = matrix<mod1e9p7>::from_functor(n-1, n-1, [&](int i, int j) { mod1e9p7 x = -__gcd(a[i], a[j]); for (int k = 0; k < n && i == j; ++k) x += __gcd(a[i], a[k]); return x; }); mod1e9p7 ans = det(m); cout << ans << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); return 0; } |