//Szymon Januszek, 3LO Gdynia //Z tw. Kirchhoff-a //Oblicza wznaznik przy użyciu algorytmu Bareiss //całkowita złożoność: O(n^3*logn) #include <bits/stdc++.h> using namespace std; //#define DEBUG typedef long long ll; const ll mod = 1e9 + 7; ll MOD(ll x) { x = x % mod; while(x < 0) x += mod; return x; } const int N = 5010; int vals[N]; //vector<vector<ll>> matrix(N, vector<ll> (N)); ll matrix[N][N]; ll fast_pow_mod(ll b, ll e) { ll r = 1; while(e) { if(e & 1) { r *= b; r %= mod; } e >>= 1; b *= b; b %= mod; } return r; } void print_matrix(int n) { #ifdef DEBUG cout << endl; for(int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) cout << matrix[i][j] << " "; cout << endl; } #endif } int main() { int n; cin >> n; if(n == 1){ cout << 1 << endl; return 0; } cin >> vals[1]; for(int i = 2; i <= n; i++) { scanf("%d", vals + i); for(int j = 1; j < i; j++) { int x = __gcd(vals[j], vals[i]); matrix[i][j] = matrix[j][i] = -x; matrix[i][i] += x; matrix[j][j] += x; } } print_matrix(n); for(int i = 1; i ^ n; ++i) for(int j = 1; j ^ n; ++j) matrix[i][j] = MOD(matrix[i][j]); for(int i = 1; i ^ n - 1; ++i) { for(int j = i + 1; j ^ n; ++j) { for(int k = i + 1; k ^ n; ++k) { matrix[j][k] = MOD(matrix[i][i] * matrix[j][k] - matrix[j][i] * matrix[i][k]); if(i != 1) matrix[j][k] = MOD(matrix[j][k] * fast_pow_mod(matrix[i - 1][i - 1], mod - 2)); } } } print_matrix(n); cout << llabs(MOD(matrix[n - 1][n - 1])) << endl; 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 | //Szymon Januszek, 3LO Gdynia //Z tw. Kirchhoff-a //Oblicza wznaznik przy użyciu algorytmu Bareiss //całkowita złożoność: O(n^3*logn) #include <bits/stdc++.h> using namespace std; //#define DEBUG typedef long long ll; const ll mod = 1e9 + 7; ll MOD(ll x) { x = x % mod; while(x < 0) x += mod; return x; } const int N = 5010; int vals[N]; //vector<vector<ll>> matrix(N, vector<ll> (N)); ll matrix[N][N]; ll fast_pow_mod(ll b, ll e) { ll r = 1; while(e) { if(e & 1) { r *= b; r %= mod; } e >>= 1; b *= b; b %= mod; } return r; } void print_matrix(int n) { #ifdef DEBUG cout << endl; for(int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) cout << matrix[i][j] << " "; cout << endl; } #endif } int main() { int n; cin >> n; if(n == 1){ cout << 1 << endl; return 0; } cin >> vals[1]; for(int i = 2; i <= n; i++) { scanf("%d", vals + i); for(int j = 1; j < i; j++) { int x = __gcd(vals[j], vals[i]); matrix[i][j] = matrix[j][i] = -x; matrix[i][i] += x; matrix[j][j] += x; } } print_matrix(n); for(int i = 1; i ^ n; ++i) for(int j = 1; j ^ n; ++j) matrix[i][j] = MOD(matrix[i][j]); for(int i = 1; i ^ n - 1; ++i) { for(int j = i + 1; j ^ n; ++j) { for(int k = i + 1; k ^ n; ++k) { matrix[j][k] = MOD(matrix[i][i] * matrix[j][k] - matrix[j][i] * matrix[i][k]); if(i != 1) matrix[j][k] = MOD(matrix[j][k] * fast_pow_mod(matrix[i - 1][i - 1], mod - 2)); } } } print_matrix(n); cout << llabs(MOD(matrix[n - 1][n - 1])) << endl; return 0; } |