-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path7718. Number of common divisors.cpp
62 lines (60 loc) · 1.21 KB
/
7718. Number of common divisors.cpp
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
52
53
54
55
56
57
58
59
60
61
62
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
#define OM 1000000
bitset < OM + 1 > sieve;
vector < int > pl;
void sieveGen(){
sieve.flip();
sieve.reset(0);
sieve.reset(1);
for( int k = 2; k <= OM; k++ )
if(sieve.test(k)){
pl.push_back(k);
if(k <= sqrt(OM))
for(int i = k + k; i <= OM; i += k )
sieve.reset(i);
}
}
int divisors(int n){
int ans = 1;
int num;
for(int k = 0; k < (int) pl.size() && pl[k]<= n; k++ ){
num = 0;
while(n % pl[k] == 0){num++; n /= pl[k];}
ans *= (num+1);
}
return ans;
}
int gcd(int a, int b ){
if( b == 0 ) return a;
return gcd(b, a%b);
}
int main(){
#ifndef ONLINE
//freopen("input.txt", "r", stdin);
#endif
sieveGen();
int test, a,b;
scanf("%d",&test);
while(test--){
scanf("%d %d", &a, &b);
printf("%d\n", divisors(gcd(a,b)));
}
return 0;
}