zoukankan      html  css  js  c++  java
  • [gcd]Codeforces Common Divisors

    Common Divisors
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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
    Copy
    5
    1 2 3 4 5
    
    output
    Copy
    1
    
    input
    Copy
    6
    6 90 12 18 30 18
    
    output
    Copy
    4

    题意:

    给你n个数,找出这n个公因数的个数

    思路:

    这些公因数的值不会超过他们的最大公因数,因此我们要找到这n个数的最大公因数
    看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn
    如果这个因子的平方等于minn那就只算一个,否则找到了i是minn的因子,就可知道n/i也是minn的因子,所以要加上2个


    注意:

    最好用scanf,如果用cin要加上流加速,否则会TLE

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int amn=4e5+5;
     5 ll a[amn];
     6 ll gcd(ll a,ll b){
     7     return !b?a:gcd(b,a%b);
     8 }
     9 int main(){
    10     ios::sync_with_stdio(0);    ///以后要习惯性地加上流加速
    11     int n;
    12     cin>>n;                 ///以后大数据时用cin还是要加上流加速,或直接用scanf,没加前在test5总TLE
    13     for(int i=1;i<=n;i++){
    14         cin>>a[i];
    15     }
    16     ll minn=a[1];
    17     for(int i=2;i<=n;i++){
    18         minn=min(gcd(minn,a[i]),minn); ///找出他们中的一个最小的最大公因数,记为minn
    19         if(minn==1){printf("1
    ");return 0;}    ///如果是1就直接输出后退出
    20     }
    21 //    cout<<minn<<endl;
    22     ll ans=0;
    23     for(ll i=1;i*i<=minn;i++){      ///看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn
    24         if(i*i==minn)ans++;         ///如果这个因子的平方等于minn那就只算一个
    25         else if(minn%i==0)ans+=2;   ///否则找到了i,就可知道n/i,所以要加上2个
    26     }
    27     printf("%lld
    ",ans);
    28 }
    29 /***
    30 给你n个数,找出这n个公因数的个数
    31 这些公因数的值不会超过他们的最大公因数,因此我们要找到这n个数的最大公因数
    32 看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn
    33 如果这个因子的平方等于minn那就只算一个,否则找到了i是minn的因子,就可知道n/i也是minn的因子,所以要加上2个
    34 注意最好用scanf,如果用cin要加上流加速,否则会TLE
    35 ***/
  • 相关阅读:
    NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用
    《powershell 的版本号所引起的载入 FSharp 编译器问题》基本解决
    hdu 2055 An easy problem (java)
    昨天登陆页面,无法进入后台,今天攻克了
    关于ubuntu下qt编译显示Cannot connect creator comm socket /tmp/qt_temp.xxx/stub-socket的解决的方法
    (转)Struts2的拦截器
    (转)Struts2的标签库
    (转)OGNL与值栈
    (转)Struts2访问Servlet的API及......
    (转)Struts2快速入门
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11356339.html
Copyright © 2011-2022 走看看