Http://codeforces.com/problemset/problem/251/B a chain of questions;
B. Playing with permutationstime limit per test2 secondsmemory limit per test256 megabytesinputstandard Inputoutputstandar D Output
Little Petya likes permutations a lot. Recently his mom have presented him permutation q1,? Q2,?...,? Q n of length n.
Apermutationaof lengthNis a sequence of integers a1,? a 2,?...,? a N (1?≤? ) Ai? ≤? n), all integers there is distinct.
The Re is a thing Petya likes more than permutations:playing with little Masha. As it turns out, Masha also have a permutation of Length n . Petya decided to get the same permutation and whatever the cost of be. For that, he devised a game with the following rules:
- Before the beginning of the game Petya writes permutation 1,?2,?...,? n on the blackboard. After that Petya makes exactly K moves, which is described below.
- During a move Petya tosses a coin. If the coin shows heads, he performs point 1, if the coin shows tails, he performs point 2.
- Let's assume that the board contains permutation P1,? P 2,?...,? P N At the given moment. Then Petya removes the written permutationPFrom the board and writes another one instead: P q 1 ,? p q 2 ,?...,? p q n . In other words, Petya applies PermutationQ(which he had got from the mother) to permutationP.
- All actions is similar to point 1, except that Petya writes permutationTOn the board, such that: tqi? =? P I For allIFrom 1 toN. In and words, Petya applies a permutation that's inverse toQto permutationP.
We know that after thek-th Move the board contained Masha ' s permutation s1,? s 2,?...,? s N . Besides, we know that throughout the game process Masha ' s permutationnever occurred on the boardBefore thek-th move. Note that the game has exactlykMoves, that's, throughout the game the coin was tossed exactlykTimes.
Your task is to determine whether the described situation are possible or else state, that Petya was mistaken somewhere. See samples and notes to them for a better understanding.
Input
The first line contains integersNandk(1?≤? n,? k. ≤?100). The second line containsNspace-separated integers q1,? Q 2,?...,? Q N (1?≤? Q i? ≤? N )-the permutation that Petya ' s got as a present. The third line contains Masha ' s permutations, in the similar format.
It is guaranteed, the given sequences Q and s is correct permutations.
Output
If the situation is described on the statement is possible, print "YES" (without the quotes), otherwise prin T "NO" (without the quotes).
Sample Test (s) input
4 12 3 4 11 2 3 4
Output
NO
Input
4 14 3 1 23 4 2 1
Output
YES
Input
4 34 3 1 23 4 2 1
Output
YES
Input
4 24 3 1 22 1 4 3
Output
YES
Input
4 14 3 1 22 1 4 3
Output
NO
Note
In the first sample Masha's permutation coincides with the permutation that is written on the board before the beginning of the game. Consequently, that violates the condition that Masha ' s permutation never occurred on the board before K moves wer E performed.
In the second sample the described situation are possible, in case if after we toss a coin, we get tails.
In the third sample the possible coin tossing sequence is:heads-tails-tails.
In the fourth sample the possible coin tossing sequence is:heads-heads.
Test instructions: Feel the topic completely do not understand Ah,, suddenly came out a T.
Test instructions is, give the Q series, and S series. The P sequence is then initially 1-n. Then through the p[q[i]]=p[i], or p[i]=p[q[i] "These two transformations, ask whether it is possible after the K-transform just p sequence is the S series. And in this K-change process, the P-sequence cannot equal the S-series. The P sequence does not begin with the S-series.
Procedure: Since two transformations are reversed, they can be offset by two different transformations. It is calculated how many steps p can achieve by the first transformation to the S number, then the second is how many steps, then the classification of the discussion.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #define INF 999999999#define EPS 0.00001#define LL __int64 #define PI ACOs ( -1.0) int pp[1010],p[1010];int q[1010],s[1010];int main () {int n,k;while (scanf ("%d%d", &n,&k)! = EOF) {for (int i=1;i<=n;i++) scanf ("%d", &q[i]), and for (int i=1;i<=n;i++) scanf ("%d", &s[i]); for (int i=1;i<=n;i++) pp[i]=p[i]=i; int Flag;int t1=0;while (t1<=k)//tou{flag=1;for (int i=1;i<=n;i++) {if (pp[i]!=s[i]) flag=0;} if (flag) break; for (int i=1;i<=n;i++) pp[i]=p[q[i]];for (int i=1;i<=n;i++) p[i]=pp[i]; t1++;} for (int i=1;i<=n;i++) Pp[i]=p[i]=i;int t2=0;while (t2<=k)//tou{flag=1;for (int i=1;i<=n;i++) {if (pp[i]!=s[i]) flag=0;} if (flag) break; for (int i=1;i<=n;i++) pp[q[i]]=p[i];for (int i=1;i<=n;i++) p[i]=pp[i]; t2++;} if (t1==0| | t2==0)//start with the same printf ("no\n"); else if (k==1&& (t1==1| | T2==1)//One Step as printf ("yes\n"), else if (k!=1&&t1==1&&t2==1) printf ("no\n"); else if (t1==k+1&& t2==k+1) printf ("no\n"), Else if ((t1-k)%2==0&&t1<=k) printf ("yes\n"); else if ((t2-k)%2==0&&t2< =k) printf ("yes\n"); elseprintf ("no\n"); }return 0;} /*4 12 3 4 11 2 3 4*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
CF 251 B Playing with permutations violence classification discussion