#pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int MOD = 1e9+7; constexpr int DIM = 4096; constexpr int BLOCK = 8; ll modInv(ll a, ll m = MOD) { if (a == 1) return 1; return ((a - modInv(m%a, a))*m + 1) / a; } int M[DIM][DIM]; int transposed[DIM][BLOCK]; ll mults[DIM][BLOCK]; ll det(int n) { ll ret = 1; int i = 0; for (; i+BLOCK <= n; i += BLOCK) { int cut = i+BLOCK; rep(t, 0, BLOCK) { rep(j, i, n) if (M[j][i]) { if (i != j) { swap(M[i], M[j]); swap(mults[i], mults[j]); ret *= -1; } break; } if (!M[i][i]) { return 0; } if (M[i][i] < 0) { M[i][i] += MOD; } ll inv = modInv(M[i][i]); ret = ret * M[i][i] % MOD; rep(j, i+1, n) { ll mult = M[j][i] * inv % MOD; mults[j][t] = int(mult); rep(k, i, cut) { M[j][k] = int((M[j][k] - M[i][k]*mult) % MOD); } } rep(j, t, BLOCK) { mults[i][j] = 0; } i++; } i -= BLOCK; rep(s, 0, n) { transposed[s][0] = M[i][s]; } rep(j, i+1, n) { int k = cut; for (; k+2 <= n; k += 2) { ll v0 = M[j][k], v1 = M[j][k+1]; rep(t, 0, BLOCK) { v0 -= transposed[k][t] * mults[j][t]; v1 -= transposed[k+1][t] * mults[j][t]; } M[j][k] = int(v0 % MOD); M[j][k+1] = int(v1 % MOD); } for (; k < n; k++) { ll val = M[j][k]; rep(t, 0, BLOCK) { val -= transposed[k][t] * mults[j][t]; } M[j][k] = int(val % MOD); } if (j < i+BLOCK) { rep(s, 0, n) { transposed[s][j-i] = M[j][s]; } } } } for (; i < n; i++) { rep(j, i, n) if (M[j][i]) { if (i != j) { swap(M[i], M[j]); ret *= -1; } break; } if (!M[i][i]) { return 0; } if (M[i][i] < 0) { M[i][i] += MOD; } ll inv = modInv(M[i][i]); ret = ret * M[i][i] % MOD; rep(j, i+1, n) { ll mult = M[j][i] * inv % MOD; rep(k, i, n) { M[j][k] = int((M[j][k] - M[i][k]*mult) % MOD); } } } ret %= MOD; if (ret < 0) ret += MOD; return ret; } void run() { int n; cin >> n; vi elems(n); each(e, elems) cin >> e; rep(i, 0, n-1) { rep(j, i+1, n-1) { M[i][j] = M[j][i] = -__gcd(elems[i], elems[j]); M[i][i] -= M[i][j]; M[j][j] -= M[i][j]; } M[i][i] += __gcd(elems[i], elems[n-1]); } cout << det(n-1) << '\n'; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(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 | #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int MOD = 1e9+7; constexpr int DIM = 4096; constexpr int BLOCK = 8; ll modInv(ll a, ll m = MOD) { if (a == 1) return 1; return ((a - modInv(m%a, a))*m + 1) / a; } int M[DIM][DIM]; int transposed[DIM][BLOCK]; ll mults[DIM][BLOCK]; ll det(int n) { ll ret = 1; int i = 0; for (; i+BLOCK <= n; i += BLOCK) { int cut = i+BLOCK; rep(t, 0, BLOCK) { rep(j, i, n) if (M[j][i]) { if (i != j) { swap(M[i], M[j]); swap(mults[i], mults[j]); ret *= -1; } break; } if (!M[i][i]) { return 0; } if (M[i][i] < 0) { M[i][i] += MOD; } ll inv = modInv(M[i][i]); ret = ret * M[i][i] % MOD; rep(j, i+1, n) { ll mult = M[j][i] * inv % MOD; mults[j][t] = int(mult); rep(k, i, cut) { M[j][k] = int((M[j][k] - M[i][k]*mult) % MOD); } } rep(j, t, BLOCK) { mults[i][j] = 0; } i++; } i -= BLOCK; rep(s, 0, n) { transposed[s][0] = M[i][s]; } rep(j, i+1, n) { int k = cut; for (; k+2 <= n; k += 2) { ll v0 = M[j][k], v1 = M[j][k+1]; rep(t, 0, BLOCK) { v0 -= transposed[k][t] * mults[j][t]; v1 -= transposed[k+1][t] * mults[j][t]; } M[j][k] = int(v0 % MOD); M[j][k+1] = int(v1 % MOD); } for (; k < n; k++) { ll val = M[j][k]; rep(t, 0, BLOCK) { val -= transposed[k][t] * mults[j][t]; } M[j][k] = int(val % MOD); } if (j < i+BLOCK) { rep(s, 0, n) { transposed[s][j-i] = M[j][s]; } } } } for (; i < n; i++) { rep(j, i, n) if (M[j][i]) { if (i != j) { swap(M[i], M[j]); ret *= -1; } break; } if (!M[i][i]) { return 0; } if (M[i][i] < 0) { M[i][i] += MOD; } ll inv = modInv(M[i][i]); ret = ret * M[i][i] % MOD; rep(j, i+1, n) { ll mult = M[j][i] * inv % MOD; rep(k, i, n) { M[j][k] = int((M[j][k] - M[i][k]*mult) % MOD); } } } ret %= MOD; if (ret < 0) ret += MOD; return ret; } void run() { int n; cin >> n; vi elems(n); each(e, elems) cin >> e; rep(i, 0, n-1) { rep(j, i+1, n-1) { M[i][j] = M[j][i] = -__gcd(elems[i], elems[j]); M[i][i] -= M[i][j]; M[j][j] -= M[i][j]; } M[i][i] += __gcd(elems[i], elems[n-1]); } cout << det(n-1) << '\n'; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); cout << flush; _Exit(0); } |