#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif constexpr int mod = 1e9 + 7; constexpr int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } constexpr int sub(int a, int b) { return add(a, mod - b); } constexpr int mul(int a, int b) { return int(a * LL(b) % mod); } constexpr int powi(int a, int b) { int ret = 1; REP(i, 30) { if(b & 1) ret = mul(ret, a); a = mul(a, a); b /= 2; } return ret; } constexpr int inv(int x) { return powi(x, mod - 2); } int determinant(vector<vector<int>>& a) { int n = ssize(a); int res = 1; vector D(n, 0), invD(n, 0); REP(i, n) { REP(j, i + 1) { if (i == j) { __int128_t sum = 0; REP(k, i) { sum += a[i][k] * __int128_t(a[i][k]) * D[k]; } D[i] = sub(a[i][i], int(sum % mod)); invD[i] = inv(D[i]); res = mul(res, D[i]); if (!res) { return 0; } } __int128_t sum = 0; REP(k, j) { sum += a[i][k] * __int128_t(a[j][k]) * D[k]; } a[i][j] = mul(invD[j], sub(a[i][j], int(sum % mod))); } } return res; } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; debug(n); vector<int> a(n); REP(i, n) cin >> a[i]; debug(a); vector m(n - 1, vector (n - 1, 0)); REP(i, n) { REP(j, i) { const int val = gcd(a[i], a[j]); if (i < n - 1) { m[i][j] = sub(m[i][j], val); m[j][i] = sub(m[j][i], val); m[i][i] = add(m[i][i], val); } m[j][j] = add(m[j][j], val); } } debug(m); const auto det = determinant(m); cout << det << '\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 | #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif constexpr int mod = 1e9 + 7; constexpr int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } constexpr int sub(int a, int b) { return add(a, mod - b); } constexpr int mul(int a, int b) { return int(a * LL(b) % mod); } constexpr int powi(int a, int b) { int ret = 1; REP(i, 30) { if(b & 1) ret = mul(ret, a); a = mul(a, a); b /= 2; } return ret; } constexpr int inv(int x) { return powi(x, mod - 2); } int determinant(vector<vector<int>>& a) { int n = ssize(a); int res = 1; vector D(n, 0), invD(n, 0); REP(i, n) { REP(j, i + 1) { if (i == j) { __int128_t sum = 0; REP(k, i) { sum += a[i][k] * __int128_t(a[i][k]) * D[k]; } D[i] = sub(a[i][i], int(sum % mod)); invD[i] = inv(D[i]); res = mul(res, D[i]); if (!res) { return 0; } } __int128_t sum = 0; REP(k, j) { sum += a[i][k] * __int128_t(a[j][k]) * D[k]; } a[i][j] = mul(invD[j], sub(a[i][j], int(sum % mod))); } } return res; } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; debug(n); vector<int> a(n); REP(i, n) cin >> a[i]; debug(a); vector m(n - 1, vector (n - 1, 0)); REP(i, n) { REP(j, i) { const int val = gcd(a[i], a[j]); if (i < n - 1) { m[i][j] = sub(m[i][j], val); m[j][i] = sub(m[j][i], val); m[i][i] = add(m[i][i], val); } m[j][j] = add(m[j][j], val); } } debug(m); const auto det = determinant(m); cout << det << '\n'; } |