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][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 (1≤n≤4⋅1051≤n≤4⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10121≤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
Input5 1 2 3 4 5Output
1Input
6 6 90 12 18 30 18Output
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; }
标签:integers,xx,ll,number,最大公约数,Common,array,CF1203C,Divisors 来源: https://www.cnblogs.com/clb123/p/11767790.html