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

      

  • 相关阅读:
    thinkphp计划任务使用cronRun-Thinkphp3.1版
    AJAX 跨域请求
    可以做外汇交易接口的网站
    thinkphp 定时执行php文件 php自动执行php文件
    关于major、minor的解释
    Codeforces Round #370 (Div. 2) A B C 水 模拟 贪心
    2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873
    Codeforces Round #280 (Div. 2) A B C 暴力 水 贪心
    codevs 1299 线段树 区间更新查询
    Codeforces Round #260 (Div. 2) A B C 水 找规律(大数对小数取模) dp
  • 原文地址:https://www.cnblogs.com/clb123/p/11767790.html
Copyright © 2020-2023  润新知