#include <bits/stdc++.h> using namespace std; template<class c> struct rge { c b, e; }; template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template<class c> auto dud(c* x) -> decltype(cerr << *x, 0); template<class c> char dud(...); struct debug { ~debug() { cerr << endl; } template<class c> typename \ enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template<class c> typename \ enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template<class c, class b> debug & operator <<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template<class c> debug & operator <<(rge<c> d) { *this << "["; for(auto it = d.b; it != d.e; ++it) { *this << ", " + 2 * (it == d.b) << *it; } return *this << "]"; } }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // This modnum class was not created by me. However, I do not remember where I // found this code, since it was a long time ago. template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: using ll = long long; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } friend modnum inv(const modnum& m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD-v : 0; return res; } friend modnum neg(const modnum& m) { return m.neg(); } modnum operator- () const { return neg(); } modnum operator+ () const { return modnum(*this); } modnum& operator ++ () { v ++; if (v == MOD) v = 0; return *this; } modnum& operator -- () { if (v == 0) v = MOD; v --; return *this; } modnum& operator += (const modnum& o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum& operator -= (const modnum& o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum& operator *= (const modnum& o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum& operator /= (const modnum& o) { return *this *= o.inv(); } friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } }; template <typename T> T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } constexpr int MOD = 1'000'000'007; constexpr int MAXN = 5'007; int a[MAXN]; modnum<MOD> laplacian[MAXN][MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int u = 0; u < n; u++) { for (int v = u + 1; v < n; v++) { int gcd = __gcd(a[u], a[v]); laplacian[u][u] += gcd; laplacian[v][v] += gcd; laplacian[u][v] -= gcd; laplacian[v][u] -= gcd; } } modnum<MOD> det = 1; for(int col = 0; col < n - 1; ++col) { int select = col; for(int row = col + 1; row < n - 1; ++row) { if(laplacian[row][col] != 0) { select = row; break; } } if(laplacian[select][col] == 0) { det = 0; break; } swap(laplacian[col], laplacian[select]); if(select != col) { det = -det; } det *= laplacian[col][col]; for(int i = col + 1; i < n - 1; ++i) { laplacian[col][i] /= laplacian[col][col]; } for(int row = 0; row < n - 1; ++row) { if(row != col && laplacian[row][col] != 0) { for(int i = col + 1; i < n - 1; ++i) { laplacian[row][i] -= laplacian[col][i] * laplacian[row][col]; } } } } cout << det << '\n'; 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | #include <bits/stdc++.h> using namespace std; template<class c> struct rge { c b, e; }; template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template<class c> auto dud(c* x) -> decltype(cerr << *x, 0); template<class c> char dud(...); struct debug { ~debug() { cerr << endl; } template<class c> typename \ enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template<class c> typename \ enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template<class c, class b> debug & operator <<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template<class c> debug & operator <<(rge<c> d) { *this << "["; for(auto it = d.b; it != d.e; ++it) { *this << ", " + 2 * (it == d.b) << *it; } return *this << "]"; } }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // This modnum class was not created by me. However, I do not remember where I // found this code, since it was a long time ago. template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: using ll = long long; int v; static int minv(int a, int m) { a %= m; assert(a); return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); } public: modnum() : v(0) {} modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; } friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = minv(v, MOD); return res; } friend modnum inv(const modnum& m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD-v : 0; return res; } friend modnum neg(const modnum& m) { return m.neg(); } modnum operator- () const { return neg(); } modnum operator+ () const { return modnum(*this); } modnum& operator ++ () { v ++; if (v == MOD) v = 0; return *this; } modnum& operator -- () { if (v == 0) v = MOD; v --; return *this; } modnum& operator += (const modnum& o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum& operator -= (const modnum& o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum& operator *= (const modnum& o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum& operator /= (const modnum& o) { return *this *= o.inv(); } friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } }; template <typename T> T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } constexpr int MOD = 1'000'000'007; constexpr int MAXN = 5'007; int a[MAXN]; modnum<MOD> laplacian[MAXN][MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int u = 0; u < n; u++) { for (int v = u + 1; v < n; v++) { int gcd = __gcd(a[u], a[v]); laplacian[u][u] += gcd; laplacian[v][v] += gcd; laplacian[u][v] -= gcd; laplacian[v][u] -= gcd; } } modnum<MOD> det = 1; for(int col = 0; col < n - 1; ++col) { int select = col; for(int row = col + 1; row < n - 1; ++row) { if(laplacian[row][col] != 0) { select = row; break; } } if(laplacian[select][col] == 0) { det = 0; break; } swap(laplacian[col], laplacian[select]); if(select != col) { det = -det; } det *= laplacian[col][col]; for(int i = col + 1; i < n - 1; ++i) { laplacian[col][i] /= laplacian[col][col]; } for(int row = 0; row < n - 1; ++row) { if(row != col && laplacian[row][col] != 0) { for(int i = col + 1; i < n - 1; ++i) { laplacian[row][i] -= laplacian[col][i] * laplacian[row][col]; } } } } cout << det << '\n'; return 0; } |