#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
long long n, wyn, tab[1009], tac[1009][1009], pruf[1009], cnt[1009];
priority_queue<int, vector<int>, greater<int> > q;
const long long M = 1e9 + 7;
long long NWD(long long a, long long b)
{
if(b == 0) return a;
if(a < b) swap(a, b);
return NWD(b, a % b);
}
void prufer(int u)
{
if(u == n-1)
{
long long a, b, c = 1;
for(int i=1; i<=n-2; i++) cnt[pruf[i]]++;
for(int i=1; i<=n; i++) if(cnt[i] == 0) q.push(i);
for(int i=1; i<=n-2; i++)
{
a = q.top();
q.pop();
cnt[pruf[i]]--;
if(cnt[pruf[i]] == 0) q.push(pruf[i]);
c = (c * tac[pruf[i]][a]) % M;
}
a = q.top();
q.pop();
b = q.top();
q.pop();
c = (c * tac[a][b]) % M;
wyn = (wyn + c) % M;
return;
}
for(int i=1; i<=n; i++)
{
pruf[u] = i;
prufer(u+1);
}
return;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int a, b;
cin>>n;
wyn = 0;
for(int i=1; i<=n; i++)
{
cin>>tab[i];
for(int j=1; j<i; j++) tac[i][j] = tac[j][i] = NWD(tab[i], tab[j]);
}
prufer(1);
cout<<wyn;
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 | #include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; long long n, wyn, tab[1009], tac[1009][1009], pruf[1009], cnt[1009]; priority_queue<int, vector<int>, greater<int> > q; const long long M = 1e9 + 7; long long NWD(long long a, long long b) { if(b == 0) return a; if(a < b) swap(a, b); return NWD(b, a % b); } void prufer(int u) { if(u == n-1) { long long a, b, c = 1; for(int i=1; i<=n-2; i++) cnt[pruf[i]]++; for(int i=1; i<=n; i++) if(cnt[i] == 0) q.push(i); for(int i=1; i<=n-2; i++) { a = q.top(); q.pop(); cnt[pruf[i]]--; if(cnt[pruf[i]] == 0) q.push(pruf[i]); c = (c * tac[pruf[i]][a]) % M; } a = q.top(); q.pop(); b = q.top(); q.pop(); c = (c * tac[a][b]) % M; wyn = (wyn + c) % M; return; } for(int i=1; i<=n; i++) { pruf[u] = i; prufer(u+1); } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a, b; cin>>n; wyn = 0; for(int i=1; i<=n; i++) { cin>>tab[i]; for(int j=1; j<i; j++) tac[i][j] = tac[j][i] = NWD(tab[i], tab[j]); } prufer(1); cout<<wyn; return 0; } |
polski