-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC1203_CF.java
More file actions
51 lines (43 loc) · 1.63 KB
/
Copy pathC1203_CF.java
File metadata and controls
51 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C1203_CF {
static long gcd(long a, long b) {
while (b != 0) {
long t = a % b;
a = b;
b = t;
}
return a;
}
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
long[] arr=new long[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)arr[i]=Long.parseLong(st.nextToken());
long g=arr[0];
for(int i=1;i<n;i++){
g=gcd(g,arr[i]);
}
int count=0;
for(int i=1;i<=g/i;i++){
if(g%i==0){
count++;
long pair=g/i;
if(i!=pair){count++;}
}
}
System.out.println(count);
}
}
// NOTE:
// We first compute the GCD of all array elements because any number that divides
// all elements must divide their GCD. To count the number of such divisors efficiently,
// we use the fact that divisors always come in pairs: if i divides g, then g/i is also
// a divisor. Hence, we iterate only up to sqrt(g) (using i <= g/i to avoid overflow),
// and for every valid divisor i, we count both i and its paired divisor g/i. The extra
// condition (i != g/i) ensures that perfect square divisors are counted only once.
// This reduces the divisor-counting complexity from O(g) to O(sqrt(g)) and avoids
// double counting, making the solution efficient for large inputs.