#include<bits/stdc++.h> using namespace std; using ll=long long; constexpr int mod=1e9+7; ll res=1; ll cnt[5001][5001]; bool ok[5001][5001]; int v[5001]; int gcd(int a, int b){ return !b?a:gcd(b,a%b); } ll exp(ll a, ll w){ ll ans=1; while(w){ if(w&1) ans=a*ans%mod; a=a*a%mod; w>>=1; } return ans; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin>>n; for(int i=1;i<=n;++i) cin>>v[i]; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(i!=j) cnt[i][j]=gcd(v[i],v[j]); ll TOTAL_ST=exp(n,n-2); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(i!=j and cnt[i][j]>1 and !ok[j][i]){ res=(res*cnt[i][j])%mod; ok[i][j]=1; } cout<<( (TOTAL_ST-n)*res%mod ); }
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 | #include<bits/stdc++.h> using namespace std; using ll=long long; constexpr int mod=1e9+7; ll res=1; ll cnt[5001][5001]; bool ok[5001][5001]; int v[5001]; int gcd(int a, int b){ return !b?a:gcd(b,a%b); } ll exp(ll a, ll w){ ll ans=1; while(w){ if(w&1) ans=a*ans%mod; a=a*a%mod; w>>=1; } return ans; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin>>n; for(int i=1;i<=n;++i) cin>>v[i]; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(i!=j) cnt[i][j]=gcd(v[i],v[j]); ll TOTAL_ST=exp(n,n-2); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(i!=j and cnt[i][j]>1 and !ok[j][i]){ res=(res*cnt[i][j])%mod; ok[i][j]=1; } cout<<( (TOTAL_ST-n)*res%mod ); } |