-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeys.cpp
108 lines (99 loc) · 2.72 KB
/
monkeys.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
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
///////////////// ____ ___ _ _ _ *******************************
///////////////// | _ \_ _/ | || | *******************************
///////////////// | |_) | || | || |_ *******************************
///////////////// | __/| || |__ _| *******************************
///////////////// |_| |___|_| |_| *******************************
#include<bits/stdc++.h>
// #include<iostream>
// #include<vector>
// #include<stack>
// #include<queue>
// #include<unordered_map>
// #include<set>
// #include<algorithm>
using namespace std;
#define int int64_t
#define ll long long
#define ull unsigned long long
#define GO_BABY_GO ios::sync_with_stdio(false); cin.tie(NULL);
#define fo(i,a,b) for(ll i=a;i<b;i++)
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define endl "\n"
// struct Point
// {
// int x, y;
// };
// ll getInt() {
// ll t;
// cin>>t;
// return t;
// }
// vll getVector(int n) {
// vll temp(n);
// fo(i,0,n) {
// cin>>temp[i];
// }
// return temp;
// }
// void printVector(vector<ll> a) {
// fo(i,0,a.size()) {
// cout<<a[i]<<" ";
// }
// cout<<endl;
// }
ll min(ll a, ll b) {return a<b?a:b;}
ll max(ll a, ll b) {return a>b?a:b;}
/**
* Limits in C++ for reference
* _____________________________________________________________________________________
* |Sr| Macro Name | Description | Value
* |No|____________|_________________________________|__________________________________
* |1.| ULLONG_MAX | Maximum value unsigned long long| 18,446,744,073,709,551,615 (10^20)
* |2.| LLONG_MAX | Maximum value long long | 9,223,372,036,854,775,807 (10^19)
* |3.| LLONG_MIN | Minimum value long long |-9,223,372,036,854,775,808 -1*(10^19)
* |4.| INT_MAX | Maximum value int | 2,147,483,647 (10^10)
* |5.| INT_MIN | Minimum value int |-2,147,483,648 (10^10)
*/
ull gcd(ull a, ull b) {
if (b == 0) return a;
return gcd(b, a%b);
}
void solve() {
int n;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;i++) {
int a;
cin>>a;
nums[i] = a-1;
}
// vector<int> times;
ull ret=1;
vector<bool> seen(n, false);
for(int i=0;i<n;i++) {
if(seen[i]) {
continue;
}
int count=1;
int j = i;
while(nums[j]!=i) {
j=nums[j];
count++;
seen[j] = true;
}
// times.push_back(count);
ret = ret*(count/gcd(ret,count));
}
cout<<ret<<endl;
}
int32_t main() {
GO_BABY_GO;
ll t ;
cin>>t;
while(t--) {
solve();
}
return 0;
}