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 | 3d7d88e5ff3cc0c84e5102628a7e8a66 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] YDSV) throws IOException{
StringTokenizer st=new StringTokenizer(br.readLine());
//int t=1;
int t=Integer.parseInt(st.nextToken());
while(t-->0){
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
// st=new StringTokenizer(br.readLine());
int[] ar=new int[n];
// for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken());
for(int i=0;i<n;i++) ar[i]=n-i;
int c=n-1;
for(int i=0;i<n;i++){
for(int j:ar){
bw.write(j+" ");
}
bw.newLine();
if(i!=n-1){
int temp=ar[c];
ar[c]=ar[c-1];
ar[c-1]=temp;
c--;
}
}
}
bw.flush();
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 5073141d292098751d4327cfb4158bc4 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | //package codeforces;
import java.util.*;
public class B123edu {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = 1;
t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = n - 1; i >= 0; i--) {
arr[n - i - 1] = i + 1;
// p(i + 1, " ");
}
// pt("");
for (int j = 0; j < n - 1; j++) {
int idx = n - 1 - j;
int el = arr[idx];
p(arr[0], " " );
p(el+" ");
for (int i = 1; i <n; i++) {
if (i!= idx)
p(arr[i], " ");
}
// arr[1] = el;
//
// for (int x : arr)
// p(x, " ");
pt("");
}
p(n - 1 + " ");
for (int i = n; i >= 1; i--) {
if (i == n - 1) {
} else
p(i + " ");
}
pt("");
}
}
public static <T> void pt(T x) {
System.out.println(x);
return;
}
public static <T> void pt(T str, T x) {
System.out.println(str + "" + x);
return;
}
public static <T> void p(T x, T y) {
System.out.print(x + "" + y);
return;
}
public static <T> void p(T x) {
System.out.print(x);
return;
}
public static int max(int x, int y) {
return Math.max(x, y);
}
public static int min(int x, int y) {
return Math.min(x, y);
}
public static int abs(int x, int y) {
return Math.abs(x - y);
}
public static <T> void sort(T arr) {
Arrays.sort((int[]) arr);
return;
}
public static void sortr(int[] arr) {
int n = arr.length;
Integer[] ar = new Integer[n];
for (int i = 0; i < n; i++)
ar[i] = arr[i];
Arrays.sort(ar, (a, b) -> {
return b - a;
});
for (int i = 0; i < n; i++)
arr[i] = ar[i];
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 0394ab96e1dfd0c121c23a79ead79fac | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
// import java.io.*;
// import java.util.*;
// public class Main{
// static class FastReader {
// BufferedReader br;
// StringTokenizer st;
// public FastReader()
// {
// br = new BufferedReader(
// new InputStreamReader(System.in));
// }
// String next()
// {
// while (st == null || !st.hasMoreElements()) {
// try {
// st = new StringTokenizer(br.readLine());
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// }
// return st.nextToken();
// }
// int nextInt() { return Integer.parseInt(next()); }
// long nextLong() { return Long.parseLong(next()); }
// double nextDouble()
// {
// return Double.parseDouble(next());
// }
// String nextLine()
// {
// String str = "";
// try {
// str = br.readLine();
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// return str;
// }
// }
// public static class Pair implements Comparable<Pair>
// {
// double cpu;
// int tu;
// int op;
// int cost;
// Pair(double cpu,int tu,int op,int cost)
// {
// this.cpu=cpu;
// this.tu=tu;
// this.op=op;
// this.cost=cost;
// }
// public int compareTo(Pair o)
// {
// return (int)(o.cpu-this.cpu);
// }
// }
// public static void main(String[] args) throws Exception {
// FastReader sc = new FastReader();
// int t=sc.nextInt();
// while(t-->0)
// {
// int n=sc.nextInt();
// int k=sc.nextInt();
// int[] val=new int[n];
// int[] coins=new int[n];
// for(int i=0;i<n;i++)
// {
// val[i]=sc.nextInt();
// }
// for(int i=0;i<n;i++)
// {
// coins[i]=sc.nextInt();
// }
// Pair cost[]=new Pair[n];
// for(int i=0;i<n;i++)
// {
// double sum=val[i]/(i+1)+1;
// int op=Math.log(10)/Math.sum;
// if(val[i]%(i+1)!=0)
// {
// op++;
// }
// int tu=val[i];
// double cpu=coins[i]/val[i];
// cost[i]=new Pair(cpu,tu,op,coins[i]);
// }
// Arrays.sort(cost);
// for( Pair i:cost)
// {
// System.out.print(i.cpu+","+i.op+"-"+i.cost+"**");
// }
// System.out.println();
// long ans=0;
// for(int i=0;i<n;i++)
// {
// long amt=cost[i].cost;
// if(cost[i].op<=k)
// {
// k-=cost[i].op;
// ans+=amt;
// System.out.print(cost[i].op+" ");
// }
// }
// System.out.println(ans);
// }
// }
// }
import java.io.*;
import java.util.*;
import javax.swing.text.AbstractDocument.LeafElement;
public class Main{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class Pair implements Comparable<Pair>
{
int i;
int j;
Pair(int i,int j)
{
this.i=i;
this.j=j;
}
public int compareTo(Pair o)
{
return this.i-o.i;
}
}
public static void main(String[] args) throws Exception
{
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int arr[]=new int[n];
int st=1;
if(n==3)
{
System.out.println("3"+" "+"2"+" "+1);
System.out.println("3"+" "+"1"+" "+2);
System.out.println("1"+" "+"3"+" "+2);
}
else
{
int idx=0;
for(int i=n;i>0;i--)
{
arr[idx++]=i;
}
idx=n-1;
while(idx>=0)
{
for(int i:arr)
System.out.print(i+" ");
System.out.println();
if(idx==0)
break;
move(arr,idx);
idx--;
}
}
}
}
public static void move(int[] arr,int n)
{
int temp=arr[n];
arr[n]=arr[n-1];
arr[n-1]=temp;
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 2401ce36892d3eeec0525587c644a9c9 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int fb[] = new int[n];
for(int i=0;i<n;i++){
fb[i] = i+1;
}
int k = 0;
for(int i=0;i<n;i++){
for(int j=n-1;j>=0;j--){
System.out.print(fb[j]+" ");
}
if(i==n-1)break;
int tmp = fb[k];
fb[k]=fb[k+1];
fb[k+1] = tmp;
k++;
System.out.println();
}
System.out.println();
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 427399837cf05328af2a4223faee92b7 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
try {
int t = in.nextInt(); while(t-- > 0) { solve(); /*out.println()*/;}
// solve();
} finally {
out.close();
}
return;
}
public static void solve() {
int n = in.nextInt();
for(int i = 1; i <= n; i++) {
out.print(i + " ");
for(int k = n; k >= 1; k--) {
if(k == i) {
continue;
}
out.print(k + " " );
}
out.println();
}
return;
}
//-------------- Helper methods-------------------
public static int[] fillArray(int n) {
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
return array;
}
public static char[] fillArray() {
char[] array = in.next().toCharArray();
return array;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static MyScanner in;
//-----------MyScanner class for faster input----------x§x
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;
}
}
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;
}
}
//--------------------------------------------------------
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 2fb8fc32bd0909626405628c3fc27242 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
try {
int t = in.nextInt(); while(t-- > 0) { solve(); /*out.println()*/;}
// solve();
} finally {
out.close();
}
return;
}
public static void solve() {
int n = in.nextInt();
if(n == 3) {
out.println("3 2 1");
out.println("3 1 2");
out.println("1 3 2");
return;
}
for(int k = n - 1; k >= 0; k--) {
int d = k;
do {
d = ((d-1) + n) % n;
out.print( (d + 1) + " ");
} while(d != k);
out.println();
}
return;
}
//-------------- Helper methods-------------------
public static int[] fillArray(int n) {
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
return array;
}
public static char[] fillArray() {
char[] array = in.next().toCharArray();
return array;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static MyScanner in;
//-----------MyScanner class for faster input----------x§x
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;
}
}
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;
}
}
//--------------------------------------------------------
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | c8b4a83dcc36a1305d5651f662d0e5c1 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class cf1644B {
public static void main(String[] args) {
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
for(int i=1; i<=n; i++) {
sb.append(i + " ");
for(int j=n; j>0; j--) {
if(j==i) continue;
sb.append(j + " ");
}
sb.append("\n");
}
}
System.out.print(sb);
}
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\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 4ebe94830908975b6240782af3c4b0a5 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class cf1644B {
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int[] a = new int[n];
if(n==3) {
System.out.println("3 2 1\n2 3 1\n1 3 2");
}
else {
for(int i=n; i>0; i--) a[n-i] = i; // [4,3,2,1]
for(int i=0; i<n; i++) {
for(int start1=i; start1<n; start1++) {
System.out.print(a[start1] + " ");
}
for(int start2=0; start2<i; start2++) {
System.out.print(a[start2] + " ");
}
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;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | ae891ff5776aa2ca8aad2afe8cf15a07 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Collections;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int T = in.nextInt();
while (T-- > 0) {
solveOne(in, out);
}
}
private void solveOne(Scanner in, PrintWriter out) {
int N = in.nextInt();
if (N == 3) {
out.println("3 2 1");
out.println("1 3 2");
out.println("2 3 1");
return;
}
int A[] = L.identityPerm(N);
L.reversesort(A);
L.printIntArray(A, out);
out.println();
int cnt = N - 1;
while (cnt > 0) {
for (int i = 0; i < N - 1 && cnt > 0; i++) {
L.swap(A, i, i + 1);
if (i - 1 >= 0 && A[i + 1] == A[i] + A[i - 1]) {
continue;
}
L.printIntArray(A, out);
out.println();
cnt--;
}
}
}
}
static class L {
public static void printIntArray(int[] array, PrintWriter out) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < array.length; i++) {
stringBuilder.append(array[i] + " ");
}
out.print(stringBuilder.toString());
}
public static void reversesort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = a.length - 1, j = 0; i >= 0; i--, j++) a[j] = l.get(i);
}
public static int[] identityPerm(int N) {
int[] p = new int[N];
for (int i = 1; i <= N; i++) {
p[i - 1] = i;
}
return p;
}
public static void swap(int[] A, int i, int j) {
int t = A[i];
A[i] = A[j];
A[j] = t;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | a179d2022079b01a2635633db647cb94 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.Scanner;
import java.io.PrintWriter;
public class antiFib{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
for(int i=1; i <= n;i++){
pw.print(i + " ");
for(int j=n ;j>0;j--){
if(j!=i)
pw.print(j + " ");
}
pw.println();
}
}
pw.flush();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 80fc46f56d01e9aade48d84979db63b1 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.LinkedList;
import java.util.Scanner;
import java.io.PrintWriter;
public class antiFib{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int arr[] = new int[n];
for(int i =0;i<n;i++){
arr[i] += (i+1);
}
LinkedList<Integer> l = new LinkedList<Integer>();
for(int i=0; i < n;i++){
l.add(arr[i]);
for(int j=0;j<n;j++){
// if(l.size()==n)
// break;
if(j!=i){
if(l.size()<2){
l.add(arr[j]);
}
else{
int n1 = l.removeLast();
int n2 = l.removeLast();
if(n1+n2!=arr[j]){
l.add(n2);
l.add(n1);
l.add(arr[j]);
}
else if(n1+arr[j]!=n2){
l.add(n2);
l.add(arr[j]);
l.add(n1);
}
else if(n2+arr[j]!=n1){
l.add(n2);
l.add(arr[j]);
l.add(n1);
}
}
}
}
while(!l.isEmpty())
pw.print(l.removeFirst() + " ");
pw.println();
}
}
pw.flush();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 424ed0e3513a49f2f8f1fd574e6eb24f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.lang.Math;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class codeforces {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
for(int i=1;i<=n;++i)
{
System.out.print(i);
for(int j=n;j>0;--j)
{
if(i!=j)
{
System.out.print(" "+j);
}
}
System.out.println("");
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | e5a55b154558c817422e902a3a88551e | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;import java.util.*;
public class Main {
static FastReader fr=new FastReader();
static int[] maxSubArraySum(int a[], int size)
{
int max_so_far = a[0];
int curr_max = a[0];
int dp[]=new int[size];
dp[0]=a[0];
for (int i = 1; i < size; i++)
{
curr_max = Math.max(a[i], curr_max+a[i]);
dp[i]=max_so_far = Math.max(max_so_far, curr_max);
}
return dp;
}
public static void main(String[] args) throws IOException {
int tt=1;
tt=fr.ni();
while(tt-->0) {
int n=fr.ni();
List<Integer> al=new ArrayList<>();
for(int i=1;i<=n;i++) {
al.add(n-i+1);
// al.add(i+1)
}
int y=n;
if(n==3) {
System.out.println(3+" "+2+" "+ 1);
System.out.println(1+" "+3+" "+ 2);
System.out.println(3+" "+1+" "+ 2);
continue;
}
List<Integer> bl=new ArrayList<>(al);
while(n-->0) {
Collections.rotate(al, -1);
opil(al);
}
}
}
static void opil(List<Integer> al) {StringBuilder sb= new StringBuilder();for(int x:al)sb.append(x+" ");System.out.println(sb.toString());}
static void opll(List<Long> al) {StringBuilder sb= new StringBuilder();for(long x:al)sb.append(x+" ");System.out.println(sb.toString());}
static int[] iarr(int n) {int a[]=new int[n];for(int i=0;i<n;i++)a[i]=fr.ni();return a;}
static long[] larr(int n) {long a[]=new long[n];for(int i=0;i<n;i++)a[i]=fr.nl();return a;}
static void op(int a[]) {StringBuilder sb= new StringBuilder();for(int x:a)sb.append(x+" ");System.out.println(sb.toString());}
static void op(long a[]) {StringBuilder sb= new StringBuilder();for(long x:a)sb.append(x+" ");System.out.println(sb.toString());}
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 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 ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 3e445041751b5e5d22eeadf822160844 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;import java.util.*;
public class Main {
static FastReader fr=new FastReader();
public static void main(String[] args) throws IOException {
int tt=1;
tt=fr.ni();
while(tt-->0) {
int n=fr.ni();
List<Integer> al=new ArrayList<>();
for(int i=1;i<=n;i++) {
al.add(n-i+1);
// al.add(i+1)
}
int y=n;
if(n==3) {
System.out.println(3+" "+2+" "+ 1);
System.out.println(1+" "+3+" "+ 2);
System.out.println(3+" "+1+" "+ 2);
continue;
}
List<Integer> bl=new ArrayList<>(al);
while(n-->0) {
Collections.rotate(al, -1);
StringBuilder sb= new StringBuilder();
for(int x:al)sb.append(x+" ");
System.out.println(sb.toString());
}
}
}
static int[] iarr(int n) {int a[]=new int[n];for(int i=0;i<n;i++)a[i]=fr.ni();return a;}
static long[] larr(int n) {long a[]=new long[n];for(int i=0;i<n;i++)a[i]=fr.nl();return a;}
static void op(int a[]) {StringBuilder sb= new StringBuilder();for(int x:a)sb.append(x+" ");System.out.println(sb.toString());}
static void op(long a[]) {StringBuilder sb= new StringBuilder();for(long x:a)sb.append(x+" ");System.out.println(sb.toString());}
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 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 ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | c3964be75c4769c3810ea2a7ef153ef3 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
out.print(i + " ");
for (int j = n; j >= 1; j--) {
if (j != i) {
out.print(j + " ");
}
}
out.println();
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | dcc3158c2d53de2a5d3cdf3bc8977a97 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
if (n == 3) {
out.println(3 + " " + 2 + " " + 1);
out.println(1 + " " + 3 + " " + 2);
out.println(3 + " " + 1 + " " + 2);
return;
}
List<Integer> antiFibonacciPermutations = new ArrayList<>();
for (int i = n; i >= 1; i--) {
antiFibonacciPermutations.add(i);
}
for (int i = 0; i < n; i++) {
for (int k = n - i; k < n; k++) {
out.print(antiFibonacciPermutations.get(k) + " ");
}
for (int k = 0; k < n - i; k++) {
out.print(antiFibonacciPermutations.get(k) + " ");
}
out.println();
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 55fe3c632c50fd2e0705e897b69e0dff | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
if (n == 3) {
out.println(3 + " " + 2 + " " + 1);
out.println(1 + " " + 3 + " " + 2);
out.println(3 + " " + 1 + " " + 2);
return;
}
List<Integer> antiFibonacciPermutations = new ArrayList<>();
for (int i = n; i >= 1; i--) {
antiFibonacciPermutations.add(i);
}
for (int i = 0; i < n; i++) {
for (int k = n - i; k < n; k++) {
out.print(antiFibonacciPermutations.get(k) + " ");
}
for (int j = 0; j < n - i; j++) {
out.print(antiFibonacciPermutations.get(j) + " ");
}
out.println();
}
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 4657fd91fb4b4d8861045a9013640aad | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Code_Forces {
static final int INT_MAX = Integer.MAX_VALUE;
static FastReader in = new FastReader();
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws java.lang.Exception {
int TestCases = in.nextInt();
while(TestCases-- > 0) {
int n = in.nextInt();
int[] A = new int[n];
int c = 0;
for (int i = n; i > 0; i--) {
A[c++] = i;
}
appendArray(A);
int indx = 0;
for (int i = 0; i < n - 1; i++) {
int[] cur = Arrays.copyOfRange(A,0, n );
if (i == n - 2){
if (n == 3){
swap(1, 2, cur);
} else swap(0, 2, cur);
}
else swap(indx, indx + 1, cur);
indx++;
appendArray(cur);
}
}
System.out.println(sb);
}
private static void appendArray(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
sb.append(a[i] + " ");
}
sb.append("\n");
}
private static int accumulate(int[] nums){
int n = nums.length;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += nums[i];
}
return sum;
}
private static long[] nextLongArray(int n){
long[] A = new long[n];
for (int i = 0; i < n; i++) {
A[i] = in.nextLong();
}
return A;
}
private static int[] nextIntArray(int n){
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = in.nextInt();
}
return A;
}
private static int ceilDiv(int a, int b){
int ans = a%b == 0 ? a/b : a/b + 1;
return ans;
}
private static void print(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
private static void swap(int indx, int i, int[] cur) {
int temp = cur[indx];
cur[indx] = cur[i];
cur[i] = temp;
}
private static int[] charToint(String[] arr){
int[] nums = new int[arr.length];
int indx = 0;
for (String it: arr){
nums[indx++] = Integer.parseInt(it);
}
return nums;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
long first;
long second;
public Pair(long first, long second) {
this.first = first;
this.second = second;
}
}
//13
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | a822ecc14d1d4bf65bd63f84212311ab | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Code_Forces {
static final int INT_MAX = Integer.MAX_VALUE;
public static void main(String[] args) throws java.lang.Exception {
FastReader in = new FastReader();
int TestCases = in.nextInt();
while(TestCases-- > 0) {
int n = in.nextInt();
int[] A = new int[n];
int c = 0;
for (int i = n; i > 0; i--) {
A[c++] = i;
}
print(A);
int indx = 0;
for (int i = 0; i < n - 1; i++) {
int[] cur = Arrays.copyOfRange(A,0, n );
if (i == n - 2){
if (n == 3){
swap(1, 2, cur);
} else swap(0, 2, cur);
}
else swap(indx, indx + 1, cur);
indx++;
print(cur);
}
}
}
private static void print(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
private static void swap(int indx, int i, int[] cur) {
int temp = cur[indx];
cur[indx] = cur[i];
cur[i] = temp;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
private static int[] charToint(String[] arr){
int[] nums = new int[arr.length];
int indx = 0;
for (var it: arr){
nums[indx++] = Integer.parseInt(it);
}
return nums;
}
}
static class Pair {
long first;
long second;
public Pair(long first, long second) {
this.first = first;
this.second = second;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | f3c31d6cbe3ff1b300c2cea651e269f3 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class codeforces{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n =sc.nextInt();
int x =0;
ArrayList<Integer> arr = new ArrayList<Integer>();
if(n==3) {
System.out.println("3 2 1\r\n"
+ "1 3 2\r\n"
+ "3 1 2");
}
else {
for(int i =1; i <=n;i++) {
for (int j =1;j<i;j++) {
arr.add(j);
}
if(i==1) {
for(int j =i;j<=n;j++) {
if((j-1)+(j-2)!=j) {
System.out.println(j);
}
else {
x = j;
}
}
System.out.println(x);
}
else {
for(int j =i;j<=n;j++) {
System.out.println(j);
}
}
Collections.sort(arr, Collections.reverseOrder());
for (int a : arr)
{
System.out.println(a);
}
arr.removeAll(arr);
}
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | c6c1aa2b9e3bef3ecded0ccdb960aee5 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
public class practice {
public static void helper(int n){
int[] arr=new int[n+1];
int f=n;
for(int i=1;i<=n;i++)arr[i]=f--;
print(arr,1,n);
System.out.println();
int idx=n;
for(int i=2;i<=n;i++){
if(i==3 && n==3)
{
int x=arr[3];
arr[3]=arr[1];
arr[1]=x;
System.out.print(3+" "+1+" "+2);
}
else {
print(arr,idx,n);
print(arr,1,idx-1);
}
System.out.println();
idx--;
}
}
public static void print(int[] arr,int i,int j){
for(int x=i;x<=j;x++)System.out.print(arr[x]+" ");
}
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);
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 954506c1d7fab38c7c61da03cd51217e | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
for(int i=n;i>=1;i--)
System.out.print(i+" ");
System.out.println();
for(int i=n-1;i>=1;i--)
{
if(i==n-1)
{
System.out.print(n-1+" "+n+" ");
continue;
}
System.out.print(i+" ");
}
System.out.println();
for(int i=1;i<=n-2;i++)
{
System.out.print(1+" ");
for(int j=n;j>n-i;j--)
System.out.print(j+" ");
for(int j=2;j<=n-i;j++)
System.out.print(j+" ");
System.out.println();
}}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 462597c312306e0d2aa9e95e7e2f73cf | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.Scanner;
public class antifibonacci{
public static void main(String[] args){
Scanner xed=new Scanner(System.in);
int t=xed.nextInt();
for(int j=0;j<t;j++)
{
int n=xed.nextInt();
int[] a=new int[n];
for(int i=0;i<a.length;i++)
{
a[i]=i+1;
}
for(int i=0;i<a.length/2;i++)
{
int f=a[i];
a[i]=a[a.length-i-1];
a[a.length-i-1]=f;
}
if(a.length==3)
{
System.out.println(3+" "+2+" "+1);
System.out.println(1+" "+3+" "+2);
System.out.println(3+" "+1+" "+2);
}
else
{
for(int z=0;z<a.length;z++)
{
for(int i=0;i<a.length-1;i++)
{
int d=a[i];
a[i]=a[i+1];
a[i+1]=d;
}
for(int k=0;k<a.length;k++)
{
System.out.print(a[k]+" ");
}
System.out.println();
}
}
}
xed.close();
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | ebbc8b1bf27356313b51006ad62e5636 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[]a=new int[n];
for(int i=n;i>=1;--i){
a[n-i]=i;
System.out.print(a[n-i]+" ");
}
System.out.println(" ");
int ref=1;
int[]dum=new int[n];
for(int i=n;i>=1;--i){
dum[n-i]=i;
}
while(ref<n){
int temp=dum[ref-1];
dum[ref-1]=dum[ref];
dum[ref]=temp;
for(int i=0;i<n;++i){
System.out.print(dum[i]+" ");
}
System.out.println(" ");
for(int i=n;i>=1;--i){
dum[n-i]=i;
}
ref++;
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | a483ded6c378cdf4560aabe535a1ea93 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner go = new Scanner(System.in);
int t = go.nextInt();
while (t-->0){
int n = go.nextInt();
int[] a = new int[n];
int[] b = new int[n+1];
if (n == 3){
System.out.println("3 2 1\n" +
"1 3 2\n" +
"3 1 2");
}else {
for (int i=n; i>=1; i--){
for (int j=n; j>=1; j--){
if (i < j){
System.out.print(j + " ");
}else if (i>j){
System.out.print(j+1 + " ");
}else {
System.out.print(1 + " ");
}
}
System.out.println();
}
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 84c3c5faa60e4d506e31671ed570492b | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | // package template;
import java.util.*;
//import pepCodingGraph.GetComponentVisited.Edge;
import java.io.*;
public class Template {
public static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next () {
while(!st.hasMoreTokens()) {
try {
st=new StringTokenizer(br.readLine());
}catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = nextInt();
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
//prime sieve
public static boolean sieve[];
public static void sievePrime(int n) {
sieve = new boolean[n+1];
for (int i = 0; i <= n; i++)
sieve[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (sieve[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
sieve[i] = false;
}
}
}
public static boolean isPrime(int n)
{
boolean flag = true;
for(int i = 2;i*i<=n;i++)
{
if(n%i==0)
{
flag = false;
break;
}
}
return flag;
}
//swap
public static void swap(int a,int b) {
int temp= a;
a=b;
b=temp;
}
//Longest Palindromic String
public String manacher(String s){
StringBuilder sb = new StringBuilder();
sb.append("@");
for(int i=0;i<s.length();i++){
sb.append("#");
sb.append(s.charAt(i));
}
sb.append("#");
sb.append("$");
String ans = manacher_Util(sb.toString(),s);
return ans;
}
public String manacher_Util(String s,String org){
int lps[] = new int[s.length()];
int c = 0;
int r = 0;
for(int i=1;i<s.length()-1;i++){
int mirror = c - (i-c);
if(i<r){
lps[i] = Math.min(lps[mirror],r-i);
}
while(s.charAt(1+i+lps[i]) == s.charAt(i-1-lps[i])){
lps[i]++;
}
if(i+lps[i] > r){
c= i ;
r = i+lps[i];
}
}
int maxlen = Integer.MIN_VALUE;
int maxindex = Integer.MIN_VALUE;
for(int i=1;i<lps.length-1;i++){
if(lps[i] > maxlen){
maxlen = lps[i];
maxindex = i;
}
}
int firstindex = maxindex - maxlen + 1;
int actualFirstIndex = (firstindex-2)/2;
return org.substring(actualFirstIndex,actualFirstIndex+maxlen);
}
//kmp
public static int lps[];
public static void kmp(String s) {
lps = new int[s.length()];
int i = 1;
int len = 0;
while(i<s.length()) {
if(s.charAt(i) == s.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else {
if(len>0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int test = sc.nextInt();
PrintWriter o = new PrintWriter(System.out);
while(test-->0) {
int n = sc.nextInt();
int a[] = new int[n];
for(int i = 0;i<n;i++)
{
a[i] = n-i;
}
int g = n-1;
int l = n-2;
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
o.print(a[j]+" ");
}
o.println();
if(i==n-1)
{
continue;
}
int ntemp = a[g];
a[g] = a[l];
a[l] = ntemp;
g--;
l--;
}
}
o.close();
}
static void fillcomp(ArrayList<Edge>[] graph,boolean[] visited,ArrayList<Integer> comp,int v)
{
visited[v] = true;
comp.add(v);
for(int i = 0;i<graph[v].size();i++)
{
int nbr = graph[v].get(i).nbr;
if(visited[nbr]==false)
{
fillcomp(graph,visited,comp,nbr);
}
}
}
static class Edge {
int src;
int nbr;
// int wt;
Edge(int src, int nbr) {
this.src = src;
this.nbr = nbr;
// this.wt = wt;
}
}
public static boolean isPalindrome(String s)
{
int i = 0;
int j = s.length()-1;
while(i<j)
{
if(!(s.charAt(i)==s.charAt(j)))
{
return false;
}
}
return true;
}
static ArrayList<String> solve(int n)
{
ArrayList<String> a = new ArrayList<>();
int close = n;
int open = n;
String op = "";
r(open,close,op,a);
for(int i = 0;i<n;i++)
{
System.out.println(a.get(i));
}
return a;
}
static void r(int open ,int close,String op,ArrayList<String> a)
{
if(open==0 && close ==0)
{
a.add(op);
return;
}
if(open!=0)
{
String op1 = op;
op1 = op1 +"(";
r(open-1,close,op1,a);
}
if(close>open)
{
String op2 = op;
op2 = op2 +")";
r(open,close-1,op2,a);
}
}
static void ruffleSort(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;
}
Arrays.sort(arr);
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int fac(int n)
{
int ans = 1;
for(int i = 1;i<=n;i++)
{
ans *= i;
}
return ans;
}
}
class ppp{
double val;
double wt;
double ratio;
ppp(double val,double wt,double ratio)
{
this.val = val;
this.wt = wt;
this.ratio = ratio;
}
public double getVal() {
return val;
}
public void setVal(double val) {
this.val = val;
}
public double getWt() {
return wt;
}
public void setWt(double wt) {
this.wt = wt;
}
public double getRatio() {
return ratio;
}
public void setRatio(double ratio) {
this.ratio = ratio;
}
}
class pair{
int value;
int index;
pair(int value,int index){
this.value= value;
this.index=index;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 9a7526756671535b137625e69351d908 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution{
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=n-i;
System.out.print(arr[i]);
if(i<n-1)
System.out.print(" ");
}
System.out.println("");
for(int i=n-1;i>0;i--){
int temp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=temp;
for(int j=0;j<n;j++){
System.out.print(arr[j]);
if(j<n-1)
System.out.print(" ");
}
System.out.println("");
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 3127077590bc7ea78daf3d2106fd5d5d | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class Codeforces {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
if(n==3){
System.out.println(3+" "+2+" "+1);
System.out.println(1+" "+3+" "+2);
System.out.println(2+" "+3+" "+1);
}
else
{
for (int i = n; i >= 1; i--) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 2; i < n; i++) {
for (int j = i; j < i + n; j++) {
if (j % n == 2)
System.out.print(3 + " ");
else if (j % n == 3)
System.out.print(2 + " ");
else if (j % n == 0)
System.out.print(n + " ");
else System.out.print(j % n + " ");
}
System.out.println();
}
for (int i = 1; i <= n; i++) {
if (i == 2)
System.out.print(3 + " ");
else if (i == 3)
System.out.print(2 + " ");
else System.out.print(i + " ");
}
System.out.println();
}
}
} catch (Exception e) {
return;
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | b454a76a17b4540236d6d2aeb1aead8b | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static FastReader obj = new FastReader();
public static PrintWriter out = new PrintWriter(System.out);
public static void sort(long[] a) {
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < a.length; i++)
arr.add(a[i]);
Collections.sort(arr);
for (int i = 0; i < arr.size(); i++)
a[i] = arr.get(i);
}
public static void revsort(long[] a) {
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < a.length; i++)
arr.add(a[i]);
Collections.sort(arr, Collections.reverseOrder());
for (int i = 0; i < arr.size(); i++)
a[i] = arr.get(i);
}
//Cover the small test cases like for n=1 .
public static class pair {
long a;
long b;
pair(long x, long y) {
a = x;
b = y;
}
}
public static long l() {
return obj.nextLong();
}
public static int i() {
return obj.nextInt();
}
public static String s() {
return obj.next();
}
public static long[] l(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = l();
return arr;
}
public static int[] i(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = i();
return arr;
}
public static long ceil(long a, long b) {
return (a + b - 1) / b;
}
public static void p(long val) {
out.println(val);
}
public static void p(String s) {
out.println(s);
}
public static void pl(long[] arr) {
for (int i = 0; i < arr.length; i++) {
out.print(arr[i] + " ");
}
out.println();
}
public static void p(int[] arr) {
for (int i = 0; i < arr.length; i++) {
out.print(arr[i] + " ");
}
out.println();
}
public static void sortpair(Vector<pair> arr) {
//ascending just change return 1 to return -1 and vice versa to get descending.
//compare based on value of pair.a
arr.sort(new Comparator<pair>() {
public int compare(pair o1, pair o2) {
long val = o1.a - o2.a;
if (val == 0)
return 0;
else if (val > 0)
return 1;
else
return -1;
}
});
}
// Take of the small test cases such as when n=1,2 etc.
// remember in case of fenwick tree ft is 1 based but our array should be 0 based.
// in fenwick tree when we update some index it doesn't change the value to val but it
// adds the val value in it so remember to add val-a[i] instead of just adding val.
//in case of finding the inverse mod do it (biexpo(a,mod-2)%mod + mod )%mod
public static void main(String[] args) {
int len = i();
while (len-- != 0) {
int n=i();
for(int i=1;i<=n;i++)
{
out.print(i+" ");
for(int j=n;j>=1;j--)
{
if(j!=i)out.print(j+" ");
}
out.println();
}
}
out.flush();
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 9465f1b029b800f7330d48564327d462 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// static int mod = 998244353 ;
// static int N = 200005;
// static long factorial_num_inv[] = new long[N+1];
// static long natual_num_inv[] = new long[N+1];
// static long fact[] = new long[N+1];
// static void InverseofNumber()
//{
// natual_num_inv[0] = 1;
// natual_num_inv[1] = 1;
// for (int i = 2; i <= N; i++)
// natual_num_inv[i] = natual_num_inv[mod % i] * (mod - mod / i) % mod;
//}
//static void InverseofFactorial()
//{
// factorial_num_inv[0] = factorial_num_inv[1] = 1;
// for (int i = 2; i <= N; i++)
// factorial_num_inv[i] = (natual_num_inv[i] * factorial_num_inv[i - 1]) % mod;
//}
//static long nCrModP(long N, long R)
//{
// long ans = ((fact[(int)N] * factorial_num_inv[(int)R]) % mod * factorial_num_inv[(int)(N - R)]) % mod;
// return ans%mod;
//}
//static boolean prime[];
//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] is not changed, then it is a
// // prime
// if (prime[p] == true)
// {
// // Update all multiples of p
// for (int i = p * p; i <= n; i += p)
// prime[i] = false;
// }
// }
//}
public static void main (String[] args) throws java.lang.Exception
{
// InverseofNumber();
// InverseofFactorial();
// fact[0] = 1;
// for (long i = 1; i <= 2*100000; i++)
// {
// fact[(int)i] = (fact[(int)i - 1] * i) % mod;
// }
FastReader scan = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int t = scan.nextInt();
while(t-->0){
int n = scan.nextInt();
if(n==3){
pw.println("1 3 2");
pw.println("3 2 1");
pw.println("3 1 2");
}
else{
for(int i=2;i<=n;i++){
for(int j=i;j<=n;j++){
pw.print(j+" ");
}
for(int j=2;j<i;j++){
pw.print(j+" ");
}
pw.print("1");
pw.println();
}
pw.print("2 4 3 ");
for(int i=5;i<=n;i++){
pw.print(i+" ");
}
pw.print("1");
pw.println();
}
pw.flush();
}
}
//static long bin_exp_mod(long a,long n){
// long res = 1;
// while(n!=0){
// if(n%2==1){
// res = ((res)*(a));
// }
// n = n/2;
// a = ((a)*(a));
// }
// return res;
//}
//static long bin_exp_mod(long a,long n){
// long mod = 1000000007;
// long res = 1;
//
// while(n!=0){
// if(n%2==1){
// res = ((res%mod)*(a%mod))%mod;
// }
// n = n/2;
// a = ((a%mod)*(a%mod))%mod;
// }
// res = res%mod;
// return res;
//}
// static long gcd(long a,long b){
// if(a==0)
// return b;
// return gcd(b%a,a);
// }
// static long lcm(long a,long b){
// return (a/gcd(a,b))*b;
// }
}
class Pair{
int x,y;
Pair(int x,int y){
this.x = x;
this.y = y;
}
//public boolean equals(Object obj) {
// // TODO Auto-generated method stub
// if(obj instanceof Pair)
// {
// Pair temp = (Pair) obj;
// if(this.x.equals(temp.x) && this.y.equals(temp.y))
// return true;
// }
// return false;
//}
//@Override
//public int hashCode() {
// // TODO Auto-generated method stub
// return (this.x.hashCode() + this.y.hashCode());
//}
}
class Compar implements Comparator<Pair>{
public int compare(Pair p1,Pair p2){
if(p1.y==p2.y)
return 0;
else if(p1.y<p2.y)
return -1;
else
return 1;
}
}
//class DisjointSet{
// Map<Long,Node> map = new HashMap<>();
// class Node{
// long data;
// Node parent;
// int rank;
// }
// public void makeSet(long data){
// Node node = new Node();
// node.data = data;
// node.parent = node;
// node.rank = 0;
// map.put(data,node);
// }
// //here we just need the rank of parent to be exact
// public void union(long data1,long data2){
// Node node1 = map.get(data1);
// Node node2 = map.get(data2);
// Node parent1 = findSet(node1);
// Node parent2 = findSet(node2);
// if(parent1.data==parent2.data){
// return;
// }
// if(parent1.rank>=parent2.rank){
// parent1.rank = (parent1.rank==parent2.rank)?parent1.rank+1:parent1.rank;
// parent2.parent = parent1;
// }
// else{
// parent1.parent = parent2;
// }
// }
// public long findSet(long data){
// return findSet(map.get(data)).data;
// }
// private Node findSet(Node node){
// Node parent = node.parent;
// if(parent==node){
// return parent;
// }
// node.parent = findSet(node.parent);
// return node.parent;
// }
// }
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 73b5b32763cb6399f960c76ebee0524f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.*;
import static java.lang.System.out ;
public class CP0125 {
public static void main(String args[])throws Exception{
PrintWriter pw = new PrintWriter(out);
FastReader sc = new FastReader();
Reader s = new Reader();
int test;
//code yaha se hai
try{
test = sc.nextInt();
}
catch(Exception E){
return;
}
int n ;
int j;
int a ;
while(test-- > 0){
//Code
n = sc.nextInt();
if(n == 3){
pw.println("3 2 1");
pw.println("1 3 2");
pw.println("3 1 2");
continue;
}
for (int b = 0 ; b < n ; b++) {
j = 0;
for (int i = 0; i < n; i++) {
if (i == b) {
pw.print(1 + " ");
} else {
if(n-j == 1){
j++;
}
pw.print((n - j)+" ");
j++;
}
}
pw.println();
}
}
pw.close();
}
/*
Note :
Try using sorting , when approach is of O(N^2)
As sorting takes : O( N Log N )
*/
/*
len : Length of Array / inputs
num : key to map
//Putting values in map <Integer , ArrayList>
for (int i = 0; i < len; i++) {
num =sc.nextInt();
if(!mp.containsKey(num)){
mp.put(num ,new ArrayList<>());
}
}
//getting size of ArrayList
for(Integer key : mp.keySet()){
maxValue = max(maxValue , mp.get(key).size());
}
*/
/*
Sieve
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
*/
static long exponentiation(long base, long exp)
{
long N = 1000000007 ;
if (exp == 0)
return 1;
if (exp == 1)
return base % N;
long t = exponentiation(base, exp / 2);
t = (t * t) % N;
// if exponent is even value
if (exp % 2 == 0)
return t;
// if exponent is odd value
else
return ((base % N) * t) % N;
}
public static int BinarySearch (long ar[] , int start , int end , long key , boolean lower){
int mid ;
boolean found = false;
int ans = 0;
if(lower){
while(start <= end){
mid = start + (end-start) / 2;
if(ar[mid] < key ){
if(!found){
ans = mid;
}
start = mid+1;
}
else{
if(ar[mid] == key){
found = true;
ans = mid ;
}
end = mid-1;
}
}
}
else{
while(start <= end){
mid = start + (end-start) / 2;
if(ar[mid] <= key ){
if(ar[mid] == key){
found = true;
ans = mid;
}
start = mid+1;
}
else{
if(!found){
ans = mid;
}
end = mid-1;
}
}
}
return ans;
}
public static int BinarySearchArrayList (ArrayList<Long> ar , int start , int end , long key , boolean lower){
int mid ;
boolean found = false;
int ans = -1;
if(lower){
while(start <= end){
mid = start + (end-start) / 2;
if(ar.get(mid) < key ){
if(!found){
ans = mid;
}
start = mid+1;
}
else{
if(ar.get(mid) == key){
found = true;
ans = mid ;
}
end = mid-1;
}
}
}
else{
while(start <= end){
mid = start + (end-start) / 2;
if(ar.get(mid) <= key ){
if(ar.get(mid) == key){
found = true;
ans = mid;
}
start = mid+1;
}
else{
if(!found){
ans = mid;
}
end = mid-1;
}
}
}
return ans;
}
public static String reverseString(String str){
StringBuilder input1 = new StringBuilder();
// append a string into StringBuilder input1
input1.append(str);
// reverse StringBuilder input1
input1.reverse();
return input1+"";
}
public static void sort(int[] arr){
//because Arrays.sort() uses quick sort , Worst Complexity : o(N^2)
ArrayList <Integer> ls = new ArrayList<>();
for(int x:arr){
ls.add(x);
}
Collections.sort(ls);
//Collections.sort(arrayList) : Uses Merge Sort : Complexity O(NlogN)
for(int i = 0 ; i < arr.length ; i++){
arr[i] = ls.get(i);
}
}
public static long gcd(long a , long b){
if(a>b){
a = (a+b) - (a = b);
}
if(a == 0L){
return b;
}
return gcd(b%a ,a);
}
public static boolean isPrime(long n){
if(n < 2){
return false;
}
if(n == 2 || n == 3){
return true;
}
if(n % 2 == 0 || n % 3 == 0){
return false;
}
long sq = (long)Math.sqrt(n) + 1;
for(long i = 5L; i <=sq ; i=i+6){
if(n % i == 0 || n % (i+2) == 0){
return false;
}
}
return true;
}
static class Pair<T , K> {
T first ;
K second;
Pair(T fst , K scnd){
first = fst;
second = scnd;
}
}
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 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\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | b656e2cbc83320cfcb1e327f47ce067b | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.stream.IntStream;
public class B_AntiFib {
public static void main(String[] args) throws IOException {
BufferedScanner input = new BufferedScanner();
BufferedOutput output = new BufferedOutput();
int tasks = input.nextInt();
while (tasks-- > 0) {
int length = input.nextInt();
int[] sequence = new int[length];
sequence[0] = 3;
sequence[1] = 1;
sequence[2] = 2;
for (int i = 3; i < length; i++) {
sequence[i] = i + 1;
}
for (int i = 0; i < length; i++) {
StringBuilder result = new StringBuilder();
if (length == 3 && i == 1) {
result.append("3 2 1");
} else {
for (int j = 0; j < length; j++) {
result.append(sequence[(j + i) % length]).append(" ");
}
}
output.println(result);
}
}
output.flush();
}
private static class BufferedScanner {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public BufferedScanner() {
reader = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public String nextLine() throws IOException {
tokenizer = null;
return reader.readLine();
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
private static class BufferedOutput {
private final StringBuilder result;
public BufferedOutput() {
result = new StringBuilder();
}
public void print(Object object) {
result.append(object);
}
public void println(Object object) {
result.append(object).append("\n");
}
public void flush() {
System.out.println(result);
System.out.flush();
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | b9065cc7550c466ad7a77c4e3e0dd114 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /*input
4
4
3
5
6
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out;
static int MOD = 1000000007;
static FastReader scan;
/*-------- I/O usaing short named function ---------*/
public static String ns(){return scan.next();}
public static int ni(){return scan.nextInt();}
public static long nl(){return scan.nextLong();}
public static double nd(){return scan.nextDouble();}
public static String nln(){return scan.nextLine();}
public static void p(Object o){out.print(o);}
public static void ps(Object o){out.print(o + " ");}
public static void pn(Object o){out.println(o);}
/*-------- for output of an array ---------------------*/
static void iPA(int arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void lPA(long arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void sPA(String arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void dPA(double arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
/*-------------- for input in an array ---------------------*/
static void iIA(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void lIA(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void sIA(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void dIA(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*------------ for taking input faster ----------------*/
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;
}
}
// Method to check if x is power of 2
static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);}
//Method to return gcd of two numbers
static int gcd(int a, int b){return a==0?b:gcd(b % a, a); }
//Method to count digit of a number
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
//Method for sorting
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);
}
//Method for sorting
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);
}
//Method foAr checking if a number is prime or not
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 void main (String[] args) throws java.lang.Exception
{
out =new PrintWriter(System.out);
scan =new FastReader();
//for fast output sometimes
StringBuilder sb = new StringBuilder();
int t = ni();
while(t-->0){
int n = ni();
int arr[] = new int[n];
int count = n;
for(int i=0; i<n; i++){
arr[i] = count--;
}
//iPA(arr);
int idx = n-1;
for(int k=0; k<n; k++){
swap(arr, n-1, idx);
if(arr[n-1] == arr[n-2] + arr[n-3]){
int res[] = new int[n];
for(int i=0; i<n; i++){
res[i] = n -i;
}
if(arr[n-1] == 5){
res[n-1] = 1;
res[n-2] = 3;
res[n-3] = 2;
}else{
res[n-1] = 2;
res[n-2] = 3;
res[n-3] = 1;
}
iPA(res);
}else{
iPA(arr);
}
swap(arr, n-1, idx);
idx--;
}
}
out.flush();
out.close();
}
public static void swap(int arr[], int l, int r){
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | a65afd098c9020885652efa0fe5a7210 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import org.w3c.dom.NamedNodeMap;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.*;
import java.util.Collections;
public class div {
static PrintWriter output = new PrintWriter(System.out);
public static void main(String[] args) throws java.lang.Exception {
div.FastReader sc =new FastReader();
int t = sc.nextInt();
for (int i =0;i<t;i++) {
int n = sc.nextInt(); int arr[]= new int [n+1]; int r =1; int rr=0;
for (int e=n;e>0;e--) {
arr[(n-e)+1]=e;
}
for(int e =0;e<n;e++) {
for(int ee=1;ee<=n;ee++) {
System.out.print(arr[ee]+" ");
}
int temp=arr[n-rr];
arr[n-rr]=arr[n-r];
arr[n-r]=temp;
r++;rr++;
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());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(long[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
//Arrays.sort(arr, Comparator.comparingDouble(o -> o[0]));
// q.replace(i, i+1, "4");
// StringBuilder q = new StringBuilder(w);
//List arr1 = new ArrayList();
/*
int arr3[]= new int [3];
long count=0;
arr3[0]= sc.nextInt(); int r =arr3[0];
arr3[1]= sc.nextInt(); int g=arr3[1];
arr3[2]= sc.nextInt(); int b = arr3[2];
int arr[][]= new int [arr3[0]][2];
int arr1[][]= new int [arr3[1]][2];
int arr2[][]= new int [arr3[2]][2];
for(int i =0;i<arr3[0];i++) {
arr[i][0]=sc.nextInt();
arr[i][1]=i;
}
for(int i =0;i<arr3[1];i++) {
arr1[i][0]=sc.nextInt();
arr1[i][1]=i;
}
for(int i =0;i<arr3[2];i++) {
arr2[i][0]=sc.nextInt();
arr2[i][1]=i;
}
Arrays.sort(arr3);
Arrays.sort(arr, Comparator.comparingDouble(o -> o[0]));
Arrays.sort(arr1, Comparator.comparingDouble(o -> o[0]));
Arrays.sort(arr2, Comparator.comparingDouble(o -> o[0]));
int arr4[][]= new int [3][2];
int y = arr3[0]+arr3[1];
System.out.println(y);
y=y*2;
if(y%2!=0) {
y++;
}
while (y>=-1) {
arr4[0][0]= arr[r-1][0]; arr4[0][1]=1;
arr4[1][0]=arr1[g-1][0]; arr4[1][1]=2;
arr4[2][0]=arr2[b-1][0]; arr4[2][1]=3;
Arrays.sort(arr4, Comparator.comparingDouble(o -> o[0]));
System.out.println(arr4[2][0]+" "+arr4[1][0]+" "+arr4[0][0]);
System.out.println(arr4[2][1]+" "+arr4[1][1]+" "+arr4[0][1]);
count+=(arr4[2][0]*arr4[1][0]);
if(arr4[2][1]==1||arr4[1][1]==1) {
arr[r-1][0]=0;
y--;
}
if(arr4[2][1]==2||arr4[1][1]==2) {
arr1[g-1][0]=0;
y--;
}
if(arr4[2][1]==3||arr4[1][1]==3) {
arr2[b-1][0]=0;
y--;
}
Arrays.sort(arr, Comparator.comparingDouble(o -> o[0]));
Arrays.sort(arr1, Comparator.comparingDouble(o -> o[0]));
Arrays.sort(arr2, Comparator.comparingDouble(o -> o[0]));
//System.out.println("m");
}
System.out.println(count);
System.out.println(Arrays.deepToString(arr));
System.out.println(Arrays.deepToString(arr1));
System.out.println(Arrays.deepToString(arr2));
}
*/
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | d6de4194ceedeb3062a1be16e5b12493 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author saikat021
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int T = in.nextInt();
while (T-- > 0) {
int n = in.nextInt();
int[] arr = new int[n];
for (int i = n - 1; i >= 0; i--) {
arr[i] = n - i;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n; j++) {
out.print(arr[j] + " ");
}
out.println();
swap(arr, n - i - 1, n - i - 2);
}
for (int j = 0; j < n; j++) {
out.print(arr[j] + " ");
}
out.println();
}
}
private void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
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() {
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private 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 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 interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | b8b7340843a49072a5746c219feeab62 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Practice {
static boolean multipleTC = true;
final static int mod = 1000000007;
final static int mod2 = 998244353;
final double E = 2.7182818284590452354;
final double PI = 3.14159265358979323846;
int MAX = 10000005;
void pre() throws Exception {
}
// All the best
void solve(int TC) throws Exception {
int n = ni();
int arr[] = new int[n];
int counter = n;
for (int i = 0; i < n; i++) {
arr[i] = counter--;
}
if (n == 3) {
pn("3 2 1");
pn("1 3 2");
pn("3 1 2");
return;
}
for (int i = 0; i < n; i++) {
StringBuilder ans = new StringBuilder();
for (int k = 0; k < n; k++) {
ans.append(arr[k] + " ");
}
pn(ans);
leftRotatebyOne(arr, n);
}
}
void leftRotatebyOne(int arr[], int n) {
int temp = arr[0], i;
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n - 1] = temp;
}
boolean isFibo(int arr[]) {
int n = arr.length;
for (int i = 0; i + 2 < n; i++) {
if (arr[i] + arr[i + 1] == arr[i + 2]) {
return true;
}
}
return false;
}
public void permute(int[] arr) {
permuteHelper(arr, 0);
}
ArrayList<int[]> perm = new ArrayList<>();
private void permuteHelper(int[] arr, int index) {
if (index >= arr.length - 1) {
perm.add(arr.clone());
return;
}
if (perm.size() > 3 * arr.length) {
return;
}
for (int i = index; i < arr.length; i++) {
int t = arr[index];
arr[index] = arr[i];
arr[i] = t;
permuteHelper(arr, index + 1);
t = arr[index];
arr[index] = arr[i];
arr[i] = t;
}
}
int[] readArr(int n) throws Exception {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
void sort(int arr[], int left, int right) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = left; i <= right; i++)
arr[i] = list.get(i - left);
}
void sort(int arr[]) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < arr.length; i++)
arr[i] = list.get(i);
}
public long max(long... arr) {
long max = arr[0];
for (long itr : arr)
max = Math.max(max, itr);
return max;
}
public int max(int... arr) {
int max = arr[0];
for (int itr : arr)
max = Math.max(max, itr);
return max;
}
public long min(long... arr) {
long min = arr[0];
for (long itr : arr)
min = Math.min(min, itr);
return min;
}
public int min(int... arr) {
int min = arr[0];
for (int itr : arr)
min = Math.min(min, itr);
return min;
}
public long sum(long... arr) {
long sum = 0;
for (long itr : arr)
sum += itr;
return sum;
}
public long sum(int... arr) {
long sum = 0;
for (int itr : arr)
sum += itr;
return sum;
}
String bin(long n) {
return Long.toBinaryString(n);
}
String bin(int n) {
return Integer.toBinaryString(n);
}
static int bitCount(int x) {
return x == 0 ? 0 : (1 + bitCount(x & (x - 1)));
}
static void dbg(Object... o) {
System.err.println(Arrays.deepToString(o));
}
int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
int abs(int a) {
return (a < 0) ? -a : a;
}
long abs(long a) {
return (a < 0) ? -a : a;
}
void p(Object o) {
out.print(o);
}
void pn(Object o) {
out.println(o);
}
void pni(Object o) {
out.println(o);
out.flush();
}
String n() throws Exception {
return in.next();
}
String nln() throws Exception {
return in.nextLine();
}
int ni() throws Exception {
return Integer.parseInt(in.next());
}
long nl() throws Exception {
return Long.parseLong(in.next());
}
double nd() throws Exception {
return Double.parseDouble(in.next());
}
public static void main(String[] args) throws Exception {
new Practice().run();
}
FastReader in;
PrintWriter out;
void run() throws Exception {
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC) ? ni() : 1;
pre();
for (int t = 1; t <= T; t++)
solve(t);
out.flush();
out.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception {
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 8a99726d3c9e0111e0e430f4689253c6 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] code_7) {
Scanner scanner = new Scanner(System.in);
StringBuilder str = new StringBuilder();
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
if (n == 3) {
str.append("3 2 1\n");
str.append("1 3 2\n");
str.append("3 1 2\n");
} else {
int[] per = new int[n];
for (int i = 0; i < n; i++) {
per[i] = i + 1;
}
per[1] = 3;
per[2] = 2;
int[] arr = Arrays.copyOf(per,n);
for (int i = 0; i < n; i++) {
str.append(leftRotate(arr, i, n));
arr = Arrays.copyOf(per,n);
}
}
}
System.out.println(str);
}
public static StringBuilder leftRotate(int[] arr, int d, int n) {
d = d % n;
int i, j, k, temp;
int g_c_d = gcd(d, n);
for (i = 0; i < g_c_d; i++) {
temp = arr[i];
j = i;
while (true) {
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
return (printArray(arr, n));
}
public static StringBuilder printArray(int[] arr, int n) {
StringBuilder ans = new StringBuilder();
for (int i = 0; i < n; i++) {
ans.append(arr[i]).append(" ");
}
ans.append("\n");
return ans;
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 20a5d83f1367454801570b7de7e5b8ce | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
if(n == 3)
{
output.write("1 3 2\n");
output.write("3 2 1\n");
output.write("3 1 2\n");
continue;
}
int arr[] = new int[n];
arr[0] = 1;
arr[1] = 3;
arr[2] = 2;
for(int i = 3; i < n; i++)
arr[i] = i + 1;
for(int i = 0; i < n; i++)
{
for(int j : arr)
output.write(j + " ");
output.write("\n");
int z = arr[0];
for(int j = 0; j < n - 1; j++)
arr[j] = arr[j+1];
arr[n-1] = z;
}
// for(int i = 0; i < n; i++)
// {
// output.write(arr[n-1-i] + " ");
// }
// output.write("\n");
// st = new StringTokenizer(br.readLine());
// int arr[] = new int[n];
// for(int i = 0; i < n; i++)
// {
// int e = Integer.parseInt(st.nextToken());
// arr[i] = e;
// }
// int k = Integer.parseInt(st.nextToken());
// char arr[] = br.readLine().toCharArray();
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.flush();
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 610e040d619454f8cd7e4adf3508082e | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
public class B {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int N = Integer.parseInt(br.readLine().trim());
sb.append(solve(N));
}
br.close();
System.out.println(sb);
}
private static String solve(int N) {
int arr[] = new int[N];
for (int i = N; i > 0; i--)
arr[N - i] = i;
// Deque<Integer> dq = new LinkedList<>();
// for (int i = N; i > 0; i--)
// dq.addLast(i);
var sb = new StringBuilder();
for (int i : arr)
sb.append(i).append(" ");
sb.append("\n");
for (int i = N - 1; i > 0; i--) {
swap(arr, i - 1, i);
for (int x : arr)
sb.append(x).append(" ");
sb.append("\n");
// dq.addLast(dq.removeFirst());
}
return sb.toString();
}
private static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 314fc408d6f8e863b6ceea5845e702b6 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main
{
InputStream is;
PrintWriter out = new PrintWriter(System.out); ;
String INPUT = "";
void run() throws Exception
{
is = System.in;
solve();
out.flush();
out.close();
}
public static void main(String[] args) throws Exception { new Main().run(); }
public byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
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);
}
public int skip()
{
int b;
while((b = readByte()) != -1 && isSpaceChar(b));
return b;
}
public double nd()
{
return Double.parseDouble(ns());
}
public char nc()
{
return (char)skip();
}
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b)))
{
sb.appendCodePoint(b); b = readByte();
}
return sb.toString();
}
private int ni()
{
return (int)nl();
}
private long nl()
{
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();
}
}
class Pair
{
int first;
int second;
Pair(int a, int b)
{
first = a;
second = b;
}
}
int[] na(int n)
{
int[] arr = new int[n];
for(int i=0; i<n; i++) arr[i]=ni();
return arr;
}
long[] nal(int n)
{
long[] arr = new long[n];
for(int i=0; i<n; i++) arr[i]=nl();
return arr;
}
void solve()
{
int t = ni();
while(t-- > 0)
{
int n = ni();
if(n == 3)
{
out.println(3+" "+1+" "+2);
out.println(3+" "+2+" "+1);
out.println(1+" "+3+" "+2);
continue;
}
int[] res = new int[n];
for(int i=0; i<n; i++) res[i]=i+1;
for(int i=2; i<n; i++)
{
if(res[i-2]+res[i-1] == res[i])
{
int temp = res[i-1];
res[i-1] = res[i];
res[i] = temp;
}
}
for(int i=1; i<=n; i++)
{
for(int j=0; j<n; j++)
{
out.print(res[j]+" ");
}
res = shift(res);
out.println();
}
}
}
int[] shift(int[] arr)
{
int n = arr.length;
int p = arr[0];
for(int i=1; i<n; i++)
{
arr[i-1] = arr[i];
}
arr[n-1] = p;
return arr;
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 5163a5663685b77641ae1c4f74bc06f4 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class codeforces_Edu123_B {
private static void solve(FastIOAdapter in, PrintWriter out) {
int n = in.nextInt();
var list = new ArrayList<Integer>();
for (int i = n; i > 0; i--) {
list.add(i);
}
for (int i = 0; i < n; i++) {
int f = 0, s = 0;
if (i > 0) {
f = list.get(i - 1);
s = list.get(i);
list.set(i - 1, s);
list.set(i, f);
}
for (Integer num : list) {
out.print(num + " ");
}
if (i > 0) {
list.set(i - 1, f);
list.set(i, s);
}
out.println();
}
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter, ioAdapter.out);
}
}
}
static void ruffleSort(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;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
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[] readArrayLong(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());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.close();
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 19d68bfac28e4673b82fd65711a16e49 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | //package com.company;
import java.io.*;
import java.util.*;
public class Main{
static boolean[] primecheck = new boolean[1000002];
static ArrayList<Integer>[] adj;
static int[] vis;
static int mod = (int)1e9 + 7;
public static void main(String[] args) {
OutputStream outputStream = System.out;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t = 1;
t = in.ni();
for (int i = 0; i < t; i++) {
//out.print("Case #" + (i+1) + ": ");
solver.solve(in, out);
}
out.close();
}
static class PROBLEM {
public void solve(FastReader in, PrintWriter out) {
int n = in.ni();
int start = n, cnt = 0;
while(start > 1){
if(n == 3 && start == 2){
start--;
continue;
}
int i = 0;
int k = start;
while(i < n){
i++;
out.print(k + " ");
k--;
if(k == 0) k = n;
}
out.println();
start--;
}
int[] a = new int[n];
int p = 1;
for (int i = n-1; i >= 0; i--) {
a[i] = p++;
}
a[n-1] = a[n-2];
a[n-2] = 1;
pa(a, out);
if(n == 3) out.println("2 3 1");
}
}
static void pa(int[] a, PrintWriter out){
for (int j : a) {
out.print(j + " ");
}
out.println();
}
static void pa(long[] a, PrintWriter out){
for (long j : a) {
out.print(j + " ");
}
out.println();
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static boolean isPoT(int n){
double p = Math.log((double)n)/Math.log(2D);
return (p == (int)p);
}
static void dfs(int i){
vis[i] = 1;
for(int j: adj[i]){
if(vis[j] == 0) {
dfs(j);
}
}
}
static long sigmaK(long k){
return (k*(k+1))/2;
}
static void swap(int[] a, int l, int r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
static int binarySearch(int[] a, int l, int r, int x){
if(r>=l){
int mid = l + (r-l)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) return binarySearch(a, l, mid-1, x);
else return binarySearch(a,mid+1, r, x);
}
return -1;
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long lcm(long a, long b){
return (a / gcd(a, b)) * b;
}
static int ceil(int a, int b){
return (a+b-1)/b;
}
static long ceil(long a, long b){
return (a+b-1)/b;
}
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 fast_pow(long a, long b) { //Jeel bhai OP
if(b == 0)
return 1L;
long val = fast_pow(a, b / 2);
if(b % 2 == 0)
return val * val % mod;
else
return val * val % mod * a % mod;
}
static int exponentMod(int A, int B, int C) {
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (int) ((y + C) % C);
}
// 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 o){
//
// int ans = Integer.compare(x, o.x);
// if(o.x == x) ans = Integer.compare(y, o.y);
//
// return ans;
//
//// int ans = Integer.compare(y, o.y);
//// if(o.y == y) ans = Integer.compare(x, o.x);
////
//// return ans;
// }
// }
static class Tuple implements Comparable<Tuple>{
int x, y, id;
Tuple(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
public int compareTo(Tuple o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public int compareToY(Pair<U, V> b) {
int cmpU = y.compareTo(b.y);
return cmpU != 0 ? cmpU : x.compareTo(b.x);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
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 ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
char nc() {
return next().charAt(0);
}
boolean nb() {
return !(ni() == 0);
}
// boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nline() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] ra(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = ni();
return array;
}
}
private static int[] mergeSort(int[] array) {
//array.length replaced with ctr
int ctr = array.length;
if (ctr <= 1) {
return array;
}
int midpoint = ctr / 2;
int[] left = new int[midpoint];
int[] right;
if (ctr % 2 == 0) {
right = new int[midpoint];
} else {
right = new int[midpoint + 1];
}
for (int i = 0; i < left.length; i++) {
left[i] = array[i];
}
for (int i = 0; i < right.length; i++) {
right[i] = array[i + midpoint];
}
left = mergeSort(left);
right = mergeSort(right);
int[] result = merge(left, right);
return result;
}
private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length || rightPointer < right.length) {
if (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
} else if (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
return result;
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 46109bdfeec908969ef9a2b7c0abdfd9 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
// Created by @thesupremeone on 22/02/22
public class B {
HashSet<String> set = new HashSet<>();
void explore(int i, int n, ArrayList<Integer> list, boolean[] used){
if(i==n){
StringBuilder builder = new StringBuilder();
for (int j = 0; j < list.size(); j++) {
int ai = list.get(j);
if(j!=0) builder.append(":");
builder.append(ai);
}
set.add(builder.toString());
return;
}
for (int j = 1; j <= n; j++) {
if(used[j]) continue;
if(i>1){
int last = list.get(i-1);
int secondLast = list.get(i-2);
if(last+secondLast==j){
continue;
}
}
int index = list.size();
list.add(j);
used[j] = true;
explore(i+1, n, list, used);
list.remove(index);
used[j] = false;
if(set.size()==n) return;
}
}
void solve() {
int ts = getInt();
for (int t = 1; t <= ts; t++) {
int n = getInt();
set.clear();
ArrayList<Integer> list = new ArrayList<>();
boolean[] used = new boolean[n+1];
explore(0, n, list, used);
for(String s : set){
String[] parts = s.split(":");
for(String p : parts){
print(p);
print(" ");
}
println("");
}
}
}
public static void main(String[] args) throws Exception {
if (isOnlineJudge()) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
new B().solve();
out.flush();
} else {
localJudge = new Thread();
in = new BufferedReader(new FileReader("input.txt"));
out = new BufferedWriter(new FileWriter("output.txt"));
localJudge.start();
new B().solve();
out.flush();
localJudge.suspend();
}
}
static boolean isOnlineJudge() {
try {
return System.getProperty("ONLINE_JUDGE") != null
|| System.getProperty("LOCAL") == null;
} catch (Exception e) {
return true;
}
}
// Fast Input & Output
static Thread localJudge = null;
static BufferedReader in;
static StringTokenizer st;
static BufferedWriter out;
static String getLine() {
try {
return in.readLine();
} catch (Exception ignored) {
return "";
}
}
static String getToken() {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(getLine());
return st.nextToken();
}
static int getInt() {
return Integer.parseInt(getToken());
}
static long getLong() {
return Long.parseLong(getToken());
}
static void print(Object s) {
try {
out.write(String.valueOf(s));
} catch (Exception ignored) {
}
}
static void println(Object s) {
try {
out.write(String.valueOf(s));
out.newLine();
} catch (Exception ignored) {
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 12163499610bf0a43698155c1bcb48de | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.*;
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
int x;
int y;
// Constructor
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
// @Override
// public int hashCode() {
// return this.x ^ this.y;
// }
//
// @Override
// public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
// Pair other = (Pair) obj;
// if (x != other.x)
// return false;
// if (y != other.y)
// return false;
// return true;
// }
}
static class Trip {
long a;
long b;
long c;
// Constructor
public Trip(long a, long b, long c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
static class Quad {
String a;
String b;
String c;
String d;
// Constructor
public Quad(String a, String b, String c, String d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
public static void swap(int i, int[] arr) {
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
static boolean isPrime(long n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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 long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int factorial(int n)
{
// single line to find factorial
return (n == 1 || n == 0) ? 1 : (n * factorial(n - 1))/1000000007;
}
// Function to convert decimal to fraction
public static void SubString(String str, int n, HashSet<String> hs)
{
for (int i = 0; i < n; i++)
for (int j = i+1; j <= n; j++)
// Please refer below article for details
// of substr in Java
// https://www.geeksforgeeks.org/java-lang-string-substring-java/
hs.add(str.substring(i, j));
}
static class TrieNode{
TrieNode children[];
boolean isEnd;
TrieNode(){
this.children = new TrieNode[26];
this.isEnd = false;
}
}
static void subStrings(String s,HashSet<String> hs)
{
// To store distinct output subStrings
HashSet<String> us = new HashSet<String>();
// Traverse through the given String and
// one by one generate subStrings beginning
// from s[i].
for (int i = 0; i < s.length(); ++i)
{
// One by one generate subStrings ending
// with s[j]
String ss = "";
for (int j = i; j < s.length(); ++j)
{
ss = ss + s.charAt(j);
us.add(ss);
}
}
// Print all subStrings one by one
for (String str : us) {
hs.add(str);
}
}
static boolean isSubSequence(String str1, String str2,
int m, int n)
{
int j = 0;
// Traverse str2 and str1, and compare
// current character of str2 with first
// unmatched char of str1, if matched
// then move ahead in str1
for (int i = 0; i < n && j < m; i++)
if (str1.charAt(j) == str2.charAt(i))
j++;
// If all characters of str1 were found
// in str2
return (j == m);
}
static int onesComplement(int n)
{
// Find number of bits in the
// given integer
int number_of_bits =
(int)(Math.floor(Math.log(n) /
Math.log(2))) + 1;
// XOR the given integer with poe(2,
// number_of_bits-1 and print the result
return ((1 << number_of_bits) - 1) ^ n;
}
static int binarySearch(HashSet<Integer> hs,HashMap<Integer,Integer> hm, int l, int r, int a, int b)
{
if(l==r) {
//System.out.println(l);
if(hs.contains(l)) return b;
else return a;
}
if (r > l) {
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
int cnt=hm.get(r)-hm.get(l-1);
if(cnt==0) return a;
else {
int x = binarySearch(hs,hm, mid + 1, r, a, b)+binarySearch(hs,hm, l, mid, a, b);
int y = cnt*(r-l+1)*b;
return Math.min(x, y);
}
}
// We reach here when element is not present
// in array
return -1;
}
public static boolean[] sieveOfEratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean prime[] = new boolean[n+1];
for(int i=0;i<=n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
return prime;
}
static long x, y;
static long gcd_extend(long a, long b)
{
// Base Case
if (b == 0)
{
x = 1;
y = 0;
return a;
}
// Recursively find the gcd
else
{
long g = gcd_extend(b, a % b);
long x1 = x, y1 = y;
x = y1;
y = x1 - ((a / b)%1000000000000000000L * y1%1000000000000000000L)%1000000000000000000L;
return g;
}
}
public static void main(String[] args) throws IOException{
BufferedWriter output = 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]=n-i;
System.out.print(arr[i]+" ");
}
System.out.println();
for(int i=n-2;i>=0;i--) {
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
for(int j=0;j<n;j++) {
System.out.print(arr[j]+" ");
}
System.out.println();
}
}
}
//output.flush();
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 24177582fe45b3603d7703e80c10b2b1 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Main{
static final int MOD = (int) 1e9 + 7;
public static void main (String[] args){
FastReader s = new FastReader();
int t=1;t=s.ni();
for(int test=1;test<=t;test++){
int n=s.ni();long ans=0,sum=0;boolean good=true;
long a[]=new long[n];
for (int i = 0; i < n; i++) {
a[i] = n - i;
}
for (int i = 0; i < n; i++) {
long temp = a[0];
a[0] = a[i];
a[i] = temp;
pArray(a);
System.out.println();
// temp = a[0];
// a[0] = a[i];
// a[i] = temp;
}
// System.out.println();
}
}
static class FastReader {
BufferedReader br; StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nl();
return a;
}
String next(){
while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());}
catch (IOException e){e.printStackTrace();}}return st.nextToken();}
String nextLine(){String str = "";try {str = br.readLine();}catch (IOException e)
{e.printStackTrace();}return str;}
}
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 void pArray(long[] a){
int n=a.length;
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
static long sum(long[] a){
int n=a.length;long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static boolean prime(long sq){
if(BigInteger.valueOf(sq).isProbablePrime(12))
return true;
return false;
}
}
//Collections.sort(a, Collections.reverseOrder()); | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | a033fb2650dd610b9ae2ad8f9e59dd08 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.Scanner;
public class CF2908{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testCases = scan.nextInt();
while(testCases>0){
int number = scan.nextInt();
if(number==3){
System.out.println("3 2 1");
System.out.println("1 3 2");
System.out.println("2 3 1");
}else{
int currentNumber=number*2;
for(int i=0; i<number; i++){
for(int j=1; j<=number; j++){
System.out.print(((currentNumber-j)%number+1) +" ");
}
System.out.println();
currentNumber--;
}
}
testCases--;
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | cef62381de76d6614aeb8ba5e0cebbc5 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | //<———My cp————
//https://takeuforward.org/interview-experience/strivers-cp-sheet/?utm_source=youtube&utm_medium=striver&utm_campaign=yt_video
import java.util.*;
import java.io.*;
public class Solution{
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws Exception{
FastReader fr = new FastReader(System.in);
int t = fr.nextInt();
while(t-->0){
int n = fr.nextInt();
for(int i = 1;i<=n;i++){
pw.print(i+" ");
for(int k=n;k>0;k--){
if(k!=i){
pw.print(k+" ");
}
}
pw.println("");
}
}
pw.close();
}
static int lowerBound(int left,int right,int[] vals,int value){
int mid = (left+right)/2;
while(left<right){
mid = (left+right)/2;
if(vals[mid]<value){
left = mid+1;
}else{
right = mid;
}
}
return left;
}
static int upperBound(int left,int right,int[] vals,int value){
while(left<right){
int mid = (left+right+1)/2;
if(vals[mid]<=value){
left=mid;
}else{
right=mid-1;
}
}
return left;
}
static class Pair{
int vals;
int index;
public Pair(int index,int val){
this.vals = val;
this.index = index;
}
}
static int isPerfectSquare(int vals){
int lastPow=1;
while(lastPow*lastPow<vals){
lastPow++;
}
if(lastPow*lastPow==vals){
return lastPow*lastPow;
}else{
return -1;
}
}
public static int[] sort(int[] vals){
ArrayList<Integer> values = new ArrayList<>();
for(int i = 0;i<vals.length;i++){
values.add(vals[i]);
}
Collections.sort(values);
for(int i =0;i<values.size();i++){
vals[i] = values.get(i);
}
return vals;
}
public static long[] sort(long[] vals){
ArrayList<Long> values = new ArrayList<>();
for(int i = 0;i<vals.length;i++){
values.add(vals[i]);
}
Collections.sort(values);
for(int i =0;i<values.size();i++){
vals[i] = values.get(i);
}
return vals;
}
public static void reverseArray(long[] vals){
int startIndex = 0;
int endIndex = vals.length-1;
while(startIndex<=endIndex){
long temp = vals[startIndex];
vals[startIndex] = vals[endIndex];
vals[endIndex] = temp;
startIndex++;
endIndex--;
}
}
public static void reverseArray(int[] vals){
int startIndex = 0;
int endIndex = vals.length-1;
while(startIndex<=endIndex){
int temp = vals[startIndex];
vals[startIndex] = vals[endIndex];
vals[endIndex] = temp;
startIndex++;
endIndex--;
}
}
static class FastReader{
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}
public static int GCD(int numA, int numB){
if(numA==0){
return numB;
}else if(numB==0){
return numA;
}else{
if(numA>numB){
return GCD(numA%numB,numB);
}else{
return GCD(numA,numB%numA);
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 2ff773faf906b139b2f380e8ff03455c | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class AntiFibo {
public static void dfs(boolean [] visit, int k, List<Integer> ans, int [] n) {
if(ans.size() == k && n[0] > 0) {
for(int i : ans) {
System.out.print(i + " ");
}
System.out.println();
n[0]--;
return;
}
for(int i=1;i<=k && n[0] > 0;i++) {
if(visit[i] || ans.size() > 1 && ans.get(ans.size()-1)+ans.get(ans.size()-2) == i) continue;
ans.add(i);
visit[i] = true;
dfs(visit,k,ans,n);
visit[i] = false;
ans.remove(ans.size()-1);
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0) {
int n = s.nextInt();
dfs(new boolean[n+1],n,new ArrayList<>(), new int[] {n});
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | b305dfdcb084a675d5df36aaedf1b287 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class AntiFibo {
public static void dfs(boolean [] visit, int k, List<Integer> ans, int [] n) {
if(ans.size() == k && n[0] > 0) {
for(int i : ans) {
System.out.print(i + " ");
}
System.out.println();
n[0]--;
return;
}
for(int i=1;i<=k && n[0] > 0;i++) {
if(visit[i] || ans.size() > 1 && ans.get(ans.size()-1)+ans.get(ans.size()-2) == i) continue;
ans.add(i);
visit[i] = true;
dfs(visit,k,ans,n);
visit[i] = false;
ans.remove(ans.size()-1);
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0) {
int n = s.nextInt();
dfs(new boolean[n+1],n,new ArrayList<>(), new int[] {n});
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 25de2720c85d78000a2eca8f172913bd | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Q1644B {
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
ArrayList<ArrayList<Integer>>al=helper(n);
for(int i=0;i<al.size();i++){
ArrayList<Integer>sm=al.get(i);
for(int val:sm){
System.out.print(val+" ");
}
System.out.println();
}
}
public static ArrayList<ArrayList<Integer>> helper(int n) {
if (n == 2) {
ArrayList<Integer> al1 = new ArrayList<>();
al1.add(1);
al1.add(2);
ArrayList<Integer> al2 = new ArrayList<>();
al2.add(2);
al2.add(1);
ArrayList<ArrayList<Integer>> al = new ArrayList<>();
al.add(al1);
al.add(al2);
return al;
}
ArrayList<ArrayList<Integer>>al=helper(n-1);
ArrayList<Integer>after=new ArrayList<>(al.get(0));
for(int i=0;i<al.size();i++){
ArrayList<Integer>smaller=al.get(i);
smaller.add(0,n);
al.set(i, smaller);
}
after.add(1,n);
al.add(after);
// System.out.println(al);
return al;
}
public static void main(String[] args) {
int test = i();
while (test-- > 0) {
solve();
}
}
// -----> POWER ---> long power(long x, long y) <---- power
// -----> LCM ---> long lcm(long x, long y) <---- lcm
// -----> GCD ---> long gcd(long x, long y) <---- gcd
// -----> NCR ---> long ncr(int n, int r) <---- ncr
// -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<--
// -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<-
// tempstart
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static InputReader in = new InputReader(System.in);
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | b8704520aba1f7ec97aeca3506a41b69 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
public class I2 {
private static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
FastReader in = new FastReader();
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt();
for (int i = 1; i <= n; i++) {
int [] arr = new int [n];
arr[0] = i;
int ind = 1;
for (int j = n; j >= 1; j--) {
if (j != i) {
arr[ind] = j;
ind++;
}
}
for (int j = 0; j < n; j++) {
System.out.print(arr[j]+" ");
}
System.out.println();
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 1dbcd8f6310c5dc06af2c2b578b1c901 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class B_Anti_Fibonacci_Permutation {
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = f.nextInt();
while(t-- > 0){
int n = f.nextInt();
if(n == 3) {
out.println(3 + " " + 2 + " " + 1);
out.println(2 + " " + 3 + " " + 1);
out.println(3 + " " + 1 + " " + 2);
continue;
}
for(int i = 0; i < n; i++) {
for(int j = n-i; j >= 1; j--) {
out.print(j + " ");
}
for(int j = n; j > n-i; j--) {
out.print(j + " ");
}
out.println();
}
}
out.close();
}
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
public static int Gcd(int a, int b) {
int dividend = a > b ? a : b;
int divisor = a < b ? a : b;
while(divisor > 0) {
int reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
public static int lcm1(int a, int b) {
int lcm = Gcd(a, b);
int hcf = (a * b) / lcm;
return hcf;
}
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());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | cc7e88e35415854c99dda9fd3319f3f7 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class go {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int x= sc.nextInt();
go:
while (x-- !=0 ) {
int num=sc.nextInt();
Integer[] arr= new Integer[num];
for(int i=1 ;i<=num; i++){
arr[i-1]=i;
}
Arrays.sort(arr, Collections.reverseOrder());
for (int el: arr){
System.out.print(el+" ");
}
for(int i=1; i<num; i++){
int temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
for (int el: arr){
System.out.print(el+" ");
}
System.out.println();
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 6b01910ac25b161a053e12c6f62d4cdd | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class practice {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int t = scan.nextInt();
while (t --> 0) {
int n = scan.nextInt();
for (int i = 1; i <= n - 1; i++) {
sb.append(i + " " + n + " ");
for (int j = n - 1; j > 0; j--) {
if (i == j) continue;
else sb.append(j + " ");
}
sb.append("\n");
}
for (int i = n; i > 0; i--) sb.append(i + " ");
sb.append("\n");
}
System.out.println(sb.toString().trim());
scan.close();
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 1fcadce74ca1b629e1f266d681bd8ac1 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.Math.PI;
import static java.lang.Math.min;
import static java.lang.System.arraycopy;
import static java.lang.System.exit;
import static java.util.Arrays.copyOf;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Comparator;
import java.lang.StringBuilder;
import java.util.Collections;
import java.util.*;
import java.text.DecimalFormat;
public class Solution {
static class Edge{
int u, v, w;
Edge(int u, int v, int w){
this.u = u;
this.v = v;
this.w = w;
}
}
static class Pair{
int first, second;
Pair(int first, int second){
this.first = first;
this.second = second;
}
}
static class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static int dx[] = {-1,0,1,0};
static int dy[] = {0,-1,0,1};
private static void solve() throws IOException{
int n = scanInt();
if(n<4){
out.println("3 2 1\n1 3 2\n3 1 2");
return;
}
int arr[] = new int[n];
for(int i =0; i<n; ++i){
arr[i] = n-i;
}
for(int h = 0; h<n; ++h){
for(int i =0; i<n; ++i){
out.print(arr[i]+" ");
}
out.println();
int temp = arr[0];
for(int i = 1; i<n; ++i){
arr[i-1] = arr[i];
}
arr[n-1] = temp;
}
}
private static int[] inputArray(int n) throws IOException {
int arr[] = new int[n];
for(int i=0; i<n; ++i)
arr[i] = scanInt();
return arr;
}
public static void main(String[] args) {
try {
long startTime = System.currentTimeMillis();
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
int test=scanInt();
for(int t=1; t<=test; t++){
// out.print("Case #"+t+": ");
solve();
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
//out.println(totalTime+"---------- "+System.currentTimeMillis() );
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static double scanDouble() throws IOException {
return parseDouble(scanString());
}
static String scanString() throws IOException {
if (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static String scanLine() throws IOException {
return in.readLine();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 3f3a1ca7a15964d30a92eea94740ec60 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
// @author : Dinosparton
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
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 {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
if(n==3) {
res.append("3 2 1"+"\n");
res.append("2 3 1"+"\n");
res.append("1 3 2"+"\n");
continue;
}
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = n-i;
}
while(n-->0) {
for(int i : a) {
res.append(i+" ");
}
res.append("\n");
int l = a.length;
int temp = a[l-1];
for(int i = l-1;i>0;i--) {
a[i] = a[i-1];
}
a[0] = temp;
}
}
System.out.println(res);
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | d20704ba4aa1e284324e04becc7a9d13 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces {
final static int mod = 1000000007;
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
outer: while (t-- > 0) {
// while (t-- > 0) {
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
StringBuilder sb = new StringBuilder();
sb.append(i + " ");
for (int j = n; j >= 1; j--) {
if (j == i)
continue;
sb.append(j + " ");
}
System.out.println(sb.toString());
}
}
}
public static boolean isSorted(int[] nums, int n) {
for (int i = 1; i < n; i++) {
if (nums[i] < nums[i - 1])
return false;
}
return true;
}
public static boolean isPalindrome(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;
}
public static void sortByColumn(int arr[][], int col) {
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
public static void backtrack(String[] letters, int index, String digits, StringBuilder build, List<String> result) {
if (build.length() >= digits.length()) {
result.add(build.toString());
return;
}
char[] key = letters[digits.charAt(index) - '2'].toCharArray();
for (int j = 0; j < key.length; j++) {
build.append(key[j]);
backtrack(letters, index + 1, digits, build, result);
build.deleteCharAt(build.length() - 1);
}
}
public static String get(String s, int k) {
int n = s.length();
int rep = k % n == 0 ? k / n : k / n + 1;
s = s.repeat(rep);
return s.substring(0, k);
}
public static int diglen(Long y) {
int a = 0;
while (y != 0L) {
y /= 10;
a++;
}
return a;
}
static class FastReader {
BufferedReader br;
StringTokenizer st; // StringTokenizer() is used to read long strings
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 class Pair implements Comparable<Pair> {
public final int index;
public final int value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair other) {
// multiplied to -1 as the author need descending sort order
return -1 * Integer.valueOf(this.value).compareTo(other.value);
}
}
static String reverseString(String str) {
StringBuilder input = new StringBuilder();
return input.append(str).reverse().toString();
}
static long factorial(int n, int b) {
if (n == b)
return 1;
return n * factorial(n - 1, b);
}
static int lcm(int ch, int b) {
return ch * b / gcd(ch, b);
}
static int gcd(int ch, int b) {
return b == 0 ? ch : gcd(b, ch % b);
}
static double ceil(double n, double k) {
return Math.ceil(n / k);
}
static int sqrt(double n) {
return (int) Math.sqrt(n);
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | ad14f4ca8da3cd1921e790e7c93fb8c2 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static int n;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
sb = new StringBuilder();
long tc = Long.parseLong(br.readLine());
while(tc-->0){
String inp = br.readLine();
n = Integer.parseInt(inp);
for(int i = 1;i<=n;i++){
sb.append(i).append(" ");
for(int j =n;j>0;j--){
if(j!=i){
sb.append(j).append(" ");
}
}
sb.append("\n");
}
}
wr.println(sb);
wr.close();
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 5770fe75efddebc32338314116a31ceb | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static int n;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
sb = new StringBuilder();
long tc = Long.parseLong(br.readLine());
while(tc-->0){
String inp = br.readLine();
n = Integer.parseInt(inp);
int arr[] = new int[n];
boolean used[] = new boolean[n+1];
find(used,0,arr);
}
wr.println(sb);
wr.close();
}
private static boolean find(boolean used[],int idx,int arr[]){
if(idx==arr.length){
for(int i =0;i<arr.length;i++){
sb.append(arr[i]).append(" ");
}
sb.append("\n");
n--;
if(n==0) return true;
return false;
}
for(int i = 1;i<=arr.length;i++){
if(used[i]==false){
if(idx>=2){
if(i!=(arr[idx-1]+arr[idx-2])){
used[i] = true;
arr[idx] = i;
boolean check = find(used,idx+1,arr);
if(check) return true;
used[i] = false;
arr[idx] = 0;
}
}else{
used[i] = true;
arr[idx] = i;
boolean check = find(used,idx+1,arr);
if(check) return true;
used[i] = false;
arr[idx] = 0;
}
}
}
return false;
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | f485b22da059e08c5315425e33e8764d | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.Scanner;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cases = Integer.parseInt(scanner.nextLine());
while (cases > 0) {
cases--;
int n = Integer.parseInt(scanner.nextLine());
if (n > 3) {
LinkedList<Integer> list = new LinkedList<>();
for (int i = n; i > 0; i--) {
list.addLast(i);
}
for (int it = 0; it < n; it++) {
int popped = list.pop();
list.add(popped);
for (int i = 0; i < n; i++) {
if (i < n - 1) {
System.out.print(list.get(i) + " ");
} else {
System.out.print(list.get(i) + " \n");
}
}
}
} else {
System.out.println("3 2 1");
System.out.println("3 1 2");
System.out.println("2 3 1");
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 7dabec7dcd40b72398e6515d1b94801f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int arr[]=new int[n];
int c=0;
for(int i=n;i>0;i--){
System.out.print(i+" ");
arr[c]=i;
c++;
}
System.out.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++){
System.out.print(arr[j]+" ");
}
int temp1=arr[i-1];
arr[i-1]=arr[i];
arr[i]=temp1;
System.out.println();
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | cf312f0063d86ee99d62fd41b29f9b66 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class AntiFibonacciPermutation {
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 sc = new Reader();
// System.out.println("car");
int t = sc.nextInt();
for (int h = 0; h < t; h++) {
int n = sc.nextInt();
for(int i=n;i>=1;i--)
{
System.out.print(i+" ");
}
System.out.println();
int a[]= new int[n];
for(int i=n;i>=1;i--)
{
a[n-i]=i;
}
int temp=0;
for(int i=0;i<n-1;i++)
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
for(int j=0;j<n;j++)
{
System.out.print(a[j]+" ");
}
System.out.println();
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 20cd2d58806ebf88e5d803498b294ac2 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
public class Codeforces_1644B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
int n;
while((t--) > 0) {
n = Integer.parseInt(br.readLine());
for (int i = n; i > 0; i --) {
// System.out.format("%d ", i);
output.write(String.valueOf(i));
output.write(" ");
for (int j = n; j > 0; j--) {
if ( j == i) {
continue;
}
// System.out.format("%d ", j);
output.write(String.valueOf(j));
output.write(" ");
}
output.write("\n");
// System.out.format("%n");
}
}
output.flush();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 4094d6b504044e01b24684caabeb09cf | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.util.*;
public class Codeforces_1644B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
int n;
while((t--) > 0) {
n = Integer.parseInt(br.readLine());
for (int i = n; i > 0; i --) {
System.out.format("%d ", i);
for (int j = n; j > 0; j--) {
if ( j == i) {
continue;
}
System.out.format("%d ", j);
}
System.out.format("%n");
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | e657bf8793b45459146e1d9affc565ae | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class _practise {
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());
}
int[] ia(int n)
{
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=nextInt();
return a;
}
int[][] ia(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;
}
long[][] la(int n , int m)
{
long a[][]=new long[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextLong();
return a;
}
char[][] ca(int n , int m)
{
char a[][]=new char[n][m];
for(int i=0;i<n;i++)
{
String x =next();
for(int j=0;j<m ;j++) a[i][j]=x.charAt(j);
}
return a;
}
long[] la(int n)
{
long a[]=new long[n];
for(int i=0;i<n;i++)a[i]=nextLong();
return a;
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static void sort(long[] a)
{int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {long oi=r.nextInt(n), temp=a[i];a[i]=a[(int)oi];a[(int)oi]=temp;}Arrays.sort(a);}
static void sort(int[] a)
{int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);}
public static long sum(long a[])
{long sum=0; for(long i : a) sum+=i; return(sum);}
public static long count(long a[] , long x)
{long c=0; for(long i : a) if(i==x) c++; return(c);}
public static int sum(int a[])
{ int sum=0; for(int i : a) sum+=i; return(sum);}
public static int count(int a[] ,int x)
{int c=0; for(int i : a) if(i==x) c++; return(c);}
public static int count(String s ,char ch)
{int c=0; char x[] = s.toCharArray(); for(char i : x) if(ch==i) c++; return(c);}
public static boolean prime(int n)
{for(int i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static boolean prime(long n)
{for(long i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static int gcd(int n1, int n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static long gcd(long n1, long n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static int[] freq(int a[], int n)
{ int f[]=new int[n+1]; for(int i:a) f[i]++; return f;}
public 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;}
public static int[] rev(int a[])
{
for(int i=0 ; i<(a.length+1)/2;i++)
{
int temp=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=temp;
}
return a;
}
public static boolean palin(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
String str=String.valueOf(sb.reverse());
if(s.equals(str))
return true;
else return false;
}
public static String rev(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
return String.valueOf(sb.reverse());
}
public static void main(String args[])
{
FastReader in=new FastReader();
PrintWriter so = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
_practise ob = new _practise();
int T = in.nextInt();
// int T = 1;
tc : while(T-->0)
{
int n = in.nextInt() ;
if(n==3)
{
so.print("3 2 1\n 3 1 2\n 2 3 1\n");
continue;
}
for(int i=1 ; i<=n ; i++)
{
for(int j=i ; j>=1 ; j--) so.print(j+" ");
for(int j=n ; j>i ; j--) so.print(j+" ");
so.println();
}
}
so.flush();
/*String s = in.next();
* Arrays.stream(f).min().getAsInt()
* BigInteger f = new BigInteger("1"); 1 ke jagah koi bhi value ho skta jo aap
* initial value banan chahte
int a[] = new int[n];
Stack<Integer> stack = new Stack<Integer>();
Deque<Integer> q = new LinkedList<>(); or Deque<Integer> q = new ArrayDeque<Integer>();
PriorityQueue<Long> pq = new PriorityQueue<Long>();
ArrayList<Integer> al = new ArrayList<Integer>();
StringBuilder sb = new StringBuilder();
HashSet<Integer> st = new LinkedHashSet<Integer>();
Set<Integer> s = new HashSet<Integer>();
Map<Long,Integer> hm = new HashMap<Long, Integer>(); //<key,value>
for(Map.Entry<Integer, Integer> i :hm.entrySet())
for(int i : hm.values())
for(int i : hm.keySet())
HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
so.println("HELLO");
Arrays.sort(a,Comparator.comparingDouble(o->o[0]))
Arrays.sort(a, (aa, bb) -> Integer.compare(aa[1], bb[1]));
Set<String> ts = new TreeSet<>();*/
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 7d347ea29028eafefc55374b874e24fb | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void sort(int a[]){ // int -> long
ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
static int log(long n){
if(n<=1)return 0;
long temp = n;
int res = 0;
while(n>1){
res++;
n/=2;
}
return (1<<res)==temp?res:res+1;
}
static int mod = (int)1e9+7;
static int INF = Integer.MAX_VALUE;
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ================================ //
int test = sc.nextInt();
while (test-- > 0) {
int s = sc.nextInt();
solver(s);
}
// ================================ //
// int n = sc.nextInt();
// solver();
// ================================ //
out.flush();
}
public static void solver(int n) {
for(int i=0;i<n;i++){
int cur = n;
for(int j=0;j<n-1;j++){
if(n-i-1==j){
out.print(1+" ");
}
out.print( cur+" ");
cur--;
}
if(i==0)
out.print(1+" ");
out.println( );
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | fe7a9e9c7ba95c9b09a418d8682d93b2 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
// import javax.security.auth.kerberos.KerberosCredMessage;
public class antiFib {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
int n=0;
while (t-->0) {
n=sc.nextInt();
int[] num=new int[n];
for(int i=0;i<n;i++)
num[i]=n-i;
for(int i:num)
System.out.print(i+" ");
System.out.println();
for(int i=1;i<n;i+=2)
{
for(int k=1;k<=i;k+=2){
System.out.print(num[k]+" ");
System.out.print(num[k-1]+" ");
}
for(int k=i+1;k<n;k++)
{
System.out.print(num[k]+" ");
}
System.out.println();
}
for(int i=2;i<n;i+=2)
{
System.out.print(num[0]+" ");
for(int k=2;k<=i;k+=2){
System.out.print(num[k]+" ");
System.out.print(num[k-1]+" ");
}
for(int k=i+1;k<n;k++)
{
System.out.print(num[k]+" ");
}
System.out.println();
}
}
sc.close();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | c5f8bcc317884e07b6237b812da95845 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | // package com.company.Cf322;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class AntiFibonacciPermutatioon {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
while (t-- > 0) {
int n = Integer.parseInt(bf.readLine());
Queue<Integer> q = new LinkedList<>();
for (int i=n;i>=1;i--){
q.add(i);
System.out.print(i+" ");
}
System.out.println();
if (n==3){
System.out.println("1 3 2");
System.out.println("3 1 2");
}else {
int j = 0;
for (int i = 1; i < n; i++) {
int x = q.poll();
q.add(x);
j = 0;
while (j < n) {
int y = q.poll();
System.out.print(y + " ");
q.add(y);
j++;
}
System.out.println();
}
}
//4321
// 3214
//2143
//1232
//permutatoin p-> n length
//pi-2 = pi-1 != pi
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 90268788178e7b38688889605a0f2c2f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class a
{
public static void main(String args[])
{
int t;
Scanner sc=new Scanner(System.in);
t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
for(int j=1;j<=n;j++)
{
System.out.print(j+" ");
for(int m=n;m>j;m--)
{
System.out.print(m+" ");
}
for(int m=j-1;m>=1;m--)
{
System.out.print(m+" ");
}
System.out.println();
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 19a24899243019f6a30e0c1eaf29ba7a | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Contest
{
static void solve(int n,List<Integer> lst) {
Collections.sort(lst,Collections.reverseOrder());
int j = 0;
for(int i=lst.size()-1;i>0;i--){
j = i-1;
printList(lst);
Collections.swap(lst,j,i);
}
printList(lst);
}
static void printList(List<Integer> lst){
for(int i=0;i<lst.size();i++) System.out.print(lst.get(i)+" ");
System.out.println();
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0;i<t;i++){
int n = sc.nextInt();
List<Integer> lst = new ArrayList<>();
for(int j=1;j<=n;j++) lst.add(j);
solve(n,lst);
}
}
}
//Important Stuff
//Integer range is from -2*10^10 - 2*10^10
//look at the constraints in the question before defining data types
// String to int conversion is Integer.parseInt(s)
// int to String conversion is String.parseInt(i)
//char to ascii int value is (int)c
//ascii int value to char is Character.getNumericValue(i)
//char to int is int n = c (not ascii value, if c='3', n=3)
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | d658c5c8bd051dcea9ef8c26432ed6a9 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static 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 swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
for(int i=3;i<=n;i++){
System.out.print(i+" ");
}
for(int i=1;i<3;i++){
System.out.print(i+" ");
}
System.out.println();
for(int i=0;i<n-1;i++){
int k=n;
int p=2;
for(int j=0;j<i;j++){
System.out.print(k+" ");
k--;
}
for(int l=i;l<n-1;l++){
System.out.print(p+" ");
p++;
}
System.out.print(1);
System.out.println();
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 9192fb85b4f3d0e777b4ad05c6216743 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
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 int[] string_to_array(String[] arr){
int[] ans=new int[arr.length];
for(int i=0;i<arr.length;i++){
ans[i]=Integer.parseInt(arr[i]);
}
return ans;
}
static boolean fibcheck(int[] arr){
for(int i=2;i<arr.length;i++){
if(arr[i-2]+arr[i-1]==arr[i]){
return false;
}
}
return true;
}
static List<ArrayList<Integer>>ans;
private static void permuteHelper(int[] arr, int index){
if(index >= arr.length - 1){
if(fibcheck(arr) && ans.size()<arr.length){
ArrayList<Integer>arr1=new ArrayList<>();
for(int i:arr)arr1.add(i);
//System.out.print(arr1+"in list ");
ans.add(arr1);
}
return;
}
for(int i = index; i < arr.length; i++){
int t = arr[index];
arr[index] = arr[i];
arr[i] = t;
permuteHelper(arr, index+1);
t = arr[index];
arr[index] = arr[i];
arr[i] = t;
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=in.nextInt();
while(testCases-- > 0){
int n=Integer.parseInt(in.nextLine());
ans=new ArrayList<>();
if(n==3){
out.println("3 2 1");
out.println("1 3 2");
out.println("3 1 2");
continue;
}
int[] arr=new int[n];
for(int i=1;i<n+1;i++)arr[i-1]=i;
//permuteHelper(arr,0);
// for(List<Integer>temp:ans){
// for(int i:temp){
// out.print(i+" ");
// }
// out.println(" ");
// }
for(int i=2;i<n;i++){
if(arr[i-2]+arr[i-1]==arr[i]){
int t=arr[i];
arr[i]=arr[i-1];
arr[i-1]=t;
}
}
int cou=1;
for(int i=0;i<n;i++){
for(int j=n-i;j<n;j++){
out.print(arr[j]+" ");
}
for(int j=0;j<n-i;j++){
out.print(arr[j]+" ");
}
out.println(" ");
}
}
out.close();
} catch (Exception e) {
System.out.print(e);
return;
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 43bd47315bb8d0053853122c2e45be5c | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dataCount = sc.nextInt();
StringBuilder[][] answer = new StringBuilder[dataCount][];
int indexAppend = 0;
for (int i = 0; i < dataCount; i++){
int data = sc.nextInt();
int[] array = new int[data];
for (int j = 1; j<=data; j++){
array[j-1] = j;
}
answer[indexAppend] = new StringBuilder[data];
for(int j = 0; j < data; j++){
if (j > 0){
int temp = array[j];
array[j] = array[j - 1];
array[j-1] = temp;
}
StringBuilder str = new StringBuilder();
for (int k = array.length - 1; k >=0; k--){
str.append(array[k]);
if(!(k == 0)) str.append(" ");
}
answer[indexAppend][j] = str;
}
indexAppend++;
}
for (StringBuilder[] stringBuilders : answer) {
for (StringBuilder stringBuilder : stringBuilders) {
System.out.println(stringBuilder);
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 2f31cba9e65e012475abd887b6bd88f7 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Test {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StringTokenizer st;
public static void print(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
}
public static void main(String[] args) throws IOException {
int t = readInt();
for (int i = 0; i < t; i++) {
int n = readInt();
Integer[] arr = new Integer[n];
for (int j = 0; j < n; j++) {
arr[j]=j+1;
}
Arrays.sort(arr,Collections.reverseOrder());
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
System.out.print(arr[k]+" ");
}
System.out.println("");
if (j==n-1) {
break;
}
int temp=arr[0];
arr[0]=arr[j+1];
arr[j+1]=temp;
}
}
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readLine() throws IOException {
return br.readLine().trim();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | c17369a0a0335a0069865110eaeae98f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | // import java.lang.reflect.Array;
// import java.math.BigInteger;
// import java.nio.channels.AcceptPendingException;
// import java.nio.charset.IllegalCharsetNameException;
// import java.util.Collections;
// import java.util.logging.SimpleFormatter;
// import java.util.regex.Matcher;
// import java.util.regex.Pattern;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
/* docstring*/
public class Codeforces {
static Templates.FastScanner sc = new Templates.FastScanner();
static PrintWriter fop = new PrintWriter(System.out);
public static void main(String[] args) {
try {
// A();
B();
// C();
// D();
// E();
} catch (Exception e) {
return;
}
}
/* docstring */
static void A() throws IOException {
int T = Integer.parseInt(sc.next());
while (T-- > 0) {
String str = sc.next();
int keys[] = new int[3];
int doors[] = new int[3];
boolean flag = true;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'R')
doors[0] = i;
else if (ch == 'G')
doors[1] = i;
else if (ch == 'B')
doors[2] = i;
else if (ch == 'r')
keys[0] = i;
else if (ch == 'g')
keys[1] = i;
else if (ch == 'b')
keys[2] = i;
}
for(int i=0;i<3;i++){
if(keys[i]>doors[i]){
flag = false;
break;
}
}
if(flag)
fop.println("YES");
else
fop.println("NO");
}
fop.flush();
fop.close();
}
/* docstring */
static void B() throws IOException {
int T = Integer.parseInt(sc.next());
while (T-- > 0) {
int n = Integer.parseInt(sc.next());
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = i+1;
}
int temp = arr[1];
arr[1] = arr[2];
arr[2] = temp;
int k = n-1;
if(n == 3){
System.out.println("3 2 1");
System.out.println("1 3 2");
System.out.println("3 1 2");
}
else{
while (k >= 0) {
for (int i = k; i < n; i++) {
System.out.print(arr[i] + " ");
}
for (int i = 0; i < k; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
k--;
}
}
}
fop.flush();
fop.close();
}
/* docstring */
static void C() throws IOException {
int T = Integer.parseInt(sc.next());
while (T-- > 0) {
// Write Your Code...
}
fop.flush();
fop.close();
}
/* docstring */
static void D() throws IOException {
int T = Integer.parseInt(sc.next());
while (T-- > 0) {
// Write Your Code...
}
fop.flush();
fop.close();
}
/* docstring */
static void E() throws IOException {
int T = Integer.parseInt(sc.next());
while (T-- > 0) {
// Write Your Code...
}
fop.flush();
fop.close();
}
static void printArr(int arr[]){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
class Templates {
// int tree[] = new int[4*n+1] ;
// BuiltTree(A , 0 , n-1 , tree , 1);
// int lazy[] = new int[4*n + 1] ;
//
// updateRangeLazy(tree , lazy , 0 , n-1 , 0 , 2 , 10 , 1);
// updateRangeLazy(tree , lazy , 0 , n-1 , 0 , 4 , 10 , 1);
//
// fop.println(querylazy(tree , lazy , 0 , n-1 , 1 ,1 ,1));
//
// updateRangeLazy(tree, lazy , 0 , n-1 , 10 , 4 , 3 ,1);
// fop.println(querylazy(tree , lazy , 0 , n-1 , 3 , 5 , 1 ));
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
// segment tree
// BuiltTree(A , 0 , n-1 , tree , 1);
static void BuiltTree(int A[], int s, int e, int tree[], int index) {
if (s == e) {
tree[index] = A[s];
return;
}
int mid = (s + e) / 2;
BuiltTree(A, s, mid, tree, 2 * index);
BuiltTree(A, mid + 1, e, tree, 2 * index + 1);
tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]);
return;
}
static int query(int tree[], int ss, int se, int qs, int qe, int index) {
// complete overlap
if (ss >= qs && se <= qe)
return tree[index];
// no overlap
if (qe < ss || qs > se)
return Integer.MAX_VALUE;
// partial overlap
int mid = (ss + se) / 2;
int left = query(tree, ss, mid, qs, qe, 2 * index);
int right = query(tree, mid + 1, se, qs, qe, 2 * index + 1);
return Math.min(left, right);
}
static void update(int tree[], int ss, int se, int i, int increment, int index) {
// i is the index of which we want to update the value
if (i > se || i < ss)
return;
if (ss == se) {
tree[index] += increment;
return;
}
int mid = (ss + se) / 2;
update(tree, ss, mid, i, increment, 2 * index);
update(tree, mid + 1, se, i, increment, 2 * index + 1);
tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]);
}
static void updateRange(int tree[], int ss, int se, int l, int r, int inc, int index) {
if (l > se || r < ss)
return;
if (ss == se) {
tree[index] += inc;
return;
}
int mid = (ss + se) / 2;
updateRange(tree, ss, mid, l, r, inc, 2 * index);
updateRange(tree, mid + 1, se, l, r, inc, 2 * index + 1);
tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]);
return;
}
// Lazy rnage Update
static void updateRangeLazy(int tree[], int lazy[], int ss, int se, int l, int r, int increment, int index) {
if (lazy[index] != 0) {
tree[index] += lazy[index];
if (ss != se) {
lazy[2 * index] += lazy[index];
lazy[2 * index + 1] += lazy[index];
}
lazy[index] = 0;
}
if (ss > r && se < l)
return;
if (ss >= l && se <= r) {
tree[index] += increment;
if (ss != se) {
lazy[2 * index] += increment;
lazy[2 * index + 1] += increment;
}
return;
}
int mid = (ss + se) / 2;
updateRange(tree, ss, mid, l, r, increment, 2 * index);
updateRange(tree, mid + 1, se, l, r, increment, 2 * index + 1);
tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]);
}
// min lazy query
static int querylazy(int tree[], int lazy[], int ss, int se, int qs, int qe, int index) {
if (lazy[index] != 0) {
tree[index] += lazy[index];
if (ss != se) {
lazy[2 * index] += lazy[index];
lazy[2 * index + 1] += lazy[index];
}
lazy[index] = 0;
}
if (ss > qe || se < qs)
return Integer.MAX_VALUE;
if (ss >= qs && se <= qe)
return tree[index];
int mid = (ss + se) / 2;
int left = querylazy(tree, lazy, ss, mid, qs, qe, 2 * index);
int right = querylazy(tree, lazy, mid + 1, se, qs, qe, 2 * index + 1);
return Math.min(left, right);
}
static void sieve(int n) {
boolean[] flag = new boolean[n];
for (int i = 2; i * i < n; i++) {
if (flag[i])
continue;
else
for (int j = i * i; j <= n; j += i) {
flag[j] = true;
}
}
}
static int gcd(int a, int b) {
if (b > a) {
int tenp = b;
b = a;
a = tenp;
}
int temp = 0;
while (b != 0) {
a %= b;
temp = b;
b = a;
a = temp;
}
return a;
}
static long gcdl(long a, long b) {
if (b > a) {
long tenp = b;
b = a;
a = tenp;
}
long temp = 0;
while (b != 0) {
a %= b;
temp = b;
b = a;
a = temp;
}
return a;
}
static final Random random = new Random();
static void ruffleSort(int[] a) {
int n = a.length;// shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | a4dfbb4d6505bf925923ed123d0ed3bb | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for (int i = 0; i < n; i++) {
int num = scan.nextInt();
int[] arr = new int[num];
for (int j = 0; j < arr.length; j++) {
arr[j] = j + 1;
}
List<List<Integer>> result = getPermutation(arr, num);
for (int j = 0; j < result.size(); j++) {
for (int k = 0; k < result.get(j).size(); k++) {
System.out.print(result.get(j).get(k) + " ");
}
System.out.println();
}
}
}
public static List<List<Integer>> getPermutation(int[] nums, int limit) {
List<List<Integer>> result = new ArrayList<>();
dfs(result, nums, new ArrayList<>(), limit);
return result;
}
public static void dfs(List<List<Integer>> result, int[] nums, List<Integer> tmp, int limit) {
if (limit == result.size()) {
return;
}
if (tmp.size() == nums.length) {
result.add(new ArrayList(tmp));
return;
}
for (int num : nums) {
if (tmp.contains(num)) {
continue;
}
if (tmp.size() >= 2) {
if (tmp.get(tmp.size() - 1) + tmp.get(tmp.size() - 2) == num) {
continue;
}
}
tmp.add(num);
dfs(result, nums, tmp, limit);
tmp.remove(tmp.size() - 1);
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 8fcacbf4172cedd8a46162572cfbd547 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main{
static InputReader sc = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException{
//setUp("input.txt", "output.txt");
//setUp("hps.in", "hps.out");
int T = 1; T = sc.nextInt();
while(T-- > 0){
int n = sc.nextInt();
solve(n);
}
out.flush();
out.close();
}
public static void solve(int n){
if(n == 2){
out.println("2 1");
out.println("1 2");
return;
}else if(n == 3){
out.println("3 2 1");
out.println("1 3 2");
out.println("3 1 2");
return;
}else if(n == 4){
out.println("4 1 3 2");
out.println("1 2 4 3");
out.println("3 4 1 2");
out.println("2 4 1 3");
return;
}
int count = 0;
List<Integer> odd = new ArrayList<>();
List<Integer> even = new ArrayList<>();
for(int i = 1; i <= n; i++){
if(i % 2 == 0){
even.add(i);
}else{
odd.add(i);
}
}
List<List<Integer>> oddPermute = new ArrayList<>();
getPermute(odd, new ArrayList<>(), new boolean[odd.size()], oddPermute, n);
for(int i = 0 ; i < n; i++){
List<Integer> arr = oddPermute.get(i);
for(int j = 0 ; j < arr.size(); j++){
out.print(arr.get(j) + " ");
}
out.print(even.get(0) + " ");
for(int j = even.size() - 1 ; j > 0; j--){
out.print(even.get(j)+ " ");
}
out.println("");
}
}
public static void getPermute(List<Integer> list, List<Integer> str, boolean[] marked, List<List<Integer>> oddPermute, int n){
if(str.size() == list.size()){
oddPermute.add(new ArrayList<>(str));
return;
}
for(int i = 0; i < list.size(); i++){
if(oddPermute.size() == n){
return;
}
if(!marked[i]){
marked[i] = true;
str.add(list.get(i));
getPermute(list, str, marked, oddPermute, n);
str.remove(str.size() - 1);
marked[i] = false;
}
}
}
static void setUp(String input, String output) throws IOException{
sc = new InputReader(new FileInputStream(input));
out = new PrintWriter(new FileOutputStream(output));
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble(){ return Double.parseDouble(next()); }
public int[] readArray(int N){
int[] A = new int[N];
for(int i = 0 ; i < N; i++){
A[i] = this.nextInt();
}
return A;
}
}
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;
}
}
void shuffleArray(long[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static class Pair<K, V>{
K key;
V val;
public Pair(K key, V val){
this.key = key;
this.val = val;
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 2d923d8147065cc45826d8ac3bfc055f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
try {
Reader sc = new Reader();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
if(n == 3)
{
System.out.println("3 2 1");
System.out.println("3 1 2");
System.out.println("1 3 2");
continue;
}
ArrayList<Integer> arr = new ArrayList<>();
for(int i=n;i>=1;i--)
{
arr.add(i);
}
for(int i=0;i<n;i++)
{
for(int j=n-i;j<n;j++)
{
System.out.print(arr.get(j)+" ");
}
for(int k=0;k<n-i;k++)
System.out.print(arr.get(k)+" ");
System.out.println();
}
}
}
catch(Exception e)
{
return;
}
}
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')
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\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 3413ff829cbc886b3d1f212d37c1f211 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.Scanner;
public class P10 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t = in.nextInt();
int []n= new int [t];
for(int i=0;i<t;i++) {
n[i]=in.nextInt();
}
for(int i=0;i<t;i++) {
int [][]m=new int [n[i]][n[i]];
for(int j=0;j<n[i];j++) {
m[j][0]=j+1;
}
for(int j=0;j<n[i];j++) {
int l=1;
for(int k=1;k<n[i];k++) {
if(k==j+1) {
l++;}
m[j][k]=l;
l++;
}
}
for(int j=0;j<n[i];j++) {
int[] f=m[j];
for(int k=2;k<n[i];k++) {
if(f[k]==(f[k-1]+f[k-2])) {
if(k==n[i]-1) {
int temp=f[k];
f[k]=f[k-1];
f[k-1]=temp;
}
else {
int temp=f[k];
f[k]=f[k+1];
f[k+1]=temp;}
}
}
for(int h=0;h<n[i];h++) {
System.out.print(f[h]+" ");
}
System.out.println();
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 5142d643283abd2fea6ad1fbf56209be | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class antiFib {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0)
{
int n = scn.nextInt();
if(n==3)
{
System.out.println("3 2 1");
System.out.println("1 3 2");
System.out.println("3 1 2");
}
else {
ArrayList list = new ArrayList();
for (int i = n; i >= 1; i--)
list.add(i);
for (int i = 0; i < n; i++) {
for (int k = n - i; k < n; k++) {
System.out.print(list.get(k) + " ");
}
for (int j = 0; j < n - i; j++)
System.out.print(list.get(j) + " ");
System.out.println();
}
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | dfd11ad1f5b34a7e522ea50a5d50181b | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class B {
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) {
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
sb.append(solve(n));
}
System.out.println(sb.toString());
}
public static void print(StringBuilder res, List<Integer> arr) {
for (int i = 0; i < arr.size(); i++)
res.append(arr.get(i) + " ");
res.append("\n");
}
private static String solve(int n) {
StringBuilder sb = new StringBuilder();
int[][] res = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = n - 1; j >= 0; j--) {
res[i][j] = n - j;
}
}
for(int i=1; i<n ;i++)
{
int temp = res[i][i];
res[i][i]= res[i][i-1];
res[i][i-1] = temp;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sb.append(res[i][j] + " ");
}
sb.append("\n");
}
return sb.toString();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 7f934c56e4d45f4b43f9abb5e17c7e84 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | /**
* @Jai_Bajrang_Bali
* @Har_Har_Mahadev
*/
import java.util.HashMap;
import java.util.Scanner;
public class practice2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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] = n - i;;
}
int k = n;
if (n == 3) {
System.out.print("3 2 1\n"+ "1 3 2\n"+"3 1 2\n");
continue;
}
while (k-->0) {
int f = arr[n - 1];
for (int i = n - 1; i >= 1; i--) {
arr[i] = arr[i - 1];
}
arr[0] = f;
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 8b9113092cb3a758a365a22e553be650 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int x = 0; x < t; x++) {
int n = scanner.nextInt();
int[] p = new int[n];
for (int y = 0; y < n; y++) p[y] = n - y;
if (n > 3) {
int index = n - 2;
for (int y = 0; y < n; y++) {
for (int num : p) {
System.out.print(num);
if (num != p[n - 1]) System.out.print(" ");
}
System.out.println();
if (y != n - 1) {
if (index == 0) index = n - 2;
p[index - 1] = p[index - 1] + p[index];
p[index] = p[index - 1] - p[index];
p[index - 1] = p[index - 1] - p[index];
index--;
}
}
}
else {
System.out.println("3 2 1");
System.out.println("2 3 1");
System.out.println("1 3 2");
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | dd731aad6e8577dc9dcfe3a4047b6f5b | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
public class B {
static final Reader in = new Reader();
static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = in.nextInt();
for (int tc = 1; tc <= t; tc++) {
solve();
out.println();
}
out.close();
}
static final Random random = new Random();
static void solve() {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
int found = 0;
Set<String> seen = new HashSet<>();
while (found < n) {
randomShuffle(a);
String key = Arrays.toString(a);
if (!isAntiFib(a) || seen.contains(key)) {
continue;
}
seen.add(key);
for (int i = 0; i < n; i++) {
out.print(a[i] + (i < n - 1 ? " " : ""));
}
found++;
if (found < n) {
out.println();
}
if (found == n) {
break;
}
}
}
static void randomShuffle(int[] array) {
int n = array.length;
for (int i = 0; i < n; i++) {
int j = i + random.nextInt(n - i);
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
static boolean isAntiFib(int[] a) {
for (int i = 2; i < a.length; i++) {
if (a[i] == a[i - 1] + a[i - 2]) {
return false;
}
}
return true;
}
static class Reader {
private final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
Reader(InputStream in) {
try {
din = new DataInputStream(in);
} catch (Exception e) {
throw new RuntimeException();
}
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
String next() {
int c;
while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r')) ;
StringBuilder s = new StringBuilder();
while (c != -1) {
if (c == ' ' || c == '\n' || c == '\r')
break;
s.append((char) c);
c = read();
}
return s.toString();
}
String nextLine() {
int c;
while ((c = read()) != -1 && (c == ' ' || c == '\n' || c == '\r')) ;
StringBuilder s = new StringBuilder();
while (c != -1) {
if (c == '\n' || c == '\r')
break;
s.append((char) c);
c = read();
}
return s.toString();
}
int nextInt() {
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;
}
int[] nextInts(int n) {
int[] ar = new int[n];
for (int i = 0; i < n; ++i)
ar[i] = nextInt();
return ar;
}
long nextLong() {
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;
}
long[] nextLongs(int n) {
long[] ar = new long[n];
for (int i = 0; i < n; ++i)
ar[i] = nextLong();
return ar;
}
double nextDouble() {
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;
}
void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
byte read() {
try {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
} catch (IOException e) {
throw new RuntimeException();
}
}
void close() throws IOException {
din.close();
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 48e5be8adfdc28a59b154653b738e1cf | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
// import javax.print.event.PrintJobListener;
import java.io.*;
public class Coding {
public static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next () {
while(!st.hasMoreTokens()) {
try {
st=new StringTokenizer(br.readLine());
}catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = nextInt();
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
//prime sieve
public static boolean sieve[];
public static void sievePrime(int n) {
sieve = new boolean[n+1];
for(int i=2;i*i<=n;i++) {
if(sieve[i] == false) { //means prime hai
for(int j=i+i;j<sieve.length;j+= i) {
sieve[j] = true;
}
}
}
}
//swap
public static void swap(int a,int b) {
int temp= a;
a=b;
b=temp;
}
//Longest Palindromic String
public String manacher(String s){
StringBuilder sb = new StringBuilder();
sb.append("@");
for(int i=0;i<s.length();i++){
sb.append("#");
sb.append(s.charAt(i));
}
sb.append("#");
sb.append("$");
String ans = manacher_Util(sb.toString(),s);
return ans;
}
public String manacher_Util(String s,String org){
int lps[] = new int[s.length()];
int c = 0;
int r = 0;
for(int i=1;i<s.length()-1;i++){
int mirror = c - (i-c);
if(i<r){
lps[i] = Math.min(lps[mirror],r-i);
}
while(s.charAt(1+i+lps[i]) == s.charAt(i-1-lps[i])){
lps[i]++;
}
if(i+lps[i] > r){
c= i ;
r = i+lps[i];
}
}
int maxlen = Integer.MIN_VALUE;
int maxindex = Integer.MIN_VALUE;
for(int i=1;i<lps.length-1;i++){
if(lps[i] > maxlen){
maxlen = lps[i];
maxindex = i;
}
}
int firstindex = maxindex - maxlen + 1;
int actualFirstIndex = (firstindex-2)/2;
return org.substring(actualFirstIndex,actualFirstIndex+maxlen);
}
//kmp
public static int lps[];
public static void kmp(String s) {
lps = new int[s.length()];
int i = 1;
int len = 0;
while(i<s.length()) {
if(s.charAt(i) == s.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else {
if(len>0) {
len = lps[len - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
// PrintWriter o = new PrintWriter(System.out);
while(test-->0) {
int n=sc.nextInt();
ArrayList<String> al=solve(n);
for(String s:al){
System.out.println(s);
}
}
}
public static ArrayList<String> solve(int n){
ArrayList<String> al=new ArrayList<>();
if(n==3){
al.add("3 2 1");
al.add("3 1 2");
al.add("1 3 2");
return al;
}
ArrayList<String> ret=solve(n-1);
String st=n+" ";
for(String s:ret){
StringBuilder sb=new StringBuilder(st);
al.add(sb.append(s).toString());
}
String str=ret.get(0).substring(2);
StringBuilder sb=new StringBuilder(n-1+" "+st);
if(str.charAt(0)==' '){
sb.deleteCharAt(sb.length()-1);
sb.append(str);
al.add(sb.toString());
}else{
sb.append(str);
al.add(sb.toString());
}
// //sb.deleteCharAt(0);
// //System.out.println(sb.charAt(0));
// sb.setCharAt(0, st.charAt(0));
// str=sb.toString();
// st=n-1+" ";
// sb=new StringBuilder(st);
// sb.append(str);
// al.add(sb.toString());
return al;
}
public static boolean check(int[] arr){
boolean ans=true;
for(int i=0;i<26;i++){
if(arr[i]>0){
ans=false;
break;
}
}
return ans;
}
// public static print(int n,int op,int cl){
// print()
// }
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int fac(int n)
{
int ans = 1;
for(int i = 1;i<=n;i++)
{
ans *= i;
}
return ans;
}
}
// //10 10
// 10 99
// 177 13 | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | a2a8016deaee043fe2094ee889ee11d9 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes |
// Working program with FastReader
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class B_Anti_Fibonacci_Permutation {
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 boolean check(int arr[]){
for(int i=2;i<arr.length;i++){
if(arr[i]==(arr[i-1]+arr[i-2])) return false;
}
return true;
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
if(n<=2) continue;
int arr[]=new int[n];
for(int i=1;i<=n;i++){
arr[i-1]=i;
}
int ans=0;
for(int i=0;i<arr.length;i++){
if(ans==n) break;
for(int j=0;j<arr.length-1;j++){
if(ans==n) break;
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
if(check(arr)){
++ans;
for(int k:arr) System.out.print(k+" ");
System.out.println();
}
}
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 196f5a4f2765390f57e2aed4aababdc1 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static int MAX_SIZE = (int) 1e6;
static boolean []prime = new boolean[MAX_SIZE + 1];
static FastReader scn = new FastReader();
static PrintWriter out= new PrintWriter(System.out);
static class Pair{
int x;
int y;
Pair(int a, int b){
this.x = a;
this.y = b;
}
}
public static void main(String[] args) throws IOException {
int t = scn.nextInt();
int caseNum = 1;
while (t-- > 0){
int n = scn.nextInt();
HashMap<Integer, Boolean> map = new HashMap<>();
if(n == 3){
out.println("3 2 1\n" +
"1 3 2\n" +
"3 1 2");
continue;
}
int[] arr = new int[n-1];
int k = 1;
arr[0] = n-1;
int limit = n-2;
map.put(n-1, true);
for(int i = n-2; i>0; i--){
for(int j = limit; j>=1; j--){
if(!map.containsKey(j) && arr[k-1] + j != n){
arr[k] = j;
k++;
limit = Math.max(limit, j);
map.put(j, true);
break;
}
}
}
for(int i = 0; i<n; i++){
for(int j = 0; j<n-1; j++){
if(i == j){
out.print(n + " ");
}
out.print(arr[j] + " ");
}
if(i == n-1){
out.print(n + " ");
}
out.println();
}
}
out.close();
}
static boolean maxima(int i, int[] arr){
return (arr[i-1] < arr[i] && arr[i] > arr[i+1]);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Sortby implements Comparator<Pair> {
public int compare(Pair a, Pair b)
{
return a.x - b.x;
}
}
static boolean isPalindrome(String s){
int n = s.length();
for(int i = 0; i<n/2; i++){
if (s.charAt(i) != s.charAt(n-i-1)){
return false;
}
}
return true;
}
static int getMex(int[] arr){
int mex = 0;
HashMap<Integer, Boolean> map = new HashMap<>();
for (int j : arr) {
map.put(j, true);
}
while(map.containsKey(mex)){
mex++;
}
return mex;
}
static void sieve() {
Arrays.fill(prime, true);
for (int p = 2; p * p <= MAX_SIZE; p++) {
if (prime[p]) {
for (int i = p * p;i <= MAX_SIZE; i += p){
prime[i] = false;
}
}
}
}
static int kthPrimeGreaterThanN(int n) {
int res = -1;
for (int i = n; i < MAX_SIZE; i++) {
if (prime[i]){
res = i;
break;
}
}
return res;
}
static int knapSack(int W, int wt[], int val[], int n) {
int i, w;
int K[][] = new int[n + 1][W + 1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = Math.max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
static int moves(int num){
int cur = num;
int ans = 0;
int val = 0;
while (cur != 0){
val = (int)(Math.log(cur)/Math.log(2));
ans += val;
cur -= (int)Math.pow(2, val);
}
if(val == 0 && num != 1){
ans++;
}
return ans;
}
static boolean isEven(long a){
return (a & 1) == 0;
}
static long pow2(long a){
return 1L <<a;
}
static boolean isPowerof2(long x){
return x!=0 && ((x&(x-1)) == 0);
}
static long highestSetBit(long n){
long k = 0;
while(1L << (k+1) <= n-1){
k++;
}
return k;
}
static void accessSubsets(int[] arr, int index){
int n = arr.length;
for(int i = index; i<n; i++){
accessSubsets(arr, i+1);
}
}
public int arrayGCD(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
return gcd(nums[0], nums[n-1]);
}
public int subsetGCD(int[] nums, int s, int e) {
int minIndex = 0;
int maxIndex = 0;
for(int i = s; i<e; i++){
if(nums[i] > nums[maxIndex]){
maxIndex = i;
}
else if(nums[i] < nums[minIndex]){
minIndex = i;
}
}
return gcd(nums[maxIndex], nums[minIndex]);
}
public int gcd(int a, int b){
if(b == 0){
return a;
}
return gcd(b,a%b);
}
public static int maxArr(int[] arr){
int val = Integer.MIN_VALUE;
int n = arr.length;
for(int i = 0; i<n; i++){
if(arr[i] > val){
val = arr[i];
}
}
return val;
}
public static void reverseArr(int[] arr){
int n = arr.length;
int temp;
for(int i = 0; i<n/2; i++){
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
}
static void print(int[] arr){
for(int val : arr){
out.print(val + " ");
}
out.println();
}
static void print(ArrayList<?> list){
for(Object val : list){
out.print(val + " ");
}
out.println();
}
static void readArray(int[] arr){
int n = arr.length;
for(int i = 0; i<n; i++){
arr[i] = scn.nextInt();
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 1e967f333178357798e3f0895a9e4769 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0;i<t;i++){
int n = sc.nextInt();
if(n==3){
System.out.println("1 3 2");
System.out.println("3 1 2");
System.out.println("3 2 1");
continue;
}
LinkedList<Integer> list = new LinkedList<Integer>();
for(int j=0;j<n;j++){
list.add(j,j+1);
}
list.set(2,4);
list.set(3,3);
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
System.out.print(list.get(k) + " ");
}
System.out.println();
int temp = list.get(n-1);
list.remove(n-1);
list.add(0,temp);
}
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 11 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 7f03a7b92670688d358f4ef4e42a2b28 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class AntiFibonacciPermutation{
public static void print(int k){
int l=k;
for(int m=1;m<=l;m++){
for(int n=1;n<=l;n++){
if(k==0) k=l;
System.out.print(k+" ");
k--;
}
k++;
System.out.println();
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
int n[]=new int[t];
int a[][]={{3,2,1},{1,3,2},{2,3,1}};
for(int i=0;i<t;i++){
n[i]=in.nextInt();
}
for(int j=0;j<t;j++){
if(n[j]==3){
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
System.out.print(a[x][y]+" ");
}
System.out.println();
}
}
else AntiFibonacciPermutation.print(n[j]);
}
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 17 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 16c88a5a1d40bc139b57ea2d3bd4808f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.stream.IntStream.iterate;
public class Template {
private static final FastScanner scanner = new FastScanner();
private static void solve() {
int n = i(), a[] = new int[n];
int x = 2;
for (int i = 0; i<n-1; i++) {
a[i] = x++;
}
a[n-1] = 1;
printArray(a);
for (int i = 0; i<n-1; i++) {
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
printArray(a);
}
}
public static void main(String[] args) {
int t = i();
while (t-->0) {
solve();
}
}
private static long fac(int n) {
long res = 1;
for (int i = 2; i<=n; i++) {
res*=i;
}
return res;
}
private static BigInteger factorial(int n) {
BigInteger res = BigInteger.valueOf(1);
for (int i = 2; i <= n; i++){
res = res.multiply(BigInteger.valueOf(i));
}
return res;
}
private static long l() {
return scanner.nextLong();
}
private static int i() {
return scanner.nextInt();
}
private static String s() {
return scanner.next();
}
private static void yes() {
out.println("YES");
}
private static void no() {
out.println("NO");
}
private static int toInt(char c) {
return Integer.parseInt(c+"");
}
private static void printArray(long[] a) {
StringBuilder builder = new StringBuilder();
for (long i : a)
builder.append(i).append(' ');
out.println(builder);
}
private static void printArray(int[] a) {
StringBuilder builder = new StringBuilder();
for (int i : a)
builder.append(i).append(' ');
out.println(builder);
}
private static int binPow(int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binPow(a, n-1) * a;
else {
int b = binPow(a, n/2);
return b * b;
}
}
private static boolean isPrime(long n) {
return iterate(2, i -> (long) i * i <= n, i -> i + 1).noneMatch(i -> n % i == 0);
}
private static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private static void stableSort(int[] a) {
List<Integer> list = stream(a).boxed().sorted().toList();
setAll(a, list::get);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(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[] readLong(int n) {
long[] a = new long[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());
}
}
static class SegmentTreeRMXQ {
static int getMid(int s, int e) {
return s + (e - s) / 2;
}
static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) {
if (l <= ss && r >= se)
return st[node];
if (se < l || ss > r)
return -1;
int mid = getMid(ss, se);
return max(
MaxUtil(st, ss, mid, l, r,
2 * node + 1),
MaxUtil(st, mid + 1, se, l, r,
2 * node + 2));
}
static void updateValue(int[] arr, int[] st, int ss, int se, int index, int value, int node) {
if (index < ss || index > se) {
System.out.println("Invalid Input");
return;
}
if (ss == se) {
arr[index] = value;
st[node] = value;
} else {
int mid = getMid(ss, se);
if (index <= mid)
updateValue(arr, st, ss, mid,
index, value,
2 * node + 1);
else
updateValue(arr, st, mid + 1, se, index,
value, 2 * node + 2);
st[node] = max(st[2 * node + 1],
st[2 * node + 2]);
}
}
static int getMax(int[] st, int n, int l, int r) {
if (l < 0 || r > n - 1 || l > r) {
System.out.print("Invalid Input\n");
return -1;
}
return MaxUtil(st, 0, n - 1, l, r, 0);
}
static int constructSTUtil(int[] arr, int ss, int se, int[] st, int si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = max(
constructSTUtil(arr, ss, mid,
st, si * 2 + 1),
constructSTUtil(arr, mid + 1,
se, st,
si * 2 + 2));
return st[si];
}
static int[] constructST(int[] arr, int n) {
int x = (int)Math.ceil(Math.log(n) / Math.log(2));
int max_size = 2 * (int)Math.pow(2, x) - 1;
int[] st = new int[max_size];
constructSTUtil(arr, 0, n - 1, st, 0);
return st;
}
}
static class SegmentTreeRMNQ {
int[] st;
int minVal(int x, int y) {
return min(x, y);
}
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int RMQUtil(int ss, int se, int qs, int qe, int index) {
if (qs <= ss && qe >= se)
return st[index];
if (se < qs || ss > qe)
return Integer.MAX_VALUE;
int mid = getMid(ss, se);
return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1),
RMQUtil(mid + 1, se, qs, qe, 2 * index + 2));
}
int RMQ(int n, int qs, int qe) {
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}
return RMQUtil(0, n - 1, qs, qe, 0);
}
int constructSTUtil(int[] arr, int ss, int se, int si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, si * 2 + 2));
return st[si];
}
void constructST(int[] arr, int n) {
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // allocate memory
constructSTUtil(arr, 0, n - 1, 0);
}
}
static class SegmentTreeRSQ
{
int[] st; // The array that stores segment tree nodes
/* Constructor to construct segment tree from given array. This
constructor allocates memory for segment tree and calls
constructSTUtil() to fill the allocated memory */
SegmentTreeRSQ(int[] arr, int n)
{
// Allocate memory for segment tree
//Height of segment tree
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
//Maximum size of segment tree
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // Memory allocation
constructSTUtil(arr, 0, n - 1, 0);
}
// A utility function to get the middle index from corner indexes.
int getMid(int s, int e) {
return s + (e - s) / 2;
}
/* A recursive function to get the sum of values in given range
of the array. The following are parameters for this function.
st --> Pointer to segment tree
si --> Index of current node in the segment tree. Initially
0 is passed as root is always at index 0
ss & se --> Starting and ending indexes of the segment represented
by current node, i.e., st[si]
qs & qe --> Starting and ending indexes of query range */
int getSumUtil(int ss, int se, int qs, int qe, int si)
{
// If segment of this node is a part of given range, then return
// the sum of the segment
if (qs <= ss && qe >= se)
return st[si];
// If segment of this node is outside the given range
if (se < qs || ss > qe)
return 0;
// If a part of this segment overlaps with the given range
int mid = getMid(ss, se);
return getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
}
/* A recursive function to update the nodes which have the given
index in their range. The following are parameters
st, si, ss and se are same as getSumUtil()
i --> index of the element to be updated. This index is in
input array.
diff --> Value to be added to all nodes which have I in range */
void updateValueUtil(int ss, int se, int i, int diff, int si)
{
// Base Case: If the input index lies outside the range of
// this segment
if (i < ss || i > se)
return;
// If the input index is in range of this node, then update the
// value of the node and its children
st[si] = st[si] + diff;
if (se != ss) {
int mid = getMid(ss, se);
updateValueUtil(ss, mid, i, diff, 2 * si + 1);
updateValueUtil(mid + 1, se, i, diff, 2 * si + 2);
}
}
// The function to update a value in input array and segment tree.
// It uses updateValueUtil() to update the value in segment tree
void updateValue(int arr[], int n, int i, int new_val)
{
// Check for erroneous input index
if (i < 0 || i > n - 1) {
System.out.println("Invalid Input");
return;
}
// Get the difference between new value and old value
int diff = new_val - arr[i];
// Update the value in array
arr[i] = new_val;
// Update the values of nodes in segment tree
updateValueUtil(0, n - 1, i, diff, 0);
}
// Return sum of elements in range from index qs (query start) to
// qe (query end). It mainly uses getSumUtil()
int getSum(int n, int qs, int qe)
{
// Check for erroneous input values
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}
return getSumUtil(0, n - 1, qs, qe, 0);
}
// A recursive function that constructs Segment Tree for array[ss..se].
// si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int si)
{
// If there is one element in array, store it in current node of
// segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then recur for left and
// right subtrees and store the sum of values in this node
int mid = getMid(ss, se);
st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) +
constructSTUtil(arr, mid + 1, se, si * 2 + 2);
return st[si];
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 17 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | b4aeecdf309a67688451f81ce4002624 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.stream.IntStream.iterate;
public class Template {
private static final FastScanner scanner = new FastScanner();
private static void solve() {
int n = i(), a[] = new int[n];
int x = 2;
for (int i = 0; i<n-1; i++) {
a[i] = x++;
}
a[n-1] = 1;
printArray(a);
for (int i = 0; i<n-1; i++) {
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
printArray(a);
}
}
public static void main(String[] args) {
int t = i();
while (t-->0) {
solve();
}
}
private static long fac(int n) {
long res = 1;
for (int i = 2; i<=n; i++) {
res*=i;
}
return res;
}
private static BigInteger factorial(int n) {
BigInteger res = BigInteger.valueOf(1);
for (int i = 2; i <= n; i++){
res = res.multiply(BigInteger.valueOf(i));
}
return res;
}
private static long l() {
return scanner.nextLong();
}
private static int i() {
return scanner.nextInt();
}
private static String s() {
return scanner.next();
}
private static void yes() {
out.println("YES");
}
private static void no() {
out.println("NO");
}
private static int toInt(char c) {
return Integer.parseInt(c+"");
}
private static void printArray(long[] a) {
StringBuilder builder = new StringBuilder();
for (long i : a)
builder.append(i).append(' ');
out.println(builder);
}
private static void printArray(int[] a) {
StringBuilder builder = new StringBuilder();
for (int i : a)
builder.append(i).append(' ');
out.println(builder);
}
private static int binPow(int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binPow(a, n-1) * a;
else {
int b = binPow(a, n/2);
return b * b;
}
}
private static boolean isPrime(long n) {
return iterate(2, i -> (long) i * i <= n, i -> i + 1).noneMatch(i -> n % i == 0);
}
private static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private static void stableSort(int[] a) {
List<Integer> list = stream(a).boxed().sorted().toList();
setAll(a, list::get);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(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[] readLong(int n) {
long[] a = new long[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());
}
}
static class SegmentTreeRMXQ {
static int getMid(int s, int e) {
return s + (e - s) / 2;
}
static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) {
if (l <= ss && r >= se)
return st[node];
if (se < l || ss > r)
return -1;
int mid = getMid(ss, se);
return max(
MaxUtil(st, ss, mid, l, r,
2 * node + 1),
MaxUtil(st, mid + 1, se, l, r,
2 * node + 2));
}
static void updateValue(int[] arr, int[] st, int ss, int se, int index, int value, int node) {
if (index < ss || index > se) {
System.out.println("Invalid Input");
return;
}
if (ss == se) {
arr[index] = value;
st[node] = value;
} else {
int mid = getMid(ss, se);
if (index <= mid)
updateValue(arr, st, ss, mid,
index, value,
2 * node + 1);
else
updateValue(arr, st, mid + 1, se, index,
value, 2 * node + 2);
st[node] = max(st[2 * node + 1],
st[2 * node + 2]);
}
}
static int getMax(int[] st, int n, int l, int r) {
if (l < 0 || r > n - 1 || l > r) {
System.out.print("Invalid Input\n");
return -1;
}
return MaxUtil(st, 0, n - 1, l, r, 0);
}
static int constructSTUtil(int[] arr, int ss, int se, int[] st, int si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = max(
constructSTUtil(arr, ss, mid,
st, si * 2 + 1),
constructSTUtil(arr, mid + 1,
se, st,
si * 2 + 2));
return st[si];
}
static int[] constructST(int[] arr, int n) {
int x = (int)Math.ceil(Math.log(n) / Math.log(2));
int max_size = 2 * (int)Math.pow(2, x) - 1;
int[] st = new int[max_size];
constructSTUtil(arr, 0, n - 1, st, 0);
return st;
}
}
static class SegmentTreeRMNQ {
int[] st;
int minVal(int x, int y) {
return min(x, y);
}
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int RMQUtil(int ss, int se, int qs, int qe, int index) {
if (qs <= ss && qe >= se)
return st[index];
if (se < qs || ss > qe)
return Integer.MAX_VALUE;
int mid = getMid(ss, se);
return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1),
RMQUtil(mid + 1, se, qs, qe, 2 * index + 2));
}
int RMQ(int n, int qs, int qe) {
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}
return RMQUtil(0, n - 1, qs, qe, 0);
}
int constructSTUtil(int[] arr, int ss, int se, int si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, si * 2 + 2));
return st[si];
}
void constructST(int[] arr, int n) {
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // allocate memory
constructSTUtil(arr, 0, n - 1, 0);
}
}
static class SegmentTreeRSQ
{
int[] st; // The array that stores segment tree nodes
/* Constructor to construct segment tree from given array. This
constructor allocates memory for segment tree and calls
constructSTUtil() to fill the allocated memory */
SegmentTreeRSQ(int[] arr, int n)
{
// Allocate memory for segment tree
//Height of segment tree
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
//Maximum size of segment tree
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // Memory allocation
constructSTUtil(arr, 0, n - 1, 0);
}
// A utility function to get the middle index from corner indexes.
int getMid(int s, int e) {
return s + (e - s) / 2;
}
/* A recursive function to get the sum of values in given range
of the array. The following are parameters for this function.
st --> Pointer to segment tree
si --> Index of current node in the segment tree. Initially
0 is passed as root is always at index 0
ss & se --> Starting and ending indexes of the segment represented
by current node, i.e., st[si]
qs & qe --> Starting and ending indexes of query range */
int getSumUtil(int ss, int se, int qs, int qe, int si)
{
// If segment of this node is a part of given range, then return
// the sum of the segment
if (qs <= ss && qe >= se)
return st[si];
// If segment of this node is outside the given range
if (se < qs || ss > qe)
return 0;
// If a part of this segment overlaps with the given range
int mid = getMid(ss, se);
return getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
}
/* A recursive function to update the nodes which have the given
index in their range. The following are parameters
st, si, ss and se are same as getSumUtil()
i --> index of the element to be updated. This index is in
input array.
diff --> Value to be added to all nodes which have I in range */
void updateValueUtil(int ss, int se, int i, int diff, int si)
{
// Base Case: If the input index lies outside the range of
// this segment
if (i < ss || i > se)
return;
// If the input index is in range of this node, then update the
// value of the node and its children
st[si] = st[si] + diff;
if (se != ss) {
int mid = getMid(ss, se);
updateValueUtil(ss, mid, i, diff, 2 * si + 1);
updateValueUtil(mid + 1, se, i, diff, 2 * si + 2);
}
}
// The function to update a value in input array and segment tree.
// It uses updateValueUtil() to update the value in segment tree
void updateValue(int arr[], int n, int i, int new_val)
{
// Check for erroneous input index
if (i < 0 || i > n - 1) {
System.out.println("Invalid Input");
return;
}
// Get the difference between new value and old value
int diff = new_val - arr[i];
// Update the value in array
arr[i] = new_val;
// Update the values of nodes in segment tree
updateValueUtil(0, n - 1, i, diff, 0);
}
// Return sum of elements in range from index qs (query start) to
// qe (query end). It mainly uses getSumUtil()
int getSum(int n, int qs, int qe)
{
// Check for erroneous input values
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}
return getSumUtil(0, n - 1, qs, qe, 0);
}
// A recursive function that constructs Segment Tree for array[ss..se].
// si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int si)
{
// If there is one element in array, store it in current node of
// segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then recur for left and
// right subtrees and store the sum of values in this node
int mid = getMid(ss, se);
st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) +
constructSTUtil(arr, mid + 1, se, si * 2 + 2);
return st[si];
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 17 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 5783fc3e1bce5482ba9c4786613ee480 | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc =new Scanner(System.in);
int tc = sc.nextInt();
while(tc-- > 0){
int n = sc.nextInt();
int st = 1;
while(st <= n){
System.out.print(st+" ");
for(int i = n; i > 0; i--){
if(i != st){
System.out.print(i+" ");
}
}
System.out.println();
st++;
}
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 17 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | da77039530e1d5119b149325546d008f | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.io.*;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Random;
/**
* Provide prove of correctness before implementation. Implementation can cost a lot of time.
* Anti test that prove that it's wrong.
* <p>
* Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write more informative names to simulation
* <p>
* Will program ever exceed limit?
* Try all approaches with prove of correctness if task is not obvious.
* If you are given formula/rule: Try to play with it.
* Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data
* Number theory
* Game theory (optimal play) that consider local and global strategy.
* Start writing the hardest code first
*/
public class B {
final boolean ONLINE_JUDGE = java.lang.System.getProperty("ONLINE_JUDGE") != null;
final boolean ANTI_TEST_FINDER_MODE = false;
final Random random = new Random(42);
private int solveOne(int testCase) {
int n = nextInt();
if(n == 3) {
System.out.println("3 2 1");
System.out.println("1 3 2");
System.out.println("3 1 2");
} else {
int[] ans = new int[n];
int p = 0;
for(int i = n; i >= 1; i--) {
ans[p++] = i;
}
for(int s = 0; s < n; s++) {
for(int i = 0; i < n ; i++) {
System.out.print(ans[(i + s) % n]).print(' ');
}
System.out.println();
}
}
return 0;
}
private int solveOneNaive(int testCase) {
return 0;
}
private void solve() {
if (ANTI_TEST_FINDER_MODE) {
int t = 100_000;
for (int testCase = 0; testCase < t; testCase++) {
int expected = solveOneNaive(testCase);
int actual = solveOne(testCase);
if (expected != actual) {
throw new AssertionRuntimeException(
this.getClass().getSimpleName(),
testCase,
expected,
actual);
}
}
} else {
int t = nextInt();
for (int testCase = 0; testCase < t; testCase++) {
solveOne(testCase);
}
}
}
class AssertionRuntimeException extends RuntimeException {
AssertionRuntimeException(String testName,
int testCase,
Object expected,
Object actual, Object... input) {
super("Testcase: " + testCase + "\n expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input));
}
}
private void assertThat(boolean b) {
if (!b) throw new RuntimeException();
}
private void assertThat(boolean b, String s) {
if (!b) throw new RuntimeException(s);
}
private int assertThatInt(long a) {
assertThat(Integer.MIN_VALUE <= a && a <= Integer.MAX_VALUE);
return (int) a;
}
void _______debug(String str, Object... os) {
if (!ONLINE_JUDGE) {
System.out.println(MessageFormat.format(str, os));
}
}
void _______debug(Object o) {
if (!ONLINE_JUDGE) {
_______debug("{0}", String.valueOf(o));
}
}
private int nextInt() {
return System.in.readInt();
}
private long nextLong() {
return System.in.readLong();
}
private String nextString() {
return System.in.readString();
}
private int[] nextIntArr(int n) {
return System.in.readIntArray(n);
}
private long[] nextLongArr(int n) {
return System.in.readLongArray(n);
}
public static void main(String[] args) {
new B().run();
}
static class System {
private static FastInputStream in;
private static FastPrintStream out;
}
private void run() {
final boolean USE_IO = ONLINE_JUDGE;
if (USE_IO) {
System.in = new FastInputStream(java.lang.System.in);
System.out = new FastPrintStream(java.lang.System.out);
solve();
System.out.flush();
} else {
final String nameIn = "input.txt";
final String nameOut = "output.txt";
try {
System.in = new FastInputStream(new FileInputStream(nameIn));
System.out = new FastPrintStream(new PrintStream(nameOut));
solve();
System.out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
private static class FastPrintStream {
private static final int BUF_SIZE = 8192;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastPrintStream() {
this(java.lang.System.out);
}
public FastPrintStream(OutputStream os) {
this.out = os;
}
public FastPrintStream(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastPrintStream print(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerFlush();
return this;
}
public FastPrintStream print(char c) {
return print((byte) c);
}
public FastPrintStream print(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerFlush();
}
return this;
}
public FastPrintStream print(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerFlush();
});
return this;
}
//can be optimized
public FastPrintStream print0(char[] s) {
if (ptr + s.length < BUF_SIZE) {
for (char c : s) {
buf[ptr++] = (byte) c;
}
} else {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerFlush();
}
}
return this;
}
//can be optimized
public FastPrintStream print0(String s) {
if (ptr + s.length() < BUF_SIZE) {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
}
} else {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
if (ptr == BUF_SIZE) innerFlush();
}
}
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastPrintStream print(int x) {
if (x == Integer.MIN_VALUE) {
return print((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerFlush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastPrintStream print(long x) {
if (x == Long.MIN_VALUE) {
return print("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerFlush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastPrintStream print(double x, int precision) {
if (x < 0) {
print('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
print((long) x).print(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
print((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastPrintStream println(char c) {
return print(c).println();
}
public FastPrintStream println(int x) {
return print(x).println();
}
public FastPrintStream println(long x) {
return print(x).println();
}
public FastPrintStream println(String x) {
return print(x).println();
}
public FastPrintStream println(Object x) {
return print(x.toString()).println();
}
public FastPrintStream println(double x, int precision) {
return print(x, precision).println();
}
public FastPrintStream println() {
return print((byte) '\n');
}
public FastPrintStream printf(String format, Object... args) {
return print(String.format(format, args));
}
private void innerFlush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerFlush");
}
}
public void flush() {
innerFlush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
}
private static class FastInputStream {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastInputStream(InputStream stream) {
this.stream = stream;
}
public double[] readDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = readDouble();
}
return array;
}
public String[] readStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = readString();
}
return array;
}
public char[] readCharArray(int size) {
char[] array = new char[size];
for (int i = 0; i < size; i++) {
array[i] = readCharacter();
}
return array;
}
public String readText() {
StringBuilder result = new StringBuilder();
while (true) {
int character = read();
if (character == '\r') {
continue;
}
if (character == -1) {
break;
}
result.append((char) character);
}
return result.toString();
}
public long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = readLong();
}
return array;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
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 peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int peekNonWhitespace() {
while (isWhitespace(peek())) {
read();
}
return peek();
}
public int readInt() {
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 readLong() {
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 String readString() {
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 char[] readStringAsCharArray() {
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));
char[] resArr = new char[res.length()];
res.getChars(0, res.length(), resArr, 0);
return resArr;
}
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;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double readDouble() {
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, readInt());
}
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, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return readString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 17 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 3f5794412202c6585e8443a95c4de7bc | train_108.jsonl | 1645540500 | Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class RoundEdu123B {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
RoundEdu123B sol = new RoundEdu123B();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int MAX = 50;
preprocess(MAX);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
int n = in.nextInt();
if(isDebug){
out.printf("Test %d\n", i);
}
int[][] ans = solve(n);
for(int[] anti: ans)
out.printlnAns(anti);
if(isDebug)
out.flush();
}
in.close();
out.close();
}
int[][][] ans;
private void preprocess(int MAX) {
ans = new int[MAX+1][][];
ans[3] = new int[][] {{1, 3, 2}, {3, 1, 2}, {3, 2, 1}};
for(int n=4; n<=MAX; n++) {
ans[n] = new int[n][];
ans[n][0] = new int[n];
System.arraycopy(ans[n-1][0], 0, ans[n][0], 0, n-1);
ans[n][0][n-1] = n;
for(int i=1; i<n; i++) {
ans[n][i] = new int[n];
ans[n][i][0] = ans[n][i-1][n-1];
for(int j=1; j<n; j++)
ans[n][i][j] = ans[n][i-1][j-1];
}
}
}
private int[][] solve(int n) {
return ans[n];
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextTreeEdges(int n, int offset){
int[][] e = new int[n-1][2];
for(int i=0; i<n-1; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextPairs(int n){
return nextPairs(n, 0);
}
int[][] nextPairs(int n, int offset) {
int[][] xy = new int[2][n];
for(int i=0; i<n; i++) {
xy[0][i] = nextInt() + offset;
xy[1][i] = nextInt() + offset;
}
return xy;
}
int[][] nextGraphEdges(){
return nextGraphEdges(0);
}
int[][] nextGraphEdges(int offset) {
int m = nextInt();
int[][] e = new int[m][2];
for(int i=0; i<m; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
int[] inIdx = new int[n];
int[] outIdx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][outIdx[u]++] = v;
inNeighbors[v][inIdx[v]++] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
int[] idx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][idx[u]++] = v;
neighbors[v][idx[v]++] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["2\n\n4\n\n3"] | 2 seconds | ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"] | null | Java 17 | standard input | [
"brute force",
"constructive algorithms",
"implementation"
] | 85f0621e6cd7fa233cdee8269310f141 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$). | 800 | For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem. | standard output | |
PASSED | 142b7fd90265531b15450f60c848d296 | train_108.jsonl | 1645540500 | You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | 256 megabytes | import java.util.*;
public class A {
static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static int gcd(int n, int m) {
if (m == 0)
return n;
else
return gcd(m, n % m);
}
static int lcm(int n, int m) {
return (n * m) / gcd(n, m);
}
static void dfs(ArrayList<ArrayList<Integer>> adj, int s, int[] vis, int[] cnt) {
for (int i : adj.get(s)) {
if (vis[i] == 1) {
continue;
}
cnt[0]++;
vis[i] = 1;
dfs(adj, i, vis, cnt);
}
}
public static long solve(long a, long b, long x, long y, long n) {
if (a - n >= x) {
return ((a - n) * b);
} else {
n -= a - x;
a = x;
if (b - n >= y) {
return (a * (b - n));
} else {
b = y;
return (a * b);
}
}
}
static int high(int n) {
int p = (int) (Math.log(n) / Math.log(2));
return (int) Math.pow(2, p);
}
static int bs(long[] pow, long minus, long target, int low) {
int idx = -1;
int high = pow.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (pow[mid] == target) {
idx = mid;
break;
} else if (pow[mid] - minus < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return idx;
}
static boolean prime(int n) {
int m = (int) Math.sqrt(n);
for (int i = 2; i <= m; i++)
if (n % i == 0)
return false;
return true;
}
public static void main(String[] args) {
StringBuilder str = new StringBuilder();
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int xx = 0; xx < t; xx++) {
int n = sc.nextInt();
int x = sc.nextInt();
long[] arr = new long[n + 1];
boolean ok = true;
for (int i = 1; i <= n; i++) {
arr[i] = sc.nextLong();
if (arr[i] < 0)
ok = false;
}
if (!ok) {
for (int i = 1; i <= n; i++)
arr[i] += arr[i - 1];
long[] ar = new long[n + 1];
for (int i = 1; i <= n; i++) {
long max1 = Long.MIN_VALUE;
for (int j = i; j <= n; j++) {
max1 = Math.max(max1, arr[j] - arr[j - i]);
}
ar[i] = max1;
}
for (int i = 0; i <= n; i++) {
long max = 0;
for (int j = 0; j < ar.length; j++) {
max = Math.max(ar[j] + (x * Math.min(j, i)), max);
}
str.append(max + " ");
}
} else {
long sum = 0;
for (int i = 0; i <= n; i++)
sum += arr[i];
for (int i = 0; i <= n; i++) {
str.append(sum + (x * i) + " ");
}
}
str.append("\n");
}
System.out.println(str);
sc.close();
}
} | Java | ["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"] | 2 seconds | ["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"] | NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. | Java 11 | standard input | [
"brute force",
"dp",
"greedy",
"implementation"
] | a5927e1883fbd5e5098a8454f6f6631f | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$. | 1,400 | For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | standard output | |
PASSED | 2d7f039b2e2f7f71ffc974bd4c204589 | train_108.jsonl | 1645540500 | You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int x = in.nextInt();
int[] a = new int[n];
int[] msum = new int[n + 1];
for (int j = 0; j < n; j++) {
a[j] = in.nextInt();
}
for (int j = 1; j <= n; j++) {
msum[j] = Integer.MIN_VALUE;
}
for (int j = 0; j < n; j++) {
int sum = 0;
for (int y = j; y < n; y++) {
sum += a[y];
msum[y - j + 1] = Math.max(sum, msum[y - j + 1]);
}
}
for (int j = 1; j <= n; j++) {
for (int y = j; y <= n; y++) {
int sum = msum[y];
msum[j] = Math.max(sum + x * j, msum[j]);
}
msum[j] = Math.max(msum[j - 1], msum[j]);
}
System.out.print(Math.max(msum[1] - x, 0) + " ");
for (int j = 1; j <= n; j++) {
System.out.print(msum[j] + " ");
}
System.out.println();
}
}
}
| Java | ["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"] | 2 seconds | ["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"] | NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. | Java 11 | standard input | [
"brute force",
"dp",
"greedy",
"implementation"
] | a5927e1883fbd5e5098a8454f6f6631f | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$. | 1,400 | For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | standard output | |
PASSED | c4d2464f7bce4f25daa38da699f264c2 | train_108.jsonl | 1645540500 | You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int f = 0; f < t; f++) {
int n = in.nextInt();
int x = in.nextInt();
int[] v = new int[n + 5];
int[] dp = new int[n + 5];
for (int i = 1; i <= n; i++) v[i] = in.nextInt();
v[0] = 0;
dp[0] = 0;
for (int i = 1; i <= n; i++) {
v[i] += v[i - 1];
dp[i] = Integer.MIN_VALUE;
}
for(int i = 1;i <= n; i++) {
for(int j = i; j <= n; j++) {
dp[j - i + 1] = Math.max(dp[j - i + 1], v[j] - v[i - 1]);
}
}
for(int i = 0; i <= n; i++) {
int ans = 0;
for(int j = 1; j <= n; j++) {
ans = Math.max(ans, dp[j] + Math.min(j, i) * x);
}
System.out.print(ans + " ");
}
System.out.println();
}
}
}
| Java | ["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"] | 2 seconds | ["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"] | NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. | Java 11 | standard input | [
"brute force",
"dp",
"greedy",
"implementation"
] | a5927e1883fbd5e5098a8454f6f6631f | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$. | 1,400 | For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | standard output | |
PASSED | 6afa996b745156808a417b64e16f0d66 | train_108.jsonl | 1645540500 | You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
// -- static variables --- //
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static int mod = (int) 1000000007;
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
while (t-- > 0)
Main.go();
// out.println();
out.flush();
}
// >>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<< //
static class pair {
int x, y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
pair other = (pair) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
/*
* 1) Read the statements and abstract out the task.
* 2)Read the constraints carefully to note some limitations.
* 3)Do look at the sample values if you to be sure that what you have understood is correct.
* 4)Think about the problem and see if there is a connection between any previously solved problems.
* 5)Write any brute force approach first and try to optimise it.
* 6)Try some elimination process, try out by taking small examples and asking question to yourself.
* 7) Structure code before you write the code.
* 8)Debug the code, by trying to some few more testcase. Going through the code from beginning.
* 9)Don't stuck on a particular idea.
* 10)Try same problem with some different idea.
*/
static void go() throws Exception {
int n=sc.nextInt(),x=sc.nextInt();
int a[]=sc.intArray(n);
int left=0,right=n-1;
ArrayList<Integer> req=new ArrayList<>();
for(int k=n;k>=0;k--) {
req.add(maxSum(a,n,k));
}
Collections.reverse(req);
// out.println(req);
ArrayList<Integer> ans=new ArrayList<>();
for(int i=n;i>=0;i--) {
int max=0;
for(int j=n;j>=0;j--) {
if(j<i) {
max=Math.max(req.get(j)+j*x,max);
}else {
max=Math.max(req.get(j)+i*x,max);
}
// out.println(max);
}
ans.add(max);
}
Collections.reverse(ans);
for(int i:ans)out.print(i+" ");
out.println();
}
public static int maxSum(int arr[], int n, int k)
{
// k must be smaller than n
if (n < k)
{
// System.out.println("Invalid");
return -1;
}
// Compute sum of first window of size k
HashMap<pair,Integer> map=new HashMap<>();
int res = 0;
for (int i=0; i<k; i++)
res += arr[i];
map.put(new pair(0,k-1),res);
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
int curr_sum = res;
for (int i=k; i<n; i++)
{
curr_sum += arr[i] - arr[i-k];
map.put(new pair(i-k+1,i),curr_sum);
res = Math.max(res, curr_sum);
}
return res;
}
static void addAll(int a[],int l,int r,int x) {
for(int i=l;i<=r;i++) {
a[i]+=x;
}
}
static int kadane(int a[],int n) {
int max=0;
int sum=0;
for(int i=0;i<n;i++) {
sum+=a[i];
max=Math.max(max,sum);
if(sum<0) {
sum=0;
}
}
return max;
}
static boolean check(int a[]) {
for(int i=2;i<a.length;i++) {
if(a[i]==a[i-1]+a[i-2]) {
return true;
}
}
return false;
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
// >>>>>>>>>>> Code Ends <<<<<<<<< //
// --For Rounding--//
static double round(double value, int places) {
if (places < 0)
throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
// ----Greatest Common Divisor-----//
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// --- permutations and Combinations ---//
static long fact[];
static long invfact[];
static long ncr(int n, int k) {
if (k < 0 || k > n) {
return 0;
}
long x = fact[n];
long y = fact[k];
long yy = fact[n - k];
long ans = (x / y);
ans = (ans / yy);
return ans;
}
// ---sieve---//
static int prime[] = new int[1000006];
// static void sieve() {
// Arrays.fill(prime, 1);
// prime[0] = 0;
// prime[1] = 0;
// for (int i = 2; i * i <= 1000005; i++) {
// if (prime[i] == 1)
// for (int j = i * i; j <= 1000005; j += i) {
// prime[j] = 0;
// }
// }
// }
// ---- Manual sort ------//
static void sort(long[] a) {
ArrayList<Long> aa = new ArrayList<>();
for (long i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static void sort(int[] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (int i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
// --- Fast exponentiation ---//
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x * res);
}
y /= 2;
x = (x * x);
}
return res;
}
// >>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<< //
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());
}
int[] intArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
return a;
}
long[] longArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"] | 2 seconds | ["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"] | NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. | Java 11 | standard input | [
"brute force",
"dp",
"greedy",
"implementation"
] | a5927e1883fbd5e5098a8454f6f6631f | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$. | 1,400 | For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | standard output | |
PASSED | 7ee3e9af12b5a78bead00ac983793c44 | train_108.jsonl | 1645540500 | You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
// Created by @thesupremeone on 22/02/22
public class C {
void solve() {
int ts = getInt();
for (int t = 1; t <= ts; t++) {
int n = getInt();
long x = getInt();
int[] arr = new int[n+1];
long[] prefix = new long[n+1];
for (int i = 1; i <= n; i++) {
arr[i] = getInt();
prefix[i] = prefix[i-1]+arr[i];
}
long[] sum = new long[n+1];
long max = Long.MIN_VALUE;
for (int k = n; k >= 1; k--) {
long s = Long.MIN_VALUE;
for (int l = 1; l <= n+1-k; l++) {
s = Math.max(s, prefix[l+k-1]-prefix[l-1]);
}
max = Math.max(max, s);
sum[k] = max;
}
long ans = Math.max(sum[1], 0);
print(ans);
for (int k = 1; k <= n; k++) {
ans = Math.max(ans, sum[k]+k*x);
print(" "+ans);
}
println("");
}
}
public static void main(String[] args) throws Exception {
if (isOnlineJudge()) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
new C().solve();
out.flush();
} else {
localJudge = new Thread();
in = new BufferedReader(new FileReader("input.txt"));
out = new BufferedWriter(new FileWriter("output.txt"));
localJudge.start();
new C().solve();
out.flush();
localJudge.suspend();
}
}
static boolean isOnlineJudge() {
try {
return System.getProperty("ONLINE_JUDGE") != null
|| System.getProperty("LOCAL") == null;
} catch (Exception e) {
return true;
}
}
// Fast Input & Output
static Thread localJudge = null;
static BufferedReader in;
static StringTokenizer st;
static BufferedWriter out;
static String getLine() {
try {
return in.readLine();
} catch (Exception ignored) {
return "";
}
}
static String getToken() {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(getLine());
return st.nextToken();
}
static int getInt() {
return Integer.parseInt(getToken());
}
static long getLong() {
return Long.parseLong(getToken());
}
static void print(Object s) {
try {
out.write(String.valueOf(s));
} catch (Exception ignored) {
}
}
static void println(Object s) {
try {
out.write(String.valueOf(s));
out.newLine();
} catch (Exception ignored) {
}
}
} | Java | ["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"] | 2 seconds | ["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"] | NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. | Java 11 | standard input | [
"brute force",
"dp",
"greedy",
"implementation"
] | a5927e1883fbd5e5098a8454f6f6631f | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$. | 1,400 | For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | standard output | |
PASSED | add7615fc5697dee56a990b194bebe57 | train_108.jsonl | 1645540500 | You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
// Created by @thesupremeone on 22/02/22
public class C {
void solve() {
int ts = getInt();
for (int t = 1; t <= ts; t++) {
int n = getInt();
long x = getInt();
int[] arr = new int[n+1];
long[] prefix = new long[n+1];
for (int i = 1; i <= n; i++) {
arr[i] = getInt();
prefix[i] = prefix[i-1]+arr[i];
}
long[] sum = new long[n+1];
long max = Long.MIN_VALUE;
for (int k = n; k >= 1; k--) {
long s = Long.MIN_VALUE;
for (int l = 1; l <= n+1-k; l++) {
s = Math.max(s, prefix[l+k-1]-prefix[l-1]);
}
max = Math.max(max, s);
sum[k] = max;
}
long[] ans = new long[n+1];
ans[0] = Math.max(sum[1], 0);
for (int k = 1; k <= n; k++) {
long last = ans[k-1];
ans[k] = Math.max(last, ans[k]);
ans[k] = Math.max(ans[k], sum[k]+k*x);
}
for (int k = 0; k <= n; k++) {
print(ans[k]);
print(" ");
}
println("");
}
}
public static void main(String[] args) throws Exception {
if (isOnlineJudge()) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
new C().solve();
out.flush();
} else {
localJudge = new Thread();
in = new BufferedReader(new FileReader("input.txt"));
out = new BufferedWriter(new FileWriter("output.txt"));
localJudge.start();
new C().solve();
out.flush();
localJudge.suspend();
}
}
static boolean isOnlineJudge() {
try {
return System.getProperty("ONLINE_JUDGE") != null
|| System.getProperty("LOCAL") == null;
} catch (Exception e) {
return true;
}
}
// Fast Input & Output
static Thread localJudge = null;
static BufferedReader in;
static StringTokenizer st;
static BufferedWriter out;
static String getLine() {
try {
return in.readLine();
} catch (Exception ignored) {
return "";
}
}
static String getToken() {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(getLine());
return st.nextToken();
}
static int getInt() {
return Integer.parseInt(getToken());
}
static long getLong() {
return Long.parseLong(getToken());
}
static void print(Object s) {
try {
out.write(String.valueOf(s));
} catch (Exception ignored) {
}
}
static void println(Object s) {
try {
out.write(String.valueOf(s));
out.newLine();
} catch (Exception ignored) {
}
}
} | Java | ["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"] | 2 seconds | ["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"] | NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$. | Java 11 | standard input | [
"brute force",
"dp",
"greedy",
"implementation"
] | a5927e1883fbd5e5098a8454f6f6631f | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$. | 1,400 | For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.