#include #include #include #include "assert.h" #include #include #include #include #include #include using namespace std; #define MAX_N 100 #define MAX_K 1000 long long gcd(long long a, long long b){ if(b==0) return a; return gcd(b, a%b); } long long lcm(long long a, long long b){ if(a node; public: UnionFindTree(int n){ node.resize(n); for(int i=0; i node[y].rank){ node[y].parent = x; }else{ node[x].rank++; unite(x,y); } } }; vector func(vector a, vector b){ int n = a.size(); vector ret(n); for(int i=0; i &v){ int N = v.size(); UnionFindTree uft(N); for(int i=0; i forest; for(int i=0; isecond += 1; }else{ forest[parent] = 1; } } int ans = 1; for(auto itr = forest.begin(); itr != forest.end(); itr++){ int val = itr->second; ans = lcm(ans, val); } return ans; } vector my_pow(vector x, long long y){ int n = x.size(); vector ret(n); iota(ret.begin(), ret.end(), 0); while(y){ if(y&1LL) ret = func(ret, x); x = func(x,x); y >>= 1LL; } return ret; } //O(sqrt(z) * log(z) / 2) int baby_step_giant_step(vector x, vector y, long long z){ long long H = sqrt(z) + 1; set< pair, long long> > S; vector w = y; for(long long i=0; i x_H = my_pow(x, H); w = x_H; for(long long i=1; i<=H; i++, w = func(w, x_H)){ auto itr = S.lower_bound({w, 1LL<<60}); if(itr == S.begin()) continue; itr--; if(itr->first == w){ return H * i - itr->second; } } return k; } int main(){ int N; cin >> N; assert(2<=N && N<=MAX_N); int K; cin >> K; assert(0<=K && K<=MAX_K); vector v(N); iota(v.begin(), v.end(), 0); for(int i=0; i> x >> y; assert(1<=x && x<= N); assert(1<=y && y<= N); assert(x> Q; //assert(1<=Q && Q<=100); while(Q--){ vector B(N); for(int i=0; i> B[i]; B[i]--; } int k = baby_step_giant_step(v, B, M); cout << k << endl; /* vector check(N); iota(check.begin(), check.end(), 0); sort(B.begin(), B.end()); assert(check == B); */ } return 0; }