File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+
4+ #define endl ' \n '
5+ #define D (x ) cout << #x << " = " << (x) << endl;
6+
7+ int mcm (int a, int b) {
8+ return (a * b) / __gcd (a, b);
9+ }
10+
11+ int main () {
12+ ios_base::sync_with_stdio (0 ); cin.tie (0 );
13+ int n;
14+ while (cin >> n) {
15+ if (!n) break ;
16+
17+ vector<int > v (n);
18+ for (auto &i: v) cin >> i;
19+
20+ vector<set<int >> count (n);
21+ vector<int > cur (n), next (n);
22+
23+ for (int i = 1 ; i <= n; ++i) cur[i - 1 ] = i;
24+
25+ vector<int > cycle (n, 0 );
26+ bool found = true ;
27+ while (found) {
28+ found = false ;
29+ for (int i = 0 ; i < n; ++i) {
30+ int t = cur[v[i] - 1 ];
31+ next[i] = t;
32+ if (count[i].count (t) == 0 ) {
33+ count[i].insert (t);
34+ cycle[i] ++;
35+ found = true ;
36+ }
37+ }
38+ cur = next;
39+ }
40+
41+ set<int > st (cycle.begin (), cycle.end ());
42+ int ans = 1 ;
43+ for (auto i: st) {
44+ ans = mcm (ans, i);
45+ }
46+
47+ cout << ans << endl;
48+ }
49+
50+ return 0 ;
51+ }
You can’t perform that action at this time.
0 commit comments