-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
69 lines (58 loc) · 1.69 KB
/
Copy pathsolution.cpp
File metadata and controls
69 lines (58 loc) · 1.69 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution
{
public:
int minOperations(vector<int> &b)
{
const int MOD = 1000000007;
int n = b.size();
// Marks whether an index has already been included in a cycle
vector<int> vis(n, 0);
// Stores maximum exponent needed for every prime
unordered_map<int, int> mxExp;
// Find every cycle in the permutation
for (int i = 0; i < n; i++)
{
if (vis[i])
continue;
int cur = i;
int len = 0;
// Traverse one complete cycle
while (!vis[cur])
{
vis[cur] = 1;
cur = b[cur] - 1; // Convert to 0-based index
len++;
}
// Prime factorization of current cycle length
int x = len;
for (int p = 2; p * p <= x; p++)
{
if (x % p == 0)
{
int cnt = 0;
while (x % p == 0)
{
x /= p;
cnt++;
}
mxExp[p] = max(mxExp[p], cnt);
}
}
// Remaining value is also a prime
if (x > 1)
mxExp[x] = max(mxExp[x], 1);
}
long long ans = 1;
// Build the LCM using highest prime powers
for (auto &it : mxExp)
{
long long base = it.first;
int exp = it.second;
while (exp--)
{
ans = (ans * base) % MOD;
}
}
return (int)ans;
}
};