[gcd]Codeforces Common Divisors
You are given an array aa consisting of nn integers.
Your task is to say the number of such positive integers xx such that xx divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.
For example, if the array aa will be [2,4,6,2,10][2,4,6,2,10], then 11 and 22 divide each number from the array (so the answer for this test is 22).
The first line of the input contains one integer nn (1≤n≤4⋅1051≤n≤4⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10121≤ai≤1012), where aiai is the ii-th element of aa.
Print one integer — the number of such positive integers xx such that xx divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).
5 1 2 3 4 5
1
6 6 90 12 18 30 18
4
题意:
给你n个数,找出这n个公因数的个数
思路:
这些公因数的值不会超过他们的最大公因数,因此我们要找到这n个数的最大公因数
看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn
如果这个因子的平方等于minn那就只算一个,否则找到了i是minn的因子,就可知道n/i也是minn的因子,所以要加上2个
注意:
最好用scanf,如果用cin要加上流加速,否则会TLE
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int amn=4e5+5; 5 ll a[amn]; 6 ll gcd(ll a,ll b){ 7 return !b?a:gcd(b,a%b); 8 } 9 int main(){ 10 ios::sync_with_stdio(0); ///以后要习惯性地加上流加速 11 int n; 12 cin>>n; ///以后大数据时用cin还是要加上流加速,或直接用scanf,没加前在test5总TLE 13 for(int i=1;i<=n;i++){ 14 cin>>a[i]; 15 } 16 ll minn=a[1]; 17 for(int i=2;i<=n;i++){ 18 minn=min(gcd(minn,a[i]),minn); ///找出他们中的一个最小的最大公因数,记为minn 19 if(minn==1){printf("1\n");return 0;} ///如果是1就直接输出后退出 20 } 21 // cout<<minn<<endl; 22 ll ans=0; 23 for(ll i=1;i*i<=minn;i++){ ///看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn 24 if(i*i==minn)ans++; ///如果这个因子的平方等于minn那就只算一个 25 else if(minn%i==0)ans+=2; ///否则找到了i,就可知道n/i,所以要加上2个 26 } 27 printf("%lld\n",ans); 28 } 29 /*** 30 给你n个数,找出这n个公因数的个数 31 这些公因数的值不会超过他们的最大公因数,因此我们要找到这n个数的最大公因数 32 看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn 33 如果这个因子的平方等于minn那就只算一个,否则找到了i是minn的因子,就可知道n/i也是minn的因子,所以要加上2个 34 注意最好用scanf,如果用cin要加上流加速,否则会TLE 35 ***/