Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH; import java.awt.geom.*; import java.util.*; import java.io.*; import java.math.*; import java.util.regex.*; public class Main { static InputReader in; public static void main(String[] args) throws IOException{ File file = new File("input.txt"); if( file.exists() )in = new InputReader( new FileInputStream( file ) ); else in = new InputReader( System.in ); int n=in.nextInt(); List<HashSet<Integer>> g=new ArrayList<>(n+1); for( int i=0; i<=n; i++ ) g.add( new HashSet<Integer>() ); for( int i=0; i<2*n; i++ ){ int u=in.nextInt(); int v=in.nextInt(); g.get( u ).add( v ); g.get( v ).add( u ); } for( int i=1; i<=n; i++ ) if( g.get(i).size()!=4 ) { System.out.println("-1"); return; } List<Integer> ans, arr=new ArrayList<>(); for( int ai : g.get( 1 ) ) arr.add( ai ); for( int mask=0; mask<(1<<4); mask++ ) { if( Integer.bitCount( mask )!=2 ) continue; int a=-1, b=-1; for( int i=0; i<4; i++ ) { if( (mask&(1<<i))!=0 ) { if( a==-1 ) a=arr.get(i); else b=arr.get(i); } } ans=getIt( a , b, g, n ); if( ans==null ) ans=getIt( b, a, g, n ); if( ans!=null ) { for( int ai : ans ) System.out.print( ai + " " ); return; } } System.out.println("-1"); } static List<Integer> getIt( int a, int b, List<HashSet<Integer>> g, int n ) { List<Integer> ans=new ArrayList<>(); boolean can=true; ans.add(a); ans.add(b); ans.add( 1 ); g.get( 1 ).remove( a ); g.get( 1 ).remove( b ); int cur=1; boolean used[]=new boolean[n+1]; used[1]=used[a]=used[b]=true; final int A=a, B=b; for( int i=1; can && i<=n-3; i++ ) { boolean ok=false; for( int next : g.get(cur) ) { if( !used[next] && g.get(next).contains(cur) && g.get(next).contains(b) ) { ok=true; b=cur; cur=next; ans.add(cur); used[cur]=true; break; } } if( !ok ) can=false; } g.get( 1 ).add( A ); g.get( 1 ).add( B ); if( can ) return ans; else return null; } // IO utilities: static void out( Object ...o ){ System.out.println( Arrays.deepToString( o ) ); } static class InputReader { private BufferedInputStream inp; private int offset; private final int size=5120000; private byte buff[]; InputReader( InputStream in ) throws IOException { inp = new BufferedInputStream( in ); buff=new byte[size]; offset=0; inp.read( buff, 0, size ); } private void copyLeftovers() throws IOException { int j = 0; for ( ; offset<size; j++, offset++ ) buff[j] = buff[offset]; // and now fill the buffer inp.read( buff, j, size - j ); } int nextInt() throws IOException { int parsedInt=0, by=1; int i=offset; if( buff[i]==0 ) throw new IOException(); //EOF // skip any non digits while ( i<size && ( (buff[i]<'0' || buff[i]>'9') && buff[i]!='-' ) ) i++; // read digits and parse number if( i<size && buff[i]=='-' ) { by=-1; i++; } while( i<size && buff[i]>='0' && buff[i]<='9') { parsedInt*=10; parsedInt+=buff[i]-'0'; i++; } // check if we reached end of buffer if ( i==size ) { // copy leftovers to buffer start copyLeftovers(); // and attempt to parse int again offset = 0; parsedInt = nextInt(); } else offset=i; return parsedInt*by; } long nextLong() throws IOException{ long parsedLong=0, by=1; int i=offset; if( buff[i]==0 ) throw new IOException(); //EOF // skip any non digits while ( i<size && ( (buff[i]<'0' || buff[i]>'9') && buff[i]!='-' ) ) i++; // read digits and parse number if( i<size && buff[i]=='-' ) { by=-1; i++; } while( i<size && buff[i]>='0' && buff[i]<='9') { parsedLong*=10L; parsedLong+=buff[i]-'0'; i++; } // check if we reached end of buffer if ( i==size ) { // copy leftovers to buffer start copyLeftovers(); // and attempt to parse long again offset = 0; parsedLong = nextLong(); } else offset=i; return parsedLong*by; } double nextDouble() throws IOException{ double parsedDouble=0, by=1; int i=offset; if( buff[i]==0 ) throw new IOException(); //EOF // skip any non digits while ( i<size && ( (buff[i]<'0' || buff[i]>'9') && buff[i]!='-' && buff[i]!='.' ) ) i++; // read digits and parse number if( i<size && buff[i]=='-' ) { by=-1; i++; } while( i<size && buff[i]>='0' && buff[i]<='9') { parsedDouble*=10; parsedDouble+=buff[i]-'0'; i++; } if( i<size && buff[i]=='.' ) { double div=10; i++; while( i<size && buff[i]>='0' && buff[i]<='9') { parsedDouble+=(buff[i]-'0')/div; div*=10; i++; } } // check if we reached end of buffer if ( i==size ) { // copy leftovers to buffer start copyLeftovers(); // and attempt to parse double again offset = 0; parsedDouble=nextDouble(); } else offset=i; return parsedDouble*by; } String next() throws IOException { StringBuilder token=new StringBuilder(); int i=offset; if( buff[i]==0 ) throw new IOException(); //EOF // skip any non chars while( i<size && ( buff[i]=='\n' || buff[i]==' ' || buff[i]=='\r' || buff[i]=='\t' ) ) i++; // read chars while( i<size && buff[i]!='\n' && buff[i]!=' ' && buff[i]!='\r' && buff[i]!='\t' && buff[i]!=0 ) { token.append( (char)buff[i] ); i++; } // check if we reached end of buffer if ( i==size ) { // copy leftovers to buffer start copyLeftovers(); // and attempt to parse again offset = 0; return next(); } else offset=i; return token.toString(); } int[] nextInts( int n ) throws IOException { int arr[]=new int[n]; for( int i=0; i<n; i++ ) arr[i]=in.nextInt(); return arr; } long[] nextLongs( int n ) throws IOException { long arr[]=new long[n]; for( int i=0; i<n; i++ ) arr[i]=in.nextLong(); return arr; } double[] nextDoubles( int n ) throws IOException { double arr[]=new double[n]; for( int i=0; i<n; i++ ) arr[i]=in.nextDouble(); return arr; } } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author FinnTheHuman */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { TreeSet<Integer>[] g; public TreeSet<Integer> intersect(TreeSet<Integer> a, TreeSet<Integer> b, boolean[] ok) { TreeSet<Integer> ret = new TreeSet<>(); for (int x : a) { if (b.contains(x) && !ok[x]) ret.add(x); } return ret; } boolean trywith(int a, int b, int c, PrintWriter out) { //out.println((1+a) + " " + (1+b) + " " + (1+c)); int n = g.length; int[] ans = new int[n]; boolean[] ok = new boolean[n]; ok[a] = ok[b] = ok[c] = true; ans[0] = a; ans[1] = b; ans[2] = c; for (int i = 3; i < n; ++i) { TreeSet<Integer> S = intersect(g[ans[i - 1]], g[ans[i - 2]], ok); if (S.isEmpty()) return false; ans[i] = S.first(); ok[ans[i]] = true; } for (int i = 0; i < n; ++i) { if (i > 0) out.print(" "); out.print(ans[i] + 1); } out.println(); return true; } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); g = new TreeSet[n]; int[] freqs = new int[n]; for (int i = 0; i < n; ++i) g[i] = new TreeSet<>(); for (int i = 0; i < 2 * n; ++i) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; g[u].add(v); g[v].add(u); ++freqs[u]; ++freqs[v]; } for (int i = 0; i < n; ++i) { if (freqs[i] != 4) { out.println(-1); return; } } for (int adj : g[0]) { for (int adjadj : g[adj]) { if (g[0].contains(adjadj)) { if (trywith(0, adj, adjadj, out)) return; } } } out.println(-1); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 65536 / 2); 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()); } } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int MAX_V = 100000; vector<int> adjlist[MAX_V + 5]; int N, x, y, component, sz, deg[MAX_V + 5], A[MAX_V + 5]; bool visit[MAX_V + 5]; void dfs(int u) { visit[u] = 1; for (int i = 0; i < (int)adjlist[u].size(); i++) { int v = adjlist[u][i]; if (!visit[v]) dfs(v); } } bool edge_exists(int u, int v) { for (int i = 0; i < (int)adjlist[u].size(); i++) if (adjlist[u][i] == v) return 1; return 0; } int main() { scanf("%d", &N); memset(deg, 0, sizeof deg); for (int i = 0; i < (int)2 * N; i++) { scanf("%d %d", &x, &y); adjlist[x].push_back(y); adjlist[y].push_back(x); deg[x]++, deg[y]++; } for (int u = (int)1; u <= (int)N; u++) if (deg[u] != 4) { puts("-1"); return 0; } memset(visit, 0, sizeof visit); component = 0; for (int u = (int)1; u <= (int)N; u++) if (!visit[u]) { dfs(u); component++; } if (component > 1) { puts("-1"); return 0; } if (N == 5) { puts("1 2 3 4 5"); } else if (N == 6) { for (int i = (int)1; i <= (int)N; i++) A[i] = i; bool found = 0; do { bool ok = 1; for (int i = (int)1; i <= (int)N; i++) { int next1 = (i < N) ? i + 1 : 1; int next2 = (i < N - 1) ? i + 2 : i + 2 - N; if (!edge_exists(A[i], A[next1])) { ok = 0; break; } if (!edge_exists(A[i], A[next2])) { ok = 0; break; } } if (!ok) continue; found = 1; for (int i = 0; i < (int)N; i++) { if (i) printf(" "); printf("%d", A[i + 1]); } puts(""); } while (next_permutation(A + 1, A + N + 1) && !found); if (!found) puts("-1"); } else { for (int u = (int)1; u <= (int)N; u++) { int counter1 = 0, counter2 = 0; for (int i = 0; i < (int)adjlist[u].size(); i++) { int cnt = 0; for (int j = 0; j < (int)adjlist[u].size(); j++) if (edge_exists(adjlist[u][i], adjlist[u][j])) cnt++; if (cnt == 1) counter1++; else if (cnt == 2) counter2++; } if (counter1 != 2 || counter2 != 2) { puts("-1"); return 0; } } sz = 0; for (int i = 0; i < (int)adjlist[1].size(); i++) { int cnt = 0; for (int j = 0; j < (int)adjlist[1].size(); j++) if (edge_exists(adjlist[1][i], adjlist[1][j])) cnt++; if (cnt == 2) A[sz++] = adjlist[1][i]; if (sz == 1) A[sz++] = 1; } while (sz < N) { for (int i = 0; i < (int)adjlist[A[sz - 1]].size(); i++) { int u = adjlist[A[sz - 1]][i]; if (edge_exists(A[sz - 2], u) && u != A[sz - 3]) { A[sz++] = u; break; } } } for (int i = 0; i < (int)sz; i++) { if (i) printf(" "); printf("%d", A[i]); } puts(""); } }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> a[100020]; int edgeu[100020 * 2], edgev[100020 * 2]; bool chose[100020]; vector<int> apart[100020]; int cnt[100020], ans[100020]; bool vis[100020]; int n, m = 0; void Read() { int i, u, v; scanf("%d", &n); for (i = 1; i <= n * 2; i++) { scanf("%d%d", &u, &v); a[u].push_back(v); a[v].push_back(u); edgeu[i] = u; edgev[i] = v; } } bool Check() { int i; for (i = 1; i <= n; i++) if (a[i].size() != 4) return false; return true; } bool Connect(int u, int v, int k) { int i, j; for (i = 0; i < apart[u].size(); i++) for (j = 0; j < apart[v].size(); j++) if (apart[u][i] == k && apart[v][j] == k) return true; return false; } void Dfs_Get(int u, int fa) { if (m >= n) return; if (vis[u]) return; vis[u] = true; ans[++m] = u; int i; for (i = 0; i < apart[u].size(); i++) if (apart[u][i] != fa) { Dfs_Get(apart[u][i], u); return; } } bool Got_it() { int i, j; bool flag = true; for (i = 1; i <= (n << 1); i++) { if (!chose[i]) { cnt[edgeu[i]]++; cnt[edgev[i]]++; } } for (i = 1; i <= n; i++) { if (cnt[i] != 2) flag = false; cnt[i] = 0; apart[i].clear(); } if (!flag) return false; for (i = 1; i <= (n << 1); i++) { if (!chose[i]) { apart[edgeu[i]].push_back(edgev[i]); apart[edgev[i]].push_back(edgeu[i]); } } for (i = 1; i <= (n << 1); i++) { if (chose[i]) { flag = false; for (j = 1; j <= n; j++) if (j != edgeu[i] && j != edgev[i] && Connect(edgeu[i], edgev[i], j)) { flag = true; break; } if (!flag) return false; } } Dfs_Get(1, -1); if (m != n) return false; for (i = 1; i <= m; i++) printf("%d ", ans[i]); printf("\n"); return true; } void Dfs(int i) { if (i > 2 * n) { bool flag = Got_it(); if (flag) exit(0); return; } chose[i] = true; Dfs(i + 1); chose[i] = false; Dfs(i + 1); } void Work() { int i, j, k, v, cnt; for (i = 1; i <= n; i++) { for (j = 0; j < a[i].size(); j++) vis[a[i][j]] = true; for (j = 0; j < a[i].size(); j++) { v = a[i][j]; cnt = 0; for (k = 0; k < a[v].size(); k++) if (vis[a[v][k]]) cnt++; if (cnt == 2) apart[i].push_back(v); else if (cnt != 1) return; } for (j = 0; j < a[i].size(); j++) vis[a[i][j]] = false; } Dfs_Get(1, -1); if (m != n) return; for (i = 1; i <= m; i++) printf("%d ", ans[i]); printf("\n"); exit(0); } int main() { Read(); bool flag = Check(); if (flag) { if (n <= 10) { Dfs(1); printf("-1\n"); } else { Work(); printf("-1\n"); } } else printf("-1\n"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int i, j, n, k, c, x, y, c1, c2, c3, z; bool u[100009]; vector<int> a[100009]; int ans[100009]; bool res; bool cmp(int a, int b) { return a < b; } void search() { int i, j, k; bool p1, p2; for (i = 0; i < 100009; i++) { if (a[i].size() != 0) { c1 = i; c2 = a[i][0]; for (j = 1; j < a[i].size(); j++) { if (a[i][j] == i) continue; p1 = false; p2 = false; for (k = 0; k < a[a[i][j]].size(); k++) { if (a[a[i][j]][k] == c1) p1 = true; if (a[a[i][j]][k] == c2) p2 = true; } if (p1 && p2) { c3 = a[i][j]; break; } } return; } } } bool go(int p, int f, int t, int tt) { int i, l = a[t].size(); for (i = 0; i < l; i++) { if (a[t][i] == p) { tt++; if (tt == n) { ans[z] = t; u[t] = true; z--; return true; } int j, l2; u[t] = true; for (j = 0; j < l; j++) if (a[t][j] != p && a[t][j] != f && !u[a[t][j]] && go(f, t, a[t][j], tt)) { ans[z] = t; u[t] = true; z--; return true; } u[t] = false; return false; } } return false; } int tt; int main() { scanf("%d", &n); k = 2 * n; for (i = 0; i < k; i++) { scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } for (i = 1; i < n; i++) { if (a[i].size() != 4) { cout << -1; return 0; } } search(); res = false; u[c1] = true; u[c2] = true; ans[0] = c1; ans[1] = c2; z = n - 1; go(c1, c2, c3, 2); if (z == 1) res = true; if (!res) { memset(u, 0, sizeof(u)); u[c1] = true; u[c3] = true; ans[0] = c1; ans[1] = c3; tt = 2; z = n - 1; go(c1, c3, c2, 2); if (z == 1) res = true; } if (!res) { memset(u, 0, sizeof(u)); u[c3] = true; u[c2] = true; ans[0] = c3; ans[1] = c2; tt = 2; z = n - 1; go(c3, c2, c1, 2); if (z == 1) res = true; } if (!res) { memset(u, 0, sizeof(u)); u[c3] = true; u[c1] = true; ans[0] = c3; ans[1] = c1; tt = 2; z = n - 1; go(c3, c1, c2, 2); if (z == 1) res = true; } if (!res) { cout << -1; } else { for (i = 0; i < n; i++) cout << ans[i] << " "; } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int n; int ans[100100]; vector<int> g[100100]; int vis[100100]; bool judge1(int cur, int val) { if (cur <= 2) return true; int i, j; int pre = cur - 2; int k = ans[pre]; for (i = 0; i < 4; i++) { if (g[k][i] == val) { return true; } } return false; } bool judge2() { int i, j; int x = ans[2]; int y = ans[n]; for (i = 0; i < 4; i++) { if (g[x][i] == y) { break; } } if (i == 5) { return false; } x = ans[1], y = ans[n - 1]; for (i = 0; i < 4; i++) { if (g[x][i] == y) { return true; } } return false; } bool dfs(int cur, int val) { int i, j; if (cur == n) { if (judge2()) { return true; } return false; } for (i = 0; i < 4; i++) { int y = g[val][i]; if (!vis[y] && judge1(cur + 1, y)) { vis[y] = 1; ans[cur + 1] = y; if (dfs(cur + 1, y)) { return true; } vis[y] = 0; } } return false; } int main() { int i, j; cin >> n; for (i = 0; i < 2 * n; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (i = 1; i <= n; i++) { if (g[i].size() != 4) { puts("-1"); return 0; } } memset(vis, 0, sizeof(vis)); ans[1] = 1; vis[1] = 1; if (dfs(1, 1)) { for (i = 1; i <= n; i++) { cout << ans[i]; if (i == n) { cout << endl; } else { cout << " "; } } } else { puts("-1"); } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int a[500500], n, x, y, used[500500]; vector<int> g[500500]; set<pair<int, int> > s; int main() { scanf("%d", &n); for (int i = 0; i < n + n; i++) { scanf("%d%d", &x, &y); --x; --y; g[x].push_back(y); g[y].push_back(x); s.insert(make_pair(x, y)); s.insert(make_pair(y, x)); } for (int i = 0; i < n; i++) if (g[i].size() != 4) { puts("-1"); return 0; } for (int i1 = 0; i1 < 4; i1++) for (int i2 = 0; i2 < 4; i2++) if (i1 != i2) { memset(used, 0, sizeof(used)); a[0] = 0; a[1] = g[0][i1]; a[2] = g[0][i2]; used[a[0]] = 1; used[a[1]] = 1; used[a[2]] = 1; for (int j = 3; j < n; j++) { a[j] = -1; for (int t = 0; t < 4; t++) if ((s.find(make_pair(a[j - 2], g[a[j - 1]][t])) != s.end()) && (!used[g[a[j - 1]][t]])) { used[g[a[j - 1]][t]] = 1; a[j] = g[a[j - 1]][t]; break; } if (a[j] < 0) { a[0] = -1; break; } } if (a[0] < 0) continue; for (int j = 0; j < n; j++) printf("%d ", a[j] + 1); puts(""); return 0; } puts("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.util.*; import java.io.*; import java.math.BigInteger; public class Solution { BufferedReader in; PrintWriter out; StringTokenizer st; String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(nextToken()); } long nextLong() throws Exception { return Long.parseLong(nextToken()); } void solve() throws Exception { int n = nextInt(); int[] st = new int[n]; Set<Integer>[] a = new TreeSet[n]; for (int i = 0; i < n; i++) { a[i] = new TreeSet<Integer>(); } for (int i = 0; i < 2 * n; i++) { int q = nextInt() - 1; int w = nextInt() - 1; st[q]++; st[w]++; a[q].add(w); a[w].add(q); } boolean fuck = false; for (int i = 0; i < n; i++) if (st[i] != 4) fuck = true; if (fuck) { out.println(-1); return; } int[] ans = new int[n]; boolean[] w = new boolean[n]; if (n == 5) { for (int i = 0; i < n; i++) ans[i] = i; } else if (n == 6) { ans[0] = 0; w[0] = true; int q = 0; for (int i = 0; i < 3; i++) { int j = 1; while (j == q || a[q].contains(j)) j++; w[j] = true; ans[i + 3] = j; if (i == 2) break; while (w[q]) q++; w[q] = true; ans[i + 1] = q; } } else { ans[0] = 0; main: for (int x : a[0]) { for (int y : a[0]) if (y != x) { for (int z : a[0]) if (z != y && z != x) { for (int t : a[0]) if (t != x && t != y && t != z) { if (a[x].contains(y) && a[x].contains(z) && !a[x].contains(t) && !a[y].contains(z) && !a[y].contains(t)) { ans[1] = x; ans[2] = y; break main; } } } } } for (int i = 3; i < n; i++) { for (int x : a[ans[i - 1]]) if (x != ans[i - 2] && x != ans[i - 3] && a[x].contains(ans[i - 2])) { ans[i] = x; break; } } } Arrays.fill(w, false); boolean f = true; for (int i = 0; i < n; i++) { w[i] = true; int j = (i + 1) % n; if (!a[ans[i]].contains(ans[j]) || !a[ans[j]].contains(ans[i])) { f = false; break; } j = (i + 2) % n; if (!a[ans[i]].contains(ans[j]) || !a[ans[j]].contains(ans[i])) { f = false; break; } } for (int i = 0; i < n; i++) f &= w[i]; if (f) { for (int i = 0; i < n; i++) out.print((ans[i] + 1) + " "); } else { out.println(-1); } } void run() { try { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { out.close(); } } public static void main(String[] args) { new Solution().run(); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; vector<int> road[100011]; bool vis[100011]; bool find(int p, int a, int b, int c, int d) { for (int i = 0; i < 4; i++) if (road[p][i] != a && road[p][i] != b && road[p][i] != c && road[p][i] != d) return 0; return 1; } int now[100011] = {0}; int main() { int n; while (scanf("%d", &n) > 0) { for (int i = 0; i < 2 * n; i++) { int t1, t2; scanf("%d%d", &t1, &t2); road[t1].push_back(t2); road[t2].push_back(t1); } for (int i = 1; i <= n; i++) if (road[i].size() != 4) { puts("-1"); return 0; } int a[10] = {0}; for (int i = 0; i < 4; i++) a[i] = road[1][i]; sort(a, a + 4); do { bool can = 1; memset(vis, 0, sizeof(vis)); now[0] = a[0]; now[1] = a[1]; now[2] = 1; now[3] = a[2]; now[4] = a[3]; for (int i = 0; i <= 4; i++) vis[now[i]] = 1; for (int i = 5; i < n; i++) { int tmp = -1; for (int j = 0; j < 4; j++) if (vis[road[now[i - 2]][j]] == 0) { tmp = road[now[i - 2]][j]; break; } if (tmp == -1) { can = 0; goto loop2; } vis[tmp] = 1; now[i] = tmp; } for (int i = 0; i < n; i++) { if (!find(now[i], now[(i - 2 + n) % n], now[(i - 1 + n) % n], now[(i + 1) % n], now[(i + 2) % n])) { can = 0; break; } } if (can) { for (int i = 0; i < n; i++) printf("%d ", now[i]); puts(""); goto loop; } loop2:; } while (next_permutation(a, a + 4)); puts("-1"); loop:; } return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; set<int> G[maxn]; bool v[maxn]; int cnt[maxn]; vector<int> ans; void dfs(int now, int p) { v[now] = true; ans.emplace_back(now); for (int u : G[now]) if (!v[u]) { if (p && G[p].count(u) == 0) continue; int cnt = 0; for (int k : G[now]) cnt += G[u].count(k); if (cnt >= 2) { dfs(u, now); return; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n << 1; ++i) { int a, b; cin >> a >> b; G[a].insert(b); G[b].insert(a); ++cnt[a]; ++cnt[b]; } for (int i = 1; i <= n; ++i) if (cnt[i] < 4) return cout << "-1" << endl, 0; dfs(1, 0); if (ans.size() != n) return cout << "-1" << endl, 0; for (int i : ans) cout << i << ' '; cout << endl; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, head[N], pre[N << 2], nxt[N << 2], e, deg[N], ans[N]; bool vis[N]; set<pair<int, int> > st; void init() { memset(head, -1, sizeof(head)); e = 0; } void add(int u, int v) { pre[e] = v, nxt[e] = head[u], head[u] = e++; } bool judge() { int i, j, x[4], y[4]; for (i = 1; i <= n; ++i) if (deg[i] != 4) return false; int u = 1, T = 0; while (1) { int m = 0; for (i = head[u]; ~i; i = nxt[i]) { x[m++] = pre[i]; } memset(y, 0, sizeof(y)); bool f = true; for (i = 0; i < 4; ++i) { for (j = i + 1; j < 4; ++j) { if (st.count(make_pair(min(x[i], x[j]), max(x[i], x[j])))) { ++y[i]; ++y[j]; } } } for (i = 0; i < 4; ++i) if (y[i] == 2) { if (!vis[x[i]]) { ans[T++] = u; vis[u] = 1; u = x[i]; f = false; break; } } if (f) { ans[T++] = u; break; } } if (T != n) return false; return true; } bool dfs(int lef) { int i; if (lef == 0) { for (i = 1; i <= n; ++i) { int a = min(ans[i], ans[i % n + 1]); int b = max(ans[i], ans[i % n + 1]); if (st.count(make_pair(a, b)) == 0) return false; a = min(ans[i], ans[(i + 1) % n + 1]); b = max(ans[i], ans[(i + 1) % n + 1]); if (st.count(make_pair(a, b)) == 0) return false; } for (i = 1; i <= n; ++i) printf("%d ", ans[i]); puts(""); return true; } for (i = 1; i <= n; ++i) if (!vis[i]) { vis[i] = 1; ans[lef] = i; if (dfs(lef - 1)) return true; vis[i] = 0; } return false; } int main() { cin >> n; init(); int i, u, v; for (i = 0; i < n * 2; ++i) { scanf("%d%d", &u, &v); add(u, v); add(v, u); ++deg[u], ++deg[v]; st.insert(make_pair(min(u, v), max(u, v))); } if (n <= 6) { if (!dfs(n)) puts("-1"); return 0; } if (judge()) { for (i = 0; i < n; ++i) printf("%d ", ans[i]); puts(""); } else puts("-1"); return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
#include <bits/stdc++.h> using namespace std; int adj[111111][4]; int edge[222222][2]; int n; void build() { memset(adj, 0, sizeof(adj)); for (int i = 0; i < n + n; i++) { for (int j = 0; j <= 1; j++) { int f = edge[i][j]; int t = edge[i][1 - j]; if (adj[f][0] == 0) adj[f][0] = t; else if (adj[f][1] == 0) adj[f][1] = t; else if (adj[f][2] == 0) adj[f][2] = t; else if (adj[f][3] == 0) adj[f][3] = t; } } } bool judge(vector<int> &ans) { for (int i = 3; i <= n + 10; i++) { int now = ans[i]; int used[4] = {0}; for (int j = -2; j <= 1; j++) { int next = ans[i + j]; for (int k = 0; k < 4; k++) { if (next == adj[now][k]) used[k] = 1; } } if (used[0] + used[1] + used[2] + used[3] != 3) return false; for (int j = 0; j < 4; j++) { if (used[j] == 0) { ans.push_back(adj[now][j]); } } } vector<int> tmp(ans); sort(tmp.begin(), tmp.end()); int cnt = unique(tmp.begin(), tmp.end()) - tmp.begin(); if (cnt != n) return false; if (ans[0] == ans[n] && ans[1] == ans[n + 1] && ans[2] == ans[n + 2] && ans[3] == ans[n + 3] && ans[4] == ans[n + 4]) return true; return false; } int main() { scanf("%d", &n); for (int i = 0; i < n + n; i++) { int f, t; scanf("%d%d", &f, &t); edge[i][0] = f; edge[i][1] = t; } build(); int init[] = {1, adj[1][0], adj[1][1], adj[1][2], adj[1][3]}; sort(init, init + 5); do { if (init[2] != 1) continue; vector<int> ans(init, init + 5); if (judge(ans)) { for (int i = 0; i < n; i++) { printf("%d ", ans[i]); } return 0; } } while (next_permutation(init, init + 5)); puts("-1"); return 0; return 0; }
CPP
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import static java.util.Arrays.*; import java.io.*; import java.lang.reflect.*; public class C { final int MOD = (int)1e9 + 7; final double eps = 1e-12; final int INF = (int)1e9; int [][] L; int N; public C () { N = sc.nextInt(); L = new int [N+1][4]; int [] C = new int [N+1]; if (N == 5) exit(1, 2, 3, 4, 5); for (int i = 0; i < 2*N; ++i) { int x = sc.nextInt(); int y = sc.nextInt(); try { L[x][C[x]++] = y; L[y][C[y]++] = x; } catch (ArrayIndexOutOfBoundsException e) { exit(-1); } } int [][] M = new int [4][2]; for (int i = 0; i < 4; ++i) { int m = L[1][i]; for (int j = 0; j < 4; ++j) { int n = L[1][j]; for (int k = 0; k < 4; ++k) { int r = L[m][k]; if (r == n && r != m) M[i][0] = r; } } for (int j = 0; j < 4; ++j) { int n = L[1][j]; for (int k = 0; k < 4; ++k) { int r = M[i][0], s = L[m][k]; if (s == n && s != r && s != m) M[i][1] = s; } } } for (int i = 0; i < 4; ++i) for (int j = 0; j < 2; ++j) { int [] res = solve(1, L[1][i], M[i][j]); if (res != null) exit(res); } exit(-1); } int [] solve(int a, int b, int c) { if (c == 0) return null; int [] res = new int [N+3]; res[0] = a; res[1] = b; res[2] = c; for (int k = 3; k < N+3; ++k) { out: for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) { int x = L[res[k-1]][i]; int y = L[res[k-2]][j]; if (x == y && x != res[k-3]) { res[k] = x; break out; } } if (res[k] == 0) return null; } if (res[N] != a) return null; if (res[N+1] != b) return null; if (res[N+2] != c) return null; return copyOf(res, N); } //////////////////////////////////////////////////////////////////////////////////// static MyScanner sc; static class MyScanner { public String next() { newLine(); return line[index++]; } public char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { line = null; return readLine(); } public String [] nextStrings() { line = null; return readLine().split(" "); } public char [] nextChars() { return next().toCharArray(); } public Integer [] nextInts() { String [] L = nextStrings(); Integer [] res = new Integer [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } public Long [] nextLongs() { String [] L = nextStrings(); Long [] res = new Long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } public Double [] nextDoubles() { String [] L = nextStrings(); Double [] res = new Double [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Double.parseDouble(L[i]); return res; } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error(e); } } private final BufferedReader r; MyScanner () { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = readLine().split(" "); index = 0; } } } static void print (Object o, Object... a) { pw.println(build(o, a)); } static void exit (Object o, Object... a) { print(o, a); exit(); } static void exit () { pw.close(); System.out.flush(); System.err.println("------------------"); System.err.println("Time: " + ((millis() - t) / 1000.0)); System.exit(0); } void NO() { throw new Error("NO!"); } //////////////////////////////////////////////////////////////////////////////////// static String build(Object... a) { StringBuilder b = new StringBuilder(); for (Object o : a) append(b, o); return b.toString().trim(); } static void append(StringBuilder b, Object o) { if (o.getClass().isArray()) { int L = Array.getLength(o); for (int i = 0; i < L; ++i) append(b, Array.get(o, i)); } else if (o instanceof Iterable<?>) { for (Object p : (Iterable<?>)o) append(b, p); } else b.append(" ").append(o); } //////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { sc = new MyScanner (); new C(); exit(); } static void start() { t = millis(); } static PrintWriter pw = new PrintWriter(System.out); static long t; static long millis() { return System.currentTimeMillis(); } }
JAVA
263_C. Circle of Numbers
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
2
9
import java.util.*; public class NumbersCircleSolver { private int n; private List<List<Integer>> a; public static void main(String[] args) { NumbersCircleSolver solver = new NumbersCircleSolver(); solver.readData(); List<Integer> result = solver.solve(); if (result.isEmpty()) { System.out.println("-1"); } else { System.out.println(result.toString().replace("[", "").replace("]", "").replace(",", "")); } } private List<Integer> solve() { List<Integer> result = new ArrayList<Integer>(n); List<Integer> edges = a.get(0); if (edges.size() != 4) { return Collections.emptyList(); } else { int[] degrees = getDegrees(edges); if (n == 5 && validate(degrees, 3, 3, 3, 3)) { result.add(1); result.add(2); result.add(3); result.add(4); result.add(5); return result; } if (n == 6 && validate(degrees, 2, 2, 2, 2)) { List<Integer> z = new ArrayList<Integer>(); z.add(0); z.add(1); z.add(2); z.add(3); z.add(4); z.add(5); z.removeAll(edges); z.remove((Integer)0); int[] d = getDegrees(a.get(z.get(0))); if (!validate(d, 2, 2, 2, 2)) { return Collections.emptyList(); } else { result.add(edges.get(0) + 1); result.add(edges.get(1) + 1); result.add(1); result.add(edges.get(2) + 1); result.add(edges.get(3) + 1); result.add(z.get(0) + 1); return result; } } if (!validate(degrees, 1, 1, 2, 2)) { return Collections.emptyList(); } int firstIndex = getOneDegree(degrees, 0); int first = edges.get(firstIndex); int fourth = edges.get(getOneDegree(degrees, firstIndex + 1)); int second = getNeighbour(first, edges); int third = getNeighbour(fourth, edges); result.add(first + 1); result.add(second + 1); result.add(1); result.add(third + 1); result.add(fourth + 1); } int index = 3; while (index < n - 2) { List<Integer> e = new ArrayList<Integer>(a.get(result.get(index) - 1)); if (e.size() != 4) { return Collections.emptyList(); } e.remove((Integer)(result.get(index - 1) - 1)); e.remove((Integer)(result.get(index - 2) - 1)); e.remove((Integer)(result.get(index + 1) - 1)); int next = e.get(0); if (hasEdge(result.get(index + 1) - 1, next) == 0 || next == result.get(0) - 1 || next == result.get(1) - 1) { return Collections.emptyList(); } result.add(next + 1); index++; } if (hasEdge(result.get(n - 2) - 1, result.get(0) - 1) == 0 || hasEdge(result.get(n - 1) - 1, result.get(1) - 1) == 0) { return Collections.emptyList(); } return result; } private int[] getDegrees(List<Integer> edges) { int x01 = hasEdge(edges.get(0), edges.get(1)); int x02 = hasEdge(edges.get(0), edges.get(2)); int x03 = hasEdge(edges.get(0), edges.get(3)); int x12 = hasEdge(edges.get(1), edges.get(2)); int x13 = hasEdge(edges.get(1), edges.get(3)); int x23 = hasEdge(edges.get(2), edges.get(3)); return new int[]{x01 + x02 + x03, x01 + x12 + x13, x02 + x12 + x23, x03 + x13 + x23}; } private boolean validate(int[] degrees, int... result) { int[] a = new int[degrees.length]; System.arraycopy(degrees, 0, a, 0, degrees.length); Arrays.sort(a); return Arrays.equals(a, result); } private int getOneDegree(int[] degrees, int start) { for (int i = start; i < degrees.length; i++) { if (degrees[i] == 1) { return i; } } return -1; } private int getNeighbour(int v, List<Integer> neighbours) { for (int i = 0; i < neighbours.size(); i++) { if (hasEdge(v, neighbours.get(i)) == 1) { return neighbours.get(i); } } return -1; } private int hasEdge(int x, int y) { return a.get(x).contains(y) ? 1 : 0; } private void readData() { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); a = new ArrayList<List<Integer>>(n); for (int i = 0; i < n; i++) { a.add(new ArrayList<Integer>()); } for (int i = 0; i < 2 * n; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); a.get(x - 1).add(y - 1); a.get(y - 1).add(x - 1); } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if ((n / 2) % 2 > 0) { cout << -1; return 0; } for (int i = 1; i <= n / 4; i++) { cout << i * 2 << ' ' << n - (i - 1) * 2 << ' '; } if (n % 2 != 0) cout << (n + 1) / 2 << ' '; for (int i = n / 4; i >= 1; i--) { cout << i * 2 - 1 << ' ' << n - (i - 1) * 2 - 1 << ' '; } }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int N, res; int ar[MAXN]; int main() { cin >> N; if (N % 4 > 1) { cout << -1 << endl; return 0; } for (int i = 1, k = 0; k + 4 <= N; i += 2, k += 4) { ar[i] = i + 1; ar[i + 1] = N - i + 1; ar[N - i + 1] = N - i; ar[N - i] = i; } if (N % 2) ar[N / 2 + 1] = N / 2 + 1; for (int i = 1; i <= N; i++) printf("%d ", ar[i]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; int temp = n / 2; if (temp % 2 != 0) { cout << -1 << endl; return 0; } int arr[100010]; if (n % 2 == 1) { arr[(n / 2) + 1] = n / 2 + 1; } for (int i = 1; i <= n / 2; i++) { int temp1 = i; int temp2 = n - i + 1; int temp3 = i + 1; int temp4 = n - (i + 1) + 1; arr[temp1] = temp3; arr[temp3] = temp2; arr[temp2] = temp4; arr[temp4] = temp1; i++; } for (int i = 0; i < n; i++) { cout << arr[i + 1] << ' '; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int g[100100]; int main() { int n; scanf("%d", &n); if (((n & 1) && (n - 1) % 4 != 0) || ((n & 1) == 0 && n % 4 != 0)) printf("-1\n"); else { int ini = n / 2; for (int i = 1; i < n + 1; ++i) g[i] = -1; if (n & 1) g[n / 2 + 1] = ini + 1; for (int i = 1; i < n + 1; ++i) { if (g[i] == -1) { g[i] = ini; g[ini] = n + 1 - i; g[n + 1 - i] = n + 1 - ini; g[n + 1 - ini] = i; ini--; } } for (int i = 1; i < n + 1; ++i) { if (i > 1) printf(" "); printf("%d", g[i]); } printf("\n"); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int N, P[200001]; int main() { cin >> N; if (N & 1) { if (N % 4 == 1) { for (int i = 1; i <= N / 2; i += 2) { P[i] = i + 1; P[i + 1] = N - i + 1; P[N - i + 1] = N - i; P[N - i] = i; } P[N / 2 + 1] = N / 2 + 1; for (int i = 1; i < N; ++i) cout << P[i] << " "; cout << P[N] << endl; } else cout << -1 << endl; } else { if (N % 4 == 0) { for (int i = 1; i <= N / 2; i += 2) { P[i] = i + 1; P[i + 1] = N - i + 1; P[N - i + 1] = N - i; P[N - i] = i; } for (int i = 1; i < N; ++i) cout << P[i] << " "; cout << P[N] << endl; } else cout << -1 << endl; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) cout << "-1"; else { int a[n + 1]; a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i < n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } for (int i = 1; i <= n; i++) cout << a[i] << ' '; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int A[111111]; int main(void) { int n; int a = 2, b; cin >> n; if (n % 4 > 1) { cout << -1 << endl; return 0; } b = n; for (int i = 0; i < n / 2; i++) { if (i % 2 == 0) { A[i] = a; a += 2; } else { A[i] = b; b -= 2; } } a = 1; b = n - 1; for (int i = n - 1; i >= n / 2; i--) { if (i % 2 != n % 2) { A[i] = b; b -= 2; } else { A[i] = a; a += 2; } } if (n % 2) A[n / 2] = n / 2 + 1; for (int i = 0; i < n; i++) { cout << A[i] << " "; } cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.awt.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; import com.sun.org.apache.bcel.internal.generic.LLOAD; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; static int[] dx = new int[]{-1,0,1,0}; static int[] dy = new int[]{0,1,0,-1}; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { int n = readInt(); if(n%4==2 || n%4==3) { pw.println(-1); } else if(n == 1) { pw.println(1); } else { int[] list = new int[n]; if(n%4==1) { int curr = 2; for(int i = 0; i < list.length/2; i += 2) { list[i] = curr; list[i+1] = (n+1)-(curr-1); list[list.length-2-i] = curr-1; list[list.length-1-i] = n+1-curr; curr += 2; } list[list.length/2] = (n+1)/2; } else { int curr = 2; for(int i = 0; i < list.length/2; i += 2) { list[i] = curr; list[i+1] = n+2-curr; curr += 2; } curr = 1; for(int i = list.length-2; i >= list.length/2; i -= 2) { list[i] = curr; list[i+1] = n-curr; curr += 2; } } StringBuilder sb = new StringBuilder(); for(int out: list) { sb.append(out + " "); } pw.println(sb.toString().trim()); } } pw.close(); } public static boolean lucky(int[] list) { for (int i = 0; i < list.length; i++) { if(list[list[i]-1] != list.length-i) { return false; } } return true; } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { pw.close(); System.exit(0); } st = new StringTokenizer(br.readLine().trim()); } return st.nextToken(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Deque; import java.util.LinkedList; import java.util.StringTokenizer; public class Main { private void solve() throws IOException { int n = ni(); if (n == 1) { prln(1); return; } if (n < 4) { prln(-1); return; } if (n % 4 == 0 || n % 4 == 1) { Deque<Integer> deque = new LinkedList<>(); if (n % 4 == 1) deque.add(n / 2 + 1); int t = n / 4; int l = n / 2; int r = n / 2 + 1; if (n % 4 == 1) ++r; for (int i = 0; i < t; ++i) { deque.addFirst(r + 1); deque.addFirst(l); deque.addLast(l - 1); deque.addLast(r); l -= 2; r += 2; } for (int i = 0; i < n; ++i) { pr(deque.pollFirst() + " "); } } else prln(-1); } public static void main(String[] args) { new Main().run(); } public void run() { try { if (isFileIO) { pw = new PrintWriter(new File("output.out")); br = new BufferedReader(new FileReader("input.in")); } else { pw = new PrintWriter(System.out); br = new BufferedReader(new InputStreamReader(System.in)); } solve(); pw.close(); br.close(); } catch (IOException e) { System.err.println("IO Error"); } } private int[] nia(int n) throws IOException { int arr[] = new int[n]; for (int i = 0; i < n; ++i) arr[i] = Integer.parseInt(nextToken()); return arr; } private int[] niam1(int n) throws IOException { int arr[] = new int[n]; for (int i = 0; i < n; ++i) arr[i] = Integer.parseInt(nextToken()) - 1; return arr; } private long[] nla(int n) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; ++i) arr[i] = Long.parseLong(nextToken()); return arr; } private void pr(Object o) { pw.print(o); } private void prln(Object o) { pw.println(o); } private void prln() { pw.println(); } int ni() throws IOException { return Integer.parseInt(nextToken()); } long nl() throws IOException { return Long.parseLong(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } private String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(br.readLine()); } return tokenizer.nextToken(); } // private void qsort(int a[]) { // Random rand = new Random(271828182l); // qsort(a, 0, a.length, rand); // } // // private void qsort(int a[], int l, int r, Random rand) { // if (r - l <= 1) // return; // // if (r - l == 2) { // if (a[r - 1] < a[l]) { // int t = a[r - 1]; // a[r - 1] = a[l]; // a[l] = t; // } // // return; // } // // int x = a[rand.nextInt(r - l) + l]; // int i = l, j = r - 1; // while (i < j) { // while (a[i] < x) // ++i; // while (a[j] > x) // --j; // if (i < j) { // int t = a[i]; // a[i] = a[j]; // a[j] = t; // ++i; // --j; // } // } // // qsort(a, l, j + 1, rand); // qsort(a, i, r, rand); // } private BufferedReader br; private StringTokenizer tokenizer; private PrintWriter pw; private final boolean isFileIO = false; }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.Locale; import java.io.OutputStream; import java.io.PrintWriter; import java.util.RandomAccess; import java.util.AbstractList; import java.io.Writer; import java.util.List; import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; import java.util.InputMismatchException; import java.math.BigInteger; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Jacob Jiang */ 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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); if ((n / 2) % 2 == 1) { out.println(-1); return; } int[] p = new int[n]; int i = 0, j = n - 1; while (i <= j) { if (i == j) { p[i] = i; break; } else { int a = i++; int c = j--; int b = i++; int d = j--; p[a] = b; p[b] = c; p[c] = d; p[d] = a; } } ArrayUtils.increaseByOne(p); out.printLine(ArrayUtils.asList(p).toArray()); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; 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 & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } class OutputWriter { private PrintWriter writer; public OutputWriter(OutputStream stream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void println(int i) { writer.println(i); } public void print(Object obj) { writer.print(obj); } public void println() { writer.println(); } public void print(char c) { writer.print(c); } public void close() { writer.close(); } public void printItems(Object... items) { for (int i = 0; i < items.length; i++) { if (i != 0) { print(' '); } print(items[i]); } } public void printLine(Object... items) { printItems(items); println(); } } class ArrayUtils { public static void increaseByOne(int[]... arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) { array[i]++; } } } public static List<Integer> asList(int[] array) { return new IntList(array); } private static class IntList extends AbstractList<Integer> implements RandomAccess { int[] array; private IntList(int[] array) { this.array = array; } public Integer get(int index) { return array[index]; } public Integer set(int index, Integer element) { int result = array[index]; array[index] = element; return result; } public int size() { return array.length; } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n=input() if n==1: print 1 if n==2 or n==3: print -1 from collections import deque q=deque() if n>=4: v=n/4 l1=[] l2=[] v2=n%4 for i in range(1,2*(v)+1,2): q.appendleft(n-i) q.appendleft(i) l2.append(i+1) l2.append(n-i+1) # print l2,q if v2==1: l2.append((n+1)/2) if v2==2: print -1 exit() if v2==3: print -1 exit() l2=l2+list(q) for i in l2: print i,
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = int(raw_input()) if n % 4 == 2 or n % 4 == 3 : print -1 exit() p = [0] * (n + 1) if(n % 2 == 1): p[(n + 1) / 2] = (n + 1) / 2; for i in range(1, n/2, 2): p[i] = i + 1 p[n - i + 1] = n - i p[n - i] = i p[i + 1] = n + 1 - i for i in range(1, n): print p[i], print p[n]
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.StringTokenizer; public class A implements Runnable { PrintWriter out; BufferedReader breader; StringTokenizer st; void solve() throws NoSuchAlgorithmException, IOException { int n = nextInt(); if (n == 1) { out.println(1); return; } if (n / 2 % 2 == 1) { out.println(-1); return; } for (int i = 0; i < n / 2; ++i) { if (i % 2 == 0) { out.print((i + 2) + " "); } else { out.print((n - i + 1) + " "); } } if (n % 2 == 1) { out.print((n / 2 + 1) + " "); } for (int i = n / 2 - 1; i >= 0; --i) { if (i % 2 == 0) { out.print((n - (i + 2) + 1) + " "); } else { out.print((i) + " "); } } } /** * @param args * @throws NoSuchAlgorithmException * @throws IOException * @throws LineUnavailableException */ public static void main(String[] args) { (new Thread(new A())).start(); } @Override public void run() { try { breader = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); out = new PrintWriter(System.out); solve(); out.close(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String next() throws IOException { while (!st.hasMoreTokens()) { String temp = breader.readLine(); if (temp == null) { return null; } st = new StringTokenizer(temp); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; bool vis[1000 * 100 + 100]; bool vis2[1000 * 100 + 100]; int val[1000 * 100 + 100]; int main() { int n; cin >> n; if (n % 4 > 1) { cout << -1 << endl; return 0; } if (n == 1) { cout << 1 << endl; return 0; } int v = 2; for (int i = 1; i <= n; ++i) { if (!vis[i]) { if (n % 2 == 1 && i == (n + 1) / 2) val[i] = i; else { vis[n - v + 1] = vis[n - i + 1] = vis[v] = vis[i] = true; val[v] = n - i + 1; val[n - i + 1] = n - v + 1; val[i] = v; val[n - v + 1] = i; v += 2; ; } } } for (int i = 1; i <= n; ++i) cout << val[i] << ' '; cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int i, t, j, tmp, n, m, k, cnt, fl, ind; cin >> n; if (n % 4 != 0 and n % 4 != 1) { printf("-1\n"); return 0; } vector<int> ar(n); ar[n / 2] = n / 2 + 1; i = 0; j = n - 1; while (i < j) { ar[i] = i + 2; ar[i + 1] = j + 1; ar[j] = j; ; ar[j - 1] = i + 1; i += 2; j -= 2; } for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++) { cout << *it << " "; } printf("\n"); ; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int i, j, k, x, y, z, n, m; bool found; int a[100001]; int main() { cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1 << endl; return 0; } if (n % 2 == 1) { for (i = 1; i < n / 2 + 1; i += 2) a[i] = i + 1; for (i = n / 2 + 3; i <= n; i += 2) a[i] = i - 1; for (i = n - 1; i > n / 2 + 1; i -= 2) a[i] = n - i; for (i = n / 2; i > 0; i -= 2) a[i] = n - i + 2; a[n / 2 + 1] = n / 2 + 1; } else { for (i = 1, j = n; i < j; i += 2, j -= 2) { a[i] = i + 1; a[j] = j - 1; a[i + 1] = j; a[j - 1] = i; } } for (i = 1; i <= n; ++i) cout << a[i] << ' '; cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.*; import java.math.*; import java.util.*; public class SolutionA { public static void main(String[] args){ new SolutionA().run(); } int n; int a[]; void solve(){ n = in.nextInt(); if( n % 4 == 3 || n % 4 == 2){ out.println(-1); return; } a = new int[n+1]; int l = 1; int r = n; while( l<r){ a[l] = l+1; a[l+1] = r; a[r] = r - 1; a[r-1] = l; l+=2; r-=2; } if( l == r) a[l] = l; for(int i = 1; i<=n; i++) out.print(a[i] + " "); } PrintWriter out; FastScanner in; public void run(){ in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner{ BufferedReader bf; StringTokenizer st; public FastScanner(InputStream is){ bf = new BufferedReader(new InputStreamReader(is)); } String next(){ while(st == null || !st.hasMoreTokens()){ try{ st = new StringTokenizer(bf.readLine()); } catch(Exception ex){ ex.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()); } BigInteger nextBigInteger(){ return new BigInteger(next()); } BigDecimal nextBigDecimal(){ return new BigDecimal(next()); } int[] nextArray(int n){ int x[] = new int[n]; for(int i = 0; i < n; i++){ x[i] = Integer.parseInt(next()); } return x; } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int n; cin >> n; if (n == 1) { cout << 1 << endl; return 0; } if (n <= 3) { cout << -1 << endl; return 0; } if (n % 2 == 0) { if (n % 4 != 0) { cout << -1 << endl; return 0; } for (int i = 1; i <= n / 2; i = i + 2) { a[i] = i + 1; a[i + 1] = n + 2 - (i + 1); a[n - i + 1] = a[i + 1] - 1; a[n - (i + 1) + 1] = a[i] - 1; } } if (n % 2 == 1) { if ((n - 1) % 4 != 0) { cout << -1 << endl; return 0; } a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n / 2; i = i + 2) { a[i] = i + 1; a[i + 1] = n + 2 - (i + 1); a[n - i + 1] = a[i + 1] - 1; a[n - (i + 1) + 1] = a[i] - 1; } } for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; void solve() { int n; scanf("%d", &n); if (n % 4 > 1) printf("-1\n"); else { for (int i = 1; i <= n >> 1; i++) { if (i % 2) printf("%d ", i + 1); else printf("%d ", n - i + 2); } int m = n >> 1; if (n % 4 == 1) { printf("%d", m + 1); if (m + 1 < n) printf(" "); m++; } for (int i = m + 1; i < n; i++) { if ((i - m) & 1) printf("%d ", n - i); else printf("%d ", i - 1); } if (m + 1 < n) printf("%d ", n - 1); printf("\n"); } } int main() { solve(); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[100100]; int main() { int n; scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); return 0; } if (n % 2) a[n / 2 + 1] = n / 2 + 1; int num1 = 2; int num2 = n; for (int i = 1; i <= n / 2; i += 2) { a[i] = num1; a[n - i + 1] = n - num1 + 1; num1 += 2; a[i + 1] = num2; a[n - i] = n - num2 + 1; num2 -= 2; } for (int i = 1; i < n; ++i) { printf("%d ", a[i]); } printf("%d\n", a[n]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int N = 111111; int rec[N]; int main() { int n; while (cin >> n) { if ((n & 3) >= 2) { puts("-1"); continue; } int hf = n >> 1; for (int i = 1; i <= hf; i++, i++) { rec[i] = i + 1; rec[n - i] = i; } for (int i = 2; i <= hf; i++, i++) { rec[i] = n - i + 2; rec[n - i + 2] = n - i + 1; } if (n & 1) { rec[hf + 1] = hf + 1; } for (int i = 1; i <= n; i++) cout << rec[i] << ' '; cout << endl; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } int n, v[100010]; int p[100010]; bool flag, z = 0; void dfs(int id, int s, int ci) { if (ci == 5) { flag = 1; return; } p[id] = s; v[s] = id; dfs(s, n - id + 1, ci + 1); } int main() { int i, j; scanf("%d", &n); if (n % 2 == 1) { if (n == 1) puts("1"); else if (n % 4 == 1) { for (int i = 1; i < n / 2; i += 2) dfs(i, i + 1, 1); p[(n + 1) / 2] = (n + 1) / 2; z = 1; for (i = 1; i < n; i++) printf("%d ", p[i]); printf("%d\n", p[i]); } else puts("-1"); return 0; } if (n % 4 == 0) { for (int i = 1; i <= n / 2; i += 2) dfs(i, i + 1, 1); z = 1; } if (z) { for (i = 1; i < n; i++) printf("%d ", p[i]); printf("%d\n", p[i]); } else puts("-1"); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n; int ans[100010]; bool vst[100010]; void dfs(int pos) { if (!vst[ans[pos]]) { vst[ans[pos]] = 1; ans[ans[pos]] = n - pos + 1; dfs(ans[pos]); } else return; } int main() { while (scanf("%d", &n) != EOF) { memset(ans, 0, sizeof(ans)); memset(vst, 0, sizeof(vst)); if (n == 1) { puts("1"); continue; } if (n == 2) { puts("-1"); continue; } if (n == 3) { puts("-1"); continue; } int t = n / 4; for (int i = 1; i <= t; ++i) { vst[i * 2 - 1] = 1; ans[i * 2 - 1] = i * 2; dfs(i * 2 - 1); } int nu = t * 2 + 2; int md = n % 4; if (md == 1) { ans[(t + 1) * 2 - 1] = t * 2 + 1; } if (md == 2) { puts("-1"); continue; } if (md == 3) { puts("-1"); continue; } printf("%d", ans[1]); for (int i = 2; i < n + 1; ++i) { printf(" %d", ans[i]); } puts(""); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.util.Scanner; public class p287C { /** * @param args */ public static void main(String[] args) { Scanner cin = new Scanner(System.in); int N = cin.nextInt(); int[] ans = new int[N + 1]; if (N % 4 > 1) { System.out.println("-1"); } else { if (N % 2 == 1) ans[N / 2 + 1] = N / 2 + 1; for (int i = 1; i < N / 2; i += 2) { ans[i] = i + 1; ans[i + 1] = N - i + 1; ans[N - i + 1] = N - i; ans[N - i] = i; } for (int i = 1; i < N; i++) System.out.print("" + ans[i] + " "); System.out.println(ans[N]); } cin.close(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int perm[100001]; int main() { ios_base::sync_with_stdio(false); cout.precision(30); int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1 << endl; return 0; } if (n % 4 == 1) perm[(n + 1) / 2] = (n + 1) / 2; for (int i = 1; i <= n / 2; i += 2) { perm[i] = i + 1; perm[i + 1] = (n + 1 - i); perm[n - i] = i; perm[n + 1 - i] = n - i; } for (int i = 1; i <= n; ++i) cout << perm[i] << " "; cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int N; int main() { scanf("%d", &N); if (N % 4 == 2 || N % 4 == 3) { printf("-1\n"); return 0; } for (int i = 1; i <= N / 2; i += 2) { printf("%d %d ", i + 1, N - i + 1); } if (N % 4 == 1) printf("%d ", (N + 1) / 2); for (int i = (N + 3) / 2; i <= N; i += 2) { printf("%d %d ", N - i, i); } }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int p[100010]; int N; int main() { cin >> N; int pre; for (int i = 0; i < N / 4; i++) p[2 * i + 1] = 2 * i + 2; if (N % 4 == 1) p[N / 2 + 1] = N / 2 + 1; if (N % 4 != 0 && N % 4 != 1) { cout << -1 << endl; return 0; } for (int i = 1; i < N; i++) { pre = i; while (p[p[pre]] == 0) { p[p[pre]] = N - pre + 1; pre = p[pre]; } } for (int i = 1; i < N + 1; i++) cout << p[i] << (i == N ? '\n' : ' '); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> int clock0 = clock(); using namespace std; template <class _T> inline _T sqr(const _T &first) { return first * first; } template <class _T> inline string tostr(const _T &a) { ostringstream os(""); os << a; return os.str(); } const long double PI = 3.1415926535897932384626433832795L; const long double EPS = 4e-5; char TEMPORARY_CHAR; const int INF = 2e9; inline void fft(vector<complex<long double> > &a, bool invert) { int n = (int)a.size(); for (int i = 1, j = 0; i < n; ++i) { int bit = n >> 1; for (; j >= bit; bit >>= 1) j -= bit; j += bit; if (i < j) swap(a[i], a[j]); } for (int len = 2; len <= n; len <<= 1) { long double ang = 2 * PI / len * (invert ? -1 : 1); complex<long double> wlen(cos(ang), sin(ang)); for (int i = 0; i < n; i += len) { complex<long double> w(1); for (int j = 0; j < len / 2; ++j) { complex<long double> u = a[i + j], v = a[i + j + len / 2] * w; a[i + j] = u + v; a[i + j + len / 2] = u - v; w *= wlen; } } } if (invert) for (int i = 0; i < n; ++i) a[i] /= n; } inline void input(int &a) { a = 0; while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-')) { } char neg = 0; if (TEMPORARY_CHAR == '-') { neg = 1; TEMPORARY_CHAR = getchar(); } while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0') { a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0'; TEMPORARY_CHAR = getchar(); } if (neg) a = -a; } inline void out(int a) { if (!a) putchar('0'); if (a < 0) { putchar('-'); a = -a; } char s[10]; int i; for (i = 0; a; ++i) { s[i] = '0' + a % 10; a /= 10; } for (int j = (i)-1; j >= 0; j--) putchar(s[j]); } inline int nxt() { int(ret); input((ret)); ; return ret; } struct lnum { vector<int> a; int base; lnum(int num = 0, int base = 1000000000) : base(base) { if (!num) a.resize(1); while (num) { a.push_back(num % base); num /= base; } } inline int len() const { return a.size(); } lnum &operator=(const lnum &l) { if (this != &l) { a = l.a; base = l.base; } return *this; } inline friend lnum operator+(const lnum &l, const lnum &r) { lnum ret(0, l.base); int base = l.base; int ln = l.len(), rn = r.len(); int n = max(ln, rn); ret.a.resize(n); int o = 0; for (int i = 0; i < n; ++i) { int s = o; if (i < ln) s += l.a[i]; if (i < rn) s += r.a[i]; o = s >= base; if (o) s -= base; ret.a[i] = s; } if (o) ret.a.push_back(1); return ret; } inline friend lnum operator-(const lnum &l, const lnum &r) { lnum ret(0, l.base); int base = l.base; int n = l.len(); int rn = r.len(); ret.a.resize(n); int o = 0; for (int i = 0; i < n; ++i) { int s = l.a[i] - o; if (i < rn) s -= r.a[i]; o = s < 0; if (o) s += base; ret.a[i] = s; } if (ret.len() > 1 && !ret.a.back()) ret.a.pop_back(); return ret; } inline friend lnum operator*(const lnum &l, const lnum &r) { lnum ret(0, l.base); int base = l.base; if (l.len() * r.len() > 1000000) { vector<complex<long double> > fa(l.a.begin(), l.a.end()), fb(r.a.begin(), r.a.end()); int n = 1; while (n < max(l.len(), r.len())) n <<= 1; n <<= 1; fa.resize(n), fb.resize(n); fft(fa, false), fft(fb, false); for (int i = 0; i < n; ++i) fa[i] *= fb[i]; fft(fa, true); ret.a.resize(n); for (int i = 0; i < n; ++i) ret.a[i] = int(fa[i].real() + 0.5); int carry = 0; for (int i = 0; i < n; ++i) { ret.a[i] += carry; carry = ret.a[i] / base; ret.a[i] %= base; } } else { ret.a.resize(l.len() + r.len()); for (int i = 0; i < l.len(); ++i) for (int j = 0, carry = 0; j < r.len() || carry; ++j) { long long cur = ret.a[i + j] + (long long)l.a[i] * (j < r.len() ? r.a[j] : 0) + carry; ret.a[i + j] = cur % base; carry = cur / base; } } while (ret.len() > 1 && !ret.a.back()) ret.a.pop_back(); return ret; } inline friend lnum operator/(const lnum &l, const int &r) { lnum ret(0, l.base); ret.a.resize(l.len()); int carry = 0; for (int i = l.len() - 1; i >= 0; --i) { long long cur = l.a[i] + (long long)carry * l.base; ret.a[i] = cur / r; carry = cur % r; } while (ret.len() > 1 && ret.a.back() == 0) ret.a.pop_back(); return ret; } inline friend bool operator<(const lnum &l, const lnum &r) { if (l.len() < r.len()) return true; if (l.len() > r.len()) return false; int n = l.len(); for (int i = n - 1; i >= 0; --i) { if (l.a[i] < r.a[i]) return true; if (l.a[i] > r.a[i]) return false; } return false; } inline friend bool operator>(const lnum &l, const lnum &r) { return r < l; } inline friend bool operator==(const lnum &l, const lnum &r) { if (l.len() != r.len()) return false; int n = l.len(); for (int i = n - 1; i; --i) { if (l.a[i] != r.a[i]) return false; } return true; } inline friend bool operator!=(const lnum &l, const lnum &r) { return !(l == r); } inline void print() { if (base == 1000000000) { printf("%d", a.back()); for (int i = a.size() - 2; i >= 0; --i) printf("%09d", a[i]); } else { for (int i = a.size() - 1; i >= 0; --i) printf("%d", a[i]); } } }; int main() { int(n); input((n)); ; if (n == 1) { puts("1"); return 0; } if (n == 2) { puts("-1"); return 0; } if (n == 3) { puts("-1"); return 0; } if ((n % 4 != 0) && ((n - 1) % 4 != 0)) { puts("-1"); return 0; } int a[n]; int i = 0; while (i + 1 < n / 2) { a[i] = i + 1; a[i + 1] = n - i - 1; a[n - i - 1] = n - i - 2; a[n - i - 2] = i; i += 2; } if (n & 1) a[n / 2] = n / 2; for (int i = 0; i < (int)(n); i++) { printf("%d ", a[i] + 1); } puts(""); }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import static java.lang.Math.*; import static java.math.BigInteger.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class A { final static boolean autoflush = false; final static int MOD = (int) 1e9 + 7; final static double eps = 1e-9; final static int INF = (int) 1e9; public A () { int N = sc.nextInt(); int [] res = new int [N]; if (N%4 == 0 || N%4 == 1) { if (N%4 == 1) res[(N-1)/2] = (N-1)/2; for (int i : rep(N/4)) { int a = 2*i, b = 2*i+1; res[a] = b; res[b] = N-1-a; res[N-1-a] = N-1-b; res[N-1-b] = a; } for (int i : rep(N)) ++res[i]; exit(res); } else exit(-1); } //////////////////////////////////////////////////////////////////////////////////// static int [] rep(int N) { return rep(0, N); } static int [] rep(int S, int T) { int [] res = new int [max(T-S, 0)]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } static int [] req(int S, int T) { return rep(S, T+1); } //////////////////////////////////////////////////////////////////////////////////// /* Dear hacker, don't bother reading below this line, unless you want to help me debug my I/O routines :-) */ final static MyScanner sc = new MyScanner(); static class MyScanner { public String next() { newLine(); return line[index++]; } public char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { line = null; return readLine(); } public String [] nextStrings() { line = null; return readLine().split(" "); } public char [] nextChars() { return next ().toCharArray (); } public Integer [] nextInts() { String [] L = nextStrings(); Integer [] res = new Integer [L.length]; for (int i : rep(L.length)) res[i] = Integer.parseInt(L[i]); return res; } public Long [] nextLongs() { String [] L = nextStrings(); Long [] res = new Long [L.length]; for (int i : rep(L.length)) res[i] = Long.parseLong(L[i]); return res; } public Double [] nextDoubles() { String [] L = nextStrings(); Double [] res = new Double [L.length]; for (int i : rep(L.length)) res[i] = Double.parseDouble(L[i]); return res; } public String [] next (int N) { String [] res = new String [N]; for (int i : rep(N)) res[i] = sc.next(); return res; } public Integer [] nextInt (int N) { Integer [] res = new Integer [N]; for (int i : rep(N)) res[i] = sc.nextInt(); return res; } public Long [] nextLong (int N) { Long [] res = new Long [N]; for (int i : rep(N)) res[i] = sc.nextLong(); return res; } public Double [] nextDouble (int N) { Double [] res = new Double [N]; for (int i : rep(N)) res[i] = sc.nextDouble(); return res; } public String [][] nextStrings (int N) { String [][] res = new String [N][]; for (int i : rep(N)) res[i] = sc.nextStrings(); return res; } public Integer [][] nextInts (int N) { Integer [][] res = new Integer [N][]; for (int i : rep(N)) res[i] = sc.nextInts(); return res; } public Long [][] nextLongs (int N) { Long [][] res = new Long [N][]; for (int i : rep(N)) res[i] = sc.nextLongs(); return res; } public Double [][] nextDoubles (int N) { Double [][] res = new Double [N][]; for (int i : rep(N)) res[i] = sc.nextDoubles(); return res; } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = readLine().split(" "); index = 0; } } } static void print (Object o, Object... a) { printDelim(" ", o, a); } static void cprint (Object o, Object... a) { printDelim("", o, a); } static void printDelim (String delim, Object o, Object... a) { pw.println(build(delim, o, a)); } static void exit (Object o, Object... a) { print(o, a); exit(); } static void exit() { pw.close(); System.out.flush(); System.err.println("------------------"); System.err.println("Time: " + ((millis() - t) / 1000.0)); System.exit(0); } static void NO() { throw new Error("NO!"); } //////////////////////////////////////////////////////////////////////////////////// static String build (String delim, Object o, Object... a) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : a) append(b, p, delim); return b.toString().trim(); } static void append(StringBuilder b, Object o, String delim) { if (o.getClass().isArray()) { int L = java.lang.reflect.Array.getLength(o); for (int i : rep(L)) append(b, java.lang.reflect.Array.get(o, i), delim); } else if (o instanceof Iterable<?>) for (Object p : (Iterable<?>)o) append(b, p, delim); else b.append(delim).append(o); } //////////////////////////////////////////////////////////////////////////////////// static void statics() { abs(0); valueOf(0); asList(new Object [0]); reverseOrder(); } public static void main (String[] args) { new A(); exit(); } static void start() { t = millis(); } static java.io.PrintWriter pw = new java.io.PrintWriter(System.out, autoflush); static long t; static long millis() { return System.currentTimeMillis(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> int n, i, k, a[100100]; int main() { scanf("%d", &n); if (n % 4 > 1) { puts("-1"); return 0; } for (i = k = 0; k + 4 <= n; i += 2, k += 4) { a[i + 1] = i + 2; a[i + 2] = n - i; a[n - i] = n - i - 1; a[n - i - 1] = i + 1; } if (k + 1 == n) a[i + 1] = i + 1; for (i = 1; i <= n; i++) printf("%d%c", a[i], i == n ? '\n' : ' '); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
/** * Created with IntelliJ IDEA. * User: yuantian * Date: 3/24/13 * Time: 12:26 AM * Copyright (c) 2013 All Right Reserved, http://github.com/tyuan73 */ import java.util.*; public class LuckyPermutation { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] p = new int[n]; int r = n % 4; if(r == 2 || r == 3) { System.out.println(-1); return; } for(int i = 1, j = n; i < j; i += 2, j -= 2) { p[i-1] = i+1; p[i] = j; p[j-1] = j-1; p[j-2] = i; } if(r == 1) p[(n-1)/2] = (n+1)/2; System.out.print(p[0]); for(int i = 1; i < n; i++) System.out.print(" " + p[i]); System.out.println(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); int n, i, ara[100009], cnt; unordered_map<int, bool> q; cin >> n; for (i = 1, cnt = 1; i <= n / 2; i++) { if (cnt++ % 2) { if (q[i + 1]) cout << "-1" << '\n', exit(0); ara[i] = i + 1; q[i + 1] = 1; } else { if (q[n - i + 2]) cout << "-1" << '\n', exit(0); ara[i] = n - i + 2; q[n - i + 2] = 1; } } for (i = n, cnt = 1; i > ceil((float)n / 2.0); i--) { if (cnt++ % 2) { if (q[i - 1]) cout << "-1" << '\n', exit(0); ara[i] = i - 1; q[i - 1] = 1; } else { if (q[n - i]) cout << "-1" << '\n', exit(0); ara[i] = n - i; q[n - i] = 1; } } if (n % 2) ara[n / 2 + 1] = n / 2 + 1; for (i = 1; i <= n; i++) if (ara[ara[i]] != n - i + 1) cout << "-1" << '\n', exit(0); for (i = 1; i <= n; i++) cout << ara[i] << ' '; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const double PI = 3.141592653589793238463; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> p(n); for (int i = 0; i < n; ++i) { cin >> p[i]; } if (n % 4 == 3 || n % 4 == 2) { cout << -1 << endl; return 0; } vector<int> a(n + 1); int l = 1, r = n; while (l < r) { a[l] = l + 1; a[l + 1] = r; a[r] = r - 1; a[r - 1] = l; l += 2; r -= 2; } if (l == r) { a[l] = l; } for (int i = 0; i < n; ++i) { cout << a[i + 1] << " "; } cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = input() if n%4 in [2, 3]: print -1 else: ans = [0 for i in range(n)] for i in range(0, n/2, 2): ans[i] = i + 2 ans[i + 1] = n - i ans[n - i - 1] = n - i - 1 ans[n - i - 2] = i + 1 if n%4 == 1: ans[n/2] = n/2 + 1 print ' '.join([str(x) for x in ans])
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.PrintWriter; import java.util.Scanner; public class C176 { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); if (n % 4 == 2 || n % 4 == 3) out.println(-1); else { int[] p = new int[n]; for (int i = 0; i < n / 4; i++) { p[2 * i] = 2 * i + 2; p[2 * i + 1] = n + 2 - p[2 * i]; p[n - 1 - (2 * i + 1)] = 2 * i + 1; p[n - 1 - 2 * i] = n - p[n - 1 - (2 * i + 1)]; } if (n % 4 == 1) p[n / 2] = n / 2 + 1; for (int i = 0; i < n; i++) { out.print(p[i] + " "); } out.println(); } out.close(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> int a[100000 + 1]; int main() { int n; scanf("%d", &n); if (n % 4 < 2) { for (int i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n + 1 - i; a[n + 1 - i] = n - i; a[n - i] = i; } if (n % 4 == 1) a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; i++) printf("%d ", a[i]); printf("\n"); } else { printf("-1\n"); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; long double sqr(long double a) { return a * a; } const long long int inf = 1000000000000000LL; long double mpow(long double a, long long int b) { if (b == 0) return 1.0; if (b % 2 == 0) { long double c = mpow(a, b / 2); return c * c; } return a * mpow(a, b - 1); } template <typename tname> class Array { private: tname x; public: Array() {} }; struct T {}; struct V {}; class Hist { public: Hist(Array<T>* a); void append(T); void commit(); void undo(); }; long long int FINISH_X = 0; long long int FINISH_Y = 0; long long int visited[201][201][6][6]; struct CubeState { long long int faces[6]; long long int x, y; CubeState() { x = 0, y = 0; for (int i = 0; i < 6; ++i) { faces[i] = i; } } long long int getTopFace() const { return faces[1]; } long long int getFrontFace() const { return faces[2]; } void setLength(long long int length) { visited[x + 100][y + 100][getTopFace()][getFrontFace()] = length; } long long int getLength() const { return visited[x + 100][y + 100][getTopFace()][getFrontFace()]; } bool isAtFinish() const { return (faces[0] == 0 && faces[1] == 1 && x == FINISH_X && y == FINISH_Y); } bool isInside() { return (abs(x) <= 30 && abs(y) <= 30); } }; CubeState rotate_cube_up(CubeState& cs) { CubeState newState = cs; newState.faces[1] = cs.faces[0]; newState.faces[2] = cs.faces[1]; newState.faces[3] = cs.faces[2]; newState.faces[0] = cs.faces[3]; newState.y++; return newState; } CubeState rotate_cube_down(CubeState& cs) { CubeState newState = cs; newState.faces[0] = cs.faces[1]; newState.faces[1] = cs.faces[2]; newState.faces[2] = cs.faces[3]; newState.faces[3] = cs.faces[0]; newState.y--; return newState; } CubeState rotate_cube_right(CubeState& cs) { CubeState newState = cs; newState.faces[1] = cs.faces[5]; newState.faces[4] = cs.faces[1]; newState.faces[3] = cs.faces[4]; newState.faces[5] = cs.faces[3]; newState.x++; return newState; } CubeState rotate_cube_left(CubeState& cs) { CubeState newState = cs; newState.faces[5] = cs.faces[1]; newState.faces[1] = cs.faces[4]; newState.faces[4] = cs.faces[3]; newState.faces[3] = cs.faces[5]; newState.x--; return newState; } long long int mySign(long long int num) { if (num == 0) { return 0; } return (num > 0) ? 1 : -1; } int main() { clock_t start = clock(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); srand(13); int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << "-1 "; return 0; } vector<long long int> a, b; int x = 1; int y = n; while (x < y) { a.push_back(x + 1); a.push_back(y); b.push_back(y - 1); b.push_back(x); x += 2; y -= 2; } for (int i = 0; i < a.size(); ++i) { cout << a[i] << " "; } if (n % 2 == 1) cout << n / 2 + 1 << " "; for (int i = 0; i < b.size(); ++i) { cout << b[b.size() - i - 1] << " "; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = input() if n % 4 < 2: v = [(n + 1) /2] * n for i in xrange(n / 4): a = i * 2 v[a],v[a + 1], v[-a-2], v[-a-1] = a + 2, n - a, a + 1, n - a - 1 print ' '.join(map(str, v)) else : print -1
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[1 << 17]; int N; int go(int i) { a[i] = i + 1; while (a[a[i]] == 0) { a[a[i]] = N - i + 1; i = a[i]; } return i + a[a[i]] == N + 1; } int main() { scanf("%d", &N); int good = 1; for (int i = 1; i <= N; ++i) if (!a[i]) { if (i * 2 == N + 1) a[i] = i; else good |= go(i); } if (N % 4 > 1) good = 0; if (not good) printf("-1\n"); else { for (int i = 1; i <= N; ++i) printf("%d ", a[i]); printf("\n"); } }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int arr[100010]; int main() { int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << "-1"; return 0; } if (n % 4 == 1) { arr[(n + 1) / 2] = (n + 1) / 2; } int rep = n / 4; int start = 1; for (int i = 1; i <= rep; i++) { int tmp1 = i * 2 - 1; arr[tmp1] = i * 2; arr[i * 2] = n - tmp1 + 1; arr[n - tmp1 + 1] = n - i * 2 + 1; arr[n - i * 2 + 1] = tmp1; } for (int i = 1; i < n; i++) { cout << arr[i] << " "; } cout << arr[n]; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; struct node {}; int a[100100]; int main() { int n, i; scanf("%d", &n); if (n % 4 > 1) { puts("-1"); } else if (n == 1) { puts("1"); } else { for (i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } if (n % 2 == 1) { a[n / 2 + 1] = n / 2 + 1; } printf("%d", a[1]); for (i = 2; i <= n; i++) printf(" %d", a[i]); puts(""); } }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.PrintWriter; import java.util.Scanner; public class A { public static void main(String [] args){ Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); if(n%4==1 || n%4==0){ int a [] = new int[n+1]; for(int i = 1 ; i <= n/2 ; i+=2 ){ a[i]=i+1; a[n-i+1] = n-i; } for(int i = 2 ; i <= n/2 ; i+=2){ a[i]=n-i+2; a[n-i+1] = i - 1; } if(n%2 ==1){ a[n/2+1]= n/2+1; } out.print(a[1]); for(int i = 2 ; i <= n ; i++){ out.print(" "+a[i]); } } else{ out.print(-1); } out.close(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; char ch = getchar(); int fh = 1; while (ch < '0' || ch > '9') { if (ch == '-') fh = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= fh; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } int n; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1 << endl; return 0; } vector<int> ft, bk; for (int i = 1; i <= n / 4; i++) { ft.push_back(i * 2); ft.push_back(n + 2 - i * 2); bk.push_back(n - i * 2 + 1); bk.push_back(i * 2 - 1); } for (int i = 0; i < ft.size(); i++) cout << ft[i] << ' '; if (n % 4 == 1) cout << (n + 1) / 2 << ' '; for (int i = bk.size() - 1; i >= 0; i--) cout << bk[i] << ' '; cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; struct debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << " "; return *this; } } dbg; void debugarr(int* arr, int n) { cout << "["; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << "]" << endl; } int main() { int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1 << endl; return 0; } int arr[100010] = {0}; int x = 2, y = n; for (int i = 0; i < n / 2; i += 2) { arr[i] = x; arr[i + 1] = y; arr[n - 1 - i] = n + 1 - x; arr[n - 1 - i - 1] = n + 1 - y; x += 2; y -= 2; } if (n % 2) arr[(n) / 2] = (n + 1) / 2; for (int i = 0; i <= (int)n - 1; i++) cout << arr[i] << ' '; cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) cout << "-1" << endl; else { int result[200000] = {0}; if (n % 4 == 0) { for (int i = 0; i < n / 4; i++) { result[2 * i] = 2 * i + 2; result[2 * i + 1] = n - 2 * i; result[n - 1 - 2 * i] = n - 1 - 2 * i; result[n - 2 - 2 * i] = 2 * i + 1; } for (int i = 0; i < n; i++) cout << result[i] << " "; cout << endl; } else if (n % 4 == 1) { for (int i = 0; i < n / 4; i++) { result[2 * i] = 2 * i + 2; result[2 * i + 1] = n - 2 * i; result[n - 1 - 2 * i] = n - 1 - 2 * i; result[n - 2 - 2 * i] = 2 * i + 1; } result[n / 2] = n / 2 + 1; for (int i = 0; i < n; i++) cout << result[i] << " "; cout << endl; } } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n=input() if n%4<2: v=[(n+1)/2]*n for i in range(n/4): a=i*2 v[a],v[a+1],v[-a-2],v[-a-1]=a+2,n-a,a+1,n-a-1 print ' '.join(map(str,v)) else: print -1
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; template <class T> inline T max(T a, T b, T c) { return max(a, max(b, c)); } template <class T> inline T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> void debug(T a, T b) { for (; a != b; ++a) cerr << *a << ' '; cerr << endl; } template <class T> T gcd(T a, T b) { return (b == (T)0 ? a : gcd(b, a % b)); } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> T mathround(T x) { long long i64x = (long long)x; if (x < i64x + 0.5) return (T)i64x; return (T)i64x + 1; } template <class T> bool isprime(T x) { int till = (T)sqrt(x + .0); if (x == 1) return 0; if (x == 2) return 1; if (x % 2 == 0) return 0; for (int i = 3; i <= till; i += 2) if (x % i == 0) return 0; return 1; } int n; int p[100001]; int main() { cin >> n; if (n % 4 > 1) { cout << -1; return 0; } int till = n / 4; if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1; int id, xx; for (int i = 1; i <= till; i++) { id = i * 2 - 1; xx = i * 2; for (int j = 0; j < 4; j++) { p[id] = xx; int nid = xx; int nxx = n - id + 1; xx = nxx; id = nid; } } for (int i = 1; i <= n; i++) cout << p[i] << " "; return EXIT_SUCCESS; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.util.*; public class luckyp { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if (n % 4 == 2 || n % 4 == 3) { System.out.println(-1); return; } int[] perm = new int[n+1]; boolean[] used = new boolean[n+1]; if (n % 4 == 1) { perm[(n+1)/2] = (n+1)/2; used[(n+1)/2] = true; } int smallestUnused = 1; for (int i = 1; i <= n; ++i) { if (used[i]) continue; // find smallest used, i != j and i + j != n+1 int toUse = smallestUnused; if (toUse == i || i + toUse == n+1) { ++toUse; while (used[toUse]) { ++toUse; } if (i + toUse == n+1) { ++toUse; while (used[toUse]) ++toUse; } } perm[i] = toUse; used[toUse] = true; perm[toUse] = n - i + 1; used[n - i + 1] = true; perm[n - i + 1] = n - toUse + 1; used[n - toUse + 1] = true; perm[n - toUse + 1] = i; used[i] = true; //System.out.println(Arrays.toString(perm)); while ( smallestUnused <= n && used[smallestUnused]) ++smallestUnused; } System.out.print(perm[1]); for (int i = 2; i <= n; ++i) System.out.printf(" %d", perm[i]); System.out.println(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); if (n == 1) System.out.println("1"); else if (n % 4 >= 2) System.out.println("-1"); else { int[] p = new int[n]; int hf = n >> 1; for (int i = 0; i < hf; i += 2) { p[i] = i + 2; p[i+1] = n - i; p[n-i-1] = n - i - 1; p[n-i-2] = i + 1; } if (n % 2 == 1) p[n>>1] = n + 1 >> 1; for (int num : p) System.out.print(num + " "); } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n; int a[100001]; int main() { cin >> n; if (n % 4 < 2) { int m = n / 4; for (int i = 0; i < (m); i++) { a[2 * i] = 2 * i + 2; a[2 * i + 1] = n - 2 * i; a[n - 1 - 2 * i] = n - 1 - 2 * i; a[n - 1 - 2 * i - 1] = 2 * i + 1; } if (n % 4 == 1) a[n / 2] = (n + 1) / 2; } else { cout << -1 << endl; return 0; } for (int i = 0; i < (n); i++) cout << a[i] << ' '; cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n, a[200006]; set<int> s; int main() { scanf("%d", &n); if (n == 2 || n == 3) { printf("-1\n"); return 0; } bool f = 1; int cur = n; int s = n + 1; for (int i = n / 2; i >= 1; i--) { if (f) { a[i] = cur; a[n - i + 1] = s - cur; } else { a[i] = s - cur; a[n - i + 1] = cur; } f = !f; cur--; } if (n % 2) { a[n / 2 + 1] = s / 2; } bool yes = true; for (int i = 1; i <= n; i++) { if (a[a[i]] != n - i + 1) { yes = false; } } if (!yes) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) { printf("%d%c", a[i], i == n ? '\n' : ' '); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n=int(input()) L=[0]*(n+1) X=[False]*(n+1) if(n%4!=0 and n%4!=1): print(-1) else: for i in range(1,n+1): if(X[i]): continue X[i]=True X[n-i+1]=True for j in range(i+1,n+1): if(X[j]): continue X[j]=True X[n-j+1]=True L[i]=j L[n-i+1]=n-j+1 L[j]=n-i+1 L[n-j+1]=i break if(n%4==1): L[n//2+1]=n//2+1 for i in range(1,n): print(L[i],end=" ") print(L[n])
PYTHON3
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e9 + 7; const int N = (int)1e5 + 4; const int M = 1005; const int K = 25; int a[N]; int main() { int n; scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("-1"); return 0; } if (n % 4 == 1) { a[n / 2] = n / 2 + 1; } for (int i = n / 2 + (n % 2 == 1); i < n && a[i] == 0; ++i) { int k = i, j = i - n / 2 - (n % 2 == 1); for (int t = 0; t < 4; ++t) { a[k] = j + 1; int d = k; k = j; j = n - d - 1; } } for (int i = 0; i < n; ++i) { printf("%d ", a[i]); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.*; import java.util.*; public class My { public static void main(String[] args) { new My().go(); } void go() { Scanner in = new Scanner(System.in); int n = in.nextInt(); if (n % 4 == 0 || n % 4 == 1) { int rn = n / 4; int[] val = new int[n]; for (int i = 0; i < rn; i++) { val[2*i] = n - 2*i - 2; val[2*i + 1] = 2*i; val[n - 2*i - 1] = 2*i + 1; val[n - 2*i - 2] = n - 2*i - 1; } if (n % 4 == 1) { val[n/2] = n/2; } for (int i = 0; i < n; i++) { if (i < n - 1) { p(++val[i] + " "); } else { pl(++val[i] + ""); } } } else { pl("-1"); } } void p(String s) { System.out.print(s); } void pl(String s) { System.out.println(s); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; public class ProblemA { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.valueOf(in.readLine()); if (n == 1) { out.println(1); out.flush(); return; } if (n % 4 == 2 || n % 4 == 3) { out.println(-1); out.flush(); return; } int[] a = new int[n+1]; int fi = 1; int ti = n; while (fi < ti) { a[fi] = fi+1; a[fi+1] = ti; a[ti] = ti-1; a[ti-1] = fi; fi += 2; ti -= 2; } if (n % 4 == 1) { a[(n+1)/2] = (n+1)/2; } StringBuffer b = new StringBuffer(); for (int i = 1 ; i <= n ; i++) { b.append(a[i]).append(" "); } out.println(b.substring(0, b.length()-1)); out.flush(); } public static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = input() if ( (n>>1)&1 ): print -1; else: a = [0 for i in range(n+1)] for i in range(1, n/2, 2): a[i]=i+1; a[i+1]=n-i+1; a[n-i+1]=n-i; a[n-i]=i; if n&1: a[n>>1|1] = n>>1|1 a.pop(0) print ' '.join(map(str, a) )
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.*; public class TestClass{ public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] arr = new int[n+1]; if(n%4 > 1) System.out.println(-1); else{ int loops = n/4; int start_ind = 1; int end_ind = n; int front = 1; int end = n; for(int i = 0; i<loops; i++){ arr[end_ind-1] = front++; arr[start_ind] = front++; arr[start_ind+1] = end--; arr[end_ind] = end--; start_ind += 2; end_ind -= 2; } if(n%2 == 1){ arr[n/2 + 1] = n/2+1; } for(int i = 1; i<arr.length; i++){ System.out.print(arr[i]+" "); } } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: ira * Date: 3/23/13 * Time: 1:40 PM */ public class R { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = reader.readLine(); StringTokenizer tokenizer = new StringTokenizer(line); int n = Integer.parseInt(tokenizer.nextToken()); int[] res = new int[n]; int[] a = new int[n]; for (int i = 1; i <= n; ++i){ a[i - 1] = i; } int left = 0; int right = n - 1; while (true){ if (right - left + 1== 0){ print(res); return; } if (right - left + 1 == 1){ res[left] = a[left]; print(res); return; } if (right - left + 1 ==2 || right - left + 1 == 3){ System.out.println("-1"); return; } res[left] = a[left + 1]; res[left + 1] = a[right]; res[right - 1] = a[left]; res[right] = a[right - 1]; left = left + 2; right = right -2; } } static void print(int[] a){ for (int i = 0; i < a.length; ++i){ System.out.print(a[i] + " "); } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { private static StringTokenizer tokenizer; private static BufferedReader bf; private static PrintWriter out; private static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextToken() throws IOException { while(tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(bf.readLine()); } return tokenizer.nextToken(); } public static void main(String[] args) throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); int k = n%4; int[] a = new int[n+1]; if(k == 0) { for(int i = 1; i <= n/2; i += 2) { a[i] = i+1; a[i+1] = n+1-i; a[n+1-i] = n-i; a[n-i] = i; } for(int i = 1; i <= n; i++) out.print(a[i] + " "); } else if(k == 1) { for(int i = 1; i <= n/2; i += 2) { a[i] = i+1; a[i+1] = n+1-i; a[n+1-i] = n-i; a[n-i] = i; } a[n/2+1] = n/2+1; for(int i = 1; i <= n; i++) out.print(a[i] + " "); } else out.println(-1); out.close(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int nm = 100005; const int mm = 100005; int n, k, m, t; int a[nm]; bool check[nm]; void DO() { int i, u = 1, j = n, c1 = 2, c2; int z = n / 4, y; for (y = 1; y <= z; y++) { i = u; a[i] = c1; c1 += 2; a[a[i]] = n - i + 1; i = a[i]; a[a[i]] = n - i + 1; i = a[i]; a[a[i]] = n - i + 1; u += 2; } if (n % 4) { a[n / 2 + 1] = n / 2 + 1; } for (i = 1; i <= n; i++) printf("%d ", a[i]); } int main() { int i, j, x, y, z, w; scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("%d ", -1); return 0; } if (n == 1) { printf("%d ", 1); return 0; } DO(); }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class A{ Scanner sc=new Scanner(System.in); int INF=1<<28; double EPS=1e-9; void test(int n){ int[] is=new int[n]; for(int i=0; i<n; i++){ is[i]=i; } for(;;){ boolean ok=true; for(int i=0; i<n; i++){ ok&=is[is[i]]==n-1-i; } if(ok){ debug(is); } if(!nextPermutation(is)){ break; } } } boolean nextPermutation(int[] is){ int n=is.length; for(int i=n-1; i>0; i--){ if(is[i-1]<is[i]){ int j=n; for(; is[i-1]>=is[--j];); swap(is, i-1, j); rev(is, i, n); return true; } } rev(is, 0, n); return false; } void swap(int[] is, int i, int j){ int t=is[i]; is[i]=is[j]; is[j]=t; } void rev(int[] is, int i, int j){ for(j--; i<j; i++, j--) swap(is, i, j); } int n; void run(){ n=sc.nextInt(); solve(); } void solve(){ // test(n); if(n%4==0||n%4==1){}else{ println("-1"); return; } TreeSet<Integer> set=new TreeSet<Integer>(); for(int i=0; i<n; i++){ set.add(i); } TreeSet<Integer> temp=new TreeSet<Integer>(); int[] a=new int[n]; fill(a, -1); for(int index=0; index<n/2; index++){ if(a[index]>-1){ continue; } int i; for(i=set.pollFirst(); index==i||n-1-index==i; i=set.pollFirst()){ temp.add(i); } // debug("index", index, "i", i); a[index]=i; a[n-1-index]=n-1-i; a[a[index]]=n-1-index; a[a[n-1-index]]=index; set.addAll(temp); set.remove(i); set.remove(n-1-i); set.remove(n-1-index); set.remove(index); temp.clear(); // debug(a); } if(n%2==1){ a[n/2]=n/2; } // debug(a); boolean ok=true; for(int i=0; i<n; i++){ ok&=a[a[i]]==n-1-i; } // debug(ok); StringBuilder sb=new StringBuilder(); for(int i=0; i<n; i++){ sb.append(a[i]+1); if(i<n-1){ sb.append(' '); } } println(sb.toString()); } void println(String s){ System.out.println(s); } void print(String s){ System.out.print(s); } void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } public static void main(String[] args){ Locale.setDefault(Locale.US); new A().run(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int N, V[100010], CrtPos; bool Used[100010]; int main() { int i; scanf("%i", &N); if (N == 1) { printf("1\n"); return 0; } CrtPos = 1; V[1] = 2; bool Move = 1; while (Move) { Move = 0; while (!Used[V[CrtPos]]) { Used[V[CrtPos]] = 1; V[V[CrtPos]] = N - CrtPos + 1; CrtPos = V[CrtPos]; Move = 1; } if (!Move) { for (i = 1; i <= N; ++i) if (V[i] == 0 && !Used[i + 1]) { CrtPos = i; V[CrtPos] = i + 1; Move = 1; break; } } } if (N % 2 == 1) { V[N / 2 + 1] = N / 2 + 1; Used[N / 2 + 1] = N / 2 + 1; } bool OK = 1; for (i = 1; i <= N; ++i) if (!Used[i]) OK = 0; if (OK) for (i = 1; i <= N; ++i) printf("%i ", V[i]); else printf("-1"); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; long long n, m, k, q, l, r, x, y; const long long N = 2e5 + 5; vector<long long> arr(N); string s, t; long long ans = 0; void solve() { cin >> n; map<long long, long long> mp, ans; for (long long i = 1; i <= n; i++) mp[i] = n - i + 1, ans[i] = mp[i]; if (n == 1) { cout << 1 << "\n"; return; } if ((n / 2) & 1) { cout << -1 << "\n"; return; } l = n; n /= 4; long long i = 1; while (n--) { ans[i] = i + 1; ans[i + 1] = mp[i]; ans[ans[i + 1]] = mp[i + 1]; ans[mp[i + 1]] = i; i += 2; } for (long long j = 1; j <= l; j++) cout << ans[j] << " "; cout << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(12) << fixed; long long tt = 1; while (tt--) { solve(); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int intmax = 0x3f3f3f3f; const long long lldmax = 0x3f3f3f3f3f3f3f3fll; double eps = 1e-6; template <class T> inline void checkmin(T &a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, T b) { if (b > a) a = b; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T lowbit(T n) { return (n ^ (n - 1)) & n; } template <class T> inline int countbit(T n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } template <class T> inline T checkmod(T n, T m) { return (n % m + m) % m; } inline int rand(int a, int b) { if (a >= b) return a; return rand() % (b - a) + a; } template <class T> inline T lcm(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } template <class T> inline T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <class T> inline T euclid(T a, T b, T &x, T &y) { if (a < 0) { T d = euclid(-a, b, x, y); x = -x; return d; } if (b < 0) { T d = euclid(a, -b, x, y); y = -y; return d; } if (b == 0) { x = 1; y = 0; return a; } else { T d = euclid(b, a % b, x, y); T t = x; x = y; y = t - (a / b) * y; return d; } } template <class T> inline vector<pair<T, int> > factorize(T n) { vector<pair<T, int> > R; for (T i = 2; n > 1;) { if (n % i == 0) { int C = 0; for (; n % i == 0; C++, n /= i) ; R.push_back(make_pair(i, C)); } i++; if (i > n / i) i = n; } if (n > 1) R.push_back(make_pair(n, 1)); return R; } template <class T> inline bool isPrimeNumber(T n) { if (n <= 1) return false; for (T i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } template <class T> inline T eularFunction(T n) { vector<pair<T, int> > R = factorize(n); T r = n; for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1); return r; } template <class T> inline int dblcmp(T a, const T b) { a -= b; return a > eps ? 1 : (a < -eps ? -1 : 0); } template <class T> inline int sgn(T a) { return a > eps ? 1 : (a < -eps ? -1 : 0); } struct vec2 { double x, y; vec2(double x = 0.0, double y = 0.0) : x(x), y(y) {} vec2 operator+(const vec2 &b) const { return vec2(x + b.x, y + b.y); } vec2 operator-(const vec2 &b) const { return vec2(x - b.x, y - b.y); } vec2 operator*(const double &b) const { return vec2(x * b, y * b); } vec2 operator/(const double &b) const { return vec2(x / b, y / b); } double operator*(const vec2 &b) const { return x * b.x + y * b.y; } double operator^(const vec2 &b) const { return x * b.y - y * b.x; } double operator%(const vec2 &b) const { return x * b.x - y * b.y; } double len() { return sqrt(x * x + y * y); } vec2 unit() { return *this / len(); } vec2 rotate(double r) { vec2 t(sin(r), cos(r)); return vec2(*this ^ t, *this * t); } bool operator<(const vec2 &b) const { if (dblcmp(x, b.x) != 0) return dblcmp(x, b.x) < 0; else return dblcmp(y, b.y) < 0; } }; const int N = 110000; int a[N]; bool gao(int n) { if (n % 4 == 2 || n % 4 == 3) return false; memset(a, 0, sizeof(a)); for (int i = 1; i < n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } if (n & 1) a[(n + 1) / 2] = (n + 1) / 2; for (int i = 1; i <= n; ++i) { if (a[a[i]] != n - i + 1) return false; } return true; } int main() { int n; while (cin >> n) { if (gao(n) == false) { puts("-1"); continue; } for (int i = 1; i <= n; ++i) { printf("%d ", a[i]); } puts(""); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); long long int n, i; cin >> n; if (n == 1) { cout << 1; return 0; } if (n % 4 == 2 || n % 4 == 3) { cout << -1; return 0; } long long int a[n]; if (n % 2 == 0) { for (i = 1; i <= n / 2; i += 2) { a[i - 1] = i + 1; } for (i = 2; i <= n / 2; i += 2) { a[i - 1] = n - i + 2; } for (i = n; i > n / 2; i -= 2) { a[i - 1] = i - 1; } for (i = n - 1; i > n / 2; i -= 2) { a[i - 1] = n - i; } } else { for (i = 1; i <= n / 2; i += 2) { a[i - 1] = i + 1; } for (i = 2; i <= n / 2 + 1; i += 2) { a[i - 1] = n - i + 2; } for (i = n; i > n / 2 + 1; i -= 2) { a[i - 1] = i - 1; } for (i = n - 1; i > n / 2 + 1; i -= 2) { a[i - 1] = n - i; } a[(n - 1) / 2] = n / 2 + 1; } for (i = 0; i < n; i++) { cout << a[i] << " "; } }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.PrintWriter; import java.util.Scanner; public class Problem3 implements Runnable { public int n; public int [] p; public boolean [] used; public boolean isGood() { for (int i = 0; i < n; i++) { if (p[p[i] - 1] != n - (i + 1) + 1) { return false; } } return true; } /*public void rec(int pos) { if (pos == n) { boolean flag = true; for (int i = 0; i < n; i++) { if (p[p[i] - 1] != n - (i + 1) + 1) { flag = false; } } if (flag) { for (int i = 0; i < n; i++) { System.out.print(p[i] + " "); } System.out.println(); } return; } for (int i = 1; i <= n; i++) { if (!used[i]) { used[i] = true; p[pos] = i; rec(pos + 1); used[i] = false; } } } */ public void run() { Scanner scanner = new Scanner(System.in); PrintWriter writer = new PrintWriter(System.out); n = scanner.nextInt(); //used = new boolean[n + 1]; p = new int[n]; //rec(0); if (n == 2 || n ==3) { writer.println(-1); writer.close(); return; } if (n % 2 == 0) { if (n % 4 != 0) { writer.println(-1); writer.close(); return; } for (int i = 0; i < n / 4; i++) { p[i * 2] = (i + 1) * 2; p[i * 2 + 1] = (n - i * 2); } for (int i = 0; i < n / 4; i++) { p[n - (i + 1) * 2] = (i * 2 + 1); p[n - (i + 1) * 2 + 1] = n - (i * 2 + 1); } if (isGood()) { for (int i = 0; i < n; i++) { writer.print(p[i] + " "); } } else { writer.println(-1); } writer.close(); return; } else { if ((n - 1) % 4 != 0) { writer.println(-1); writer.close(); return; } for (int i = 0; i < n / 4; i++) { p[i * 2] = (i + 1) * 2; p[n - 1 - (i * 2)] = (n - 1) - i * 2; } for (int i = 0; i < n / 4; i++) { p[i * 2 + 1] = n - i * 2; p[n - 2 - (i * 2)] = i * 2 + 1; } p[n / 2] = n / 2 + 1; if (isGood()) { for (int i = 0; i < n; i++) { writer.print(p[i] + " "); } } else { writer.println(-1); } writer.close(); } writer.close(); } public static void main(String[] args) { new Problem3().run(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.util.*; import java.io.*; public class Main { BufferedReader in; StringTokenizer str = null; PrintWriter out; private String next() throws Exception{ if (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } private long nextLong() throws Exception{ return Long.parseLong(next()); } private double nextDouble() throws Exception{ return Double.parseDouble(next()); } public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); if (n == 1){ System.out.println(1); return; } if (n%4 == 2 || n%4 == 3){ System.out.println(-1); return; } int a[] = new int[n]; Arrays.fill(a, -1); int c1 = 1; int c2 = n-1; for(int i=0,j=n-1;i<j;i++, j--){ if (i % 2 == 0){ a[i] = c1; a[n-i-1] = n-1-c1; c1+=2; }else{ a[i] = c2; a[n-i-1] = n-1-c2; c2-=2; } } if (n % 2 == 1){ a[n/2] = n/2; } for(int i=0;i<n;i++){ out.print((a[i]+1) + " "); } out.println(); out.close(); } public static void main(String[] args) throws Exception{ new Main().run(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[100005]; int place[100005]; int main() { int n; cin >> n; if (n % 4 >= 2) cout << -1 << endl; else { for (int i = 0; i < n / 2; i += 2) { a[i] = i + 2; a[i + 1] = n + 2 - a[i]; } for (int i = n - 1; i > n / 2; i -= 2) { a[i] = i; a[i - 1] = n - a[i]; } if (n % 4 == 1) a[n / 2] = n / 2 + 1; for (int i = 0; i < n; i++) cout << a[i] << ' '; cout << endl; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if ((n / 2) % 2 == 1) { System.out.println(-1); return; } int[] ans = new int[n + 5]; if (n % 2 == 1) { ans[(n + 1) / 2] = (n + 1) / 2; } for (int i = 1; i + i < n; i += 2) { int x = i; int X = (n + 1) - x; int y = i + 1; int Y = (n + 1) - y; ans[x] = Y; ans[Y] = X; ans[X] = y; ans[y] = x; } StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) { sb.append(ans[i]); sb.append(" "); } System.out.println(sb.toString().trim()); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> int main() { int n, i; scanf("%d", &n); if (n % 4 == 0) { for (i = 1; i <= n / 2; i += 2) { printf("%d %d ", i + 1, n + 1 - i); } for (i = n / 2 + 2; i < n; i += 2) { printf("%d %d ", n + 1 - i, i - 1); } printf("%d %d\n", n + 1 - i, i - 1); } else if (n == 1) { printf("1\n"); } else if (n % 4 == 1) { for (i = 1; i <= n / 2; i += 2) { printf("%d %d ", i + 1, n + 1 - i); } printf("%d ", (n + 1) / 2); for (i = n / 2 + 3; i < n; i += 2) { printf("%d %d ", n + 1 - i, i - 1); } printf("%d %d\n", n + 1 - i, i - 1); } else { printf("-1\n"); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; static const int INF = 500000000; template <class T> void debug(T a, T b) { for (; a != b; ++a) cerr << *a << ' '; cerr << endl; } int n; int ar[100005]; int main() { scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { puts("-1"); return 0; } for (int i = 0; i < n / 4; ++i) { ar[i * 2] = 2 * i + 2; ar[i * 2 + 1] = n - i * 2; ar[n - 1 - i * 2] = n - 1 - i * 2; ar[n - 2 - i * 2] = i * 2 + 1; } if (n % 4 == 1) ar[n / 2] = n / 2 + 1; for (int i = 0; i < n; ++i) printf("%d\n", ar[i]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.*; import java.util.*; import java.lang.*; public class templ { public static int a[]=new int[1000000]; int binarySearch(long arr[], int l, int r, long x) { if (r >= l) { int mid = l + (r - l)/2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); return binarySearch(arr, mid+1, r, x); } return -1; } int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low-1); for (int j=low; j<high; j++) { if (arr[j] <= pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; /*temp = a[i]; a[i] = a[j]; a[j] = temp;*/ } } int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; /*temp = a[i+1]; a[i+1] = a[high]; a[high] = temp;*/ return i+1; } void sort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); sort(arr, low, pi-1); sort(arr, pi+1, high); } } public static void main(String[] args) throws FileNotFoundException { try { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); templ ob=new templ(); int n=in.nextInt(); if(n==1) out.println("1"); else if((n/2)%2==1) out.println("-1"); else { int m[]=new int[n+1]; for(int I=2;I<=n/2;I+=2) { //System.out.println(I); //int k=0; int i=I-1; m[i]=I; while(true) { //System.out.println(i); if(m[m[i]]!=0) { break; } m[m[i]]=n-i+1; i=m[i]; } } if(n%2==1) m[n/2+1]=n/2+1; for(int i=1;i<=n;i++) out.print(m[i]+" "); } out.close(); } catch(Exception e){ return; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> int a[100010]; int main() { int n; scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) printf("-1"); else { if (n % 4 == 0) { for (int i = 1; i <= n / 2 - 1; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } for (int i = 1; i <= n; i++) printf("%d ", a[i]); } else { for (int i = 1; i <= n / 2 - 1; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; i++) printf("%d ", a[i]); } } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n; int c[100001]; void init() { scanf("%d", &n); memset(c, 0, sizeof(c)); } void work() { if (n == 1) { printf("1"); return; } if ((n / 2) & 1) { printf("-1"); return; } for (int i = 1; i <= n / 2; i += 2) { c[i] = i + 1; c[i + 1] = n - i + 1; c[n - i] = i; c[n - i + 1] = n - i; } if (n & 1) c[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; ++i) { printf("%d ", c[i]); } } int main() { init(); work(); }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = int(input()) if n % 4 > 1: print(-1) exit() a = [i for i in range(0, n+1)] for i in range(1, n//2+1, 2): p, q, r, s = i, i+1, n-i,n-i+1 a[p], a[q], a[r], a[s] = a[q], a[s], a[p], a[r] def check(arr): for i in range(1, n+1): k = arr[i] if arr[arr[k]] != n-k+1: return False return True # print(check(a)) print(*a[1:])
PYTHON3
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1; return 0; } int a[n + 1]; if (n % 4 == 0) { for (int i = 1; i <= n / 2 - 1; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } } if (n % 4 == 1) { a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n / 2 - 1; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } } for (int i = 1; i <= n; i++) { cout << a[i] << ' '; } }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = int(input()) if n % 4 > 1: print(-1) else: k = n // 2 t = [0] * n for i in range(0, k, 2): t[i] = str(i + 2) for i in range(1, k, 2): t[i] = str(n - i + 1) if n & 1: k += 1 t[k - 1] = str(k) for i in range(k, n, 2): t[i] = str(n - i - 1) for i in range(k + 1, n, 2): t[i] = str(i) print(' '.join(t))
PYTHON3
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; template <class T, class U> bool cmp_second(const pair<T, U> &a, const pair<T, U> &b) { return a.second < b.second; } pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) { return make_pair(a.first + b.first, a.second + b.second); } pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) { return make_pair(a.first - b.first, a.second - b.second); } pair<int, int> &operator+=(pair<int, int> &a, const pair<int, int> &b) { a.first += b.first; a.second += b.second; return a; } pair<int, int> &operator-=(pair<int, int> &a, const pair<int, int> &b) { a.first -= b.first; a.second -= b.second; return a; } inline int sg(int x) { return x ? (x > 0 ? 1 : -1) : 0; } int n; int s[100500]; bool solve() { if ((n & 3) == 2 || (n & 3) == 3) return 0; if ((n & 3) == 1) s[n >> 1] = n >> 1; int cc = n >> 2; for (int i = 0; i < cc; i++) { int b = i << 1; s[b] = n - b - 2; s[n - b - 2] = n - b - 1; s[n - b - 1] = b + 1; s[b + 1] = b; } for (int i = 0; i < n; i++) printf("%d ", s[i] + 1); puts(""); return 1; } int main(void) { scanf("%d", &(n)); if (!solve()) puts("-1"); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n; bool ans = true; int a[105000], p[105000]; bool u[105000]; queue<int> q; void dfs(int pos) { u[p[pos]] = true; int to = a[pos]; if (!u[to]) { p[p[pos]] = to; dfs(p[pos]); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) a[i] = n - i + 1; if (n == 1) { printf("1"); return 0; } for (int i = 1; i <= n; i++) if (p[i] == 0) { if (p[i + 1] == 0) { p[i] = i + 1; dfs(i); } else { p[i] = i; u[i] = true; } } for (int i = 1; i <= n; i++) { if (p[i] == 0 || p[p[i]] != n - i + 1) ans = false; } if (ans == false) { printf("-1"); return 0; } for (int i = 1; i <= n; i++) printf("%d ", p[i]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class LuckyPermutation { /* index , val i, f(i) f(i),n-i+1 n-i+1,n-f(i)+1 n-f(i)+1,i if n-i+1 = i -> i=(n+1)/2, cycle length = 1 if n-i+1 != i -> f(i)!=n-f(i)+1 to be a permutation too, cycle length = 4, must total n mod 4 = 1 or 0, then choose f(i) = i+1 because it's nice to see how the array will be filled up. Maybe it's possible to choose a different f(i) too? */ public static void main(String[] args){ FastScanner sc = new FastScanner(); int n = sc.nextInt(); if(n%4 <= 1){ int[] a = new int[n+1]; for(int i=1;i<=n/2;i+=2){ a[i] = i+1; a[i+1] = n-i+1; a[n-i+1] = n-i; a[n-i] = i; } if(n%4 == 1){ a[(n+1)/2] = (n+1)/2; } for(int i=1;i<=n;i++){ System.out.print(a[i] + " "); } }else{ System.out.println(-1); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> const int MAXN = 100000 + 9; int a[MAXN]; int main() { int n, i; scanf("%d", &n); if (n % 4 > 1) { puts("-1"); return 0; } if (n % 4) { a[n / 2 + 1] = n / 2 + 1; } for (i = 1; i * 2 < n; i += 2) { a[n - i] = i; a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; } for (i = 1; i < n; ++i) printf("%d ", a[i]); printf("%d\n", a[n]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = input() a = [i + 1 for i in range(n)] if n % 4 > 1: print -1 else: for i in range(0,n / 2, 2): a[n - i - 1] = n - i - 1 a[n - i - 2] = i + 1 a[i] = i + 2 a[i + 1] = n - i print " ".join(map(str,a))
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 100; int p[maxn]; int main() { int n; scanf("%d", &n); int i, j; if (n == 1) { printf("1\n"); return 0; } if (n % 2 == 0) { if (n % 4) { printf("-1\n"); return 0; } int l, r; l = 0; r = n - 1; while (l < r) { p[l] = l + 1; p[l + 1] = r; p[r] = r - 1; p[r - 1] = l; l += 2; r -= 2; } } else { if ((n - 1) % 4) { printf("-1\n"); return 0; } p[n / 2] = n / 2; int l = 0; int r = n - 1; while (l < n / 2 && r > n / 2) { p[l] = l + 1; p[l + 1] = r; p[r] = r - 1; p[r - 1] = l; l += 2; r -= 2; } } for (i = 0; i < n; ++i) printf("%d ", p[i] + 1); puts(""); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) { cout << "1\n"; return 0; } else if (n % 4 == 2 || n % 4 == 3) { cout << "-1\n"; return 0; } int arr[n + 1], k = 2; if (n % 2 == 1) { arr[(n + 1) / 2] = (n + 1) / 2; } int i = 1; while (i < n / 2) { arr[i] = k; arr[arr[i]] = n - i + 1; arr[arr[arr[i]]] = n - i; arr[arr[arr[arr[i]]]] = k - 1; i = i + 2; k = k + 2; } for (int i = 1; i <= n; i++) cout << arr[i] << " "; cin >> n; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
''' from itertools import permutations def main(): for num in xrange(1, 12): print num per = range(1, num+1) for it in permutations(per, num): if judge(it) == True: print it def judge(tup): n = len(tup) for i in xrange(n): if not tup[ tup[i]-1 ] == n-i: return False else: return True if __name__ == '__main__': main() ''' n = input() if n % 4 == 2 or n % 4 == 3: print -1 elif n % 4 == 0 or n % 4 == 1: end = n/2 + 1 for i in xrange(2, end, 2): print i, n+2-i, if n % 4 == 1: print n/2 + 1, begin = (n-2) / 2 for i in xrange(begin, 0, -2): print i, n-i,
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n). A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; __inline bool nextInt(int &val) { char ch; int sgn = 1; while ((ch = getchar()) != EOF) { if (ch == '-') sgn = -1; if (ch >= '0' && ch <= '9') break; } if (ch == EOF) return false; val = (int)(ch - '0'); while (true) { ch = getchar(); if (ch >= '0' && ch <= '9') { val = 10 * val + (int)(ch - '0'); } else break; } val *= sgn; return true; } __inline bool nextString(string &s) { char ch; while ((ch = getchar()) != EOF) { if (ch >= 33 && ch <= 126) break; } if (ch == EOF) return false; s = string(1, ch); while (true) { ch = getchar(); if (ch >= 33 && ch <= 126) { s = s + string(1, ch); } else break; } return true; } int n, p[300888]; int main() { ios_base::sync_with_stdio(false); scanf("%d", &n); memset(p, 0, sizeof(p)); ; if (n % 2 == 1) p[(n + 1) / 2] = (n + 1) / 2; for (int i = (1); i < (n + 1); i++) if (p[i] == 0) { int lastI = i, lastP = i + 1; p[lastI] = lastP; while (p[lastP] == 0) { p[lastP] = n - lastI + 1; lastI = lastP; lastP = p[lastP]; } } bool ok = true; for (int i = (1); i < (n + 1); i++) if (p[p[i]] != (n - i + 1)) { ok = false; break; } if (!ok) printf("-1\n"); else { for (int i = (1); i < (n + 1); i++) printf("%d ", p[i]); printf("\n"); } return 0; }
CPP