#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7, LIM=5e3+7; ll T[LIM], n, ans; void rowne() { ans=1; rep(i, n-2) ans=(ans*n)%MOD; rep(i, n-1) ans=(ans*T[0])%MOD; cout << ans << '\n'; } void rek(vector<int>spojne, int a, int b, int d, ll x) { if(d==0) { ans=(ans+x)%MOD; return; } ++b; if(b==n) { ++a; b=a+1; } while(a<n-1) { if(spojne[a]!=spojne[b]) { vector<int>nowe=spojne; rep(i, n) if(nowe[i]==spojne[b]) nowe[i]=spojne[a]; rek(nowe, a, b, d-1, (x*gcd(T[a], T[b]))%MOD); } ++b; if(b==n) { ++a; b=a+1; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; rep(i, n) cin >> T[i]; sort(T, T+n); if(T[0]==T[n-1]) { rowne(); return 0; } vector<int>V; rep(i, n) V.pb(i); rek(V, 0, 0, n-1, 1); cout << ans << '\n'; }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7, LIM=5e3+7; ll T[LIM], n, ans; void rowne() { ans=1; rep(i, n-2) ans=(ans*n)%MOD; rep(i, n-1) ans=(ans*T[0])%MOD; cout << ans << '\n'; } void rek(vector<int>spojne, int a, int b, int d, ll x) { if(d==0) { ans=(ans+x)%MOD; return; } ++b; if(b==n) { ++a; b=a+1; } while(a<n-1) { if(spojne[a]!=spojne[b]) { vector<int>nowe=spojne; rep(i, n) if(nowe[i]==spojne[b]) nowe[i]=spojne[a]; rek(nowe, a, b, d-1, (x*gcd(T[a], T[b]))%MOD); } ++b; if(b==n) { ++a; b=a+1; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; rep(i, n) cin >> T[i]; sort(T, T+n); if(T[0]==T[n-1]) { rowne(); return 0; } vector<int>V; rep(i, n) V.pb(i); rek(V, 0, 0, n-1, 1); cout << ans << '\n'; } |