#include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; int expo(int a, long long n, int mod) { int ans = 1; while (n) { if (n & 1LL) { ans = ((long long) ans * a) % mod; } n >>= 1; a = ((long long) a * a) % mod; } return ans; } int revMod(int a, int mod) { return expo(a, mod - 2, mod); } int determinant(int n, vector <vector<int>> &mat) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (mat[i][j] < 0) { mat[i][j] += MOD; } } int det = 1; for (int i = 0; i < n; i++) { int nonZeroIdx = -1; for (int j = i; j < n; j++) if (mat[j][i] != 0) { nonZeroIdx = j; break; } assert(nonZeroIdx != -1); if (nonZeroIdx != i) { swap(mat[i], mat[nonZeroIdx]); det = (MOD - det) % MOD; } det = ((long long) det * mat[i][i]) % MOD; int rv = revMod(mat[i][i], MOD); for (int j = i; j < n; j++) { mat[i][j] = ((long long) mat[i][j] * rv) % MOD; } for (int j = i + 1; j < n; j++) { int lead = mat[j][i]; for (int k = i; k < n; k++) { mat[j][k] = (mat[j][k] - (long long) mat[i][k] * lead) % MOD; if (mat[j][k] < 0) { mat[j][k] += MOD; } } } } return det; } int solve(int n, vector <int> &a) { if (n == 1) { return 0; } vector <vector<int>> mat(n - 1, vector <int> (n - 1)); for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n - 1; j++) { int d = __gcd(a[i], a[j]); mat[i][j] -= d; mat[j][i] -= d; mat[i][i] += d; mat[j][j] += d; } for (int i = 0; i < n - 1; i++) { int d = __gcd(a[i], a[n - 1]); mat[i][i] += d; } return determinant(n - 1, mat); } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> a(n); for (int &ai : a) { cin >> ai; } cout << solve(n, a); 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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; int expo(int a, long long n, int mod) { int ans = 1; while (n) { if (n & 1LL) { ans = ((long long) ans * a) % mod; } n >>= 1; a = ((long long) a * a) % mod; } return ans; } int revMod(int a, int mod) { return expo(a, mod - 2, mod); } int determinant(int n, vector <vector<int>> &mat) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (mat[i][j] < 0) { mat[i][j] += MOD; } } int det = 1; for (int i = 0; i < n; i++) { int nonZeroIdx = -1; for (int j = i; j < n; j++) if (mat[j][i] != 0) { nonZeroIdx = j; break; } assert(nonZeroIdx != -1); if (nonZeroIdx != i) { swap(mat[i], mat[nonZeroIdx]); det = (MOD - det) % MOD; } det = ((long long) det * mat[i][i]) % MOD; int rv = revMod(mat[i][i], MOD); for (int j = i; j < n; j++) { mat[i][j] = ((long long) mat[i][j] * rv) % MOD; } for (int j = i + 1; j < n; j++) { int lead = mat[j][i]; for (int k = i; k < n; k++) { mat[j][k] = (mat[j][k] - (long long) mat[i][k] * lead) % MOD; if (mat[j][k] < 0) { mat[j][k] += MOD; } } } } return det; } int solve(int n, vector <int> &a) { if (n == 1) { return 0; } vector <vector<int>> mat(n - 1, vector <int> (n - 1)); for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n - 1; j++) { int d = __gcd(a[i], a[j]); mat[i][j] -= d; mat[j][i] -= d; mat[i][i] += d; mat[j][j] += d; } for (int i = 0; i < n - 1; i++) { int d = __gcd(a[i], a[n - 1]); mat[i][i] += d; } return determinant(n - 1, mat); } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> a(n); for (int &ai : a) { cin >> ai; } cout << solve(n, a); return 0; } |