Given a permutation B of 26 uppercase letters, is there A2 = b
Each permutation can be considered a product of several loops. We can think of these loops as a necklace of UVA 10294, and the number in the loop is equivalent to the beads in the necklace.
A2 is the equivalent of rotating the necklace between two beads of distance, beads 0, 2, 4 ... form a loop with a total of GCD (n, 2) loops with a length of N/GCD (n, 2) for Each loop
So when the length of a loop is odd, the square is still the original length;
When the length of a loop is even, the square is decomposed into a loop that has two lengths equal to half the original cycle length.
First, the permutation B decomposition cycle, for which the length is an odd 2k+1 cycle, can be 2k+1 by the same length of the loop squared from A2, or 2 (2k+1) decomposition of two 2k+1 cycle.
But for a loop of even 2k length, it must be decomposed by the square of the loop with a length of 4k.
So if permutation a satisfies A2 = B, then the even-numbered loop section in B must be even.
1#include <cstdio>2#include <cstring>3 4 intMain ()5 {6 //freopen ("In.txt", "R", stdin);7 8 intT;9scanf"%d", &T);Ten while(t--) One { A Chars[ -]; -scanf"%s", s); - BOOLvis[ -]; thememset (Vis,false,sizeof(Vis)); - intcnt[ -]; -memset (CNT,0,sizeof(CNT)); - + for(inti =0; I < -; i++)if(!Vis[i]) - { + intj =i; A intn =0; at Do - { -VIS[J] =1; -n++; -j = S[j]-'A'; -} while(J! =i); incnt[n]++; - } to + BOOLOK =true; - for(inti =2; I <= -; i + =2)if(Cnt[i] &1) {OK =false; Break; } theprintf"%s\n"Ok?"Yes":"No"); * } $ Panax Notoginseng return 0; -}
code June
LA 3641 (decomposition of the permutation cycle) Leonardo ' s Notebook