LA 3510 (displacement cycle decomposition) Pixel Shuffle

Source: Internet
Author: User
Tags shuffle

The idea is quite simple, each command in the title (including the inverse of the command) is equivalent to a permutation.

The time complexity of O (n2k) is used to find the product A of these permutations from right to left, and then m to make AM = I (i is congruent permutation)

First, the A decomposition cycle, M is equal to all the length of the Loop section least common multiple.

It is important to note that:

The execution command is executed from right to left, which is said in the title =_=

The other commands are OK, mix that command to get me confused, the title is reversed, we want to reverse the original image (I, j) in the new image position.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <string>5#include <sstream>6#include <vector>7 using namespacestd;8 9 intgcdintAintb)Ten{returnb = =0? A:GCD (b, a%b); } One  A intLcmintAintb) -{returnA/GCD (A, b) *b;} -  the Const intMAXN =1025; - intN; - Chars[ -], op[ *][Ten]; -  +InlineintID (intIintj) -{returnI*n +J;} +  A intNewpos (intIintJConst Char*op) at { -      if(op[0] =='R')returnID (n-j-1, i); -      if(op[0] =='s')returnID (i, n-j-1); -      if(op[0] =='b'&& op[1] =='h') -      { -          if(I >= n/2)returnID (i, n-j-1); in          returnID (i, j); -      } to      if(op[0] =='b'&& op[1] =='v') +      { -          if(I >= n/2)returnID (N-(i-n/2)-1, j); the          returnID (i, j); *      } $      if(op[0] =='D')Panax Notoginseng      { -          if(I &1)returnID (n/2+ i/2, j); the          returnID (i/2, j); +      } A      if(op[0] =='m') the      { +          intK = i/2; -          if(J < n/2)returnI2==0? ID (k*2, j*2): ID (k*2, j*2+1); $          Else returnI2==0? ID (2*k+1,2* (j-n/2)): ID (2*k+1,2* (j-n/2)+1); $      } -      returnID (i, j); - } the  - intCUR[MAXN * MAXN], ORIGIN[MAXN *MAXN];Wuyi  the voidApplyConst Char*op) - { Wu      for(inti =0; i < n*n; i++) Origin[i] =Cur[i]; -     BOOLINV = Op[strlen (OP)-1] =='-'?true:false; About      for(inti =0; I < n; i++) $          for(intj =0; J < N; J + +) -         { -             intp = ID (i, j), p2 =Newpos (I, J, op); -             if(INV) Cur[p] =ORIGIN[P2]; A             ElseCUR[P2] =Origin[p]; +         } the } -  $ BOOLVIS[MAXN *MAXN]; the  the intSolve () the { thememset (Vis,false,sizeof(Vis)); -     intAns =1; in      for(inti =0; i < n*n; i++)if(!Vis[i]) the     { the         intCNT =0, j =i; About          Do the         { theVIS[J] =1; thecnt++; +j =Cur[j]; -} while(J! =i); theAns =LCM (cnt, ans);Bayi     } the     returnans; the } -  - intMain () the { the     //freopen ("In.txt", "R", stdin); the  the     intT; -scanf"%d", &T); the      for(intKase =0; Kase < T; kase++) the     { the         if(Kase) puts ("");94  thescanf"%d", &N); GetChar (); the         stringLine , temp; the getline (CIN, line);98 StringStream SS (line); Aboutvector<string>op; -          while(SS >>temp) Op.push_back (temp);101 102          for(inti =0; i < n*n; i++) Cur[i] =i;103          for(inti = op.size ()-1; I >=0; i--) Apply (OP[I].C_STR ());104printf"%d\n", Solve ()); the     }106 107     return 0;108}
code June

LA 3510 (displacement cycle decomposition) Pixel Shuffle

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.