C. Common Divisors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array ?
consisting of ?
integers.
Your task is to say the number of such positive integers ?
such that ?
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 ?
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 ?
(1≤?≤4⋅105) — the number of elements in ?
.
The second line of the input contains ?
integers ?1,?2,…,?? (1≤??≤1012), where ?? is the ?-th element of ?
.
Output
Print one integer — the number of such positive integers ?
such that ?
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
Input
Copy
5
1 2 3 4 5
Output
Copy
1
Input
Copy
6
6 90 12 18 30 18
Output
Copy
4
题意:给你一组数,寻找这组数的公约数的个数;
思路:思路很简单,但是需要优化,用GCD求最大公约数,公约数因子的个数就是答案,具体优化看代码;
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define ULL unsigned long long
using namespace std;
const int maxn=4e5+50;
const int inf=0x3f3f3f3f;
long long int a[maxn];
long long int gcd(long long int n,long long int m)
{
if(m==0)
return n;
else
return gcd(m,n%m);
}
int main()
{
long long int i,n,m,j,k,l,t;
scanf("%lld",&n);
for(i=0;i<=n-1;i++)
{
scanf("%lld",&a[i]);
}
j=a[0];
for(i=1;i<=n-1;i++)
{
j=gcd(j,a[i]);
}
long long int ans=0;
for(i=1;i<=sqrt(j);i++)//开根号减少运行时间,不然会超时
{
if(j%i==0)//j%i==0,说明i*x==j.而且x>=i,因此要加入判断
{
ans++;
if(i*i!=j)
ans++;
}
}
printf("%lld\n",ans);
return 0;
}