CF #579 (Div. 3) C.Common Divisors

自闭症网瘾萝莉.ら 提交于 2019-11-27 06:27:26

C.Common Divisors

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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).

input

5
1 2 3 4 5

output

1

input

6
6 90 12 18 30 18

output

4

题意就是找一组数的所有公因子的个数。

感觉就是这组数最大公因数的因子个数。
用试除法,根号的复杂度,完全没问题。

大概坑点就是为了求公因数方便直接读入了2个数但是忘掉了n==1的情况!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
 
 
 
ll div(ll x) {
    if(x==1){
        return 1;
    }
    ll ans=0;
    ll tmp=sqrt(x);
 
    for(ll i=1;i<=tmp;i++) {
        if(i*i==x){
            ans++;
            continue;
        }
        if(x%i==0){
            ans+=2;
        }
    }
    return ans;
}
 
int main()
{
    int n;
    ll t,a,b;
    scanf("%d",&n);
    if(n==1){
        scanf("%lld",&a);
        ll fk=div(a);
        cout<<fk<<endl;
        return 0;
    }
    scanf("%lld%lld",&a,&b);
    t=gcd(a,b);
    for(int i=0;i<n-2;i++){
        scanf("%lld",&a);
        t=gcd(t,a);
    }
    ll res=div(t);
    cout<<res<<endl;
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!