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 | 26ff77284a0176ea3b8aac49ec41ac18 | 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.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author xpeng
*/
public class codeforces {
// Working program using Reader Class
public static void print(int[] arr){
for (int i = 0; i < arr.length; i++) {
out.print(arr[i]+" ");
}
out.println();
}
static Scanner in = new Scanner(System.in);
static BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out));
static PrintWriter out = new PrintWriter(write);
public static void main(String[] args) {
//Scanner scanner = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j]=j+1;
}
out.println(n);
print(arr);
for (int j = 0; j < n-1; j++) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
print(arr);
}
}
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 8 | 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 | 793ef3a8a03426b328b79f8fae2dfc9d | 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 {
public static void main(String[] args) {
new Thread(null, () -> new Main().run(), "1", 1 << 23).start();
}
private void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Solution solve = new Solution();
int t = scan.nextInt();
//int t = 1;
for (int qq = 0; qq < t; qq++) {
solve.solve(scan, out);
out.println();
}
out.close();
}
}
class Solution {
/*
* think and coding
*/
double EPS = 0.000_0001;
public void solve(FastReader scan, PrintWriter out) {
int n = scan.nextInt();
out.println(n);
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
int k = n - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
out.print(arr[j] + " ");
}
if (k > 0) {
int temp = arr[k];
arr[k] = arr[k - 1];
arr[k - 1] = temp;
k--;
out.println();
}
}
}
static class Pair implements Comparable<Pair> {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public Pair(Pair p) {
this.a = p.a;
this.b = p.b;
}
@Override
public int compareTo(Pair p) {
return Integer.compare(a, p.a);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public String toString() {
return "Pair{" +
"a=" + a +
", b=" + b +
'}';
}
}
}
class FastReader {
private final BufferedReader br;
private StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
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 8 | 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 | 39bc29c38c5ed2b57d84ef95a0bbd4ec | 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 IOException{
Scanner in=new Scanner(System.in);
PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
int T=in.nextInt();
int arr[]=new int [1000010];
while(T>0){
int n=in.nextInt();
System.out.println(n);
for(int i=1;i<n+1;i++){
arr[i]=i;
pr.print(i+" ");
}
pr.println();
for(int v=2;v<=n;v++){
int temp=arr[1];
arr[1]=arr[v];
arr[v]=temp;
for(int i=1;i<=n;i++){
pr.print(arr[i]+" ");
}
pr.println();
}
T--;
pr.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 8 | 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 | 8ea179a7c00c3575a54d75ed5f569b47 | 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 | /******************************************************************************
Practice,Practice and Practice....!!
*******************************************************************************/
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.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();
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=in.nextInt();
while(testCases-- > 0){
int n=in.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++)
out.print(arr[i]+" ");
out.println("");
for(int i=1;i<n;i++){
int temp=arr[n-i-1];
arr[n-i-1]=n;
arr[n-i]=temp;
for(int j=0;j<n;j++){
out.print(arr[j]+" ");
}
out.println("");
}
}
out.close();
} 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 8 | 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 | 64de16c4a91f296a65818d743426ae85 | 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 Solution {
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr) {
StringBuilder sb = new StringBuilder();
for(int a: arr)
sb.append(a).append(" ");
System.out.println(sb.toString().trim());
}
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0) {
int n = scan.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++) {
swap(arr, n-1, n-i-1);
print(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 8 | 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 | 252a101ae156ee59d5221adcd193128f | 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.Scanner;
public class Test {
private static PrintWriter out = new PrintWriter(System.out);
private static void print(int[] a) {
for(int num : a)out.print(num + " ");
out.println();
}
private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static void helper(int n) {
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--) {
print(a);
if(i > 0)swap(a, i - 1, i);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
helper(n);
}
sc.close();
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 8 | 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 | ef11b7826b39c456679f79899f9eca74 | 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 Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder res = new StringBuilder();
while (t-- > 0) {
int N = sc.nextInt();
int[] arr = new int[N];
res.append(N + "\n");
for (int i=0; i<N; i++) {
arr[i] = i+1;
}
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
res.append(arr[j] + " ");
}
res.append("\n");
int temp = arr[i];
arr[i] = arr[N-1];
arr[N-1] = temp;
}
}
System.out.println(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 8 | 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 | f6ee5ed1adb5a13f3fbb6facad13f191 | 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 | /**
* @Jai_Bajrang_Bali
* @Har_Har_Mahadev
*/
/* Author: Sanat Kumar Dubey (sanat04)
* File: B.java
*/
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class B {
static void swap(int a[],int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder res=new StringBuilder();
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;
}
res.append(n).append("\n");
for(int i:arr) res.append(i+" ");
res.append("\n");
swap(arr,0,n-1);
for(int i:arr) res.append(i+" ");
res.append("\n");
int j=0,k=1;
for (int i = 2; i <n ; i++) {
swap(arr,j++,k++);
for(int l:arr) res.append(l+" ");
res.append("\n");
}
}
System.out.println(res.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 8 | 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 | a1645b4cd6a4711c5c421debbff63ce8 | 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.Scanner;
import java.util.StringTokenizer;
public class forCP {
public static void main(String[] args) throws IOException {
FastReader scn = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int tc = scn.nextInt();
for (int i = 0; i < tc; i++) {
int n = scn.nextInt();
System.out.println(n);
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = j + 1;
}
for (int y = 0; y < n; y++) {
out.write(arr[y] + " ");
}
// System.out.println();
out.write("\n");
swaparr(arr, 0, n - 1);
for (int y = 0; y < n; y++) {
out.write(arr[y] + " ");
}
// System.out.println();
out.write("\n");
int fix = n - 2;
int idx = 1;
while (fix != 0) {
swaparr(arr, 0, idx);
for (int y = 0; y < n; y++) {
out.write(arr[y] + " ");
}
// System.out.println();
out.write("\n");
idx++;
fix--;
}
out.flush();
}
}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------
public static void swaparr(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = 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 {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
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 8 | 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 | f27869f4f30d9e6e514bbfefcb48db75 | 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.Arrays;
import java.util.StringTokenizer;
/**
* B. Permutation Chain
*/
public class Main {
static class FastReader {
BufferedReader reader;
StringTokenizer tokenizer;
FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
// reads in the next string
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
// reads in the next int
int nextInt() {
return Integer.parseInt(next());
}
// reads in the next long
long nextLong() {
return Long.parseLong(next());
}
// reads in the next double
double nextDouble() {
return Double.parseDouble(next());
}
void close() {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static FastReader in = new FastReader(System.in);
private static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
private static final int MAXN = 110;
private static int[] q = new int[MAXN];
private static void swap(int x, int y) {
int t = q[x];
q[x] = q[y];
q[y] = t;
}
public static void main(String[] args) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
for (int i = 1; i <= n; i++) q[i] = i;
out.println(n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) out.print(q[j] + " ");
out.println();
swap(i, i + 1);
}
}
out.flush();
out.close();
in.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 8 | 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 | e702786adb03604fe26df9554ad9f10c | 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 cp{
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t-->0){
String[] str=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
// int x=Integer.parseInt(str[1]);
// int y=Integer.parseInt(str[2]);
// String[] str1=br.readLine().split(" ");
int[] arr=new int[n];
pw.println(n);
for(int i=0;i<n;i++){
arr[i]=i+1;
pw.print((i+1)+" ");
}
pw.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++){
pw.print(arr[j]+" ");
}
pw.println();
}
}
pw.flush();
}
//****************************function to find all factor*************************************************
public static ArrayList<Long> findAllFactors(long num){
ArrayList<Long> factors = new ArrayList<Long>();
for(long i = 1; i <= num/i; ++i) {
if(num % i == 0) {
//if i is a factor, num/i is also a factor
factors.add(i);
factors.add(num/i);
}
}
//sort the factors
Collections.sort(factors);
return factors;
}
//*************************** function to find GCD of two number*******************************************
public static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%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 8 | 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 | 5f4cae042c047203d44acd3fcffb7ed4 | 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 whatever //do not write package name here */
import java.util.*;
import java.io.*;
public class Main {
public static void main (String[] args) throws IOException{
//Scanner sc = new Scanner(System.in);
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());
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = i+1;
}
int k = n-1;
int l = n;
bw.write(n+"\n");
for(int j=1;j<=n;j++){
for(int i=0;i<n;i++){
bw.write(arr[i]+" ");
}
bw.write("\n");
if(k-1<0)
break;
int tm = arr[k];
arr[k] = arr[k-1];
arr[k-1] = tm;
k--;
}
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 8 | 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 | 5333ba86ad7c4300b6a69082c9155a9f | 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.lang.reflect.Parameter;
import java.math.BigInteger;
import java.util.*;
public class Habd {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.nextInt();
while(tc-->0){
int n = sc.nextInt();
pw.println(n);
int[] ans = new int[n]; for(int i = 0; i<n; i++)pw.print((ans[i] = i + 1) + " ");
pw.println();
int i = 0;
n--;
while(n-->0){
int temp = ans[i];
ans[i] = ans[i + 1];
ans[i + 1] = temp;
i++;
for(int x: ans)pw.print(x + " ");
pw.println();
}
}
pw.flush();
}
static class SegmentTree{
long[] tree;
int N;
public SegmentTree(long[] arr){
N = arr.length;
tree = new long[2*N - 1];
build(tree, arr);
}
public void build(long[] tree, long[] arr){
for(int i = N-1, j = 0; i<tree.length; i++, j++)tree[i] = arr[j];
for(int i = tree.length - 1, j = i - 1, k = N-2; k>=0; i -= 2, j-= 2, k--){
tree[k] = tree[i] + tree[j];
}
}
public void update(int idx, int val){
tree[idx + N - 2] = val;
boolean f = true;
int i = idx + N - 2;
int j = i - 1;
if(i % 2 != 0){
i++;
j++;
}
for(int k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); k>=0; ){
tree[k] = tree[i] + tree[j];
f = !f;
i = k;
j = k - 1;
if(k % 2 != 0){
i++;
j++;
}
k = (tree.length - N - 1) - ((tree.length - 1 - i)/2);
}
}
}
public static boolean isSorted(int[] arr){
boolean f = true;
for(int i = 1; i<arr.length; i++){
if(arr[i] < arr[i - 1]){
f = false;
break;
}
}
return f;
}
public static int binary_Search(long key, long[] arr){
int low = 0;
int high = arr.length;
int mid = (low + high) / 2;
while(low <= high){
mid = low + (high - low) / 2;
if(arr[mid] == key)break;
else if(arr[mid] > key) high = mid - 1;
else low = mid + 1;
}
return mid;
}
public static int differences(int n, int test){
int changes = 0;
while(test > 0){
if(test % 10 != n % 10)changes++;
test/=10;
n/=10;
}
return changes;
}
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return start;
}
static int maxSubArraySum2(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return end;
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair x){
if(this.x == x.x) return this.y - x.y;
else return this.x - x.x;
}
public String toString(){
return "("+this.x + ", " + this.y + ")";
}
}
public static class Tuple implements Comparable<Tuple>{
int x;
int y;
int z;
public Tuple(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
public int compareTo(Tuple x){
if(this.x == x.x) return x.y - this.y;
else return this.x - x.x;
}
public String toString(){
return "("+this.x + ", " + this.y + "," + this.z + ")";
}
}
public static boolean isSubsequence(char[] arr, String s){
boolean ans = false;
for(int i = 0, j = 0; i<arr.length; i++){
if(arr[i] == s.charAt(j)){
j++;
}
if(j == s.length()){
ans = true;
break;
}
}
return ans;
}
public static void sortIdx(long[]a,long[]idx) {
mergesortidx(a, idx, 0, a.length-1);
}
static void mergesortidx(long[] arr,long[]idx,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesortidx(arr,idx,b,m);
mergesortidx(arr,idx,m+1,e);
mergeidx(arr,idx,b,m,e);
}
return;
}
static void mergeidx(long[] arr,long[]idx,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
long[] l=new long[len1];
long[] lidx=new long[len1];
long[] r=new long[len2];
long[] ridx=new long[len2];
for(int i=0;i<len1;i++) {
l[i]=arr[b+i];
lidx[i]=idx[b+i];
}
for(int i=0;i<len2;i++) {
r[i]=arr[m+1+i];
ridx[i]=idx[m+1+i];
}
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
arr[k++]=l[i++];
idx[k-1]=lidx[i-1];
}
else {
arr[k++]=r[j++];
idx[k-1]=ridx[j-1];
}
}
while(i<len1) {
idx[k]=lidx[i];
arr[k++]=l[i++];
}
while(j<len2) {
idx[k]=ridx[j];
arr[k++]=r[j++];
}
return;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | 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 8 | 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 | 971d2c3626624ec506774da959ba98a3 | 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 {
FastReader in = new FastReader();
int numCases = in.nextInt();
StringBuilder ans = new StringBuilder();
while(numCases-->0)
{
int n = in.nextInt();
int [] arr = new int[n];
for (int i = 0; i<n;i++)
{
arr[i] = i+1;
}
ans.append(n+"\n");
for (int i = 0; i<n;i++)
{
ans.append(arr[i]+" ");
}
ans.append("\n");
for (int a = 1; a<n;a++)
{
int hold = arr[a-1];
arr[a-1] = arr[a];
arr[a] = hold;
for (int i = 0; i<n;i++)
{
ans.append(arr[i]+" ");
}
ans.append("\n");
}
}
System.out.println(ans);
}
static class FastReader {
StringTokenizer st;
BufferedReader br;
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 = "";
while (st == null || st.hasMoreElements()) {
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
return 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 8 | 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 | 9422e267544da2fd17b28b364b39ab27 | 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 {
static Scanner sc;
static PrintWriter pw;
static int mod = 1000000007;
public static void main(String[] args) throws IOException, InterruptedException {
sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
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;
pw.println(n);
print(arr);
for (int i = 1; i < n; i++) {
int tmp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = tmp;
print(arr);
}
}
pw.close();
}
public static void print(int[] arr) {
for (int x : arr)
pw.print(x + " ");
pw.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 8 | 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 | 5f56a066d05f85b456ed0ff62becaecd | 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 {
static List<List<Integer>> perms = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int test = 0; test < t; test++) {
perms.clear();
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=1; i<=n; i++) {
arr[i-1] = i;
}
int l = 0;
int r = n-1;
System.out.println(n);
StringBuilder s = new StringBuilder("");
for(int i=0; i<n; i++) {
s.append(arr[i]).append((i < n - 1) ? " " : "");
}
System.out.println(s);
int extra = 0;
while(l <= r) {
int temp = arr[r];
arr[r] = arr[l];
arr[l] = temp;
l++;
extra++;
StringBuilder sb = new StringBuilder("");
if(extra < n) {
for(int i=0; i<n; i++) {
sb.append(arr[i]).append((i < n - 1) ? " " : "");
}
System.out.println(sb.toString());
}
}
}
}
public static void heapsAlgo(int k, int[] A) {
if( k == 1) {
List<Integer> list = new ArrayList<>();
for(int i=0; i<A.length; i++) {
list.add(A[i]);
}
perms.add(list);
} else {
heapsAlgo(k-1, A);
for(int i=0; i<k-1; i++) {
if(k % 2 == 0) {
int temp = A[i];
A[i] = A[k-1];
A[k-1] = temp;
} else {
int temp = A[0];
A[0] = A[k-1];
A[k-1] = temp;
}
heapsAlgo(k-1, A);
}
}
}
}
/*
for(int test = 0; test < t; test++) {
int n = sc.nextInt();
if(n == 1) {
System.out.println(2);
continue;
}
int x = 1000000001;
int[] dp = new int[n+1];
dp[0] = 0;
dp[1] = 2;
dp[2] = 1;
dp[3] = 1;
for(int i=4; i<=n; i++) {
dp[i] = Math.min(dp[i-2], dp[i-3]) + 1;
}
System.out.println(dp[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 8 | 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 | bfed50acb6f8938f05e9c986a98a67fb | 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 permchain {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
String[] perms = new String[100];
for (int i = 1; i <= 100; i++){
int start = 2;
perms[i - 1] = "";
for (int j = 0; j < i; j++){
perms[i-1] += start + " ";
start++;
}
perms[i - 1] += 1;
}
for (int i = 0; i < t; i++){
int n = scan.nextInt();
String end = "";
for (int j = 0; j < n; j++){
end += (j + 1) + " ";
}
System.out.println(n);
System.out.println(end);
end = end.substring(3);
for (int j = 0; j < n - 1; j++){
end = end.substring(end.indexOf(" ",end.indexOf(" ")) + 1);
System.out.print(perms[j] + " " + end);
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 8 | 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 | cd1348424ec22acbbc08e0d107c30cd5 | 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 div_4_806_a {
public static void main(String args[]){
FScanner in = new FScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while(t-->0) {
int n=in.nextInt();
out.println(n);
for(int i=1;i<=n;i++)
out.print(i+" ");
out.println();
for(int i=1;i<n;i++){
for(int j=1;j<=n-1-i;j++)
out.print(j+" ");
out.print(n+" ");
for(int j=n-i;j<n;j++)
out.print(j+" ");
out.println();
}
}
out.close();
}
static class FScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sb = new StringTokenizer("");
String next(){
while(!sb.hasMoreTokens()){
try{
sb = new StringTokenizer(br.readLine());
} catch(IOException e){ }
}
return sb.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
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] = nextInt();
return a;
}
float nextFloat(){
return Float.parseFloat(next());
}
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 8 | 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 | f819490d4fc38c4f0d1e0804173b216b | 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.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.valueOf(in.readLine());
while (t>0) {
int n=Integer.valueOf(in.readLine());
System.out.println(n);
int[] ans=new int[n];
for (int i = 0; i < n; i++) {
ans[i]=i+1;
}
for (int i = 0; i < ans.length; i++) {
print(ans);
swap(i,n-1,ans);
}
t--;
}
in.close();
}
private static void swap(int i, int j, int[] ans) {
int t=ans[j];
ans[j]=ans[i];
ans[i]=t;
}
private static void print(int[] ans) {
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n=ans.length-1;
for (int i = 0; i < n; i++) {
out.print(ans[i]+" ");
}
out.println(ans[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 8 | 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 | a69f0dc8bd3dc78b5e7cdcb136f36ec2 | 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.math.BigInteger;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0){
int n = in.nextInt();
System.out.println(n);
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = i + 1;
}
for (int i = 0; i < n - 1; i++) {
printArray(ans);
int temp = ans[i];
ans[i] = ans[i + 1];
ans[i + 1] = temp;
}
printArray(ans);
}
}
static void printArray(int[] a){
PrintWriter out = new PrintWriter(System.out);
for (int i = 0; i < a.length; i++) {
out.print(a[i] + " ");
}
out.println();
out.flush();
}
static void sortLargeNumbers(String arr[]) {
Arrays.sort(arr, (left, right) -> {
if (left.length() != right.length())
return left.length() - right.length();
return left.compareTo(right);
});
}
static int max_in_array(int[] a){
int max = 0;
for (int i = 0; i < a.length; i++) {
max = Math.max(max, a[i]);
}
return max;
}
static int min_in_array(int[] a){
int min = Integer.MAX_VALUE;
for (int i = 0; i < a.length; i++) {
min = Math.min(min, a[i]);
}
return min;
}
static int sum_in_array(int[] n){
int sum = 0;
for (int i = 0; i < n.length; i++) {
sum += n[i];
}
return sum;
}
static double distance_between_two_points(double x2, double x1, double y2, double y1){
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
static double distance_vector(int x, int y, int z){
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int number1, int number2) {
if (number1 == 0 || number2 == 0)
return 0;
else {
int gcd = gcd(number1, number2);
return Math.abs(number1 * number2) / gcd;
}
}
} | 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 8 | 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 | cbd7ac6ca5f3980f50f90b0995f3c30b | 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.text.*;
import java.math.*;
import java.util.regex.*;
public class JaiShreeRam{
static Scanner in=new Scanner();
static long systemTime;
static long mod = 1000000007;
static ArrayList<ArrayList<Integer>> adj;
static int seive[]=new int[1000001];
static long C[][];
static PrintWriter pw;
public static void main(String[] args) throws Exception{
int z=in.readInt();
pw = new PrintWriter(System.out);
for(int test=1;test<=z;test++) {
//setTime();
solve();
//printTime();
//printMemory();
}
pw.close();
}
static void solve() {
// int n=in.readInt();
// long a[][]=new long[2][n];
// for(int i=0;i<n;i++) {
// a[0][i]=in.readInt();
// }
// for(int i=0;i<n;i++) {
// a[1][i]=in.readInt();
// }
// a[0][0]=-1;
// long p[][]=new long[2][n];
//
int n=in.readInt();
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=i+1;
}
pw.println(n);
print(a);
int j=n-1;
int k=n-1;
while(k>0) {
int tmp=a[j];
a[j]=a[j-1];
a[j-1]=tmp;
j--;
k--;
print(a);
}
}
static long pow(long n, long m) {
if(m==0)
return 1;
else if(m==1)
return n;
else {
long r=pow(n,m/2);
if(m%2==0)
return (r*r);
else
return (r*r*n);
}
}
static long maxsumsub(ArrayList<Long> al) {
long max=0;
long sum=0;
for(int i=0;i<al.size();i++) {
sum+=al.get(i);
if(sum<0) {
sum=0;
}
max=Math.max(max,sum);
}
return max;
}
static long abs(long a) {
return Math.abs(a);
}
static void ncr(int n, int k){
C= new long[n + 1][k + 1];
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
}
static boolean isPalin(String s) {
int i=0,j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
static int knapsack(int W, int wt[],int val[], int n){
int []dp = new int[W + 1];
for (int i = 1; i < n + 1; i++) {
for (int w = W; w >= 0; w--) {
if (wt[i - 1] <= w) {
dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]);
}
}
}
return dp[W];
}
static void seive() {
Arrays.fill(seive, 1);
seive[0]=0;
seive[1]=0;
for(int i=2;i*i<1000001;i++) {
if(seive[i]==1) {
for(int j=i*i;j<1000001;j+=i) {
if(seive[j]==1) {
seive[j]=0;
}
}
}
}
}
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);
}
static void sort(long[] a) {
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 int[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long[] nla1(int n){
long[] arr= new long[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static Integer[] nIa(int n){
Integer[] arr= new Integer[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static Long[] nLa(int n){
Long[] arr= new Long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static void no() {
System.out.println("NO");
}
static void yes() {
System.out.println("YES");
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
pw.print(i+" ");
}
pw.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void setTime() {
systemTime = System.currentTimeMillis();
}
static void printTime() {
System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
}
static void printMemory() {
System.err.println("Memory consumed: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
} | 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 8 | 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 | a5e0f7b5804cd10c282d1fedbe9184aa | 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;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Codechef
{
public static void main(String[] args)
{
FastReader sc = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int test;
test = sc.nextInt();
while(test-->0) {
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i+1;
}
pw.println(n);
int ind = 0;
while (n-->0){
for (int num:arr) {
pw.print(num+" ");
}
pw.println("");
if(n!=0)
{
swap(arr,ind,arr.length-1);
}
ind++;
}
}
pw.close();
}
public static ArrayList<Long> fac(long n)
{
ArrayList<Long> f = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if(n%i==0)
{
if (n/i==i)
f.add(i);
else
{
f.add(i);
f.add(n/i);
}
}
}
Collections.sort(f);
return f;
}
public static void swap(int[] arr,int a,int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static int gcd(int n1, int n2)
{
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
//Pair Class
public static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return this.x - o.x;
}
}
/*
FASTREADER
*/
public 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;
}
/*DEFINED BY ME*/
//READING ARRAY
int[] readArray(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
//COLLECTIONS SORT
void sort(int arr[]) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : arr)
l.add(i);
Collections.sort(l);
for (int i = 0; i < arr.length; i++)
arr[i] = l.get(i);
}
//EUCLID'S GCD
int gcd(int a, int b) {
if (b != 0)
return gcd(b, a % b);
else
return a;
}
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[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 8 | 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 | d1f9f2e9ce92fe7690e874c3d8f174eb | 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.util.StringTokenizer;
public class Permutation_Chain
{
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 PrintWriter out;
public static void main(String Args[]) throws java.lang.Exception
{
out = new PrintWriter(System.out);
try
{
Reader obj = new Reader();
int t = obj.nextInt();
while (t > 0)
{
t--;
int n = obj.nextInt();
int arr[] = new int[n];
int i, j;
out.println (n);
for (i=0;i<n;i++)
{
arr[i] = i + 1;
out.print (arr[i] + " ");
}
out.println ();
for (i=0;i<n-1;i++)
{
int num = arr[i];
arr[i] = arr[i+1];
arr[i+1] = num;
for (j=0;j<n;j++)
{
out.print (arr[j] + " ");
}
out.println ();
}
}
}catch(Exception e){
return;
}
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 8 | 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 | 2517bc83e40d92ac8dd75e6b2fa708fd | 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.concurrent.TimeUnit;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class B1716B{
public static void solve(){
}
public static void main(String args[])throws IOException, ParseException{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
StringBuffer res=new StringBuffer();
while(t-->0){
int n=sc.nextInt();
int a[]=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=i;
}
res.append(""+n+"\n");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
res.append(a[j]+" ");
}
res.append("\n");
if(i+1<=n){
int temp=a[i+1];
a[i+1]=a[i];
a[i]=temp;
}
}
solve();
}
System.out.println(res);
}
public static <K,V> Map<K,V> getLimitedSizedCache(int size){
/*Returns an unlimited sized map*/
if(size==0){
return (LinkedHashMap<K, V>) Collections.synchronizedMap(new LinkedHashMap<K, V>());
}
/*Returns the map with the limited size*/
Map<K, V> linkedHashMap = Collections.synchronizedMap(new LinkedHashMap<K, V>() {
protected boolean removeEldestEntry(Map.Entry<K, V> eldest)
{
return size() > size;
}
});
return linkedHashMap;
}
}
| 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 8 | 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 | 5bef662e676bbbc151183a2ebf35f0fb | 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.*;
/**
*
* @author eslam
*/
public class IceCave {
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(problemName + ".out");
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
String nextLine() {
String str = "";
try {
str = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
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());
}
}
// Beginning of the solution
static Kattio input = new Kattio();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
static ArrayList<ArrayList<Integer>> powerSet = new ArrayList<>();
static ArrayList<LinkedList<Integer>> allprem = new ArrayList<>();
static ArrayList<LinkedList<String>> allprems = new ArrayList<>();
static ArrayList<Long> luc = new ArrayList<>();
static long mod = (long) (Math.pow(10, 9) + 7);
static long dp[][];
public static void main(String[] args) throws IOException {
int test = input.nextInt();
loop:
while (test-- > 0) {
int n = input.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
int ind = 0;
log.write(n + "\n");
for (int i = 0; i < n; i++) {
print(a);
if (i != n - 1) {
int s = a[ind];
a[ind] = a[n - 1];
a[n - 1] = s;
ind++;
}
}
}
log.flush();
}
public static String revs(String w) {
String ans = "";
for (int i = w.length() - 1; i > -1; i--) {
ans += w.charAt(i);
}
return ans;
}
public static boolean isPalindrome(String w) {
for (int i = 0; i < w.length() / 2; i++) {
if (w.charAt(i) != w.charAt(w.length() - i - 1)) {
return false;
}
}
return true;
}
public static void getPowerSet(Queue<Integer> a) {
int n = a.poll();
if (!a.isEmpty()) {
getPowerSet(a);
}
int s = powerSet.size();
for (int i = 0; i < s; i++) {
ArrayList<Integer> ne = new ArrayList<>();
ne.add(n);
for (int j = 0; j < powerSet.get(i).size(); j++) {
ne.add(powerSet.get(i).get(j));
}
powerSet.add(ne);
}
ArrayList<Integer> p = new ArrayList<>();
p.add(n);
powerSet.add(p);
}
public static int getlo(int va) {
int v = 1;
while (v <= va) {
if ((va&v) != 0) {
return v;
}
v <<= 1;
}
return 0;
}
static long fast_pow(long a, long p) {
long res = 1;
while (p > 0) {
if (p % 2 == 0) {
a = (a * a) % mod;
p /= 2;
} else {
res = (res * a) % mod;
p--;
}
}
return res;
}
public static int countPrimeInRange(int n, boolean isPrime[]) {
int cnt = 0;
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * 2; j <= n; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
cnt++;
}
}
return cnt;
}
public static void create(long num) {
luc.add(num);
if (num > power(10, 9)) {
return;
}
create(num * 10 + 4);
create(num * 10 + 7);
}
public static long ceil(long a, long b) {
return (a + b - 1) / b;
}
public static long round(long a, long b) {
if (a < 0) {
return (a - b / 2) / b;
}
return (a + b / 2) / b;
}
public static void allPremutationsst(LinkedList<String> l, boolean visited[], ArrayList<String> st) {
if (l.size() == st.size()) {
allprems.add(l);
}
for (int i = 0; i < st.size(); i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<String> nl = new LinkedList<>();
for (String x : l) {
nl.add(x);
}
nl.add(st.get(i));
allPremutationsst(nl, visited, st);
visited[i] = false;
}
}
}
public static void allPremutations(LinkedList<Integer> l, boolean visited[], int a[]) {
if (l.size() == a.length) {
allprem.add(l);
}
for (int i = 0; i < a.length; i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<Integer> nl = new LinkedList<>();
for (Integer x : l) {
nl.add(x);
}
nl.add(a[i]);
allPremutations(nl, visited, a);
visited[i] = false;
}
}
}
public static int binarySearch(long[] a, long value) {
int l = 0;
int r = a.length - 1;
while (l <= r) {
int m = (l + r) / 2;
if (a[m] == value) {
return m;
} else if (a[m] > value) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}
public static void reverse(int l, int r, char ch[]) {
for (int i = 0; i < r / 2; i++) {
char c = ch[i];
ch[i] = ch[r - i - 1];
ch[r - i - 1] = c;
}
}
public static int logK(long v, long k) {
int ans = 0;
while (v > 1) {
ans++;
v /= k;
}
return ans;
}
public static long power(long a, long n) {
if (n == 1) {
return a;
}
long pow = power(a, n / 2);
pow *= pow;
if (n % 2 != 0) {
pow *= a;
}
return pow;
}
public static long get(long max, long x) {
if (x == 1) {
return max;
}
int cnt = 0;
while (max > 0) {
cnt++;
max /= x;
}
return cnt;
}
public static int numOF0(long v) {
long x = 1;
int cnt = 0;
while (x <= v) {
if ((x & v) == 0) {
cnt++;
}
x <<= 1;
}
return cnt;
}
public static int log2(double n) {
int cnt = 0;
while (n > 1) {
n /= 2;
cnt++;
}
return cnt;
}
public static int[] bfs(int node, ArrayList<Integer> a[]) {
Queue<Integer> q = new LinkedList<>();
q.add(node);
int distances[] = new int[a.length];
Arrays.fill(distances, -1);
distances[node] = 0;
while (!q.isEmpty()) {
int parent = q.poll();
ArrayList<Integer> nodes = a[parent];
int cost = distances[parent];
for (Integer node1 : nodes) {
if (distances[node1] == -1) {
q.add(node1);
distances[node1] = cost + 1;
}
}
}
return distances;
}
public static int get(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static ArrayList<Integer> primeFactors(int n) {
ArrayList<Integer> a = new ArrayList<>();
while (n % 2 == 0) {
a.add(2);
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
a.add(i);
n /= i;
}
if (n < i) {
break;
}
}
if (n > 2) {
a.add(n);
}
return a;
}
public static ArrayList<Integer> printPrimeFactoriztion(int n) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = 1; i < Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
if (isPrime(i)) {
a.add(i);
n /= i;
i = 0;
} else if (isPrime(n / i)) {
a.add(n / i);
n = i;
i = 0;
}
}
}
return a;
}
// end of solution
public static BigInteger f(long n) {
if (n <= 1) {
return BigInteger.ONE;
}
long t = n - 1;
BigInteger b = new BigInteger(t + "");
BigInteger ans = new BigInteger(n + "");
while (t > 1) {
ans = ans.multiply(b);
b = b.subtract(BigInteger.ONE);
t--;
}
return ans;
}
public static long factorial(long n) {
if (n <= 1) {
return 1;
}
long t = n - 1;
while (t > 1) {
n = mod((mod(n, mod) * mod(t, mod)), mod);
t--;
}
return n;
}
public static long rev(long n) {
long t = n;
long ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans;
}
public static boolean isPalindrome(int n) {
int t = n;
int ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans == n;
}
static class tri {
int x, y, z;
public tri(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return x + " " + y + " " + z;
}
}
static boolean isPrime(long num) {
if (num == 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
if (num == 3) {
return true;
}
for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void prefixSum(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = a[i] + a[i - 1];
}
}
public static void suffixSum(long[] a) {
for (int i = a.length - 2; i > -1; i--) {
a[i] = a[i] + a[i + 1];
}
}
static long mod(long a, long b) {
long r = a % b;
return r < 0 ? r + b : r;
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] >= a[i + 1]) {
return false;
}
}
return true;
}
public static void print(int[] a) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt();
}
}
static class pair {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
static class pai {
String x;
int y;
public pai(String x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return x / GCD(x, y) * y;
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
public static void simplifyTheFraction(long a, long b) {
long GCD = GCD(a, b);
System.out.println(a / GCD + " " + b / GCD);
}
/**
*/ // class RedBlackNode
static class RedBlackNode<T extends Comparable<T>> {
/**
* Possible color for this node
*/
public static final int BLACK = 0;
/**
* Possible color for this node
*/
public static final int RED = 1;
// the key of each node
public T key;
/**
* Parent of node
*/
RedBlackNode<T> parent;
/**
* Left child
*/
RedBlackNode<T> left;
/**
* Right child
*/
RedBlackNode<T> right;
// the number of elements to the left of each node
public int numLeft = 0;
// the number of elements to the right of each node
public int numRight = 0;
// the color of a node
public int color;
RedBlackNode() {
color = BLACK;
numLeft = 0;
numRight = 0;
parent = null;
left = null;
right = null;
}
// Constructor which sets key to the argument.
RedBlackNode(T key) {
this();
this.key = key;
}
}// end class RedBlackNode
static class RedBlackTree<T extends Comparable<T>> {
// Root initialized to nil.
private RedBlackNode<T> nil = new RedBlackNode<T>();
private RedBlackNode<T> root = nil;
public RedBlackTree() {
root.left = nil;
root.right = nil;
root.parent = nil;
}
// @param: X, The node which the lefRotate is to be performed on.
// Performs a leftRotate around X.
private void leftRotate(RedBlackNode<T> x) {
// Call leftRotateFixup() which updates the numLeft
// and numRight values.
leftRotateFixup(x);
// Perform the left rotate as described in the algorithm
// in the course text.
RedBlackNode<T> y;
y = x.right;
x.right = y.left;
// Check for existence of y.left and make pointer changes
if (!isNil(y.left)) {
y.left.parent = x;
}
y.parent = x.parent;
// X's parent is nul
if (isNil(x.parent)) {
root = y;
} // X is the left child of it's parent
else if (x.parent.left == x) {
x.parent.left = y;
} // X is the right child of it's parent.
else {
x.parent.right = y;
}
// Finish of the leftRotate
y.left = x;
x.parent = y;
}// end leftRotate(RedBlackNode X)
// @param: X, The node which the leftRotate is to be performed on.
// Updates the numLeft & numRight values affected by leftRotate.
private void leftRotateFixup(RedBlackNode x) {
// Case 1: Only X, X.right and X.right.right always are not nil.
if (isNil(x.left) && isNil(x.right.left)) {
x.numLeft = 0;
x.numRight = 0;
x.right.numLeft = 1;
} // Case 2: X.right.left also exists in addition to Case 1
else if (isNil(x.left) && !isNil(x.right.left)) {
x.numLeft = 0;
x.numRight = 1 + x.right.left.numLeft
+ x.right.left.numRight;
x.right.numLeft = 2 + x.right.left.numLeft
+ x.right.left.numRight;
} // Case 3: X.left also exists in addition to Case 1
else if (!isNil(x.left) && isNil(x.right.left)) {
x.numRight = 0;
x.right.numLeft = 2 + x.left.numLeft + x.left.numRight;
} // Case 4: X.left and X.right.left both exist in addtion to Case 1
else {
x.numRight = 1 + x.right.left.numLeft
+ x.right.left.numRight;
x.right.numLeft = 3 + x.left.numLeft + x.left.numRight
+ x.right.left.numLeft + x.right.left.numRight;
}
}// end leftRotateFixup(RedBlackNode X)
// @param: X, The node which the rightRotate is to be performed on.
// Updates the numLeft and numRight values affected by the Rotate.
private void rightRotate(RedBlackNode<T> y) {
// Call rightRotateFixup to adjust numRight and numLeft values
rightRotateFixup(y);
// Perform the rotate as described in the course text.
RedBlackNode<T> x = y.left;
y.left = x.right;
// Check for existence of X.right
if (!isNil(x.right)) {
x.right.parent = y;
}
x.parent = y.parent;
// y.parent is nil
if (isNil(y.parent)) {
root = x;
} // y is a right child of it's parent.
else if (y.parent.right == y) {
y.parent.right = x;
} // y is a left child of it's parent.
else {
y.parent.left = x;
}
x.right = y;
y.parent = x;
}// end rightRotate(RedBlackNode y)
// @param: y, the node around which the righRotate is to be performed.
// Updates the numLeft and numRight values affected by the rotate
private void rightRotateFixup(RedBlackNode y) {
// Case 1: Only y, y.left and y.left.left exists.
if (isNil(y.right) && isNil(y.left.right)) {
y.numRight = 0;
y.numLeft = 0;
y.left.numRight = 1;
} // Case 2: y.left.right also exists in addition to Case 1
else if (isNil(y.right) && !isNil(y.left.right)) {
y.numRight = 0;
y.numLeft = 1 + y.left.right.numRight
+ y.left.right.numLeft;
y.left.numRight = 2 + y.left.right.numRight
+ y.left.right.numLeft;
} // Case 3: y.right also exists in addition to Case 1
else if (!isNil(y.right) && isNil(y.left.right)) {
y.numLeft = 0;
y.left.numRight = 2 + y.right.numRight + y.right.numLeft;
} // Case 4: y.right & y.left.right exist in addition to Case 1
else {
y.numLeft = 1 + y.left.right.numRight
+ y.left.right.numLeft;
y.left.numRight = 3 + y.right.numRight
+ y.right.numLeft
+ y.left.right.numRight + y.left.right.numLeft;
}
}// end rightRotateFixup(RedBlackNode y)
public void insert(T key) {
insert(new RedBlackNode<T>(key));
}
// @param: z, the node to be inserted into the Tree rooted at root
// Inserts z into the appropriate position in the RedBlackTree while
// updating numLeft and numRight values.
private void insert(RedBlackNode<T> z) {
// Create a reference to root & initialize a node to nil
RedBlackNode<T> y = nil;
RedBlackNode<T> x = root;
// While we haven't reached a the end of the tree keep
// tryint to figure out where z should go
while (!isNil(x)) {
y = x;
// if z.key is < than the current key, go left
if (z.key.compareTo(x.key) < 0) {
// Update X.numLeft as z is < than X
x.numLeft++;
x = x.left;
} // else z.key >= X.key so go right.
else {
// Update X.numGreater as z is => X
x.numRight++;
x = x.right;
}
}
// y will hold z's parent
z.parent = y;
// Depending on the value of y.key, put z as the left or
// right child of y
if (isNil(y)) {
root = z;
} else if (z.key.compareTo(y.key) < 0) {
y.left = z;
} else {
y.right = z;
}
// Initialize z's children to nil and z's color to red
z.left = nil;
z.right = nil;
z.color = RedBlackNode.RED;
// Call insertFixup(z)
insertFixup(z);
}// end insert(RedBlackNode z)
// @param: z, the node which was inserted and may have caused a violation
// of the RedBlackTree properties
// Fixes up the violation of the RedBlackTree properties that may have
// been caused during insert(z)
private void insertFixup(RedBlackNode<T> z) {
RedBlackNode<T> y = nil;
// While there is a violation of the RedBlackTree properties..
while (z.parent.color == RedBlackNode.RED) {
// If z's parent is the the left child of it's parent.
if (z.parent == z.parent.parent.left) {
// Initialize y to z 's cousin
y = z.parent.parent.right;
// Case 1: if y is red...recolor
if (y.color == RedBlackNode.RED) {
z.parent.color = RedBlackNode.BLACK;
y.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
z = z.parent.parent;
} // Case 2: if y is black & z is a right child
else if (z == z.parent.right) {
// leftRotaet around z's parent
z = z.parent;
leftRotate(z);
} // Case 3: else y is black & z is a left child
else {
// recolor and rotate round z's grandpa
z.parent.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
rightRotate(z.parent.parent);
}
} // If z's parent is the right child of it's parent.
else {
// Initialize y to z's cousin
y = z.parent.parent.left;
// Case 1: if y is red...recolor
if (y.color == RedBlackNode.RED) {
z.parent.color = RedBlackNode.BLACK;
y.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
z = z.parent.parent;
} // Case 2: if y is black and z is a left child
else if (z == z.parent.left) {
// rightRotate around z's parent
z = z.parent;
rightRotate(z);
} // Case 3: if y is black and z is a right child
else {
// recolor and rotate around z's grandpa
z.parent.color = RedBlackNode.BLACK;
z.parent.parent.color = RedBlackNode.RED;
leftRotate(z.parent.parent);
}
}
}
// Color root black at all times
root.color = RedBlackNode.BLACK;
}// end insertFixup(RedBlackNode z)
// @param: node, a RedBlackNode
// @param: node, the node with the smallest key rooted at node
public RedBlackNode<T> treeMinimum(RedBlackNode<T> node) {
// while there is a smaller key, keep going left
while (!isNil(node.left)) {
node = node.left;
}
return node;
}// end treeMinimum(RedBlackNode node)
// @param: X, a RedBlackNode whose successor we must find
// @return: return's the node the with the next largest key
// from X.key
public RedBlackNode<T> treeSuccessor(RedBlackNode<T> x) {
// if X.left is not nil, call treeMinimum(X.right) and
// return it's value
if (!isNil(x.left)) {
return treeMinimum(x.right);
}
RedBlackNode<T> y = x.parent;
// while X is it's parent's right child...
while (!isNil(y) && x == y.right) {
// Keep moving up in the tree
x = y;
y = y.parent;
}
// Return successor
return y;
}// end treeMinimum(RedBlackNode X)
// @param: z, the RedBlackNode which is to be removed from the the tree
// Remove's z from the RedBlackTree rooted at root
public void remove(RedBlackNode<T> v) {
RedBlackNode<T> z = search(v.key);
// Declare variables
RedBlackNode<T> x = nil;
RedBlackNode<T> y = nil;
// if either one of z's children is nil, then we must remove z
if (isNil(z.left) || isNil(z.right)) {
y = z;
} // else we must remove the successor of z
else {
y = treeSuccessor(z);
}
// Let X be the left or right child of y (y can only have one child)
if (!isNil(y.left)) {
x = y.left;
} else {
x = y.right;
}
// link X's parent to y's parent
x.parent = y.parent;
// If y's parent is nil, then X is the root
if (isNil(y.parent)) {
root = x;
} // else if y is a left child, set X to be y's left sibling
else if (!isNil(y.parent.left) && y.parent.left == y) {
y.parent.left = x;
} // else if y is a right child, set X to be y's right sibling
else if (!isNil(y.parent.right) && y.parent.right == y) {
y.parent.right = x;
}
// if y != z, trasfer y's satellite data into z.
if (y != z) {
z.key = y.key;
}
// Update the numLeft and numRight numbers which might need
// updating due to the deletion of z.key.
fixNodeData(x, y);
// If y's color is black, it is a violation of the
// RedBlackTree properties so call removeFixup()
if (y.color == RedBlackNode.BLACK) {
removeFixup(x);
}
}// end remove(RedBlackNode z)
// @param: y, the RedBlackNode which was actually deleted from the tree
// @param: key, the value of the key that used to be in y
private void fixNodeData(RedBlackNode<T> x, RedBlackNode<T> y) {
// Initialize two variables which will help us traverse the tree
RedBlackNode<T> current = nil;
RedBlackNode<T> track = nil;
// if X is nil, then we will start updating at y.parent
// Set track to y, y.parent's child
if (isNil(x)) {
current = y.parent;
track = y;
} // if X is not nil, then we start updating at X.parent
// Set track to X, X.parent's child
else {
current = x.parent;
track = x;
}
// while we haven't reached the root
while (!isNil(current)) {
// if the node we deleted has a different key than
// the current node
if (y.key != current.key) {
// if the node we deleted is greater than
// current.key then decrement current.numRight
if (y.key.compareTo(current.key) > 0) {
current.numRight--;
}
// if the node we deleted is less than
// current.key thendecrement current.numLeft
if (y.key.compareTo(current.key) < 0) {
current.numLeft--;
}
} // if the node we deleted has the same key as the
// current node we are checking
else {
// the cases where the current node has any nil
// children and update appropriately
if (isNil(current.left)) {
current.numLeft--;
} else if (isNil(current.right)) {
current.numRight--;
} // the cases where current has two children and
// we must determine whether track is it's left
// or right child and update appropriately
else if (track == current.right) {
current.numRight--;
} else if (track == current.left) {
current.numLeft--;
}
}
// update track and current
track = current;
current = current.parent;
}
}//end fixNodeData()
// @param: X, the child of the deleted node from remove(RedBlackNode v)
// Restores the Red Black properties that may have been violated during
// the removal of a node in remove(RedBlackNode v)
private void removeFixup(RedBlackNode<T> x) {
RedBlackNode<T> w;
// While we haven't fixed the tree completely...
while (x != root && x.color == RedBlackNode.BLACK) {
// if X is it's parent's left child
if (x == x.parent.left) {
// set w = X's sibling
w = x.parent.right;
// Case 1, w's color is red.
if (w.color == RedBlackNode.RED) {
w.color = RedBlackNode.BLACK;
x.parent.color = RedBlackNode.RED;
leftRotate(x.parent);
w = x.parent.right;
}
// Case 2, both of w's children are black
if (w.left.color == RedBlackNode.BLACK
&& w.right.color == RedBlackNode.BLACK) {
w.color = RedBlackNode.RED;
x = x.parent;
} // Case 3 / Case 4
else {
// Case 3, w's right child is black
if (w.right.color == RedBlackNode.BLACK) {
w.left.color = RedBlackNode.BLACK;
w.color = RedBlackNode.RED;
rightRotate(w);
w = x.parent.right;
}
// Case 4, w = black, w.right = red
w.color = x.parent.color;
x.parent.color = RedBlackNode.BLACK;
w.right.color = RedBlackNode.BLACK;
leftRotate(x.parent);
x = root;
}
} // if X is it's parent's right child
else {
// set w to X's sibling
w = x.parent.left;
// Case 1, w's color is red
if (w.color == RedBlackNode.RED) {
w.color = RedBlackNode.BLACK;
x.parent.color = RedBlackNode.RED;
rightRotate(x.parent);
w = x.parent.left;
}
// Case 2, both of w's children are black
if (w.right.color == RedBlackNode.BLACK
&& w.left.color == RedBlackNode.BLACK) {
w.color = RedBlackNode.RED;
x = x.parent;
} // Case 3 / Case 4
else {
// Case 3, w's left child is black
if (w.left.color == RedBlackNode.BLACK) {
w.right.color = RedBlackNode.BLACK;
w.color = RedBlackNode.RED;
leftRotate(w);
w = x.parent.left;
}
// Case 4, w = black, and w.left = red
w.color = x.parent.color;
x.parent.color = RedBlackNode.BLACK;
w.left.color = RedBlackNode.BLACK;
rightRotate(x.parent);
x = root;
}
}
}// end while
// set X to black to ensure there is no violation of
// RedBlack tree Properties
x.color = RedBlackNode.BLACK;
}// end removeFixup(RedBlackNode X)
// @param: key, the key whose node we want to search for
// @return: returns a node with the key, key, if not found, returns null
// Searches for a node with key k and returns the first such node, if no
// such node is found returns null
public RedBlackNode<T> search(T key) {
// Initialize a pointer to the root to traverse the tree
RedBlackNode<T> current = root;
// While we haven't reached the end of the tree
while (!isNil(current)) {
// If we have found a node with a key equal to key
if (current.key.equals(key)) // return that node and exit search(int)
{
return current;
} // go left or right based on value of current and key
else if (current.key.compareTo(key) < 0) {
current = current.right;
} // go left or right based on value of current and key
else {
current = current.left;
}
}
// we have not found a node whose key is "key"
return null;
}// end search(int key)
// @param: key, any Comparable object
// @return: return's the number of elements greater than key
public int numGreater(T key) {
// Call findNumGreater(root, key) which will return the number
// of nodes whose key is greater than key
return findNumGreater(root, key);
}// end numGreater(int key)
// @param: key, any Comparable object
// @return: return's teh number of elements smaller than key
public int numSmaller(T key) {
// Call findNumSmaller(root,key) which will return
// the number of nodes whose key is greater than key
return findNumSmaller(root, key);
}// end numSmaller(int key)
// @param: node, the root of the tree, the key who we must
// compare other node key's to.
// @return: the number of nodes greater than key.
public int findNumGreater(RedBlackNode<T> node, T key) {
// Base Case: if node is nil, return 0
if (isNil(node)) {
return 0;
} // If key is less than node.key, all elements right of node are
// greater than key, add this to our total and look to the left
else if (key.compareTo(node.key) < 0) {
return 1 + node.numRight + findNumGreater(node.left, key);
} // If key is greater than node.key, then look to the right as
// all elements to the left of node are smaller than key
else {
return findNumGreater(node.right, key);
}
}// end findNumGreater(RedBlackNode, int key)
/**
* Returns sorted list of keys greater than key. Size of list will not
* exceed maxReturned
*
* @param key Key to search for
* @param maxReturned Maximum number of results to return
* @return List of keys greater than key. List may not exceed
* maxReturned
*/
public List<T> getGreaterThan(T key, Integer maxReturned) {
List<T> list = new ArrayList<T>();
getGreaterThan(root, key, list);
return list.subList(0, Math.min(maxReturned, list.size()));
}
private void getGreaterThan(RedBlackNode<T> node, T key,
List<T> list) {
if (isNil(node)) {
return;
} else if (node.key.compareTo(key) > 0) {
getGreaterThan(node.left, key, list);
list.add(node.key);
getGreaterThan(node.right, key, list);
} else {
getGreaterThan(node.right, key, list);
}
}
// @param: node, the root of the tree, the key who we must compare other
// node key's to.
// @return: the number of nodes smaller than key.
public int findNumSmaller(RedBlackNode<T> node, T key) {
// Base Case: if node is nil, return 0
if (isNil(node)) {
return 0;
} // If key is less than node.key, look to the left as all
// elements on the right of node are greater than key
else if (key.compareTo(node.key) <= 0) {
return findNumSmaller(node.left, key);
} // If key is larger than node.key, all elements to the left of
// node are smaller than key, add this to our total and look
// to the right.
else {
return 1 + node.numLeft + findNumSmaller(node.right, key);
}
}// end findNumSmaller(RedBlackNode nod, int key)
// @param: node, the RedBlackNode we must check to see whether it's nil
// @return: return's true of node is nil and false otherwise
private boolean isNil(RedBlackNode node) {
// return appropriate value
return node == nil;
}// end isNil(RedBlackNode node)
// @return: return's the size of the tree
// Return's the # of nodes including the root which the RedBlackTree
// rooted at root has.
public int size() {
// Return the number of nodes to the root's left + the number of
// nodes on the root's right + the root itself.
return root.numLeft + root.numRight + 1;
}// end size()
}// end class RedBlackTree
}
| 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 8 | 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 | f150fb34414777a8e5b75776d276cbfc | 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 kriti;
import java.math.*;
import java.io.*;
import java.util.*;
public class A
{
static PrintWriter out = new PrintWriter(System.out);
static FastReader in=new FastReader();
public static void main(String args[])throws IOException
{
int t =i();
outer:while(t-->0) {
long n=l();
long k=n;
int[] A=new int[(int)n];
for(int i=0;i<n;i++) {
A[i]=i+1;
}
out.println(n);
for(int i:A)out.print(i+" ");
out.println();
for(int i=(int)n-1;i>0;i--) {
int temp=A[i];
A[i]=A[i-1];
A[i-1]=temp;
for(long j:A)out.print(j+" ");
out.println();
}
}
out.close();
}
static HashMap<Long,Integer> hash(long A[]){
HashMap<Long,Integer> map=new HashMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
public static int index(ArrayList<String> y,String s) {
for(int i=0;i<y.size();i++) {
if(y.get(i)==s)return i;
}
return -1;
}
public static String reverseString(String str)
{
if(str.isEmpty())
{
return str;
}
else
{
return reverseString(str.substring(1))+str.charAt(0);
}
}
static void printno() {
System.out.println("NO");
}
static void printyes() {
System.out.println("YES");
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static int[] Swap(int[] A, int i, int j) {
int temp = A[i];
A[i]=A[j];
A[j]=temp;
return A;
}
static String ChartoString(char[] x) {
String ans="";
for(char i:x) {
ans+=i;
}
return ans;
}
static int HCF(int num1, int num2) {
int temp1 = num1;
int temp2 = num2;
while(temp2 != 0){
int temp = temp2;
temp2 = temp1%temp2;
temp1 = temp;
}
int hcf = temp1;
return hcf;
}
static boolean palindrome(String s) {
char[] x = s.toCharArray();
int i=0; int r= x.length-1;
while(i<r) {
if(x[i]!=x[r]) {
return false;
}
i++;
r--;
}
return true;
}
static void sorting(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 int LCM(int x,int y) {
int a=(x*y)/findGcd(x,y);
return a;
}
static int findGcd(int x, int y)
{
if (x == 0)
return y;
return findGcd(y % x, x);
}
static int findLcm(int x, int y)
{
return (x / findGcd(x, y)) * y;
}
public static int checkTriangle(long a,
long b, long c)
{
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
static boolean isitSorted(long A[])
{
for(int i=1; i<A.length; i++) {
if(A[i]<A[i-1])return false;
}
return true;
}
static void print(int A[])
{
for(long i:A)System.out.print(i+ " ");
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(long a:A)System.out.print(a);
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static String S() {
return in.next();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputL(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++) {
A[i]=in.nextLong();
}
return A;
}
}
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 8 | 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 | cdb6a11d54306a3777f444e238a25ab0 | 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.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
public class PermutationChain {
static InputReader inputReader=new InputReader(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static void solve() throws IOException
{
int n=inputReader.nextInt();
List<Integer>list=new ArrayList<>();
for (int i=0;i<n;i++)
{
list.add(i+1);
}
List<List<Integer>>answer=new ArrayList<>();
int end=n-2;
answer.add(new ArrayList<>(list));
while (end>=0)
{
List<Integer>list1=new ArrayList<>(answer.get(answer.size()-1));
int temp=list1.get(end);
list1.set(end,list1.get(end+1));
list1.set(end+1,temp);
end--;
answer.add(new ArrayList<>(list1));
}
out.println(answer.size());
for(List<Integer>list1:answer)
{
for (int ele:list1)
{
out.print(ele+" ");
}
out.println();
}
}
static PrintWriter out=new PrintWriter((System.out));
static void SortDec(long arr[])
{
List<Long>list=new ArrayList<>();
for(long ele:arr) {
list.add(ele);
}
Collections.sort(list,Collections.reverseOrder());
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
static void Sort(long arr[])
{
List<Long>list=new ArrayList<>();
for(long ele:arr) {
list.add(ele);
}
Collections.sort(list);
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
public static void main(String args[])throws IOException
{
int test= inputReader.nextInt();
while (test-->0) {
solve();
}
long s = System.currentTimeMillis();
// out.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} 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 interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| 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 8 | 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 | e29ef2c4f3fec4fc0c35a75c13298631 | 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.security.cert.X509CRL;
import java.util.*;
import java.lang.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class Main {
static InputStream is;
static PrintWriter out;
//static String INPUT = "in.txt";
static String INPUT = "";
static String OUTPUT = "";
//global const
//private final static long BASE = 998244353L;
private final static int ALPHABET = (int)('z') - (int)('a') + 1;
private final static long BASE = 1000000007l;
private final static int INF_I = 1000000000;
private final static long INF_L = 10000000000000000l;
private final static int MAXN = 200100;
private final static int MAXK = 31;
private final static int[] DX = {-1,0,1,0};
private final static int[] DY = {0,1,0,-1};
static void progress() {
int N = readInt();
out.println(N);
for (int pos=0; pos<N; pos++) {
int[] ans = new int[N];
ans[pos] = 1;
for (int i=0,num=2; i<N; i++) {
if (i==pos) continue;
ans[i] = num;
num++;
}
for (int i=0; i<N; i++) out.print(ans[i] + " ");
out.println();
}
}
//global
static void solve() {
//int ntest = 1;
int ntest = readInt();
for (int test=0; test<ntest; test++) {
progress();
}
}
public static void main(String[] args) throws Exception {
long S = System.currentTimeMillis();
if (INPUT=="") {
is = System.in;
} else {
File file = new File(INPUT);
is = new FileInputStream(file);
}
if (OUTPUT == "") out = new PrintWriter(System.out);
else out = new PrintWriter(OUTPUT);
solve();
out.flush();
long G = System.currentTimeMillis();
}
private static class MultiSet<T extends Comparable<T>> {
private TreeSet<T> set;
private Map<T, Integer> count;
public MultiSet() {
this.set = new TreeSet<>();
this.count = new HashMap<>();
}
public void add(T x) {
this.set.add(x);
int o = this.count.getOrDefault(x, 0);
this.count.put(x, o+1);
}
public void remove(T x) {
int o = this.count.getOrDefault(x, 0);
if (o==0) return;
this.count.put(x, o-1);
if (o==1) this.set.remove(x);
}
public void removeAll(T x) {
int o = this.count.getOrDefault(x, 0);
if (o==0) return;
this.count.put(x, 0);
this.set.remove(x);
}
public T first() {
return this.set.first();
}
public T last() {
return this.set.last();
}
public int getCount(T x) {
return this.count.getOrDefault(x, 0);
}
public int size() {
int res = 0;
for (T x: this.set) res += this.count.get(x);
return res;
}
}
private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {return x;}
public T getY() {return y;}
@Override
public int compareTo(Point<T> o) {
int cmp = x.compareTo(o.getX());
if (cmp==0) return y.compareTo(o.getY());
return cmp;
}
}
private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> {
public ClassComparator() {}
@Override
public int compare(T a, T b) {
return a.compareTo(b);
}
}
private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
public ListComparator() {}
@Override
public int compare(List<T> o1, List<T> o2) {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int c = o1.get(i).compareTo(o2.get(i));
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double readDouble() { return Double.parseDouble(readString()); }
private static char readChar() { return (char)skip(); }
private static String readString()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] readChar(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readTable(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = readChar(m);
return map;
}
private static int[] readIntArray(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = readInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i=0;i<n;i++) a[i] = readLong();
return a;
}
private static int readInt()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long readLong()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
} | 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 8 | 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 | ae3683d37bc1a6d7cdd60458a9e74ed8 | 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 | //KENAA
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
B_Permutation_Chain();
}
public static void B_Permutation_Chain() {
FastReader fr = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int tt = fr.nextInt();
while (tt-- > 0) {
int n = fr.nextInt();
int[]arr = new int[n];
for (int i = 1; i <= arr.length; i++) {
arr[i-1]=i;
}
out.println(n);
for (int i = 0; i < arr.length; i++) {
out.print(arr[i]+" ");
}
out.println();
for (int i = arr.length-1; i > 0; i--) {
int temp= arr[i];
arr[i]=arr[i-1];
arr[i-1]=temp;
for (int j = 0; j < arr.length; j++) {
out.print(arr[j]+" ");
}
out.println();
}
}
out.close();
}
public static void A_2_3_Moves() {
FastReader fr = new FastReader();
int tt = fr.nextInt();
while (tt-- > 0) {
int n = fr.nextInt();
if (n==1) {
System.out.println(2);
}else if (n==2) {
System.out.println(1);
}else {
if (n%3==0) {
System.out.println((n/3));
}else {
System.out.println((n/3)+1);
}
}
}
}
public static void C_Min_Max_Array_Transformation() {
FastReader fr = new FastReader();
int tt = fr.nextInt();
while (tt-- > 0) {
int n = fr.nextInt();
int[] a = fr.readArray(n);
int[] b = fr.readArray(n);
int[] dmax = new int[n];
int[] dmin = new int[n];
LinkedList<Integer> list = new LinkedList<Integer>();
int bPo = n-1;
for (int i = n-1; i >= 0; i--) {
while (bPo>=0 && b[bPo]>=a[i]) {
list.add(b[bPo--]);
}
dmax[i]=list.getFirst()-a[i];
dmin[i]= list.getLast()-a[i];
int aa =n-i;
if (aa==n-(bPo+1)) {
list = new LinkedList<Integer>();
}
}
for (int i = 0; i < dmin.length; i++) {
System.out.print(dmin[i]+" ");
}
System.out.println();
for (int i = 0; i < dmin.length; i++) {
System.out.print(dmax[i]+" ");
}
System.out.println();
}
}
static class FastReader {
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[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
public static long gcd(long a, long b) {
while (b != 0) {
long m = a % b;
a = b;
b = m;
}
return a;
}
public static long factorial(int n) {
if (n == 0)
return 1;
long res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static long nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
public static ArrayList<Integer> factors(long n) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 2; i < n / i; i++) {
while (n % i == 0) {
if (!list.contains(i)) {
list.add(i);
n /= i;
}
}
}
if (n > 2) {
if (!list.contains((int) n)) {
list.add((int) n);
}
}
return list;
}
public static int numOfPrimes(int n) {
if (n < 2) {
return 0;
}
boolean[] bool = new boolean[n + 1];
outer: for (int i = 2; i < bool.length / i; i++) {
if (bool[i]) {
continue outer;
}
for (int j = 2 * i; j < bool.length; j += i) {
bool[j] = true;
}
}
int counter = 0;
for (int i = 0; i < bool.length; i++) {
if (!bool[i]) {
counter++;
}
}
return counter;
}
public static void sort2DGivenArray(Object[][] arr, int colNum) {
Arrays.sort(arr, (val1, val2) -> {
if (val1[colNum] == val2[colNum]) {
return 0;
} else if (((Integer) (val1[colNum])).compareTo(((Integer) (val2[colNum]))) < 0) {
return 1;
} else {
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 8 | 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 | 341d38ce8d7e08692fd002b35e9f9822 | 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 {
// Alphabet size (# of symbols)
public static class Pair implements Comparable < Pair > {
int d;
int i;
Pair(int i, int d) {
this.d = d;
this.i = i;
System.out.println("TEst");
}
public int compareTo(Pair o) {
if (this.d == o.d)
return this.i - o.i;
if (this.d < o.d)
return -1;
else
return 1;
}
}
public static class SegmentTree {
long[] st;
long[] lazy;
int n;
SegmentTree(long[] arr, int n) {
this.n = n;
st = new long[4 * n];
lazy = new long[4 * n];
construct(arr, 0, n - 1, 0);
}
public long construct(long[] arr, int si, int ei, int node) {
if (si == ei) {
st[node] = arr[si];
return arr[si];
}
int mid = (si + ei) / 2;
long left = construct(arr, si, mid, 2 * node + 1);
long right = construct(arr, mid + 1, ei, 2 * node + 2);
st[node] = left + right;
return st[node];
}
public long get(int l, int r) {
return get(0, n - 1, l, r, 0);
}
public long get(int si, int ei, int l, int r, int node) {
if (r < si || l > ei)
return 0;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei)
return st[node];
int mid = (si + ei) / 2;
return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2);
}
public void update(int index, int value) {
update(0, n - 1, index, 0, value);
}
public void update(int si, int ei, int index, int node, int val) {
if (si == ei) {
st[node] = val;
return;
}
int mid = (si + ei) / 2;
if (index <= mid) {
update(si, mid, index, 2 * node + 1, val);
} else {
update(mid + 1, ei, index, 2 * node + 2, val);
}
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
public void rangeUpdate(int l, int r, int val) {
rangeUpdate(0, n - 1, l, r, 0, val);
}
public void rangeUpdate(int si, int ei, int l, int r, int node, int val) {
if (r < si || l > ei)
return;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei) {
st[node] += val * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (si + ei) / 2;
rangeUpdate(si, mid, l, r, 2 * node + 1, val);
rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val);
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
}
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();
}
}
static int count = 0;
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setIn(new FileInputStream(new File("input.txt")));
System.setOut(new PrintStream(new File("output.txt")));
} catch (Exception e) {
}
}
Reader sc = new Reader();
int tc = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (tc-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
for (int idx = 0; idx < n; idx++ ) {
arr[idx] = (idx + 1);
}
sb.append(n + "\n");
int k = n;
for (int idx = 0; idx < n; idx++)
sb.append(arr[idx] + " ");
sb.append("\n");
for (int i = n - 1; i >= 1; i--) {
int temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
for (int idx = 0; idx < n; idx++)
sb.append(arr[idx] + " ");
sb.append("\n");
}
}
System.out.println(sb);
}
public static int[][] matrixExpo(int[][] mat, int n) {
int[][] res = new int[2][2];
for (int i = 0; i < 2; i++)
res[i][i] = 1;
while (n > 0) {
if (n % 2 == 0) {
mat = multiplyMatrix(mat, mat);
} else {
res = multiplyMatrix(mat, res);
mat = multiplyMatrix(mat, mat);
}
n = n / 2;
}
return res;
}
public static void print(int[][] mat) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
public static int[][] multiplyMatrix(int[][] mat1, int[][] mat2) {
int n = mat1.length;
int[][] res = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
for (int k = 0; k < n; k++) {
sum += (mat1[i][k] * mat2[k][j]);
}
res[i][j] = sum;
}
}
return res;
}
public static int[] resultMat(int[][] mat, int[] arr) {
int n = arr.length;
int[] res = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
res[i] += (arr[j] * mat[j][i]);
}
}
return res;
}
public static boolean contained(int[] arr, int k) {
for (int ele : arr) {
if (ele % k == 0)
return true;
}
return false;
}
static long power(long x, long y, long p) {
long res = 1; // Initialize result
// Update x if it is more
// than or equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply
// x with the 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;
}
public static int countBit(int n) {
int count = 0;
while (n > 0) {
count += 1;
n /= 2;
}
return count;
}
public static int get(int[] dsu, int x) {
if (dsu[x] == x)
return x;
int k = get(dsu, dsu[x]);
dsu[x] = k;
return k;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int Xor(int n) {
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
public static long pow(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) > 0)
res = (res * a) % mod;
b = b >> 1;
a = ((a % mod) * (a % mod)) % mod;
}
return (res % mod + mod) % mod;
}
public static double digit(long num) {
return Math.floor(Math.log10(num) + 1);
}
public static void pattern(int n) {
int k = n;
int p = 1;
while (k-- > 0) {
for (int i = 1; i <= k; i++) {
System.out.print(" ");
}
for (int i = 1; i <= p; i++)
System.out.print("* ");
System.out.println();
p += 1;
}
}
static int upperBound(ArrayList<Long> arr, long key) {
int mid, N = arr.size();
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr.get(mid)) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
return low;
}
static int lowerBound(ArrayList<Long> array, long key) {
// Initialize starting index and
// ending index
int low = 0, high = array.size();
int mid;
// Till high does not crosses low
while (low < high) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is less than or equal
// to array[mid], then find in
// left subarray
if (key <= array.get(mid)) {
high = mid;
}
// If key is greater than array[mid],
// then find in right subarray
else {
low = mid + 1;
}
}
// If key is greater than last element which is
// array[n-1] then lower bound
// does not exists in the array
if (low < array.size() && array.get(low) < key) {
low++;
}
// Returning the lower_bound index
return low;
}
}
| 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 8 | 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 | d97dec2a9fe1f2a310d276ce08a300e9 | 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.InputStream;
import java.io.PrintWriter;
import java.io.*;
import java.util.*;
import java.util.LinkedList;
import java.math.*;
import java.lang.*;
import java.util.PriorityQueue;
import static java.lang.Math.*;
@SuppressWarnings("unchecked")
public class Solution implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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 String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
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 long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long 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 double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
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 readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static long min(long a,long b)
{
if(a>b)
{
return b;
}
return a;
}
public static int min(int a,int b)
{
if(a>b)
{
return b;
}
return a;
}
public static int max(int a,int b)
{
if(a>b)
{
return a;
}
return b;
}
static class pair implements Comparable<pair>
{
int x;
int y;
pair(int x,int y)
{
this.x = x;
this.y = y;
}
public int compareTo(pair b)
{
return this.x-b.x;
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Solution(),"Main",1<<26).start();
}
public static boolean generateResult(long n, long k, long b, long s, List<Long> result) {
long bk = b * k;
// Failure Cases
if (s < bk) {
return false;
}
if (n == 0) {
if (bk != s) {
return false;
}
return true;
}
// Each Iteration
long remainder = min(k-1, s-bk);
long a = bk + remainder;
result.add(a);
return generateResult(n-1, k, 0, s-a, result);
}
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t1 = sc.nextInt();
while(t1-->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 i=n-1;i>=0;i--) {
for(int j=0;j<n;j++) {
out.print(arr[j] + " ");
}
out.println();
if (i==0) continue;
int swap = arr[i];
arr[i] = arr[i-1];
arr[i-1] = swap;
}
}
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 8 | 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 | 75d9315b2df2340aafa3a8adc7d3b254 | 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 final class Main {
//int 2e9 - long 9e18
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)};
static Pair[] movesDiagonal = new Pair[]{new Pair(-1, -1), new Pair(-1, 1), new Pair(1, -1), new Pair(1, 1)};
static int mod = (int) (1e9 + 7);
static int mod2 = 998244353;
public static void main(String[] args) {
int tt = i();
while (tt-- > 0) {
solve();
}
out.flush();
}
public static void solve() {
int n = i();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
out.println(n);
print(a);
swap(a, 0, 1);
print(a);
for (int i = 2; i < n; i++) {
swap(a, i - 1, i);
print(a);
}
}
// (10,5) = 2 ,(11,5) = 3
static long upperDiv(long a, long b) {
return (a / b) + ((a % b == 0) ? 0 : 1);
}
static long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static int[] preint(int[] a) {
int[] pre = new int[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] pre(int[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] post(int[] a) {
long[] post = new long[a.length + 1];
post[0] = 0;
for (int i = 0; i < a.length; i++) {
post[i + 1] = post[i] + a[a.length - 1 - i];
}
return post;
}
static long[] pre(long[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static void print(char A[]) {
for (char c : A) {
out.print(c);
}
out.println();
}
static void print(boolean A[]) {
for (boolean c : A) {
out.print(c + " ");
}
out.println();
}
static void print(int A[]) {
for (int c : A) {
out.print(c + " ");
}
out.println();
}
static void print(long A[]) {
for (long i : A) {
out.print(i + " ");
}
out.println();
}
static void print(List<Integer> A) {
for (int a : A) {
out.print(a + " ");
}
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static String c() {
return in.next();
}
static int[][] inputWithIdx(int N) {
int A[][] = new int[N][2];
for (int i = 0; i < N; i++) {
A[i] = new int[]{i, in.nextInt()};
}
return A;
}
static int[] input(int N) {
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[] = new long[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextLong();
}
return A;
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long LCM(int a, int b) {
return (long) a / GCD(a, b) * b;
}
static long LCM(long a, long b) {
return a / GCD(a, b) * b;
}
// find highest i which satisfy a[i]<=x
static int lowerbound(int[] a, int x) {
int l = 0;
int r = a.length - 1;
while (l < r) {
int m = (l + r + 1) / 2;
if (a[m] <= x) {
l = m;
} else {
r = m - 1;
}
}
return l;
}
static void shuffle(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
}
static void shuffleAndSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int[] temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr, comparator);
}
static void shuffleAndSort(long[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
long temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static boolean isPerfectSquare(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static void swap(int A[], int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
static void swap(char A[], int a, int b) {
char t = A[a];
A[a] = A[b];
A[b] = t;
}
static long pow(long a, long b, int mod) {
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 pow(long a, long b) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow *= x;
}
x = x * x;
b /= 2;
}
return pow;
}
static long modInverse(long x, int mod) {
return pow(x, mod - 2, mod);
}
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;
}
public static String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
public static void reverse(int[] arr) {
int l = 0;
int r = arr.length - 1;
while (l < r) {
swap(arr, l, r);
l++;
r--;
}
}
public static String repeat(char ch, int repeat) {
if (repeat <= 0) {
return "";
}
final char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {
buf[i] = ch;
}
return new String(buf);
}
public static int[] manacher(String s) {
char[] chars = s.toCharArray();
int n = s.length();
int[] d1 = new int[n];
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
return d1;
}
public static int[] kmp(String s) {
int n = s.length();
int[] res = new int[n];
for (int i = 1; i < n; ++i) {
int j = res[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = res[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
++j;
}
res[i] = j;
}
return res;
}
}
class Pair {
int i;
int j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return i == pair.i && j == pair.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class ThreePair {
int i;
int j;
int k;
ThreePair(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ThreePair pair = (ThreePair) o;
return i == pair.i && j == pair.j && k == pair.k;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
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 Node {
int val;
public Node(int val) {
this.val = val;
}
}
class ST {
int n;
Node[] st;
ST(int n) {
this.n = n;
st = new Node[4 * Integer.highestOneBit(n)];
}
void build(Node[] nodes) {
build(0, 0, n - 1, nodes);
}
private void build(int id, int l, int r, Node[] nodes) {
if (l == r) {
st[id] = nodes[l];
return;
}
int mid = (l + r) >> 1;
build((id << 1) + 1, l, mid, nodes);
build((id << 1) + 2, mid + 1, r, nodes);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
void update(int i, Node node) {
update(0, 0, n - 1, i, node);
}
private void update(int id, int l, int r, int i, Node node) {
if (i < l || r < i) {
return;
}
if (l == r) {
st[id] = node;
return;
}
int mid = (l + r) >> 1;
update((id << 1) + 1, l, mid, i, node);
update((id << 1) + 2, mid + 1, r, i, node);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
Node get(int x, int y) {
return get(0, 0, n - 1, x, y);
}
private Node get(int id, int l, int r, int x, int y) {
if (x > r || y < l) {
return new Node(0);
}
if (x <= l && r <= y) {
return st[id];
}
int mid = (l + r) >> 1;
return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y));
}
Node comb(Node a, Node b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return new Node(GCD(a.val, b.val));
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % 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 8 | 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 | 57aba5155383f49e999aa97371c5a943 | 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.math.BigInteger;
import java.util.*;
import java.io.*;
public class Vaibhav {
/*-----------==============-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static long bit[];
static boolean prime[];
public static long lcm(long x, long y) {
return (x * y) / gcd(x, y);
}
static class Pair implements Comparable<Pair> {
int cqm;
int co;
Pair(int cqm, int co) {
this.cqm = cqm;
this.co = co;
}
public int compareTo(Pair o){
return this.cqm-o.cqm;
}
}
public static void update(long bit[], int i, int x) {
for (; i < bit.length; i += (i & (-i))) {
bit[i] += x;
}
}
public static long sum(int i) {
long sum = 0;
for (; i > 0; i -= (i & (-i))) {
sum += bit[i];
}
return sum;
}
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 int LongestIncreasingSubsequenceLength(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;
}
static long power(long x, long y, long p) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1l;
x = x % p;
while (y > 0) {
if (y % 2 == 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void sort(long[] a) {
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 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;
}
long nextLong() {
return Long.parseLong(next());
}
}
static void sieveOfEratosthenes(int n) {
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;
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long nCk(int n, int k) {
long res = 1;
for (int i = n - k + 1; i <= n; ++i) res *= i;
for (int i = 2; i <= k; ++i) res /= i;
return res;
}
static BigInteger bi(String str) {
return new BigInteger(str);
}
/*------------=========------------------------------------===================================================------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------===============================================------=================-=-------=============--------*/
static long max;
static long res;
static long total;
static FastScanner fs = null;
static long ans;
//static long col;
static int p[];
static boolean vis[];
static long val[];
static long mod = 1_000_000_007;
static ArrayList<Integer> al[];
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while(t-->0) {
int n = fs.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] = i+1;
}
out.println(n);
for(int i=0;i<n;i++){
if(i==0){
for(int v:a){
out.print(v+" ");
}
out.println();
}else{
int tmp = a[i];
a[i] = a[i-1];
a[i-1]=tmp;
for(int v:a){
out.print(v+" ");
}
out.println();
}
}
}
out.close();
}
public static void dfs(int child ,long c) {
vis[child] = true;
long start = c;
if(child==0){
start = c-1;
}else{
start = c-2;
}
for(int i:al[child]){
if(!vis[i]){
val[i] = start;
dfs(i,c);
start--;
}
}
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 8 | 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 | 1a72d7d9a8c663ab06e2675f49cc842d | 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.util.stream.*;
public class App {
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 Exception {
if (System.getProperty("ONLINE_JUDGE") == null) {
File inputFile = new File("/Users/vipinjain/self/cp/input.txt");
File outputFile = new File("/Users/vipinjain/self/cp/output.txt");
br = new BufferedReader(new FileReader(inputFile));
bw = new BufferedWriter(new FileWriter(outputFile));
}
int tests;
tests = Integer.parseInt(br.readLine());
//tests = 1;
while (tests-- > 0) {
solve();
}
bw.flush();
bw.close();
br.close();
}
static void solve() throws Exception {
// String[] tmp = br.readLine().split(" ");
// int a = Integer.parseInt(tmp[0]);
// int b = Integer.parseInt(tmp[1]);
// int c = Integer.parseInt(tmp[2]);
int n = Integer.parseInt(br.readLine());
//int[] arr = Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] arr = new int[n];
for (int i = 0; i < n; ++i) {
arr[i] = i + 1;
}
bw.write(n + "\n");
printArray(arr);
bw.write("\n");
for (int i = 0; i < n - 1; ++i) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
printArray(arr);
bw.write("\n");
}
//bw.write("\n");
}
static void printArray(int[] arr) throws IOException{
StringBuilder sb = new StringBuilder();
for (int num : arr) {
sb.append(num + " ");
}
bw.write(sb.toString().trim());
}
static void shuffleArray(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
} | 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 8 | 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 | 96861ca273712c280ea9b0097bc8a86a | 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 Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
System.out.print(solve(n));
}
}
private static String solve(int n) {
StringBuilder result = new StringBuilder();
result.append(n).append("\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
result.append(j + 2).append(" ");
}
result.append(1).append(" ");
for (int j = i + 1; j < n; j++) {
result.append(j + 1).append(" ");
}
result.append("\n");
}
return result.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 8 | 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 | 6fcf577bcc07989980095105c45a6fd0 | 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.*;
import java.util.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static PrintWriter out;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt();
solve(N);
}
in.close();
out.close();
}
private static void solve(int n) {
pr(n);
int[] res = new int[n];
for (int i = 0; i < res.length; i++) {
res[i] = i + 1;
}
int lo = 0;
for (int i = 0; i < n; i++) {
for (int num : res) {
out.print(num + " ");
}
pr("");
if (lo < n - 1)
swap(res, lo, lo + 1);
lo++;
}
}
private static void swap(int[] nums, int a, int b) {
int t = nums[a];
nums[a] = nums[b];
nums[b] = t;
}
private static void pr(String s) {
out.println(s);
}
private static void pr(int s) {
out.println(s);
}
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();
}
}
}
| 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 8 | 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 | 8aff86e50d906f024b77627f9cd65950 | 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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class B {
public static void main(String[]args) throws IOException {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
out.println(n);
for(int i=0;i<n;i++) {
for(int j=0;j<i;j++) {
out.print((j+2)+" ");
}
out.print("1 ");
for(int j=i+1;j<n;j++) {
out.print((j+1)+" ");
}
out.println();
}
}
out.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public boolean hasNext() {return st.hasMoreTokens();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
| 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 8 | 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 | 95473647938d2360202ff7b0a013108a | 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 | /*
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
Motto: Do what i Love <=> Love what i do
If you don't use your brain 100%, it deteriorates gradually
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class C {
static StringBuffer str=new StringBuffer();
static int n;
static void solve(){
int a[]=new int[n+1];
str.append(n).append("\n");
for(int i=1;i<=n;i++){
a[i]=i;
str.append(a[i]).append(" ");
}
str.append("\n");
int j=2;
a[n]=1;
a[1]=n;
for(int i=1;i<=n;i++) str.append(a[i]).append(" ");
str.append("\n");
int i1=n-1, i2=n;
while(j<n){
a[i1]^=a[i2];
a[i2]^=a[i1];
a[i1]^=a[i2];
for(int i=1;i<=n;i++) str.append(a[i]).append(" ");
str.append("\n");
i1--;
i2--;
j++;
}
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
int q1 = Integer.parseInt(bf.readLine().trim());
while (q1-- > 0) {
n=Integer.parseInt(bf.readLine().trim());
solve();
}
pw.println(str);
pw.flush();
// System.out.print(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 8 | 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 | ab17e8539e8d66504aafa1c71d9c4116 | 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 sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
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;
sb.append(n).append('\n');
for (int x: arr)
sb.append(x + " ");
sb.append('\n');
for (int i=0; i<n-1; ++i) {
int tmp=arr[n-1];
arr[n-1]=arr[i];
arr[i]=tmp;
for (int x: arr)
sb.append(x + " ");
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 8 | 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 | 42e6adb1e323b0410c2431370a684e58 | 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.*;
import static java.time.Year.isLeap;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
Solve solver = new Solve();
int T = 1;
T = in.nextInt();
for (int i = 1; i <= T; i++) {
solver.solve(i, in, out);
}
out.close();
}
static class Solve {
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static final long inf = 0x3f3f3f3f;
static final int N = (int) 2e5 + 7;
static final int mod = (int) 1e9 + 7;
static final int[] monthDay = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
public void solve(int testNumber, InputReader in, OutputWriter out) {
//ow.println("Case #"+testNumber+": ");
//write code here
//long startTime = System.currentTimeMillis();
int n = in.nextInt();
List<Integer> t = new ArrayList<>();
for (int i = 0; i < n; i++) {
t.add(i+1);
}
List<List<Integer>> l = new ArrayList<>();
for (int i = 0; i < n; i++) {
l.add(new ArrayList<>(t));
int k = t.get(i);
t.set(i,t.get(n-1));
t.set(n-1,k);
}
out.println(l.size());
for (List<Integer> its : l) {
for (int it : its) {
out.print(it+" ");
}
out.println();
}
//long endTime = System.currentTimeMillis();int time=(int) (endTime - startTime);System.out.println("运行时间:" + time + "ms");
//here is end!
}
static void test(int[] nums, OutputWriter out) {
for (int num : nums) {
out.print(num + " ");
}
out.println();
}
}
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 println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.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 nextInt() {
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 long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
long sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long 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 nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return nextString();
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
} | 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 8 | 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 | 24f6fcc4b401d5cc0336896e86c82e6f | 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 codeforces1716B {
public static void main(String[] args) throws Exception {
FastReader in = new FastReader();
int numCases = in.nextInt();
StringBuilder ans = new StringBuilder();
while(numCases-->0)
{
int n = in.nextInt();
int [] arr = new int[n];
for (int i = 0; i<n;i++)
{
arr[i] = i+1;
}
ans.append(n+"\n");
for (int i = 0; i<n;i++)
{
ans.append(arr[i]+" ");
}
ans.append("\n");
for (int a = 1; a<n;a++)
{
int hold = arr[a-1];
arr[a-1] = arr[a];
arr[a] = hold;
for (int i = 0; i<n;i++)
{
ans.append(arr[i]+" ");
}
ans.append("\n");
}
}
System.out.println(ans);
}
static class FastReader {
StringTokenizer st;
BufferedReader br;
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 = "";
while (st == null || st.hasMoreElements()) {
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
return 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 8 | 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 | 48902e1e4c791927ef5cb35482af0623 | 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.time.*;
import static java.time.temporal.ChronoUnit.MINUTES;
import javafx.util.Pair;
public class Program {
public static void print(Object str) {
System.out.print(str);
}
public static void println(Object str) {
System.out.println(str);
}
public static void printArr(int[] arr) {
// for(int i=0;i<arr.length;i++) {
// System.out.print(arr[i]+" ");
// }
// System.out.println("");
StringBuilder sb = new StringBuilder("");
for(int i=0;i<arr.length;i++) {
sb.append(arr[i]+" ");
}
System.out.println(sb.toString());
}
public static void printArr2(Object[][] arr) {
int n = arr.length;
int m = arr[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
}
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) {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
// code
FastReader sc = new FastReader();
int t = sc.nextInt();
for(int tt=0; tt<t; tt++) {
int n = sc.nextInt();
// long[] arr = new long[n];
// for(int i=0;i<n;i++) {
// arr[i] = sc.nextLong();
// }
Object result = find(n);
// println(result);
}
return;
}
public static Object find(int n) {
println(n);
int[] arr = new int[n];
for(int i=1;i<=n;i++) {
arr[i-1] = i;
}
printArr(arr);
for(int i=1;i<n;i++) {
swap(arr, i, i-1);
printArr(arr);
}
return 0;
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[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 8 | 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 | 0cdbe07a1af053ffd55708d87172ba79 | 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.*;
@SuppressWarnings("unused")
public class B_Permutation_Chain {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(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;
out.println(arr.length);
int i = 0;
while (n-- > 0) {
for (int x : arr) {
out.print(x + " ");
}
out.println();
if (n >= 1)
swap(arr, i, i + 1);
i++;
}
}
out.close();
}
public static PrintWriter out;
public static long mod = (long) 1e9 + 7;
public static int[] parent = new int[101];
public static int[] rank = new int[101];
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());
}
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[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void google(int tt) {
out.print("Case #" + (tt) + ": ");
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
static int[][] matrixMul(int[][] a, int[][] m) {
if (a[0].length == m.length) {
int[][] b = new int[a.length][m.length];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
int sum = 0;
for (int k = 0; k < m.length; k++) {
sum += m[i][k] * m[k][j];
}
b[i][j] = sum;
}
}
return b;
}
return null;
}
static void swap(int[] a, int l, int r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
static void SieveOfEratosthenes(int n, boolean prime[]) {
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true)
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
static void dfs(int root, boolean[] vis, int[] value, ArrayList[] gr, int prev) {
vis[root] = true;
value[root] = 3 - prev;
prev = 3 - prev;
for (int i = 0; i < gr[root].size(); i++) {
int next = (int) gr[root].get(i);
if (!vis[next])
dfs(next, vis, value, gr, prev);
}
}
static boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
static boolean isPrime(long n) {
for (long i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
static int abs(int a) {
return a > 0 ? a : -a;
}
static int max(int a, int b) {
return a > b ? a : b;
}
static int min(int a, int b) {
return a < b ? a : b;
}
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static boolean isSquare(double a) {
boolean isSq = false;
double b = Math.sqrt(a);
double c = Math.sqrt(a) - Math.floor(b);
if (c == 0)
isSq = true;
return isSq;
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
static long pow(long n, long m) {
if (m == 0)
return 1;
long temp = pow(n, m / 2);
long res = ((temp * temp) % mod);
if (m % 2 == 0)
return res;
return (res * n) % mod;
}
static long modular_add(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
static long modular_sub(long a, long b) {
return ((a % mod) - (b % mod) + mod) % mod;
}
static long modular_mult(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static int gcd(int n1, int n2) {
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
static int find(int u) {
if (u == parent[u])
return u;
return parent[u] = find(parent[u]);
}
static void union(int u, int v) {
int a = find(u), b = find(v);
if (a == b)
return;
if (rank[a] > rank[b]) {
parent[b] = a;
rank[a] += rank[b];
} else {
parent[a] = b;
rank[b] += rank[a];
}
}
static void dsu() {
for (int i = 0; i < 101; i++) {
parent[i] = i;
rank[i] = 1;
}
}
static class Pair {
int x[], y;
Pair(int x[], int y) {
this.x = x;
this.y = y;
}
static void sortbyy(Pair[] coll) {
List<Pair> al = new ArrayList<>(Arrays.asList(coll));
Collections.sort(al, new Comparator<Pair>() {
public int compare(Pair p1, Pair p2) {
return p1.y + p2.y;
}
});
for (int i = 0; i < al.size(); i++) {
coll[i] = al.get(i);
}
}
static void sortbyy(ArrayList<Pair> al) {
Collections.sort(al, new Comparator<Pair>() {
public int compare(Pair p1, Pair p2) {
return Integer.compare(p1.y, p2.y);
}
});
}
public String toString() {
return String.format("(%s, %s)", Arrays.toString(x), String.valueOf(y));
}
}
static void sort(int[] a) {
ArrayList<Integer> list = new ArrayList<>();
for (int i : a)
list.add(i);
Collections.sort(list);
for (int i = 0; i < a.length; i++)
a[i] = list.get(i);
}
static void sort(long a[]) {
ArrayList<Long> list = new ArrayList<>();
for (long i : a)
list.add(i);
Collections.sort(list);
for (int i = 0; i < a.length; i++)
a[i] = list.get(i);
}
static int[] array(int n, int value) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = value;
return a;
}
static long sum(long a[]) {
long sum = 0;
for (long i : a)
sum += i;
return (sum);
}
static long count(long a[], long x) {
long c = 0;
for (long i : a)
if (i == x)
c++;
return (c);
}
static int sum(int a[]) {
int sum = 0;
for (int i : a)
sum += i;
return (sum);
}
static int count(int a[], int x) {
int c = 0;
for (int i : a)
if (i == x)
c++;
return (c);
}
static int count(String s, char ch) {
int c = 0;
char x[] = s.toCharArray();
for (char i : x)
if (ch == i)
c++;
return (c);
}
static int[] freq(int a[], int n) {
int f[] = new int[n + 1];
for (int i : a)
f[i]++;
return f;
}
static int[] pos(int a[], int n) {
int f[] = new int[n + 1];
for (int i = 0; i < n; i++)
f[a[i]] = i;
return f;
}
static boolean isPalindrome(String s) {
StringBuilder sb = new StringBuilder();
sb.append(s);
String str = String.valueOf(sb.reverse());
if (s.equals(str))
return true;
else
return false;
}
} | 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 8 | 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 | 5f75e47bde4187260b90ea999a6fb35b | 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);
int t = in.nextInt();
for (int zz = 0; zz < t; zz++) {
int n = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n;i++) {
a[i] = i + 1;
}
System.out.println(n);
for(int i = 0; i < n; i++) {
StringBuilder s = new StringBuilder();
for(int k = 0; k < n;k++) {
s.append(a[k]).append(" ");
}
System.out.println(s);
if(i!=n-1) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = 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 8 | 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 | 377ee3c0d8ce2fc9b0a15f2b465b1725 | 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 | // "static void main" must be defined in a public class.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T = fs.nextInt();
for(int i = 0; i < T; i++) {
int N = fs.nextInt();
out.println(N);
int[] A = new int[N+1];
for(int j = 1; j <=N; j++) {
A[j] = j;
}
for(int j = 1; j<=N; j++) {
out.print(A[j] + " ");
}
out.println();
int start = 1;
int end = N;
while(start<end) {
int temp = A[start];
A[start] = A[end];
A[end] = temp;
for(int k = 1; k<=N; k++) {
out.print(A[k] + " ");
}
start++;
out.println();
}
if(N%2 == 0) {
continue;
}
}
out.close();
}
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());
}
}
} | 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 8 | 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 | 05a9fa58ab2aadddc4ab8cf76d01c46a | 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 Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(in.readLine());
while(t-- > 0) {
int n = Integer.parseInt(in.readLine());
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = i + 1;
}
out.write(n+"\n");
for(int i = 0; i < n; i++) {
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
for(int j = 0; j < n; j++) {
out.write(arr[j]+" ");
}
out.write("\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 8 | 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 | 94ab2b343343ef98b774859a3fee45b7 | 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 | //created by Toufique on 04/08/2022
import java.io.*;
import java.util.*;
public class EDU_ROUND_133B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
int[][] ans = new int[n][n];
int x = 1;
for (int i = 0; i < n; i++)ans[0][i] = x++;
int ii = 1;
for (int i = n - 2; i >= 0; i--) {
int temp = ans[ii - 1][i + 1];
int temp2 = ans[ii - 1][i];
ans[ii][i] = temp;
ans[ii][i + 1] = temp2;
for (int j = 0; j < n; j++) {
if (ans[ii][j] == 0)ans[ii][j] = ans[ii - 1][j];
}
ii++;
}
pw.println(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
pw.print(ans[i][j] + " ");
}
pw.println();
}
}
pw.close();
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| 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 8 | 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 | 92e1e9f52941e398be1e67b47f088bbb | 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 Codeforces {
static int M = 1_000_000_007;
static int INF = 2_000_000_000;
static int N = (int) 2e5 + 1;
static long[] factorial;
static boolean[] isPrime = new boolean[N+1];
static final FastScanner fs = new FastScanner();
static final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
//variable
public static void main(String[] args) throws IOException {
int T = fs.nextInt();
while (T-- > 0) {
int n = fs.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++) {
arr[i] = i+1;
}
System.out.println(n);
printArray(arr, n);
for(int i=0; i<n-1; i++) {
int temp = arr[i];
arr[i] = arr[n-1];
arr[n-1] = temp;
printArray(arr, n);
}
}
}
//class
private static class Edge implements Comparable<Edge>{
long distance;
int i, j;
Edge(long distance, int i, int j) {
this.distance = distance;
this.i = i;
this.j = j;
}
@Override
public int compareTo(Edge o) {
return Long.compare(this.distance, o.distance);
}
}
//function
static void printArray(int[] arr, int n) throws IOException {
for(int i=0; i<n; i++) {
out.write(arr[i]+" ");
}
out.write("\n");
out.flush();
}
static long distanceBetweenPoint(Pairs p1, Pairs p2) {
return (long) (p1.f - p2.f) * (p1.f - p2.f) + (long) (p1.s - p2.s) *(p1.s-p2.s);
}
static void getPrimes() {
for(int i=2;i<N;i++) isPrime[i] = true;
int i = 2;
while(i<N) {
if(isPrime[i]) {
for(int j=2*i;j<N;j+=i)
isPrime[j] = false;
}
System.out.println(i+" "+isPrime[i]);
i++;
}
}
static void build(int[] a, int[] seg, int ind, int low, int high) {
if (low == high) {
seg[ind] = a[low];
return;
}
int mid = (low + high) / 2;
build(a, seg, 2 * ind + 1, low, mid);
build(a, seg, 2 * ind + 2, mid + 1, high);
seg[ind] = Math.max(seg[2 * ind + 1], seg[2 * ind + 2]);
}
static long query(int ind, int[] seg, int l, int h, int low, int high) {
if (low > h || high < l) return -INF;
if (low >= l && high <= h) return seg[ind];
int mid = (low + high) / 2;
long left = query(2 * ind + 1, seg, l, h, low, mid);
long right = query(2 * ind + 2, seg, l, h, mid + 1, high);
return Math.max(left, right);
}
// Template
static long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
static void premutation(int n, ArrayList<Integer> arr, boolean[] chosen) {
if (arr.size() == n) {
System.out.println(arr);
} else {
for (int i = 1; i <= n; i++) {
if (chosen[i]) continue;
arr.add(i);
chosen[i] = true;
premutation(n, arr, chosen);
arr.remove(Integer.valueOf(i));
chosen[i] = false;
}
}
}
static boolean isPalindrome(char[] c) {
int n = c.length;
for (int i = 0; i < n / 2; i++) {
if (c[i] != c[n - i - 1]) return false;
}
return true;
}
static long nCk(int n, int k) {
return (modMult(fact(n), fastExpo(modMult(fact(n - k), fact(k)), M - 2)));
}
static long nPk(int n, int k) {
return (modMult(fact(n), fastExpo(fact(n - k), M - 2)));
}
static long fact(int n) {
if (factorial != null) return factorial[n];
else factorial = new long[N];
factorial[0] = 1;
long fact = 1;
for (int i = 1; i < N; i++) {
factorial[i] = fact = modMult(fact, i);
}
return factorial[n];
}
static long modMult(long a, long b) {
return (int) (a * b % M);
}
static long negMult(long a, long b) {
return (int) ((a * b) % M + M) % M;
}
static long fastExpo(long x, int y) {
if (y == 1) return x;
if (y == 0) return 1;
long ans = fastExpo(x, y / 2);
if (y % 2 == 0) return modMult(ans, ans);
else return modMult(ans, modMult(ans, x));
}
static final Random random = new Random();
static void ruffleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int j = random.nextInt(n);
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
private static class Pairs implements Comparable<Pairs> {
int f, s;
Pairs(int f, int s) {
this.f = f;
this.s = s;
}
public int compareTo(Pairs p) {
if (this.f != p.f) return Integer.compare(this.f, p.f);
return -Integer.compare(this.s, p.s);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pairs)) return false;
Pairs pairs = (Pairs) o;
return f == pairs.f && s == pairs.s;
}
@Override
public int hashCode() {
return Objects.hash(f, s);
}
}
private static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer str = new StringTokenizer("");
String next() throws IOException {
while (!str.hasMoreTokens())
str = new StringTokenizer(br.readLine());
return str.nextToken();
}
char nextChar() throws IOException {
return next().charAt(0);
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
float nextFloat() throws IOException {
return Float.parseFloat(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
byte nextByte() throws IOException {
return Byte.parseByte(next());
}
int[] arrayIn(int n) throws IOException {
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 8 | 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 | 5893f9d0ff465c3f61c0889c24f5a4cb | 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 Cses {
static boolean[] table = new boolean[10001];
static StringBuilder sb = new StringBuilder();
static TreeSet<String> sn = new TreeSet<>();
static List<Long> sm = new ArrayList<>();
static boolean[][] visited = new boolean[7][7];
static int numberOfWays = 0;
static class Point {
int a;
int b;
public Point(int a, int b) {
this.a = a;
this.b = b;
}
}
static int ways;
static void dfs(int n, int m, int i, int j, boolean[][] visited, int steps) {
System.out.println(i + " " + j);
boolean r, l, u, d = false;
visited[i][j] = true;
steps++;
if (i < n - 1) if (!visited[i + 1][j]) {
dfs(n, m, i + 1, j, visited, steps);
}
if (j < m - 1) if (!visited[i][j + 1]) dfs(n, m, i, j + 1, visited, steps);
if (i > 0) if (!visited[i - 1][j]) dfs(n, m, i - 1, j, visited, steps);
if (j > 0) if (!visited[i][j - 1]) dfs(n, m, i, j - 1, visited, steps);
if (steps == 3 * 3) ways++;
}
static void sieveOfEratosthenes() {
// 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.
for (int i = 0; i <= 10000; i++) {
table[i] = true;
}
for (int p = 2; p * p <= 100000000; p++) {
// If prime[p] is not changed, then it is a prime
if (table[p]) {
// Update all multiples of p
for (int i = p * p; i <= 100000000; i += p)
table[i] = false;
}
}
// Print all prime numbers
}
public static void minDistance(int[] tab, long s, int index) {
if (tab.length == index) {
sm.add(s);
return;
}
int go = tab[index];
index++;
minDistance(tab, s, index);
minDistance(tab, s + go, index);
}
public static void steps(int n, int start, int end) {
int mid = 0;
if (n == 0) return;
if (start + end == 3) mid = 3;
if (start + end == 4) mid = 2;
if (start + end == 5) mid = 1;
steps(n - 1, start, mid);
sb.append(start).append(" ").append(end).append(System.lineSeparator());
steps(n - 1, mid, end);
}
public static void print_rec(LinkedList<String> strings, StringBuilder s) {
if (strings.size() == 0) {
sn.add(String.valueOf(s));
return;
}
for (int i = 0; i < strings.size(); i++) {
StringBuilder ss = new StringBuilder();
LinkedList<String> subStrings = (LinkedList<String>) strings.clone();
subStrings.remove(i);
ss.append(s).append(strings.get(i));
print_rec(subStrings, ss);
}
}
static boolean[] ld = new boolean[15];
static boolean[] rd = new boolean[15];
static boolean[] col = new boolean[8];
static boolean[][] reserved = new boolean[8][8];
static void callNumberOfWays(int j) {
if (j == 8) {
numberOfWays++;
return;
}
for (int i = 0; i < 8; i++) {
if (!ld[i + j] && !rd[i - j + 7] && !col[i] && !reserved[j][i]) {
ld[i + j] = rd[i - j + 7] = col[i] = true;
callNumberOfWays(j + 1);
ld[i + j] = rd[i - j + 7] = col[i] = false;
}
}
}
static int comp(String s1, String s2) {
char[] tab1 = s1.toCharArray();
char[] tab2 = s2.toCharArray();
int res = 0;
for (int i = 0; i < s1.length(); i++) {
res += Math.abs(tab1[i] - tab2[i]);
}
return res;
}
static int binarySearch(ArrayList<Long> arr, int l, int r, long x) {
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr.get(mid) == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr.get(mid) > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
} else return l;
// We reach here when element is not present
// in array
}
static int binarySearch2(ArrayList<Integer> arr, int l, int r, long x) {
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr.get(mid) == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr.get(mid) > x)
return binarySearch2(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch2(arr, mid + 1, r, x);
} else return l;
// We reach here when element is not present
// in array
}
static public int findComplement(int num) {
int MSB = (int) (Math.log(num) / Math.log(2)); // finding the Most Significant Bit position of num
int mask = ((1 << MSB) | ((int) Math.pow(2, MSB) - 1)); // creating a mask like - 00..MSB..11111. So all zeroes till MSB and MSB and afterwards it is all ones.
return num ^ mask; // now we can simply xor it - which is used as an inverter of bits
}
static public boolean checkswap(int[][] arr, int n, int m, int in, int io) {
int[] aa1 = new int[n];
int[] aa2 = new int[n];
for (int i = 0; i < n; i++) {
aa1[i] = arr[i][in];
aa2[i] = arr[i][io];
}
for (int i = 0; i < n; i++) {
arr[i][io] = aa1[i];
arr[i][in] = aa2[i];
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < m; j++) {
if (arr[i][j] < arr[i][j - 1]) return false;
}
}
return true;
}
static boolean check(int a) {
if (a == 0) return true;
else {
int b = a - 1;
return ((a & b) == 0);
}
}
;
static int findmin(int[] tab, int l, int r) {
int min = tab[l];
for (int i = l; i < r; i++) {
if (tab[i] < min) min = tab[i];
}
return min;
}
public static void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
class Element implements Comparable<Element> {
int id;
int ti;
int ram;
public Element(int id, int ti, int ram) {
this.ti = ti;
this.id = id;
this.ram = ram;
}
@Override
public int compareTo(Element el) {
if (el.ti == this.ti) {
if (el.ram == this.ram) {
return el.id - this.id;
} else return el.ram - this.ram;
} else return el.ti - this.ti;
}
}
// Java program to Find upper bound
// Using Binary Search Iteratively
// Importing Arrays utility class
// Main class
// Iterative approach to find upper bound
// using binary search technique
static void upper_bound(int arr[], int key) {
int mid, N = arr.length;
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr[mid]) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
if (low == N) {
System.out.print("The upper bound of " + key + " does not exist.");
return;
}
// Print the upper_bound index
System.out.print("The upper bound of " + key + " is " + arr[low] + " at index " + low);
}
static int recursive_lower_bound(int array[], int low,
int high, int key) {
// Base Case
if (low > high) {
return low;
}
// Find the middle index
int mid = low + (high - low) / 2;
// If key is lesser than or equal to
// array[mid] , then search
// in left subarray
if (key <= array[mid]) {
return recursive_lower_bound(array, low,
mid - 1, key);
}
// If key is greater than array[mid],
// then find in right subarray
return recursive_lower_bound(array, mid + 1, high,
key);
}
// Method 2
// To compute the lower bound
static int lower_bound(int array[], int key) {
// Initialize starting index and
// ending index
int low = 0, high = array.length;
// Call recursive lower bound method
return recursive_lower_bound(array, low, high, key);
}
static public class pair implements Comparable<pair> {
int a;
int b;
pair(int p, int q) {
a = p;
b = q;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.a == a && p.b == b;
}
return false;
}
public int hashCode() {
return new Integer(a).hashCode() * 31 + new Integer(b).hashCode();
}
@Override
public int compareTo(pair o) {
if (this.a == o.a) {
return o.b - this.b;
}
return this.a - o.a;
}
@Override
public String toString() {
return "pair{" +
"a=" + a +
", b=" + b +
'}';
}
}
public static void add_map_occurance(HashMap<Integer, Integer> hashMap, int a) {
if (hashMap.containsKey(a)) hashMap.put(a, hashMap.get(a) + 1);
else hashMap.put(a, 1);
}
public static void rm_map_occurance(HashMap<Integer, Integer> hashMap, int a) {
if (hashMap.containsKey(a)) {
if (hashMap.get(a) == 1) hashMap.remove(a);
else hashMap.put(a, hashMap.get(a) - 1);
}
}
///////////////////////////////nice////////////////////////
/////////////////////////////the smallest higher then x ///////////////
public static int call(ArrayList<Integer> array, int a, int b, int x) {
if (a == array.size()) {
//System.out.println("n "+a);
return -1;
}
if (a > b) return a;
else {
int m = (a + b) / 2;
// System.out.println(a+" "+b);
if (array.get(m) <= x) return call(array, m + 1, b, x);
else return call(array, a, m - 1, x);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) throws Exception {
Reader s = new Reader();
// BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int[] tab = new int[n];
for (int i = 0; i < n; i++) {
tab[i]=i+1;
}
StringBuilder res =new StringBuilder();
res.append(n).append(System.lineSeparator());
for (int j = 0; j < n; j++) {
res.append(tab[j]).append(" ");
}
res.append(System.lineSeparator());
for (int i = 1; i < n ; i++) {
swap(tab,i,i-1);
for (int j = 0; j < n; j++) {
res.append(tab[j]).append(" ");
}
res.append(System.lineSeparator());
}
System.out.print(res);
}
}
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 main(String[] args)
throws IOException
{
Reader s = new Reader();
int n = s.nextInt();
int k = s.nextInt();
int count = 0;
while (n-- > 0) {
int x = s.nextInt();
if (x % k == 0)
count++;
}
System.out.println(count);
}*/
} | 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 8 | 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 | d6c124fb2c7e54579d787676631236c0 | 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.math.BigInteger;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import static java.lang.System.out;
public class app {
public static ArrayList<ArrayList<Integer>>as=new ArrayList<>();
public static Boolean x[];
public static int z;
public static long m;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int s=sc.nextInt();
String z[]=new String[s];
for(int i=0;i<s;i++){
z[i]=i+1+"";
}
out.println(s);
int sa=s-2;
System.out.println(Arrays.toString(z).replace(",","").replace("]","").replace("[",""));
for(int i=1;i<s;i++){
String n=z[sa];
z[sa]=z[s-1];
z[s-1]=n;
System.out.println(Arrays.toString(z).replace(",","").replace("]","").replace("[",""));
sa--;
}
}
}
}
| 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 8 | 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 | 79b5a12e4c2cc4478dd25fed1a4737cc | 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{
PrintWriter out = new PrintWriter(System.out);
Input in = new Input();
int test = in.getInt();
while(test-->0){
int n = in.getInt();
out.println(n);
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] = i+1;
}
for(int i=n-1;i>=0;i--){
if(i<n-1){
int t = a[i];
a[i] = a[i+1];
a[i+1] = t;
}
for(int j=0;j<n;j++){
out.print(a[j]+" ");
}
out.println();
}
out.flush();
}
out.close();
in.close();
}
}
class Input{
BufferedReader in;
StringTokenizer st;
public Input() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
st = null;
}
public String getString() throws Exception{
while(st==null||st.hasMoreTokens()==false){
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public boolean getBoolean() throws Exception{
return Boolean.parseBoolean(getString());
}
public byte getByte() throws Exception{
return Byte.parseByte(getString());
}
public short getShort() throws Exception{
return Short.parseShort(getString());
}
public int getInt() throws Exception{
return Integer.parseInt(getString());
}
public long getLong() throws Exception{
return Long.parseLong(getString());
}
public float getFloat() throws Exception{
return Float.parseFloat(getString());
}
public double getDouble() throws Exception{
return Double.parseDouble(getString());
}
public String getLine() throws Exception{
return in.readLine();
}
public void close() throws Exception{
if(in!=null) in.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 8 | 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 | 73bfd3bbad1e2a5036b0854c6b2dee36 | 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;
/**
*
* @author Acer
*/
public class NewClass_B {
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 str = new StringBuilder();
if(i == 1){
for (int j = 1; j <= n; j++) {
str.append(j+" ");
}
}
else{
int k = 2;
for (int j = 1; j <= n; j++) {
if(i == j){
str.append(1+" ");
}
else{
str.append(k+" ");
k++;
}
}
}
System.out.println(str.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 8 | 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 | 7b0f047d1499bb4c22309eb85998c891 | 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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int n, t, w;
static long cnt, mod = (int) 1e9 + 7;
static int[] time, v;
static int[][] memo;
static String res[];
static PrintWriter pw = new PrintWriter(System.out);
static boolean ok(int i) {
int sum = 0;
while (i > 0) {
sum += i % 10;
i /= 10;
}
return sum % 4 == 0;
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int arr[] = new int[n];
int k = n;
int lol = 0;
pw.println(k);
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
pw.print((i + 1) + " ");
}
pw.println();
while (k-- > 1)
{
int temp = 1;
if (k == n - 1) {
arr[lol] = arr[n - 1];
arr[n - 1] = temp;
lol = n - 1;
} else {
arr[lol] = arr[lol - 1];
arr[lol - 1] = 1;
lol--;
}
for (int i = 0; i < n; i++) {
pw.print(arr[i] + " ");
}
pw.println();
}
}
pw.close();
}
/*
* 5 7 8 9 10 11
*/
static int lol = 0;
static int dp(int i, int remtime) {
int take = 0, leave = 0;
if (i == n)
return 0;
if (memo[i][remtime] != -1)
return memo[i][remtime];
if (remtime >= time[i])
take = v[i] + dp(i + 1, remtime - time[i]);
leave = dp(i + 1, remtime);
return memo[i][remtime] = Math.max(take, leave);
}
static void build(int i, int remtime) {
int take = -1, leave = 0;
if (i == n)
return;
if (remtime >= time[i])
take = v[i] + dp(i + 1, remtime - time[i]);
leave = dp(i + 1, remtime);
int result = Math.max(take, leave);
if (result == take) {
res[lol++] = (time[i] / (3 * w)) + " " + v[i];
build(i + 1, remtime - time[i]);
} else {
build(i + 1, remtime);
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | 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 8 | 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 | b9739673ed92eaa6dd6e0ff157c7a8db | 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 Main{
static int N=100010;
public static void main(String[] args) {
int t=cin.nextInt();
while(t-->0) {
int n=cin.nextInt();
out.println(n);
int a[]=new int[n+1];
for(int i=0;i<=n;i++) {
a[i]=i;
if(i>1)out.print(" ");
if(i>0)out.print(a[i]);
}
out.println();
for(int i=1;i<n;i++) {
int tmp=a[i];
a[i]=a[n];a[n]=tmp;
for(int j=0;j<=n;j++) {
if(j>1)out.print(" ");
if(j>0)out.print(a[j]);
}
out.println();
}
out.flush();
}
out.flush();
}
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.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 8 | 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 | 004f64fa450c727546d1b9b5388f87b4 | 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.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.util.Arrays.sort;
import java.util.*; import java.io.*;import java.math.*;
public class Main{
static int mod=1000000007;
static int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;
static long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
public static void main(String[] args) {
FastScanner sc = new FastScanner() ;
StringBuilder sb = new StringBuilder();
int t=1 ;
t = sc.nextInt() ;
while(t-->0){
int n = sc.nextInt() ;
int[] a = new int[n] ;
for (int i = 0; i < n; i++) {
a[i] = i+1 ;
}
sb.append(n+"\n") ;
for (int j = 0; j < n; j++) {
sb.append(a[j]+" ");
}
sb.append("\n") ;
for (int i = 1; i < n; i++) {
int tx = a[0] ;
a[0] = a[i] ;
a[i] = tx ;
for (int j = 0; j < n; j++) {
sb.append(a[j]+" ");
}
sb.append("\n") ;
}
}
System.out.print(sb);
}
static int[] so(int ar[]){Integer r[]=new Integer[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];
sort(r);for(int x=0;x<ar.length;x++)ar[x]=r[x];return ar;}
static long[] so(long ar[]){Long r[]=new Long[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];
sort(r);for(int x=0;x<ar.length;x++)ar[x]=r[x];return ar;}
static char[] so(char ar[]){Character r[]=new Character[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];
sort(r);for(int x=0;x<ar.length;x++)ar[x]=r[x];return ar;}
static void print(int[] arr){ for(int x: arr){System.out.print(x+" ");} System.out.println(); }
static void print(long[] arr){ for(long x: arr){System.out.print(x+" ");} System.out.println(); }
static int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); }
static class FastScanner {
BufferedReader br; StringTokenizer st;
public FastScanner() { 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 8 | 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 | 95707d73c22f6a6d2113f9b61cd723f4 | 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.Arrays;
import java.util.StringTokenizer;
public class round177 {
static int[] arr,segmenttree;
static PrintWriter out;
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
arr=new int[n];
for (int i = 0; i < n; i++)
arr[i]=i+1;
out.println(n);
print();
for (int i = n-1; i >0 ; i--) {
int temp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=temp;
print();
}
}
out.flush();
}
public static void print(){
for (int i = 0; i < arr.length; i++)
out.print(arr[i] +" ");
out.println();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| 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 8 | 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 | e4b8e92b1ed4689194fd087b6ff47fa0 | 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
{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader sc = new FastReader();
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;
bw.write(n + "\n");
for (int i = 0; i < n; i++)
bw.write(arr[i] + " ");
bw.write("\n");
int temp = arr[0];
arr[0] = arr[n-1];
arr[n-1] = temp;
for(int i=0; i<n; i++)
bw.write(arr[i] + " ");
bw.write("\n");
int idx = n-2;
while(idx != 0)
{
temp = arr[idx];
arr[idx] = arr[idx+1];
arr[idx+1] = temp;
for (int j = 0; j < n; j++)
bw.write(arr[j] + " ");
bw.write("\n");
idx--;
}
}
bw.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());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
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 8 | 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 | d0163237eb258f844cf9069d6be58f9d | 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 | // 1 2 3 4 5 -> 5
// 2 1 3 4 5 -> 3
// 3 1 2 4 5 -> 2
// 4 1 2 3 5 -> 1
import java.util.*;
import java.io.*;
public class c306
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while(t-->0)
{
int n = sc.nextInt();
int a[] = new int[n+1];
sb.append(n).append("\n");
for(int i=1;i<=n;i++)
{
sb.append(i).append(" ");
a[i] = i;
}
sb.append("\n");
int pos = 2;
for(int i=1;i<=n-1;i++)
{
int temp = a[1];
a[1] = a[pos];
a[pos] = temp;
for(int j=1;j<=n;j++)
sb.append(a[j]).append(" ");
sb.append("\n");
pos++;
}
}
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 8 | 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 | 23a325636bf4c1f1731a5042c46f3a81 | 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.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pt = new PrintWriter(System.out);
FastReader sc = new FastReader();
// Scanner sc = new Scanner(System.in);
int o1 = sc.nextInt();
for(int t1 = 0; t1 < o1; t1++) {
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println(n);
for(int i = 1 ; i<=n;i++) {
// System.out.print(i + " ");
out.write(i + " ");
arr[i-1] = i;
}
// System.out.println();
out.write("\n");
for(int i = n-1 ; i>0;i--){
// System.out.print(i + " ");
int tmp = arr[i];
arr[i] = arr[i-1];
arr[i-1] = tmp;
for(int x : arr) {
// System.out.print(x + " ");
out.write(x + " ");
}
// System.out.println();
out.write("\n");
}
out.flush();
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
//
public static ArrayList<Long> printDivisors(long n) {
ArrayList<Long> al = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
al.add(i);
else {
al.add(i);
al.add(n / i);
}
}
}
return al;
}
public static boolean palin(String s) {
int n = s.length();
int i = 0;
int j = n - 1;
while (i <= j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public static boolean check(int[] arr, int n, int v, int l) {
int x = v / 2;
int y = v / 2;
// System.out.println(x + " " + y);
if (v % 2 == 1) {
x++;
}
for (int i = 0; i < n; i++) {
int d = l - arr[i];
int c = Math.min(d / 2, y);
y -= c;
arr[i] -= c * 2;
if (arr[i] > x) {
return false;
}
x -= arr[i];
}
return true;
}
public static int cnt_set(long x) {
long v = 1l;
int c = 0;
int f = 0;
while (v <= x) {
if ((v & x) != 0) {
c++;
}
v = v << 1;
}
return c;
}
public static int lis(int[] arr, int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
dp[0] = 1;
for (int i = 1; i < n; i++) {
int x = al.get(al.size() - 1);
if (arr[i] > x) {
al.add(arr[i]);
} else {
int v = lower_bound(al, 0, al.size(), arr[i]);
// System.out.println(v);
al.set(v, arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
public static int lis2(int[] arr, int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(-arr[n - 1]);
dp[n - 1] = 1;
// System.out.println(al);
for (int i = n - 2; i >= 0; i--) {
int x = al.get(al.size() - 1);
// System.out.println(-arr[i] + " " + i + " " + x);
if ((-arr[i]) > x) {
al.add(-arr[i]);
} else {
int v = lower_bound(al, 0, al.size(), -arr[i]);
// System.out.println(v);
al.set(v, -arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
static int cntDivisors(int n) {
int cnt = 0;
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt += 2;
}
}
return cnt;
}
public static long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ncr(long[] fac, int n, int r, long m) {
if (r > n) {
return 0;
}
return fac[n] * (modInverse(fac[r], m)) % m * (modInverse(fac[n - r], m)) % m;
}
public static int lower_bound(ArrayList<Integer> arr, int lo, int hi, int k){
int s = lo;
int e = hi;
while (s != e) {
int mid = s + e >> 1;
if (arr.get(mid) < k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == arr.size()) {
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> arr, int lo, int hi, int k){
int s = lo;
int e = hi;
while (s != e) {
int mid = s + e >> 1;
if (arr.get(mid) <= k) {
s = mid + 1;
} else {
e = mid;
}
}
if (s == arr.size()) {
return -1;
}
return s;
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public static long modInverse(long a, long m) {
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
//_________________________________________________________________________________________________________________________________________________________________
// initially parent of every number is number itself
// initially rank of every number is 0
public static int find(int v, int[] parent) {
if (v == parent[v]) {
return v;
}
return parent[v] = find(parent[v], parent);
}
public static void union(int u, int v, int[] parent, int[] rank) {
u = find(u, parent);
v = find(v, parent);
if (rank[v] < rank[u]) {
parent[v] = u;
} else if (rank[u] < rank[v]) {
parent[u] = v;
} else {
parent[v] = u;
rank[u]++;
}
}
//// private static int[] parent;
//// private static int[] size;
//// public static int find(int[] parent, int u) {
//// while (u != parent[u]) {
//// parent[u] = parent[parent[u]];
//// u = parent[u];
//// }
//// return u;
//// }
////
//// private static void union(int[] parent, int[] size, int u, int v) {
//// int rootU = find(parent, u);
//// int rootV = find(parent, v);
//// if (rootU == rootV) {
//// return;
//// }
//// if (size[rootU] < size[rootV]) {
//// parent[rootU] = rootV;
//// size[rootV] += size[rootU];
//// } else {
//// parent[rootV] = rootU;
//// size[rootU] += size[rootV];
//// }
//// }
////-----------------------------------------------------------------------------------------------------------------------------------
// segment tree
// for finding minimum in range
public static void build(long[] seg, long[] arr, int idx, int lo, int hi) {
if (lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi) / 2;
build(seg, arr, 2 * idx + 1, lo, mid);
build(seg, arr, idx * 2 + 2, mid + 1, hi);
seg[idx] = seg[idx * 2 + 1] + seg[idx * 2 + 2];
}
//for finding minimum in range
public static long query(long[] seg, int idx, int lo, int hi, int l, int r) {
if (lo >= l && hi <= r) {
return seg[idx];
}
if (hi < l || lo > r) {
return 0;
}
int mid = (lo + hi) / 2;
long left = query(seg, idx * 2 + 1, lo, mid, l, r);
long right = query(seg, idx * 2 + 2, mid + 1, hi, l, r);
return left + right;
}
public static void build2(long[] seg, long[] arr, int idx, int lo, int hi) {
if (lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi) / 2;
build2(seg, arr, 2 * idx + 1, lo, mid);
build2(seg, arr, idx * 2 + 2, mid + 1, hi);
seg[idx] = Math.max(seg[idx * 2 + 1], seg[idx * 2 + 2]);
}
public static long query2(long[] seg, int idx, int lo, int hi, int l, int r) {
if (r < l) {
return Long.MIN_VALUE;
}
if (lo >= l && hi <= r) {
return seg[idx];
}
if (hi < l || lo > r) {
return Long.MIN_VALUE;
}
int mid = (lo + hi) / 2;
long left = query(seg, idx * 2 + 1, lo, mid, l, r);
long right = query(seg, idx * 2 + 2, mid + 1, hi, l, r);
return Math.max(left, right);
}
public static void update(boolean[] seg, int idx, int lo, int hi, int node, boolean val) {
if (lo == hi) {
seg[idx] = val;
} else {
int mid = (lo + hi) / 2;
if (node <= mid && node >= lo) {
update(seg, idx * 2 + 1, lo, mid, node, val);
} else {
update(seg, idx * 2 + 2, mid + 1, hi, node, val);
}
seg[idx] = seg[idx * 2 + 1] & seg[idx * 2 + 2];
}
//
}
////---------------------------------------------------------------------------------------------------------------------------------------
//
////
//// static void shuffleArray(int[] ar) {
//// // If running on Java 6 or older, use `new Random()` on RHS here
//// Random rnd = ThreadLocalRandom.current();
//// for (int i = ar.length - 1; i > 0; i--) {
//// int index = rnd.nextInt(i + 1);
//// // Simple swap
//// int a = ar[index];
//// ar[index] = ar[i];
//// ar[i] = a;
//// }
//// }
//// static void shuffleArray(coup[] ar)
//// {
//// // If running on Java 6 or older, use `new Random()` on RHS here
//// Random rnd = ThreadLocalRandom.current();
//// for (int i = ar.length - 1; i > 0; i--)
//// {
//// int index = rnd.nextInt(i + 1);
//// // Simple swap
//// coup a = ar[index];
//// ar[index] = ar[i];
//// ar[i] = a;
//// }
//// }
////-----------------------------------------------------------------------------------------------------------------------------------------------------------
//
}
//
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 coup {
int a;
long b;
public coup(int a, long b) {
this.a = a;
this.b = b;
}
}
class dobb {
char a;
int b;
public dobb(char a, int b) {
this.a = a;
this.b = b;
}
}
class dob {
int a;
double b;
public dob(int a, double b) {
this.a = a;
this.b = b;
}
}
class tripp {
int a;
int b;
long c;
public tripp(int a, int b, long c) {
this.a = a;
this.b = b;
this.c = c;
}
} | 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 8 | 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 | 1310d7aedce3cc1ec3e25ff3d1f2f6d8 | 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 final class Codeforces2
{
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int arr[] = new int[n];
pw.println(n);
for(int i=0;i<n;i++){
arr[i] = i+1;
pw.print(arr[i]+" ");
}
pw.println();
for(int i=1;i<n;i++){
int temp = arr[i-1];
arr[i-1] = arr[i];
arr[i] = temp;
for(int j=0;j<n;j++){
pw.print(arr[j]+" ");
}
pw.println();
}
}
pw.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());
}
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 8 | 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 | 0359ec14918f715c53594495af69562e | 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;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class cfContest1716 {
public static void main(String[] args) throws IOException {
Reader scan = new Reader();
int t = scan.nextInt();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int n = scan.nextInt();
int c = 1;
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = i;
}
sb.append(n + "\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
sb.append(a[j] + " ");
}
if (n != i) {
int temp = a[c + 1];
a[c + 1] = a[c];
a[c] = temp;
++c;
}
sb.append("\n");
}
}
System.out.println(sb);
}
}
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') {
break;
}
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();
}
}
| 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 8 | 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 | a8dae3149526dfefb8b914fe19adf341 | 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 C {
public static long gcd(long a, long b) {
return a == 0 ? b : gcd(b, a % b);
}
public static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t1 = sc.nextInt();
while (t1-- > 0) {
int n = sc.nextInt();
int l [] = new int[n];
for (int i = 0;i<n;i++)
l[i] = i+1;
out.println(n);
for (int i = 0;i<n;i++) {
out.print(l[i]+ " ");
}
out.println();
int tmp = l[0];
l[0] = l[n-1];
l[n-1] = tmp;
for (int i = 0;i<n;i++) {
out.print(l[i]+ " ");
}
out.println();
for (int i = n-1;i>1;i--) {
tmp = l[i-1];
l[i-1] = l[i];
l[i] = tmp;
for (int j = 0;j<n;j++) {
out.print(l[j]+ " ");
}
out.println();
}
}
out.close();
}
static class Pair implements Comparable<Pair> {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair p) {
if (first != p.first)
return Integer.compare(first, p.first);
else if (second != p.second)
return Integer.compare(second, p.second);
else
return 0;
}
}
static final Random random = new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = random.nextInt(n), temp = a[r];
a[r] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
int[] readArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = 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 8 | 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 | 50acc16cadd2791954722ac237422c5a | 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 codeforce.educational.r133;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import static java.lang.System.out;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1716/problem/B" target="_top">https://codeforces.com/contest/1716/problem/B</a>
* @since 04/08/22 8:18 PM
*/
public class B {
static FastScanner sc = new FastScanner(System.in);
public static void main(String[] args) {
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
int n = sc.nextInt();
System.out.println(n);
StringBuilder sb = new StringBuilder();
int[] raw = new int[n];
for (int i = 0; i < n; i++) {
raw[i] = i + 1;
}
sb.append(concat(raw));
swap(raw, n - 2, n - 1);
sb.append(concat(raw));
int si = n - 3;
for (int i = 0; i < n - 2; i++, si--) {
swap(raw, si, si + 1);
sb.append(concat(raw));
}
System.out.print(sb);
// 1 2 3 4 -> 1 2 4 3 -> 1 4 2 3 -> 4 1 2 3
//
}
}
}
private static StringBuilder concat(int[] raw) {
StringBuilder sb = new StringBuilder();
for (int ii : raw)
sb.append(ii).append(" ");
sb.append("\n");
return sb;
}
private static void swap(int[] raw, int i, int j) {
int t = raw[i];
raw[i] = raw[j];
raw[j] = t;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
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 8 | 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 | 6045a5d987c432282dd7a1076245c1a7 | 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 static java.lang.System.*;
public class Solution {
static MyScanner str = new MyScanner();
public static void main(String[] args) throws IOException {
long T = l();
while (T-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = i();
out.println(n);
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
StringBuffer sf = new StringBuffer();
for (int x : a) {
sf.append(x + " ");
}
sf.append("\n");
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n; j++) {
if (j > 0 && a[j] == n) {
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
for (int x : a) {
sf.append(x + " ");
}
sf.append("\n");
}
out.print(sf);
}
public static int i() throws IOException {
return str.nextInt();
}
public static long l() throws IOException {
return str.nextLong();
}
public static double d() throws IOException {
return str.nextDouble();
}
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;
}
}
private static class ArraysInt {
static void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
static void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static void sort(int[] arr) {
sort(arr, 0, arr.length - 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 8 | 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 | 9011a204a03226c33b83403252d08f82 | 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 Main {
public static void main(String args[]) {
Scanner in = new Scanner(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 = n; i > 0; i--) {
for (int j = 1; j <= n-1; j++) {
if(j==i){
out.print(n+" ");
out.print(j+" ");
}else {
out.print(j+" ");
}
}
if(i==n){
out.println(n+" ");
}else {
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 8 | 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 | 105f8f7287806aa52ef5dbc0e4292800 | 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 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintStream out = System.out;
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(br.readLine());
out.println(n);
int[] arr = new int[n];
StringBuilder str = new StringBuilder();
for (int j = 0; j < n; j++) {
arr[j] = j+1;
str.append(j+1);
str.append(" ");
}
out.println(str);
for (int j = 0; j < n-1; j++) {
int x = arr[j];
arr[j] = arr[j+1];
arr[j+1] = x;
str = new StringBuilder();
for (int k = 0; k < n; k++) {
str.append(arr[k]);
str.append(" ");
}
out.println(str);
}
}
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 8 | 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 | f1cc1c7fbaeed06c1a85a471ed8708a4 | 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.sql.Array;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Map.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.System.*;
public class Main
{
public void tq() throws Exception
{
st=new StringTokenizer(bq.readLine());
int tq=i();
sb=new StringBuilder(2000000);
o:
while(tq-->0)
{
int n=i();
int ar[]=new int[n];
for(int x=0;x<n;x++)ar[x]=x+1;
sl(n);
s(ar);
int i=n-2;
for(int x=2;x<=n;x++)
{
ar[i]^=ar[i+1];
ar[i+1]^=ar[i];
ar[i]^=ar[i+1];
i--;
s(ar);
}
}
p(sb);
}
long mod=1000000007l;
int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
BufferedReader bq = new BufferedReader(new InputStreamReader(in));StringTokenizer st;StringBuilder sb;
public static void main(String[] a)throws Exception{new Main().tq();}
int di[][]={{-1,0},{1,0},{0,-1},{0,1}};
int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};
void f1(int ar[],int v){fill(ar,v);}
void f2(int ar[][],int v){for(int a[]:ar)fill(a,v);}
void f3(int ar[][][],int v){for(int a[][]:ar)for(int b[]:a)fill(b,v);}
void f1(long ar[],long v){fill(ar,v);}
void f2(long ar[][],long v){for(long a[]:ar)fill(a,v);}
void f3(long ar[][][],long v){for(long a[][]:ar)for(long b[]:a)fill(b,v);}
void f(){out.flush();}
int p(int i,int p[]){return p[i]<0?i:(p[i]=p(p[i],p));}
boolean c(int x,int y,int n,int m){return x>=0&&x<n&&y>=0&&y<m;}
int[] so(int ar[])
{
Integer r[] = new Integer[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
long[] so(long ar[])
{
Long r[] = new Long[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
char[] so(char ar[])
{
Character r[] = new Character[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
void p(Object p) {out.print(p);}void pl(Object p) {out.println(p);}void pl() {out.println();}
void s(String s) {sb.append(s);}
void s(int s) {sb.append(s);}
void s(long s) {sb.append(s);}
void s(char s) {sb.append(s);}
void s(double s) {sb.append(s);}
void ss() {sb.append(' ');}
void sl(String s) {s(s);sb.append("\n");}
void sl(int s) {s(s);sb.append("\n");}
void sl(long s) {s(s);sb.append("\n");}
void sl(char s) {s(s);sb.append("\n");}
void sl(double s) {s(s);sb.append("\n");}
void sl() {sb.append("\n");}
int l(int v) {return 31 - Integer.numberOfLeadingZeros(v);}
long l(long v) {return 63 - Long.numberOfLeadingZeros(v);}
int sq(int a) {return (int) sqrt(a);}
long sq(long a) {return (long) sqrt(a);}
int gcd(int a, int b)
{
while (b > 0)
{
int c = a % b;
a = b;
b = c;
}
return a;
}
long gcd(long a, long b)
{
while (b > 0l)
{
long c = a % b;
a = b;
b = c;
}
return a;
}
boolean p(String s, int i, int j)
{
while (i < j) if (s.charAt(i++) != s.charAt(j--)) return false;
return true;
}
boolean[] si(int n)
{
boolean bo[] = new boolean[n + 1];
bo[0] = bo[1] = true;
for (int x = 4; x <= n; x += 2) bo[x] = true;
for (int x = 3; x * x <= n; x += 2)
{
if (!bo[x])
{
int vv = (x << 1);
for (int y = x * x; y <= n; y += vv) bo[y] = true;
}
}
return bo;
}
long mul(long a, long b, long m)
{
long r = 1l;
a %= m;
while (b > 0)
{
if ((b & 1) == 1) r = (r * a) % m;
b >>= 1;
a = (a * a) % m;
}
return r;
}
int i() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Integer.parseInt(st.nextToken());
}
long l() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Long.parseLong(st.nextToken());
}
String s() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return st.nextToken();
}
double d() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Double.parseDouble(st.nextToken());
}
void s(int a[])
{
for (int e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(long a[])
{
for (long e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(char a[])
{
for (char e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(int ar[][]) {for (int a[] : ar) s(a);}
void s(long ar[][]) {for (long a[] : ar) s(a);}
void s(char ar[][]) {for (char a[] : ar) s(a);}
int[] ari(int n) throws IOException
{
int ar[] = new int[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Integer.parseInt(st.nextToken());
return ar;
}
long[] arl(int n) throws IOException
{
long ar[] = new long[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Long.parseLong(st.nextToken());
return ar;
}
char[] arc(int n) throws IOException
{
char ar[] = new char[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = st.nextToken().charAt(0);
return ar;
}
double[] ard(int n) throws IOException
{
double ar[] = new double[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Double.parseDouble(st.nextToken());
return ar;
}
String[] ars(int n) throws IOException
{
String ar[] = new String[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = st.nextToken();
return ar;
}
int[][] ari(int n, int m) throws IOException
{
int ar[][] = new int[n][m];
for (int x = 0; x < n; x++)ar[x]=ari(m);
return ar;
}
long[][] arl(int n, int m) throws IOException
{
long ar[][] = new long[n][m];
for (int x = 0; x < n; x++)ar[x]=arl(m);
return ar;
}
char[][] arc(int n, int m) throws IOException
{
char ar[][] = new char[n][m];
for (int x = 0; x < n; x++)ar[x]=arc(m);
return ar;
}
double[][] ard(int n, int m) throws IOException
{
double ar[][] = new double[n][m];
for (int x = 0; x < n; x++)ar[x]=ard(m);
return ar;
}
void p(int ar[])
{
sb = new StringBuilder(11 * ar.length);
for (int a : ar) {sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(long ar[])
{
StringBuilder sb = new StringBuilder(20 * ar.length);
for (long a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(double ar[])
{
StringBuilder sb = new StringBuilder(22 * ar.length);
for (double a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(char ar[])
{
StringBuilder sb = new StringBuilder(2 * ar.length);
for (char aa : ar){sb.append(aa);sb.append(' ');}
out.println(sb);
}
void p(String ar[])
{
int c = 0;
for (String s : ar) c += s.length() + 1;
StringBuilder sb = new StringBuilder(c);
for (String a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(int ar[][])
{
StringBuilder sb = new StringBuilder(11 * ar.length * ar[0].length);
for (int a[] : ar)
{
for (int aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(long ar[][])
{
StringBuilder sb = new StringBuilder(20 * ar.length * ar[0].length);
for (long a[] : ar)
{
for (long aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(double ar[][])
{
StringBuilder sb = new StringBuilder(22 * ar.length * ar[0].length);
for (double a[] : ar)
{
for (double aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(char ar[][])
{
StringBuilder sb = new StringBuilder(2 * ar.length * ar[0].length);
for (char a[] : ar)
{
for (char aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void pl(Object... ar) {for (Object e : ar) p(e + " ");pl();}
} | 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 8 | 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 | 8c04a3d7a9fdfbc9d6acb7c47840a614 | 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.util.Arrays;
import java.util.Random;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/*
Solution Created: 16:42:22 04/08/2022
Custom Competitive programming helper.
*/
public class Main {
public static void solve() {
int n = in.nextInt();
int[] a = new int[n];
for(int i = 0; i<n; i++) a[i] = i+1;
out.println(n);
out.printlnArray(a);
for(int i = n-2; i>=0; i--) {
int tmp = a[i];
a[i] = a[i+1];
a[i+1] = tmp;
out.printlnArray(a);
}
}
public static void main(String[] args) {
in = new Reader();
out = new Writer();
int t = in.nextInt();
while(t-->0) solve();
out.exit();
}
static Reader in; static Writer out;
static class Reader {
static BufferedReader br;
static StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String f){
try {
br = new BufferedReader(new FileReader(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] ns(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) a[i] = next();
return a;
}
public int nextInt() {
ensureNext();
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
ensureNext();
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
ensureNext();
return Long.parseLong(st.nextToken());
}
public String next() {
ensureNext();
return st.nextToken();
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void ensureNext() {
if (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class Util {
private static Random random = new Random();
private static long MOD;
static long[] fact, inv, invFact;
public static void initCombinatorics(int n, long mod, boolean inversesToo, boolean inverseFactorialsToo) {
MOD = mod;
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i < n + 1; i++) fact[i] = (fact[i - 1] * i) % mod;
if (inversesToo) {
inv = new long[n + 1];
inv[1] = 1;
for (int i = 2; i <= n; ++i) inv[i] = (mod - (mod / i) * inv[(int) (mod % i)] % mod) % mod;
}
if (inverseFactorialsToo) {
invFact = new long[n + 1];
invFact[n] = Util.modInverse(fact[n], mod);
for (int i = n - 1; i >= 0; i--) {
if (invFact[i + 1] == -1) {
invFact[i] = Util.modInverse(fact[i], mod);
continue;
}
invFact[i] = (invFact[i + 1] * (i + 1)) % mod;
}
}
}
public static long modInverse(long a, long mod) {
long[] gcdE = gcdExtended(a, mod);
if (gcdE[0] != 1) return -1; // Inverse doesn't exist
long x = gcdE[1];
return (x % mod + mod) % mod;
}
public static long[] gcdExtended(long p, long q) {
if (q == 0) return new long[] { p, 1, 0 };
long[] vals = gcdExtended(q, p % q);
long tmp = vals[2];
vals[2] = vals[1] - (p / q) * vals[2];
vals[1] = tmp;
return vals;
}
public static long nCr(int n, int r) {
if (r > n) return 0;
return (((fact[n] * invFact[r]) % MOD) * invFact[n - r]) % MOD;
}
public static long nPr(int n, int r) {
if (r > n) return 0;
return (fact[n] * invFact[n - r]) % MOD;
}
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 = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n + 1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i * i <= n; i++)
if (isPrime[i])
for (int j = i; i * j <= n; j++)
isPrime[i * j] = false;
return isPrime;
}
static long pow(long x, long pow, long mod) {
long res = 1;
x = x % mod;
if (x == 0) return 0;
while (pow > 0) {
if ((pow & 1) != 0) res = (res * x) % mod;
pow >>= 1;
x = (x * x) % mod;
}
return res;
}
public static int gcd(int a, int b) {
int tmp = 0;
while (b != 0) {
tmp = b;
b = a % b;
a = tmp;
}
return a;
}
public static long gcd(long a, long b) {
long tmp = 0;
while (b != 0) {
tmp = b;
b = a % b;
a = tmp;
}
return a;
}
public static int random(int min, int max) {
return random.nextInt(max - min + 1) + min;
}
public static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static void reverse(int[] s, int l, int r) {
for (int i = l; i <= (l + r) / 2; i++) {
int tmp = s[i];
s[i] = s[r + l - i];
s[r + l - i] = tmp;
}
}
public static void reverse(int[] s) {
reverse(s, 0, s.length - 1);
}
public static void reverse(long[] s, int l, int r) {
for (int i = l; i <= (l + r) / 2; i++) {
long tmp = s[i];
s[i] = s[r + l - i];
s[r + l - i] = tmp;
}
}
public static void reverse(long[] s) {
reverse(s, 0, s.length - 1);
}
public static void reverse(float[] s, int l, int r) {
for (int i = l; i <= (l + r) / 2; i++) {
float tmp = s[i];
s[i] = s[r + l - i];
s[r + l - i] = tmp;
}
}
public static void reverse(float[] s) {
reverse(s, 0, s.length - 1);
}
public static void reverse(double[] s, int l, int r) {
for (int i = l; i <= (l + r) / 2; i++) {
double tmp = s[i];
s[i] = s[r + l - i];
s[r + l - i] = tmp;
}
}
public static void reverse(double[] s) {
reverse(s, 0, s.length - 1);
}
public static void reverse(char[] s, int l, int r) {
for (int i = l; i <= (l + r) / 2; i++) {
char tmp = s[i];
s[i] = s[r + l - i];
s[r + l - i] = tmp;
}
}
public static void reverse(char[] s) {
reverse(s, 0, s.length - 1);
}
public static <T> void reverse(T[] s, int l, int r) {
for (int i = l; i <= (l + r) / 2; i++) {
T tmp = s[i];
s[i] = s[r + l - i];
s[r + l - i] = tmp;
}
}
public static <T> void reverse(T[] s) {
reverse(s, 0, s.length - 1);
}
public static void shuffle(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(long[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
long t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(float[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
float t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(double[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
double t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(char[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static <T> void shuffle(T[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
T t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void sortArray(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(float[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(double[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(char[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T extends Comparable<T>> void sortArray(T[] a) {
Arrays.sort(a);
}
public static int[][] rotate90(int[][] a) {
int n = a.length, m = a[0].length;
int[][] ans = new int[m][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
ans[m - j - 1][i] = a[i][j];
return ans;
}
public static char[][] rotate90(char[][] a) {
int n = a.length, m = a[0].length;
char[][] ans = new char[m][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
ans[m - j - 1][i] = a[i][j];
return ans;
}
}
static class Writer {
private PrintWriter pw;
public Writer(){
pw = new PrintWriter(System.out);
}
public Writer(String f){
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public void yesNo(boolean condition) {
println(condition?"YES":"NO");
}
public void printArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void printArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void print(Object o) {
pw.print(o.toString());
}
public void println(Object o) {
pw.println(o.toString());
}
public void println() {
pw.println();
}
public void flush() {
pw.flush();
}
public void exit() {
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 8 | 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 | 1d3cc95683950fb99ac0edd7ea1c7930 | 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 B {
public static void main(String[] args) throws IOException {
FastReader in = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
int[] a = new int[n];
pw.println(n);
for (int i = 0; i < n; i++) {
pw.print((i + 1) + " ");
a[i] = (1 + i);
}
pw.println();
for (int i = 0; i < n - 1; i++) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
for (int j = 0; j < n; j++) pw.print(a[j] + " ");
pw.println();
}
}
pw.close();
}
static boolean isPrime(int n) {
if (n <= 1) return false;
else if (n == 2) return true;
else if (n % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0) return false;
}
return true;
}
public static long Xsum(int[][] a, int i, int j, int n, int m) {
long res = 0;
res += a[i][j];
int tempi = i, tempj = j;
tempi--;
tempj--;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi--;
tempj--;
}
tempi = i;
tempj = j;
tempi++;
tempj--;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi++;
tempj--;
}
tempi = i;
tempj = j;
tempi++;
tempj++;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi++;
tempj++;
}
tempi = i;
tempj = j;
tempi--;
tempj++;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi--;
tempj++;
}
return res;
}
static void subString(char str[], int n) {
for (int len = 1; len <= n; len++) {
for (int i = 0; i <= n - len; i++) {
int j = i + len - 1;
for (int k = i; k <= j; k++) {
System.out.print(str[k]);
}
System.out.println();
}
}
}
static long gcd(long a, long b) {
if (b == 0) return a;
else return gcd(b, a % b);
}
static boolean isPalindrom(String txt) {
return txt.equals(new StringBuilder(txt).reverse().toString());
}
public static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++)
if (arr[i] < arr[i - 1]) return false;
return true;
}
static long binaryExponentiation(long x, long n) {
if (n == 0) return 1;
else if (n % 2 == 0) return binaryExponentiation(x * x, n / 2);
else return x * binaryExponentiation(x * x, (n - 1) / 2);
}
public static void Sort(int[] a) {
ArrayList<Integer> lst = new ArrayList<>();
for (int i : a) lst.add(i);
Collections.sort(lst);
for (int i = 0; i < lst.size(); i++) a[i] = lst.get(i);
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
static class Pair implements Comparable<Pair> {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair ob) {
return first - ob.first;
}
}
static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
public FastReader(InputStream is) {
this.is = is;
}
public int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
public String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public String nextLine() {
int c = skip();
StringBuilder sb = new StringBuilder();
while (!isEndOfLine(c)) {
sb.appendCodePoint(c);
c = readByte();
}
return sb.toString();
}
public int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char[] next(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
public char readChar() {
return (char) skip();
}
public long[] readArrayL(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = nextLong();
return arr;
}
public int[] readArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt();
return arr;
}
public int[][] read2Darray(int n, int m) {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
/*public static int min(int a, int b) {
return Math.min(a, b);
}
public static int max(int a, int b) {
return Math.max(a, 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 8 | 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 | 11e5270025661075db29584bbb74e5f0 | 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.reflect.Array;
import static java.lang.Math.*;
// import java .util.Map.Entry;
public class Main
//class bhaiya
{ static int MOD = 1000000007;
static long mod=(long)1e9+7;
static int isPrimeLimit=(int)(1e6);
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();
}
}
static class Pair{
long x;long y;
Pair(long x,long y){
this.x=x;
this.y=y;
}
}
static int upper_bound(long arr[], long key)
{
int mid, N = arr.length;
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr[mid]) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
if (low == N ) {
// System.out.print("The upper bound of " + key + " does not exist.");
return N;
}
// Print the upper_bound index
// System.out.print("The upper bound of " + key + " is " + arr[low] + " at index " + low);
return low;
}
public static void main(String[] args) {
try {
FastReader sc=new FastReader();
FastWriter out = new FastWriter();
int testCases=sc.nextInt();
while(testCases-- > 0){
int n = sc.nextInt();
out.println(n);
long[] arr = new long[n];
for(int i =0;i<n;i++){
arr[i]=i+1;
}
for(int i=0;i<n;i++){
out.print(arr[i] + " ");
}
out.print("\n");
for(int i =1;i<n;i++){
long swap=0;
swap=arr[0];
arr[0]=arr[i];
arr[i]=swap;
for(int j=0;j<n;j++){
out.print(arr[j] + " ");
}
out.print("\n");
}
}
out.close();
} 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 8 | 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 | 23fe4c34bdfe78a775ccd72ea5402866 | 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: RonWonWon
* CREATED: 07.11.2022 17:26:52
/*==========================================================================*/
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
/*
in.ini();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
*/
long startTime = System.nanoTime();
int t = in.nextInt(), T = 1;
while(t-->0) {
int n = in.nextInt();
out.println(n);
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = i+1;
int k = n-1;
for(int i=0;i<n;i++){
for(int j : a) out.print(j+" ");
out.println();
int p = k-1, q = k;
if(p==-1) break;
int temp = a[q];
a[q] = a[p];
a[p] = temp;
k--;
}
//out.println("Case #"+T+": "+ans); T++;
}
/*
startTime = System.nanoTime() - startTime;
System.err.println("Time: "+(startTime/1000000));
*/
out.flush();
}
static FastScanner in = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static int oo = Integer.MAX_VALUE;
static long ooo = Long.MAX_VALUE;
static long mod = 1_000_000_007; //998244353;
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
void ini() throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() {
while(!st.hasMoreTokens())
try { st = new StringTokenizer(br.readLine()); }
catch(IOException e) {}
return st.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
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());
}
long[] readArrayL(int n) {
long a[] = new long[n];
for(int i=0;i<n;i++) a[i] = nextLong();
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] readArrayD(int n) {
double a[] = new double[n];
for(int i=0;i<n;i++) a[i] = nextDouble();
return a;
}
}
static final Random random = new Random();
static void ruffleSort(int[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n), temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSortL(long[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n);
long temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(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 8 | 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 | 0f658881bbdf5d1bafe7e47fff786de4 | 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 {
public static void swap (int [] arr , int i , int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) throws IOException {
OutputStreamWriter osr = new OutputStreamWriter(System.out);
PrintWriter o = new PrintWriter(osr);
FastReader fr = new FastReader();
int t = fr.nextInt();
while (t--!=0) {
int n = fr.nextInt();
int[] arr = new int[n + 1];
for (int i = 0; i <= n; i++)
arr[i] = i;
o.println(n);
for (int i = 0; i < n; i++) {
if (i >= 1)
swap(arr, n - i, n - i + 1);
for (int j = 1; j <= n; j++)
o.print(arr[j] + " ");
o.println();
}
}
o.close();
}
}
class FastReader {
// Attributes :
BufferedReader br;
StringTokenizer st;
// Constructor :
public FastReader() {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
}
// Operations :
// #01 :
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
// #02 :
public String nextLine() throws IOException {
return br.readLine();
}
// #03 :
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
// #04 :
public long nextLong() throws IOException {
return Long.parseLong(next());
}
// #05 :
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
// #06 :
public int [] intArray (int size) throws IOException{
int [] arr = new int[size];
for (int i = 0 ; i < size; i++)
arr[i] = nextInt();
return arr;
}
// #07 :
public char [] charArray() throws IOException {
return nextLine().toCharArray();
}
}
class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
static class Compare implements Comparator<Pair> {
@Override
public int compare(Pair o1, Pair o2) {
return (o1.y - o2.y);
}
}
} | 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 8 | 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 | b25fb6351522b72e1eccd0f92143b1b7 | 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;
public class Main {
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());
StringBuilder sb = new StringBuilder();
sb.append(n);
int[] a = new int[n];
for (int i = 1; i < n; i++) {
a[i] = i;
}
for (int i = 0; i < n; i++) {
sb.append("\n");
for (int x : a) {
sb.append(x + 1).append(" ");
}
if (i == n - 1) {
break;
}
int tmp = a[i + 1];
a[i + 1] = a[i];
a[i] = tmp;
}
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 8 | 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 | 265ae232c22536cf1483f8e8944cc4ca | 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.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution {
static PrintWriter pw;
static FastScanner s;
static int[] a;
public static void main(String[] args) throws Exception {
pw=new PrintWriter(System.out);
s=new FastScanner(System.in);
int t=s.nextInt();
while(t-->0) {
int n=s.nextInt();
pw.println(n);
a=new int[n];
for(int i=0;i<n;i++) {
a[i]=i+1;
}
for(int i=1;i<n;i++) {
printArray();
swap(i,i-1);
}
printArray();
}
pw.flush();
}
public static void printArray() {
for(int i=0;i<a.length;i++)
pw.print(a[i]+" ");
pw.println();
}
public static void swap(int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public FastScanner(String s) throws Exception {
br = new BufferedReader(new FileReader(new File(s)));
}
public String next() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
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 8 | 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 | d292ab6cd9dd63048728035cc3b85860 | 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 B1716 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder out = new StringBuilder();
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
out.append(N).append('\n');
int[] P = new int[N];
for (int n=0; n<N; n++) {
P[n] = n+1;
}
for (int p : P) {
out.append(p).append(' ');
}
out.append('\n');
for (int n=1; n<N; n++) {
int temp = P[n-1];
P[n-1] = P[n];
P[n] = temp;
for (int p : P) {
out.append(p).append(' ');
}
out.append('\n');
}
}
System.out.print(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 8 | 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 | 62b977581c03cb194f52ca462a977b16 | 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 {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static void solve(int n){
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = i + 1;
}
out.println(n);
print1DArray(arr);
for(int t = 1; t < n; t++){
int temp = arr[t];
arr[t] = arr[t-1];
arr[t-1] = temp;
print1DArray(arr);
}
}
static void print1DArray(int[] arr){
for(int i = 0; i < arr.length; i++){
out.print(arr[i]);
if(i < arr.length - 1){
out.print(' ');
}
}
out.print('\n');
}
public static void main(String[] args){
MyScanner scanner = new MyScanner();
int testCount = scanner.nextInt();
for(int testIdx = 1; testIdx <= testCount; testIdx++){
int n = scanner.nextInt();
solve(n);
}
out.close();
}
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;
}
}
} | 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 8 | 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 | 56013b62c2bebb84222809e3916445e3 | 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 kg.my_algorithms.Codeforces;
import java.io.*;
import java.util.*;
/*
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader fr = new FastReader();
int test_cases = fr.nextInt();
StringBuilder sb = new StringBuilder();
for(int test=1;test<=test_cases;test++){
int n = fr.nextInt();
sb.append(n).append("\n");
for(int fixed=n;fixed>0;fixed--){
int number = 1;
for(int i=1;i<=n;i++){
if(i == fixed) sb.append(n).append(" ");
else {
sb.append(number).append(" ");
number++;
}
}
sb.append("\n");
}
}
output.write(sb.toString());
output.flush();
}
}
//Fast Input
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;
}
} | 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 8 | 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 | 7caa80f4eb6e44d5a032ba3fbaab9147 | 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.*;
public class Main
{
public static void main(String[] args)
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work:
while (tc-- > 0) {
int n = input.nextInt();
int a[] = new int[n];
for (int i = 0; i <n; i++) {
a[i] = i+1;
}
// System.out.println(Arrays.toString(a));
StringBuilder result = new StringBuilder();
result.append(n+"\n");
for (int i = 0; i <n; i++) {
if(i!=0)
{
int temp = a[i];
a[i] = a[i-1];
a[i-1] = temp;
for (int j = 0; j <n; j++) {
result.append(a[j]+" ");
}
result.append("\n");
}
else
{
for (int j = 0; j <n; j++) {
result.append(a[j]+" ");
}
result.append("\n");
}
}
System.out.println(result);
}
}
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());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.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 8 | 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 | 024c0b0afc3953d8b51127303b1ce0c2 | 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.Math;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class Main {
static PrintWriter pw;
static Scanner sc;
static StringBuilder ans;
static long mod = 1000000000+7;
static void pn(final Object arg) { pw.print(arg); pw.flush(); }
/*-------------- for input in an value ---------------------*/
static int ni() { return sc.nextInt(); }
static long nl() { return sc.nextLong(); }
static double nd() { return sc.nextDouble(); }
static String ns() { return sc.next(); }
static String nLine() { return sc.nextLine(); }
static void ap(int arg) { ans.append(arg); }
static void ap(long arg) { ans.append(arg); }
static void ap(String arg) { ans.append(arg); }
static void ap(StringBuilder arg) { ans.append(arg); }
static void apn() { ans.append("\n"); }
static void apn(int arg) { ans.append(arg+"\n"); }
static void apn(long arg) { ans.append(arg+"\n"); }
static void apn(String arg) { ans.append(arg+"\n"); }
static void apn(StringBuilder arg) { ans.append(arg+"\n"); }
static void yes() { ap("Yes\n"); }
static void no() { ap("No\n"); }
/* for Dubuggin */
static void printArr(int ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(long ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(String ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printIntegerList(List<Integer> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printLongList(List<Long> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printStringList(List<String> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
/*-------------- for input in an array ---------------------*/
static void readArray(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void readArray(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void readArray(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void readArray(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*-------------- File vs Input ---------------------*/
static void runFile() throws Exception {
sc = new Scanner(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
}
static void runIo() throws Exception {
pw =new PrintWriter(System.out);
sc = new Scanner(System.in);
}
static int log2(int n) { return (int)(Math.log(n) / Math.log(2)); }
static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);}
static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); }
static long nCr(long n, long r) { // Combinations
if (n < r)
return 0;
if (r > n - r) { // because nCr(n, r) == nCr(n, n - r)
r = n - r;
}
long ans = 1L;
for (long i = 0; i < r; i++) {
ans *= (n - i);
ans /= (i + 1);
}
return ans;
}
static int countDigit(long n){ if( n == 0 )return 1; else return (int)Math.floor(Math.log10(n) + 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 (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean sv[] = new boolean[10002];
static void seive() {
//true -> not prime
// false->prime
sv[0] = sv[1] = true;
sv[2] = false;
for(int i = 0; i< sv.length; i++) {
if( !sv[i] && (long)i*(long)i < sv.length ) {
for ( int j = i*i; j<sv.length ; j += i ) {
sv[j] = true;
}
}
}
}
static long kadensAlgo(long ar[]) {
int n = ar.length;
long pre = ar[0];
long ans = ar[0];
for(int i = 1; i<n; i++) {
pre = Math.max(pre + ar[i], ar[i]);
ans = Math.max(pre, ans);
}
return ans;
}
static long binpow( long a, long b) {
long res = 1;
while (b > 0) {
if ( (b & 1) > 0){
res = (res * a);
}
a = (a * a);
b >>= 1;
}
return res;
}
static long factorial(long n) {
long res = 1, i;
for (i = 2; i <= n; i++){
res = ((res%mod) * (i%mod))%mod;
}
return res;
}
static int getCountPrime(int n) {
int ans = 0;
while (n%2==0) {
ans ++;
n /= 2;
}
for (int i = 3; i *i<= n; i+= 2) {
while (n%i == 0) {
ans ++;
n /= i;
}
}
if(n > 1 )
ans++;
return ans;
}
static void sort(int[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void sort(long[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
ArrayList<Long> ls = new ArrayList<Long>();
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 lowerBound(ArrayList<Integer> ar, int k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar.get(m) >= k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static int upperBound(long ar[], long k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] > k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static int lowerBound(int ar[], int k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] >= k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static void findDivisor(int n, ArrayList<Integer> al) {
for(int i = 1; i*i <= n; i++){
if( n % i == 0){
if(n/i==i){
al.add(i);
}
else{
al.add(n/i);
al.add(i);
}
}
}
}
static class Pair{
int a;
int b;
Pair( int a, int b ){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
int result = a;
result = 31 * result + b;
return result;
}
}
public static void main(String[] args) throws Exception {
// runFile();
runIo();
int t;
t = 1;
t = sc.nextInt();
ans = new StringBuilder();
while( t-- > 0 ) {
solve();
}
pn(ans+"");
}
public static void solve ( ) {
int n = ni();
int ar[] = new int[n];
for(int i = 1; i<=n; i++)
ar[i-1] = i;
apn(n);
pnt(ar);
for(int i = n-2; i>=0; i--) {
int t = ar[i];
ar[i] = ar[i+1];
ar[i+1] = t;
pnt(ar);
}
}
static void pnt(int ar[]) {
for(int i :ar)
ap(i+" ");
apn();
}
}
// 1 2 3 = 3
// 1 3 2 = 1
// 3 1 2 = 0
// 1 2 3 4 5 = 5
// 1 2 3 5 4 = 3
// 1 2 5 4 3 2 1 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 8 | 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 | 7bd7afdbfe5058635bd69af094e67bf8 | 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 B {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int testCases, n, m;
static long a[], b[];
static void solve(int t) {
a = new long[n];
for (int i = 0; i < n; ++i) {
a[i] = i + 1;
}
//ans.append(n).append("\n");
int index = 1;
ArrayList1<long[]> list = new ArrayList1<>();
for (int j = 0; j < m; ++j) {
b = new long[n];
for(int i = 0; i < n; ++i) b[i] = a[i];
list.add(b);
//ans.append("\n");
if (index >= n) {
break;
} else {
swap(a, index, 0);
//print(a);
++index;
}
//++index;
}
ans.append(list.size()).append("\n");
while (!list.isEmpty()) {
long now_the_array_a[] = list.get(0);
//if(list.size() > 1) print(list.get(1));
for (long i : now_the_array_a) {
ans.append(i).append(" ");
}
ans.append("\n");
list.popFront();
}
}
public static void main(String[] Priya) throws IOException {
testCases = in.nextInt();
for (int t = 0; t < testCases; ++t) {
n = in.nextInt();
m = n;
solve(t + 1);
}
in.close();
out.print(ans.toString());
out.flush();
}
static void print(long a[]) {
for(long i : a) {
out.print(i + " ");
out.flush();
}
out.println();
out.flush();
}
static int search(long a[], long x, int last) {
int i = 0, j = last;
while (i <= j) {
int mid = i + (j - i) / 2;
if (a[mid] == x) {
return mid;
}
if (a[mid] < x) {
i = mid + 1;
} else {
j = mid - 1;
}
}
return -1;
}
static void swap(long a[], int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void reverse(long a[]) {
n = a.length;
for (int i = 0; i < n / 2; ++i) {
swap(a, i, n - i - 1);
}
}
static long max(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.max(a[i], max);
return max(a, i + 1, n, max);
}
static long min(long a[], int i, int n, long max) {
if (i > n) {
return max;
}
max = Math.min(a[i], max);
return max(a, i + 1, n, max);
}
static void printArray(long a[]) {
for (long i : a) {
System.out.print(i + " ");
}
System.out.println();
}
static boolean isSmaller(String str1, String str2) {
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2) {
return true;
}
if (n2 < n1) {
return false;
}
for (int i = 0; i < n1; i++) {
if (str1.charAt(i) < str2.charAt(i)) {
return true;
} else if (str1.charAt(i) > str2.charAt(i)) {
return false;
}
}
return false;
}
static String sub(String str1, String str2) {
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n1 - n2;
int carry = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sub
= (((int) str1.charAt(i + diff) - (int) '0')
- ((int) str2.charAt(i) - (int) '0')
- carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
} else {
carry = 0;
}
str += String.valueOf(sub);
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (str1.charAt(i) == '0' && carry > 0) {
str += "9";
continue;
}
int sub = (((int) str1.charAt(i) - (int) '0')
- carry);
if (i > 0 || sub > 0) {
str += String.valueOf(sub);
}
carry = 0;
}
return new StringBuilder(str).reverse().toString();
}
static String sum(String str1, String str2) {
if (str1.length() > str2.length()) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
int carry = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((int) (str1.charAt(i) - '0')
+ (int) (str2.charAt(i + diff) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((int) (str2.charAt(i) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0) {
str += (char) (carry + '0');
}
return new StringBuilder(str).reverse().toString();
}
static long detect_sum(int i, long a[], long sum) {
if (i >= a.length) {
return sum;
}
return detect_sum(i + 1, a, sum + a[i]);
}
static String mul(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
if (len1 == 0 || len2 == 0) {
return "0";
}
int result[] = new int[len1 + len2];
int i_n1 = 0;
int i_n2 = 0;
for (int i = len1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = num1.charAt(i) - '0';
i_n2 = 0;
for (int j = len2 - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0) {
result[i_n1 + i_n2] += carry;
}
i_n1++;
}
int i = result.length - 1;
while (i >= 0 && result[i] == 0) {
i--;
}
if (i == -1) {
return "0";
}
String s = "";
while (i >= 0) {
s += (result[i--]);
}
return s;
}
static class Node<T> {
T data;
Node<T> next;
public Node() {
this.next = null;
}
public Node(T data) {
this.data = data;
this.next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return this.getData().toString() + " ";
}
}
static class ArrayList1<T> {
Node<T> head, tail;
int len;
public ArrayList1() {
this.head = null;
this.tail = null;
this.len = 0;
}
int size() {
return len;
}
boolean isEmpty() {
return len == 0;
}
int indexOf(T data) {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int index = -1, i = 0;
while (temp != null) {
if (temp.getData() == data) {
index = i;
}
i++;
temp = temp.getNext();
}
return index;
}
void add(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
tail.setNext(newNode);
tail = newNode;
len++;
}
}
void see() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
while (temp != null) {
System.out.print(temp.getData().toString() + " ");
//out.flush();
temp = temp.getNext();
}
System.out.println();
//out.flush();
}
void inserFirst(T data) {
Node<T> newNode = new Node<>(data);
Node<T> temp = head;
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
newNode.setNext(temp);
head = newNode;
len++;
}
}
T get(int index) {
if (isEmpty() || index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
return head.getData();
}
Node<T> temp = head;
int i = 0;
T data = null;
while (temp != null) {
if (i == index) {
data = temp.getData();
}
i++;
temp = temp.getNext();
}
return data;
}
void addAt(T data, int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> newNode = new Node<>(data);
int i = 0;
Node<T> temp = head;
while (temp.next != null) {
if (i == index) {
newNode.setNext(temp.next);
temp.next = newNode;
}
i++;
temp = temp.getNext();
}
// temp.setNext(temp);
len++;
}
void popFront() {
if (isEmpty()) {
//return;
throw new ArrayIndexOutOfBoundsException();
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
}
len--;
}
void removeAt(int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
this.popFront();
return;
}
Node<T> temp = head;
int i = 0;
Node<T> n = new Node<>();
while (temp != null) {
if (i == index) {
n.next = temp.next;
temp.next = n;
break;
}
i++;
n = temp;
temp = temp.getNext();
}
tail = n;
--len;
}
void clearAll() {
this.head = null;
this.tail = null;
}
}
static void merge(long a[], int left, int right, int mid) {
int n1 = mid - left + 1, n2 = right - mid;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; i++) {
L[i] = a[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
}
int i = 0, j = 0, k1 = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k1] = L[i];
i++;
} else {
a[k1] = R[j];
j++;
}
k1++;
}
while (i < n1) {
a[k1] = L[i];
i++;
k1++;
}
while (j < n2) {
a[k1] = R[j];
j++;
k1++;
}
}
static void sort(long a[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
static class Scanner {
BufferedReader in;
StringTokenizer st;
public Scanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void close() throws IOException {
in.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 8 | 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 | c744c4a1fb691619b2db4160600f1aea | 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 | ab29b151236206d8efbec2b2193e2e63 | 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.*;
public class Main {
static long M = (long) (1e9 + 7);
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 heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = (Integer) temp;
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int lcm(int a, int b) {
return (a * b / gcd(a, b));
}
public static String sortString(String inputString) {
// Converting input string to character array
char[] tempArray = inputString.toCharArray();
// Sorting temp array using
Arrays.sort(tempArray);
// Returning new sorted string
return new String(tempArray);
}
static boolean isSquare(int n) {
int v = (int) Math.sqrt(n);
return v * v == n;
}
static boolean PowerOfTwo(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 int power(long a, long b) {
long res = 1;
while (b > 0) {
if (b % 2 == 1) {
res = (res * a) % M;
}
a = ((a * a) % M);
b = b / 2;
}
return (int) res;
}
public static boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) {
return false;
}
return true;
}
static int pown(long n) {
if (n == 0)
return 1;
if (n == 1)
return 1;
if ((n & (~(n - 1))) == n)
return 0;
return 1;
}
static long computeXOR(long n) {
// If n is a multiple of 4
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
static long binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
long result = 0;
for (int i = numbers.length - 1; i >= 0; i--)
if (numbers[i] == '1')
result += Math.pow(2, (numbers.length - i - 1));
return result;
}
static String reverseString(String str) {
char ch[] = str.toCharArray();
String rev = "";
for (int i = ch.length - 1; i >= 0; i--) {
rev += ch[i];
}
return rev;
}
static int countFreq(int[] arr, int n) {
HashMap<Integer, Integer> map = new HashMap<>();
int x = 0;
for (int i = 0; i < n; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
}
for (int i = 0; i < n; i++) {
if (map.get(arr[i]) == 1)
x++;
}
return x;
}
static boolean symm(String str) {
if (str.length() == 1 || str.length() == 0)
return true;
if (str.substring(0, (str.length() / 2)).equals(str.substring(str.length() / 2)))
return symm(str.substring(0, (str.length() / 2)));
return false;
}
static void reverse(int[] arr, int l, int r) {
int d = (r - l + 1) / 2;
for (int i = 0; i < d; i++) {
int t = arr[l + i];
arr[l + i] = arr[r - i];
arr[r - i] = t;
}
// print array here
}
static int f(int x) {
x++;
while (x % 10 == 0) {
x /= 10;
}
return x;
}
public static boolean isFair(long x) {
long n = x;
while (n != 0) {
long r = (long) n % 10;
if (r != 0 && x % r != 0)
return false;
n = n / 10;
}
return true;
}
static void sort(String[] s, int n) {
for (int i = 1; i < n; i++) {
String temp = s[i];
// Insert s[j] at its correct position
int j = i - 1;
while (j >= 0 && temp.length() < s[j].length()) {
s[j + 1] = s[j];
j--;
}
s[j + 1] = temp;
}
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- != 0) {
long n = sc.nextLong();
int arr[]=new int[(int) n];
System.out.println(n);
for(int i=0;i<n;i++)
arr[i]=i+1;
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
System.out.println();
for(int i=1;i<n;i++){
swap(arr,i , i-1);
String s="";
for(int j=0;j<n;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 | 2c1845eb8e1f2634fa7948c2ede07e8e | 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.*;
public class Main {
static long M = (long) (1e9 + 7);
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 heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void swap(Integer[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = (Integer) temp;
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int lcm(int a, int b) {
return (a * b / gcd(a, b));
}
public static String sortString(String inputString) {
// Converting input string to character array
char[] tempArray = inputString.toCharArray();
// Sorting temp array using
Arrays.sort(tempArray);
// Returning new sorted string
return new String(tempArray);
}
static boolean isSquare(int n) {
int v = (int) Math.sqrt(n);
return v * v == n;
}
static boolean PowerOfTwo(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 int power(long a, long b) {
long res = 1;
while (b > 0) {
if (b % 2 == 1) {
res = (res * a) % M;
}
a = ((a * a) % M);
b = b / 2;
}
return (int) res;
}
public static boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) {
return false;
}
return true;
}
static int pown(long n) {
if (n == 0)
return 1;
if (n == 1)
return 1;
if ((n & (~(n - 1))) == n)
return 0;
return 1;
}
static long computeXOR(long n) {
// If n is a multiple of 4
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
static long binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
long result = 0;
for (int i = numbers.length - 1; i >= 0; i--)
if (numbers[i] == '1')
result += Math.pow(2, (numbers.length - i - 1));
return result;
}
static String reverseString(String str) {
char ch[] = str.toCharArray();
String rev = "";
for (int i = ch.length - 1; i >= 0; i--) {
rev += ch[i];
}
return rev;
}
static int countFreq(int[] arr, int n) {
HashMap<Integer, Integer> map = new HashMap<>();
int x = 0;
for (int i = 0; i < n; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
}
for (int i = 0; i < n; i++) {
if (map.get(arr[i]) == 1)
x++;
}
return x;
}
static boolean symm(String str) {
if (str.length() == 1 || str.length() == 0)
return true;
if (str.substring(0, (str.length() / 2)).equals(str.substring(str.length() / 2)))
return symm(str.substring(0, (str.length() / 2)));
return false;
}
static void reverse(int[] arr, int l, int r) {
int d = (r - l + 1) / 2;
for (int i = 0; i < d; i++) {
int t = arr[l + i];
arr[l + i] = arr[r - i];
arr[r - i] = t;
}
// print array here
}
static int f(int x) {
x++;
while (x % 10 == 0) {
x /= 10;
}
return x;
}
public static boolean isFair(long x) {
long n = x;
while (n != 0) {
long r = (long) n % 10;
if (r != 0 && x % r != 0)
return false;
n = n / 10;
}
return true;
}
static void sort(String[] s, int n) {
for (int i = 1; i < n; i++) {
String temp = s[i];
// Insert s[j] at its correct position
int j = i - 1;
while (j >= 0 && temp.length() < s[j].length()) {
s[j + 1] = s[j];
j--;
}
s[j + 1] = temp;
}
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- != 0) {
long n = sc.nextLong();
int arr[]=new int[(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 | 33643a6e3a79e2e9e97cf77d1f579769 | 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.lang.reflect.Array;
import java.math.BigInteger;
/* Name of the class has to be "Main" only if the class is public. */
public class codeforces
{
public static void solver(int i,int n) {
StringBuilder str=new StringBuilder("");
for(int j=1;j<n-i;j++){
str.append(j+" ");
}
str.append(n+" ");
for(int j=n-i;j<n;j++){
str.append(j+" ");
}System.out.println(str);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){//testcases
//input
int n=sc.nextInt();
System.out.println(n);
for(int i=0;i<n;i++){
solver(i, 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 | 9e3bac137283718d8a821163361c525c | 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.util.*;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
public class probB {
public static void main(String[] args) throws IOException{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int tt = Integer.parseInt(f.readLine());
for(int w = 0; w < tt; w++) {
int n = Integer.parseInt(f.readLine());
int[] pocperm = new int[n];
int[][] matperm = new int[n][n];
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
log.write(n+"\n");
for(int i=0; i<n; i++) {
pocperm[i] = i+1;
matperm[0][i] = pocperm[i];
log.write(matperm[0][i]+" ");
}
log.write("\n");
int br = n-1;
int counter = 1;
for(int i=0; i<n; i++) {
if(n-i-2<0) {
break;
}
int tmp = pocperm[n-i-1];
pocperm[n-i-1] = pocperm[n-i-2];
pocperm[n-i-2] = tmp;
matperm[counter] = pocperm;
for(int j=0; j<n; j++) {
log.write(matperm[counter][j]+" ");
}
counter++;
log.write("\n");
}
log.write("\n");
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 | 1e91864812f7a7acf9fb8615e2716454 | 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.Scanner;
/**
*
* @author Lenovo
*/
public class Main {
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
// BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int t = input.nextInt();
for (int j = 0; j < t; j++) {
int n = input.nextInt();
int a[] = new int[n + 2];
for (int i = 1; i <= n; i++) {
a[i] = i;
}
out.println(n);
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= n; k++) {
out.print(a[k] + " ");
}
out.println("");
int tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
}
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 | 72ac7c9969cff5e681dff3224fce425a | 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;
/**
*
* @author Lenovo
*/
public class Main {
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
//Scanner input = new Scanner(System.in);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(input.readLine());
for (int j = 0; j < t; j++) {
int n = Integer.parseInt(input.readLine());
int a[] = new int[n + 2];
for (int i = 1; i <= n; i++) {
a[i] = i;
}
out.println(n);
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= n; k++) {
out.print(a[k] + " ");
}
out.println("");
int tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
}
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 | 999466a4b694726021e2446fa0ab5457 | 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);
// long s =
StringBuilder sb = new StringBuilder();
for (int t= in.nextInt(); t>0; t--){
int n= in.nextInt();
int[] num = new int[n+1];
sb.append(n).append("\n");
for (int i = 1; i <= n; i++) {
num[i] = i;
sb.append(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);
// long e = System.currentTimeMillis();
// System.out.println(e-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 | 654301cc3c524f8c630c9e822469386b | 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 lll
{
static class pair{
int x,y;
pair(int x,int y){
this.x=x;
this.y=y;
}
}
static long get(long []a,int i){
return Math.max(0,Math.max(a[i-1],a[i+1])-a[i]+1);
}
static int f(char []ch,int i){
int n=ch.length;
if(i==ch.length){
int j=0,k=n-1;
while(j<k){
if(!(ch[j]=='(' && ch[k]==')'))return 0;j++;k--;
}return 1;
}
if(ch[i]=='?')
{
char []a=new char[n];
char []b=new char[n];
for(int k=0;k<n;k++)a[k]=ch[k];
for(int k=0;k<n;k++)b[k]=ch[k];
a[i]='(';b[i]=')';
return f(a,i+1)+f(b,i+1);
}
else
return f(ch,i+1);
}
static int gcd(int a,int b){
if(a>b)return gcd(b,a);
if(a==0)return b;
else
return gcd(b%a,a);
}
static void swap(int []a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
public static void main (String[] args) throws IOException
{
// Scanner sc=new Scanner (System.in);
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());
// long []a=new long[n];
// int []right=new int[n];
// String []s=br.readLine().split(" ");
// for(int i=0;i<n;i++)a[i]=Long.parseLong(s[i]);
// if(n%2==1){
// long ans=0;
// for(int i=1;i<n-1;i++,i++)ans+=get(a,i);
// bw.write(ans+"\n");continue;
// }
// else{
// long left=0;
// for(int i=1;i<n-1;i++,i++)left+=get(a,i);
// long ans=left;
// for(int i=n-2;i>=1;i--,i--){
// left-=get(a,i-1);
// left+=get(a,i);
// ans=Math.min(ans,left);
// }
// bw.write(ans+"\n");
// }
// }
// bw.flush();
// Scanner sc=new Scanner (System.in);
// String s="abcacbabc";
// int n=s.length();
// int lps[]=new int[n];
// int i=1,len=0;
// while(i<n){
// if(s.charAt(i)==s.charAt(len)){
// lps[i]=len+1;
// len++;i++;
// }
// else{
// if(len==0){
// lps[i]=0;i++;
// }
// else{
// len=lps[len-1];
// }
// }
// }
// System.out.println(Arrays.toString(lps));
// int t=Integer.parseInt(br.readLine());
// while(t-->0){
// // int n=Integer.parseInt(br.readLine());
// String s=br.readLine();int n=s.length();
// int m=Integer.parseInt(br.readLine());
// List<String> l=new ArrayList<>();
// int mm=m;
// while(mm-->0)l.add(br.readLine());
// List<String > lt=new ArrayList<>(l);
// Collections.sort(l,(i,j)->{
// if(i.length()<j.length())return 1;return -1;
// });boolean f=true;int count=0;
// int []c=new int[n];
// int i=0,max=i-1;
// List<pair> al=new ArrayList<>();
// int index=-1;
// int ind=-1;
// while(i<n){
// for(int j=0;j<m;j++){
// String temp=l.get(j);
// int len=temp.length();
// if(n-i>=len && s.substring(i,i+len).equals(temp))
// if(max<i+len){
// max=i+len;
// index=i;
// ind=lt.indexOf(temp);
// }
// }
// if(c[i]==0 && max<=i){
// f=false;break;
// }
// else if(c[i]==0){
// int k=i;
// while(k<max)
// c[k++]=1;
// count++;al.add(new pair(ind+1,index+1));i++;
// }
// else
// i++;
// }
// if(!f){
// // for(pair p:al)
// // bw.write(p.x+" "+p.y+"\n");
// bw.write(-1+"\n");
// }
// else{
// bw.write(count+"\n");
// for(pair p:al)
// bw.write(p.x+" "+p.y+"\n");
// }
// }
// bw.flush();
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
bw.write(n+"\n");
for(int j=1;j<=n;j++)
bw.write(j+" ");
bw.write("\n");
for(int i=1;i<n;i++){
//swap(a,i,i+1);
for(int j=1;j<=n;j++){
if(j<=i)
bw.write(j+1+" ");
else if(j==i+1)
bw.write(1+" ");
else
bw.write(j+" ");
}
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 | cd0b2bb5a745588b04c10a08760ce95d | 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 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 | 9ad955ca217b7d9b525d95819fc3cc90 | 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.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();
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int t=in.nextInt();
while(t-- > 0){
int n = in.nextInt();
out.println(n);
int arr[]=new int[n];
for(int j=0;j<n;j++){
arr[j]=j+1;
}
for(int j=0;j<n-1;j++){
for(int k=0;k<n;k++){
out.print(arr[k]+" ");
}
out.print("\n");
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
for(int k=0;k<n;k++){
out.print(arr[k]+" ");
}
out.print("\n");
}
out.close();
} 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 | 27e06ca65a7844dd724da861e5cadbfe | 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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
public class coding {
//-----------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[] ) throws Exception {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
StringBuilder sb = new StringBuilder();
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;
}
int n1=n;
out.println(n);
for(int i=0;i<n;i++)
{
out.print(arr[i]+" ");
}
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 i1=0;i1<n;i1++)
{
out.print(arr[i1]+" ");
}
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 | 55ff5b6703f5292bd32820512ecdfc5f | 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.*;
import java.util.stream.Collectors;
public class P03 {
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 ;
}
}
static class BPair {
Integer index;boolean value;
public BPair(Integer index, boolean value) {this.value =value;this.index =index;}
int getIndex() {return index;}
boolean getValue() {return value;}
}
static class Pair implements Comparable<Pair> {
Integer index,value;
public Pair(Integer index, Integer value) {this.value = value;this.index = index;}
@Override public int compareTo(Pair o){return value - o.value;}
int getValue(){ return value;}
int getindex(){return index;}
}
private static long gcd(long l, long m){
if(m==0) {return l;}
return gcd(m,l%m);
}
static void swap(long[] arr, int i, int j){long temp = arr[i];arr[i] = arr[j];arr[j] = temp;}
static int partition(long[] arr, int low, int high){
long pivot = arr[high];
int i = (low - 1);
for(int j = low; j <= high - 1; j++){
if (arr[j] < pivot){
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}
static void quickSort(long[] arr, int low, int high){
if (low < high){
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
static long M = 1000000007;
static long powe(long a,long b) {
long res =1;
while(b>0){
if((b&1)!=0) {
res=(res*a)&M;
}
a=(a*a)%M;
b>>=1;
}
return res;
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0){
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static boolean [] seive(int n) {
boolean a[] = new boolean[n+1];
Arrays.fill(a, true);
a[0]=false;
a[1]=false;
for(int i =2;i<=Math.sqrt(n);i++) {
for(int j =2*i;j<=n;j+=i) {
a[j]=false;
}
}
return a;
}
private static String swap(String s, int i, int j){
if(i>=j)return s;
String ans = s.substring(0,i);
ans+=s.charAt(j);
ans+=s.substring(i+1,j);
ans+=s.charAt(i);
ans+=s.substring(j+1);
return ans;
}
private static void pa(char[] cs){for(int i =0;i<cs.length;i++){System.out.print(cs[i]+" ");}System.out.println();}
private static void pa(long[] b){for(int i =0;i<b.length;i++) {System.out.print(b[i]+" ");}System.out.println();}
private static void pm(Character[][] a){for(int i =0;i<a.length;i++){for(int j=0;j<a[i].length;j++){System.out.print(a[i][j]);}System.out.println();}}
private static void pm(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();}}
private static void pa(int[] a){for(int i =0;i<a.length;i++){System.out.print(a[i]+" ");}System.out.println();}
private static boolean isprime(int n){if (n <= 1) return false;else if (n == 2) return true;else if (n % 2 == 0) return false;for (int i = 3; i <= Math.sqrt(n); i += 2){if (n % i == 0) return false;}return true;}
static int lb(int[] b, long a){
int l=-1,r=b.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(b[m]>=a) r=m;
else l=m;
}
return r;
}
static int ub(long a[], long x){
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
static //MAIN FUNCTION ------->
ArrayList<String> alpos = new ArrayList<>();
public static void main(String[] args) throws IOException {
long start2 = System.currentTimeMillis();
FastReader fs = new FastReader();
// Scanner fs = new Scanner (System.in);
HashMap<Integer,String[]> hm = new HashMap<>();
String temp [] = {"1"};
hm.put(1,temp);
for(int i =2;i<=100;i++) {
hm.put(i,fun(i));
}
int t =1;
t = fs.nextInt();
while(t-->0) {
int n = fs.nextInt();
String ans [] = hm.get(n);
System.out.println(n);
for(int i =0;i<n;i++) {
System.out.println(ans[i]);
}
}
long end2 = System.currentTimeMillis();
// System.out.println("time :"+(end2-start2));
// sc.close();
}
public static String [] fun(int n) {
int a [] = new int [n];
for(int i =0;i<n;i++) {
a[i]=i+1;
}
// System.out.println(n);
String ans [] = new String [n];
ans[0]=st(a);
for(int i =0;i<n-1;i++) {
int temp = a[i];
a[i]=a[n-1];
a[n-1]=temp;
ans[i+1]=st(a);
}
return ans;
}
public static String st(int a[]) {
String s="";
for(int i =0;i<a.length;i++) {
s+=Integer.toString(a[i]);
if(i!=a.length)s+=" ";
else s+='\n';
}
return 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 | 6bf9c1ff50e87b6a3d74477877c17f25 | 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 Main{
static class Node{
int s,e;
String val;
Node left, right;
}
static Node segmentTree(int s, int e, int[] arr){
Node res = new Node();
res.s= s;
res.e = e;
if(s==e){
res.val = String.valueOf(arr[s]);
return res;
}
int mid = (s+e)/2;
res.left = segmentTree(s, mid, arr);
res.right =segmentTree(mid+1, e, arr);
res.val = res.left.val + " " + res.right.val;
return res;
}
static void update(Node node, int i, int val){
if(node.s == node.e){
node.val = String.valueOf(val);
return;
}
int mid = (node.s + node.e)/2;
if(i <= mid) update(node.left, i, val);
else update(node.right, i, val);
node.val = node.left.val + " " + node.right.val;
}
static Node root;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t, n, temp;
t = sc.nextInt();
while(t-- > 0){
n = sc.nextInt();
int[] arr = new int[n];
System.out.println(n);
for(int i= 0; i< n;i++) {
arr[i] = i+1;
}
root = segmentTree(0, n-1, arr);
System.out.println(root.val);
for(int i=1;i<n;i++){
update(root, i, arr[i-1]);
update(root, i-1, arr[i]);
temp = arr[i-1];
arr[i-1] = arr[i];
arr[i] = temp;
System.out.println(root.val);
}
}
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 | 20b48dc14f9b45668b37c3abfcf52093 | 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.sql.PreparedStatement;
import java.util.*;
public class Main {
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) throws IOException {
MyScanner s=new MyScanner();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
String check=s.nextLine();
if(check==null){
return;
}
int t=Integer.parseInt(check);
while(t-->0) {
int a =s.nextInt();
int j=a-1;
System.out.println(a);
for(int i=0;i<a;i++){
output.write((i+1)+" ");
}
output.write("\n");
while(j>0){
output.write(a+" ");
for(int i=1;i<a;i++){
if(i==j){
output.write(1+" ");
}else if(i>j){
output.write(i+" ");
}else{
output.write((i+1)+" ");
}
}
output.write("\n");
j--;
}
output.flush();
// for(int i=0;i<a;i++){
// System.out.print(arr[i]+" ");
// }
// System.out.println();
// if(a%2==1){
// int temp=arr[j];
// arr[j]=arr[j+1];
// arr[j+1]=temp;
// for(int i=0;i<a;i++){
// System.out.print(arr[i]+" ");
// }
// System.out.println();
// }
// if(a==0){
// System.out.println(0);
// continue;
// }
// if(a==1){
// System.out.println(2);
// continue;
// }
// if(a==2||a==3){
// System.out.println(1);
// continue;
// }
// if(a%3==0){
// System.out.println(a/3);
// }else if(a%3==2){
// System.out.println(1+ a/3);
// }else{
// System.out.println(Math.min(2+ a/3,a/2));
}
// int c=s.nextInt();
// for(int i=0;i<arr.length;i++) {
// arr[i] = s.nextInt();
// }
// Arrays.sort(arr);
// int lo=1;
// int hi=arr[arr.length-1]-arr[0];
// int res=-1;
// while(lo<=hi){
// int mid=lo+(hi-lo)/2;
// int ans=bs(arr,mid);
// if(ans<c){
// hi=mid-1;
// }else{
// res=mid;
// lo=mid+1;
// }
// }
// System.out.println(res);
/*int n=s.nextInt();
if(n%2==0){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
output.write(-1+" ");
}
output.write("\n");
}
}else{
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j){
output.write(-1+" ");
continue;
}
output.write(1+" ");
}
output.write("\n");
}
}
output.flush();
}*/
}
public static int dfs(int[][] arr,int rem,int i,int j,int val){
if(i<0||i>1||j<0||j>=arr.length||arr[i][j]==-1){
return -1;
}
rem--;
if(rem==0){
if(val>arr[i][j]){
return 1;
}
return arr[i][j]- val;
}
int temp=arr[i][j];
arr[i][j]=-1;
int min=Integer.MAX_VALUE;
int a=dfs(arr,rem,i+1,j,temp);
if(a>-1){
min=Math.min(min,a);
}
int a1=dfs(arr,rem,i-1,j,temp);
if(a1>-1){
min=Math.min(min,a1);
}
int a2=dfs(arr,rem,i,j+1,temp);
if(a2>-1){
min=Math.min(min,a2);
}
int a3=dfs(arr,rem,i,j-1,temp);
if(a3>-1){
min=Math.min(min,a3);
}
if(min==Integer.MAX_VALUE){
return -1;
}
return min;
}
public static int bs(int[] arr,int mid){
int dis=0;
int ans=1;
for(int i=1;i<arr.length;i++){
dis+=arr[i]-arr[i-1];
if(dis>=mid){
dis=0;
ans++;
}
}
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 | 934c7d2d0621c6a340c749036bdcd848 | 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.*;
public class Main {
public static void main(String[] args) throws IOException {
//Scanner sc = new Scanner(System.in);
long start = System.currentTimeMillis();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
for (int k = 0; k < t; k++) {
int n = Integer.parseInt(br.readLine());
pw.println(n);
int[] arr = new int[n];
for (int i = 0; i < n ; i++) {
arr[i] = i + 1;
pw.print(i+1 + " ");
}
pw.println();
for (int i = 0; i < n-1; i++) {
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
for (int num:arr)
pw.print(num + " ");
pw.println();
}
}
long finish = System.currentTimeMillis();
long time = finish - start;
// System.out.println(time);
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 | 8fb856cab55b666c909e0e856890af7b | 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.Random;
import java.util.StringTokenizer;
import java.util.concurrent.ThreadLocalRandom;
// 1 2 3 4
// 2 1 3 4
// 2 3 1 4
// 2 3 4 1
public class CF {
static int[] b;
static PrintWriter pw;
private static void sport(int n) {
b = new int[n];
pw.println(n);
//System.out.println(n);
for (int i = 0; i < n; i++) {
b[i] = i + 1;
pw.print(b[i]);
pw.print(" ");
}
pw.println();
for (int i = n - 1; i > 0; i--) {
swap(b, i, i - 1);
for (int j = 0; j < n; j++) {
pw.print(b[j]);
pw.print(" ");
}
pw.println();
}
}
/*
static void gen() {
a = new int[100][100];
int[] a0 = new int[100];
for (int i = 0; i < 100; i++) {
a0[i] = i + 1;
}
a[0] = Arrays.copyOf(a0, 100);
for (int i = 1; i <= 99; i++) {
swap(a0, i, i - 1);
a[i] = Arrays.copyOf(a0, 100);
}
}
*/
static void shuffleArray(int[] ar) {
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
pw = new PrintWriter(System.out);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
sport(n);
}
pw.close();
}
static void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
static class BIT {
// The size of the array holding the Fenwick tree values
final int N;
// This array contains the Fenwick tree ranges
private long[] tree;
// Create an empty Fenwick Tree with 'sz' parameter zero based.
public BIT(int sz) {
tree = new long[(N = sz + 1)];
}
// Construct a Fenwick tree with an initial set of values.
// The 'values' array MUST BE ONE BASED meaning values[0]
// does not get used, O(n) construction.
public BIT(long[] values) {
if (values == null) {
throw new IllegalArgumentException("Values array cannot be null!");
}
N = values.length;
values[0] = 0L;
// Make a clone of the values array since we manipulate
// the array in place destroying all its original content.
tree = values.clone();
for (int i = 1; i < N; i++) {
int parent = i + lsb(i);
if (parent < N) {
tree[parent] += tree[i];
}
}
}
// Returns the value of the least significant bit (LSB)
// lsb(108) = lsb(0b1101100) = 0b100 = 4
// lsb(104) = lsb(0b1101000) = 0b1000 = 8
// lsb(96) = lsb(0b1100000) = 0b100000 = 32
// lsb(64) = lsb(0b1000000) = 0b1000000 = 64
private static int lsb(int i) {
// Isolates the lowest one bit value
return i & -i;
// An alternative method is to use the Java's built in method
// return Integer.lowestOneBit(i);
}
// Computes the prefix sum from [1, i], O(log(n))
private long prefixSum(int i) {
long sum = 0L;
while (i != 0) {
sum += tree[i];
i &= ~lsb(i); // Equivalently, i -= lsb(i);
}
return sum;
}
// Returns the sum of the interval [left, right], O(log(n))
public long sum(int left, int right) {
if (right < left) {
throw new IllegalArgumentException("Make sure right >= left");
}
return prefixSum(right) - prefixSum(left - 1);
}
// Get the value at index i
public long get(int i) {
return sum(i, i);
}
// Add 'v' to index 'i', O(log(n))
public void add(int i, long v) {
while (i < N) {
tree[i] += v;
i += lsb(i);
}
}
// Set index i to be equal to v, O(log(n))
public void set(int i, long v) {
add(i, v - sum(i, i));
}
@Override
public String toString() {
return java.util.Arrays.toString(tree);
}
}
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());
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
int[] readArrayInt(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());
}
}
} | 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 | 2eadc3fc7f35d29298dffed38ce5464b | 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 code4 {
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t-- > 0) {
StringBuilder s = new StringBuilder("");
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i+1;
}
s.append(n);
s.append("\n");
for (int i = 0; i < n; i++) {
s.append(arr[i] + " ");
}
s.append("\n");
for (int i = 0; i < n-1; i++) {
swap(arr, i, n-1);
for (int j = 0; j < n; j++) {
s.append(arr[j] + " ");
}
if(i != n-1-1)
s.append("\n");
}
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 | 941a2ee81c3fd5bf575f18703a67e3bf | 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 FastScanner fs;
static FastWriter fw;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}};
private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100);
private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100);
private static final int mod1 = (int) (1e9 + 7);
private static final int mod2 = 998244353;
public static void main(String[] args) throws IOException {
fs = new FastScanner();
fw = new FastWriter();
int t = 1;
t = fs.nextInt();
while (t-- > 0) {
solve();
}
fw.out.close();
}
private static void solve() {
int n = fs.nextInt();
int[] res = new int[n];
for(int i = 0; i<n ; i++){
res[i] = i+1;
}
int k = n;
fw.out.println(k);
int l = 0;
int r = n-1;
while(l<=r){
for(int i = 0; i<n; i++){
fw.out.print(res[i] + " ");
}
fw.out.println();
swap(res, l, r);
l++;
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static class UnionFind {
private final int[] parent;
private final int[] rank;
UnionFind(int n) {
parent = new int[n + 5];
rank = new int[n + 5];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
private int find(int i) {
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}
private void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
if (rank[a] == rank[b])
rank[a]++;
}
}
}
private static class Calc_nCr {
private final long[] fact;
private final long[] invfact;
private final int p;
Calc_nCr(int n, int prime) {
fact = new long[n + 5];
invfact = new long[n + 5];
p = prime;
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (i * fact[i - 1]) % p;
}
invfact[n] = pow(fact[n], p - 2, p);
for (int i = n - 1; i >= 0; i--) {
invfact[i] = (invfact[i + 1] * (i + 1)) % p;
}
}
private long nCr(int n, int r) {
if (r > n || n < 0 || r < 0) return 0;
return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p;
}
}
private static long gcd(long a, long b) {
return (b == 0 ? a : gcd(b, a % b));
}
private static long lcm(long a, long b) {
return ((a * b) / gcd(a, b));
}
private static long pow(long a, long b, int mod) {
long result = 1;
while (b > 0) {
if ((b & 1L) == 1) {
result = (result * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long ceilDiv(long a, long b) {
return ((a + b - 1) / b);
}
private static long getMin(long... args) {
long min = lMax;
for (long arg : args)
min = Math.min(min, arg);
return min;
}
private static long getMax(long... args) {
long max = lMin;
for (long arg : args)
max = Math.max(max, arg);
return max;
}
private static boolean isPalindrome(String s, int l, int r) {
int i = l, j = r;
while (j - i >= 1) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
private static List<Integer> primes(int n) {
boolean[] primeArr = new boolean[n + 5];
Arrays.fill(primeArr, true);
for (int i = 2; (i * i) <= n; i++) {
if (primeArr[i]) {
for (int j = i * i; j <= n; j += i) {
primeArr[j] = false;
}
}
}
List<Integer> primeList = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (primeArr[i])
primeList.add(i);
}
return primeList;
}
private static int noOfSetBits(long x) {
int cnt = 0;
while (x != 0) {
x = x & (x - 1);
cnt++;
}
return cnt;
}
private static boolean isPerfectSquare(long num) {
long sqrt = (long) Math.sqrt(num);
return ((sqrt * sqrt) == num);
}
private static class Pair<U, V> {
private final U first;
private final V second;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
private Pair(U ff, V ss) {
this.first = ff;
this.second = ss;
}
}
private static void randomizeIntArr(int[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInIntArr(arr, i, j);
}
}
private static void randomizeLongArr(long[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInLongArr(arr, i, j);
}
}
private static void swapInIntArr(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static void swapInLongArr(long[] arr, int a, int b) {
long temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextInt();
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextLong();
return arr;
}
private static List<Integer> readIntList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextInt());
return list;
}
private static List<Long> readLongList(int n) {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextLong());
return list;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws IOException {
if (checkOnlineJudge)
this.br = new BufferedReader(new FileReader("src/input.txt"));
else
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
private static class FastWriter {
PrintWriter out;
FastWriter() throws IOException {
if (checkOnlineJudge)
out = new PrintWriter(new FileWriter("src/output.txt"));
else
out = new PrintWriter(System.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 | da8616db8e9c955d077eaf61a5e38e21 | 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;
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int test_cases = scan.nextInt();
int[] n = new int[test_cases];
for(int i = 0 ; i < test_cases ; i++ ){
n[i] = scan.nextInt();
}
for(int i = 0; i < test_cases; i++){
int[] permutation = new int[n[i]];
for(int j = 1; j <= n[i]; j++){
permutation[j-1] = j;
}
System.out.println(n[i]);
System.out.println(Arrays.toString(permutation).replace("[", "")
.replace("]", "")
.replace(",", " "));
for(int k = 0; k < n[i]; k++){
if(k == n[i] - 1){
System.out.println();
break;
}
int temp = permutation[k];
permutation[k] = permutation[n[i] - 1];
permutation[n[i] - 1] = temp;
System.out.println(Arrays.toString(permutation).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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.