exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
ceb8eea02796379c37bc0e7836278a41
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
//package Algorithm; import java.awt.List; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; import java.util.PriorityQueue; import java.util.Queue; import java.util.StringTokenizer; import javax.xml.stream.events.StartDocument; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); int T = Integer.parseInt(st.nextToken()); for (int test = 1; test <= T; test++) { st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); bw.write(N+"\n"); int[] seq = new int[N + 1]; for (int i = 1; i <= N; i++) { seq[i] = i; } for(int i=1;i<=N;i++) { bw.write(seq[i]+" "); } bw.write("\n"); for (int j = 2; j <= N; j++) { int tmp = seq[j]; seq[j] = seq[j-1]; seq[j-1] = tmp; for (int i = 1; i <= N; i++) { bw.write(seq[i] + " "); } bw.write("\n"); } } bw.flush(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
98aad397caf711173d0b133e5e18d4d8
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class main { static long mod = (int)1e9+7; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=i+1; } out.println(n); for(int x:arr) { out.print(x+" "); } out.println(); for(int i=0;i<n-1;i++) { int temp = arr[i]; arr[i] = arr[n - 1]; arr[n - 1] = temp; for(int x:arr) { out.print(x+" "); } out.println(); } } out.flush(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } static class FenwickTree { //Binary Indexed Tree //1 indexed public int[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new int[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public int find(int i) { int res = 0; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public int find(int l, int r) { return find(r)-find(l-1); } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } private static long mergeAndCount(int[] arr, int l, int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l;long swaps = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static long mergeSortAndCount(int[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree long count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static void my_sort(long[] arr) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
37e133b0421f58d378a33dd7f090229b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class MyClass { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; System.out.println(n); for(int i=0;i<n;i++) arr[i]=i+1; for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); for(int i=1;i<n;i++){ int temp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=temp; String s=""; for(int j=0;j<arr.length;j++) s+=arr[j]+" "; System.out.println(s); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
8cccd704aa964db5e9892d845a07296f
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class practice{ public static FastReader sc; public static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer str; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (str == null || !str.hasMoreElements()){ try{ str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation){ lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException lastMonthOfVacation){ lastMonthOfVacation.printStackTrace(); } return str; } } public static void main(String[] args) throws IOException{ out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int t = sc.nextInt(); for(int i = 0; i<t; i++){ int n = sc.nextInt(); int[] Array = new int[n+1]; out.println(n); for(int j = 1; j<=n ; j++) { Array[j] = j; out.print(Array[j]+" "); } out.println(); for(int k = 1; k<n ; k++){ int temp = Array[k]; Array[k] = Array[k+1]; Array[k+1] = temp; for(int p = 1; p<=n ; p++) out.print(Array[p]+" "); out.println(); } } out.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
6e91c67cc06b9570c857da2f2f1ba85f
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.*; public class weird_algrithm { static BufferedWriter output = new BufferedWriter( new OutputStreamWriter(System.out)); static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); static int mod = 1000000007; static String toReturn = ""; static int steps = Integer.MAX_VALUE; static int maxlen = 1000005; /*MATHEMATICS FUNCTIONS START HERE MATHS MATHS MATHS MATHS*/ static long gcd(long a, long b) { if(b == 0) return a; else return gcd(b, a % b); } static long powerMod(long x, long y, int mod) { if(y == 0) return 1; long temp = powerMod(x, y / 2, mod); temp = ((temp % mod) * (temp % mod)) % mod; if(y % 2 == 0) return temp; else return ((x % mod) * (temp % mod)) % mod; } static long modInverse(long n, int p) { return powerMod(n, p - 2, p); } static long nCr(int n, int r, int mod, long [] fact, long [] ifact) { return ((fact[n] % mod) * ((ifact[r] * ifact[n - r]) % mod)) % mod; } static boolean isPrime(long a) { if(a == 1) return false; else if(a == 2 || a == 3 || a== 5) return true; else if(a % 2 == 0 || a % 3 == 0) return false; for(int i = 5; i * i <= a; i = i + 6) { if(a % i == 0 || a % (i + 2) == 0) return false; } return true; } static int [] seive(int a) { int [] toReturn = new int [a + 1]; for(int i = 0; i < a; i++) toReturn[i] = 1; toReturn[0] = 0; toReturn[1] = 0; toReturn[2] = 1; for(int i = 2; i * i <= a; i++) { if(toReturn[i] == 0) continue; for(int j = 2 * i; j <= a; j += i) toReturn[j] = 0; } return toReturn; } static long [] fact(int a) { long [] arr = new long[a + 1]; arr[0] = 1; for(int i = 1; i < a + 1; i++) { arr[i] = (arr[i - 1] * i) % mod; } return arr; } /*MATHS MATHS MATHS MATHS MATHEMATICS FUNCTIONS END HERE */ /*SWAP FUNCTION START HERE SWAP SWAP SWAP SWAP */ static void swap(int i, int j, long[] arr) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void swap(int i, int j, int[] arr) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void swap(int i, int j, String [] arr) { String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void swap(int i, int j, char [] arr) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } /*SWAP SWAP SWAP SWAP SWAP FUNCTION END HERE*/ /*BINARY SEARCH METHODS START HERE * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH */ static boolean BinaryCheck(long test, long [] arr, long health) { for(int i = 0; i <= arr.length - 1; i++) { if(i == arr.length - 1) health -= test; else if(arr[i + 1] - arr[i] > test) { health = health - test; }else { health = health - (arr[i + 1] - arr[i]); } if(health <= 0) return true; } return false; } static long binarySearchModified(long start1, long n, ArrayList<Long> arr, int a, long r) { long start = start1, end = n, ans = -1; while(start < end) { long mid = (start + end) / 2; //System.out.println(mid); if(arr.get((int)mid) + arr.get(a) <= r && mid != start1) { ans = mid; start = mid + 1; }else{ end = mid; } } //System.out.println(); return ans; } static int binarySearch(int start, int end, ArrayList<Long> arr, long val) { while(start < end) { int mid = (start + end) / 2; if(arr.get(mid) <= val) start = mid + 1; else end = mid; } return start; } /*BINARY SEARCH * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH * BINARY SEARCH BINARY SEARCH METHODS END HERE*/ /*RECURSIVE FUNCTION START HERE * RECURSIVE * RECURSIVE * RECURSIVE * RECURSIVE */ static int recurse(int x, int y, int n, int steps1, Integer [][] dp) { if(x > n || y > n) return 0; if(dp[x][y] != null) { return dp[x][y]; } else if(x == n || y == n) { return steps1; } return dp[x][y] = Math.max(recurse(x + y, y, n, steps1 + 1, dp), recurse(x, x + y, n, steps1 + 1, dp)); } /*RECURSIVE * RECURSIVE * RECURSIVE * RECURSIVE * RECURSIVE RECURSIVE FUNCTION END HERE*/ /*GRAPH FUNCTIONS START HERE * GRAPH * GRAPH * GRAPH * GRAPH * */ static class edge{ int from, to; long weight; public edge(int x, int y, long weight2) { this.from = x; this.to = y; this.weight = weight2; } } static class sort implements Comparator<pair>{ @Override public int compare(pair o1, pair o2) { // TODO Auto-generated method stub return (int)o1.a - (int)o2.a; } } static void addEdge(ArrayList<ArrayList<edge>> graph, int from, int to, long weight) { edge temp = new edge(from, to, weight); edge temp1 = new edge(to, from, weight); graph.get(from).add(temp); //graph.get(to).add(temp1); } static int ans = 0; static void topoSort(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, ArrayList<Integer> toReturn) { if(visited[vertex]) return; visited[vertex] = true; for(int i = 0; i < graph.get(vertex).size(); i++) { if(!visited[graph.get(vertex).get(i)]) topoSort(graph, graph.get(vertex).get(i), visited, toReturn); } toReturn.add(vertex); } static boolean isCyclicDirected(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, boolean [] reStack) { if(reStack[vertex]) return true; if(visited[vertex]) return false; reStack[vertex] = true; visited[vertex] = true; for(int i = 0; i < graph.get(vertex).size(); i++) { if(isCyclicDirected(graph, graph.get(vertex).get(i), visited, reStack)) return true; } reStack[vertex] = false; return false; } static int e = 0; static long mst(PriorityQueue<edge> pq, int nodes) { long weight = 0; int [] size = new int[nodes + 1]; Arrays.fill(size, 1); while(!pq.isEmpty()) { edge temp = pq.poll(); int x = parent(parent, temp.to); int y = parent(parent, temp.from); if(x != y) { //System.out.println(temp.weight); union(x, y, rank, parent, size); weight += temp.weight; e++; } } return weight; } static void floyd(long [][] dist) { // to find min distance between two nodes for(int k = 0; k < dist.length; k++) { for(int i = 0; i < dist.length; i++) { for(int j = 0; j < dist.length; j++) { if(dist[i][j] > dist[i][k] + dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } } static void dijkstra(ArrayList<ArrayList<edge>> graph, long [] dist, int src) { for(int i = 0; i < dist.length; i++) dist[i] = Long.MAX_VALUE / 2; dist[src] = 0; boolean visited[] = new boolean[dist.length]; PriorityQueue<pair> pq = new PriorityQueue<>(new sort()); pq.add(new pair(src, 0)); while(!pq.isEmpty()) { pair temp = pq.poll(); int index = (int)temp.a; for(int i = 0; i < graph.get(index).size(); i++) { if(dist[graph.get(index).get(i).to] > dist[index] + graph.get(index).get(i).weight) { dist[graph.get(index).get(i).to] = dist[index] + graph.get(index).get(i).weight; pq.add(new pair(graph.get(index).get(i).to, graph.get(index).get(i).weight)); } } } } static int parent1 = -1; static boolean ford(ArrayList<ArrayList<edge>> graph1, ArrayList<edge> graph, long [] dist, int src, int [] parent) { for(int i = 0; i < dist.length; i++) dist[i] = Long.MIN_VALUE / 2; dist[src] = 0; boolean hasNeg = false; for(int i = 0; i < dist.length - 1; i++) { for(int j = 0; j < graph.size(); j++) { int from = graph.get(j).from; int to = graph.get(j).to; long weight = graph.get(j).weight; if(dist[to] < dist[from] + weight) { dist[to] = dist[from] + weight; parent[to] = from; } } } for(int i = 0; i < graph.size(); i++) { int from = graph.get(i).from; int to = graph.get(i).to; long weight = graph.get(i).weight; if(dist[to] < dist[from] + weight) { parent1 = from; hasNeg = true; /* * dfs(graph1, parent1, new boolean[dist.length], dist.length - 1); * //System.out.println(ans); dfs(graph1, 0, new boolean[dist.length], parent1); */ //System.out.println(ans); if(ans == 2) break; else ans = 0; } } return hasNeg; } /*GRAPH FUNCTIONS END HERE * GRAPH * GRAPH * GRAPH * GRAPH */ /*disjoint Set START HERE * disjoint Set * disjoint Set * disjoint Set * disjoint Set */ static int [] rank; static int [] parent; static int parent(int [] parent, int x) { if(parent[x] == x) return x; else return parent[x] = parent(parent, parent[x]); } static boolean union(int x, int y, int [] rank, int [] parent, int [] setSize) { if(parent(parent, x) == parent(parent, y)) { return true; } if (rank[x] > rank[y]) { parent[y] = x; setSize[x] += setSize[y]; } else { parent[x] = y; setSize[y] += setSize[x]; if (rank[x] == rank[y]) rank[y]++; } return false; } /*disjoint Set END HERE * disjoint Set * disjoint Set * disjoint Set * disjoint Set */ /*INPUT START HERE * INPUT * INPUT * INPUT * INPUT * INPUT */ static int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(sc.readLine()); } static long nextLong() throws NumberFormatException, IOException { return Long.parseLong(sc.readLine()); } static long [] inputLongArr() throws NumberFormatException, IOException{ String [] s = sc.readLine().split(" "); long [] toReturn = new long[s.length]; for(int i = 0; i < s.length; i++) { toReturn[i] = Long.parseLong(s[i]); } return toReturn; } static int max = 0; static int [] inputIntArr() throws NumberFormatException, IOException{ String [] s = sc.readLine().split(" "); //System.out.println(s.length); int [] toReturn = new int[s.length]; for(int i = 0; i < s.length; i++) { toReturn[i] = Integer.parseInt(s[i]); } return toReturn; } /*INPUT * INPUT * INPUT * INPUT * INPUT * INPUT END HERE */ static long [] preCompute(int level) { long [] toReturn = new long[level]; toReturn[0] = 1; toReturn[1] = 16; for(int i = 2; i < level; i++) { toReturn[i] = ((toReturn[i - 1] % mod) * (toReturn[i - 1] % mod)) % mod; } return toReturn; } static class pair{ long a; long b; long d; public pair(long in, long y) { this.a = in; this.b = y; this.d = 0; } } static int [] nextGreaterBack(char [] s) { Stack<Integer> stack = new Stack<>(); int [] toReturn = new int[s.length]; for(int i = 0; i < s.length; i++) { if(!stack.isEmpty() && s[stack.peek()] >= s[i]) { stack.pop(); } if(stack.isEmpty()) { stack.push(i); toReturn[i] = -1; }else { toReturn[i] = stack.peek(); stack.push(i); } } return toReturn; } static int [] nextGreaterFront(char [] s) { Stack<Integer> stack = new Stack<>(); int [] toReturn = new int[s.length]; for(int i = s.length - 1; i >= 0; i--) { if(!stack.isEmpty() && s[stack.peek()] >= s[i]) { stack.pop(); } if(stack.isEmpty()) { stack.push(i); toReturn[i] = -1; }else { toReturn[i] = stack.peek(); stack.push(i); } } return toReturn; } static int lps(String s) { int max = 0; int [] lps = new int[s.length()]; lps[0] = 0; int j = 0; for(int i = 1; i < lps.length; i++) { j = lps[i - 1]; while(j > 0 && s.charAt(i) != s.charAt(j)) j = lps[j - 1]; if(s.charAt(i) == s.charAt(j)) { lps[i] = j + 1; max = Math.max(max, lps[i]); } } return max; } static int [][] vectors = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; static String dir = "DRUL"; static boolean check(int i, int j, boolean [][] visited) { if(i >= visited.length || j >= visited[0].length) return false; if(i < 0 || j < 0) return false; return true; } static void selectionSort(long arr[], long [] arr1, ArrayList<ArrayList<Integer>> ans) { int n = arr.length; for (int i = 0; i < n-1; i++) { int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; else if(arr[j] == arr[min_idx]) { if(arr1[j] < arr1[min_idx]) min_idx = j; } if(i == min_idx) { continue; } ArrayList<Integer> p = new ArrayList<Integer>(); p.add(min_idx + 1); p.add(i + 1); ans.add(new ArrayList<Integer>(p)); swap(i, min_idx, arr); swap(i, min_idx, arr1); } } static int saved = Integer.MAX_VALUE; static String ans1 = ""; public static boolean isValid(int x, int y, String [] mat) { if(x >= mat.length || x < 0) return false; if(y >= mat[0].length() || y < 0) return false; return true; } public static void recurse3(ArrayList<Character> arr, int index, String s, int max, ArrayList<String> toReturn) { if(s.length() == max) { toReturn.add(s); return; } if(index == arr.size()) return; recurse3(arr, index + 1, s + arr.get(index), max, toReturn); recurse3(arr, index + 1, s, max, toReturn); } /* if(arr[i] > q) return Math.max(f(i + 1, q - 1) + 1, f(i + 1, q); else return f(i + 1, q) + 1 */ static void dfsDP(ArrayList<ArrayList<Integer>> graph, int src, int [] dp1, int [] dp2, int parent) { int sum1 = 0; int sum2 = 0; for(int x : graph.get(src)) { if(x == parent) continue; dfsDP(graph, x, dp1, dp2, src); sum1 += Math.min(dp1[x], dp2[x]); sum2 += dp1[x]; } dp1[src] = 1 + sum1; dp2[src] = sum2; System.out.println(src + " " + dp1[src] + " " + dp2[src]); } static int balanced = 0; static void dfs(ArrayList<ArrayList<ArrayList<Long>>> graph, long src, int [] dist, long sum1, long sum2, long parent, ArrayList<Long> arr, int index) { index = binarySearch(index, arr.size() - 1, arr, sum1); if(index < arr.size() && arr.get(index) <= sum1) { dist[(int)src] = index + 1; } else dist[(int)src] = index; for(ArrayList<Long> x : graph.get((int)src)) { if(x.get(0) == parent) continue; if(arr.size() != 0) arr.add(arr.get(arr.size() - 1) + x.get(2)); else arr.add(x.get(2)); dfs(graph, x.get(0), dist, sum1 + x.get(1), sum2, src, arr, index); arr.remove(arr.size() - 1); } } static int compare(String s1, String s2) { Queue<Character> q1 = new LinkedList<>(); Queue<Character> q2 = new LinkedList<Character>(); for(int i = 0; i < s1.length(); i++) { q1.add(s1.charAt(i)); q2.add(s2.charAt(i)); } int k = 0; while(k < s1.length()) { if(q1.equals(q2)) { break; } q2.add(q2.poll()); k++; } return k; } static void solve() throws IOException { int n = nextInt(); int [] arr = new int [n]; if(n == 1) { output.write(1 + "\n"); }else { output.write(n + "\n"); } for(int i = 0; i < n; i++) { arr[i] = i + 1; output.write(i + 1 + " "); } output.write("\n"); int f = n; int i = 0; int j = n - 1; while(j >= 1) { swap(j, j - 1, arr); j--; for(int k = 0; k < arr.length; k++) { output.write(arr[k] + " "); } f -= 2; output.write("\n"); } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int t = Integer.parseInt(sc.readLine()); for(int i = 0; i < t; i++) solve(); output.flush(); } } class TreeNode { int val; TreeNode next; public TreeNode(int x, TreeNode y) { this.val = x; this.next = y; } } /* 1 10 6 10 7 9 11 99 45 20 88 31 */
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
b5acd80fd717e1d8f82be228bf20d8c4
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Solution{ public static void main(String args[]){ Scanner in = new Scanner(System.in); long test = in.nextLong(); while(test-- > 0){ long n = in.nextLong(); ArrayList<Integer> list = new ArrayList<>(); int i, a = 1; for(i = 0; i < n; i++){ list.add(i, i + 1); } ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>(); for(i = 0; i < n; i++){ ans.add(new ArrayList<>(list)); if(a >= n) break; else{ int temp = list.get(a); list.set(a, list.get(0)); list.set(0, temp); a++; } } System.out.println(ans.size()); for(i = 0; i < n; i++){ System.out.println(Arrays.asList(ans.get(i)).toString().substring(1).replace("]", "").replace("[", "").replace(", ", " ")); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
e24d7bd7d0a1815c4154c4d5292f8896
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Codechef { //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static int cnt; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { int mod = 1000000007; MyScanner scan = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int test = scan.nextInt(); for(int t=0;t<test;t++){ int n = scan.nextInt(); int arr[] = new int[n]; out.println(n); for(int i=0;i<n;i++){ out.print((i+1)+" "); arr[i] = i+1; } out.println(); for(int i=0;i<n-1;i++){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; for(int j=0;j<n;j++){ out.print(arr[j]+" "); } out.println(); } } out.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
fd742c86432a4a06da1ddeef3486a0d0
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.math.BigInteger; public class Main{ static class Node { long sum, pre; Node(long a, long b) { this.sum = a; this.pre = b; } } static class SegmentTree { int l , r; // range responsible for SegmentTree left , right; long val; SegmentTree(int l,int r,long a[]) { this.l = l; this.r = r; // list= new ArrayList<>(); if(l == r) { val = a[l]; return ; } int mid = l + (r-l)/2; this.left = new SegmentTree(l ,mid , a); this.right = new SegmentTree(mid + 1 , r,a); this.val = Math.max(this.left.val , this.right.val); } public long query(int left ,int right) { if(this.l > right || this.r < left) { return 0l; } if(this.l >= left && this.r <= right) { return this.val; } return this.left.query(left , right ) + this.right.query(left , right ); } // public void pointUpdate(int index , long val) { // if(this.l > index || this.r < index) return; // if(this.l == this.r && this.l == index) { // this.val = val; // return ; // } // this.left.pointUpdate(index ,val); // this.right.pointUpdate(index , val); // this.val = join(this.left.val , this.right.val); // } // public void rangeUpdate(int left , int right,long val) { // if(this.l > right || this.r < left) return ; // if(this.l >= left && this.r <= right) { // this.val += val; // } // if(this.l == this.r) return; // this.left.rangeUpdate(left , right , val); // this.right.rangeUpdate(left , right , val); // } // public long valueAtK(int k) { // if(this.l > k || this.r < k) return 0; // if(this.l == this.r && this.l == k) { // return this.val; // } // return join(this.left.valueAtK(k) , this.right.valueAtK(k)); // } public int join(int a ,int b) { return a + b; } } static class Hash { long hash[] ,mod = (long)1e9 + 7 , powT[] , prime , inverse[]; Hash(char []s) { prime = 131; int n = s.length; powT = new long[n]; hash = new long[n]; inverse = new long[n]; powT[0] = 1; inverse[n-1] = pow(pow(prime , n-1 ,mod), mod-2 , mod); for(int i = 1;i < n; i++ ) { powT[i] = (powT[i-1]*prime)%mod; } for(int i = n-2; i>= 0;i-=1) { inverse[i] = (inverse[i+1]*prime)%mod; } hash[0] = (s[0] - 'a' + 1); for(int i = 1; i < n;i++ ) { hash[i] = hash[i-1] + ((s[i]-'a' + 1)*powT[i])%mod; hash[i] %= mod; } } public long hashValue(int l , int r) { if(l == 0) return hash[r]%mod; long ans = hash[r] - hash[l-1] +mod; ans %= mod; // System.out.println(inverse[l] + " " + pow(powT[l], mod- 2 , mod)); ans *= inverse[l]; ans %= mod; return ans; } } static class ConvexHull { Stack<Integer>stack; Stack<Integer>stack1; int n; Point arr[]; ConvexHull(Point arr[]) { n = arr.length; this.arr = arr; Arrays.sort(arr , (a , b)-> { if(a.x == b.x) return (int)(b.y-a.y); return (int)(a.x-b.x); }); Point min = arr[0]; stack = new Stack<>(); stack1 = new Stack<>(); stack.push(0); stack1.push(0); Point ob = new Point(2,2); for(int i =1;i < n;i++) { if(stack.size() < 2) stack.push(i); else { while(stack.size() >= 2) { int a = stack.pop() , b = stack.pop() ,c = i; int dir = ob.cross(arr[b] , arr[a] , arr[c]); if(dir < 0) { stack.push(b); stack.push(a); stack.push(c); break; } stack.push(b); } if(stack.size() < 2) { stack.push(i); } } } for(int i =1;i < n;i++) { if(stack1.size() < 2) stack1.push(i); else { while(stack1.size() >= 2) { int a = stack1.pop() , b = stack1.pop() ,c = i; int dir = ob.cross(arr[b] , arr[a] , arr[c]); if(dir > 0) { stack1.push(b); stack1.push(a); stack1.push(c); break; } stack1.push(b); } if(stack1.size() < 2) { stack1.push(i); } } } } public List<Point> getPoints() { boolean vis[] = new boolean[n]; List<Point> list = new ArrayList<>(); // for(int x : stack) { // list.add(arr[x]); // vis[x] = true; // } for(int x : stack1) { // if(vis[x]) continue; list.add(arr[x]); } return list; } } public static class Suffix implements Comparable<Suffix> { int index; int rank; int next; public Suffix(int ind, int r, int nr) { index = ind; rank = r; next = nr; } public int compareTo(Suffix s) { if (rank != s.rank) return Integer.compare(rank, s.rank); return Integer.compare(next, s.next); } } static class Point { long x , y; Point(long x , long y) { this.x = x; this.y = y; } public String toString() { return this.x + " " + this.y; } public Point sub(Point a,Point b) { return new Point(a.x - b.x , a.y-b.y); } public int cross(Point a ,Point b , Point c) { Point g = sub(b,a) , l = sub(c, b); long ans = g.y*l.x - g.x*l.y; if(ans == 0) return 0; if(ans < 0) return -1; return 1; } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } static Kattio sc = new Kattio(); static long mod = 998244353l; // static Kattio out = new Kattio(); //Heapify function to maintain heap property. public static void swap(int i,int j,int arr[]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static long max(long ...a) { return maxArray(a); } public static void swap(int i,int j,long arr[]) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void swap(int i,int j,char arr[]) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static String endl = "\n" , gap = " "; static int size[]; static int parent[]; static HashMap<Integer , Long> value; // static HashMap<String , Boolean > dp; // static HashMap<Integer , List<int[]>> graph; static boolean vis[]; static int answer; static HashSet<String> guess; static long primePow[]; // static long dp[]; static int N; static int dis[]; static int height[]; static long p[]; // static long fac[]; // static long inv[]; static HashMap<Integer ,List<Integer>> graph; public static long rd() { return (long)((Math.random()*10) + 1); } public static void main(String[] args)throws IOException { long t = ri(); // List<Integer> list = new ArrayList<>(); // int MAX = (int)4e4; // for(int i =1;i<=MAX;i++) { // if(isPalindrome(i + "")) list.add(i); // } // // System.out.println(list); // long dp[] = new long[MAX +1]; // dp[0] = 1; // long mod = (long)(1e9+7); // for(int x : list) { // for(int i =1;i<=MAX;i++) { // if(i >= x) { // dp[i] += dp[i-x]; // dp[i] %= mod; // } // } // } // int MAK = (int)1e6 + 10; // boolean seive[] = new boolean[MAK]; // Arrays.fill(seive , true); // seive[1] = false; // seive[0] = false; // for (int i = 1; i < MAK; i++) { // if(seive[i]) { // for (int j = i+i; j < MAK; j+=i) { // seive[j] = false; // } // } // } // TreeSet<Long>primeSet = new TreeSet<>(); // for(long i = 2;i <= (long)1e6;i++) { // if(seive[(int)i])primeSet.add(i); // } // List<Integer> list = new ArrayList<>(); // for (int i = 1; i < seive.length; i++) { // if(seive[i]) list.add(i); // } int test_case = 1; while(t-->0) { // sc.print("Case #"+(test_case++)+": "); solve(); } sc.close(); } static int endTime[]; static int time; public static int dis(int a[] , int b[]) { return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]); } static Long dp[][][]; static long fac[], inv[]; public static long ncr(int a , int b) { if(a == b) return 1l; return (((fac[a]*inv[b])%mod)*inv[a-b])%mod; } public static void solve() throws IOException { List<int[]> l = new ArrayList<>(); int n = ri(); int a[] = new int[n]; for(int i =1 ;i <= n;i++) a[i-1] = i; l.add(a); for(int i = n-2;i >= 0;i--) { int size = l.size()-1; int b[] = l.get(size).clone(); swap(n-1 , i , b); l.add(b); } sc.println(l.size()); for(int g[] : l) { for(int x : g)sc.print(x + " "); sc.println(); } } public static String slope(Point a , Point b) { if((a.x-b.x) == 0) return "inf"; long n = a.y- b.y; if(n == 0) return "0"; long m = a.x-b.x; boolean neg = (n*m < 0?true:false); n = Math.abs(n); m = Math.abs(m); long g = gcd(Math.abs(n),Math.abs(m)); n /= g; m /= g; String ans = n+"/"+m; if(neg) ans = "-" + ans; return ans; } public static int lis(int A[], int size) { int[] tailTable = new int[size]; int len; tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) tailTable[len++] = A[i]; else tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; } public static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] >= key) r = m; else l = m; } return r; } public static long unset(long n, long k) { long ans = (n & (~(1l << (k - 1l)))); return ans; } public static long setKthBit(long n, long k) { return ((1l << k) | n); } // public static void makeGraph(int s , int n,int m) { // graph = new HashMap<>(); // for(int i = s;i<=n;i++) { // graph.put(i , new ArrayList<>()); // } // for(int i =0;i < m;i++) { // int u = ri() , v = ri(); // graph.get(u).add(v); // graph.get(v).add(u); // } // } public static int lcs(char a[] , char b[]) { int n = a.length , m = b.length; int dp[][] = new int[n + 1][m + 1]; for(int i =1;i <= n;i++) { for(int j =1;j <= m;j++) { if(a[i-1] == b[j-1]) dp[i][j] = 1 + dp[i-1][j-1]; else dp[i][j] = Math.max(dp[i-1][j] , dp[i][j-1]); } } return dp[n][m]; } public static int find(int node) { if(node == parent[node]) return node; return parent[node] = find(parent[node]); } public static void merge(int a ,int b ) { a = find(a); b = find(b); if(a == b) return; if(size[a] >= size[b]) { parent[b] = a; size[a] += size[b]; // primePow[a] += primePow[b]; // primePow[b] = 0; } else { parent[a] = b; size[b] += size[a]; // primePow[b] += primePow[a]; // primePow[a] = 0; } } public static void processPowerOfP(long arr[]) { int n = arr.length; arr[0] = 1; long mod = (long)1e9 + 7; for(int i =1;i<n;i++) { arr[i] = arr[i-1]*51; arr[i] %= mod; } } public static long hashValue(char s[]) { int n = s.length; long powerOfP[] = new long[n]; processPowerOfP(powerOfP); long ans =0; long mod = (long)1e9 + 7; for(int i =0;i<n;i++) { ans += (s[i]-'a'+1)*powerOfP[i]; ans %= mod; } return ans; } public static void dfs(int r,int c,char arr[][]) { int n = arr.length , m = arr[0].length; arr[r][c] = '#'; for(int i =0;i<4;i++) { int nr = r + colx[i] , nc = c + coly[i]; if(nr < 0 || nc < 0 || nc >= m || nr>=n) continue; if(arr[nr][nc] == '#') continue; dfs(nr,nc,arr); } } public static double getSlope(int a , int b,int x,int y) { if(a-x == 0) return Double.MAX_VALUE; if(b-y == 0) return 0.0; return ((double)b-(double)y)/((double)a-(double)x); } public static boolean collinearr(long a[] , long b[] , long c[]) { return (b[1]-a[1])*(b[0]-c[0]) == (b[0]-a[0])*(b[1]-c[1]); } public static boolean isSquare(long sum) { long root = (int)Math.sqrt(sum); return root*root == sum; } public static int[] suffixArray(String s) { int n = s.length(); Suffix[] su = new Suffix[n]; for (int i = 0; i < n; i++) { su[i] = new Suffix(i, s.charAt(i) - '$', 0); } for (int i = 0; i < n; i++) su[i].next = (i + 1 < n ? su[i + 1].rank : -1); Arrays.sort(su); int[] ind = new int[n]; for (int length = 4; length < 2 * n; length <<= 1) { int rank = 0, prev = su[0].rank; su[0].rank = rank; ind[su[0].index] = 0; for (int i = 1; i < n; i++) { if (su[i].rank == prev && su[i].next == su[i - 1].next) { prev = su[i].rank; su[i].rank = rank; } else { prev = su[i].rank; su[i].rank = ++rank; } ind[su[i].index] = i; } for (int i = 0; i < n; i++) { int nextP = su[i].index + length / 2; su[i].next = nextP < n ? su[ind[nextP]].rank : -1; } Arrays.sort(su); } int[] suf = new int[n]; for (int i = 0; i < n; i++) suf[i] = su[i].index; return suf; } public static boolean isPalindrome(String s) { int i =0 , j = s.length() -1; while(i <= j && s.charAt(i) == s.charAt(j)) { i++; j--; } return i>j; } // digit dp hint; public static long callfun(String num , int N, int last ,int secondLast ,int bound ,long dp[][][][]) { if(N == 1) { if(last == -1 || secondLast == -1) return 0; long answer = 0; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i = 0;i<=max;i++) { if(last > secondLast && last > i) { answer++; } if(last < secondLast && last < i) { answer++; } } return answer; } if(secondLast == -1 || last == -1 ){ long answer = 0l; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i =0;i<=max;i++) { int nl , nsl , newbound = bound==0?0:i==max?1:0; if(last == - 1&& secondLast == -1 && i == 0) { nl = -1 ; nsl = -1; } else { nl = i;nsl = last; } long temp = callfun(num , N-1 , nl , nsl ,newbound, dp); answer += temp; if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++; } return answer; } if(dp[N][last][secondLast][bound] != -1) return dp[N][last][secondLast][bound]; long answer = 0l; int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9; for(int i =0;i<=max;i++) { int nl , nsl , newbound = bound==0?0:i==max?1:0; if(last == - 1&& secondLast == -1 && i == 0) { nl = -1 ; nsl = -1; } else { nl = i;nsl = last; } long temp = callfun(num , N-1 , nl , nsl ,newbound,dp); answer += temp; if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++; } return dp[N][last][secondLast][bound] = answer; } public static Long callfun(int index ,int pair,int arr[],Long dp[][]) { long mod = 998244353l; if(index >= arr.length) return 1l; if(dp[index][pair] != null) return dp[index][pair]; Long sum = 0l , ans = 0l; if(arr[index]%2 == pair) { return dp[index][pair] = callfun(index + 1,pair^1 , arr,dp)%mod + callfun(index + 1 ,pair , arr , dp)%mod; } else { return dp[index][pair] = callfun(index + 1,pair , arr,dp)%mod; } // for(int i =index;i<arr.length;i++) { // sum += arr[i]; // if(sum%2 == pair) { // ans = ans + callfun(i + 1,pair^1,arr , dp)%mod; // ans%=mod; // } // } // return dp[index][pair] = ans; } public static boolean callfun(int index , int n,int neg , int pos , String s) { if(neg < 0 || pos < 0) return false; if(index >= n) return true; if(s.charAt(0) == 'P') { if(neg <= 0) return false; return callfun(index + 1,n , neg-1 , pos , s.charAt(1) + "N"); } else { if(pos <= 0) return false; return callfun(index + 1 , n , neg , pos-1 , s.charAt(1) + "P"); } } public static void getPerm(int n , char arr[] , String s ,List<String>list) { if(n == 0) { list.add(s); return; } for(char ch : arr) { getPerm(n-1 , arr , s+ ch,list); } } public static int getLen(int i ,int j , char s[]) { while(i >= 0 && j < s.length && s[i] == s[j]) { i--; j++; } i++; j--; if(i>j) return 0; return j-i + 1; } public static int getMaxCount(String x) { char s[] = x.toCharArray(); int max = 0; int n = s.length; for(int i =0;i<n;i++) { max = Math.max(max,Math.max(getLen(i , i,s) , getLen(i ,i+1,s))); } return max; } public static double getDis(int arr[][] , int x, int y) { double ans = 0.0; for(int a[] : arr) { int x1 = a[0] , y1 = a[1]; ans += Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1)); } return ans; } public static boolean valid(String x ) { if(x.length() == 0) return true; if(x.length() == 1) return false; char s[] = x.toCharArray(); if(x.length() == 2) { if(s[0] == s[1]) { return false; } return true; } int r = 0 , b = 0; for(char ch : x.toCharArray()) { if(ch == 'R') r++; else b++; } return (r >0 && b >0); } public static void primeDivisor(HashMap<Long , Long >cnt , long num) { for(long i = 2;i*i<=num;i++) { while(num%i == 0) { cnt.put(i ,(cnt.getOrDefault(i,0l) + 1)); num /= i; } } if(num > 2) { cnt.put(num ,(cnt.getOrDefault(num,0l) + 1)); } } public static boolean isSubsequene(char a[], char b[] ) { int i =0 , j = 0; while(i < a.length && j <b.length) { if(a[i] == b[j]) { j++; } i++; } return j >= b.length; } public static long fib(int n ,long M) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { long[][] mat = {{1, 1}, {1, 0}}; mat = pow(mat, n-1 , M); return mat[0][0]; } } public static long[][] pow(long[][] mat, int n ,long M) { if (n == 1) return mat; else if (n % 2 == 0) return pow(mul(mat, mat , M), n/2 , M); else return mul(pow(mul(mat, mat,M), n/2,M), mat , M); } static long[][] mul(long[][] p, long[][] q,long M) { long a = (p[0][0]*q[0][0] + p[0][1]*q[1][0])%M; long b = (p[0][0]*q[0][1] + p[0][1]*q[1][1])%M; long c = (p[1][0]*q[0][0] + p[1][1]*q[1][0])%M; long d = (p[1][0]*q[0][1] + p[1][1]*q[1][1])%M; return new long[][] {{a, b}, {c, d}}; } public static long[] kdane(long arr[]) { int n = arr.length; long dp[] = new long[n]; dp[0] = arr[0]; long ans = dp[0]; for(int i = 1;i<n;i++) { dp[i] = Math.max(dp[i-1] + arr[i] , arr[i]); ans = Math.max(ans , dp[i]); } return dp; } public static void update(int low , int high , int l , int r, int val , int treeIndex ,int tree[]) { if(low > r || high < l || high < low) return; if(l <= low && high <= r) { System.out.println("At " +low + " and " + high + " ans ttreeIndex " + treeIndex); tree[treeIndex] += val; return; } int mid = low + (high - low)/2; update(low , mid , l , r , val , treeIndex*2 + 1, tree); update(mid + 1 , high , l , r , val , treeIndex*2 + 2 , tree); } // static int colx[] = {1 ,1, -1,-1 , 2,2,-2,-2}; // static int coly[] = {-2 ,2, 2,-2,1,-1,1,-1}; static int colx[] = {1 ,-1, 0,0 , 1,1,-1,-1}; static int coly[] = {0 ,0, 1,-1,1,-1,1,-1}; public static void reverse(char arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static void reverse(long arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static void reverse(int arr[]) { int i =0 , j = arr.length-1; while(i < j) { swap(i , j , arr); i++; j--; } } public static long inverse(long x , long mod) { return pow(x , mod -2 , mod); } public static int maxArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static long maxArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =1;i<n;i++) { ans = Math.max(ans , arr[i]); } return ans; } public static int minArray(int arr[]) { int ans = arr[0] , n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static long minArray(long arr[]) { long ans = arr[0]; int n = arr.length; for(int i =0;i<n;i++ ) { ans = Math.min(ans ,arr[i]); } return ans; } public static int sumArray(int arr[]) { int ans = 0; for(int x : arr) { ans += x; } return ans; } public static long sumArray(long arr[]) { long ans = 0; for(long x : arr) { ans += x; } return ans; } public static long rl() { return sc.nextLong(); } public static char[] rac() { return sc.next().toCharArray(); } public static String rs() { return sc.next(); } public static char rc() { return sc.next().charAt(0); } public static int [] rai(int n) { int ans[] = new int[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextInt(); } return ans; } public static long [] ral(int n) { long ans[] = new long[n]; for(int i =0;i<n;i++) { ans[i] = sc.nextLong(); } return ans; } public static int ri() { return sc.nextInt(); } public static int getValue(int num ) { int ans = 0; while(num > 0) { ans++; num = num&(num-1); } return ans; } public static boolean isValid(int x ,int y , int n,char arr[][],boolean visited[][][][]) { return x>=0 && x<n && y>=0 && y <n && !(arr[x][y] == '#'); } // public static Pair join(Pair a , Pair b) { // Pair res = new Pair(Math.min(a.min , b.min) , Math.max(a.max , b.max) , a.count + b.count); // return res; // } // segment tree query over range // public static int query(int node,int l , int r,int a,int b ,Pair tree[] ) { // if(tree[node].max < a || tree[node].min > b) return 0; // if(l > r) return 0; // if(tree[node].min >= a && tree[node].max <= b) { // return tree[node].count; // } // int mid = l + (r-l)/2; // int ans = query(node*2 ,l , mid ,a , b , tree) + query(node*2 +1,mid + 1, r , a , b, tree); // return ans; // } // // segment tree update over range // public static void update(int node, int i , int j ,int l , int r,long value, long arr[] ) { // if(l >= i && j >= r) { // arr[node] += value; // return; // } // if(j < l|| r < i) return; // int mid = l + (r-l)/2; // update(node*2 ,i ,j ,l,mid,value, arr); // update(node*2 +1,i ,j ,mid + 1,r, value , arr); // } public static long pow(long a , long b , long mod) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2 , mod)%mod; if(b%2 == 0) { return (ans*ans)%mod; } else { return ((ans*ans)%mod*a)%mod; } } public static long pow(long a , long b ) { if(b == 1) return a; if(b == 0) return 1; long ans = pow(a , b/2); if(b%2 == 0) { return (ans*ans); } else { return ((ans*ans)*a); } } public static boolean isVowel(char ch) { if(ch == 'a' || ch == 'e'||ch == 'i' || ch == 'o' || ch == 'u') return true; if((ch == 'A' || ch == 'E'||ch == 'I' || ch == 'O' || ch == 'U')) return true; return false; } // public static int getFactor(int num) { // if(num==1) return 1; // int ans = 2; // int k = num/2; // for(int i = 2;i<=k;i++) { // if(num%i==0) ans++; // } // return Math.abs(ans); // } public static int[] readarr()throws IOException { int n = sc.nextInt(); int arr[] = new int[n]; for(int i =0;i<n;i++) { arr[i] = sc.nextInt(); } return arr; } public static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); } public static boolean isPrime(long num) { if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(long i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } public static boolean isPrime(int num) { // System.out.println("At pr " + num); if(num==1) return false; if(num<=3) return true; if(num%2==0||num%3==0) return false; for(int i =5;i*i<=num;i+=6) { if(num%i==0 || num%(i+2) == 0) return false; } return true; } // public static boolean isPrime(long num) { // if(num==1) return false; // if(num<=3) return true; // if(num%2==0||num%3==0) return false; // for(int i =5;i*i<=num;i+=6) { // if(num%i==0) return false; // } // return true; // } public static void allMultiple() { // int MAX = 0 , n = nums.length; // for(int x : nums) MAX = Math.max(MAX ,x); // int cnt[] = new int[MAX + 1]; // int ans[] = new int[MAX + 1]; // for (int i = 0; i < n; ++i) cnt[nums[i]]++; // for (int i = 1; i <= MAX; ++i) { // for (int j = i; j <= MAX; j += i) ans[i] += cnt[j]; // } } public static long gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } public static int gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static int get_gcd(int a , int b) { if (b == 0) return a; return gcd(b, a % b); } public static long get_gcd(long a , long b) { if (b == 0) return a; return gcd(b, a % b); } // public static long fac(long num) { // long ans = 1; // int mod = (int)1e9+7; // for(long i = 2;i<=num;i++) { // ans = (ans*i)%mod; // } // return ans; // } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
b006d4f99f1f0b06c65060bebc53c3a2
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class aa { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuffer ans = new StringBuffer(); while(t-->0) { int n=sc.nextInt(); int a[] = new int[n]; for(int i = 0; i<n; i++) { a[i] = i+1; } ans.append(String.valueOf(n)); ans.append(System.getProperty("line.separator")); for(int i = 0; i<n; i++) { for(int x:a) { ans.append(String.valueOf(x)+' '); } if(i!=n-1) ans.append(System.getProperty("line.separator"));; int temp = a[i]; a[i] = a[n-1]; a[n-1] = temp; } if(t>0) ans.append(System.getProperty("line.separator"));; } System.out.println(ans); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
4a625e5fbe7b62ce061331a8c0dcd30b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; // Problem Link: // Runtime: O() // Space: O() public class B2 { // Public Static Variables // Private Static Methods Starts Here private static class StringLT implements Comparator<String> { @Override public int compare(String o1, String o2) { return o1.compareToIgnoreCase(o2); } } private static class Pair<K, V>{ K key; V value; public Pair(K first, V second){ key = first; value = second; } } public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); // Code Starts Here int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); out.append(n + "\n"); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = i+1; out.append(arr[i]+" "); } out.append("\n"); for(int i = 0; i < n-1; i++) { int store = arr[i]; arr[i] = arr[i+1]; arr[i+1] = store; for(int j = 0; j < n; j++) { out.append(arr[j] + " "); } out.append("\n"); } out.flush(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
8c97bee8fd2c43e9c38a2dd2a501c0df
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; // Problem Link: // Runtime: O() // Space: O() public class B { // Public Static Variables // Private Static Methods Starts Here private static class StringLT implements Comparator<String> { @Override public int compare(String o1, String o2) { return o1.compareToIgnoreCase(o2); } } private static class Pair<K, V>{ K key; V value; public Pair(K first, V second){ key = first; value = second; } } public static void main(String[] args) throws IOException { BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); Scanner sc = new Scanner(System.in); // Code Starts Here int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); out.append(n + "\n"); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = i+1; out.append(arr[i]+" "); } out.append("\n"); for(int i = 0; i < n-1; i++) { int store = arr[i]; arr[i] = arr[i+1]; arr[i+1] = store; for(int j = 0; j < n; j++) { out.append(arr[j] + " "); } out.append("\n"); } out.flush(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
c36b0c85c9eab246c5f0f5c92c210997
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; O : while(t-->0) { int n = sc.nextInt(); out.println(n); for(int i=0;i<n;i++){ for(int j=1;j<=i;j++){ out.print((j+1)+" "); } out.print("1 "); for(int j=i+1;j<n;j++){ out.print((j+1)+" "); } out.println(); } } out.flush(); } static void printN() { System.out.println("NO"); } static void printY() { System.out.println("YES"); } static int findfrequencies(int a[],int n) { int count=0; for(int i=0;i<a.length;i++) { if(a[i]==n) { count++; } } return count; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int[] EvenOddArragement(int nums[]) { int i1=0,i2=nums.length-1; while(i1<i2){ while(nums[i1]%2==0 && i1<i2){ i1++; } while(nums[i2]%2!=0 && i2>i1){ i2--; } int temp=nums[i1]; nums[i1]=nums[i2]; nums[i2]=temp; } return nums; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static String minLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for(int i=0;i<n;i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[0]; } static String maxLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[arr.length-1]; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i=i; this.j=j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } static class pair{ int i,j; pair(int x,int y){ i=x; j=y; } } static int binary_search(int a[],int value) { int start=0; int end=a.length-1; int mid=start+(end-start)/2; while(start<=end) { if(a[mid]==value) { return mid; } if(a[mid]>value) { end=mid-1; } else { start=mid+1; } mid=start+(end-start)/2; } return -1; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
cddb1f628f609cda3b4a855970c1cd72
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Main { public static String solve(int j, int n) { String ans = ""; int k = 1; for (int i = 0; i < n; i++) { if (i == j) { ans = ans + n + " "; } else { ans = ans + k + " "; k++; } } return ans + "\n"; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); Map<Integer, String> m1 = new HashMap<>(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); if (m1.containsKey(n)) { System.out.println(n); System.out.println(m1.get(n)); continue; } List<Integer> l1 = new ArrayList<>(); System.out.println(n); String ans = ""; for (int i = n - 1; i >= 0; i--) { String p = solve(i, n); ans = ans + p; } m1.put(n, ans); System.out.println(ans); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
61dbf71c8435a2056401fdeef209217c
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.Scanner; import java.util.stream.IntStream; public class Solution { private static void solve() { for (int t = 0, T = i32(); t < T; t++) { int n = i32(); int[] perm = IntStream.rangeClosed(1, n).toArray(); print(n); print(perm); for (int i = 1; i < perm.length; i++) { swap(perm, i, i - 1); print(perm); } } } static void swap(int[] array, int i, int j) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; } static final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); static final PrintStream out = new PrintStream(new BufferedOutputStream(System.out)); public static void main(String[] args) { try (in; out) { solve(); } } private static void print(String s) { out.println(s); } private static void print(int i) { out.println(i); } private static void print(int[] ints) { for (int i : ints) out.print(i + " "); out.println(); } private static int i32() { return in.nextInt(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
52ce5bf4b0755082e56462043bfe167d
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.Scanner; public class Solution { private static void solve(Scanner in, PrintStream out) { int t = in.nextInt(); while (t-- > 0) { int[] perm = new int[in.nextInt()]; out.println(perm.length); for (int i = 0; i < perm.length; i++) { perm[i] = i + 1; out.print(perm[i] + " "); } out.println(); for (int i = 1; i < perm.length; i++) { swap(perm, i, i - 1); for (int item : perm) { out.print(item + " "); } out.println(); } } } static void swap(int[] array, int i, int j) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; } public static void main(String[] args) { final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); final PrintStream out = new PrintStream(new BufferedOutputStream(System.out)); try (in; out) { solve(in, out); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
ca10e6e4dac9404af64b18316d16115b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Main { static int mod = 1000000007; static void read(int arr[], int start, int end, FastReader in) { for (int i = start; i < end; i++) { arr[i] = in.nextInt(); } } static int sumArr(int arr[]) { int sum = 0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; } return sum; } static final int MAX = 10000000; static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) { prime[i] = false; } } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) { prefix[p]++; } } } static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static Long gcd(Long a, Long b) { if (b == 0) { return a; } return gcd(b, a % b); } static int min(int arr[]) { int min = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { min = Math.min(min, arr[i]); } return min; } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<>(); for (int x : arr) { ls.add(x); } Collections.sort(ls); for (int i = 0; i < arr.length; i++) { arr[i] = ls.get(i); } } public static void sortrev(int[] arr) { ArrayList<Integer> ls = new ArrayList<>(); for (int x : arr) { ls.add(x); } Collections.sort(ls,Collections.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i] = ls.get(i); } } public static void sort(long[] arr) { ArrayList<Long> ls = new ArrayList<>(); for (long x : arr) { ls.add(x); } Collections.sort(ls); for (int i = 0; i < arr.length; i++) { arr[i] = ls.get(i); } } static int max(int arr[]) { int max = Integer.MIN_VALUE; for (int i = 0; i < arr.length; i++) { max = Math.max(max, arr[i]); } return max; } static long max(long arr[]) { long max = Long.MIN_VALUE; for (int i = 0; i < arr.length; i++) { max = Math.max(max, arr[i]); } return max; } static int max(int a, int b) { return Math.max(a, b); } static long max(long a, long b){ return (a>b)?a:b; } static int min(int a, int b) { return Math.min(a, b); } static long min(long a, long b){ return Math.min(a, b); } public static boolean isPrime(long n) { if (n < 2) { return false; } if (n == 2 || n == 3) { return true; } if (n % 2 == 0 || n % 3 == 0) { return false; } long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) { return false; } } return true; } static class Pair { int first; int second; Pair(int f, int s) { first = f; second = s; } @Override public String toString() { return "first: " + first + " second: " + second; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } void read(int arr[]) { for (int i = 0; i < arr.length; i++) { arr[i] = nextInt(); } } void read(long arr[]) { for (int i = 0; i < arr.length; i++) { arr[i] = nextLong(); } } void read(char arr[][]){ for(int i=0;i<arr.length;i++){ String s=next(); for(int j=0;j<s.length();j++) arr[i][j]=s.charAt(j); } } void read(Pair arr[]) { for (int i = 0; i < arr.length; i++) { int x = nextInt(); int y = nextInt(); arr[i] = new Pair(x, y); } } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void printarr(int arr[]) throws IOException { println(Arrays.toString(arr)); } public void printarr(long arr[]) throws IOException { println(Arrays.toString(arr)); } public void print(Object object) throws IOException { bw.append("" + object); } public void close() throws IOException { bw.close(); } } public static int count(String s, char c) { int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == c) { count++; } } return count; } public static int stoi(String s) { return Integer.parseInt(s); } public static String itos(int n) { return Integer.toString(n); } public static int ctoi(char c) { return (int) c - 48; } public static long countSetBits(long n) { long count = 0; while (n != 0) { count += (n & 1); // check last bit n >>= 1; } return count; } static long MOD = (long) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) { result = result * x % MOD; } n = n / 2; x = x * x % MOD; } return result; } static long countBits(long number) { return (long) (Math.log(number) / Math.log(2) + 1); } static boolean isPowerOfTwo(long n) { if (n == 0) { return false; } while (n != 1) { if (n % 2 != 0) { return false; } n = n / 2; } return true; } public static int LCS(String s1, String s2){ int l1=s1.length(); int l2=s2.length(); int dp[][]=new int[l1+1][l2+1]; for(int i=0;i<=l1;i++){ for(int j=0;j<=l2;j++){ if(i==0 || j==0) dp[i][j]=0; else{ if(s1.charAt(i-1)==s2.charAt(j-1)) dp[i][j]=1+dp[i-1][j-1]; else dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]); } } } // for(var v:dp) // System.out.println(Arrays.toString(v)); return dp[l1][l2]; } public static String LCSstring(String s1, String s2){ int l1=s1.length(); int l2=s2.length(); int dp[][]=new int[l1+1][l2+1]; for(int i=0;i<=l1;i++){ for(int j=0;j<=l2;j++){ if(i==0 || j==0) dp[i][j]=0; else{ if(s1.charAt(i-1)==s2.charAt(j-1)) dp[i][j]=1+dp[i-1][j-1]; else dp[i][j]=Math.max(dp[i-1][j], dp[i][j-1]); } } } String ans=""; int i=l1,j=l2; while(i>0 && j>0){ if(s1.charAt(i-1)==s2.charAt(j-1)){ ans=s1.charAt(i-1)+ans; i--; j--; } else if(dp[i-1][j]>dp[i][j-1]){ i--; } else j--; } return ans; } public static int factorial(int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } public static long factorial(long n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } public static int nCr(int n, int r,int a) { return factorial(n) / (factorial(r) * factorial(n - r)); } public static boolean[] sieve() { int n=10000000; // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int maxSubArraySum(int a[]) { int size = a.length; int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } public static int calc(int arr[],int n){ if(n<0 || n<arr[0]) return 0; if(n>=arr[2]) return max(max(1+calc(arr,n-arr[2]),1+calc(arr,n-arr[1])),1+calc(arr,n-arr[0])); else if(n>=arr[1]) return max(1+calc(arr,n-arr[1]),1+calc(arr,n-arr[0])); else if(n>=arr[0]) return 1+calc(arr,n-arr[0]); else return 0; } public static int calc(String s1, String s2){ int arr1[]=new int[26]; int arr2[]=new int[26]; for(char c:s1.toCharArray()) arr1[c-'a']++; for(char c:s2.toCharArray()) arr2[c-'a']++; int sum=0; for(int i=0;i<26;i++) sum+=((arr2[i]-arr1[i])>0)?(arr2[i]-arr1[i]):0; return sum; } public static boolean isSorted(int arr[]){ int sor[]=new int[arr.length]; for(int i=0;i<arr.length;i++) sor[i]=arr[i]; sort(sor); boolean check=true; for(int i=0;i<arr.length;i++){ if(sor[i]!=arr[i]){ check=false; break; } } return check; } public static int find(int arr[], int n){ for(int i=0;i<arr.length;i++) if(arr[i]==n) return i; return -1; } public static long calc(long n,long m, long mid,long k){ long ans=0; for(int i=1;i<=n;i++) ans+=Math.min(mid/i,m); return ans; } public static boolean check(int arr[],int k){ int count=0; for(int i=0;i<arr.length;i++) if(arr[i]%(i+1) == 0) count++; return count==k; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b){ return (a/gcd(a,b))*b; } static int limit=1000000; public static boolean same(String s){ boolean check=true; for(int i=1;i<s.length();i++){ if(s.charAt(i)!=s.charAt(i-1)){ check=false; break; } } return check; } static int limit1=1000000000; public static void printCount(int arr[],FastWriter out){ try{ for(int p:arr) out.print(p+" "); out.println(""); } catch ( Exception E){ } } public static class item{ int len; int cost; item(int l, int c){ len=l; cost=c; } @Override public String toString() { return "len: " + len + " cost: " + cost; } } public static class comparator implements Comparator<item>{ public int compare(item i, item j){ if(i.len*i.cost != j.len*j.cost) return (-(i.len*i.cost - j.len*j.cost)); else return (-(i.len-j.len)); } } public static void cpy(int arr1[],int arr2[]){ for(int i=0;i<arr1.length;i++) arr2[i]=arr1[i]; } static int UpperBound(ArrayList<Integer> a, int x) {// x is the key or target value int l=-1,r=a.size(); while(l+1<r) { int m=(l+r)>>>1; if(a.get(m)<=x) l=m; else r=m; } return l+1; } static int LowerBound(ArrayList<Integer> a, int x) { // x is the target value or key int l=-1,r=a.size(); while(l+1<r) { int m=(l+r)>>>1; if(a.get(m)>=x) r=m; else l=m; } return r-1; } static int binarySearch(ArrayList<Integer> arr, int x) { int left = 0, right = arr.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; // Check if x is present at mid if (arr.get(mid) == x) return mid; // If x greater, ignore left half if (arr.get(mid) < x) left = mid + 1; // If x is smaller, ignore right half else right = mid - 1; } // if we reach here, then element was // not present return -1; } static int upper_bound(ArrayList<Integer> arr, int key) { int upperBound = 0; while (upperBound < arr.size()) { // If current value is lesser than or equal to // key if (arr.get(upperBound) <= key) upperBound++; // This value is just greater than key else{ // System.out.print("The upper bound of " + key + " is " + arr[upperBound] + " at index " + upperBound); return upperBound; } } return -1; // System.out.print("The upper bound of " + key + " does not exist."); } public static void solve(FastReader in,FastWriter out){ try{ //----------------------------------------- int n=in.nextInt(); out.println(n); for(int i=0;i<n;i++){ int counter=2; for(int j=0;j<n;j++){ if(j==i) out.print("1 "); else out.print(counter++ +" "); } out.println(""); } //----------------------------------------- } catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { try { FastReader in = new FastReader(); FastWriter out = new FastWriter(); int tc=1; tc=in.nextInt(); while(tc-- != 0) solve(in,out); out.close(); } catch (Exception e) { e.printStackTrace(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
9a1d7ac54ce2496444d0fab395963fbe
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class contestA { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Arrays.fill(mk,false); int t = scanner.nextInt(); while (t-->0){ int n = scanner.nextInt(); ac(n); } } static boolean[] mk = new boolean[101]; static String[] ans = new String[101]; static void ac(int n){ if(n>100) return; if(!mk[n]){ ans[n]=n+"\n"; for(int i=n;i>=1;--i){ for(int j=1;j<=n;++j){ if(j==i) ans[n]+=n+" "; if(j<n) ans[n]+=j+" "; } ans[n]+= '\n'; } mk[n] = true; } System.out.print(ans[n]); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
63ea62efb7cf466e63877a45ab03816b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); int t; t=sc.nextInt(); first: while(t-->0) { long res,fill,place; int n=sc.nextInt(); System.out.println(n); for(int i=1;i<=n;i++) { StringBuilder str=new StringBuilder(); for(int j=2;j<=i;j++) str.append(j+" "); str.append(1+" "); for(int j=i+1;j<=n;j++) str.append(j+" "); System.out.println(str); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
2e01f9e2088112cfb1b32fa6f74fc046
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class _1716B{ public static void main (String[] args) { Scanner in = new Scanner(System.in); StringBuilder sb = new StringBuilder(); for (int t = in.nextInt(); t>0; t--){ int n = in.nextInt(); sb.append(n).append("\n"); int[] num = new int[n+1]; for (int i = 1; i <= n; i++) sb.append(num[i]=i).append(" "); sb.append("\n"); for (int i = n-1; i >= 1; i--) { num[i] = num[n]; num[n] = i; for (int j = 1; j <= n; j++) sb.append(num[j]).append(" "); sb.append("\n"); } } System.out.println(sb); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
946b3ad722d9759391e1c4276ab61721
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; import java.io.PrintWriter; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.math.BigInteger; public class Solution{ public static void main(String[] args) throws IOException{ FastReader sc=new FastReader(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int z=sc.nextInt(); for(int k=0;k<z;k++){ int a=sc.nextInt(); int res[]=new int[a]; for(int i=1;i<=a;i++){ res[i-1]=i; } System.out.println(a); for(int i=0;i<a;i++){ for(int j=0;j<a;j++){ out.write(res[j]+" "); } out.write("\n"); out.flush(); if(i<a-1){ int temp=res[i]; res[i]=res[i+1]; res[i+1]=temp; } } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
46fd19c7aae21c4c37b4ef0f52c63b2d
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class MyClass { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; System.out.println(n); for(int i=0;i<n;i++) arr[i]=i+1; for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); for(int i=1;i<n;i++){ int temp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=temp; String s=""; for(int j=0;j<arr.length;j++) s+=arr[j]+" "; System.out.println(s); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
ff960d3f06475d19b30fb2381b9f7452
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
//codeforces //package someAlgorithms; import java.util.*; import java.io.*; import java.lang.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException{ String[] strNums = br.readLine().split(" "); int t=Integer.parseInt(strNums[0]); while(t-->0) { //todo String[] strNums1 = br.readLine().split(" "); long n=Long.parseLong(strNums1[0]); // long h=Long.parseLong(strNums1[1]); // long m=Long.parseLong(strNums1[2]); // String str = br.readLine(); Long[] arr = new Long[(int)n]; bw.write(n+"");//if apply \n then bw come in new lineand delete prev line without printing therefore use .flush to come to next line as it prints before coming to ext line!! // StringTokenizer tk = new StringTokenizer(br.readLine().trim()); bw.newLine(); bw.flush(); for(int i=0;i<n;i++) { arr[i]=(long)i+1; bw.write(arr[i]+" "); } bw.newLine(); bw.flush(); int ind=0; while(ind+1<n) { long temp =arr[ind+1]; arr[ind+1]=arr[ind]; arr[ind]=temp; ind++; for(int i=0;i<n;i++) { bw.write(arr[i]+" "); } bw.newLine(); bw.flush(); } } } } /* 3 3 2 3 1 1 4 2 4 3 1 4 1 */ //class Pair{ // public long a=0; // public long b=0; // public Pair(long val,long id){ // this.a=val; // this.b=id; // } // //} //class Pair{ // public long val; // public long id; // public Pair(long val,long id){ // this.val=val; // this.id=id; // } // //} //class Node{ // public int val; //// public Node left=null; //// public Node right=null; // ArrayList<Node> children = new ArrayList<>(); // //} //class Comp implements Comparator<Pair>{ // public int compare(Pair p1,Pair p2) { // if(p1.val<p2.val) { // return -1; // } // if(p1.val>p2.val){ // return 1; // } // else return 0; //MUST WRITE THIS RETURN 0 FOR EQUAL CASE SINCE GIVE RUNTIME ERROR IN SOME COMPLIERS // } //} //if take gcd of whole array the take default gcd=0 and keep doing gcd of 2 elements of array! //public static int gcd(int a, int b){ // if(b==0){ // return a; // } // return gcd(b,a%b); //} //ArrayList<Long>[] adjlist = new ArrayList[(int)n+1]; //array of arraylist //for(int i=0;i<n;i++) { // String[] strNums2 = br.readLine().split(" "); // long a=Long.parseLong(strNums2[0]); // long b=Long.parseLong(strNums2[1]); // // adjlist[(int)a].add(b); // adjlist[(int)b].add(a); //} //int[][] vis = new int[(int)n+1][(int)n+1]; //OR can make list of list :- //List<List<Integer>> adjlist = new ArrayList<>(); //for(int i=0;i<n;i++){ // adjlist.add(new ArrayList<>()); //} //OR 1-D vis array //int[] vis = new int[(int)n+1]; /* Long[] arr = new Long[(int)n]; StringTokenizer tk = new StringTokenizer(br.readLine().trim()); for(int i=0;i<n;i++) { arr[i]=Long.parseLong(tk.nextToken()); } Long[][] arr = new Long[(int)n][(int)m]; for(int i=0;i<n;i++) { String[] strNums2 = br.readLine().split(" "); for(int j=0;j<m;j++) { arr[i][j]=Long.parseLong(strNums2[j]); } } 4 4 4 3 2 4 4 4 3 Main m = new Main(); //no need since pair class main class ne niche banao Pair p = m.new Pair(i,i+1); li.add(p); */ //double num = 3.9634; //double onum=num; //num = (double)((int)(num*100))/100; //double down = (num); //float up =(float) down; //if take up as double then will get large value when add (3.96+0.01)!! //if(down<onum) { // up=(float)down+(float)0.01; //// System.out.println(((float)3.96+(float)0.01)); //// System.out.println(3.96+0.01);//here both double, so output double , so here get large output other than 3.97!! //} ////in c++ 3.96+0.01 is by default 3.97 but in java need to type cast to float to get this output!! //System.out.println(down +" "+up); /* #include <iostream> #include <string> #include<vector> #include<queue> #include<utility> #include<limits.h> #include <unordered_set> #include<algorithm> using namespace std; */
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
f1f54f57557afdb6ae49f62cd72c3c82
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.math.BigInteger; import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int testcase=sc.nextInt(); for(int tc=1;tc<=testcase;tc++){ //System.out.print("Case #"+tc+": "); int n = sc.nextInt(); System.out.println(n); for (int i = 1; i <= n; i++) { StringBuilder str = new StringBuilder(); for (int j = 2; j <= i; j++) { str.append(j + " "); } str.append(1 + " "); for (int j = i + 1; j <= n; j++) str.append(j+" "); System.out.println(str); } //sc.close(); } } static long minSum(int arr[], int n, int k) { // k must be smaller than n if (n < k) { // System.out.println("Invalid"); return -1; } // Compute sum of first window of size k long res = 0; for (int i=0; i<k; i++) res += arr[i]; // Compute sums of remaining windows by // removing first element of previous // window and adding last element of // current window. long curr_sum = res; for (int i=k; i<n; i++) { curr_sum += arr[i] - arr[i-k]; res = Math.min(res, curr_sum); } return res; } static long gcd(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static int nextIndex(int a[], int x){ int n=a.length; for(int i=x;i<n-1;i++){ if(a[i]>a[i+1]){ return i; } } return n; } static void rev(int a[], int i, int j){ while(i<j){ int t=a[i]; a[i]=a[j]; a[j]=t; i++; j--; } } static int sorted(int arr[], int n) { // Array has one or no element or the // rest are already checked and approved. if (n == 1 || n == 0) return 1; // Unsorted pair found (Equal values allowed) if (arr[n - 1] < arr[n - 2]) return 0; // Last pair was sorted // Keep on checking return sorted(arr, n - 1); } static void sieveOfEratosthenes(int n, Set<Integer> set) { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value in // prime[i] will finally be false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it numbers which // are multiple of p and are less than p^2 // are already been marked. for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) set.add(i); } } static boolean isPowerOfTwo(int n) { return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static int countBits(int number) { // log function in base 2 // take only integer part return (int)(Math.log(number) / Math.log(2) + 1); } public static void swap(int ans[], int i, int j) { int temp=ans[i]; ans[i]=ans[j]; ans[j]=temp; } static int power(int x, int y, int p) { int res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
59d2df7333fdc4dc57bc557f3a141b2c
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class A_1 { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; static HashMap<String,Integer>map=new HashMap<>(); public static void main(String args[]) throws IOException { int T = in.nextInt(); StringBuilder res=new StringBuilder(); for(int f = 1; f<=T; f++) { int p = in.nextInt(); res.append(p + "\n"); for(int i=0; i<p; i++) { for(int j=0; j<p-i-1; j++) { res.append((j+1) + " "); } res.append(p + " "); for(int j=p-i; j<p; j++) { res.append(j + " "); } res.append("\n"); } } print(res); } static int max(int a, int b) { if(a<b) return b; return a; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static < E > void print(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
b49b68a98c579faae8964868d56ef61d
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args)throws Exception { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); while(x-->0) { int a=sc.nextInt(); int arr[]=new int[a]; BufferedWriter output =new BufferedWriter(new OutputStreamWriter(System.out)); for(int i=0;i<a;i++) { arr[i]=i+1; } output.write(a+"\n"); output.write(""+arr[0]); for(int i=1;i<a;i++) { output.write(" "+arr[i]); } output.write("\n"); for(int i=0;i<a-1;i++) { arr[i]=arr[i]^arr[i+1]; arr[i+1]=arr[i]^arr[i+1]; arr[i]=arr[i]^arr[i+1]; output.write(""+arr[0]); for(int j=1;j<a;j++) { output.write(" "+arr[j]); } output.write("\n"); } output.flush(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
264f8206b85a4914933ef38b92ee55b6
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class hello { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader(new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } // Catch block to handle the exceptions catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void getans(int s,int i,String ans[],String str,int count) { if(s==0) { if(ans[0]!=null&&count<ans[0].length()) { ans[0] = new String(str); } else if(ans[0]==null) { ans[0] = new String(str); } return; } if(s<i) return; for(int j=i;j<=9;j++) { getans(s-j,j+1,ans,str+Integer.toString(j),count+1); // System.out.println(j); } } public static void main (String[] args) throws java.lang.Exception { try { FastReader sc = new FastReader(); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); BufferedWriter output = new BufferedWriter( new OutputStreamWriter(System.out)); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) arr[i]=i+1; System.out.println(n); for(int i=0;i<n;i++) { output.write(arr[i]+" "); } output.write("\n"); int k = 1; while(k<n) { int temp = arr[k-1]; arr[k-1] = arr[k]; arr[k] = temp; for(int val:arr) { output.write(val+" "); } output.write("\n"); k++; } output.flush(); } } catch (Exception e) { return; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
baf8b4df6c7c7388d06780ce351666e4
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { int T=0; Scanner in = new Scanner(System.in); T = in.nextInt(); while(T > 0) { int n=0; n=in.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; i++) { arr[i] =i+1; } System.out.println(n); for(int i=0; i<n; i++) { System.out.print(arr[i]+" "); } System.out.println(); int j=1, temp=0; while(j<n) { temp = arr[0]; arr[0] = arr[j]; arr[j] = temp; String s=""; s = Arrays.toString(arr); s=s.replace("[", ""); s=s.replace("]", ""); s=s.replace(",", " "); System.out.println(s); // for(int i=0; i<n ;i++) { // System.out.print(arr[i]+" "); // } // System.out.println(); j++; } T--; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
6b52d59e8d0c0c0ac9fc18b2e5f61e37
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class codeforces { static BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { StringTokenizer st = new StringTokenizer(r.readLine()); int t = Integer.parseInt(st.nextToken()); for (int i = 0; i < t; i++) { // for each test case st = new StringTokenizer(r.readLine()); int n = Integer.parseInt(st.nextToken()); pw.println(n); int[] arr = new int[n]; for (int j = 0; j < n; j++) { arr[j] = j+1; pw.print(j+1+" "); } pw.println(); for (int j = 0; j < n-1; j++) { int first = arr[j]; int second = arr[j+1]; arr[j+1] = first; arr[j] = second; for(int num : arr) { pw.print(num+" "); } pw.println(); } /* if (n % 2 == 1) { int first = arr[n-2]; int second = arr[n-1]; arr[n-1] = first; arr[n-2] = second; for(int num : arr) { pw.print(num+" "); } pw.println(); } */ } pw.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
100bd0a8965ba8b15ed409aaf0612c9f
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void print(int arr[],BufferedWriter log) throws Exception { for(int i=0;i<arr.length;i++) { log.write(arr[i]+" "); } log.write("\n"); } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Reader s=new Reader(); //FastReader s=new FastReader(); //Scanner s=new Scanner(System.in); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t=s.nextInt(); for(int x=0;x<t;x++) { int n=s.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=i+1; } log.write(n+"\n"); print(arr,log); for(int i=0;i<n-1;i++) { arr[i]=arr[i+1]; arr[i+1]=1; print(arr,log); } } log.flush(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
33b3520102245be9b10aeb6be1892c95
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.sort; public class Round12 { public static void main(String[] args) { FastReader fastReader = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fastReader.nextInt(); while (t-- > 0) { int n = fastReader.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = (i + 1); out.println(n); for (int i = n - 1; i >= 0; i--) { if (i != n - 1){ int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } StringBuilder ans = new StringBuilder(); for (int j = 0; j < n; j++) { ans.append(a[j]).append(" "); } out.println(ans); } } out.close(); } public static void nextPermutation(int[] nums) { int i = nums.length - 2; while (i >= 0 && nums[i + 1] <= nums[i]) { i--; } if (i >= 0) { int j = nums.length - 1; while (nums[j] <= nums[i]) { j--; } swap(nums, i, j); } reverse(nums, i + 1); } public static void reverse(int[] nums, int start) { int i = start, j = nums.length - 1; while (i < j) { swap(nums, i, j); i++; j--; } } public static void swap(int[] nums, int j, int i) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } static class Pair { int len; int x; int y; Pair(int len, int x, int y) { this.len = len; this.x = x; this.y = y; } } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 92233720368547L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() { return Long.parseLong(next()); } long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
3ed1a526985bbd9d998783de12c18f6e
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Codeforces { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { StringBuilder sb= new StringBuilder(""); int n=sc.nextInt(); System.out.println(n); for(int i=0;i<n;i++) { sb.append((i+1)+" "); } sb.append("\n"); //sSystem.out.println(); int arr[]= new int[n]; for(int k=0;k<n;k++) { arr[k]=k+1; } for(int i=1;i<n;i++) { int temp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=temp; for(int j=0;j<n;j++) { sb.append((arr[j])+" "); } sb.append("\n"); } System.out.print(sb); } sc.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
60501ad7531e4f652ee5be79c27c74b3
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public final class Main{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void print(int[] arr){ PrintWriter out=new PrintWriter(System.out); for(int i=0;i<arr.length-1;i++) { out.print(arr[i]+" "); } out.println(arr[arr.length-1]); out.flush(); } public static void main(String[] args) throws java.lang.Exception{ PrintWriter out=new PrintWriter(System.out); FastReader sc = new FastReader(); int t=sc.nextInt(); // HashMap<Integer,ArrayList<int[]>> map = new HashMap<>(); // int[] dp = new int[] while(t-->0) { int n =sc.nextInt(); // if (map.containsKey(n)){ // System.out.println(n); // for(int[] ppp:map.get(n)) { // print(ppp); // } // continue; // } int[] arr = new int[n]; for(int i=1;i<=n;i++) { arr[i-1]=i; } System.out.println(n); // ArrayList<int[]> pp = new ArrayList<>(); // pp.add(arr.clone()); print(arr); for(int i=1;i<n;i++) { int tt=arr[i]; arr[i]=arr[i-1]; arr[i-1]=tt; print(arr); // pp.add(arr.clone()); } // map.put(n, pp); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
0efdb00d4e9b0f1130cbf47e4e41a217
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class B_Permutation_Chain { public static void main(String[] args)throws Exception { new Solver().solve(); } } //* Success is not final, failure is not fatal: it is the courage to continue that counts. class Solver { void solve() throws Exception { int t = sc.nextInt(); while(t-->0){ int n =sc.nextInt(); // int x = n-1; System.out.println(n); // int r = n; // for(int i = 1;i<=n;i++){ // System.out.print(i+" "); // } // System.out.println(); for(int k = n;k>=1;k--){ StringBuilder SB = new StringBuilder(); int i = 1; while(i<k){ SB.append(i+" "); i++; } SB.append(n+" "); while(i<n){ SB.append(i+" "); i++; } System.out.println(SB.toString()); } } sc.flush(); } final Helper sc; final int MAXN = 1000_006; final long MOD = (long) 1e9 + 7; Solver() { sc = new Helper(MOD, MAXN); sc.initIO(System.in, System.out); } } class Helper { final long MOD; final int MAXN; final Random rnd; public Helper(long mod, int maxn) { MOD = mod; MAXN = maxn; rnd = new Random(); } public static int[] sieve; public static ArrayList<Integer> primes; public void setSieve() { primes = new ArrayList<>(); sieve = new int[MAXN]; int i, j; for (i = 2; i*i < MAXN; ++i) if (sieve[i] == 0) { primes.add(i); for (j = i*i; j < MAXN; j += i) { sieve[j] = i; } } } public static long[] factorial; public void setFactorial() { factorial = new long[MAXN]; factorial[0] = 1; for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD; } public long getFactorial(int n) { if (factorial == null) setFactorial(); return factorial[n]; } public long ncr(int n, int r) { if (r > n) return 0; if (factorial == null) setFactorial(); long numerator = factorial[n]; long denominator = factorial[r] * factorial[n - r] % MOD; return numerator * pow(denominator, MOD - 2, MOD) % MOD; } public long[] getLongArray(int size) throws Exception { long[] ar = new long[size]; for (int i = 0; i < size; ++i) ar[i] = nextLong(); return ar; } public int[] getIntArray(int size) throws Exception { int[] ar = new int[size]; for (int i = 0; i < size; ++i) ar[i] = nextInt(); return ar; } public long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public long max(long[] ar) { long ret = ar[0]; for (long itr : ar) ret = Math.max(ret, itr); return ret; } public int max(int[] ar) { int ret = ar[0]; for (int itr : ar) ret = Math.max(ret, itr); return ret; } public long min(long[] ar) { long ret = ar[0]; for (long itr : ar) ret = Math.min(ret, itr); return ret; } public int min(int[] ar) { int ret = ar[0]; for (int itr : ar) ret = Math.min(ret, itr); return ret; } public long sum(long[] ar) { long sum = 0; for (long itr : ar) sum += itr; return sum; } public long sum(int[] ar) { long sum = 0; for (int itr : ar) sum += itr; return sum; } public void shuffle(int[] ar) { int r; for (int i = 0; i < ar.length; ++i) { r = rnd.nextInt(ar.length); if (r != i) { ar[i] ^= ar[r]; ar[r] ^= ar[i]; ar[i] ^= ar[r]; } } } public void shuffle(long[] ar) { int r; for (int i = 0; i < ar.length; ++i) { r = rnd.nextInt(ar.length); if (r != i) { ar[i] ^= ar[r]; ar[r] ^= ar[i]; ar[i] ^= ar[r]; } } } public long pow(long base, long exp, long MOD) { base %= MOD; long ret = 1; while (exp > 0) { if ((exp & 1) == 1) ret = ret * base % MOD; base = base * base % MOD; exp >>= 1; } return ret; } static byte[] buf = new byte[1000_006]; static int index, total; static InputStream in; static BufferedWriter bw; public void initIO(InputStream is, OutputStream os) { try { in = is; bw = new BufferedWriter(new OutputStreamWriter(os)); } catch (Exception e) { } } public void initIO(String inputFile, String outputFile) { try { in = new FileInputStream(inputFile); bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outputFile))); } catch (Exception e) { } } private int scan() throws Exception { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } public String nextLine() throws Exception { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c >= 32; c = scan()) sb.append((char) c); return sb.toString(); } public String next() throws Exception { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) sb.append((char) c); return sb.toString(); } public int nextInt() throws Exception { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') c = scan(); for (; c >= '0' && c <= '9'; c = scan()) val = (val << 3) + (val << 1) + (c & 15); return neg ? -val : val; } public long nextLong() throws Exception { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') c = scan(); for (; c >= '0' && c <= '9'; c = scan()) val = (val << 3) + (val << 1) + (c & 15); return neg ? -val : val; } public void print(Object a) throws Exception { bw.write(a.toString()); } public void printsp(Object a) throws Exception { print(a); print(" "); } public void println() throws Exception { bw.write("\n"); } public void printArray(int[] arr) throws Exception{ for(int i = 0;i<arr.length;i++){ print(arr[i]+ " "); } println(); } public void println(Object a) throws Exception { print(a); println(); } public void flush() throws Exception { bw.flush(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
a351fb198a903adc2013829ebf7cd0ff
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
/*input Prateek Singh */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class codeforces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { if (System.getProperty("ONLINE_JUDGE") == null) { // Input is a file try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } } else { // Input is System.in } FastReader sc = new FastReader(); // Scanner sc = new Scanner(System.in); //System.out.println(java.time.LocalTime.now()); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while (t > 0) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 1; i <= n; i++)arr[i - 1] = i; sb.append(n + "\n"); print(arr, sb); arr[0] = n; arr[n - 1] = 1; print(arr, sb); for (int i = 3; i <= n; i++) { int temp = arr[0]; arr[0] = arr[i - 2]; arr[i - 2] = temp; print(arr, sb); } //sb.append(ans + "\n"); t--; } System.out.println(sb); } public static void print(int[] arr, StringBuilder sb) { for (int i : arr)sb.append(i + " "); sb.append("\n"); } public static int bs(ArrayList<Long> al, long tar) { int l = 0; int r = al.size() - 1; while (l < r) { int mid = (l + r) >> 1; if (al.get(mid) < tar)l = mid + 1; else r = mid; } if (al.get(l) <= tar )return l; else return l - 1; } public static void dfs(Pair src, ArrayList<ArrayList<Pair>> al, ArrayList<Long> dist, ArrayList<Long> dist2, int[] ans) { long a = src.a; long b = src.b; if (dist.size() != 0)a += dist.get(dist.size() - 1); if (dist2.size() != 0)b += dist2.get(dist2.size() - 1); dist.add(a); dist2.add(b); ans[src.des] = bs(dist2, dist.get(dist.size() - 1)); for (Pair p : al.get(src.des)) { dfs(p, al, dist, dist2, ans); } dist.remove(dist.size() - 1); dist2.remove(dist2.size() - 1); } public static boolean isPowerOfTwo(long x) { return x != 0 && ((x & (x - 1)) == 0); } static class Pair { int des; long a; long b; Pair (int des, long a, long b) { //constructor this.des = des; this.a = a; this.b = b; } } //////////nCr//////////////////////////////////////// ///////// SUM OF EACH DIGIT OF A NUMBER /////////////// public static long digSum(long a) { long sum = 0; while (a > 0) { sum += a % 10; a /= 10; } return sum; } ///////// TO CHECK NUMBER IS PRIME OR NOT /////////////// public static boolean isPrime(int n) { if (n <= 1)return false; if (n <= 3)return true; if (n % 2 == 0 || n % 3 == 0)return false; for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0)return false; } return true; } ///////// NEXT PRIME NUMBER BIGGER THAN GIVEN NUMBER /////////////// public static int nextPrime(int n) { while (true) { n++; if (isPrime(n)) break; } return n; } ///////// GCD /////////////// public static long gcd(long a, long b) { if (b == 0)return a; return gcd(b, a % b); } ///////// LCM /////////////// // public static int lcm(int a, int b) { // return (a * b) / gcd(a, b); // } public static long factorial(int n) { long ans = 1; if (n == 0 || n == 1)return (ans); for (int i = 2; i <= n; i++) { ans *= i; } return ans; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
7a645c9eed20d8aa30ff80ef702c44e4
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int h; static int n; static long mod = 1000000007; public static void main(String[] args) { Sca in = new Sca(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); out.println(n); for (int i = 0; i < n; i++) { out.printf("%d ", i + 1); } out.println(); for (int i = 1; i < n; i++) { int j = 2; for (; j < i + 2; j++) { out.printf("%d ", j); } out.printf("1 "); for (; j <= n; j++) { out.printf("%d ", j); } out.println(); } } out.close(); } static class Sca { BufferedReader br; StringTokenizer st; public Sca(InputStream io) { br = new BufferedReader(new InputStreamReader(io)); } public String line() { String result = ""; try { result = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return result; } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public char[] nextc() { return next().toCharArray(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
62e51ea93f1122f8312385f9eefa1151
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solution127 { private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private final PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { Solution127 solution = new Solution127(); solution.solve(); solution.close(); } private void solve() { int t = readInt(); for (int i = 0; i < t; i++) { solveCase(); } } void append(int[] a, StringBuilder sb) { sb.append(a[0]); for (int i = 1; i < a.length; i++) { sb.append(" "); sb.append(a[i]); } sb.append("\n"); } private void solveCase() { int n = readInt(); int[] a = new int[n]; for (int i = 1; i <= n; i++) { a[i - 1] = i; } StringBuilder sb = new StringBuilder(); append(a, sb); int t = a[0]; a[0] = a[1]; a[1] = t; int l = 2; append(a, sb); l: while (true) { for (int j = 0; j < n; j++) { if (a[j] == j + 1) { l++; t = a[0]; a[0] = a[j]; a[j] = t; append(a, sb); continue l; } } break; } pw.println(l + "\n" + sb.toString().trim()); } private void close() { pw.close(); } private String readLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } private String readString() { return readLine(); } private long readLong() { return Long.parseLong(readLine()); } private int readInt() { return Integer.parseInt(readLine()); } private String[] stringArray() { StringTokenizer st = new StringTokenizer(readLine()); int n = st.countTokens(); String ret[] = new String[n]; for (int i = 0; i < n; i++) { ret[i] = st.nextToken(); } return ret; } private int[] readIntArr() { String[] str = stringArray(); int arr[] = new int[str.length]; for (int i = 0; i < arr.length; i++) arr[i] = Integer.parseInt(str[i]); return arr; } private double[] readDoubleArr() { String[] str = stringArray(); double arr[] = new double[str.length]; for (int i = 0; i < arr.length; i++) arr[i] = Double.parseDouble(str[i]); return arr; } private long[] readLongArr() { String[] str = stringArray(); long arr[] = new long[str.length]; for (int i = 0; i < arr.length; i++) arr[i] = Long.parseLong(str[i]); return arr; } private double readDouble() { return Double.parseDouble(readLine()); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
e51c000373d3c67b8e0c699380dc63b4
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class c{ static int []f=new int [2000001]; //static int []a=new int [500001]; static long max=123456789; public static void main(String []args) { MyScanner s=new MyScanner(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int []f=new int [n]; for(int i=0;i<n;i++) f[i]=i+1; int l=0; System.out.println(n); for(int x:f) out.print(x+" "); out.println(); while(l<n-1) { int a=f[l]; f[l]=f[l+1]; f[l+1]=a; for(int x:f) out.print(x+" "); out.println(); l++; } out.flush(); } } public static void dfs(int n,long sum,int u,int k) { if(k>=n) { if(k==n) max=Math.min(sum,max); return ; } for(int i=u;i<=9;i++) { dfs(n,sum*10+i,i+1,k+i); } } /* public static void buildertree(int k,int l,int r) { if(l==r) { f[k]=a[l]; return ; } int m=l+r>>1; buildertree(k+k,l,m); buildertree(k+k+1,m+1,r); f[k]= } public static void update(int u,int l,int r,int x,int c) { if(l==x && r==x) { f[u]=c; return; } int mid=l+r>>1; if(x<=mid)update(u<<1,l,mid,x,c); else if(x>=mid+1)update(u<<1|1,mid+1,r,x,c); f[u]=Math.max(f[u+u], f[u+u+1]); } public static int query(int k,int l,int r,int x,int y) { if(x==l&&y==r) { return f[k]; } int m=l+r>>1; if(y<=m) { return query(k+k,l,m,x,y); } else if(x>m)return query(k+k+1,m+1,r,x,y); else { int i=query(k+k,l,m,x,m),j=query(k+k+1,m+1,r,m+1,y); return Math.max(j, Math.max(i+j, i)); } } public static void calc(int k,int l,int r,int x,int z) { f[k]+=z; if(l==r) { return ; } int m=l+r>>1; if(x<=m) calc(k+k,l,m,x,z); else calc(k+k+1,m+1,r,x,z); } */ static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
a0fca170e78ea0f6cb2d2da71d8b1de3
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
/****************************************************************************** No one else matters, this is the moment, this is day. *******************************************************************************/ import java.io.*; import java.util.*; public class Main { static PrintWriter out=new PrintWriter(System.out); public static void printArr(int arr[], int N){ for(int i=0; i<N; i++){ out.print(arr[i]+" "); } out.println(""); } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(System.in); // int T = sc.nextInt(); int T = Integer.parseInt(br.readLine()); while (T-- > 0) { int N = Integer.parseInt(br.readLine()); out.println(N); int arr[] = new int[N]; for(int i=1; i<=N; i++) arr[i-1] = i; printArr(arr, N); for(int i=1; i<N; i++){ int t = arr[N-i-1]; arr[N-i-1] = arr[N-i]; arr[N-i] = t; printArr(arr, N); } } out.flush(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
7ba995590a19ad7bdb13acf40d3faff1
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int pos = 1; System.out.println(n); for (int i = 1; i <= n; i++) { StringBuilder str = new StringBuilder(); for (int j = 2; j <= i; j++) { str.append(j + " "); } str.append(1 + " "); for (int j = i + 1; j <= n; j++) str.append(j+" "); System.out.println(str); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
e30c0b7358d07e8d93f65331ff018d31
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
//package codeforces; import java.util.*; import java.io.*; public class CF { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int q = Integer.parseInt(br.readLine()); while(q-->0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); pw.println(n); int[] arr = new int[n]; for (int i=0; i<n; i++) { arr[i] = i+1; pw.print(arr[i]+" "); } pw.println(); for (int i=0; i<n-1; i++) { int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; for (int j: arr) { pw.print(j+" "); } pw.println(); } } pw.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
46bb85357dbed6952cd512ee4af21a49
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; /* */ public class B{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); PrintWriter out=new PrintWriter(System.out); for(int tt=0;tt<t;tt++) { int n=sc.nextInt(); out.println(n); for(int i=0;i<n;i++) { int ans[]=new int[n]; for(int j=0;j<n;j++)ans[j]=j+1; for(int j=0;j<i;j++) ans[j]=ans[j+1]; ans[i]=1; for(int e:ans)out.print(e+" "); out.println(); } } out.close(); } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String next() { while(!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
efd4dc17ae451699f10eb4e793bd7d80
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
// package com.company.Codeforces;/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class newPro { public static void solve(){ int n = sc.nextInt(); int [] p = new int [n]; for(int i=0; i<n; i++){ p[i] = i+1; } out.println(n); for(int i=0; i<n; i++){ for(int num : p){ out.print(num + " "); } out.println(); if(i < n-1){ int tmp = p[i]; p[i] = p[i+1]; p[i+1] = tmp; } } } static PrintWriter out; static Scanner sc; public static void main (String[] args) throws java.lang.Exception { out = new PrintWriter(System.out); sc = new Scanner(); int t = sc.nextInt(); while(t-->0){ solve(); } out.flush(); out.close(); } private static long power(long a, long b) { long res = 1; while (b > 0) { if ((b & 1)==1) res = res * a; a = a * a; b >>= 1; } return res; } private static long power(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { if ((b & 1)==1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } private static long lcm(long a, long b) { return a * b / gcd(a, b); } private static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); /* try { br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("file_i_o\\input.txt")))); } catch (FileNotFoundException e) { e.printStackTrace(); } */ } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } Integer[] nextIntegerArray(int n) { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } String[] nextStringArray() { return nextLine().split(" "); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } return array; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
42276c066f6535b51961fa41537cee9e
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
//Code By KB. import java.beans.Visibility; import java.io.*; import java.lang.annotation.Target; import java.lang.reflect.Array; import java.nio.channels.AsynchronousCloseException; import java.security.KeyStore.Entry; import java.util.*; import java.util.logging.*; import java.io.*; import java.util.logging.*; import java.util.regex.*; import javax.management.ValueExp; import javax.print.DocFlavor.INPUT_STREAM; import javax.swing.plaf.basic.BasicBorders.SplitPaneBorder; import javax.swing.text.AbstractDocument.LeafElement; import javax.xml.validation.Validator; public class Codeforces { public static void main(String[] args) { FlashFastReader in = new FlashFastReader(System.in); try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));) { new Codeforces().solution(in, out); } catch (Exception e) { System.out.println(e.getStackTrace()); } } public void solution(FlashFastReader in, PrintWriter out) { try { int t = in.nextInt(); while (t-->0) { int n = in .nextInt(); int a[] = new int[n]; out.println(n); for (int i = 0; i < n ; i++) { a[i] = i+1; out.print(a[i]+" "); } out.println(); for (int i = 0; i < a.length-1; i++) { int temp= a[i]; a[i]=a[i+1]; a[i+1]=temp; for (int j = 0; j < a.length; j++) { out.print(a[j]+" "); } out.println(); } } } catch (Exception exception) { out.println(exception); } } public int countSquares(int[][] matrix) { int dp [][] = new int [matrix.length+1][matrix[0].length+1]; for (int i = 1; i <=matrix.length; i++) { for (int j = 1; j <=matrix[0].length; j++) { if (matrix[i][j]==1) { if (i>=1&&j>=1) { dp[i][j] =1+ Math.min(dp[i-1][j], Math.min(dp[i-1][j-1], dp[i][j-1]) ); } } x+=dp[i][j]; } } return x; } HashMap<Integer, Integer> map = new HashMap<>(); public int totalNQueens(int n) { if (n==1) { return 1; } if (n==2||n==3) { return 0; } dfsNqueens( map , 0 , n ); return x; } public void dfsNqueens( HashMap<Integer, Integer>map , int col , int n ) { if (col == n) { x++; return; } for (int i = 0; i < n; i++) { map.put(col, i); boolean f = true; for (int j = 0; j < n; j++) { if (map.containsKey(j)) { if (map.get(j)==i||Math.abs(col - j)==Math.abs(map.get(j)-i) ) { f = false; } } } if (f) { dfsNqueens(map, col+1, n); } } } public int[] minOperations(String boxes) { int a [] = new int[boxes.length()]; for (int i = 0; i < boxes.length(); i++) { StringBuilder sb = new StringBuilder(boxes); int x = 0; for (int j = 0; j < sb.length(); j++) { if (sb.indexOf("1")<0) { break; } x+=Math.abs(i - sb.indexOf("1")); sb.setCharAt(sb.indexOf("1"), '0'); } a[i] = x; } return a; } public List<List<Integer>> subsets(int[] nums) { List<Integer> curr = new ArrayList<>(); subsetsGen(0, nums , curr); return list; } public void subsetsGen(int j ,int[] n , List<Integer>curr) { list.add(curr); for (int i = j; i < n.length; i++) { curr.add(n[i]); subsetsGen(j+1, n, curr); curr.remove(curr.size()-1); } } boolean visited[]= new boolean[1000]; public int findCircleNum(int[][] isConnected) { for (int i = 0; i < isConnected.length; i++) { if (visited[i]!=true) { x++; dfsFindProvinces( i , isConnected ); } } return x; } public void dfsFindProvinces( int i ,int [][]isConnected ) { visited[i]=true; for (int j = 0; j < isConnected.length; j++) { if (isConnected[i][j]==1&&visited[j]!=true) { dfsFindProvinces(j, isConnected); } } } public int[] canSeePersonsCount(int[] heights) { int ans [] = new int[heights.length]; Stack<Integer> stck = new Stack<>(); for (int i = 0; i < heights.length; i++) { while(!stck.isEmpty()&&heights[stck.peek()]<heights[i]) { ans[stck.pop()]++; } if (!stck.isEmpty()) { ans[stck.peek()]++; } stck.push(i); } return ans; } public int pathSum(TreeNode root, int targetSum) { List<Integer> path = new ArrayList<>(); dsfPathSum(root, targetSum , 0 ,path); pathSum(root.left, targetSum); pathSum(root.right, targetSum); for (List<Integer> p : list) { for (int i = 0; i < p.size() ; i++) { System.out.print(p.get(i)+" "); } System.out.println(); } return list.size(); } public void dsfPathSum(TreeNode root , int t , long sum , List<Integer>path ) { if (root==null) { return; } sum+=root.val ; path.add(root.val); if (sum==t) { list.add(path); } dsfPathSum(root.left, t, sum , path); dsfPathSum(root.right, t, sum , path); } public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode l = new ListNode(); int a = 0; int b =0; l1 = reverseList(l1); l2 = reverseList(l2); while (l1!=null) { a=a*10+l1.val; l1=l1.next; } while (l2!=null) { b=b*10+l2.val; l2=l2.next; } a=a+b; ListNode head = null; while (a!=0) { int r = a%10; if(l==null) { head= new ListNode(r); l=head; }else { l.next = new ListNode(r); l= l.next; } a/=10; } return head; } public ListNode reverseList(ListNode head) { if(head==null || head.next==null) return head; ListNode nextNode=head.next; ListNode newHead=reverseList(nextNode); nextNode.next=head; head.next=null; return newHead; } int x=0; public int uniquePathsIII(int[][] grid) { int zeros = 0; int sx=0; int sy =0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j]==0) { zeros++; } else if (grid[i][j]==1) { sx=i; sy=j; } } } dfsUniquePaths(zeros , sx , sy , grid); return x; } public void dfsUniquePaths(int zeros ,int i ,int j , int g[][]) { if (i<0||i>=g.length||j>=g[0].length||j<0||g[i][j]<0) { System.out.println(i+" "+j); return; } if (g[i][j]==2) { if(zeros==0) x++; return; } g[i][j]=-2; zeros--; dfsUniquePaths(zeros, i+1, j, g); dfsUniquePaths(zeros, i-1, j, g); dfsUniquePaths(zeros, i, j+1, g); dfsUniquePaths(zeros, i, j-1, g); g[i][j]=0; zeros++; } public TreeNode addOneRow(TreeNode root, int val, int depth) { if (depth==1) { TreeNode t = new TreeNode(val); t.left=root; return t; } dfsAddOneRow(root , 1 , val , depth); return root; } public void dfsAddOneRow(TreeNode root , int currD , int val , int depth) { if (root==null) { return ; } if (currD==depth) { TreeNode l = root.left; root.left = new TreeNode(val); root.left.left = l; TreeNode r = root.right; root.right = new TreeNode(val); root.right.right = r; } currD++; dfsAddOneRow(root.left, currD, val, depth); dfsAddOneRow(root.right, currD, val, depth); } public List<List<Integer>> threeSum(int[] nums) { int n = nums.length; int sum =0; HashSet<List<Integer>> h = new HashSet<List<Integer>>(); for (int i = 0; i < nums.length; i++) { sum=nums[i]; for (int j = i+1; j < nums.length; j++) { sum+=nums[j]; int k =j; n = nums.length; while (k<n) { int mid = (k+n)/2; if ((sum+nums[mid])==0) { HashSet<Integer> m = new HashSet<>(); m.add(nums[i]); m.add(nums[j]); m.add(nums[mid]); if (m.size()==3) { List<Integer>x = new ArrayList<>(); x.add(nums[i]); x.add(nums[j]); x.add( nums[mid]); h.add(x); } } else if ((sum+nums[mid])>0) { n=mid; } else { k=mid; } } } } return list; } public int uniquePaths(int m, int n) { int dp[][]= new int [m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if(i==0&&j==0) { dp[i][j]=1; } else if (i==0) { dp[i][j]=dp[i][j]+dp[i][j-1]; } else if (j==0) { dp[i][j]=dp[i][j]+dp[i-1][j]; } else { dp[i][j]+=dp[i][j-1]+dp[i-1][j]; } } } return dp[m-1][n-1]; } public int maxProfit(int[] prices) { int p = -1; return p ; } public int trap(int[] height) { int n = height.length; int left[] = new int [n]; int right[] = new int [n]; left[0] = height[0]; for (int i = 1; i <n; i++) { left[i] = Math.max(height[i], left[i-1]); } right[n-1]=height[n-1]; for (int i = n-2; i>=0 ; i--) { right[i] = Math.max(height[i], right[i+1]); } for (int i = 1; i < right.length-1; i++) { int s =Math.min(left[i] , right[i])-height[i]; if (s>0) { x+=s; } } return x; } public int maxProduct(int[] nums) { int maxSoFar = Integer.MIN_VALUE; int maxTillEnd = 0; for (int i = 0; i < nums.length; i++) { maxTillEnd*=nums[i]; if (maxSoFar<maxTillEnd) { maxSoFar=maxTillEnd; } if (maxTillEnd<=0) { maxTillEnd=1; } } return maxSoFar; } public int maximumScore(int[] nums, int[] multipliers) { int n = multipliers.length; int dp[] = new int[n+1]; for (int i = 1; i < multipliers.length; i++) { for (int j = 1; j < nums.length; j++) { dp[i] = Math.max(dp[j-1], multipliers[i-1]*nums[i-1]); } } return dp[n]; } public int islandPerimeter(int[][] grid) { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if(grid[i][j]==1){ dfsIslandPerimeter(i , j , grid); } } } return x; } public void dfsIslandPerimeter(int i , int j , int[][] grid) { if(i<0||i>=grid.length||j<0||j>grid[0].length) { x++; return; } if(grid[i][j]==0) { x++; return; } if(grid[i][j]==1) { return; } dfsIslandPerimeter(i+1, j, grid); dfsIslandPerimeter(i-1, j, grid); dfsIslandPerimeter(i, j+1, grid); dfsIslandPerimeter(i, j-1, grid); } public int[] findOriginalArray(int[] changed) { int n =changed.length; int m = n/2; int a[] = new int[m]; if (n<2) { return new int[]{}; } if (n%2!=0) { return new int[]{}; } TreeMap<Integer , Integer> map = new TreeMap<>(); for (int i = 0; i < changed.length; i++) { map.put(changed[i], map.getOrDefault(changed[i] ,0)+1); } int j =0; for (Map.Entry<Integer , Integer> e : map.entrySet()) { if (map.containsKey(e.getKey()*2)&&map.get(e.getKey()*2)>=1&&map.get(e.getKey())>=1) { map.put(e.getKey(), map.get(e.getKey()) -1); if(j==n-1) break; a[j++] = e.getKey(); } } return a; } public int jump(int[] nums) { int n =nums.length; int jumps =1; int i =0; int j =0; while (i<n) { int max =0; for (j=i+1 ;j<=i+nums[i];j++) { max = Math.max(max , nums[j]+j); } jumps++; i=max; } return jumps; } static void sort(int[] arr) { if (arr.length < 2) return; int mid = arr.length / 2; int[] left_half = new int[mid]; int[] right_half = new int[arr.length - mid]; // copying the elements of array into left_half for (int i = 0; i < mid; i++) { left_half[i] = arr[i]; } // copying the elements of array into right_half for (int i = mid; i < arr.length; i++) { right_half[i - mid] = arr[i]; } sort(left_half); sort(right_half); merge(arr, left_half, right_half); } static void merge(int[] arr, int[] left_half, int[] right_half) { int i = 0, j = 0, k = 0; while (i < left_half.length && j < right_half.length) { if (left_half[i] < right_half[j]) { arr[k++] = left_half[i++]; } else { arr[k++] = right_half[j++]; } } while (i < left_half.length) { arr[k++] = left_half[i++]; } while (j < right_half.length) { arr[k++] = right_half[j++]; } } public int minCostClimbingStairs(int[] cost) { int n = cost.length; int dp[] = new int[n+1]; dp[1] = cost[0]; dp[2] = cost[1]; for (int i = 3; i <=n; i++) { dp[i]=cost[i-1]+Math.min(dp[i-1], dp[i-2]); } return dp[n]; } TreeNode newAns =null; public TreeNode removeLeafNodes(TreeNode root, int target) { newAns = root; dfsRemoveNode(newAns , target); return newAns; } public void dfsRemoveNode(TreeNode root , int t) { if (root==null) { return; } if (root.val==t) { if (root.left!=null) { root=root.left; }else if (root.right!=null){ root=root.right; } else { root=null; } } dfsRemoveNode(root.left, t); dfsRemoveNode(root.right, t); } public int maxProfit(int k, int[] prices) { int n = prices.length; if (n <= 1) return 0; if (k >= n/2) { return stocks(prices, n); } int[][] dp = new int[k+1][n]; for (int i = 1; i <=k; i++) { int c= dp[i-1][0] - prices[0]; for (int j = 1; j < n; j++) { dp[i][j] = Math.max(dp[i][j-1], prices[j] + c); c = Math.max(c, dp[i-1][j] - prices[j]); } } return dp[k][n-1]; } public int stocks(int [] prices , int n ) { int maxPro = 0; for (int i = 1; i < n; i++) { if (prices[i] > prices[i-1]) maxPro += prices[i] - prices[i-1]; } return maxPro; } public List<List<Integer>> combine(int n, int k) { List<Integer> x = new ArrayList<>(); combinations( n , k , 1 ,x); return list; } public void combinations(int n , int k ,int startPos , List<Integer> x) { if (k==x.size()) { list.add(new ArrayList<>(x)); //x=new ArrayList<>(); return; } for (int i = startPos; i <=n; i++) { x.add(i); combinations(n, k, i+1, x); x.remove(x.size()-1); } } List<List<Integer>> list = new ArrayList<>(); public List<List<Integer>> permute(int[] a) { List<Integer> x = new ArrayList<>(); permutations(a , 0 ,a.length ,x ); return list ; } public void permutations(int a[] , int startPos , int l , List<Integer>x) { if (l==x.size()) { list.add(new ArrayList<>(x)); //x=new ArrayList<>(); } for (int i = 0; i <=l; i++) { if (!x.contains(a[i])) { x.add(a[i]); permutations(a, i, l, x); x.remove(x.size()-1); } } } List<String> str = new ArrayList<>(); public List<String> letterCasePermutation(String s) { casePermutations(s , 0 , s.length() , "" ); return str; } public void casePermutations(String s ,int i , int l , String curr) { if (curr.length()==l) { str.add(curr); return; } char b = s.charAt(i); curr+=b; casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); if (Character.isAlphabetic(b)&&Character.isLowerCase(b)) { curr+=Character.toUpperCase(b); casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); } else if (Character.isAlphabetic(b)) { curr+=Character.toLowerCase(b); casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); } } TreeNode ans= null; public TreeNode lcaDeepestLeaves(TreeNode root) { dfslcaDeepestLeaves(root , 0 ); return ans; } public void dfslcaDeepestLeaves(TreeNode root , int level) { if (root==null) { return; } if (depth(root.left)==depth(root.right)) { ans= root; return; } else if (depth(root.left)>depth(root.right)){ dfslcaDeepestLeaves(root.left, level+1); } else { dfslcaDeepestLeaves(root.right, level+1); } } public int depth(TreeNode root) { if (root==null) { return 0; } return 1+Math.max(depth(root.left), depth(root.right)); } TreeMap<Integer , Integer> m = new TreeMap<>(); public int maxLevelSum(TreeNode root) { int maxlevel =0; int mx = Integer.MIN_VALUE; dfsMaxLevelSum(root , 0); for (Map.Entry<Integer,Integer> e : m.entrySet()) { if (e.getValue()>mx) { mx=e.getValue(); maxlevel=e.getKey()+1; } } return maxlevel; } public void dfsMaxLevelSum(TreeNode root , int currLevel) { if (root==null) { return; } if (!m.containsKey(currLevel)) { m.put(currLevel, root.val); } else { m.put(currLevel, m.get(currLevel)+root.val); } dfsMaxLevelSum(root.left, currLevel+1); dfsMaxLevelSum(root.right, currLevel+1); } int teampPerf = 0; int teampSpeed = 0; public int maxPerformance(int n, int[] speed, int[] efficiency, int k) { int [][] map = new int[efficiency.length][2]; for (int i = 0; i < efficiency.length; i++) { map[i][0] = efficiency[i]; map[i][1] = speed[i]; } Arrays.sort(map , (e1 , e2 )-> (e2[0] - e1[0])); PriorityQueue<Integer> pq = new PriorityQueue<>(); calmax(map , speed , efficiency , pq , k); return teampPerf ; } public void calmax(int [][]map , int s[] , int e[] , PriorityQueue<Integer>pq , int k) { for (int i = 0 ; i<n ; i++) { if (pq.size()==k) { int lowestSpeed =pq.remove(); teampSpeed-=lowestSpeed; } pq.add(map[i][1]); teampSpeed+=map[i][1]; teampPerf=Math.max(teampPerf, teampSpeed*map[i][1]); } } int maxRob = 0; public int rob(int[] nums) { int one = 0; int two = 0; for (int i = 0; i < nums.length; i++) { if (i%2==0) { one+=nums[i]; } else { two+=nums[i]; } } return Math.max(one, two); } public int numberOfWeakCharacters(int[][] properties) { int c =0; Arrays.sort(properties, (a, b) -> (b[0] == a[0]) ? (a[1] - b[1]) : b[0] - a[0]); PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); pq.offer(0); for (int i = 0; i < properties.length; i++) { if (properties[i][1]<pq.peek()) { c++; } pq.offer(properties[i][1]); } return c; } public boolean canFinish(int numCourses, int[][] prerequisites) { boolean f = true ; TreeMap <Integer,Integer> map = new TreeMap<>(); for (int i = 0; i < prerequisites.length; i++) { if (map.get(prerequisites[i][1])!=null) { return false; } map.put(prerequisites[i][0], prerequisites[i][1]); } return f; } int n =0; char res[][] = new char [1000][1000]; public int countBattleships(char[][] board) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { res[i][j]= board[i][j]; } } for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (res[i][j]=='X') { int x = dfsB( i , j ,board , res); n++; } } } return n ; } public int dfsB(int i , int j , char [][]b , char [][]res) { if (i<0||i>=b.length|j<0||j>=b[0].length||res[i][j]=='.') { return 0; } if (res[i][j]=='X') { return 1+dfsB(i+1, j, b, res)+dfsB(i, j+1, b, res); } return 0; } List<Integer> l = new ArrayList<>(); public List<Integer> rightSideView(TreeNode root) { return dfsRightSideView(root); } public List<Integer> dfsRightSideView(TreeNode root) { if (root!=null) { l.add(root.val); } if (root==null) { return l; } if (root.right==null) { l.add(root.right.val); } dfsRightSideView(root.right); return l; } public void dfsSumNumber(TreeNode root ,int sum , int l ) { l=l*10 +root.val; if (root.left!=null) { dfsSumNumber(root.left,sum , l); } if (root.right!=null) { dfsSumNumber(root.right , sum, l); } if (root.left==null && root.right==null) { sum+=l; } // r=l*10+root.val; l-=root.val; l/=10; } //BFS SOLN public int[][] updateMatrix(int[][] mat) { int m =mat.length; int n = mat[0].length; Queue<int[]> q = new LinkedList<>(); for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[0].length; j++) { if (mat[i][j]==0) { q.offer(new int[] { i , j}); } else { mat[i][j] = Integer.MAX_VALUE; } } } int dir[][] = {{1 , 0} , {-1 , 0} , {0 , 1} , { 0 , -1}}; while (!q.isEmpty()) { int zeroPos[] = q.poll(); for (int[] d : dir) { int r = zeroPos[0]+d[0]; int c = zeroPos[1]+d[1]; if (r<0||r>=mat.length || c<0||c>=mat[0].length||mat[r][c]<=mat[zeroPos[0]][zeroPos[1]]+1) { continue; } q.add(new int[]{r,c}); mat[r][c] = mat[zeroPos[0]][zeroPos[1]]+1; } } return mat; } // DFS SOLN // public int[][] updateMatrix(int[][] mat) { // int res [][] = new int[mat.length][mat[0].length]; // for (int i = 0; i < mat.length; i++) { // for (int j = 0; j < mat.length; j++) { // if (mat[i][j]==0) { // Dis0(i, j, mat , res, 0); // } // } // } // return res; // } // public void Dis0(int i , int j , int [][]mat, int res[][] , int dist) { // if (i<0||i>=mat.length||j>=mat[0].length||j<0) { // return ; // } // if (dist==0 ||mat[i][j]==1&&(res[i][j]==0||res[i][j]>dist)) { // res[i][j]= dist; // Dis0(i+1, j, mat, res, dist+1); // Dis0(i-1, j, mat, res, dist+1); // Dis0(i, j+1, mat, res, dist+1); // Dis0(i, j-1, mat, res, dist+1); // } // // return dist; // } public int maxDepth(TreeNode root) { if (root==null) { return 0; } return maxD(root , 0); } public int maxD(TreeNode root , int d ) { if (root==null) { return d; } return Math.max(maxD(root.left, d+1), maxD(root.right, d+1)); } public int reverse(int x) { int sign=x>0?1:-1; //x=Math.abs(x); long s= 0; int r= 0; int n=x; while (n!=0) { r=n%10; s=s*10+r; n/=10; if (s>Integer.MAX_VALUE||s<Integer.MIN_VALUE) { return 0; } } return (int)s*sign; } public boolean checkInclusion(String s1, String s2) { boolean f = false ; if (s1.length()>s2.length()) { return false; } for (int i = 0; i < s2.length(); i++) { HashMap<Character ,Integer> c1 = new HashMap<>(); HashMap<Character ,Integer> c2 = new HashMap<>(); for (int j = 0; j < s1.length(); j++) { char ch2 = s2.charAt(i+j); char ch1 = s1.charAt(j); // if (c1.get(key)) { // } } } return f; } public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } public ListNode middleNode(ListNode head) { ListNode mid = head; ListNode last = head; int k =0; while (last.next!=null&&last.next.next!=null) { k++; mid= mid.next; last = last.next.next; } if (getLen(head)%2==0) { return mid.next; } return mid; } public int getLen(ListNode mid) { int l = 0; ListNode p = mid; while (p!=null) { l++; p=p.next; } return l; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } } public List<List<Integer>> verticalTraversal(TreeNode root) { TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map = new TreeMap<>(); dfs(root, 0, 0, map); List<List<Integer>> list = new ArrayList<>(); for (TreeMap<Integer, PriorityQueue<Integer>> ys : map.values()) { list.add(new ArrayList<>()); for (PriorityQueue<Integer> nodes : ys.values()) { while (!nodes.isEmpty()) { // list.get(list.size() - 1).add(nodes.poll()); } } } return list; } private void dfs(TreeNode root, int x, int y, TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map) { if (root == null) { return; } if (!map.containsKey(x)) { map.put(x, new TreeMap<>()); } if (!map.get(x).containsKey(y)) { map.get(x).put(y, new PriorityQueue<>()); } map.get(x).get(y).offer(root.val); dfs(root.left, x - 1, y + 1, map); dfs(root.right, x + 1, y + 1, map); } public long pow (int x ,int n ) { long y = 1; if (n==0) { return y; } while (n>0) { if (n%2!=0) { y=y*x; } x*=x; n/=2; } return y; } public long powrec (int x ,int n ) { long y = 1; if (n==0) { return y; } y = powrec(x, n/2); if (n%2==0) { return y*y; } return y*y*x; } public int fib(int n) { if (n==0||n==11) { return n; } return fib(n-1)+fib(n-2); } public long gcd(long a , long b) { if (b%a==0) { return a; } return gcd(b%a,a); } public int maximumElement(int a[]) { int x = Integer.MIN_VALUE; for (int i = 0; i < a.length; i++) { if (a[i]>x) { x=a[i]; } } return x; } public int minimumElement(int a[]) { int x = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) { if (a[i]<x) { x=a[i]; } } return x; } public boolean [] sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p]) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } // GFG arraylist function for referral public ArrayList create2DArrayList(int n ,int k) { // Creating a 2D ArrayList of Integer type ArrayList<ArrayList<Integer> > x = new ArrayList<ArrayList<Integer> >(); // One space allocated for R0 // Adding 3 to R0 created above x(R0, C0) //x.get(0).add(0, 3); int xr = 0; for (int i = 1; i <= n; i+=2) { if ((i+k)*(i+1)%4==0) { x.add(new ArrayList<Integer>()); x.get(xr).addAll(Arrays.asList(i , i+1)); } else { x.add(new ArrayList<Integer>()); x.get(xr).addAll(Arrays.asList(i+1 , i)); } xr++; } // Creating R1 and adding values // Note: Another way for adding values in 2D // collections // x.add( // new ArrayList<Integer>(Arrays.asList(3, 4, 6))); // // Adding 366 to x(R1, C0) // x.get(1).add(0, 366); // // Adding 576 to x(R1, C4) // x.get(1).add(4, 576); // // Now, adding values to R2 // x.add(2, new ArrayList<>(Arrays.asList(3, 84))); // // Adding values to R3 // x.add(new ArrayList<Integer>( // Arrays.asList(83, 6684, 776))); // // Adding values to R4 // x.add(new ArrayList<>(Arrays.asList(8))); // // Appending values to R4 // x.get(4).addAll(Arrays.asList(9, 10, 11)); // // Appending values to R1, but start appending from // // C3 // x.get(1).addAll(3, Arrays.asList(22, 1000)); // This method will return 2D array return x; } } class FlashFastReader { BufferedReader in; StringTokenizer token; public FlashFastReader(InputStream ins) { in=new BufferedReader(new InputStreamReader(ins)); token=new StringTokenizer(""); } public boolean hasNext() { while (!token.hasMoreTokens()) { try { String s = in.readLine(); if (s == null) return false; token = new StringTokenizer(s); } catch (IOException e) { throw new InputMismatchException(); } } return true; } public String next() { hasNext(); return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextIntsInputAsArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } public long nextLong() { return Long.parseLong(next()); } public long[] nextLongsInputAsArray(int n) { long [] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public String nextString() { return String.valueOf(next()); } public String[] nextStringsInputAsArray(int n) { String [] a = new String[n]; for (int i = 0; i < n; i++) a[i] = nextString(); return a; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
d6978590cb78d5889f5b06db04cf91f1
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.OutputStream; import java.io.BufferedOutputStream; import java.util.*; public class CodeForces_Edu_133 { public static void main(String[] args)throws Exception{ FastScanner in = new FastScanner();OutputStream out = new BufferedOutputStream ( System.out ); int t=in.nextInt(); while (t-->0){ int n=in.nextInt();int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=i+1; } out.write((n+"\n").getBytes()); for(int j=0;j<n;j++){ out.write((a[j]+" ").getBytes()); } out.write("\n".getBytes()); for(int i=1;i<n;i++){ int temp=a[i];a[i]=a[i-1];a[i-1]=temp; for(int j=0;j<n;j++){ out.write((a[j]+" ").getBytes()); } out.write("\n".getBytes()); } } out.flush(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
c8b192c74d3b0d1562aed6d760c27333
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class B { static PrintWriter out = new PrintWriter(System.out); static MyFastReaderB in = new MyFastReaderB(); static long mod = (long) (1e9 + 7); public static void main(String[] args) throws Exception { //Scanner sc=new Scanner(System.in); int test = i(); while (test-- > 0) { int n=i(); int[] arr=new int[n]; out.print(n+"\n"); for(int i=1;i<=n;i++) arr[i-1]=i; for(int p: arr) out.print(p+" "); out.print("\n"); for(int i=n-1;i>0;i--) { int x=arr[i]; arr[i]=arr[i-1]; arr[i-1]=x; for(int p: arr) out.print(p+" "); out.print("\n"); } out.flush(); } out.close(); } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } static void sort(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } static long pow(long a, long b) { long mod = 1000000007; long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) pow = (pow * x) % mod; x = (x * x) % mod; b /= 2; } return pow; } static long toggleBits(long x)// one's complement || Toggle bits { int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1; return ((1 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static boolean isPrime(long N) { if (N <= 1) return false; if (N <= 3) return true; if (N % 2 == 0 || N % 3 == 0) return false; for (int i = 5; i * i <= N; i = i + 6) if (N % i == 0 || N % (i + 2) == 0) return false; return true; } static long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } // Debugging Functions Starts static void print(char A[]) { for (char c : A) System.out.print(c + " "); System.out.println(); } static void print(boolean A[]) { for (boolean c : A) System.out.print(c + " "); System.out.println(); } static void print(int A[]) { for (int a : A) System.out.print(a + " "); System.out.println(); } static void print(long A[]) { for (long i : A) System.out.print(i + " "); System.out.println(); } static void print(ArrayList<Integer> A) { for (int a : A) System.out.print(a + " "); System.out.println(); } // Debugging Functions END // ---------------------- // IO FUNCTIONS STARTS static HashMap<Integer, Integer> getHashMap(int A[]) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } public static Map<Character, Integer> mapSortByValue(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getValue() - o2.getValue(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static String string() { return in.nextLine(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] arrI(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] arrL(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class MyFastReaderB { BufferedReader br; StringTokenizer st; public MyFastReaderB() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
f1573418fbb8b364293b8e5f17e864df
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.Scanner; public class PermutationChain { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tc= scanner.nextInt(); for (int i = 0; i<tc; i++){ int length = scanner.nextInt(); int [] array = new int[length]; System.out.println(length); for (int j = 1; j<=length; j++){ array[j-1]=j; System.out.print(j+ " "); } System.out.println(); StringBuilder first = new StringBuilder(); for (int j = 0; j<length-1; j++){ int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; System.out.print(first); first.append(array[j]).append(" "); for (int t = j; t<length; t++){ System.out.print(array[t]+ " "); } System.out.println(); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
e9e335308643161299fb0e6cd8e5ea43
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static Scanner scn = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = 1; t = scn.nextInt(); for(int tests = 0; tests < t; tests++) solve(); out.close(); } public static void solve() { int n = scn.nextInt(); out.println(n); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = (i + 1); printArray(a); swap(a, 0, 1); printArray(a); for(int i = 3; i <= n; i++) { swap(a, 0, i - 1); printArray(a); } } public static void printArray(int[] a) { for(int i = 0; i < a.length; i++) out.print(a[i] + " "); out.println(); } public static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
8be720f8a767226ad9e0e654ae61b052
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.util.regex.*; import java.io.BufferedReader; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; // sorting on the basis of count of set bits //Arrays.sort(arr, (a,b)-> Integer.compare(Integer.bitCount(b), Integer.bitCount(a))); // provide key to pq, it will store the key according to the frequency (asc/desc) // PriorityQueue <Integer> pq = new PriorityQueue <>((n1, n2) -> mp.get(n1)-mp.get(n2)); // Collections.sort(Arrays.asList(arr)); //char c[] = fs.next().toCharArray(); //int [] tmp = Arrays.copyOf(arr, n); // deep copy is done // ArrayList <Integer> al = new ArrayList<>(list); // this is also a deep copy i.e changes in 'al' won't reflect in 'list' // long ans = (long)a*(long)b; if a and b is of int type, otherwise overflow will occur // very careful with type conversion in java. // dont put 'long type' as index of the array,list or any Collections, only 'int' will work. /*----------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public class Main { static long mod = 1000000007; static double pi = 3.14159265358979323846; public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tc = fs.nextInt(); //int tc = 1; for(int i = 0; i < tc; i++) { solve(fs, out); } out.close(); } public static void solve(FastScanner fs, PrintWriter out) { int n = fs.nextInt(); int [] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = i+1; } out.println(n); int k = n-2; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { out.print(arr[j] + " "); } out.println(); if(k < 0) { break; } int tmp = arr[k]; arr[k] = arr[k+1]; arr[k+1] = tmp; k--; } } } class Pair { int fr; int sc; public Pair(int fr, int sc) { this.fr = fr; this.sc = sc; } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); // read only string and breaks if finds a space String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } // read a complete line of string including spaces String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
5f17211f9b24efd25a53b4826e400d27
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { //##############################FAST READER################################### static class FastReader{ BufferedReader br; StringTokenizer st; FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null||!st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); }catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } } //########################################################################### static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y){ this.x=x; this.y=y; } @Override public int compareTo(Pair o) { return this.x-o.x; } } //##############################USEFUL FUNCTIONS##################################### static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } static List primeFactors(int n){ List<Integer> ar=new ArrayList<>(); int ct=0; for(int i=2;i*i<=n;i++) { if(n%i==0) { ar.add(i); ct++; while(n%i==0) { n/=i; } } } if(n>1) { ct++; ar.add(n); } return ar; } static List primeFactors(long n){ List<Integer> ar=new ArrayList<>(); int ct=0; for(int i=2;i*i<=n;i++) { if(n%i==0) { ar.add(i); ct++; while(n%i==0) { n/=i; } } } if(n>1) { ct++; ar.add((int)n); } return ar; } static long powM(long x,long y,long mod) { if(y==0) return 1; long k=powM(x,y/2,mod); if(y%2==1) { return (((((long)1*k*k)%mod)*x)%mod); } else { return (((long)1*k*k)%mod); } } static int ub(ArrayList<Integer>ar,int k) { int s=0; int e=ar.size(); while(s!=e) { int mid=s+e>>1; if(ar.get(mid)<=k) s=mid+1; else e=mid; } if(s==ar.size()) return -1; return s; } static boolean isPrime(long n) { boolean flag=true; if(n<2) flag=false; else { for(int i=2;i*i<=n;i++){ if(n%i==0) { flag=false; break; } } } return flag; } static void print(int a[]) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } System.out.println(); } static void print(long a[]) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } System.out.println(); } static void print(int a[][]) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } static void print(long a[][]) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } static int[] read(int[] a,FastReader sc) { for(int i=0;i<a.length;i++) { a[i]=sc.nextInt(); } return a; } static long[] read(long[] a,FastReader sc) { for(int i=0;i<a.length;i++) { a[i]=sc.nextLong(); } return a; } static int[][] read(int [][] a,FastReader sc){ for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { a[i][j]=sc.nextInt(); } } return a; } static long[][] read(long [][] a,FastReader sc){ for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { a[i][j]=sc.nextLong(); } } return a; } static void printL(List<Integer>li) { for(int i=0;i<li.size();i++) { System.out.print(li.get(i)+" "); } System.out.println(); } static void print(List<Long>li) { for(int i=0;i<li.size();i++) { System.out.print(li.get(i)+" "); } System.out.println(); } static String sort(String s) { char ch[]=s.toCharArray(); Arrays.sort(ch); return new String(ch); } static boolean isPalindrome(long a) { String s=Long.toString(a); StringBuilder rev=new StringBuilder(s); rev.reverse(); String s1=rev.toString(); return s.equals(s1); } static int[] swap(int a[],int i,int j) { int temp=a[i]; a[i]=a[j]; a[j]=temp; return a; } static long[] swap(long a[],int i,int j) { long temp=a[i]; a[i]=a[j]; a[j]=temp; return a; } public static String addCharToString(String str, char c, int pos) { StringBuffer stringBuffer = new StringBuffer(str); stringBuffer.insert(pos, c); return stringBuffer.toString(); } static int BigMod(String num, int a) { int res = 0; for (int i = 0; i < num.length(); i++) res = (res * 10 + (int)num.charAt(i)); return res; } static boolean isSquare(int n) { for(int i=1;i*i<=n;i++) { if((i*i)==n) return true; } return false; } static long firstsetBitPower(long n) { int k = (int)(Math.log(n) / Math.log(2)); return 1 << k; } static boolean inrange(int key,int l,int r,int n) { boolean flag=false; if(key==0) { if(key+1>=l&&key+1<=r) { flag=true; } } else if(key==n+1) { if(key-1>=l&&key+1<=r) { flag=true; } } else { if(key>=l&&key<=r) { flag=true; } } return flag; } static long fact(int n) { long ans=1; for(int i=1;i<=n;i++) { ans*=i; } return ans; } //#############################MAIN FUNCTION################################## public static void main(String[] args) { try { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); System.out.println(n); for(int i=1;i<=n;i++) System.out.print(i+" "); System.out.println(); int tp=2; StringBuilder sb=new StringBuilder(""); for(int i=1;i<n;i++) { for(int j=0;j<tp;j++) { sb.append(((j+1)%tp)+1+" "); } for(int j=tp;j<n;j++) { sb.append((j+1)+" "); } tp++; sb.append("\n"); } System.out.println(sb); } }catch(Exception e) {return;} } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
23cd0f83f0c75094f159ff995bfc1ed4
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class HackerEarth { public static void main(String args[]){ InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static int mod = (int)1e9+7; static class Task{ public void solve(int T, InputReader in, PrintWriter out) { T = in.nextInt(); while (T-->0){ int N = in.nextInt(); out.println(N); int a[] = new int[N]; for (int i =1; i <= N; i++){ a[i-1] = i; } for (int i = 0; i < N-1; i++) { printAns(a, N, out); int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } for (int j = 0; j < N; j++){ out.print(a[j] + " "); } out.println(); } } private void printAns(int a[], int N, PrintWriter out) { for (int j = 0; j < N; j++){ out.print(a[j] + " "); } out.println(); } } static class InputReader { BufferedReader br; StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
28348988d335d443c8e6011cecf54f87
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static AReader scan = new AReader(); static int N = 110; static int[] a = new int[N]; static void solve() { int n = scan.nextInt(); for(int i = 1;i<=n;i++) a[i] = i; int k = 1; StringBuffer sb = new StringBuffer(); sb.append(n).append("\n"); while(k<=n){ for(int i = 1;i<=n;i++) sb.append(a[i]+" "); sb.append("\n"); int t = a[k]; a[k] = a[k+1]; a[k+1] = t; k++; } System.out.print(sb.toString()); } public static void main(String[] args) { int T = scan.nextInt(); while (T-- > 0) { solve(); } } } class Pair{ int x,y; public Pair(int x, int y) { this.x = x; this.y = y; } } class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
215588ff7a89362f5e357d7cc8d5ebf0
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.PrintWriter; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = 0; int[] arr = new int[n]; for (int i = 1; i <= n; i++) { arr[i - 1] = i; } out.println(n); for (int i = 0; i < n; i++) { out.print(arr[i] + " "); } out.println(); for (int i = 1; i < n; i++) { int temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; for (int j = 0; j < n; j++) { out.print(arr[j] + " "); } out.println(); } } out.flush(); out.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
0b2ac89c319200e0b280a60b8ac0bec5
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Main { static StringBuilder sb; static dsu dsu; static long fact[]; static int mod = (int) (1e9 + 7); static void build(int node, int start, int end,int[]tree,int[]arr) { if (start == end) { tree[node] = arr[start]; } else { int mid = (start + end) / 2; int left = node * 2; int right = node * 2 + 1; build(left, start, mid,tree,arr); build(right, mid + 1, end,tree,arr); tree[node] = Math.max(tree[left], tree[right]); } } static int query(int node, int start, int end, int l, int r,int[]tree,int[]arr) { if (end < l || r < start)return Integer.MIN_VALUE; if (start == end) { return tree[node]; } else if (l <= start && end <= r) { return tree[node]; } else { int mid = (start + end) / 2; int left = query(node * 2, start, mid, l, r,tree,arr); int right = query(node * 2 + 1, mid + 1, end, l, r,tree,arr); return Math.max(left, right); } } static void swap(int[]arr,int i,int j){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static void solve() { int n=i(); int[]arr=new int[n+1]; sb.append(n+"\n"); for(int i=1;i<=n;i++){ sb.append(i+" "); arr[i]=i; } sb.append("\n"); for(int i=2;i<=n;i++){ swap(arr,i,i-1); for(int j=1;j<=n;j++){ sb.append(arr[j]+" "); } sb.append("\n"); } } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); HashMap<Integer,Integer> hm=new HashMap<>(); // System.out.println(hm); // o n e t w fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++) { fact[i]=((long)(i%mod)*(long)(fact[i-1]%mod))%mod; } int cnt=1; while (test-- > 0) { solve(); cnt++; } System.out.println(sb); } //**************NCR%P****************** static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) ; y--; } x = (x * x); y = y / 2; } return res; } //**************END****************** // *************Disjoint set // union*********// static class dsu { int parent[]; dsu(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = i; } int find(int a) { if (parent[a] ==a) return a; else { int x = find(parent[a]); parent[a] = x; return x; } } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; parent[b] = a; } } //**************PRIME FACTORIZE **********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } //****CLASS PAIR ************************************************ static class Pair implements Comparable<Pair> { int src; long a; long b; Pair(int src, long a,long b) { this.src = src; this.a = a; this.b = b; } public int compareTo(Pair o) { return (int) (this.b - o.b); } } //****CLASS PAIR ************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static char[] sort(char[] a2) { int n = a2.length; ArrayList<Character> l = new ArrayList<>(); for (char i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArrayi(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
90372ff567ed4e683938811e93ef48c6
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class Main { public static FastReader sc; public static PrintWriter out; static class FastReader{ static BufferedReader br; static StringTokenizer str; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } static String next(){ while (str == null || !str.hasMoreElements()){ try{ str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation){ lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } static int nextInt(){ return Integer.parseInt(next()); } static long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException lastMonthOfVacation){ lastMonthOfVacation.printStackTrace(); } return str; } } public static void main(String[] args) throws IOException{ sc = new FastReader(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); for (int i = 0; i < t; i++){ int n = sc.nextInt(); out.println(n); int arr[] = new int[n+1]; for (int j = 1; j< n + 1; j++){ arr[j] = j; out.print(arr[j]+" "); } out.println(); for(int k = 1; k<n ; k++){ int temp = arr[k]; arr[k] = arr[k+1]; arr[k+1] = temp; for(int p =1 ; p<=n ; p++) out.print(arr[p]+" "); out.println(); } } out.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
4c66bb282fe6f2f9a37cf210c5e369ba
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { static int INF = (int) 1e9 + 7, MOD = 998244353; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 1; t <= test; t++) { solve(t); } out.close(); } private static void solve(int t) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = i + 1; } out.println(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { out.print(arr[j] + " "); } out.println(); if (i + 1 < n) { int temp = arr[i + 1]; arr[i + 1] = arr[i]; arr[i] = temp; } } } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } return str; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
39c5d38c429459a6442cdac3a72ea623
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; //import java.math.BigInteger; import java.util.*; public class Rough { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = s.nextInt(); for (int t = 1; t <= tc; t++) { int n=s.nextInt(); int ar[]=new int[n]; for(int i=0;i<n;i++)ar[i]=i+1; int l1=n-1; int l2=n-2; pw.println(n); while(n-->0) { for(int x:ar)pw.print(x+" "); pw.println(); int temp=ar[l1]; ar[l1]=ar[l2]; ar[l2]=temp; if(l1>0)l1--; if(l2>0)l2--; } } pw.close(); } static boolean ispalin(String in) { StringBuilder sb = new StringBuilder(in); if (sb.reverse().toString().equals(in)) return true; else return false; } static void maping(String key, int val, HashMap<String, Integer> hm) { if (hm.containsKey(key)) { int valu = hm.get(key) + 1; hm.put(key, valu); } else hm.put(key, val); } static final Random ran = new Random(); static void sort(int[] ar) { int n = ar.length; for (int i = 0; i < n; i++) { int doer = ran.nextInt(n), temp = ar[doer]; ar[doer] = ar[i]; ar[i] = temp; } Arrays.sort(ar); } static void sort(long[] ar) { int n = ar.length; for (int i = 0; i < n; i++) { int doer = ran.nextInt(n); long temp = ar[doer]; ar[doer] = ar[i]; ar[i] = temp; } Arrays.sort(ar); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
f8c331227124a3109a561dd42fd5d7d7
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class JavaApplication { static BufferedReader in; static BufferedWriter out; static StringTokenizer st; static String token; String getLine() throws IOException { return in.readLine(); } String getToken() throws IOException { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(getLine(), ",[] "); return st.nextToken(); } int getInt() throws IOException { return Integer.parseInt(getToken()); } long getLong() throws IOException { return Long.parseLong(getToken()); } char getChar() throws IOException { if (token == null || token.length() == 0) token = getToken(); char r = token.charAt(0); token = token.substring(1); return r; } static void print(Object s) { try { out.write(String.valueOf(s)); } catch (Exception ignored) { } } static void println() { try { out.newLine(); } catch (Exception ignored) { } } static void println(Object s) { try { out.write(String.valueOf(s)); out.newLine(); } catch (Exception ignored) { } } public void Solve() throws IOException { int t=getInt(); while(t-->0){ int n=getInt(); int[] temp=new int[n]; println(n); for(int i=1;i<=n;i++){ print(i+" "); temp[i-1]=i; } println(); int a=0; for(int i=1;i<n;i++) { a=temp[i-1]; temp[i-1]=temp[i]; temp[i]=a; for (int j=0;j<n;j++) print(temp[j]+" "); println(); } } } public static void main(String[] args) throws java.lang.Exception { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); new JavaApplication().Solve(); out.flush(); return; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
44da367657528a737263615a1a17b41b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; public class PermutationChain { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); bw.write(n + "\n"); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i + 1; } bw.write(getString(a)); for (int i = 0; i < n - 1; i++) { a[i] = a[i + 1]; a[i + 1] = 1; bw.write(getString(a)); } } br.close(); bw.close(); } private static String getString(int[] a) { StringBuilder sb = new StringBuilder(); sb.append(a[0]); for (int i = 1; i < a.length; i++) { sb.append(" ").append(a[i]); } sb.append("\n"); return sb.toString(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
ba21712bac7906bb5fcd5f35c4018e27
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Solution{ public static void main(String args[]){ Scanner in = new Scanner(System.in); long test = in.nextLong(); while(test-- > 0){ int n = in.nextInt(); int[] ans = new int[n]; for(int i=0; i<n; i++){ ans[i]=i+1; } System.out.println(n); System.out.println(Arrays.toString(ans).substring(1).replace("]", "").replace("[", "").replace(", ", " ")); for(int i=1; i<n; i++){ int temp1 = ans[i]; ans[i]=ans[i-1]; ans[i-1]=temp1; System.out.println(Arrays.toString(ans).substring(1).replace("]", "").replace("[", "").replace(", ", " ")); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
934edd88b37f0c7605614cc0e14a4277
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int A = scn.nextInt(); StringBuilder sb = new StringBuilder(""); while(A-- > 0) { int n = scn.nextInt(); StringBuilder str = new StringBuilder(""); int[] arr = new int[n]; for(int i=0 ; i<n ; i++) arr[i] = i + 1; for(int j=n-1 ; j>0 ; j--) { for(int i=0 ; i<n ; i++) { str.append(arr[i] + " "); } str.append("\n"); int temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; } for(int i=0 ; i<n ; i++) str.append(arr[i] + " "); str.append("\n"); sb.append(n + "\n" + str); } System.out.println(sb); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
28cf3eda48e02f5528f9480595e09ceb
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.text.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++)arr[i] = i+1; writer.println(n); int point = 0; int temp = n; while(temp-->0) { for(int i = 0; i < n; i++)writer.print(arr[i] + " "); writer.println(); if(temp != 0) { arr = swap(arr, point); point++; } } } writer.flush(); writer.close(); } public static boolean areEqual(ArrayList<Integer> arr1, ArrayList<Integer>arr2) { for(int i = 0; i < arr1.size(); i++)if(arr1.get(i) != arr2.get(i))return false; return true; } private static int [] swap(int arr[], int j) { int compl = j+1; int ele = arr[j]; arr[j] = arr[compl]; arr[compl] = ele; return arr; } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[(int)a] = find(arr[(int)a], arr); return arr[(int)a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static int gcd(int a, int b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class SegmentTree{ int size; long arr[]; SegmentTree(int n){ size = 1; while(size<n)size*=2; arr = new long[size*2]; } public void build(int input[]) { build(input, 0,0, size); } public void set(int i, int v) { set(i,v,0,0,size); } // sum from l + r-1 public long sum(int l, int r) { return sum(l,r,0,0,size); } private void build(int input[], int x, int lx, int rx) { if(rx-lx==1) { if(lx < input.length ) arr[x] = 1; return; } int mid = (lx+rx)/2; build(input, 2*x+1, lx, mid); build(input,2*x+2,mid,rx); arr[x] = arr[2*x+1] + arr[2*x+2]; } private void set(int i, int v, int x, int lx, int rx) { if(rx-lx==1) { arr[x] = v; return; } int mid = (lx+rx)/2; if(i < mid) set(i, v, 2*x+1, lx, mid); else set(i,v,2*x+2,mid,rx); arr[x] = arr[2*x+1] + arr[2*x+2]; } private long sum(int l, int r, int x, int lx, int rx) { if(lx>=r || rx <= l)return 0; if(lx>=l && rx <= r)return arr[x]; int mid = (lx+rx)/2; long s1 = sum(l,r,2*x+1,lx,mid); long s2 = sum(l,r,2*x+2,mid,rx); return s2+s1; } // int first_above(int v, int l, int x, int lx, int rx) { // if(arr[x].a<v)return -1; // if(rx<=l)return -1; // if(rx-lx==1)return lx; // int m = (lx+rx)/2; // int res = first_above(v,l,2*x+1,lx,m); // if(res==-1)res = first_above(v,l,2*x+2,m,rx); // return res; // // } } class Trie{ Trie arr[] = new Trie[26]; boolean isEnd; Trie(){ for(int i = 0; i < 26; i++)arr[i] = null; isEnd = false; } } class Pair implements Comparable<Pair>{ long a; long b; long c; Pair(long a, long b, long c){ this.a = a; this.b = b; this.c = c; } Pair(long a, long b){ this.a = a; this.b = b; this.c = 0; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b && pair.c == this.c); } @Override public int compareTo(Pair o) { if(o.a != this.a) return Long.compare(this.a, o.a); else return Long.compare(this.b, o.b); } @Override public String toString() { return this.a + " " + this.b; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
9c29578190549ba300afbb7091333a5e
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main (String[] args) throws java.lang.Exception { FastReader scn = new FastReader(); int t =scn.nextInt(); while(t-- > 0){ int n = scn.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = i+1; } System.out.println(n); System.out.println(print(a)); for(int j=0; j<n-1; j++){ swap(a,j,j+1); System.out.println(print(a)); } } } static String print(int arr[]){ StringBuilder sb=new StringBuilder(); for(int num:arr) sb.append(num+" "); return sb.toString(); } static void swap(int[] a, int i,int j){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
40de54bef8d58a00b4153116018cec93
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class B_PermutationChain { public final static MyScanner sc = new MyScanner(); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void swap(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } public static void printArray(int[] arr, int a, int b) { String ans = ""; for (int i = a; i < b; i++) { // System.out.print(arr[i] + " "); ans += arr[i] + " "; } System.out.println(ans); } public static void solve() { int n = sc.nextInt(); System.out.println(n); int[] arr = new int[n+2]; for (int i = 1; i <= n; i++) { arr[i] = i; } for (int i = 1; i <= n; i++) { printArray(arr, 1, n+1); swap(arr, i, i+1); } } public static void main(String[] args) { int T = sc.nextInt(); for (int t = 0; t < T; t++) { solve(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
e04f7d17ff684a830445e7e506f6ab0b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class ProbB { public static void main(String[] args) { FastReader in = new FastReader(); int numOfTestCases = in.nextInt(); for(int w = 0; w < numOfTestCases; w++) { int n = in.nextInt(); System.out.println(n); ArrayList<Integer> p = new ArrayList<Integer>(); for(int i = 1; i <= n; i++) { p.add(i); } print(p); int c = 1; for(int i = 0; i < n - 1; i++) { int temp = p.get(i + 1); p.set(i + 1, p.get(i)); p.set(i, temp); c++; print(p); } } } public static void print(ArrayList<Integer> p) { String sp = p.toString(); sp = sp.replace(",", ""); sp = sp.replace("[", ""); sp = sp.replace("]", ""); System.out.println(sp); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } int[] intArray(int n){ int[] arr = new int[n]; for(int i =0; i < n; i ++){ arr[i] = nextInt(); } return arr; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
e8a4374e1140f3b5b37864800d414e3b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.lang.Math; import java.io.*; import java.util.Arrays; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); //StringTokenizer st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(br.readLine()); for (int i = 0; i < a; i++) { int k = Integer.parseInt(br.readLine()); pw.println(k); int[] arr_of_nums = new int[k]; for (int j = 0; j < k; j++) { arr_of_nums[j] = j+1; } pw.print(arr_of_nums[0]); for (int j = 1; j < k; j++) { pw.print(" " + arr_of_nums[j]); } pw.print("\n"); int temp = arr_of_nums[0]; arr_of_nums[0] = arr_of_nums[1]; arr_of_nums[1] = temp; int fixedness = k - 2; int index = 2; pw.print(arr_of_nums[0]); for (int j = 1; j < k; j++) { pw.print(" " + arr_of_nums[j]); } pw.print("\n"); while (fixedness != 0) { arr_of_nums[index-1] = arr_of_nums[index]; arr_of_nums[index] = temp; fixedness -=1; index++; for (int num : arr_of_nums) { pw.print(num + " "); } pw.print("\n"); } } pw.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
8663749fa0925e1f7f3ae2faa27b4618
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main(String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(System.out); FastScanner sc = new FastScanner(); int tater = sc.nextInt(); while (tater-- > 0) { int n = sc.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = i+1; } out.println(n); int x = n; int j = 0; while(x-->0){ for (int i = 0 ; i < a.length ; i++){ out.print(a[i] + " "); } out.println(); if (x!=0){ int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } j++; } } out.flush(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
692fca65cfb8941820b0d47188446e88
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Main { static class Pair { Pair() { } } public static void main(String[] args) { takeInput(); Scanner scn = new Scanner(System.in); int t = 1; t = scn.nextInt(); while (t-- > 0) { solve(scn); } } public static void solve(Scanner scn) { int n = scn.nextInt(); System.out.println(n); StringBuilder ans = new StringBuilder(); for (int i = n - 1; i >= 0; i--) { int pos = i; int num = 1; while (pos-- > 0) { ans.append(num + " "); num++; } ans.append(n + " "); while (num <= n - 1) { ans.append(num + " "); num++; } if (i > 0)ans.append("\n"); } System.out.println(ans); } public static void takeInput() { try { System.setIn(new FileInputStream("input1.txt")); System.setOut(new PrintStream(new FileOutputStream("output1.txt"))); } catch (Exception e) { System.err.println("Error"); } } private static boolean isPalindrome(String s) { int l = 0, r = s.length() - 1; while (l < r) { if (s.charAt(l) != s.charAt(r)) { return false; } l++; r--; } return true; } public static void swap(int x, int y) { x = x ^ y ^ (y = x); //x = x ^ y ^ (y = x); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
ca7e2313e17ba5678e0785ea1ad87c7c
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class codeforces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void help(int[] arr, int k){ int temp = arr[k]; arr[k] =arr[k-1]; arr[k-1] = temp; } public static void add(int[] arr,StringBuilder st ){ for(int i : arr)st.append(i).append(' '); st.append('\n'); } public static void main(String[] args) { FastReader in = new FastReader(); int t =in.nextInt(); StringBuilder st = new StringBuilder(); while (t>0){ int n =in.nextInt(); int[] arr = new int[n]; st.append(n).append('\n'); for(int i=0;i<n;i++)arr[i] = i+1; add(arr,st); int k = n-1; for(int i=1;i<n;i++){ help(arr,k); k--; add(arr,st); } t--; } System.out.println(st); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
372730d72cad449487a9d823dc1e3f85
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Sample { public static void main(String[] args) { //diptiman code //say no to plagarism //i dont share code nor take from anyone Scanner xy=new Scanner(System.in); StringBuilder ans=new StringBuilder(); //say no to plagarism //diptiman code //say no to plagarism //i dont share code nor take from anyone int tc=xy.nextInt(); while(tc>0) { tc--; int n=xy.nextInt(); int A[]=new int[n]; for(int i=1;i<=n;i++) A[i-1]=i; int cap=0; ans.append(n).append("\n"); while(cap<n-1) { for(int i=0;i<n;i++) ans.append(A[i]).append(" "); ans.append("\n"); int tmp=A[cap]; A[cap]=A[cap+1]; A[cap+1]=tmp; cap++; } for(int i=0;i<n;i++) ans.append(A[i]).append(" "); ans.append("\n"); } System.out.print(ans); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
7db53620d9af8294be9985b01e20f3ef
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { Reader r = new Reader(); int t = r.tN(); while(t-->0){ //solve(r); //solveB(r); solveC(r); //solveD(r); } } public static void solve(Reader r) throws Exception{ int n = r.tN(); // int[] arr = r.tIA(); if(n==1){ System.out.println(2); return; } if(n==2){ System.out.println(1); return; } int ans = n/3; if(n%3!=0) ans++; System.out.println(ans); } public static void solveB(Reader r) throws Exception{ int n = r.tN(); int[] arr = r.tIA(); } public static void solveC(Reader r) throws Exception{ int n = r.tN(); //int[] arr = r.tIA(); System.out.println(n); for(int i = 1; i<=n; i++){ int count = 2; StringBuilder sb = new StringBuilder(); for(int j = 1; j<=n; j++){ if(j==i){ sb.append(1+" "); } else{ sb.append(count+" "); count++; } } System.out.println(sb); } } public static void solveD(Reader r) throws Exception{ int n= r.tN(); } public static class Graph{ ArrayList<Integer>[] graph; int nodeCount; int edgesCount; int[] nodeValue; //boolean[] visited = new boolean[n+1]; public Graph(int n){ nodeCount = n; graph = new ArrayList[n+1]; initGraph(); } public void setNodeValue(int[] arr){ nodeValue = new int[nodeCount+1]; for(int i = 1; i<=nodeCount; i++){ nodeValue[i] = arr[i-1]; } } private void initGraph(){ for(int i = 0; i<=nodeCount; i++){ graph[i] = new ArrayList<>(); } } public void addOrientedEdge(int u, int v){ graph[u].add(v); } public void addEdge(int u, int v){ graph[u].add(v); graph[v].add(u); } } public static class myArray{ public static void swap(int[] arr, int i, int j){ int temp = arr[i] ; arr[i] = arr[j]; arr[j] = temp; } public static void reverseArr(int[] arr){ int i = 0; int j = arr.length-1; while(i<j){ swap(arr,i,j); i++; j--; } } } public static class SegmentTree{ int[] tree; int count; public SegmentTree(int[] arr){ count = arr.length; tree = new int[4*count]; build(arr,0,count-1,1); } public SegmentTree(int n){ count = n; int[] arr = new int[n]; tree = new int[n*4]; build(arr,0,count-1,1); } public void build(int[] arr, int s, int e, int node){ if(s==e){ tree[node] = arr[s]; return; } int mid = s+(e-s)/2; build(arr,s,mid,node*2); build(arr,mid+1,e,node*2+1); tree[node]= tree[node*2]+tree[node*2+1]; } void update(int pos ,int val){ update(1,0,count-1,pos,val); } private void update(int node, int s , int e , int pos, int val){ if(e<pos||s>pos) return; if(s==e && e==pos){ // System.out.println("Updating "+ s + " to " + e + " : " + val); tree[node] = val; return; } int mid = s+(e-s)/2; update(node*2,s,mid,pos,val); update(node*2+1,mid+1,e,pos,val); tree[node]= tree[node*2]+tree[node*2+1]; //System.out.println("Updating "+ s + " to " + e + " : " + tree[node]); } int get(int l , int r){ return get(1,0,count-1,l,r); } private int get(int node , int s , int e, int l , int r){ if(e<l||s>r) return 0; if(s==e ||(s>=l && e<=r)){ //System.out.println("returning sum from " + s + " to " +e " : " + tree[node]); return tree[node]; } int mid = s + (e-s)/2; int left = get(node*2,s,mid,l,r); int right = get(node*2+1,mid+1,e,l,r); return left + right; } } public static class Pair{ long val; int sval; public Pair(long v , int i){ this.val = v; this.sval = i; } } public static class Reader { BufferedReader br; public Reader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public int tN() throws Exception { return Integer.parseInt(takeString()); } public long tL() throws Exception { return Long.parseLong(takeString()); } public int[] tIA() throws Exception { String[] arr = tSA(); int[] vals = convertStringIntoArray(arr); return vals; } public long[] tLA() throws Exception{ String[] arr = br.readLine().split(" "); long[] res = new long[arr.length]; for(int i = 0; i<arr.length; i++){ res[i] = Long.parseLong(arr[i]); } return res; } public String takeString() throws Exception{ return br.readLine(); } public String[] tSA() throws Exception{ return takeString().split(" "); } public static int[] convertStringIntoArray(String[] nums){ int[] arr = new int[nums.length]; for(int i =0; i<nums.length; i++){ arr[i] = Integer.parseInt(nums[i]); } return arr; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
abcd1f785b2321c5c2108e9f267e6054
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class d3 { static class Node{ int left; int right; public Node(int left,int right){ this.left = left; this.right = right; } } static class pair1{ int number; int nothappy; int size ; pair1(int a,int b,int c){ this.number = a; this.nothappy = b; this.size = c; } } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while(t-->0){ st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[] arr = new int[n]; bw.write(n+"\n"); for(int i = 0;i<n;i++){ arr[i] = i+1; bw.write(arr[i]+" "); } bw.write("\n"); arr[1] = 1; arr[0] = 2; for(int i = 1;i<n-1;i++){ for(int j = 0;j<n;j++){ bw.write(arr[j]+" "); } bw.write("\n"); int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } // int temp = arr[n-2]; // arr[n-2] = arr[n-1]; // arr[n-1] = temp; for(int j = 0;j<n;j++){ bw.write(arr[j]+" "); } bw.write("\n"); } bw.flush(); } public static void fill(int x,int l,int r,long[] arr,long[] seg){ if(l == r){ seg[x] = arr[l]; //leaf element return; } int mid = (l+r)/2; fill(2*x,l,mid,arr,seg); fill(2*x+1,mid+1,r,arr,seg); seg[x] =seg[2*x]+seg[2*x+1]; } public static long sum(int x,int l,int r,int lr,int rr,long[] arr,long[] seg){ //completely outside if(r < lr || rr < l){ return 0; } // lr.....l....r......rr if(lr <= l && r <= rr){ return seg[x]; } int mid = (l+r)/2; long a = 0; a += sum(2*x,l,mid,lr,rr,arr,seg); a += sum(2*x+1,mid+1,r,lr,rr,arr,seg); return a; } public static void update(int x,int l,int r,int i,int val,long[] arr,long seg[]){ if(l == r){ seg[x] = val; return; } int mid = (l+r)/2; if(l <= i && i <= mid){ update(2*x,l,mid,i,val,arr,seg); } else{ update(2*x+1,mid+1,r,i,val,arr,seg); } seg[x] = (seg[2*x]+seg[2*x+1]); } // public static void mergesort(int[] arr,int low,int high){ // if(low == high){ // return; // } // int mid = (low+high)/2; // mergesort(arr, low, mid); // mergesort(arr, mid+1, high); // merge(arr,low,high); // } // public static void merge(int[] arr,int low,int high){ // int mid = (low+high)/2; // int p1 = low; // int p2 = mid+1; // int k = 0; // int[] temp = new int[high-low+1]; // while(p1 <= mid && p2 <= high){ // if(arr[p1] <= arr[p2]){ // temp[k++] = arr[p1++]; // } // else{ // temp[k++] = arr[p2++]; // count += (mid-p1+1); // } // } // while(p1 <= mid){ // temp[k++] = arr[p1++]; // } // while(p2 <= high){ // temp[k++] = arr[p2++]; // } // for(int i = 0;i<temp.length;i++){ // arr[low+i] = temp[i]; // } // } public static boolean check2(long mid,long n,long r,long c){ long noofrows = mid/r; long percolumn = mid/c; long cur = noofrows*percolumn; if(cur >= n){ return true; } return false; } public static int ceil1(int[] arr,int low,int high,int ele){ while(low <= high){ int mid = (low+high)/2; if(arr[mid] < ele){ low = mid+1; } else{ high = mid-1; } } return low; } public static int floor1(int[] arr,int low,int high,int ele){ while(low <= high){ int mid = (low+high)/2; if(arr[mid] <= ele){ low = mid+1; } else{ high = mid-1; } } return low; } public static int dfs(int[] arr,int i,int[] ans,boolean[] visited){ if(i >= arr.length){ return 0; } if(visited[i]){ return ans[i]; } visited[i] = true; ans[i] += arr[i]+dfs(arr,i+arr[i],ans,visited); return ans[i]; } // public static int dfs(ArrayList<ArrayList<Integer>> graph,int i,boolean[] visited){ // if(!visited[i]){ // visited[i]=true; // for(int child : graph.get(i)){ // if(!visited[child]){ // return dfs(graph, child, visited); // } // else{ // return child; // } // } // } // else{ // return i; // } // return 0; // } public static boolean recur(int i,int[] arr){ if(i < 0){ return true; } if(arr[i] != arr[arr.length-1-i]){ return false; } if(recur(i-1,arr)){ return true; }else{ return false; } } public static long power(long a,long b,long mod){ long res = 1L; while(b > 0){ if(b%2 == 1){ res = (res%mod * a%mod)%mod; } b /= 2; a = (a*a)%mod; } return res; } public static boolean comp(String s1,String s2){ int count = 0; for(int i = 0;i<2;i++){ if(s1.charAt(i) == s2.charAt(1-i)){ count++; } else{ return false; } } // System.out.println(s1+" "+s2); return true; } public static boolean palin1(String s){ int n = s.length()-1; int mid = (n+1)/2; for(int i = 0;i<mid;i++){ if(s.charAt(i) != s.charAt(n-i)){ return false; } } return true; } static class pair{ int a; int b; pair(int a,int b){ this.a = a; this.b = b; } } public static boolean checksum(int n){ int temp = 0; while(n > 0){ temp += n%10; n /= 10; } return (temp == 10)?true:false; } public static int ceil(ArrayList<Long> al,long val){ int low = 0; int high = al.size()-1; int pos = 0; while(low <= high){ int mid = (low+high)/2; if(al.get(mid) == val){ pos = Math.max(pos,mid); low = mid+1; } else if(al.get(mid) < val){ low = mid+1; } else{ high = mid-1; } } return pos; } public static int floor(ArrayList<Long> al,long val){ int low = 0; int high = al.size()-1; int pos = high; while(low <= high){ int mid = (low+high)/2; if(al.get(mid) == val){ pos = Math.min(pos,mid); high = mid-1; } else if(al.get(mid) < val){ low = mid+1; } else{ high = mid-1; } } return pos; } public static boolean palin(String s){ for(int i = 0;i<s.length()/2;i++){ if(s.charAt(i) == s.charAt(s.length()-i-1)){ continue; } else{ return false; } } return true; } public static long gcd(long a,long b){ if(b == 0L){ return a; } return gcd(b,a%b); } public static long fin(long n){ return ((long)n)*(n+1)/2; } public static int bceil(ArrayList<Integer> al,int val,int low,int high){ int index = 0; while(low <= high){ int mid = low + (high-low)/2; if(al.get(mid) <= val){ index = Math.max(mid,index); low = mid+1; } else{ high = mid-1; } } return index; } public static int bfloor(ArrayList<Integer> al,int val){ int low = 0; int high = al.size()-1; int index = -1; while(low <= high){ int mid = low + (high-low)/2; int mval = al.get(mid); if(mval >= val){ high = mid-1; index = Math.max(index,mid); } else{ low = mid+1; } } return index; } public static void primefact(int n){ for(int i = 2;i*i<=n;i++){ if(n%i == 0){ while(n%i == 0){ n /= i; System.out.println(i); } } } System.out.println(n); } public static boolean prime(long n){ int count = 0; for(long i = 1;i*i <= n;i++){ if(n%i == 0){ if(i*i == n){ count++; } else{ count += 2; } } } if(count == 2){ return true; } return false; } public static long fact1(int n){ long ans = 1L; for(int i = 2;i<=n;i++){ ans *= i; } return ans; } public static String reverse(String s){ StringBuilder sb = new StringBuilder(s); return sb.reverse().toString(); } public static int find(int n){ return n*(n+1)/2; } public static double calc(int x1,int x2,int y1,int y2){ return Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
89049776b03f3ed4f1cbf0b3c71ee5e2
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Objects; import java.util.StringTokenizer; import java.util.stream.Collectors; import java.util.stream.IntStream; public class B_PermutationsSeq { public static void main(String[] args) throws IOException { BufferedScanner input = new BufferedScanner(); BufferedOutput output = new BufferedOutput(); int tasks = input.nextInt(); while (tasks-- > 0) { int size = input.nextInt(); List<Integer> permutation = IntStream.range(1, size + 1).boxed().collect(Collectors.toList()); output.println(size); output.println(permutation.stream().map(Objects::toString).collect(Collectors.joining(" "))); for (int i = size - 1; i > 0; i--) { permutation.set(i, permutation.get(0)); permutation.set(0, i + 1); output.println(permutation.stream().map(Objects::toString).collect(Collectors.joining(" "))); } } output.flush(); } private static class BufferedScanner { private final BufferedReader reader; private StringTokenizer tokenizer; public BufferedScanner() { reader = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public String nextLine() throws IOException { tokenizer = null; return reader.readLine(); } private String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } } private static class BufferedOutput { private final StringBuilder result; public BufferedOutput() { result = new StringBuilder(); } public void print(Object object) { result.append(object); } public void println(Object object) { result.append(object).append("\n"); } public void flush() { System.out.println(result); System.out.flush(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
20b038fd6970105dcd1fea4e06f584f6
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
//<———My cp———— //https://takeuforward.org/interview-experience/strivers-cp-sheet/?utm_source=youtube&utm_medium=striver&utm_campaign=yt_video import java.util.*; import java.io.*; public class B_Permutation_Chain{ static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception{ FastReader fr = new FastReader(System.in); int t = fr.nextInt(); while(t-->0){ int n = fr.nextInt(); int[] vals = new int[n]; for(int i = 0;i<vals.length;i++){ vals[i]=i+1; } pw.println(n); for(int i = 0;i<n;i++){ pw.print(vals[i]+" "); } pw.println(""); for(int i =n-1;i>0;i--){ int temp = vals[i-1]; vals[i-1]=vals[i]; vals[i]=temp; for(int k = 0;k<n;k++){ pw.print(vals[k]+" "); } pw.println(""); } } pw.close();; } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static void reverseArray(long[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ long temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } public static void reverseArray(int[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ int temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
1f947186c855cc48ae989cbf78179816
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tests = in.nextInt(); for (int i = 0;i < tests;i++) { solve (in, out); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } // main code ? static void solve (FastScanner in, PrintWriter out) { int n = in.nextInt(), arr[] = new int[n]; out.println(n); for (int i = 1;i <= n;i++) arr[i-1] = i; for (int i = 0;i < n;i++) { for (int j = 0;j < n;j++) { out.print(arr[j] + " "); } if (i < n-1) { int t = arr[i+1]; arr[i+1] = arr[i]; arr[i] = t; } out.println(); } out.flush(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
9ec623f10fa282a3e6c214a76691f5fb
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
// Author - Diwakar Singh //----------------------------------------------------------------------Libraries---------------------------------------------------------------------- import java.io.*; import java.util.*; import java.math.*; //--------------------------------------------------------------------fast functions---------------------------------------------------------------------- // fastPower(int a, int n) --> Power of a number // cnt_div(int n) --> count divisors // GcdOfArray(array, 0) ---> get gcd of whole array //----------------------------------------------------------------------Main Class-------------------------------------------------------------------------- public class Main { static PrintWriter out=new PrintWriter((System.out)); static int mod = 1000000007, imax=Integer.MAX_VALUE, imin=Integer.MIN_VALUE, e9=1000000009; static long lmax=Long.MAX_VALUE, lmin=Long.MIN_VALUE; public static int solver(long n){ int max = imin; for (int i=1; i<=Math.sqrt(n); i++) { if(n%i==0){ max = Math.max(max, i); } } return max; } //-------------------------------------------------------------------Driver code------------------------------------------------------------------------------ public static void main(String args[])throws IOException { Reader sc=new Reader(); int t=sc.i(); while(t-->0){ int n = sc.i(), start=0, end=n-1; int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = i+1; } out.println(n); for(int i=0;i<arr.length;i++){ out.print(arr[i]+" "); } out.println(); while(start<end){ int temp = arr[start]; arr[start] =arr[end]; arr[end] = temp; for(int i=0;i<arr.length;i++){ out.print(arr[i]+" "); } start++; out.println(); } } out.close(); } // -----------------------------------------------------------------fast functions--------------------------------------------------------------------- // static int __gcd(int a, int b) // { // return b == 0? a:__gcd(b, a % b); // } // static int GcdOfArray(int[] arr, int idx) // { // if (idx == arr.length - 1) { // return arr[idx]; // } // int a = arr[idx]; // int b = GcdOfArray(arr, idx + 1); // return __gcd(a, b); // __gcd(a,b) is inbuilt library function // } // static int fastPower(int a, int n){ // if(n==0) return 1; // if(n==1) return a; // if((n&1)==1) return a*fastPower(a, n/2)*fastPower(a, n/2); // return fastPower(a,n/2)*fastPower(a, n/2); // } static boolean prime(int n){ int count=0; for(int i=1;i*i<=n;i++){ if(n%i==0){ count++; if(i!=n/i) count++; } if(count>2) return false; } return true; } //---------------------------------------------------------------------Reader for input-------------------------------------------------------------------- static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String s() { while(!st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); } catch(Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int i() { return Integer.parseInt(s()); } public long l() { return Long.parseLong(s()); } public double d() { return Double.parseDouble(s()); } public String ns() { try { return br.readLine(); } catch(Exception e) { e.printStackTrace(); } return null; } public boolean b() { String next=null; try { next=br.readLine(); } catch(Exception e) { } if(next==null) { return false; } st=new StringTokenizer(next); return true; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
0438b3580a8a00ba459dc02a07fc5c74
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) { arr[i] = i + 1; } out.println(n); for(int x:arr) { out.print(x+" "); } out.println(); for(int i=0;i<n-1;i++) { int temp = arr[i]; arr[i] = arr[n - 1]; arr[n - 1] = temp; for(int x:arr) { out.print(x+" "); } out.println(); } } out.flush(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } static class FenwickTree { //Binary Indexed Tree //1 indexed public int[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new int[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public int find(int i) { int res = 0; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public int find(int l, int r) { return find(r)-find(l-1); } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } private static long mergeAndCount(int[] arr, int l, int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l;long swaps = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static long mergeSortAndCount(int[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree long count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static void my_sort(long[] arr) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } } class Pair { int first; int second; Pair(int first , int second) { this.first = first; this.second = second; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
fe9cc9c7ac3a7386506fa3f8b736126d
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int mod = 998244353; //static int mod = (int)1e9+7; static boolean[] prime = new boolean[1]; static int[][] dir1 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; static int[][] dir2 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; static int inf = 0x3f3f3f3f; static { for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i < prime.length; i++) { if (prime[i]) { for (int k = 2; i * k < prime.length; k++) { prime[i * k] = false; } } } } static class DSU { int[] fa; DSU(int n) { fa = new int[n]; for (int i = 0; i < n; i++) fa[i] = i; } int find(int t) { if (t != fa[t]) fa[t] = find(fa[t]); return fa[t]; } void join(int x, int y) { x = find(x); y = find(y); fa[x] = y; } } static List<Integer>[] lists; static void init(int n) { lists = new List[n]; for(int i = 0; i< n;i++){ lists[i] = new ArrayList<>(); } } static class LCA{ int[] dep; int[][] fa; int[] log; boolean[] v; public LCA(int n){ dep = new int[n+5]; log = new int[n+5]; fa = new int[n+5][31]; v = new boolean[n+5]; for (int i = 2; i <= n; ++i) { log[i] = log[i/2] + 1; } dfs(1,0); } private void dfs(int cur, int pre){ if(v[cur]) return; v[cur] = true; dep[cur] = dep[pre]+1; fa[cur][0] = pre; for (int i = 1; i <= log[dep[cur]]; ++i) { fa[cur][i] = fa[fa[cur][i - 1]][i - 1]; } for(int i : lists[cur]){ dfs(i,cur); } } private int lca(int a, int b){ if(dep[a] > dep[b]){ int t = a; a = b; b = t; } while (dep[a] != dep[b]){ b = fa[b][log[dep[b] - dep[a]]]; } if(a == b) return a; for (int k = log[dep[a]]; k >= 0; k--) { if (fa[a][k] != fa[b][k]) { a = fa[a][k]; b = fa[b][k]; } } return fa[a][0]; } } static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static int get() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Integer.parseInt(ss); } static long getx() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Long.parseLong(ss); } static int[] getint() throws Exception { String[] s = bf.readLine().split(" "); int[] a = new int[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(s[i]); } return a; } static long[] getlong() throws Exception { String[] s = bf.readLine().split(" "); long[] a = new long[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(s[i]); } return a; } static String getstr() throws Exception { return bf.readLine(); } static void println() throws Exception { bw.write("\n"); } static void print(int a) throws Exception { bw.write(a + "\n"); } static void print(long a) throws Exception { bw.write(a + "\n"); } static void print(String a) throws Exception { bw.write(a + "\n"); } static void print(int[] a) throws Exception { for (int i : a) { bw.write(i + " "); } println(); } static void print(long[] a) throws Exception { for (long i : a) { bw.write(i + " "); } println(); } static void print(int[][] a) throws Exception { for (int i[] : a) print(i); } static void print(long[][] a) throws Exception { for (long[] i : a) print(i); } static void print(char[] a) throws Exception { for (char i : a) { bw.write(i + ""); } println(); } static long pow(long a, long b) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans *= a; } a *= a; b >>= 1; } return ans; } static int powmod(long a, long b, int mod) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return (int) ans; } static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void resort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n - 1 - i]; } static void resort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n - 1 - i]; } static int max(int a, int b) { return Math.max(a, b); } static int min(int a, int b) { return Math.min(a, b); } static long max(long a, long b) { return Math.max(a, b); } static long min(long a, long b) { return Math.min(a, b); } static int max(int[] a) { int max = a[0]; for (int i : a) max = max(max, i); return max; } static int min(int[] a) { int min = a[0]; for (int i : a) min = min(min, i); return min; } static long max(long[] a) { long max = a[0]; for (long i : a) max = max(max, i); return max; } static long min(long[] a) { long min = a[0]; for (long i : a) min = min(min, i); return min; } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } static void yes() throws Exception { print("Yes"); } static void no() throws Exception { print("No"); } static int[] getarr(List<Integer> list) { int n = list.size(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = list.get(i); return a; } static int[] ans; public static void main(String[] args) throws Exception { int T = 1; T = get(); while (T-- > 0) { int n = get(); List<int[]> list = new ArrayList<>(); int[] a = new int[n]; for(int i = 0;i < n;i++){ a[i] = i+1; } list.add(a); int k = 0,f = 0; while (k < n-1) { int[] c = list.get(list.size() - 1).clone(); swap(c, k, k + 1); k++; list.add(c); } print(list.size()); for(int i[] : list) print(i); } bw.flush(); } static void swap(int[] a, int i, int j){ int t = a[i]; a[i] = a[j]; a[j] = t; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
ed38a268a3de0d371decbbd95ad1d223
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main { //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static int cnt; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { int mod = 1000000007; MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int a[] = new int[n+1]; out.println(n); for(int i=1;i<=n;i++){ out.print(i+" "); a[i] = i; } out.println(); for(int i=1;i<n;i++){ int t1 = a[i]; a[i] = a[i+1]; a[i+1] = t1; for(int j=1;j<=n;j++){ out.print(a[j]+" "); } out.println(); } } out.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
0e5cc7e4104b9b6c5a39fdbddda30749
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.lang.*; public class Solution{ static int mod=(int)1e9+7; static int mod1=998244353; static FastScanner sc = new FastScanner(); static StringBuffer as=new StringBuffer(""); public static void solve() { int n=sc.nextInt(); int[] ans=new int[n]; // System.out.println(n); as.append(n+"\n"); for (int i=0;i<n;i++ ) { ans[i]=i+1; } for (int i=0;i<n;i++) { int temp=ans[i]; ans[i]=ans[0]; ans[0]=temp; for (int j=0;j<n;j++ ) { // System.out.print(ans[j]+" "); as.append(ans[j]+" "); } // System.out.println(""); as.append("\n"); } } public static void main(String[] args) { int t = sc.nextInt(); // int t=1; outer: for (int tt = 0; tt < t; tt++) { solve(); } System.out.println(as); } static int[] leftRotate(int arr[], int n, int k) { int[] b=new int[n]; int mod = k % n; for (int i = 0; i < n; ++i) b[i]=arr[(i + mod) % n] ; return b; } static boolean isSubSequence(String str1, String str2, int m, int n) { int j = 0; for (int i = 0; i < n && j < m; i++) if (str1.charAt(j) == str2.charAt(i)) j++; return (j == m); } static int reverseDigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } /* Function to check if n is Palindrome*/ static boolean isPalindrome(int n) { // get the reverse of n int rev_n = reverseDigits(n); // Check if rev_n and n are same or not. if (rev_n == n) return true; else return false; } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static int[] LPS(String s){ int[] lps=new int[s.length()]; int i=0,j=1; while (j<s.length()) { if(s.charAt(i)==s.charAt(j)){ lps[j]=i+1; i++; j++; continue; }else{ if (i==0) { j++; continue; } i=lps[i-1]; while(s.charAt(i)!=s.charAt(j) && i!=0) { i=lps[i-1]; } if(s.charAt(i)==s.charAt(j)){ lps[j]=i+1; i++; } j++; } } return lps; } static long getPairsCount(int n, double sum,int[] arr) { HashMap<Double, Integer> hm = new HashMap<>(); for (int i = 0; i < n; i++) { if (!hm.containsKey((double)arr[i])) hm.put((double)arr[i], 0); hm.put((double)arr[i], hm.get((double)arr[i]) + 1); } long twice_count = 0; for (int i = 0; i < n; i++) { if (hm.get(sum - arr[i]) != null) twice_count += hm.get(sum - arr[i]); if (sum - (double)arr[i] == (double)arr[i]) twice_count--; } return twice_count / 2l; } static boolean[] sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } prime[1]=false; return prime; } static long power(long x, long y, long p) { long res = 1l; x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % p; y>>=1; x = (x * x) % p; } return res; } public static int log2(int N) { int result = (int)(Math.log(N) / Math.log(2)); return result; } //////////////////////////////////////////////////////////////////////////////////// ////////////////////DO NOT READ AFTER THIS LINE ////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// static long modFact(int n, int p) { if (n >= p) return 0; long result = 1l; for (int i = 2; i <= n; i++) result = (result * i) % p; return result; } static boolean isPalindrom(char[] arr, int i, int j) { boolean ok = true; while (i <= j) { if (arr[i] != arr[j]) { ok = false; break; } i++; j--; } return ok; } static int max(int a, int b) { return Math.max(a, b); } static int min(int a, int b) { return Math.min(a, b); } static long max(long a, long b) { return Math.max(a, b); } static long min(long a, long b) { return Math.min(a, b); } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } static void swap(long arr[], int i, int j) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int maxArr(int arr[]) { int maxi = Integer.MIN_VALUE; for (int x : arr) maxi = max(maxi, x); return maxi; } static int minArr(int arr[]) { int mini = Integer.MAX_VALUE; for (int x : arr) mini = min(mini, x); return mini; } static long maxArr(long arr[]) { long maxi = Long.MIN_VALUE; for (long x : arr) maxi = max(maxi, x); return maxi; } static long minArr(long arr[]) { long mini = Long.MAX_VALUE; for (long x : arr) mini = min(mini, x); return mini; } static int lcm(int a,int b){ return (int)(((long)a*b)/(long)gcd(a,b)); } static long lcm(long a,long b){ return ((a*b)/(long)gcd(a,b)); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void ruffleSort(int[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); int temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } public static int binarySearch(int a[], int target) { int left = 0; int right = a.length - 1; int mid = (left + right) / 2; int i = 0; while (left <= right) { if (a[mid] <= target) { i = mid + 1; left = mid + 1; } else { right = mid - 1; } mid = (left + right) / 2; } return i-1; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] read2dArray(int n, int m) { int arr[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = nextInt(); } } return arr; } ArrayList<Integer> readArrayList(int n) { ArrayList<Integer> arr = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int a = nextInt(); arr.add(a); } return arr; } long nextLong() { return Long.parseLong(next()); } } static class SegmentTree { int seg[]; int arr[]; int n; public SegmentTree(int arr[], int n) { this.n = n; seg = new int[n * 4]; this.arr = arr; } int task(int a, int b) { return Math.max(a, b);// TO DO } int mSeg(int i, int l, int r) { if (l == r) { seg[i] = task(arr[l], arr[l]); return arr[l]; } int m = (l + r) / 2; return seg[i] = task(mSeg(2 * i + 1, l, m), mSeg(2 * i + 2, m + 1, r)); } int qSeg(int ql, int qr, int i, int l, int r) { if (r < ql || l > qr) return Integer.MIN_VALUE;// TO DO if (ql <= l && qr >= r) return seg[i]; int m = (l + r) / 2; return task(qSeg(ql, qr, 2 * i + 1, l, m), qSeg(ql, qr, 2 * i + 2, m + 1, r)); } } static class Pair{ int first, second,third; Pair(int f, int s,int t){ first = f; second = s; third=t; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
c6eab1bdec9e0f3fb68dc2797cb13d89
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class pchain { public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); for (int x = 0; x < t; x++) { int n = Integer.parseInt(in.readLine()); System.out.println(n); for (int i = 0; i < n; i++) { int curr = 2; StringBuilder sb = new StringBuilder(); for (int j = 0; j < n; j++) { if (j == i) sb.append(1+" "); else { sb.append(curr+" "); curr++; } } System.out.println(sb.toString()); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
de1e7d363fc81dd94ec5ae1d84d5775e
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Stack; public class Main { static long MOD = 998244353l; int min = Integer.MAX_VALUE; int max = 0; char result[][]; int count = 0; int pattern = 0; public static void main(String[] args) throws Exception { // FileInputStream fis = new FileInputStream(new File("1.txt")); var sc = new FastScanner(); // var sc = new FastScanner(fis); // var pw = new FastPrintStream("test_normal_result.csv"); var pw = new FastPrintStream(); solve(sc, pw); sc.close(); pw.flush(); pw.close(); } public static void solve(FastScanner sc, FastPrintStream pw) { int times = sc.nextInt(); for (int t = 0; t < times; t++) { int n = sc.nextInt(); pw.println(n); int a[] = new int[n]; for (int i=0;i<n;i++) { a[i] = i+1; } for (int i=0;i<n;i++) { for (int j=0;j<n;j++) { pw.print(a[j]+" "); } pw.println(); if (i<n-1) { int temp = a[n-i-1]; a[n-i-1] = a[n-i-2]; a[n-i-2] = temp; } } } } public static Point checkPoint(char ch) { Point re = new Point(0, 0); switch (ch) { case 'N': { re.x--; break; } case 'S': { re.x++; break; } case 'E': { re.y++; break; } case 'W': { re.y--; break; } } return re; } public char[][] copyArray(char c[][], int n) { char re[][] = new char[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { re[i][j] = c[i][j]; } } return re; } public static void swap(int[] s, int i, int j) { int tmp = s[i]; s[i] = s[j]; s[j] = tmp; } public void permutation(int[] s, int from, int to, int a[]) { if (to <= 1) return; if (from == to) { check(s, a); } else { for (int i = from; i <= to; i++) { swap(s, i, from); permutation(s, from + 1, to, a); swap(s, from, i); } } } public void check(int[] s, int a[]) { int re = 0; int b[][] = new int[2][3]; for (int i = 0; i < 6; i++) { b[i / 3][i % 3] = a[s[i]]; } int maxx = 0; int maxy = 0; int minx = 10000; int miny = 10000; for (int i = 0; i < 3; i++) { maxx = Math.max(maxx, b[0][i]); minx = Math.min(minx, b[0][i]); maxy = Math.max(maxy, b[1][i]); miny = Math.min(miny, b[1][i]); } re = (maxx - minx) * (maxy - miny) * 2; re = re - (Math.abs(b[0][0] - b[0][1]) * Math.abs(b[1][0] - b[1][1])); re = re - (Math.abs(b[0][0] - b[0][2]) * Math.abs(b[1][0] - b[1][2])); re = re - (Math.abs(b[0][2] - b[0][1]) * Math.abs(b[1][2] - b[1][1])); max = Math.max(re, max); } public static long anothertoTen(long ano, int another) { long ten = 0; long now = 1; long temp = ano; while (temp > 0) { long i = temp % 10; ten += now * i; now *= another; temp /= 10; } return ten; } public static long tentoAnother(long ten, int another) { Stack<Long> stack = new Stack<Long>(); while (ten > 0) { stack.add(ten % another); ten /= another; } long re = 0; while (!stack.isEmpty()) { long pop = stack.pop(); re = re * 10 + pop; } return re; } // 2C5 = 5*4/(2*1) public static long XCY(long x, long y) { long temp = 1; for (int i = 0; i < x; i++) { temp = (temp * (y - i)) % MOD; } long tempx = 1; for (int i = 2; i <= x; i++) { tempx = (tempx * i) % MOD; } tempx = modpow(tempx, (long) MOD - 2); temp = (temp * tempx) % MOD; return temp; } static long modpow(long N, Long K) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(MOD)).longValue(); } static long modpow(long N, Long K, long mod) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(mod)).longValue(); } public static long gcd(long a, long b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } } class Node implements Comparable<Node> { int tyoten; long minDistance; public Node(int t, long m) { tyoten = t; minDistance = m; } @Override public int compareTo(Node o) { int res = -1; if (this.minDistance - o.minDistance >= 0) { res = 1; } return res; } } class Range { long x = 0; long c = 0; public Range(long x, long c) { this.x = x; this.c = c; } } class Vertex { String key; Vertex(String key) { this.key = key; } } class Edge { Vertex start; Vertex end; long key; Edge(Vertex start, Vertex end, long key) { this.start = start; this.end = end; this.key = key; } } class Target { int t; long c; boolean bool; } class Point implements Comparable { int x; int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Object p) { Point t = (Point) p; if (this.x > t.x) { return 1; } if (this.x < t.x) { return -1; } return 0; } @Override public boolean equals(Object p) { Point t = (Point) p; return this.x == t.x; } } class PointY implements Comparable { int a; int b; public int compareTo(Object p) { PointY t = (PointY) p; if (this.a > t.a) { return -1; } if (this.a < t.a) { return 1; } return 0; } } class FastPrintStream implements AutoCloseable { private static final int BUF_SIZE = 1 << 15; private final byte[] buf = new byte[BUF_SIZE]; private int ptr = 0; private final java.lang.reflect.Field strField; private final java.nio.charset.CharsetEncoder encoder; private java.io.OutputStream out; public FastPrintStream(java.io.OutputStream out) { this.out = out; java.lang.reflect.Field f; try { f = java.lang.String.class.getDeclaredField("value"); f.setAccessible(true); } catch (NoSuchFieldException | SecurityException e) { f = null; } this.strField = f; this.encoder = java.nio.charset.StandardCharsets.US_ASCII.newEncoder(); } public FastPrintStream(java.io.File file) throws java.io.IOException { this(new java.io.FileOutputStream(file)); } public FastPrintStream(java.lang.String filename) throws java.io.IOException { this(new java.io.File(filename)); } public FastPrintStream() { this(System.out); try { java.lang.reflect.Field f = java.io.PrintStream.class.getDeclaredField("autoFlush"); f.setAccessible(true); f.set(System.out, false); } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException e) { // ignore } } public FastPrintStream println() { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) '\n'; return this; } public FastPrintStream println(java.lang.Object o) { return print(o).println(); } public FastPrintStream println(java.lang.String s) { return print(s).println(); } public FastPrintStream println(char[] s) { return print(s).println(); } public FastPrintStream println(char c) { return print(c).println(); } public FastPrintStream println(int x) { return print(x).println(); } public FastPrintStream println(long x) { return print(x).println(); } public FastPrintStream println(double d, int precision) { return print(d, precision).println(); } private FastPrintStream print(byte[] bytes) { int n = bytes.length; if (ptr + n > BUF_SIZE) { internalFlush(); try { out.write(bytes); } catch (java.io.IOException e) { throw new RuntimeException(); } } else { System.arraycopy(bytes, 0, buf, ptr, n); ptr += n; } return this; } public FastPrintStream print(java.lang.Object o) { return print(o.toString()); } public FastPrintStream print(java.lang.String s) { if (strField == null) { return print(s.getBytes()); } else { try { return print((byte[]) strField.get(s)); } catch (IllegalAccessException e) { return print(s.getBytes()); } } } public FastPrintStream print(char[] s) { try { return print(encoder.encode(java.nio.CharBuffer.wrap(s)).array()); } catch (java.nio.charset.CharacterCodingException e) { byte[] bytes = new byte[s.length]; for (int i = 0; i < s.length; i++) { bytes[i] = (byte) s[i]; } return print(bytes); } } public FastPrintStream print(char c) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) c; return this; } public FastPrintStream print(int x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(long x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(double d, int precision) { if (d < 0) { print('-'); d = -d; } d += Math.pow(10, -d) / 2; print((long) d).print('.'); d -= (long) d; for (int i = 0; i < precision; i++) { d *= 10; print((int) d); d -= (int) d; } return this; } private void internalFlush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void flush() { try { out.write(buf, 0, ptr); out.flush(); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void close() { try { out.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } private static int len(int x) { int d = 1; if (x >= 0) { d = 0; x = -x; } int p = -10; for (int i = 1; i < 10; i++, p *= 10) if (x > p) return i + d; return 10 + d; } private static int len(long x) { int d = 1; if (x >= 0) { d = 0; x = -x; } long p = -10; for (int i = 1; i < 19; i++, p *= 10) if (x > p) return i + d; return 19 + d; } } class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buf = new byte[2048]; private int ptr = 0; private int buflen = 0; public FastScanner(java.io.InputStream in) { this.in = in; } public FastScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buf); } catch (java.io.IOException e) { throw new RuntimeException(e); } return buflen > 0; } private int readByte() { return hasNextByte() ? buf[ptr++] : -1; } public boolean hasNext() { while (hasNextByte() && !(32 < buf[ptr] && buf[ptr] < 127)) ptr++; return hasNextByte(); } private StringBuilder nextSequence() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); for (int b = readByte(); 32 < b && b < 127; b = readByte()) { sb.appendCodePoint(b); } return sb; } public String next() { return nextSequence().toString(); } public String next(int len) { return new String(nextChars(len)); } public char nextChar() { if (!hasNextByte()) throw new java.util.NoSuchElementException(); return (char) readByte(); } public char[] nextChars() { StringBuilder sb = nextSequence(); int l = sb.length(); char[] dst = new char[l]; sb.getChars(0, l, dst, 0); return dst; } public char[] nextChars(int len) { if (!hasNext()) throw new java.util.NoSuchElementException(); char[] s = new char[len]; int i = 0; int b = readByte(); while (32 < b && b < 127 && i < len) { s[i++] = (char) b; b = readByte(); } if (i != len) { throw new java.util.NoSuchElementException( String.format("Next token has smaller length than expected.", len)); } return s; } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') { n = n * 10 + b - '0'; } else if (b == -1 || !(32 < b && b < 127)) { return minus ? -n : n; } else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { return Math.toIntExact(nextLong()); } public double nextDouble() { return Double.parseDouble(next()); } public void close() { try { in.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } } /** * @verified https://atcoder.jp/contests/practice2/tasks/practice2_j */ class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Long.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX)); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return -1; } int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return false; } return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) { return -1; } return -parentOrSize[leader(a)]; } ArrayList<ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < n; i++) { result.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } return result; } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
308ef656495b0eaf85f83cf2890455fa
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Test { final static private FastReader fr = new FastReader(); final static private PrintWriter out = new PrintWriter(System.out) ; final static private long mod = (long)1e9 + 7; private static void solve() { int n = fr.nextInt() ; int[] arr = new int[n] ; for (int i = 0 ; i < n; i++) { arr[i] = i+1 ; } out.println(n); for (int x: arr) out.print(x+" "); out.println(); for (int j = n-1 ; j > 0; j--) { int temp = arr[j] ; arr[j] = arr[j-1] ; arr[j-1] = temp ; for (int x: arr) out.print(x+" "); out.println(); } } public static void main(String[] args) { int t = 1 ; t = fr.nextInt() ; while (t-- > 0) { solve() ; } out.close() ; } private static long[] inputArr(int n){ long[] arr = new long[n] ; for (int i = 0 ;i < n ; i++) { arr[i] = fr.nextLong() ; } return arr; } private static void sort(int[] arr){ ArrayList<Integer> al = new ArrayList<>() ; for (int x : arr){ al.add(x) ; } Collections.sort(al); for (int i = 0 ; i < arr.length; i++){ arr[i] = al.get(i) ; } } private static void sort(long[] arr){ ArrayList<Long> al = new ArrayList<>() ; for (long x : arr){ al.add(x) ; } Collections.sort(al); for (int i = 0 ; i < arr.length; i++){ arr[i] = al.get(i) ; } } private static long getMax(long ... a) { long max = Long.MIN_VALUE ; for (long x : a) max = Math.max(x, max) ; return max ; } private static long getMin(long ... a) { long max = Long.MAX_VALUE ; for (long x : a) max = Math.min(x, max) ; return max ; } private static long fastPower(double a, long b) { double ans = 1 ; while (b > 0) { if ((b & 1) != 0) ans *= a ; a *= a ; b >>= 1 ; } return (long)(ans + 0.5) ; } private static long fastPower(long a, long b, long mod) { long ans = 1 ; while (b > 0) { if ((b&1) != 0) ans = (ans%mod * a%mod) %mod; b >>= 1 ; a = (a%mod * a%mod)%mod ; } return ans ; } private static int lower_bound(List<Integer> arr, int key) { int pos = Collections.binarySearch(arr, key) ; if (pos < 0) { pos = - (pos + 1) ; } return pos ; } private static int upper_bound(List<Integer> arr, int key) { int pos = Collections.binarySearch(arr, key); pos++ ; if (pos < 0) { pos = -(pos) ; } return pos ; } private static int upper_bound(int[] arr, int key) { int start = 0 , end = arr.length ; while (start < end) { int mid = start + ((end - start) >> 1) ; if (arr[mid] <= key) start = mid + 1 ; else end = mid ; } return start ; } private static int lower_bound(int[] arr, int key) { int start = 0 , end = arr.length; while (start < end) { int mid = start + ((end - start )>>1) ; if (arr[mid] >= key){ end = mid ; } else start = mid + 1 ; } return start ; } private static class Pair{ int x ; int y ; Pair(int x, int y){ this.x = x ; this.y = y ; } @Override public int hashCode() { return super.hashCode(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return x == pair.x && y == pair.y; } } private static long gcd(long a, long b) { if (b == 0) return a ; return gcd(b, a%b) ; } private static long lcm(long a, long b) { return (a * b)/gcd(a, b); } private static boolean isPrime(int num) { if (num <= 2) return true ; for (int i = 2; i <= Math.sqrt(num); i++) { if (num%i == 0) return false ; } return true; } private static List<Long> seive(int n) { // all are false by default // false -> prime, true -> composite boolean[] nums = new boolean[n+1] ; for (int i = 2 ; i <= Math.sqrt(n); i++) { if (!nums[i]) { for (int j = i*i ; j <= n ; j += i) { nums[j] = true ; } } } ArrayList<Long>primes = new ArrayList<>() ; for (int i = 2 ; i <=n ; i++) { if (!nums[i]) primes.add((long) i) ; } return primes ; } private static boolean isVowel(char ch) { return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } private static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
e11b6ae8164595347cb1e31d73571f79
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.*; import java.util.*; public class gotoJapan { public static void main(String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solution solver = new Solution(); boolean isTest = true; int tC = isTest ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= tC; i++) solver.solve(in, out, i); out.close(); } /* ............................................................. */ static class Solution { InputReader in; PrintWriter out; public void solve(InputReader in, PrintWriter out, int test) { this.in = in; this.out = out; int n=ni(); int x=1; pn(n); for(int i=1;i<=n;i++) { int p=2; for(int j=1;j<=n;j++) { if(x==j)out.print(1+" "); else { out.print(p+" "); p++; } } x++; out.println(); } } class Pair { int x; int y; long w; Pair(int x, int y, long w) { this.x = x; this.y = y; this.w = w; } } char[] n() { return in.next().toCharArray(); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } long[] nal(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } void pn(long zx) { out.println(zx); } void pn(String sz) { out.println(sz); } void pn(double dx) { out.println(dx); } void pn(long ar[]) { for (int i = 0; i < ar.length; i++) out.print(ar[i] + " "); out.println(); } void pn(String ar[]) { for (int i = 0; i < ar.length; i++) out.println(ar[i]); } } /* ......................Just Input............................. */ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } /* ......................Just Input............................. */ }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
92fd588f7db4c670d1d38ee7444d07bd
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class A { static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } static long gcd(long n, long m) { if (m == 0) return n; else return gcd(m, n % m); } static long lcm(long n, long m) { return (n * m) / gcd(n, m); } static void dfs(ArrayList<ArrayList<Integer>> adj, int s, int[] vis, int[] cnt) { for (int i : adj.get(s)) { if (vis[i] == 1) { continue; } cnt[0]++; vis[i] = 1; dfs(adj, i, vis, cnt); } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); StringBuilder str = new StringBuilder(); int t = sc.nextInt(); for (int xx = 0; xx < t; xx++) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = i + 1; str.append(n+"\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { str.append(arr[j] + " "); } if (i != n - 1) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } str.append("\n"); } } System.out.println(str); sc.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 11
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
7d4d6bf2a8104bfc92e8fec096df8c2e
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.util.stream.IntStream; import static java.lang.System.out; import static java.util.Arrays.stream; public class B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-->0) { int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 1; i<=n; i++) { a[i-1]+=i; } out.println(n); printArray(a); for (int i = n-1; i>=1; i--) { swap(a, i, i-1); printArray(a); } } } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static void printArray(int[] a) { StringBuilder builder = new StringBuilder(); for (int i : a) builder.append(i).append(" "); builder.append("\n"); out.print(builder); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
dc734cdb3a3ebc5aea0fcee60f06e5b5
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.util.stream.IntStream; import static java.lang.System.out; import static java.util.Arrays.stream; public class B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-->0) { int n = scanner.nextInt(); out.println(n); for (int i = 1; i<=n; i++) { StringBuilder builder = new StringBuilder(i+" "); for (int j = 1; j<=n; j++) { if (j==i) continue; builder.append(j).append(" "); } out.println(builder); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
6480e1cfec24464c994f61d7e7e5e313
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; public class Main{ public static void main(String[] args) throws IOException { Scanner sc= new Scanner(System.in); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); int t=sc.nextInt(); while(t>0){ t-=1; int n=sc.nextInt(); int a[]=new int[n]; for(int i=0; i<n; i++) a[i]=i+1; int y=n-1; writer.write(n + "\n"); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ writer.write(a[j] + " "); } writer.write("\n"); int temp=a[i]; a[i]=a[y]; a[y]=temp; } } sc.close(); writer.flush(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
72d8d08c4493d6bc6127437429462041
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Solution extends Helper { private static final SuperFastReader sc = new SuperFastReader(); // private static final FastReader sc = new FastReader(); // private static final Scanner sc = new Scanner(System.in); private static final PrintWriter out = new PrintWriter(System.out); private static final int MOD = (int) (1e9) + 7;// ((a + b) % MOD + MOD) % MOD public static void main(String[] args) throws IOException { int t = sc.Int(); for (int i = 1; i <= t; ++i) { // System.out.print("Case #" + i + ": "); solve(); } // solve(); sc.close(); out.close(); } public static void solve() throws IOException { int n = sc.Int(); int perm[] = new int[n]; println(n); for (int i = 0; i < n; i++) { perm[i] = i + 1; print(perm[i] + " "); } println(); for (int i = 2; i < n; i++) { int t = perm[i]; perm[i] = perm[i - 1]; perm[i - 1] = t; for (int j = 0; j < n; j++) { print(perm[j] + " "); } println(); } int t = perm[n - 1]; perm[n - 1] = perm[0]; perm[0] = t; for (int j = 0; j < n; j++) { print(perm[j] + " "); } println(); } public static <T> void print(T data) { out.print(data); } public static <T> void println(T data) { out.println(data); } public static void println() { out.println(); } public static void read(int arr[]) throws IOException { for (int i = 0; i < arr.length; i++) { arr[i] = sc.Int(); } } public static void read(long arr[]) throws IOException { for (int i = 0; i < arr.length; i++) { arr[i] = sc.Long(); } } public static void read(String arr[]) throws IOException { for (int i = 0; i < arr.length; i++) { arr[i] = sc.next(); } } public static void read(int mat[][]) throws IOException { for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[i].length; j++) { mat[i][j] = sc.Int(); } } } public static void read(long mat[][]) throws IOException { for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[i].length; j++) { mat[i][j] = sc.Long(); } } } } class Pair<K, V> { public K key; public V value; Pair(K k, V v) { key = k; value = v; } } class SuperFastReader { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public SuperFastReader() { stream = System.in; } // file input public SuperFastReader(String i) throws IOException { stream = new FileInputStream(i); } private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString().trim(); // .trim() used to remove '\n' from either ends } public int Int() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long Long() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double Double() { return Double.parseDouble(next()); } public char Char() { return next().charAt(0); } public void close() throws IOException{ stream.close(); } } class Helper { }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
564726ae8b8ec7d3cd1438de4c5f069b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Solution { private static final SuperFastReader sc = new SuperFastReader(); private static final int MOD = (int) (1e9) + 7;// ((a + b) % MOD + MOD) % MOD public static void main(String[] args) throws IOException { int t = sc.Int(); for (int i = 1; i <= t; ++i) { // System.out.print("Case #" + i + ": "); solve(); } // solve(); sc.close(); } public static void solve() throws IOException { int n = sc.Int(); int perm[] = new int[n]; StringBuilder res = new StringBuilder(); res.append(n).append("\n"); for (int i = 0; i < n; i++) { perm[i] = i + 1; res.append(perm[i] + " "); } res.append("\n"); for (int i = 2; i < n; i++) { int t = perm[i]; perm[i] = perm[i - 1]; perm[i - 1] = t; for (int j = 0; j < n; j++) { res.append(perm[j] + " "); } res.append("\n"); } int t = perm[n - 1]; perm[n - 1] = perm[0]; perm[0] = t; for (int j = 0; j < n; j++) { res.append(perm[j] + " "); } res.append("\n"); System.out.print(res); } } class Pair<K, V> { public K key; public V value; Pair(K k, V v) { key = k; value = v; } } class SuperFastReader extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public SuperFastReader() { this(System.in, System.out); } public SuperFastReader(InputStream i, OutputStream o) { super(o); stream = i; } // file input public SuperFastReader(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString().trim(); // .trim() used to remove '\n' from either ends } public int Int() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long Long() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public double Double() { return Double.parseDouble(next()); } public char Char(){ return next().charAt(0); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
d2284a52960741f6f3a61bba39dfbccb
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class CF1716B_build_permutation { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // end of fast i/o code public static boolean isPalindrome(String str) { int i = 0; int j = str.length() - 1; int flag = 0; while (i <= j) { if (str.charAt(i) != str.charAt(j)) { flag = 1; break; } i++; j--; } return flag == 1 ? false : true; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } class Pair { int x1; int x2; public Pair(int x1, int x2) { this.x1 = x1; this.x2 = x2; } } static void buildPermut(int n ,ArrayList<Integer>temp,int i,boolean vis[],ArrayList<ArrayList<Integer>>ans){ if(temp.size()==n){ ArrayList<Integer>t=new ArrayList<>(); for(int j:temp) t.add(j); ans.add(t); // System.out.println(ans); return; } for(int j =1;j<=n;j++){ if(!vis[j]){ vis[j]=true; temp.add(j); buildPermut(n, temp, j, vis, ans); temp.remove(temp.size()-1); vis[j]=false; } } } static void swap(int nums[],int i,int j){ int temp=nums[i]; nums[i]=nums[j]; nums[j]=temp; } public static void main(String[] args) { FastReader fs = new FastReader(); PrintWriter out= new PrintWriter(System.out); int t=fs.nextInt(); while(t-->0){ int n =fs.nextInt(); out.println(n); int ans []=new int[n+1]; for(int i =1;i<=n;i++){ out.print(i+" "); ans[i]=i; } out.println(); for(int i =1;i<n;i++){ swap(ans,i,i+1); for(int j =1;j<=n;j++) out.print(ans[j]+" "); out.println(); } } out.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
f65203134e3bb7d38dc1fc6c5beba2ba
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class InVoker { //Variables static long mod = 1000000007; static long mod2 = 998244353; static FastReader inp= new FastReader(); static PrintWriter out= new PrintWriter(System.out); public static void main(String args[]) { InVoker g=new InVoker(); g.main(); out.close(); } //Main void main() { // Ara ara :/ int t=inp.nextInt(); loop: while(t-->0) { int n=inp.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=i+1; } out.println(n); print(a); a[0]=a[n-1]; a[n-1]=1; print(a); for(int j=0;j<n-2;j++) { int x=a[j]; a[j]=a[j+1]; a[j+1]=x; print(a); } } } /********************************************************************************************************************************************************************************************************* * ti;. .:,:i: :,;;itt;. fDDEDDEEEEEEKEEEEEKEEEEEEEEEEEEEEEEE###WKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWW#WWWWWKKKKKEE :,:. f::::. .,ijLGDDDDDDDEEEEEEE* *ti;. .:,:i: .:,;itt;: GLDEEGEEEEEEEEEEEEEEEEEEDEEEEEEEEEEE#W#WEKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKKKKKKG. .::. f:,...,ijLGDDDDDDDDEEEEEE * *ti;. .:,:i: :,;;iti, :fDDEEEEEEEEEEEEEEEKEEEEDEEEEEEEEEEEW##WEEEKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWWKKKKKKEG .::. .f,::,ijLGDDDDDDDDEEEEEE * *ti;. .:,:i: .,,;iti;. LDDEEEEEEEEEEKEEEEWEEEDDEEEEEEEEEEE#WWWEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWKKKKKEDj .::. .:L;;ijfGDDDDDDDDDEEEEE * *ti;. .:,:i: .:,;;iii:LLDEEEEEEEEEEEKEEEEEEEEDEEEEEEEEEEEW#WWEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWKWWWWWWWWWWWWWWKKKKKKKEL .::. .:;LijLGGDDDDDDDDEEEEE * *ti;. .:,:;: :,;;ittfDEEEEEEEEEEEEEEEEKEEEGEEEEEEEEEEEKWWWEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWKKKKKKKELj .::. :,;jffGGDDDDDDDDDEEEE * *ti;. .:,:i: .,;;tGGDEEEEEEEEEEEKEEEKEEEDEEEEEEEEEEEEWWWEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWKWWWWWWKKKKKKKEEL .::. .:;itDGGDDDDDDDDDEEEE * *ti;. .:::;: :;ifDEEEEEEEEEEEEKEEEKEEEEEEEEEEEEEEEWWWEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WWWKKKKKKKKEEf .::. :,itfGEDDDDDDDDDDDEE * *ti;. .:::;: :GGEEEEEEEEEEEKEKEEKEEEEEEEEEEEEEEEEWWEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKKEEDG .::. .,;jfLGKDLDDDDDDEEDD * *ti;. .:::;: fDEEEEEEKKKKKKKKKEKEEEEEEEEEEEEEEE#WEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#KKKKKKKKKKEEf .:::. .,;tfLGDEDDDDDDDDEEE * *ti;. :::;: fDEEEEEEKKKKKKKKKKWKEEEEEEEEEEEEEEEWKEEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKKKEEft :::. .,;tfLGDDDKDDDDDDDDD * *ti;. .::;: fDEEEEEEKKKKKKKWKKKKKEEEEEEEEEEEEE#WEEWEEEEEDEEDEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKEEGG :,:. .,;tfLGGDDDKDDDDDDDD * *ti;. .:.;: tGDEEEEKKKKKKKKKKKKKKKKKEEEEEEEEEEEWEEKWEEEEEEEDEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKKKKKEEDf :::. .,;tfLGGDDDDEDDDDDDD * *ti;. .::;: fDEEEEEKKKKKKKKKKKWKKKKKKKKEEEEEEEWWEEWEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKEEEft.::. .,;tfLGGDDDDDDEDDDDD * *ti;. .:.;: tGDEEEKKKKKKKKKKKKKKKKKKKKKKEKEEEEE#EEWWEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKEEEGD:::. .,;tfLGGDDDDDDDEDDDD * *ti;. .:.,. LDEEEEKKKKKKKKKKWKWKKKKKKKKKKKKEEEKWEKW#EEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKW##KKKKKKEEEEf,,:. .,;tfLGGDDDDDDDDEDDD * *ti;. ..:.,. LGDEEEEKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEEW#WEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKK##KKKKKEEEEEfi;,. .,;tfLGGDDDDDDDDDKDD * *tt;. .:.,: jDEEEEKKKKKKKKKKWWKKKKKKKKKKKKKKKKKWKE#WWEEEEEEEEEEEEEEWEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKWWKKKKEEEEDfG;,: .,;tfLGGDDDDDDDDDDKD * *tii,. .:.,. tGDEEEEKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKKWWWKEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKW#KKKKEEEEDGGi;,. .,;tfLGGDDDDDDDDDDDE * *ti;;,:. .:.,: fDEEEEKKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEK#WWKEEEEEEEEEEEEDEEEEEEEEEEEEEEGEEEEEEEEEEEEEEEEEEEKKKKKKKWWKKEEEEEEDDf;;;,. .,;tfLGGDDDDDDDDDDDD * *tii;,,:.. ...,. ;LEEEEEKKKKKKWKKKKWKKKKKKKKKKKKKKKKKEKKW#WEEEEEEEEEEEEEjEEEEEEEEEKEEEEGEEEEEEEEEKEEEEEEEEEEEEEEEEE#WKEEEEEEDDf;;;;,: .,itfLGGDDDDDDDDDDDD * *ti;,,,,,:. ...,. LDEEEEKKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKK#W#WEEEEEEEEEEEDDLEEEEEEEEEWEEEEDEEEEEEEEEKEEEEEEEEEEEEEEEEEWWEEEEEEEDDfj,,,,,:. .,itfGGGDDDDDDDDDDDD * *tii,,,,::::. ...,: .fDEEEEKKKKKKWKKKKWWWKKKKKKKKKKKKKKKEKKW#WWEEEEEEEEEEEKiKEEKEEEEEEWEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEEWWEEEEEEEDDLD:::,,,:. .,ijfGGGDDDDDDDDDDDD * *ti;:::::::::.. .:.,: LDEEEEKKKKKKKWKKKKWWKKKKKKKKKKKKKKKKtKKWWWWKEEEEEEEEEDiiDEEEEEEEEWWEEEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEWKEEEEEDDDGL:. .:,,,: .,ijLGGGDDDDDDDDDDDD * *tt;. .::::::::.. ...,: :fDEEEKKKKKKKKKKKKWW#KKKKKKKKKKKKKKKKfKKWWWWKEEEEEEEEDti,DEKEEEEEEWWEEEDEEEEEEEEEKEEEEEEEEEEEEEDEEEEE#WEEEEEGGDGf:. .:,;,:. .,ijLGGDDDDDDDDDDDDD * *tt;. .:::::::.. ...,: GDEEEKKKKKKKKWKKKKWWWKKKWKKKKKKKWWWKDEKLWWWWKKEEEEEEDEi,LDEEEEEEEEWWEEEEEEEEEEEEEEEEEEEEEEEEEDEDEEEEEW#EEEEDDDDGf,. :,,,:...,ijLGGGDDDDDDDDDDDD * *tt;. .....::::.. ...,: fDEEEKKKKKKKKWKKKKWWWWKKKWKKKKKKKKKKfWKiWWW#KKEEEEEEEi;.EDfEEEDEEiWWEEEEEEEEEEEEDGKEEEEEEEEEEDEEEEEEEWWEEEEDDDGGLi. .,;,:::,ijLGGGDDDDDDDDDDDD * *tt;. ....:::::. ...,. iDEEEEKKKKKKKKWKKWKWWWWWKKWWWKKKKKKKKtWKt#WWWKKEEEEEDji..DDKDDEDEGiWKEEEEEEEEEEDDEjEEEEEEEEEEEDEEEEEEEKWKEEDDDDGGff. .:,;,,;ijLGGGDDDDDDDDDDDD * *tt;. ....::::.. .:.,: .LDEEEKKKKKKKKKKKKWWWWKWWWWWWWWWWKKKKWtKKiDWWWKKKEEEEKi:..DEDDDDDDiiWKEEEEEEEEEEDDEijDEEEEEKEEEEEEEEEEEEWWEEGDDDGGLG. .:,;;iijLGGGDDDDDDDDDDDD * *tt;. .....:::.. ...,. .fEEEEKKKKKKKKWKKKKWWWWWWWWWWWWWWKWKKKiKDiLWWWWKEEEEEi,..fD:DDDDDti;WEEEEEEEEEEKDDi:iDDEEEEWEEEEEEEEEEEE#WEEGDDDDGGG. :,iitjLGGGDDDDDDDDDDDD * *tti. .....:::.. ...,. GDEEEKKKKKKKKKWKKKWWW#WWWWWWWWWWWKWKKjiEjitWWWKKWEEEDi...DDLDDDDji;;WEEEEEEEEEEEDEj.iDDEEEEWEEEEEEEEEEEEWWEEDDDDDDGf. .,;tjfLGGDDDDDDDDDDDD * *tti. ....::::.. ...,. fEEEKKKKKKKKKKKKKKKW#WWWWWWWWWWWWWWWWtiEiiiWWWKKEWKEi....D.EDDDEi;.fWEEEEEEEEEEDDfL.;EDDEEEWEEEEEEEEEEEEWWEEEDDDDDGf. :;ijfLGGDDDDDDDDDDDD * *tti. ....::::.. ...,. LDEEEKKKKKKKKKKKKKKWWWWWWWWWWWWWWWW####WKiiiWWWKKKEEK,...:E:DDDEii..GWEEEEEEEEDWDDiL.,KDDEEEWEEEEEEEEEEEEWWKEEDDDDDGf: .,itfLGGDDDDDDDDDDDD * *tti. .....:::.. ...,. fDEEEKKKKKKKKKWKKKKWWWWWWWWWWWWW########WLiiWWWKKKEEjD...G,DDDDi;...EWEEEEEEEEDKDEii..LDDEEEWEEEEEEEEEEEEWWWEEDDDDDGfi .,;tfLGGGDDDDDDDDDDD * *tti. .....:::.. ...,. iGEEEKKKKKKKKKKWKKKKWWWWWWWWWWWW###########KiWWWKKEEE,.D..D.DDDii:...KKEEEEEEEEEDDj:...tEDEEEWEEEEEEEEEEEEWWWEEEDDDDDLL .,;tjLLGGDDDDDDDDDDD * *tti. ....::::......:. LEEEKKKKKKKKKKWWKKKWWW#KWWWWWWWW#####W####W##KWWKKEEL..:D.jjDDi;,....KKEEEEEEEDfDDi...:iKDEEEWKEEEEEEEEEEEWWWEEEEDDDDLG .,;tjLLGGDDDDDDDDDDD * *tti. ...::::::..,. :GEEEKKKKKKKKKKKKWWWWW##WWWWWWWWW##WKWK#W#W####WWKEEK.....G.DDti,.....KKEEEEEEDWGDf.,...iKDEEEWWEEEEEEEEEEEW#WEEEEEDDDGL .,;tjLLGGDDDDDDDDDDD * *tti. ....::::::,. GDEEKKKKKKKKKKKKKWWWW###WWWWWWWWWW#WWWK###W#####WKEKK.....jDDL;;......KKEEEEEEEEEDi.f...;KDEEEWWEEEEEEEEEEEWWWWEEEEEDDGf .,;tjLLGGDDDDDDDDDDD * *tti. ....:::,,. .LEEEKKKKKWKKKKKWWWWWW###WWWWWWWWWW#WWKW#WW##W#WWWKEKD:....:DD:;......;KEEEEEEEKiDD..f...,KKEEEWWEEEEEEEEEEEWWWWEEEEEDDDf .:;tjLLGGGDDDDDDDDDD * *tti. ...::,,,:. GDEEKKKKKKKKKKKKWWWWWWW#WWWWWWWWWWW#KjKWWWWWWWWWWWWEK.j,..;fD.;.......fKEEEEEDKG:Di..,....DKEEEWWEEEEEEKEKKKWWWWEEEEEEDDf .:;tjLLGGDDDDDDDDDDD * *jti. ...::,,,,:. .fEEEKKKKKWKKKKKKWWWWWWW#WWWWWWWWWWK#KKKWWWWWWWWWWWWWK..f:.:G.,:.......EKEEEEEKK;:E:.......fKEEEWWKEKEKKKKKKKW#WWEEEEEEDDf: .,;tfLLGGDDDDDDDDDDD * *tti. ...:,,,;;,: iDEEKKKKKWKKKKKKKWWWWWWW#WWWWWWWWWWK#WDKWWKKWWWWWWWWWE..;G:G..,........KKEEEEEKi.Gi..:.....tKEEKWWWKKKKKKKKKKW##WKEEEEEEDfi .,;tfLLGGGDDDDDDDDDD * *tti. ....::,,;;;,LEEKKKKKKWKKKKKWWWWWWW###WWWWWWWWWWKWWDKWEEEWKKWWWWWKKj.:LG..;.........EKEEEEKG;.G...;.....;KKEKWWWKKKKKKKKKKW##WWKEEEEEDfL .,;tfLGGGDDDDDDDDDDD * *jti. ...::::,;ijDEEKKKKKWKKKKKKWKWWWWW##WWWWWWWWWWWKK#KKGDGDWEEWKKWKKGE,.i;.:.........:EKEEEKE;.:L...j.....,KWEKWWWKKKKKKKKKK####WKKEEEEDLG .,;tfLGGGGDDDDDDDDDD * *jti. ...:...,,;GEEKKKKKWWKKKKKWWWWWWWW###WWWWWWWWWKKKWWKiLGGEDEDEKGKKiEG..;...........jKEEEKK;:.G....,.....:KKEWWWWKKKKKKKWKK####WKKKKEEEGL .,;tfLGGGGDDDDDDDDDD * *jti. ...:. .:,GEEKKKKKWKKKKKWWWWWWWW####WWWWWWWWWKKKWWKii;fDLGDK: EEi:E:.............EKEEKK;;..L...........KKKWWWWKKKKKKKWKK####WKKKWKEEDf .,;tfGGGGDDDDDDDDDDD * *jti. ...:. ,EEKKKKKWWKKKKKWWWWWWWWW###WWWWWWWWKKKKfWWLt;i,. fi EG..D:.............EKEKK;;..t....:.......KWKWWWWKKKKKKKWKK####WKKKWEEEDf:. .,;tfGGGGDDDDDDDDDDD * *jti. ...:. GEEKKKKKWKKKKKWWWWWWWWW####WWWWWWWKKKKKt;KKEfff .;t.................KKKKi;:..GtGGfG.......KWWWWWWKKKKKKKWKK###WWWKKKKEEEf,,: .,;tfGGGGDDDDDDDDDDD * *jti. ...:. GEKKKKKWWKKKKKWWWWWWWWWW##WWWWWWWKKKKKKt;EiKKKK, ...t................jEKKG;;..,.....,LGi....KWWWWWWKKKKKKWKKKW####WKKKKKEEL,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. .GEEKKKKKWKKKKKWWWWWWWWWW###WWWWWWWKKKKKKtiE::tGG........................EEEj;;...,.........:D..DKWWWWWWKKKKK#KKW###W#WKKKKKEEfj:,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. DEKKKKKWWKKKKKWWWWWWWWW####WWWWWWWKKKKKKiiE:::.::.......................EEi;;...j.....f......:iDKWWWWWWKKKKK#WW######WKKKKKEELG :,,,,:. .,;tfGGGDDDDDDDDDDDD * *jti. ...:. fEEKKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKK;tE::..........................DD;.;,.::......;........EWWWWWWWKKKKW#WW#####WWKKKWKKELG .:,,,:::,;tfGGGDDDDDDDDDDDD * *jti. ...:. .DEKEKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKE,iD::..........................D..,;.,;tLffi...........DWDWWWW#KKKWWWWW#####W#KKKWKEEGL .:,;,,,;tfGGGDDDDDDDDDDDD * *jti. ...:. ;EEKKKKWWKKKKKWWWWWW#WWWW####WWWWWWKKKKKEL:iD:..........................j ..;..;;:.....i,........DKtWWWWWKKWWWWWW#####WWWKKKEKEDf .:,;;;itfGGGDDDDDDDDDDDD * *jti. ...:. DEKKKKKWWKKKKWWWWWWW#WWWW####WWWWWWKKKKKEj:iG...............................:....................GKiWWWWWKKWW#WWW######WWKKKKKEEf .,;iitfGGGDDDDDDDDDDDD * *jti. ...:.:EKKKKKWWKKKKKWWWWWWW#WWW#####WWWWWKWKKKKEi:if:.................................iEKEKKKKKKDj......DKiWWWWWKWK##WW#######WWKKK:KEEL .:;itfGGGDDDDDDDDDDDD * *jji. ...:,DEEKKKWWWKWKKWWWWWWWWWWWW#####WWWWWWWKKKKEi:it..................................j. KKKKKKKKKKKf..DKiWWWWWKWW##WW#######WWKKK,KEEf .,;tfGGGDDDDDDDDDDDD * *jji. ..L:iDEEKKKWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKKi.i;.................................. . KKKWWWWWWWWK..DGiWWWWWKK##WWW#####W#WWKKKjEKEL, .:;tfGGGDDDDDDDDDDDD * *jji. .f:::EEEKKKWWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKK;.i,.................................:: KKEKWWWWWWfWK..EiiWWWWWKWW#WW##########KKKD,KELj .:;tfGGDDDDDDDDDDDDD * *jji. .t::::,DEEKKKWWKKKKWWWWWWWWW#WWWW#####WWWWKKWKKKEK;.i:.................................GDDEEEKKKWWWWWtWWD.E;iWWWWWW###WW#########WWKKK.EEDG .:;tfGGGDDDDDDDDDDDD * *jji. . j..::::EKEKKKWWWKKKKWWWWWWWWW#WWW######WWWWKKWKKKEK;.t:.................................ELLEDDEEEWWWWEtWK,.KiiWWWWWW###W##########WWKKK:EEEG .;tjfLLGDDDDDDDDDDDDDDD * *jji. i.::::::,EEEKKWWWKKKKKWWWWWWWWW#WWW#####WWWWWKWKKKKEE,.t..................................DfiEGDDDEEKKKttKWG.KiiWWWWW##WWW##########WWKKK:fEEL ,fGGGDDDDDDDDEEEDDDDDDDDDD * *jji. .;:..:::::DEEEKKWWWKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKED,.t..................................ifjDDGGEGDKK.ttKKE.DiWWWWW###WW##########WWWKKK:.KELiLGGGGDDDDDDDDDDDDEEEDDDDDDD * *jji. i.:.::::::,KEEKKWWWKKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKEL:.j..................................GGf,;ifLLED .iiKKi:fWWWWWW##W#W##########WWWKKK:.KKLGGDDDDDDDDDDDDDDDDEDDEEDDDDD * *jji. .j:.::::::::EEEKKKWWWKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKf:.f..................................:EEfftf .,. ;iE,..jWWWWWWW###W############WWKKK,:KKGDDDDDDDDDDDDDDDDDDDDDDDEDDDD * *jji. .:.::::::::,,EEEKKWWWKKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKt..G....................................EEELL; .j....tKWWWWWWW################WWWKKtfGKGEDDDDDDDDDDDDDDDDDDDDDDDEEDD * *jji. :...:::::::,,jEEKKWWWWKKKKKKWWWWWWWWW##KWW#####KWWWWWWKKKKEi..D....................................:jEEE.........;KKWWWWWWWW#WW##W##########WWKKDLGKEKDDDDDDDDDDDDDDDDDDDDDDDDDED * *jji. i:.::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWW##WWW#####WWWWWWWKKKKKi..D......................................:::::......,KKKWWWWWWWWW#####W########WWWKKKGGKKEGGGGGGGGDDDDDDDDDDDDDDDDDDE * *jji. i..:::::::::,,tEEKKWWWWKKKKKWWWWWWWWWWW##WW######WWWWWWWKKKKKi..D......................................::::......:EKKKWWWWWWWWWWW##WW########W#WKKWGGKKGGGGGGGGGGGGGGGDDDDDDDDDDDDD * *jji. .:::::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWWW##WW#####WWWWWWWWKKKKKi..D....................................:::::::::..tELii;KWWWWWWWWWW##WW######WWWWWWKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGDG * *jjt. :.::::::::,,,,fEEKKWWWWKKKKKKWWWWWWWWWW###WW####WWWWWWW#WKKKKKi..D....................................:::::::.:.,;;;;;;,KKWWWWWWWWW#WW########WWWKKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGGG * *jji. ;.::::::::,,,,;EEEKWWWWWKKKKKWWWWWWWWWWWW##WW###WKWWWWWK#WKKKKKi..G......................................:::::::,;;;;:...KKKWWWWWWWWWKWW#######WWWWKKGLGKDGGGGGGLLGGGGGGGGGGGGGGGGGGG * *jjt. f.:::::::::,,,,fEEKKWWWWWKKKKKWWWWWWWWWWW###WW##WKKWWWWWW#WKKKKK;.jt........i.............................:::::::;j;;....:E.KKKWWWWWWWKWW#####W#WWWWKKLLGWEEGGGGGLGGGGGGGGGGGGGGGGGGGG * *jjt. ...:::::::,,,,,;DEEKWWWWWKKKKKWWWWWWWWWWWW####WWWKKKWWWWWWWWKKKKK;.E;.........t.............................:::::ii;;.....D...KKWWWWWWWKWW#####WWEWWWKKGGGEKKGGGGGLGGGGGGGGGGGGGLGGGGGG * *fji. ;.:::::::,,,,,;LEEKKWWWWWKKKKKWWWWWWWWWWWW####KWKKKKWWWWWWWWKKKKKi.D;..........j.............................:::tt;,.....:.....KKWWWWWWKWWWW##WWWGWWWKKGGGGKEGGGGGGGGGGGGGGGGGGGLLGGGGL * *fji. t::::::::,,,,,,;EEEKWWWWWKKKKKKKWWWWWWWWWWW##WKWKKKKKWWWWWWWWKKKKKi:D;............j...........................::LL;,.............KKWWWWWKWWWWWWWWWGWWWKKGGGGKGGGGGGGGGGGGGGGGGGGGLLGGGGL * *fjt: .:::::::,,,,,,,DEEKWWWWWWKKKKKKKWWWWWWWWWWWWKKWKKKKKKWWWWK#WWKKKKWitE;........... ............................:G;;:...............KKKWWKKWWWWWWWWWGWWWKKGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGL * *fjji;:. .f:::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWWKKKKKKKKKKKWWKWWWWWKKKKWGKD;........................................L;;..................DKKWKKWWWWWWWWWGWWWKKDGGGKDGGGGGGGGGGGGGGGGGGGGGGGGGG * *fjjtii;,:. :::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWKKKKKKKKKKKKWWWWWW#WWKKKKWiEj;......................................:i,;....,...............;KKEKWWWWWWWWWGKWWKKDDGGDEGGGDGGGGGDGGGGGGGGGGGGGGGG * *fjtiiiii;;:. j::::::,,,,,,,;;EEEKWWWWW#KKKKKWWWWWWWWWKKKKKKWKKKKKKKWWWWWWWWWKKKKWtEL;:....................................;;;:...,;j................:KEEWWWWWWWWWDDWWKKDDDDDKDDDDDDDDDDDDDDDGGGGGGGGGGG * *fjti;;iiii;;,:::::::,,,,,,,,;EEEKWWWWWWWKKKKWWWWWWWWKKKKKKKWKKKKKKKWWWWWWW#W#KKKKWEEii;...................................f;:....,;L...................EEKWWWWWWWWDDWWKKDDDDDKEDDDDDDDDDDDDDDDDDDDDDGGGG * *fjt,,,;;;;ii;f::::::,,,,,,,;;EEKWWWWWWWKKKKKWWWKWWKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWKEij;:...............................:G;,.....,;f....................:tKKWWWWWWWDDWWKKDDDDDKKDDDDDDDDDDDDDDDDDDDDDDDDD * *jjt. ..:,;;;;,::::,,,,,,,,;;GEEWWWWWWWWKKKKWKKWKKKKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWEDi;j;............................,Li;L;;;..,;;f........................KKKKWWWKDDWWKKDDDGDKKGGGGGGGGDGDDDDDDDDDDDDDDD * *fjt. .:,,,:::::,,,,,,,;;;EEKWWWWWWWKKKKKKWKKKKKKKKKKKKKKKKKKKKKWKKKWKW##W#KKKKWEti;;G;........................tEEEL;;;;;;;;;;L..........................DKKKKKEDDWWKEDGftiLE;;;;itjLGGGGGGDDDDDDDDDDD * *fjt. .j::::,,,,,,,;;;DEEWWWWWWWWKKKKWKKKKKKKKKKKKKKKKKKKKKKKWKKWWWK##W#KKKKKEii;;;L;...................iDEEEEEEKKi;j;;;;jD.....:......................,KKKKDGGEKKE:::::;E::::::::::,tLGGDDDDDDDDDD * *fjt. .;:::,,,,,,,;;;;EEKWWWWWWWWKWKKKKKKKKKKKKKKKWKKKKKKKKKKWKKWWWW#WW#KKKKKKii;;;;f;.............:tDEEEEEKKKKKKKKEti;;;L...............................EEKf;:iKKE::::::E::::::::::::::ifDDDDDDDDD * *fjt: :::,,,,,,,,;;;DEEWWWWWWWWWEKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####KKKKKEiii;;;;f,.........iDEEEEKKKKKKKKKKKKKKKf;iG......i..........................fK::::KKE::::::E::::::::::::::::,tGGDDDDD * *fjt: t:::,,,,,,;;;;iDEKWWWWWWKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####WKKKKLiii;;;;;L,....,Li;EDEEEEKKKKKKKKKKKKKKKKiG......;:...........................:i:::KKE:::::,E,::::::::::::::::::iGDDDD * *jjt. f::,,,,,,,;;;;GEEWWWWKEEKEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKWWWWW###WWKKKKiii;;;;;;;G,;L;;iiEEEEEEEKKKKKKKKKKKKKWWKE......;t.........:....................j::KEE:,::,,D,,::::,,,,,,:::::::::tDDD * *fjt:. ,::,,,,,,,;;;;EEWWKEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKKWWWWW#W#KKKKKKiiiiii;;;;;i;;iiiEEKEEKKWKKKKKKKWKKKKKWWWGi;...;t......,;;;;,....................:,EEE,,,,,,D,,,,,,,,,,,,,,,,::,::::tG * *fjt:. ,::,,,,,,,;;;;DEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWW#W#KKKKKKiiiii;;i;;;;;iiiKKKEKKKKWWKWWWWWWKKKKWWWWW;;;:;L.....;;;;;;;;;....................,KEE,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,; * *fjt:. f:,,,,,,,;;;;jEDEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWW#W##KKKKKKiiiiiiii;i;;iiiEKKKKKKKKWKWWWWWWWWKKKWWWWWKi;;i.....,jEEfGi;;;;;...................EED,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,, * *fjt:. .f::,,,,,,;;jEEDEEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWKWWWWW###KKKKKLiiiiiiiiiiiiiiEEKKKKKKKKWWWWWWWWWWWWKWWWWWWGi;i;,..;jDDDKEGi;;;;;;:................EED,,,,,,D,,,,,,,,,,,,,,,,,,,,,,,,, * *fjt:. .. ;::,,,,,;;EDDEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKW#WW####KWKKKiiiiiiiiiiiiijKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWt;i;;;;i;DDDDDDGi;;;;;;;;:.............EDf;,,,;,G;;;;;;;;;;;;;;;,,,,,,,,,, * *fjt:......:,,,,,,;LDDDEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWWKWWWW####KKKKKiiiiiiiiiiijKEKKWKKKKKKKWWWWWWWWWWWWWWWWWWWWWWiLiii;i;DEEEEDDE;i;;;;;;;;;:..........EDi,;;;;;L;;;;;;;;;;;;;;;;;;,,,,,,, * *fjt:......:,,,,,;EDDDEEKEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKWWWWKKWWW##W#KWKKWEiiiiiijGKKKKKWWKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKi;iiiiDDEEEEEEDEi;;;;;;;;;;;;;,:.....ED;;;;;;;j;;;;;;;;;;;;;;;;;;;;;;;,, * *fjt:.....t,,,,,;DDDDEEEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKWWWW##WKWKKWKiiiKKKKKKKKKWWKKKKKKKKWWWWWWWWWWWWWWW#WWWWWWWWWiiiiifLEEEEEEEEDi;i;;;;;;;;;;;;.....DD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; * *fjt:.....G,,,,,GDDDEEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKKWWWKKKWWW###WKWKKWKitKKKKKKKKKWKKKKKKKKKKWWWWWWWWWWWWWW###WWWWWWWWEiiiiiiiEEEEEEEEDGiiii;;;;;;;;;.....GD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; * *fjt:.....L,,,,;GDDDEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWDGWWW###KKWWKWKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWW####WWWWWWWWWiiiiiiiiEEEEEEEEEEDi;i;;;;;;;;.....Lj;;;;;;i;iiiiii;;;;;;ii;;;;;;;;;;; * ***********************************************************************************************************************************************************************************************************/ class Edge { int to; long a,b; Edge(int to, long a, long b){ this.to=to; this.a=a; this.b=b; } } void sort(int a[]) { ArrayList<Integer> list=new ArrayList<>(); for(int x: a) list.add(x); Collections.sort(list); for(int i=0;i<a.length;i++) a[i]=list.get(i); } void sort(long a[]) { ArrayList<Long> list=new ArrayList<>(); for(long x: a) list.add(x); Collections.sort(list); for(int i=0;i<a.length;i++) a[i]=list.get(i); } void ruffleSort(int a[]) { Random rand=new Random(); int n=a.length; for(int i=0;i<n;i++) { int j=rand.nextInt(n); int temp=a[i]; a[i]=a[j]; a[j]=temp; } Arrays.sort(a); } void ruffleSort(long a[]) { Random rand=new Random(); int n=a.length; for(int i=0;i<n;i++) { int j=rand.nextInt(n); long temp=a[i]; a[i]=a[j]; a[j]=temp; } Arrays.sort(a); } static long gcd(long a, long b) { return b==0?a:gcd(b,a%b); } long fact[]; long invFact[]; void init(int n) { fact=new long[n+1]; invFact=new long[n+1]; fact[0]=1; for(int i=1;i<=n;i++) { fact[i]=mul(i,fact[i-1]); } invFact[n]=power(fact[n],mod-2); for(int i=n-1;i>=0;i--) { invFact[i]=mul(invFact[i+1],i+1); } } long nCr(int n, int r) { if(n<r || r<0) return 0; return mul(fact[n],mul(invFact[r],invFact[n-r])); } long add(long a, long b) { return (a+b)%mod; } long mul(long a, long b) { return a*b%mod; } long power(long x, long y) { long gg=1; while(y>0) { if(y%2==1) gg=mul(gg,x); x=mul(x,x); y/=2; } return gg; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st==null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String s=""; try { s=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } // Functions static int gcd(int a, int b) { return b==0?a:gcd(b,a%b); } void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) out.print(a[i]+" "); out.println(); } void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) out.print(a[i]+" "); } //Input Arrays static void input(long a[], int n) { for(int i=0;i<n;i++) { a[i]=inp.nextLong(); } } static void input(int a[], int n) { for(int i=0;i<n;i++) { a[i]=inp.nextInt(); } } static void input(String s[],int n) { for(int i=0;i<n;i++) { s[i]=inp.next(); } } static void input(int a[][], int n, int m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=inp.nextInt(); } } } static void input(long a[][], int n, int m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { a[i][j]=inp.nextLong(); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
13034a22d7c63715be28c7a5cee9c47b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class ACMP { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); System.out.println(n); for (int i = 1; i <= n; i++) { StringBuilder builder = new StringBuilder(i + " "); for (int j = 1; j <= n; j++) { if (j == i) continue; builder.append(j).append(" "); } System.out.println(builder); } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
8ea0c06bce9b8ea64d2ce8b3f9a12b08
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.PrintWriter; public class PermutationChain { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int tc = sc.nextInt(); PrintWriter out = new PrintWriter(System.out); int[] arr = new int[101]; while(tc != 0){ tc--; int n = sc.nextInt(); for(int i = 0; i < n ; i++){ arr[i] = i+1; } int l = n-2; out.println(n); for(int i = 0; i < n; i++){ out.print(arr[i] + " "); } out.println(); while(l >= 0){ int temp = arr[l]; arr[l] = arr[l+1]; arr[l+1] = temp; l--; for(int i = 0; i < n; i++){ out.print(arr[i] + " "); } out.println(); } out.flush(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
a2cc062df9bdb83840a1f8ec2e973baa
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import javax.print.DocFlavor.STRING; import java.io.*; import java.lang.Math; public class cp{ public static PrintWriter out; public static FastReader sc; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static void sort(int[] a,boolean flag){ ArrayList<Integer> temp=new ArrayList<>(); for(int n:a) temp.add(n); Collections.sort(temp); if(flag){ for(int i=0;i<a.length;i++){ a[i]=temp.get(i); } } else{ for(int i=a.length-1;i>=0;i--){ a[i]=temp.get(i); } } } public static int nod(int n){ return (int)Math.log10((double)n)+1; } public static int gcd(int a,int b){ if (b == 0) return a; else return gcd(b, a % b); } public static void printarr(int[] a){ for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printlist(List<Integer> a){ for(int i:a){ out.print(i+" "); } out.println(); } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static HashMap<Integer, Integer> sortMap(HashMap<Integer, Integer> hm) { List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1,Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static HashMap<Integer,Integer> mapFreq(int[] a){ HashMap<Integer,Integer> map=new HashMap<>(); for(int i=0;i<a.length;i++){ map.put(a[i],map.getOrDefault(a[i], 0)+1); } return map; } public static void solve(){ // int n=sc.nextInt(); // int[] a=new int[n]; // for(int i=0;i<n;i++){ // a[i]=sc.nextInt(); // } int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=i+1; out.println(n); for(int i=n-1;i>=1;i--){ printarr(a); int temp=a[i]; a[i]=a[i-1]; a[i-1]=temp; } printarr(a); } public static void main(String[] args) throws IOException { sc = new FastReader(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t=sc.nextInt(); // int t=1; while (t-->0){ solve(); } out.close(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
102a9219cde4bd76364a1377c69efdbc
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.io.PrintWriter; import java.util.*; public class PermutationChain { public static void main(String[] args) { int t,n; Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); t=sc.nextInt(); for (int g=0; g<t; g++){ n=sc.nextInt(); out.println(n); int[] arr; arr=new int[n]; for (int h=0; h<n; h++){ arr[h]=h+1; out.print(arr[h]+" "); } out.println(); for (int q=1; q<n-1; q++){ int w=arr[q-1]; arr[q-1]=arr[n-1]; arr[n-1]=w; for (int s=0; s<n; s++){ out.print(arr[s]+" "); } out.println(); } int e=arr[n-2]; arr[n-2]=arr[n-1]; arr[n-1]=e; for (int p=0; p<n; p++){ out.print(arr[p]+" "); } out.println(); } out.flush(); } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
df3aa5bc0936a9623855537d0006b13b
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.PrintWriter; public class MyClass { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int tc = sc.nextInt(); PrintWriter out = new PrintWriter(System.out); int[] arr = new int[101]; while(tc != 0){ tc--; int n = sc.nextInt(); for(int i = 0; i < n ; i++){ arr[i] = i+1; } int l = n-2; out.println(n); for(int i = 0; i < n; i++){ out.print(arr[i] + " "); } out.println(); while(l >= 0){ int temp = arr[l]; arr[l] = arr[l+1]; arr[l+1] = temp; l--; for(int i = 0; i < n; i++){ out.print(arr[i] + " "); } out.println(); } out.flush(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
1e28e35d8d814b5c318a6eff3fb1fff1
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); System.out.println(n); int[] arr = new int[n+1]; StringBuilder sb = new StringBuilder(); for(int i=1;i<=n;i++) { arr[i] = i; sb.append(i).append(" "); // System.out.print(i +" "); } System.out.println(sb.toString()); sb.setLength(0); arr[1] = n; arr[n] = 1; int k = n-1; int j = 1; while(k-->0) { for(int i=1;i<=n;i++) sb.append(arr[i]).append(" "); // System.out.print(arr[i] +" "); System.out.println(sb.toString()); sb.setLength(0); arr[j] = arr[j+1]; arr[j+1] = n; j++; } } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
6b3b1148da82b4aeab53fe2fc4f0147e
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import java.util.*; import java.io.*; public class Main{ public static int[] array(BufferedReader br,int n) throws IOException{ String [] values = br.readLine().split(" "); int [] arr = new int[n]; for(int i =0; i<n; i++){ arr[i] = Integer.parseInt(values[i]); } return arr; } public static void swap(int[] arr, int x, int y){ int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); int [] arr = new int[n]; StringBuilder sb = new StringBuilder(); for(int i = 0; i<n; i++){ arr[i] = i+1; } // System.out.println(n); sb.append(n +"\n"); for(int i = 0; i<n; i++){ //System.out.print(arr[i]+ " "); sb.append(arr[i] + " "); } //System.out.println(); sb.append("\n"); for(int i = 0; i<n-1; i++){ swap(arr,i,n-1); for(int j = 0; j<n; j++){ // System.out.print(arr[j]+ " "); sb.append(arr[j] + " "); } // System.out.println(); sb.append("\n"); } System.out.print(sb); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
5f68d784421d70cdc5d46f9d85df3d76
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
/**/ import java.io.*; import java.util.*; import java.lang.*; public class PermutationChain { public static void main(String[] args) throws IOException{ FastReader s = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = s.nextInt(); while(t-->0){ int n = s.nextInt(); int cur = 0; out.println(n); for(int i = 0; i < n; i++){ StringBuilder sb = new StringBuilder(); for(int j = 0; j < n - 1; j++){ if(j == cur){ sb.append(1); sb.append(' '); } sb.append(j + 2); sb.append(' '); } cur++; if(i == n - 1){ sb.append(1); } out.println(sb); } } out.flush(); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; if (st.hasMoreTokens()) { str = st.nextToken("\n"); } else { str = br.readLine(); } return str; } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output
PASSED
45e90878a7e805a351f71712928286c2
train_109.jsonl
1659623700
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.
256 megabytes
import static java.lang.System.out; import static java.lang.Math.abs; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.log; import java.util.*; import java.lang.*; import java.io.*; public class a_Codeforces { public static void main(String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); FastWriter out = new FastWriter(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); out.println(n); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = i + 1; out.print((i + 1) + " "); } out.println(""); int i = 0, j = n - 1; while (i < j) { swap(arr, i, j); i++; for (int x : arr) { out.print(x + " "); } out.println(""); } } out.close(); } static void no() { out.println("NO"); } static void yes() { out.println("YES"); } static void print(int arr[]) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(""); } static int[] input(int n) { FastReader sc = new FastReader(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } return arr; } static int[] swap(int arr[], int a, int b) { int temp = arr[b]; arr[b] = arr[a]; arr[a] = temp; return arr; } /* * int[] arr = new int[n]; * for (int i=0; i<n; i++){ * arr[i] = sc.nextInt(); * } */ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } }
Java
["2\n\n2\n\n3"]
2 seconds
["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"]
null
Java 17
standard input
[ "constructive algorithms", "math" ]
02bdd12987af6e666d4283f075f73725
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required length of permutations in the chain.
800
For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
standard output