zoukankan      html  css  js  c++  java
  • B

    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 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 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 (1n41051≤n≤4⋅105) — the number of elements in aa.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai10121≤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

    Input
    5
    1 2 3 4 5
    
    Output
    1
    
    Input
    6
    6 90 12 18 30 18
    
    Output
    4
    题意:求所给数的最大公因数,gcd算法就可以,然后直接求最大公因数的因子个数
    #include<iostream>
    #include<cstring>
    using namespace std;
    const int maxn=4e5+10;
    typedef long long ll;
    ll a[maxn];
    int x,y;
    ll gcd(ll a,ll b)                 //自定义函数求最大公约数
    {
        if(a%b==0)                    //终止条件
            return b;                   //b及为最大公约数
        else
            return gcd(b,a%b);
    }
    int main()
    {
        ll n;
        cin>>n;
        for(int i=0;i<n;i++) 
           cin>>a[i]; 
        ll k=a[0];
        for(int i=1;i<n;i++) 
           k=gcd(k,a[i]);
        int ans = 0;
        for (int i = 1; i * 1ll * i <= k; ++i) 
        {
            if (k % i == 0) 
            {
                ans++;
                if (i != k / i) //不能写i*i!=k,会溢出
                {
                    ans++;
                }
            }
        }
        cout << ans << endl;
    return 0;
    } 
    
    
  • 相关阅读:
    Web 应用的 UML 建模与 .NET 框架开发
    UML类详解
    学习Pythod 有感
    树形结构应用技巧
    面向对象的设计原则-类设计原则
    prototype 学习
    php framework kohana 学习2
    用批命令更新数据库
    HashTable
    jquery 调用wcf project
  • 原文地址:https://www.cnblogs.com/ylrwj/p/11773432.html
Copyright © 2011-2022 走看看