Beruflich Dokumente
Kultur Dokumente
Description
--------------------------------------------------------------------------------
Little Sasha likes to swap elements in his array. Initially, he has an array of
N numbers 1,2,3,4.....,N in ascending order. Then, he swaps some elements in it K
times. He really
likes this sequence of K swaps and repeats it T times. However, Sasha forgot his
favorite swap sequence the next day.
Given the resulting permutation, find the swap sequence used by Sasha or say that
there is no such sequence.
Example
Input:
5 3 2
4 3 2 1 5
Output:
1 2
2 4
3 4
Explanation
Let's look at the sequence after each swap:
--------------------------------------------------------------------------------
Input Format
--------------------------------------------------------------------------------
The first line of input contains three integers N, K, and T, respectively.
The second line contains a permutation of numbers 1,2,3,4....,N.
--------------------------------------------------------------------------------
Constraints
--------------------------------------------------------------------------------
2 ≤ N ≤ 100000
1 ≤ K ≤ 100000
1 ≤ T ≤ 1000000000
--------------------------------------------------------------------------------
Output Format
--------------------------------------------------------------------------------
Print K lines. The ith line contains two distinct integers ai, bi which means
that the i^th swap will be of ai^th and bi^th numbers. If there are multiple
possible answers, print any of them.
Otherwise, if there is no such sequence of swaps, print "no solution" without
quotes.
--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
void solve()
{
int n = ni(), K = ni(), T = ni();
int[] f = na(n);
for(int i = 0;i < n;i++)f[i]--;
int[][] cys = splitIntoCycleProfs(f);
int[] ff = new int[n+1];
for(int c : cys[2]){
ff[c]++;
}
int[] ds = new int[2000];
int p = 0;
for(int d = 1;d * d <= T;d++){
if(T % d == 0){
ds[p++] = d;
if(d*d < T)ds[p++] = T/d;
}
}
Arrays.sort(ds, 0, p);
ds = Arrays.copyOf(ds, p);
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}