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 eachnumber 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).
Input
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.
Output
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).
Examples
5 1 2 3 4 5
1
6 6 90 12 18 30 18
4题意:求数组公约数的数量;思路:先用gcd求出这个数组的最大公约数,然后只要是能被最大公约数整除的也是这个数组的公约数,如果直接遍历的话会超时,所以将最大公约数开方,然后从2遍历一遍找出能被整除的数然后++,因为sqrt相当于平分了,所以sqrt的右端被最大公约数整除的数跟左边是一样的,所以得乘以2,然后再特判一下sqrt不是能被最大公约数整除(如果最大公约数是1也要特判)
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll gcd(ll a,ll b) //求最大公约数 { ll c; while(b>0) { c=a%b; a=b; b=c; } return a; } int main() { ll n,i; ll ans = 0; ll x,m = 0; cin>>n; for(i=1;i<=n;i++) { scanf("%I64d",&x); m=gcd(m,x); } double y = sqrt(m); for(i=2; i < y; i++) { if(m%i==0) ans++; } ans *= 2; //两边都有,所以得乘以二 if((int)y == y&&m != 1) { //判断sqrt是不是能被整除 ans++; } if(m != 1) { //特判最大公约数是不是1; ans += 2; } else{ ans += 1; } cout<<ans<<endl; return 0; }