#include <bits/stdc++.h> using namespace std; #define ll long long #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef pair<int,int> ii; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const int MOD = 1e9+7; ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0,int shift=0){ vi v(n+shift); rep(i,shift)v[i]=0; rep(i,n)v[i+shift]=read()+off; return v; } void YesNo(bool condition, bool do_exit=true) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; if (do_exit) exit(0); } //a^b mod m, log(b) operations. //assumes a*m fits long long. long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } //modular inverse of a, using Fermat's little thm ll inverse(ll a, ll m) { return binpow (a, m-2, m); } ll det(int n, vvl & a) { ll det = 1; for (int i=0; i<n; ++i) { int k = i; while (a[k][i] == 0 and k<n-1) ++k; if (a[k][i] == 0) return 0; swap (a[i], a[k]); if (i != k) det = (MOD-det) % MOD; det = (det * a[i][i]) % MOD; for (int j=i+1; j<n; ++j) a[i][j] = (a[i][j] * inverse(a[i][i], MOD)) % MOD; for (int j=0; j<n; ++j) if (j != i && a[j][i] != 0) for (int k=i+1; k<n; ++k) a[j][k] = (a[j][k] + MOD - (a[i][k] * a[j][i]) % MOD) % MOD; } return det; } int main(void ) { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vi s = readvi(n); vvl a(n-1, vl(n-1, 0)); rep(i,n-1) rep(j,n-1) if (i != j) a[i][j] = gcd(s[i], s[j]); rep(i,n-1) { a[i][i] = MOD - gcd(s[i],s[n-1]); rep(j, n - 1) if (i != j) a[i][i] = (a[i][i] + MOD - a[i][j]) % MOD; } rep(i,n-1) rep(j,n-1) a[i][j] = (MOD - a[i][j]) % MOD; cout << det (n-1, a) << 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 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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef pair<int,int> ii; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const int MOD = 1e9+7; ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0,int shift=0){ vi v(n+shift); rep(i,shift)v[i]=0; rep(i,n)v[i+shift]=read()+off; return v; } void YesNo(bool condition, bool do_exit=true) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; if (do_exit) exit(0); } //a^b mod m, log(b) operations. //assumes a*m fits long long. long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } //modular inverse of a, using Fermat's little thm ll inverse(ll a, ll m) { return binpow (a, m-2, m); } ll det(int n, vvl & a) { ll det = 1; for (int i=0; i<n; ++i) { int k = i; while (a[k][i] == 0 and k<n-1) ++k; if (a[k][i] == 0) return 0; swap (a[i], a[k]); if (i != k) det = (MOD-det) % MOD; det = (det * a[i][i]) % MOD; for (int j=i+1; j<n; ++j) a[i][j] = (a[i][j] * inverse(a[i][i], MOD)) % MOD; for (int j=0; j<n; ++j) if (j != i && a[j][i] != 0) for (int k=i+1; k<n; ++k) a[j][k] = (a[j][k] + MOD - (a[i][k] * a[j][i]) % MOD) % MOD; } return det; } int main(void ) { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vi s = readvi(n); vvl a(n-1, vl(n-1, 0)); rep(i,n-1) rep(j,n-1) if (i != j) a[i][j] = gcd(s[i], s[j]); rep(i,n-1) { a[i][i] = MOD - gcd(s[i],s[n-1]); rep(j, n - 1) if (i != j) a[i][i] = (a[i][i] + MOD - a[i][j]) % MOD; } rep(i,n-1) rep(j,n-1) a[i][j] = (MOD - a[i][j]) % MOD; cout << det (n-1, a) << endl; return 0; } |