发布于 

【CF1203C】Common Divisors 题解

Codeforces Round #579 (Div. 3) C题

题目大意

给定你nn个数字组成的数列,求解能将这nn个数都整除的数的个数有多少。

核心思路

找到这nn个数的gcd\gcd,然后求解这个gcd\gcd的因数个数即可。但是由于数字过大,需要用Θ(a)\Theta(\sqrt a)的复杂度求解。

nn个数的GCD求解方法可以参考:gcd(a,b,c)=gcd(gcd(a,b),c)\gcd(a,b,c)=\gcd(\gcd(a,b),c)

完整代码

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;
#define REP(i,e,s) for(register int i=e; i<=s; i++)
#define DREP(i,e,s) for(register int i=e; i>=s; i--)
#define DE(...) fprintf(stderr,__VA_ARGS__);
#define DEBUG(a) DE("DEBUG: %d\n",a)
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define ll long long

ll read() {
ll x=0,f=1,ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}

const ll INF=0x3f3f3f,MAXN=400000+10;

ll a[MAXN];

ll gcd(ll a,ll b){
ll t;
while(b) {
t=a;
a=b;
b=t%b;
}
return a;
}

ll count(ll n){
ll s=1;
for(ll i=2; i*i<=n; i++) {
if(n%i==0){
ll a=0;
while(n%i==0) {
n/=i;
a++;
}
s=s*(a+1);
}
}
if(n>1) s=s*2;
return s;
}

int main() {
ll n=read();
REP(i,1,n) a[i]=read();
ll ans=gcd(a[1],a[2]);
REP(i,3,n) ans=gcd(ans,a[i]);

printf("%lld\n",count(ans));
return 0;
}