codeforces 1203 C. Common Divisors

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值