#include <cstdio> #include <queue> #include <vector> #define DBG(X) using namespace std; int p[200001]; vector<int> edge[200001]; int vis[200001]; int d[200001]; int a[5001]; int g[5001][5001]; int gcd(int a, int b) { if (!b) return a; else return gcd(b, a%b); } vector<pair<int, int>> edges; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { g[i][j] = gcd(a[i], a[j]); edges.push_back({i, j}); } } vector<vector<int>> edge(n); long long ret = 0; for (int i = 0; i < (1 << edges.size()); i++) { int ile = 0; for (int j = 0; j < edges.size(); j++) { vis[j] = 0; if ((1 << j) & i) { ile++; } } if (ile != n - 1) continue; vector<vector<int>> edge(n); long long z = 1; for (int j = 0; j < edges.size(); j++) { if ((1 << j) & i) { int u = edges[j].first; int v = edges[j].second; z *= g[u][v]; z %= 1000000007; edge[u].push_back(v); edge[v].push_back(u); } } queue<int> Q; Q.push(0); while (!Q.empty()) { int u = Q.front(); vis[u] = 1; Q.pop(); for (int j = 0; j < edge[u].size(); j++) { int v = edge[u][j]; if (!vis[v]) { Q.push(v); vis[v] = 1; } } } int r = 0; for (int j = 0; j < n; j++) { r += vis[j]; } if (r != n) continue; ret += z; ret %= 1000000007; } printf("%lld\n", ret); 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 | #include <cstdio> #include <queue> #include <vector> #define DBG(X) using namespace std; int p[200001]; vector<int> edge[200001]; int vis[200001]; int d[200001]; int a[5001]; int g[5001][5001]; int gcd(int a, int b) { if (!b) return a; else return gcd(b, a%b); } vector<pair<int, int>> edges; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { g[i][j] = gcd(a[i], a[j]); edges.push_back({i, j}); } } vector<vector<int>> edge(n); long long ret = 0; for (int i = 0; i < (1 << edges.size()); i++) { int ile = 0; for (int j = 0; j < edges.size(); j++) { vis[j] = 0; if ((1 << j) & i) { ile++; } } if (ile != n - 1) continue; vector<vector<int>> edge(n); long long z = 1; for (int j = 0; j < edges.size(); j++) { if ((1 << j) & i) { int u = edges[j].first; int v = edges[j].second; z *= g[u][v]; z %= 1000000007; edge[u].push_back(v); edge[v].push_back(u); } } queue<int> Q; Q.push(0); while (!Q.empty()) { int u = Q.front(); vis[u] = 1; Q.pop(); for (int j = 0; j < edge[u].size(); j++) { int v = edge[u][j]; if (!vis[v]) { Q.push(v); vis[v] = 1; } } } int r = 0; for (int j = 0; j < n; j++) { r += vis[j]; } if (r != n) continue; ret += z; ret %= 1000000007; } printf("%lld\n", ret); return 0; } |