generated from TNFSH-Programming-Contest/tps-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbad-factoring.cpp
58 lines (52 loc) · 1.04 KB
/
bad-factoring.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
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
ll N;
ll fpow(ll p)
{
ll a = 10 % N, b = 1 % N, r = 0;
while (p)
{
if (p & 1)
r = (a * r + b) % N;
b = (b * a + b) % N;
a = a * a % N;
p >>= 1;
}
return r;
}
const ll B = 100'000;
vector<int> factor(ll M)
{
vector<int> f;
for (int i = 2; i <= min(M, B); i++)
while(M % i == 0)
f.emplace_back(i), M /= i;
if (M > 1)
f.emplace_back(M);
return f;
}
signed main()
{
cin >> N;
ll cur = 0;
if (__gcd(N, 10LL) != 1)
{
cout << -1 << '\n';
return 0;
}
vector<int> f = factor(N);
ll phi = N;
f.resize(unique(f.begin(), f.end()) - f.begin());
for (auto p : f)
phi = phi / p * (p - 1);
f = factor(phi);
f.resize(unique(f.begin(), f.end()) - f.begin());
for (auto p : f)
while(phi % p == 0 && fpow(phi / p) == 0)
phi /= p;
cout << phi << '\n';
}