LA 3641 (decomposition of the permutation cycle) Leonardo ' s Notebook

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.