Playing with Permutations(CF-252D)

Problem Description

Little Petya likes permutations a lot. Recently his mom has presented him permutation q1, q2, ..., qn of length n.

A permutation a of length n is a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n), all integers there are distinct.

There is only one thing Petya likes more than permutations: playing with little Masha. As it turns out, Masha also has a permutation of length n. Petya decided to get the same permutation, whatever the cost may 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 are 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.
    1)1——Let's assume that the board contains permutation p1, p2, ..., pn at the given moment. Then Petya removes the written permutation p from the board and writes another one instead: pq1, pq2, ..., pqn. In other words, Petya applies permutation q (which he has got from his mother) to permutation p.
    2)All actions are similar to point 1, except that Petya writes permutation t on the board, such that: tqi = pi for all i from 1 to n. In other words, Petya applies a permutation that is inverse to q to permutation p.

We know that after the k-th move the board contained Masha's permutation s1, s2, ..., sn. Besides, we know that throughout the game process Masha's permutation never occurred on the board before the k-th move. Note that the game has exactly k moves, that is, throughout the game the coin was tossed exactly k times.

Your task is to determine whether the described situation is possible or else state that Petya was mistaken somewhere. See samples and notes to them for a better understanding.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 100). The second line contains n space-separated integers q1, q2, ..., qn (1 ≤ qi ≤ n) — the permutation that Petya's got as a present. The third line contains Masha's permutation s, in the similar format.

It is guaranteed that the given sequences q and s are correct permutations.

Output

If the situation that is described in the statement is possible, print "YES" (without the quotes), otherwise print "NO" (without the quotes).

Examples

Input

4 1
2 3 4 1
1 2 3 4

Output

NO

Input

4 1
4 3 1 2
3 4 2 1

Output

YES

Input

4 3
4 3 1 2
3 4 2 1

Output

YES

Input

4 2
4 3 1 2
2 1 4 3

Output

YES

Input

4 1
4 3 1 2
2 1 4 3

Output

NO

Note

In the first sample Masha's permutation coincides with the permutation that was 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 were performed.

In the second sample the described situation is 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.

题意:给出两个长度为 n 的序列 q、s,问通过 p[q[i]]=p[i]、p[i]=p[q[i]] 这两种变换,使得 k 次变换后 p 序列变为 s 序列,且变换过程中,p 序列不能与 s 序列相同

思路:可以看出,给出的两个操作是相反的,也就是说,两种变换分别做一次后能够相消除,因此只需要计算出序列 p 通过第一种变换要多少次,第二种变换要多少次,然后分类讨论即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
 
int op1[N],op2[N];
int q[N],s[N];
int main() {
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
        scanf("%d",&q[i]);
    for(int i=1; i<=n; i++)
        scanf("%d",&s[i]);
 
    for(int i=1; i<=n; i++)
        op1[i]=op2[i]=i;
    int numP=0;
    while(numP<=k) {
        bool flag=1;
        for(int i=1; i<=n; i++) {
            if(op1[i]!=s[i])
                flag=0;
        }
        if(flag)
            break;
        for(int i=1; i<=n; i++)
            op1[i]=op2[q[i]];
        for(int i=1; i<=n; i++)
            op2[i]=op1[i];
        numP++;
    }
 
    for(int i=1; i<=n; i++)
        op1[i]=op2[i]=i;
    int numS=0;
    while(numS<=k) {
        bool flag=1;
        for(int i=1; i<=n; i++) {
            if(op1[i]!=s[i])
                flag=0;
        }
        if(flag)
            break;
        for(int i=1; i<=n; i++)
            op1[q[i]]=op2[i];
        for(int i=1; i<=n; i++)
            op2[i]=op1[i];
        numS++;
    }
 
    if(numP==0||numS==0)
        printf("NO\n");
    else if(k==1&&(numP==1||numS==1))
        printf("YES\n");
    else if(k!=1&&numP==1&&numS==1)
        printf("NO\n");
    else if(numP==k+1&&numS==k+1)
        printf("NO\n");
    else if((numP-k)%2==0&&numP<=k)
        printf("YES\n");
    else if((numS-k)%2==0&&numS<=k)
        printf("YES\n");
    else
        printf("NO\n");
 
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值