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;
    } 
    
    
  • 相关阅读:
    sudo killall -9 php
    php 读取Excel内容时 对时间的值进行格式化处理
    执行Git命令时出现各种 SSL certificate problem 的解决办法
    git 强制拉取更新,本地修改
    PHP:如何合并多维数组中的子数组
    hibernate.validator 与 jackson
    jackson实体转json时 为NULL不参加序列化的汇总
    application.yml 增加数据库连接,重启日志卡死
    logback学习
    注解@Slf4j
  • 原文地址:https://www.cnblogs.com/ylrwj/p/11773432.html
Copyright © 2011-2022 走看看