zoukankan      html  css  js  c++  java
  • CF1203C Common Divisors

    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 eachnumber 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], 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 nn (1n4105) — the number of elements in a.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1012), where aiai is the ii-th element of a.

    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求出这个数组的最大公约数,然后只要是能被最大公约数整除的也是这个数组的公约数,如果直接遍历的话会超时,
    所以将最大公约数开方,然后从2遍历一遍找出能被整除的数然后++,因为sqrt相当于平分了,所以sqrt的右端被最大公约数整除的数跟左边是一样的,所以得乘以2,
    然后再特判一下sqrt不是能被最大公约数整除(如果最大公约数是1也要特判)
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b) //求最大公约数
    {
    	ll c;
    	while(b>0) {
    		c=a%b;
    		a=b;
    		b=c;
    	}
    	return a;
    }
    int main()
    {
      ll n,i;
      ll ans = 0;
      ll x,m = 0;
      cin>>n;
      for(i=1;i<=n;i++) {
    	scanf("%I64d",&x);
    	m=gcd(m,x);
      }
      double y = sqrt(m);
      for(i=2; i < y; i++) {
    	if(m%i==0)
         ans++;
      }
        ans *= 2;   //两边都有,所以得乘以二
        if((int)y == y&&m != 1) {  //判断sqrt是不是能被整除
            ans++;
        }
        if(m != 1) {  //特判最大公约数是不是1;
            ans += 2;
        }
        else{
            ans += 1;
        }
      cout<<ans<<endl;
      return 0;
    }
    

      

  • 相关阅读:
    poj 2186 && hdu 3836
    poj 2833 The Average
    hud 3062 Party
    论 ACM 与泡妞 (转载)
    poj 1064 Cable master
    poj Candies
    [转]人才流失的背后
    获取存储过程的ReturnValue值
    javascript js jquery获取元素位置代码总结
    【引用】xmlpath 操作 xml
  • 原文地址:https://www.cnblogs.com/clb123/p/11767790.html
Copyright © 2011-2022 走看看