#include <bits/stdc++.h>
using namespace std;
//#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=5007;
int a[N];
ll cnt[N][N];
ll ans=0;
bool is[N];
int n;
void bt(int d,ll ways,int c,vector<int>my,vector<int>prev,int i)
{
if(c==n)
{
ans=(ans+ways)%mod;
return ;
}
if(i==n+1)
{
if(sz(my)>0) bt(d+1,ways,c,{},my,2);
return ;
}
bt(d,ways,c,my,prev,i+1);
if(!is[i])
{
is[i]=1;
ll ile=0;
for(auto j:prev) ile+=cnt[j][i];
my.pb(i);
bt(d,ways*ile%mod,c+1,my,prev,i+1);
is[i]=0;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cnt[i][j]=__gcd(a[i],a[j]);
is[1]=1;
bt(1,1,1,{},{1},2);
cout<<ans<<endl;
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 | #include <bits/stdc++.h> using namespace std; //#define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=5007; int a[N]; ll cnt[N][N]; ll ans=0; bool is[N]; int n; void bt(int d,ll ways,int c,vector<int>my,vector<int>prev,int i) { if(c==n) { ans=(ans+ways)%mod; return ; } if(i==n+1) { if(sz(my)>0) bt(d+1,ways,c,{},my,2); return ; } bt(d,ways,c,my,prev,i+1); if(!is[i]) { is[i]=1; ll ile=0; for(auto j:prev) ile+=cnt[j][i]; my.pb(i); bt(d,ways*ile%mod,c+1,my,prev,i+1); is[i]=0; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cnt[i][j]=__gcd(a[i],a[j]); is[1]=1; bt(1,1,1,{},{1},2); cout<<ans<<endl; return 0; } |
English