题意:
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/26b986f3d849439fb075a388d2a432df.png)
解法:
容易想到枚举gcd=d,然后判断d能否被生成.
这题的关键点在于:
gcd(gcd(a,b),c)=gcd(a,b,c).
因此如果我们先生成了一个数t=gcd(a,b),
后续再生成tt=gcd(t,c),
则tt=gcd(t,c)=gcd(gcd(a,b),c)=gcd(a,b,c).
因此我们能生成的数一定是序列中某些数的gcd.
对于我们要生成的gcd=d,
为了尽可能能够生成d,我们将序列中所有d的倍数都选上,
判断这些数的gcd是否为d即可.
Code:
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define int long long
#define PI pair<int, int>
const int maxm=2e6+5;
const int mod=998244353;
int n;
int a[maxm];
int cnt[maxm];
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
void solve(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cnt[a[i]]++;
int ans=0;
for(int i=1;i<=1e6;i++){
if(cnt[i])continue;
int g=0;
for(int j=i;j<=1e6;j+=i){
if(cnt[j]){
g=gcd(g,j);
}
}
if(g==i)ans++;
}
cout<<ans<<endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("../in.txt", "r", stdin);
freopen("../out.txt", "w", stdout);
#endif
#ifdef MULTI_CASE
int T;
cin >> T;
while (T--)
#endif
solve();
return 0;
}