-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-multiplication.cpp
78 lines (69 loc) · 1.75 KB
/
number-multiplication.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vll = vector<ll>;
using ld = long double;
ll mul(ll a, ll b, ll m) {
ll ret = a*b - (ll)((ld)1/m*a*b+0.5)*m;
return ret < 0 ? ret+m : ret;
}
ll pow(ll a, ll b, ll m) {
ll ans = 1;
for (; b > 0; b /= 2ll, a = mul(a, a, m)) {
if (b % 2ll == 1)
ans = mul(ans, a, m);
}
return ans;
}
bool prime(ll n) {
if (n < 2) return 0;
if (n <= 3) return 1;
if (n % 2 == 0) return 0;
ll r = __builtin_ctzll(n - 1), d = n >> r;
for (int a : {2, 325, 9375, 28178, 450775, 9780504, 795265022}) {
ll x = pow(a, d, n);
if (x == 1 or x == n - 1 or a % n == 0) continue;
for (int j = 0; j < r - 1; j++) {
x = mul(x, x, n);
if (x == n - 1) break;
}
if (x != n - 1) return 0;
}
return 1;
}
ll rho(ll n) {
if (n == 1 or prime(n)) return n;
auto f = [n](ll x) {return mul(x, x, n) + 1;};
ll x = 0, y = 0, t = 30, prd = 2, x0 = 1, q;
while (t % 40 != 0 or gcd(prd, n) == 1) {
if (x==y) x = ++x0, y = f(x);
q = mul(prd, abs(x-y), n);
if (q != 0) prd = q;
x = f(x), y = f(f(y)), t++;
}
return gcd(prd, n);
}
vector<ll> fact(ll n) {
if (n == 1) return {};
if (prime(n)) return {n};
ll d = rho(n);
vector<ll> l = fact(d), r = fact(n / d);
l.insert(l.end(), r.begin(), r.end());
return l;
}
int main() {
ll n, m, k;
cin >> m >> n >> k;
vll primes(n);
for(auto &prime : primes) cin >> prime;
set<ll> ans;
for(auto &prime : primes) {
auto ff = fact(prime);
ans.insert(all(ff));
}
for(auto &ai : ans) cout << ai << ' ';
cout << endl;
return 0;
}
// AC, math, pollard rho