#include <bits/stdc++.h> #define ll long long #define debug if(0) const ll MOD = 1e9+7; const int MAX_N = 5e3; int n; int nwd(int a, int b) { if (b == 0) return a; return nwd(b, a%b); } ll fast_pow(ll a, int p) { if (p == 0) return 1; if (p % 2 == 0) { ll tmp = fast_pow(a, p/2); return tmp * tmp % MOD; } return fast_pow(a, p-1) * a % MOD; } ll get_inv(ll a) { return fast_pow(a, MOD-2) % MOD; } void add_row(std::vector<std::vector<ll> > &A, int N, int i, int j, ll x) { for (int k = 0; k < N; k++) A[j][k] = (A[j][k] + (A[i][k] * x % MOD)) % MOD; } void print_matrix(std::vector<std::vector<ll> > &A) { for (int i = 0; i < A.size(); i++) { for (int j = 0; j < A.size(); j++) std::cout << A[i][j] << "\t"; std::cout << "\n"; } } ll determinant(std::vector<std::vector<ll> > &A) { debug print_matrix(A); int N = A.size(); int cnt_piv = 0; for (int i = 0; i < N; i++) { if (A[i][i] == 0) { int pivot = -1; for (int j = i+1; j < N; j++) if (A[j][i] != 0) { pivot = j; break; } if (pivot == -1) return 0; std::swap(A[i], A[pivot]); cnt_piv++; } ll inv = get_inv(A[i][i]); for (int j = i+1; j < N; j++) if (A[j][i] != 0) { debug std::cout << "A[" << j << "] += A[" << i << "]" << " * " << inv << "\n"; add_row(A, N, i, j, MOD - (A[j][i] * inv % MOD)); } debug print_matrix(A); } ll res = 1; for (int i = 0; i < N; i++) res = res * A[i][i] % MOD; if (cnt_piv % 2 == 1) res = (MOD + MOD - res) % MOD; return res; } ll solve(std::vector<int> &v) { if (n == 1) return 0; if (n == 2) return nwd(v[0], v[1]); std::vector<std::vector<ll>> M(n, std::vector<ll>(n, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i == j) M[i][j] = 0; else M[i][j] = nwd(v[i], v[j]); } std::vector<ll> deg(n, 0); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) deg[i] += M[i][j]; std::vector<std::vector<ll>> L(n-1, std::vector<ll>(n, 0)); for (int i = 0; i < n-1; i++) for (int j = 0; j < n-1; j++) { if (i == j) L[i][j] = deg[i+1]; else L[i][j] = MOD - M[i+1][j+1]; } return determinant(L); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> n; std::vector<int> v(n, 0); for (auto &x : v) std::cin >> x; std::cout << solve(v) << "\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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <bits/stdc++.h> #define ll long long #define debug if(0) const ll MOD = 1e9+7; const int MAX_N = 5e3; int n; int nwd(int a, int b) { if (b == 0) return a; return nwd(b, a%b); } ll fast_pow(ll a, int p) { if (p == 0) return 1; if (p % 2 == 0) { ll tmp = fast_pow(a, p/2); return tmp * tmp % MOD; } return fast_pow(a, p-1) * a % MOD; } ll get_inv(ll a) { return fast_pow(a, MOD-2) % MOD; } void add_row(std::vector<std::vector<ll> > &A, int N, int i, int j, ll x) { for (int k = 0; k < N; k++) A[j][k] = (A[j][k] + (A[i][k] * x % MOD)) % MOD; } void print_matrix(std::vector<std::vector<ll> > &A) { for (int i = 0; i < A.size(); i++) { for (int j = 0; j < A.size(); j++) std::cout << A[i][j] << "\t"; std::cout << "\n"; } } ll determinant(std::vector<std::vector<ll> > &A) { debug print_matrix(A); int N = A.size(); int cnt_piv = 0; for (int i = 0; i < N; i++) { if (A[i][i] == 0) { int pivot = -1; for (int j = i+1; j < N; j++) if (A[j][i] != 0) { pivot = j; break; } if (pivot == -1) return 0; std::swap(A[i], A[pivot]); cnt_piv++; } ll inv = get_inv(A[i][i]); for (int j = i+1; j < N; j++) if (A[j][i] != 0) { debug std::cout << "A[" << j << "] += A[" << i << "]" << " * " << inv << "\n"; add_row(A, N, i, j, MOD - (A[j][i] * inv % MOD)); } debug print_matrix(A); } ll res = 1; for (int i = 0; i < N; i++) res = res * A[i][i] % MOD; if (cnt_piv % 2 == 1) res = (MOD + MOD - res) % MOD; return res; } ll solve(std::vector<int> &v) { if (n == 1) return 0; if (n == 2) return nwd(v[0], v[1]); std::vector<std::vector<ll>> M(n, std::vector<ll>(n, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i == j) M[i][j] = 0; else M[i][j] = nwd(v[i], v[j]); } std::vector<ll> deg(n, 0); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) deg[i] += M[i][j]; std::vector<std::vector<ll>> L(n-1, std::vector<ll>(n, 0)); for (int i = 0; i < n-1; i++) for (int j = 0; j < n-1; j++) { if (i == j) L[i][j] = deg[i+1]; else L[i][j] = MOD - M[i+1][j+1]; } return determinant(L); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> n; std::vector<int> v(n, 0); for (auto &x : v) std::cin >> x; std::cout << solve(v) << "\n"; } |