-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathdivisions.cc
128 lines (115 loc) · 2.57 KB
/
divisions.cc
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using namespace std;
#include <bits/stdc++.h>
#define D(x) cout << #x " = " << (x) << endl
const int rounds = 20;
// Computes (a * b) % mod
long long mod_mul(long long a, long long b, long long mod) {
long long x = 0, y = a % mod;
while (b > 0) {
if (b & 1)
x = (x + y) % mod;
y = (y * 2) % mod;
b /= 2;
}
return x % mod;
}
// Computes ( a ^ exp ) % mod.
long long mod_pow(long long a, long long exp, long long mod) {
long long ans = 1;
while (exp > 0) {
if (exp & 1)
ans = mod_mul(ans, a, mod);
a = mod_mul(a, a, mod);
exp >>= 1;
}
return ans;
}
// checks whether a is a witness that n is not prime, 1 < a < n
bool witness(long long a, long long n) {
// check as in Miller Rabin Primality Test described
long long u = n - 1;
int t = 0;
while (u % 2 == 0) {
t++;
u >>= 1;
}
long long next = mod_pow(a, u, n);
if (next == 1) return false;
long long last;
for (int i = 0; i < t; ++i) {
last = next;
next = mod_mul(last, last, n);
if (next == 1) {
return last != n - 1;
}
}
return next != 1;
}
// Checks if a number is prime with prob 1 - 1 / (2 ^ it)
// D(miller_rabin(99999999999999997LL) == 1);
// D(miller_rabin(9999999999971LL) == 1);
// D(miller_rabin(7907) == 1);
bool miller_rabin(long long n, int it = rounds) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 0; i < it; ++i) {
long long a = rand() % (n - 1) + 1;
if (witness(a, n)) {
return false;
}
}
return true;
}
long long pollard_rho(long long n) {
long long x, y, i = 1, k = 2, d;
x = y = rand() % n;
while (1) {
++i;
x = mod_mul(x, x, n);
x += 2;
if (x >= n) x -= n;
if (x == y) return 1;
d = __gcd(abs(x - y), n);
if (d != 1) return d;
if (i == k) {
y = x;
k *= 2;
}
}
return 1;
}
vector<long long> factorize(long long n) {
vector<long long> ans;
if (n == 1)
return ans;
if (miller_rabin(n)) {
ans.push_back(n);
} else {
long long d = 1;
while (d == 1)
d = pollard_rho(n);
vector<long long> dd = factorize(d);
ans = factorize(n / d);
for (int i = 0; i < dd.size(); ++i)
ans.push_back(dd[i]);
}
return ans;
}
long long num_fact(long long n) {
vector<long long> p = factorize(n);
map<long long, int> frec;
for (int i = 0; i < p.size(); ++i)
frec[p[i]]++;
long long ans = 1;
for(const auto &it : frec)
ans *= (it.second + 1);
return ans;
}
int main() {
long long n;
while (cin >> n) {
cout << num_fact(n) << endl;
}
return 0;
}