A - Common Divisors

You are given an array aa consisting of n integers.

Your task is to say the number of such positive integers x such that x 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 a will be [2,4,6,2,10] , then 1 and 2 divide each number from the array (so the answer for this test is 2).

Input

The first line of the input contains one integer n (1n41051≤n≤4⋅105 ) — the number of elements in a .

The second line of the input contains n integers a1,a2,…,an (1ai10^12 ), where ai is the i -th element of a .

Output

Print one integer — the number of such positive integers x such that x divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).

Examples

 1 Input
 2 5
 3 1 2 3 4 5
 4 Output
 5 1
 6 Input
 7 6
 8 6 90 12 18 30 18
 9 Output
10 4

题目大意:

有N个数,求N个数的共同的因数的个数。

思路

先用辗转相除法求出它们的最大公约数,然后求出最大公约数的所有因数的个数。

代码如下:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 using namespace std;
 5 #define ll long long
 6 
 7 ll  gcd(ll a,ll b){   
 8 return b==0?a:gcd(b,a%b);
 9 }
10 
11 int main()
12 {
13     int n;
14     ll maxx,a;
15     scanf("%d%lld",&n,&maxx);
16     for(ll i=1;i<n;i++){
17             scanf("%lld",&a);
18         maxx=gcd(a,maxx);  //求出最大公约数
19     }
20     ll sum=0;
21 
22     for(ll i=1;i*i<=maxx;i++){   //求出最大公约数的因子个数
23         if(maxx%i==0){
24             if(i*i==maxx){
25                 sum++;
26 
27             }else{
28             sum+=2;
29             }
30         }
31     }
32     printf("%lld\n",sum);
33     return 0;
34 }

猜你喜欢

转载自www.cnblogs.com/lovelycaier/p/12676574.html
今日推荐