#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int mod = 1e9 + 7; int power(long long n, long long k) { int ans = 1 % mod; n %= mod; if (n < 0) n += mod; while (k) { if (k & 1) ans = (long long) ans * n % mod; n = (long long) n * n % mod; k >>= 1; } return ans; } int Gauss(vector<vector<int>> a) { int n = a.size(), m = (int)a[0].size(); int free_var = 0; const long long MODSQ = (long long)mod * mod; int det = 1, rank = 0; for (int col = 0, row = 0; col < m && row < n; col++) { int mx = row; for (int k = row; k < n; k++) if (a[k][col] > a[mx][col]) mx = k; if (a[mx][col] == 0) {det = 0; continue;} for (int j = col; j < m; j++) swap(a[mx][j], a[row][j]); if (row != mx) det = det == 0 ? 0 : mod - det; det = 1LL * det * a[row][col] % mod; int inv = power(a[row][col], mod - 2); for (int i = 0; i < n && inv; i++){ if (i != row && a[i][col]) { int x = ((long long)a[i][col] * inv) % mod; for (int j = col; j < m && x; j++){ if (a[row][j]) a[i][j] = (MODSQ + a[i][j] - ((long long)a[row][j] * x)) % mod; } } } row++; ++rank; } return det; } const int nax = 5005; int inp[nax]; int n; int deg[nax]; void solve(){ cin >> n; for(int i=0;i<n;i++) cin >> inp[i]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(j != i){ deg[i] += (__gcd(inp[i], inp[j])); deg[i] %= mod; } } } vector<vector<int>> a(n-1, vector<int>(n-1)); for(int i = 0; i < n-1; i++){ for(int j = 0; j < n-1; j++){ if(i != j){ a[i][j] = (mod - __gcd(inp[i], inp[j])) % mod; } else a[i][j] = deg[i]; } } cout << Gauss(a) << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; //cin >> tt; while(tt--) 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 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int mod = 1e9 + 7; int power(long long n, long long k) { int ans = 1 % mod; n %= mod; if (n < 0) n += mod; while (k) { if (k & 1) ans = (long long) ans * n % mod; n = (long long) n * n % mod; k >>= 1; } return ans; } int Gauss(vector<vector<int>> a) { int n = a.size(), m = (int)a[0].size(); int free_var = 0; const long long MODSQ = (long long)mod * mod; int det = 1, rank = 0; for (int col = 0, row = 0; col < m && row < n; col++) { int mx = row; for (int k = row; k < n; k++) if (a[k][col] > a[mx][col]) mx = k; if (a[mx][col] == 0) {det = 0; continue;} for (int j = col; j < m; j++) swap(a[mx][j], a[row][j]); if (row != mx) det = det == 0 ? 0 : mod - det; det = 1LL * det * a[row][col] % mod; int inv = power(a[row][col], mod - 2); for (int i = 0; i < n && inv; i++){ if (i != row && a[i][col]) { int x = ((long long)a[i][col] * inv) % mod; for (int j = col; j < m && x; j++){ if (a[row][j]) a[i][j] = (MODSQ + a[i][j] - ((long long)a[row][j] * x)) % mod; } } } row++; ++rank; } return det; } const int nax = 5005; int inp[nax]; int n; int deg[nax]; void solve(){ cin >> n; for(int i=0;i<n;i++) cin >> inp[i]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(j != i){ deg[i] += (__gcd(inp[i], inp[j])); deg[i] %= mod; } } } vector<vector<int>> a(n-1, vector<int>(n-1)); for(int i = 0; i < n-1; i++){ for(int j = 0; j < n-1; j++){ if(i != j){ a[i][j] = (mod - __gcd(inp[i], inp[j])) % mod; } else a[i][j] = deg[i]; } } cout << Gauss(a) << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; //cin >> tt; while(tt--) solve(); return 0; } |