B - Common Divisors (codeforces) fundamental theorem of number theory algorithms, unique decomposition theorem template

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][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 

  seeking the number of common factors of a set of numbers.
  Look at the data, a violent certainly see a timeout. Like the pig brain, after reading this question, my first reaction, actually only know that these factors must be smaller than the minimum number of (nonsense). The results of thinking has been restricted around the inside out.
For a year, and it is still .... Oh, efforts to train it, the problem solution.
  The following two solutions are necessary to obtain the greatest common factor of this set of numbers. Why, to find the greatest common factor of the total N, then the remaining common factor must be smaller than it. This can even set the number divisible by N, then it must be divisible by N factor.
The next job is to find the number N of factors.
  Solution one: directly to, but not a violent one for, or time out. You know, a factor of the number N to sqrt (N) as the dividing line, the left half, the right half. So we just need to find <sqrt (n) of section ANS,
ANS * 2, then sqrt (N) special judge and see whether (int) sqrt (N) * (int) sqrt (N) == N .
  
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=4e5+10;
ll tot=0;
ll a[maxn];
ll cha[maxn];
ll b[maxn];
int main()
{ 
     ll n;
     while(cin>>n)
     {
         for(int i=0;i<n;i++)
             scanf("%lld",&a[i]);
         ll k=a[0];
         for(int i=1;i<n;i++)
             k=__gcd(k,a[i]);
         if(k==1)
         {
             printf("1\n");
             continue;
         }
        double mid=sqrt(k);
        ll ans=0;
        for(int i=1;i<mid;i++)
        {
            if(k%i==0)
                ans++;
        }
        ans*=2;
        if((int)mid*mid==k)
            ans++;
        cout<<ans<<endl;
     }
}
  Solution two: number theory. Fundamental Theorem of Arithmetic (excerpt from csdn):

 

 Therefore, decomposition of N:

  

ll ans=1;    
        for(ll i=2;i*i<=t;i++)
        {
            if(t%i==0)
            {
                ll cnt=0;
                while(t%i==0)
                {
                    t=t/i;
                    cnt++;
                }
                ans=ans*(cnt+1);
            }
        }
        if(t>1)  //别忘了加
            ans*=2;
// regarded as a template unique decomposition theorem

 

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=4e5+10;
ll a[maxn];
int main()
{ 
     ll n;
     cin>>n;
     for(int i=0;i<n;i++)
         cin>>a[i];
     ll t=a[0];
     for(int i=1;i<n;i++)
         t=__gcd(t,a[i]);
    if(t==1)
        cout<<"1"<<endl;
    else
    {
        ll ans=1;
        for(ll i=2;i*i<=t;i++) 
        {
            if(t%i==0)
            {
                ll cnt=0;
                while(t%i==0)
                {
                    t =t/i; 
                    cnt ++ ; 
                } 
                Years = years * (cnt + 1 ); 
            } 
        } 
        If (t> 1 ) 
            year * = 2 ; 
        cout << age << endl; 
    } 
}
 
  

 

 

 

 

Not much to say, and then training! Acm not give his career regret!

Guess you like

Origin www.cnblogs.com/liyexin/p/11771809.html