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
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()) ans = {} start = 1 end = n while end - start + 1 >= 4: ans[start] = start + 1 ans[start + 1] = end ans[end] = end - 1 ans[end - 1] = start start += 2 end -= 2 if (end - start + 1) <= 1: if end - start + 1 == 1: ans[end] = end for i in range(1, n + 1): print ans[i], # print '\n' 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
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 { static StringTokenizer st; static BufferedReader in; static PrintWriter pw; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); if (n==1) { System.out.println(1); return; } if (n % 4==2 || n % 4==3) System.out.println(-1); else { int min = 1, max = n; int[]ans = new int[n+1]; int left = 1, right = n; for (int i = 1; i <= n/4; i++) { ans[left] = min+1; ans[left+1] = max; ans[right] = max-1; ans[right-1] = min; min += 2; max -= 2; left += 2; right -= 2; } if (n % 2==1) ans[n/2+1] = n/2+1; for (int i = 1; i <= n; i++) { pw.print(ans[i]+" "); } pw.println(); } // int[]p = new int[n+1]; // for (int i = 1; i <= n; i++) { // p[i] = i; // } // while (true) { // boolean f = true; // for (int i = 1; i <= n; i++) { // if (p[p[i]] != n-i+1) { // f = false; // break; // } // } // if (f) { // for (int i = 1; i <= n; i++) { // System.out.print(p[i]+" "); // } // return; // } // int ind1 = n; // while (ind1 > 1 && p[ind1] < p[ind1-1]) // ind1--; // if (ind1==1) // break; // int ind2 = n; // while (p[ind2] < p[ind1-1]) // ind2--; // int t = p[ind1-1]; // p[ind1-1] = p[ind2]; // p[ind2] = t; // for (int i = 0; i+ind1 < n-i; i++) { // t = p[ind1+i]; // p[ind1+i] = p[n-i]; // p[n-i] = t; // } // } pw.close(); } private static int nextInt() throws IOException{ return Integer.parseInt(next()); } private static long nextLong() throws IOException{ return Long.parseLong(next()); } private static double nextDouble() throws IOException{ return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } } /* 8 2 8 4 6 3 5 1 7 9 2 9 4 7 5 3 6 1 8 */
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 double eps(1e-8); int p[110000]; void did(int a, int b, int n) { for (int i = (1); i <= (4); ++i) { p[a] = b; int tmp = b; b = n - a + 1; a = tmp; } } bool solve(int n) { if (n % 4 == 2 || n % 4 == 3) return false; for (int i = (1); i <= (n); ++i) { if (p[i] == 0) { if (i == (n + 1) / 2 && n % 2 == 1) { p[i] = i; } else { did(i, i + 1, n); } } } return true; } int main() { int n; scanf("%d", &n); bool sol = solve(n); if (!sol) puts("-1"); else { for (int i = (1); i <= (n); ++i) { if (i != 1) printf(" "); printf("%d", p[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; int perm[100010]; int main(void) { int n; scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); } else { for (int i = 1; i <= n / 2; i++) { if (i % 2 == 1) { perm[i] = n - i; int iter = i; for (int j = 0; j < 3; j++) { perm[perm[iter]] = n - iter + 1; iter = perm[iter]; } } } if (n % 2 == 1) { perm[(n + 1) / 2] = (n + 1) / 2; } for (int i = 1; i <= n; i++) { printf("%d%c", perm[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> using namespace std; template <class T> bool func(T a, T b) { return a < b; } int main(int argc, const char *argv[]) { long long *a, b, i, j, n, k; cin >> n; if (n % 4 > 1) cout << "-1"; else { b = n / 4; a = new long long[n + 1]; for (k = 1; k <= b; k++) { j = 2 * k - 1; a[j] = 2 * k; for (i = 0; i < 4; i++) { a[a[j]] = n - j + 1; j = a[j]; } } if (n % 4 == 1) { a[n / 2 + 1] = n / 2 + 1; } if (n % 4 == 3) { a[n / 2 + 1] = n / 2 + 1; a[n / 2 + 2] = n / 2; a[n / 2] = n / 2 + 2; } if (n % 4 == 2) { a[n / 2 + 1] = n / 2; a[n / 2] = n / 2 + 1; } for (i = 1; i < n + 1; 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.*; import java.util.*; public class Main{ static PrintWriter out=new PrintWriter(System.out); static void output(int l,int r){ if(l<r){ out.print(1+l+" "+r+" "); output(2+l,r-2); out.print(l+" "+(r-1)+" "); }else if(l==r) out.print(l+" "); } public static void main(String[]args){ Scanner cin=new Scanner(System.in); int n=cin.nextInt(); if(2>(3&n)) output(1,n); 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
from collections import Counter from itertools import cycle, product as prod, permutations as perm, combinations as comb, combinations_with_replacement as combr from sys import stdin, stdout read_ints = lambda: map(int, raw_input().split()) read_floats = lambda: map(float, raw_input().split()) def main(): n = input() b = [None] * (n + 1) cur = 1 if n % 4 == 0: for i in xrange(1, n/2+1, 2): o = (i, i+1, n-i+1, n-i) for j in xrange(4): b[o[j]] = o[j+1 & 3] print ' '.join(map(str, b[1:])) elif n % 4 == 1: for i in xrange(1, n/2+1, 2): o = (i, i+1, n-i+1, n-i) for j in xrange(4): b[o[j]] = o[j+1 & 3] b[n/2+1] = n/2+1 print ' '.join(map(str, b[1:])) else: print -1 if __name__ == '__main__': main()
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; #pragma comment(linker, "/STACK:102400000,102400000") template <class T> T _max(T x, T y) { return x > y ? x : y; } template <class T> T _min(T x, T y) { return x < y ? x : y; } template <class T> T _abs(T x) { return (x < 0) ? -x : x; } template <class T> T _mod(T x, T y) { return (x > 0) ? x % y : ((x % y) + y) % y; } template <class T> void _swap(T& x, T& y) { T t = x; x = y; y = t; } template <class T> void getmax(T& x, T y) { x = (y > x) ? y : x; } template <class T> void getmin(T& x, T y) { x = (x < 0 || y < x) ? y : x; } int TS, cas = 1; const int M = 100000 + 5; int n, a[M]; void run() { int i, j; if (n % 4 > 1) puts("-1"); else { for (i = 1; i < n / 2; i += 2) { j = i + 1; a[i] = j; a[n - i + 1] = n - j + 1; a[j] = n - i + 1; a[n - j + 1] = i; } if (n % 4 == 1) a[(n + 1) / 2] = (n + 1) / 2; for (i = 1; i < n; i++) printf("%d ", a[i]); printf("%d\n", a[n]); } } void preSof() {} int main() { preSof(); while ((~scanf("%d", &n))) run(); 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 MAXN = 100000 + 9; template <class T> inline T f_min(T a, T b) { return a < b ? a : b; } template <class T> inline T f_max(T a, T b) { return a > b ? a : b; } template <class T> inline T f_abs(T a) { return a > 0 ? a : -a; } template <class T> inline T f_gcd(T a, T b) { return b == 0 ? a : f_gcd(b, a % b); } int x[10] = {0, 1, 0, -1}; int y[10] = {1, 0, -1, 0}; bool vis[MAXN]; int arr[MAXN]; int main() { memset(vis, false, sizeof(vis)); int n; scanf("%d", &n); int i, j; i = n - 1, j = 1; int pos1 = 1, pos2 = 2; bool flag = true; for (; i >= j && i >= 1 && i <= n; i -= 2, j += 2) { if (vis[i] || vis[j]) { break; } vis[i] = vis[j] = true; arr[pos1] = i; arr[pos2] = j; pos1 += 2; pos2 += 2; } for (i = 1; i <= (n + 1) / 2; i++) { arr[n + 1 - i] = n + 1 - arr[i]; } if (n % 2) arr[(n + 1) / 2] = (n + 1) / 2; for (i = 1; i <= n; i++) { if (arr[arr[i]] != n - i + 1) { flag = false; break; } } if (!flag) { printf("-1\n"); } else { for (i = 1; i < n; i++) { printf("%d ", arr[i]); } printf("%d\n", arr[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[100010]; int n; void work(int l, int r) { if (l > r) return; if (l == r) { a[l] = l; return; } a[l] = l + 1; a[l + 1] = r; a[r - 1] = l; a[r] = r - 1; work(l + 2, r - 2); } int main() { cin >> n; if (n == 1) { puts("1"); } else if (n % 4 > 1) { puts("-1"); } else { work(1, n); for (int i = 1; i <= n; i++) { printf("%d", a[i]); if (i < n) printf(" "); } } }
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[100010], n; int main() { while (scanf("%d", &n) != EOF) { if (n % 4 > 1) puts("-1"); else { int stpos = 1, edpos = n - 1; int stval = 2, edval = 1; for (int i = 0; i < n / 4; i++) { a[stpos] = stval; a[edpos] = edval; a[stpos + 1] = n + 2 - stval; a[edpos + 1] = n - edval; stpos += 2; edpos -= 2; stval += 2; edval += 2; } if (n % 4 == 1) a[stpos] = stval - 1; 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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.Map; import java.util.Map.Entry; import java.io.BufferedReader; import java.util.BitSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author AnandOza */ 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); ALuckyPermutation solver = new ALuckyPermutation(); solver.solve(1, in, out); out.close(); } static class ALuckyPermutation { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] p = Permutations.identity(n); Util.reverse(p); List<List<Integer>> originalCycles = Permutations.findCycles(p); HashMap<Integer, List<List<Integer>>> cyclesByLength = new HashMap<>(); for (List<Integer> c : originalCycles) { int l = c.size(); if (!cyclesByLength.containsKey(l)) cyclesByLength.put(l, new ArrayList<>()); cyclesByLength.get(l).add(c); } List<List<Integer>> sqrtCycles = new ArrayList<>(); for (Map.Entry<Integer, List<List<Integer>>> e : cyclesByLength.entrySet()) { int len = e.getKey(); List<List<Integer>> cycles = e.getValue(); if (len % 2 == 1) { for (List<Integer> c : cycles) { List<Integer> sq = new ArrayList<>(); int k = len / 2; for (int i = 0; i < k; i++) { sq.add(c.get(i)); sq.add(c.get(i + k + 1)); } sq.add(c.get(k)); sqrtCycles.add(sq); } } else { if (cycles.size() % 2 == 1) { out.println(-1); return; } for (int i = 0; i < cycles.size(); i += 2) { List<Integer> a = cycles.get(i); List<Integer> b = cycles.get(i + 1); List<Integer> sq = new ArrayList<>(); for (int j = 0; j < len; j++) { sq.add(a.get(j)); sq.add(b.get(j)); } sqrtCycles.add(sq); } } } int[] answer = Permutations.fromCycles(n, sqrtCycles); for (int i = 0; i < answer.length; i++) answer[i]++; out.println(Util.join(answer)); } } static class InputReader { public final BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class Util { public static String join(int... i) { StringBuilder sb = new StringBuilder(); for (int o : i) { sb.append(o); sb.append(" "); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } public static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; } public static void reverse(int[] x) { for (int i = 0, j = x.length - 1; i < j; i++, j--) { swap(x, i, j); } } private Util() { } } static class Permutations { public static int[] identity(int n) { int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; } return p; } public static List<List<Integer>> findCycles(int[] permutation) { List<List<Integer>> cycles = new ArrayList<>(); int n = permutation.length; BitSet visited = new BitSet(n); for (int i = 0; i < n; i++) { if (visited.get(i)) continue; List<Integer> c = new ArrayList<>(); for (int j = i; c.isEmpty() || j != i; j = permutation[j]) { c.add(j); visited.set(j); } if (c.size() > 1) { cycles.add(c); } } return cycles; } public static int[] fromCycles(int n, List<List<Integer>> cycles) { int[] p = new int[n]; Arrays.fill(p, -1); for (List<Integer> c : cycles) { for (int i = 0; i + 1 < c.size(); i++) { p[c.get(i)] = c.get(i + 1); } p[c.get(c.size() - 1)] = c.get(0); } for (int i = 0; i < n; i++) { if (p[i] == -1) p[i] = i; } return p; } private Permutations() { } } }
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.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Egor Kulikov ([email protected]) */ 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 count = in.readInt(); if ((count & 3) >= 2) { out.printLine(-1); return; } int[] answer = new int[count]; if ((count & 1) == 1) answer[count >> 1] = count >> 1; for (int i = 0; i < (count >> 1); i += 2) { answer[i] = i + 1; answer[i + 1] = count - i - 1; answer[count - i - 1] = count - i - 2; answer[count - i - 2] = i; } for (int i = 0; i < count; i++) answer[i]++; out.printLine(answer); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.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
import java.io.*; import java.util.*; import java.lang.*; import java.math.BigInteger; import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; public class Main { static StringTokenizer st=new StringTokenizer(""); static BufferedReader br; /////////////////////////////////////////////////////////////////////////////////// public static void main(String args[]) throws FileNotFoundException, IOException, Exception { //Scanner in =new Scanner(new FileReader("input.txt")); //PrintWriter out = new PrintWriter(new FileWriter("output.txt")); //Scanner in=new Scanner(System.in); br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); //StringTokenizer st;//=new StringTokenizer(br.readLine()); int n=ni(); int arr[]=new int[n+1]; for(int i=n, j=1;i>0;i--,j++) arr[i]=j; int ans[]=new int[n+1]; int max=2; if(n%4<2) { for(int i=1;i<=n/2;i+=2) { ans[i]=max; ans[ans[i]]=arr[i]; ans[ans[ans[i]]]=arr[ans[i]]; ans[ans[ans[ans[i]]]]=arr[ans[ans[i]]]; max+=2; } if(n%2==1) ans[n/2+1]=n/2+1; for(int i=1;i<=n;i++) out.print(ans[i]+" "); } else out.print(-1); out.close(); } /////////////////////////////////////////////////////////////////////////////// private static class pair implements Comparable<pair> { int ind; long first; long second; pair() { ind=0; first=0; second=0; } pair(int i,long f,long s) { ind =i; first=f; second=s; } public int compareTo(pair o) { if(first < o.first) return 1; else if(first > o.first) return -1; else return 0; } } public static Integer ni() throws Exception { if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public static BigInteger nb()throws Exception { if(!st.hasMoreElements()) st=new StringTokenizer(br.readLine()); return BigInteger.valueOf(Long.parseLong(st.nextToken())); } public static Long nl() throws Exception { if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } public static Double nd()throws Exception { if(!st.hasMoreElements()) st=new StringTokenizer(br.readLine()); return Double.parseDouble(st.nextToken()); } public static String ns()throws Exception { if(!st.hasMoreElements()) st=new StringTokenizer(br.readLine()); 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.*; import java.util.*; public class C176 { public static void main(String[] args) { new C176(); } public C176() { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); if(n % 4 != 1 && n % 4 != 0) System.out.println("-1"); else { int[] arr = new int[n]; if(n % 2 == 1) arr[n / 2] = n / 2 + 1; for(int x = 0; x < n / 2; x++) { if(x % 2 == 0) arr[x] = x + 2; else arr[x] = n + 2 - arr[x - 1]; //System.out.println(x); } for(int x = 0; x < n / 2; x++) { arr[n - 1 - x] = n + 1 - arr[x]; //System.out.println(x); } for(int x = 0; x < n; x++) System.out.print(arr[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
import java.io.*; import java.util.*; public class CFB { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; private static final long MOD = 1000L * 1000L * 1000L + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; private static final String yes = "Yes"; private static final String no = "No"; void solve() throws IOException { int n = nextInt(); if (n % 4 == 2 || n % 4 == 3) { outln(-1); return; } if (n == 1) { outln(1); return; } int cnt = n / 4; List<List<Integer>> ls = new ArrayList<>(); List<Integer> init = new ArrayList<>(); init.add(1); init.add(n - 1); init.add(0); init.add(n - 2); ls.add(init); for (int i = 0; i < cnt - 1; i++) { List<Integer> prev = ls.get(ls.size() - 1); List<Integer> cur = new ArrayList<>(); for (int j = 0; j < prev.size(); j++) { if (j % 2 == 0) { cur.add(prev.get(j) + 2); } else { cur.add(prev.get(j) - 2); } } ls.add(cur); } List<Integer> res = new ArrayList<>(); for (int i = 0; i < ls.size(); i++) { res.add(ls.get(i).get(0)); res.add(ls.get(i).get(1)); } if (n % 4 != 0) { res.add(n / 2); } for (int i = ls.size() - 1; i >= 0; i--) { res.add(ls.get(i).get(2)); res.add(ls.get(i).get(3)); } for (int i = 0; i < res.size(); i++) { out((1 + res.get(i)) + " "); } } void shuffle(long[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); long tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } private void formatPrint(double val) { System.out.format("%.9f%n", val); } public CFB() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CFB(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; public class C { static PrintWriter out; static StreamTokenizer st; static BufferedReader bf; public static void main(String[] args) throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); st = new StreamTokenizer(bf); out = new PrintWriter(new OutputStreamWriter(System.out)); int n = nextInt(); if (n == 1){ System.out.println(1); return; } if (n%4 == 0 || ((n-1)%4) == 0){ PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int []ans = new int [n+1]; int c = n; int cnt = 0; if (n == 4){ System.out.println("2 4 1 3"); return; } while (c > cnt+1){ cnt++; ans[cnt] = cnt+1; ans[cnt+1] = c; ans[c] = --c; ans[c] = cnt; c--; cnt++; } if (n%2 != 0) ans[c] = c; for (int i = 1; i < ans.length; i++) { out.print(ans[i]+" "); } out.close(); }else{ System.out.println(-1); return; } } private static String nextLine() throws IOException { return bf.readLine().trim(); } private static double nextDouble() throws IOException{ st.nextToken(); return st.nval; } private static long nextLong() throws IOException{ st.nextToken(); return (long) st.nval; } private static int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } }
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 p[100005]; bool m[100005]; int cnt; int n; void rec(int k) { if (m[k]) return; m[k] = true; p[p[k]] = (n - (k) + 1); cnt++; rec(p[k]); } int main() { scanf(" %d", &n); for (int i = 1; i <= n; i++) if ((n - (i) + 1) == i) p[i] = i, m[i] = true, cnt++; int start = 0; for (int i = 1; i <= n; i++) if (!m[i]) { int start = 0; for (int k = i + 1; k <= n; k++) if (!m[k] && (n - (i) + 1) != k) { cnt++; m[i] = true; p[i] = start = k; p[p[i]] = (n - (i) + 1); break; } if (!start && cnt < n) { printf("-1\n"); return 0; } rec(start); } if (cnt < n) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) printf("%d ", p[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
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); sc.close(); if (n % 4 == 2 || n % 4 == 3) System.out.println(-1); else { int[] num = new int[n + 1]; for (int i = 1; i <= n / 2; i += 2) { num[i] = i + 1; num[i + 1] = n + 1 - i; num[n - i] = i; num[n + 1 - i] = n - i; } if (n % 2 == 1) num[n / 2 + 1] = n / 2 + 1; for (int i = 1; i < num.length; i++) System.out.print(num[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
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7, INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } long long inv(long long first) { return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; } inline int rd() { int first = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar(); return first; } const int N = 1e6 + 50; int n, a[N], b[N], c[N]; int main() { scanf("%d", &n); if (n % 4 > 1) return puts("-1"), 0; int l = 1, r = n - 1, now = 1; for (int i = 1; i <= n / 2; ++i) { if (i & 1) a[r] = now++, r -= 2; else a[l] = now++, l += 2; } if (n % 4 == 1) { l = n / 2, r = n / 2 + 1; a[r] = now++, r += 2; for (int i = n / 2 + 2; i <= n; ++i) { if (i & 1) a[l] = now++, l -= 2; else a[r] = now++, r += 2; } } else if (n % 4 == 0) { l = n / 2, r = n / 2 + 2; for (int i = n / 2 + 1; i <= n; ++i) { if (i & 1) a[r] = now++, r += 2; else a[l] = now++, l -= 2; } } for (int i = 1; i <= n; ++i) printf("%d ", a[i]); putchar(10); }
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 on @author: linhz ''' import sys usedNum=0 n=int(input()) p=[0 for i in range(n+1)] usedNum=0 if n%4==3 or n%4==2: print(-1) else: i=1 j=n a=1 b=n while j>i: p[i]=a+1 p[i+1]=b p[j]=b-1 p[j-1]=a i+=2 j-=2 a+=2 b-=2 if j==i: p[i]=a ans="" for i in range(1,n+1): ans+=str(p[i])+" " print(ans)
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 int OO = (int)2e9; const double eps = 1e-9; int arr[100005]; int main() { std::ios_base::sync_with_stdio(false); int i, n; cin >> n; if (!((n & 3) && ((n - 1) & 3))) { if (n & 1) arr[n / 2 + 1] = n / 2 + 1; for (i = 1; i < n / 2; i += 2) { arr[i] = i + 1; arr[i + 1] = n - i + 1; arr[n - i] = i; arr[n - i + 1] = n - i; } for ((i) = (1); (i) < (n + 1); (i)++) cout << arr[i] << " "; } else { cout << -1 << endl; return 0; } 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 A { void solve() throws IOException { int n = nextInt(); if (n % 4 == 2 || n % 4 == 3) { out.println("-1"); return; } int[] res = new int[n]; int down = 1; int up = n; for (int i = 0; up - down + 1 >= 4; i += 2) { res[i] = down + 1; res[i + 1] = up; res[n - i - 1] = up - 1; res[n - i - 2] = down; down += 2; up -= 2; } if (down == up) { res[n / 2] = down; } for (int i = 0; i < n; i++) { out.print(res[i] + " "); } } void run() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new A().run(); } BufferedReader br; PrintWriter out; StringTokenizer str; String next() throws IOException { while (str == null || !str.hasMoreTokens()) { str = new StringTokenizer(br.readLine()); } return str.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; int main() { int n; cin >> n; if (n % 4 == 3 || n % 4 == 2) { cout << "-1"; } if (n % 4 == 0) { for (int i = 0; i < n / 2; i += 2) { cout << (i + 2) << " "; cout << n - i << " "; } for (int i = 1; i < n / 2; i += 2) { cout << n / 2 - i << " "; cout << n / 2 + i << " "; } } if (n % 4 == 1) { for (int i = 0; i < n / 2; i += 2) { cout << (i + 2) << " "; cout << n - i << " "; } cout << n / 2 + 1 << " "; for (int i = 1; i < n / 2; i += 2) { cout << n / 2 - i << " "; cout << n / 2 + 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 n; int v[100005]; int main() { cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1; return 0; } for (int i = 2; i <= n / 2; i += 2) v[i - 1] = i, v[i] = n + 2 - i; if (n % 2) v[n / 2 + 1] = n / 2 + 1; for (int i = n / 2 - 1, j = 1; i >= 1; i -= 2, j += 2) v[(n + 1) / 2 + j] = i, v[(n + 1) / 2 + j + 1] = n - i; for (int i = 1; i <= n; i++) cout << v[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
n = int(raw_input()) if n % 4 > 1: print -1 exit() a = [i for i in xrange(n)] for i in xrange(0, n / 2, 2): a[i] = i + 1 a[i + 1] = n - i - 1 a[n - i - 1] = n - i - 2 a[n - i - 2] = i print ' '.join([str(i + 1) for i in 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.util.Scanner; public class C { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); if ((n - 1) % 4 == 1 || (n - 1) % 4 == 2) { System.out.println("-1"); return; } if (n % 2 == 0) { for (int i = 0; i < n/2; i += 2) { System.out.print(i + 2+ " "); System.out.print(n-i+" "); } int ab[][] = new int [n/2][2]; int ans = 0; for (int i = 0; i < n/2; i += 2) { ab[ans][0] = i+1; ab[ans][1] = n-i-1; ans++; } for (int i = ans-1; i >= 0; i--) { System.out.print(ab[i][0]+" "); System.out.print(ab[i][1]+" "); } } else { for (int i = 0; i < n/2; i += 2) { System.out.print(i + 2+ " "); System.out.print(n-i+" "); } int a[][] = new int [n/2][2]; int ans = 0; for (int i = 0; i < n/2; i += 2) { a[ans][0] = i+1; a[ans][1] = n-i-1; ans++; } System.out.print(n/2+1+" "); for (int i = ans-1; i >= 0; i--) { System.out.print(a[i][0]+" "); System.out.print(a[i][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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class TC { static int N; static void print( int[] a ) { for( int i=1; i<=N; i++ ) System.out.print( a[i] + " "); System.out.println(); } static void case0() { int cnt=0; int i=1; int[] a=new int[N+1]; Arrays.fill( a, 0 ); while( true ) { if( a[i] > 0 ) break; a[i]=i+1; int inv=N+1-i; a[i+1]=inv; a[inv]=inv-1; a[inv-1]=i; cnt+=4; i+=2; } print( a ); } static void case1() { int cnt=0; int i=1; int[] a=new int[N+1]; Arrays.fill( a, 0 ); int mid=( N+1 )/2; a[mid]=mid; while( true ) { if( a[i] > 0 ) break; a[i]=i+1; int inv=N+1-i; a[i+1]=inv; a[inv]=inv-1; a[inv-1]=i; cnt+=4; i+=2; } print( a ); } public static void main( String[] args ) throws IOException { BufferedReader br=new BufferedReader( new InputStreamReader( System.in )); N=Integer.parseInt( br.readLine() ); if( N%4 == 0 ) case0(); else if( N%4 == 1 ) case1(); else System.out.println( "-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
import java.util.*; public class Main { public static long calc(long k, long n) { long l = 2, r = k + 1, mid, tmp; while ( l < r ) { mid = (l + r) / 2; tmp = (k - mid + 1) * (k + mid) / 2 - (k - mid); if ( tmp <= n ) r = mid; else l = mid + 1; } return l; } public static void main(String[] strings) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.close(); if ( ((n % 4) > 1) ) { System.out.println("-1"); return; } int i, n2 = n / 2, gap; int[] p = new int[100005]; for (gap = n-1, i = 1; i <= n2; i += 2, gap -= 4) { p[i] = gap - 1 + i; p[i+1] = i; p[gap+i-1] = gap + i; p[gap+i] = 1 + i; } if ( (n % 4) == 1 ) p[n2+1] = n2 + 1; for ( i = 1; i <= n; ++i ) { System.out.print(p[i] + " "); } System.out.print("\r\n"); } }
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 solve() { int n; if (scanf("%d", &n) == EOF) return false; if (n % 4 > 1) { printf("-1\n"); return true; } vector<int> ans(n); for (int i = 0; i < n / 4; ++i) { ans[i * 2] = i * 2 + 1; ans[i * 2 + 1] = n - i * 2 - 1; ans[n - i * 2 - 1] = n - (i * 2 + 1) - 1; ans[n - (i * 2 + 1) - 1] = i * 2; } if (n % 2) ans[n / 2] = n / 2; for (int i = 0; i < n; ++i) printf("%d ", ans[i] + 1); return true; } 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
import java.io.InputStreamReader; import java.io.IOException; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Niyaz Nigmatullin */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, FastScanner in, FastPrinter out) { int n = in.nextInt(); int[] ans = new int[n]; int m = n; if ((n & 1) == 1) { ans[n / 2] = n / 2; --m; } if (m % 4 != 0) { out.println(-1); return; } for (int i = 0; i < n / 2; i += 2) { ans[i] = i + 1; ans[i + 1] = n - i - 1; ans[n - i - 1] = n - i - 2; ans[n - i - 2] = i; } for (int i = 0; i < n; i++) { ++ans[i]; } out.printArray(ans); } } class FastScanner extends BufferedReader { boolean isEOF; public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); if (isEOF && ret < 0) { throw new InputMismatchException(); } isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } public void printArray(int[] a) { for (int i = 0; i < a.length; i++) { if (i > 0) { print(' '); } print(a[i]); } 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; const int N = 100010; int main() { int n; scanf("%d", &n); if (n % 4 > 1) { printf("-1\n"); return 0; } int p[N]; 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; } if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; i++) printf("%d ", p[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
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int n=in.nextInt(); if(n%4>1){ out.println(-1); return; } int ans[]=new int[n]; for (int i=0;i<n/2;i+=2){ ans[i]=i+2; int t=i; for (int j=0;j<4;j++){ ans[ans[t]-1]=n-t; t=ans[t]-1; } } if(n%2==1) ans[n/2]=n/2+1; for (int i=0;i<n;i++) out.print(ans[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
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int arr[maxn]; int main() { int n; cin >> n; if ((n >> 1) & 1) return 0 * printf("-1\n"); for (int i = 0; i < n + 1; i++) arr[i] = i; for (int i = 1; i <= n; i += 2) { if (n & 1 && i == n / 2 + 1) i++; swap(arr[i], arr[i + 1]); } for (int i = 1; i <= n / 2; i += 2) { swap(arr[i], arr[n - i + 1]); } for (int i = 1; i <= n; i++) { cout << arr[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
n=int(input()) if n%4==2 or n%4==3: from sys import exit print(-1);exit() res,i=[0]*n,0 for i in range(0,n//2,2): res[i],res[i+1]=i+2,n-i res[n-i-1],res[n-i-2]=n-i-1,i+1 i+=2 if n%4==1:res[n//2]=n//2+1 print(*res)
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
import static java.lang.Math.*; import static java.math.BigInteger.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; public class A { final static boolean autoflush = false; public A () { int N = sc.nextInt(); sc = null; int [] res = new int [N]; switch(N%4) { case 1: int m = (N-1)/2; res[m] = m; case 0: for (int i : rep(N/4)) { int a = 2*i, b = a+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); default: 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 :-) */ static MyScanner sc = new MyScanner(); static class MyScanner { public String next() { readToken(); return new String(b, T[0], T[1] - T[0]); } public char nextChar() { readToken(); return (char)b[T[0]]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { readToken(); return calc(T[0], T[1]); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { readLine(); return new String(b, T[0], T[1] - T[0]); } public String [] next(int N) { String [] res = new String [N]; for (int i = 0; i < N; ++i) res[i] = next(); return res; } public Integer [] nextInt(int N) { Integer [] res = new Integer [N]; for (int i = 0; i < N; ++i) res[i] = nextInt(); return res; } public Long [] nextLong(int N) { Long [] res = new Long [N]; for (int i = 0; i < N; ++i) res[i] = nextLong(); return res; } public Double [] nextDouble(int N) { Double [] res = new Double [N]; for (int i = 0; i < N; ++i) res[i] = nextDouble(); return res; } public String [] nextLine(int N) { String [] res = new String [N]; for (int i = 0; i < N; ++i) res[i] = nextLine(); return res; } public String [] nextStrings() { readLine(); int s = T[0], c = 0; String [] res = new String[count(T[0], T[1])]; for (int i = T[0]; i < T[1]; ++i) if (b[i] == SPACE) { res[c++] = new String(b, s, i - s); s = i+1; } res[c] = new String (b, s, T[1] - s); return res; } public char [] nextChars() { readToken(); int c = 0; char [] res = new char[T[1] - T[0]]; for (int i = T[0]; i < T[1]; ++i) res[c++] = (char)b[i]; return res; } public Integer [] nextInts() { readLine(); int s = T[0], c = 0; Integer [] res = new Integer[count(T[0], T[1])]; for (int i = T[0]; i < T[1]; ++i) if (b[i] == SPACE) { res[c++] = (int)calc(s, i); s = i+1; } res[c] = (int)calc(s, T[1]); return res; } public Long [] nextLongs() { readLine(); int s = T[0], c = 0; Long [] res = new Long[count(T[0], T[1])]; for (int i = T[0]; i < T[1]; ++i) if (b[i] == SPACE) { res[c++] = calc(s, i); s = i+1; } res[c] = calc(s, T[1]); return res; } public Double [] nextDoubles() { readLine(); int s = T[0], c = 0; Double [] res = new Double[count(T[0], T[1])]; for (int i = T[0]; i < T[1]; ++i) if (b[i] == SPACE) { res[c++] = Double.parseDouble(new String(b, s, i - s)); s = i+1; } res[c] = Double.parseDouble(new String(b, s, T[1] - s)); return res; } String [][] nextStrings(int N) { String [][] res = new String [N][]; for (int i = 0; i < N; ++i) res[i] = nextStrings(); return res; } char [][] nextChars(int N) { char [][] res = new char [N][]; for (int i = 0; i < N; ++i) res[i] = nextChars(); return res; } Integer [][] nextInts(int N) { Integer [][] res = new Integer [N][]; for (int i = 0; i < N; ++i) res[i] = nextInts(); return res; } Long [][] nextLongs(int N) { Long [][] res = new Long [N][]; for (int i = 0; i < N; ++i) res[i] = nextLongs(); return res; } Double [][] nextDoubles(int N) { Double [][] res = new Double [N][]; for (int i = 0; i < N; ++i) res[i] = nextDoubles(); return res; } ////////////////////////////////////////////// private final static int MAX_SZ = (1 << 26); private final byte [] b = new byte[MAX_SZ]; private final int [] T = new int [2]; private int cur = 0; MyScanner () { try { InputStream is = new BufferedInputStream(System.in); while (is.available() == 0) Thread.sleep(1); start(); int off = 0, c; while ((c = is.read(b, off, MAX_SZ - off)) != -1) off += c; } catch (Exception e) { throw new Error(e); } } private void readLine() { int c; for (c = cur; b[c] != LF && b[c] != CR && b[c] != 0; ++c); T[0] = cur; T[1] = c; for (cur = c; b[cur] == LF || b[cur] == CR; ++cur); } private void readToken() { int c; for (c = cur; b[c] != SPACE && b[c] != LF && b[c] != CR && b[c] != 0; ++c); T[0] = cur; T[1] = c; for (cur = c; b[cur] == SPACE || b[cur] == LF || b[cur] == CR; ++cur); } private int count(int s, int e) { int cnt = 1; for (int i = s; i < e; ++i) if (b[i] == SPACE) ++cnt; return cnt; } private long calc(int s, int e) { long res = 0, f = 1; for (int i = s; i < e; ++i) if (b[i] == '-') f = -1; else res = 10 * res + b[i] - '0'; return res * f; } private static final char LF = '\n'; private static final char CR = '\r'; private static final char SPACE = ' '; } 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
import java.util.Scanner; public class c_176 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if ((n - 1) % 4 == 1 || (n - 1) % 4 == 2) { System.out.println(-1); return; } for (int i = 2; i <= n / 2; i += 2) { System.out.print(i + " " + ((n + 2) - i) + " "); } if (n % 2 == 1) { System.out.print(n / 2 + 1 + " "); } for (int i = n / 2 - 1; i >= 1; i -= 2) { System.out.print(i + " " + (n - 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
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } int p[123123]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; if ((n / 2) % 2) { cout << -1 << '\n'; return 0; } int s = 1; int l = n; if (n % 2 == 1) p[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n / 2; i += 2) { p[i] = s + 1; p[i + 1] = l; p[n - i + 1] = l - 1; p[n - i] = s; s += 2; l -= 2; } for (int i = 1; i <= n; i++) assert(p[p[i]] == n - i + 1); for (int i = 1; i <= n; i++) cout << p[i] << " "; cout << '\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 num[100005] = {0}; int flag[100005] = {0}; int main() { int n; scanf("%d", &n); if (n == 1) printf("1\n"); else if (n >= 4) { int kflag; if (n % 2 == 1) num[(n + 1) / 2] = (n + 1) / 2; while (1) { int flags = 0; kflag = 0; for (int i = 1; i <= n; i++) if (num[i] == 0) { flags = i; break; } int first = flags; int next = 1; if (flags != 0) while (first == next || num[next] != 0) next++; else break; while (num[next] == 0) { num[next] = n - first + 1; int t = next; next = num[next]; first = t; if (first == next) kflag = 1; } if (kflag == 1) break; } if (kflag == 1) { printf("-1\n"); exit(0); } for (int i = 1; i <= n; i++) if (i == n) printf("%d\n", num[i]); else printf("%d ", num[i]); } else printf("-1\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
import java.util.*; import java.lang.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if((n%4==2)||(n%4==3)){ System.out.println(-1); } else{ int[] permutation = new int[n]; int halfway = n/2; for(int i = 0; i<halfway;i+=2){ permutation[i]=i+2; permutation[n-1-i]=n-1-i; } for(int j = 1; j<halfway;j+=2){ permutation[j]=n+1-j; permutation[n-1-j]=j; } if(n%4==1){ permutation[halfway]=halfway+1; } for(int k = 0;k<n;k++){ System.out.print(permutation[k] + " "); } 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 vis[100006]; int a[100005]; int ans[100005]; int main() { ios_base::sync_with_stdio(false); int i, j, k, x, n, t; cin >> n; if (n % 4 != 1 && n % 4 != 0) { cout << -1 << endl; return 0; } for (i = 1; i <= (n + 1) / 2; i += 2) { if (i == (n + 1) / 2) { ans[i] = i; break; } ans[i] = i + 1; ans[i + 1] = n - i + 1; ans[n - i + 1] = n - i; ans[n - i] = i; } for (i = 1; i <= n; i++) { cout << ans[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> int arr[100010]; int main() { int n, counts, index, to, i, j, a, b; scanf("%d", &n); if (n == 1) { printf("1\n"); return 0; } if (n == 2) { printf("-1\n"); return 0; } if ((n % 4) > 1) { printf("-1\n"); return 0; } a = 1; b = 2; for (i = 0; i < n / 4; i++) { arr[a] = 2; index = a; to = b; for (j = 0; j < 4; j++) { arr[to] = n - index + 1; index = to; to = arr[to]; } a += 2; b += 2; } if (n % 2) arr[(n / 2) + 1] = (n / 2) + 1; for (i = 1; i <= n; i++) { printf("%d", arr[i]); if (i == n) printf("\n"); else printf(" "); } }
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 pow(long long b, long long e, long long m) { long long t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; } template <class T> inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; } template <class T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; } template <class T> inline T sqr(T x) { return x * x; } template <typename T> T gcd(T x, T y) { for (T t; x; t = x, x = y % x, y = t) ; return y; } template <class edge> struct Graph { vector<vector<edge> > adj; Graph(int n) { adj.clear(); adj.resize(n + 5); } Graph() { adj.clear(); } void resize(int n) { adj.resize(n + 5); } void add(int s, edge e) { adj[s].push_back(e); } void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); } vector<edge> &operator[](int t) { return adj[t]; } }; const int maxn = 110000; int p[maxn]; int main(int argc, char **argv) { ios_base::sync_with_stdio(false); int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1 << endl; return 0; } for (int i = 1, j = n; i <= j; i += 2, j -= 2) { if (i == j) { p[i] = j; break; } int s = i + 1, t = j - 1; p[i] = s; p[s] = j; p[j] = t; p[t] = i; } for (int i = 1; i <= n; ++i) { cout << p[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.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws IOException { (new Main()).solve(); } public Main() { } MyReader in = new MyReader(); PrintWriter out = new PrintWriter(System.out); void solve() throws IOException { // BufferedReader in = new BufferedReader(new // InputStreamReader(System.in)); // Scanner in = new Scanner(System.in); //Scanner in = new Scanner(new FileReader("forbidden-triples.in")); //PrintWriter out = new PrintWriter("forbidden-triples.out"); int n = in.nextInt(); int[] p = new int[n]; if (n == 1) { out.println(1); } else if (n == 2) { out.println(-1); } else if (n == 3) { out.println(-1); } else if (n % 4 == 1 || n % 4 == 0) { int b = 0; int e = n; while (n > 1) { p[b] = 2 + b; p[b + 1] = e; p[e - 2] = 1 + b; p[e - 1] = e - 1; n -= 4; b += 2; e -= 2; } if (n % 4 == 1) { p[p.length/2] = p.length/2 + 1; } for (int i = 0; i < p.length; i++) { out.print(p[i] + " "); } } else { out.println(-1); } out.close(); } }; class MyReader { private BufferedReader in; String[] parsed; int index = 0; public MyReader() { in = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt() throws NumberFormatException, IOException { if (parsed == null || parsed.length == index) { read(); } return Integer.parseInt(parsed[index++]); } public long nextLong() throws NumberFormatException, IOException { if (parsed == null || parsed.length == index) { read(); } return Long.parseLong(parsed[index++]); } public String nextString() throws IOException { if (parsed == null || parsed.length == index) { read(); } return parsed[index++]; } private void read() throws IOException { parsed = in.readLine().split(" "); index = 0; } public String readLine() throws IOException { return in.readLine(); } };
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 m, k; int n; int p[100100]; int main() { scanf("%d", &n); if (n == 1) { printf("1\n"); return 0; } if (n == 2 || n == 3) { printf("-1\n"); return 0; } int a = 1; set<int> per; for (int i = 2; i <= n; i++) per.insert(i); for (int i = 1; i <= n; i++) { if (p[i] == 0) { if (i == n / 2 + 1) { p[i] = i; break; } p[i] = i + 1; a = i; while (true) { if (p[p[a]] > 0) { if (p[p[a]] != n - a + 1) { printf("-1"); return 0; } else break; } p[p[a]] = n - a + 1; a = p[a]; } } } for (int i = 1; i <= n; i++) printf("%d ", p[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.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.Scanner; import java.util.ArrayList; public class Main { static int d[][]; static int N; static boolean used[]; static class point { int x = 0; int y = 0; } static point dats[]; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int N[] = new int[n+1]; int end = n; int beg = 1; if(n%4==2||n%4==3) { System.out.println(-1); return; } else while(end>beg) { N[beg] = beg+1; N[beg+1] = end; N[end] = end-1; N[end-1] = beg; end-=2; beg+=2; } if(n%2==1) N[n/2+1] = n/2+1 ; for(int i = 1;i<=n;i++) { System.out.print(N[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.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { public void solve() throws Exception { int n = sc.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 + 1; p[2 * i + 1] = n - 1 - 2 * i; p[n - 1 - 2 * i] = n - 2 - 2 * i; p[n - 2 - 2 * i] = 2 * i; } if (n % 4 == 1) { p[n / 2] = n / 2; } for (int i = 0; i < n; i++) { out.print((p[i] + 1) + " "); } out.println(); } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { //in = new BufferedReader(new FileReader(".in")); //out = new PrintWriter(new FileWriter(".out")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { new Solution().run(); //Thread thread = new Thread(null, new Solution(), "", (1 << 26)); //thread.start(); //thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { 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
#br = open('c.in') n = int(raw_input()) if n == 1: print 1 exit() a = [0 for i in range(n + 1)] i, c, a[i], s, l = 1, 0, 2, 2, 1 while c < n - 2: t = a[i] s = n - i + 1 if a[t] != 0: t += 2 s += 2 if s == l: l += 2 a[t] = s i = t c += 1 a = [l if x == 0 else x for x in a] can = 1 for i in range(1, n + 1): if a[a[i]] != n - i + 1: can = 0 print [-1, " ".join(map(str, a[1:]))][can]
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; long long power(long long a, long long n, long long lelo) { if (n == 0) return 1; if (n == 1) return a; if (n == 2) return (a * a) % lelo; if (n % 2) return (a * power(a, n - 1, lelo)) % lelo; else return power(power(a, n / 2, lelo), 2, lelo) % lelo; } void swap(long long &a, long long &b) { auto tm = a; a = b; b = tm; } const long long N = 1e5 + 5; const long long INF = 1e18; void solve() { long long n; cin >> n; if (((long long)n / 2) % 2) { cout << -1; return; } long long a[n]; for (long long(i) = (0); (i) < (n); (i)++) a[i] = i + 1; for (long long i = 0; i < n / 2; i++) { if (i % 2) swap(a[i], a[n - 1 - i]); } for (long long i = 0; i < n / 2; i += 2) swap(a[i], a[i + 1]); for (long long i = n / 2 + n % 2; i < n; i += 2) swap(a[i], a[i + 1]); for (long long(i) = (0); (i) < (n); (i)++) cout << a[i] << " "; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long double tt = 1.0 * clock(); long long t = 1; for (long long(i) = (1); (i) < (t + 1); (i)++) { solve(); if (i < t) cout << "\n"; } tt = 1.000000000 * (clock() - tt) / CLOCKS_PER_SEC; 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 arr[100015]; int N; int main() { cin >> N; int s = 1, e = N; bool fail = false; int left = N; while (true) { if (left == 0) { break; } else if (left == 1) { arr[s] = s; break; } else if (left >= 4) { arr[s] = s + 1; arr[s + 1] = e; arr[e] = e - 1; arr[e - 1] = s; s += 2; e -= 2; left -= 4; } else { fail = true; break; } } if (fail) cout << -1 << endl; else { for (int i = 1; i <= N; 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
n=int(input()) if (n//2)&1: print(-1) else: ans=[0]*(n+1) for i in range(1,(n//2)+1,2): ans[i]=i+1 ans[i+1]=n-i+1 ans[n-i+1]=n-i ans[n-i]=i if n%2: ans[(n//2)+1]=(n//2)+1 print(*ans[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; struct Node { int k, x; }; int p[100010]; int N; bool doit(int s) { int i; for (i = 1; i <= N; i++) if (p[i] == 0) { static Node q[100010]; int head = 1, tail = 2; q[1].k = s, q[1].x = i; p[s] = i; bool flag = true; while (head < tail) { int k = q[head].k, x = q[head].x; if (p[x] == 0) { p[x] = N - k + 1; q[tail].k = x; q[tail].x = N - k + 1; tail++; } if (p[x] != N - k + 1) { flag = false; for (int j = head; j < tail; j++) p[q[j].k] = 0; break; } if (p[N - x + 1] == 0) { p[N - x + 1] = k; q[tail].k = N - x + 1; q[tail].x = k; tail++; } if (p[N - x + 1] != k) { flag = false; for (int j = head; j < tail; j++) p[q[j].k] = 0; break; } head++; } if (flag) break; } if (i == N + 1) return false; return true; } int main() { cin >> N; memset(p, 0, sizeof(p)); for (int k = 1; k <= N; k++) if (p[k] == 0) { if (!doit(k)) { cout << "-1\n"; return 0; } } printf("%d", p[1]); for (int j = 2; j <= N; j++) printf(" %d", p[j]); 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
from math import * n = int(input()) if n == 1: print(1) elif n % 4 in [2, 3]: print(-1) else: ans = [0] * n for i in range(n // 2): if i & 1: ans[i] = n - 2 * (i // 2) else: ans[i] = 2 + i for i in range(1, (n // 2), 2): ans[ans[i] - 1] = n - i ans[n - i - 1] = n - (n - i) if n % 4 < 3 and n % 4 > 0: ans[n // 2] = ceil(n / 2) elif n % 4 == 3: ans[int(floor(n / 2))] = ans[int(floor(n / 2)) - 1] + 1 ans[int(ceil(n / 2))] = ans[int(ceil(n / 2)) - 1] - 2 print(*ans)
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 int NMAX = 100005; int A[NMAX]; int main() { ios::sync_with_stdio(false); int n; cin >> n; bool ok = true; if (n % 2 == 1) { if ((n - 1) % 4 == 0) { 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; } A[n / 2 + 1] = n / 2 + 1; } else { ok = false; } } else { if (n % 4 == 0) { 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; } } else { ok = false; } } if (!ok) { cout << "-1\n"; } else { for (int i = 1; i <= n; ++i) { cout << A[i] << ' '; } cout << '\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 main() { int n; cin >> n; if (n % 4 == 0 || n % 4 == 1) { int arr[n]; int j = 0; for (int i = 0; i < n / 4; i++) { arr[j] = j + 2; arr[j + 1] = n - j; arr[n - j - 1] = n - j - 1; arr[n - j - 2] = j + 1; j += 2; } if (n % 4 == 1) { arr[j] = j + 1; } for (int i = 0; i < n; i++) cout << arr[i] << ' '; } else { cout << -1 << 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> int num[100100]; int n; int main() { scanf("%d", &n); if (n % 4 != 0 && n % 4 != 1) { puts("-1"); return 0; } if (n % 2 == 1) num[n / 2] = n / 2 + 1; for (int i = 0, j = 0; j < n - n % 2; i += 2, j += 4) { num[i] = 2 + i; num[i + 1] = n - i; num[n - 1 - i] = n - 1 - i; num[n - 2 - i] = 1 + i; } for (int i = 0; i < n; i++) printf("%d ", num[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 n, ans[100010]; int main() { while (scanf("%d", &n) == 1) { int i; if ((n & 3) == 2 || (n & 3) == 3) { puts("-1"); continue; } for (i = 1; i <= n / 2; i += 2) { ans[i] = i + 1, ans[n - i + 1] = n - i; ans[i + 1] = n - i + 1, ans[n - i] = i; } if (n & 1) ans[(n + 1) >> 1] = (n + 1) >> 1; for (i = 1; i < n; i++) printf("%d ", ans[i]); printf("%d\n", ans[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
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Alex */ 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.ri(); if (n == 1){ out.printLine(1); return; } if (n % 4 == 2 || n % 4 == 3){ out.printLine(-1); return; } if (n % 4 == 0){ int[] res = new int[n]; for(int i = 0; i < res.length; i+=2) { int index = -1; if (i < res.length/2) index = i; else index = (res.length - 1) - i; res[index] = i+2; } for(int i = res.length - 2; i >= 0; i-=2){ int val = res.length - i - 1; int index = -1; if (i >= res.length/2) index = i; else index = res.length - i - 1; res[index] = val; } // if (!check(res)) throw new RuntimeException(); for(int i : res) out.print(i + " "); } else{ int[] res = new int[n]; int cur = 2; for(int i = 0; i < res.length; i+=2) { res[i] = cur; cur += 2; if (i == (res.length/2-2)) i+=2; } cur = 1; for(int i = res.length-2; i >= 0; i-=2){ res[i] = cur; cur += 2; if (i == res.length/2+1 || i == res.length/2) i++; } // if (!check(res)) throw new RuntimeException(); for(int i : res) out.print(i + " "); } // for(int length = 1; length <= 13; length++) { // int[] temp = new int[length]; // for(int i = 0; i < temp.length; i++) temp[i] = i + 1; // while(true){ // if (check(temp)) { // out.printLine(length, Arrays.toString(temp)); // break; // } // if(!ArrayUtils.nextPermutation(temp)) break; // } // } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int ri(){ return readInt(); } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void close() { writer.close(); } public void printLine(int i) { writer.println(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
#include <bits/stdc++.h> using namespace std; int v[100005]; int main() { int n; cin >> n; if (n % 4 == 1) { v[50001] = 1; int st = 50000; int dr = 50002; for (int i = 1, n1 = 1; i < n; i += 4, n1 += 4) { v[st] = n1 + 4; st--; v[st] = 2; st--; v[dr] = 1; dr++; v[dr] = n1 + 3; dr++; } int st1, dr1, adun; for (st1 = st + 3, dr1 = dr - 3, adun = 2; st1 < dr1; adun += 2) { v[st1++] += adun; v[st1++] += adun; v[dr1--] += adun; v[dr1--] += adun; } v[st1] += adun; for (int i = st + 1; i < dr; ++i) cout << v[i] << ' '; } else if (n % 4 == 2 || n % 4 == 3) { cout << -1; } else { v[50000] = 2; v[50001] = 4; v[50002] = 1; v[50003] = 3; int st = 49999, dr = 50004; for (int i = 4, n1 = 4; i < n; i += 4, n1 += 4) { v[st] = n1 + 4; st--; v[st] = 2; st--; v[dr] = 1; dr++; v[dr] = n1 + 3; dr++; } for (int st1 = st + 3, dr1 = dr - 3, adun = 2; st1 < dr1; adun += 2) { v[st1++] += adun; v[st1++] += adun; v[dr1--] += adun; v[dr1--] += adun; } for (int i = st + 1; i < dr; ++i) cout << v[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() { long long int i, j, k, n, m, t, a[100001]; cin >> n; if ((n % 4 != 0) && (n - 1) % 4 != 0) { cout << "-1"; return 0; } if (n % 4 == 0) { for (i = 1, j = n; i <= n / 2; i += 2, j -= 2) { a[i] = i + 1; a[j - 1] = i; a[i + 1] = j; a[j] = j - 1; } } else { for (i = 1, j = n; i <= n / 2; i += 2, j -= 2) { a[i] = i + 1; a[j - 1] = i; a[i + 1] = j; a[j] = j - 1; } a[(n / 2) + 1] = (n / 2) + 1; } for (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 n, l, r, i, j, k; int a[100005]; int main() { scanf("%d", &n); if ((n % 4 == 0) || ((n - 1) % 4 == 0)) { l = 1; r = n; for (k = 1; k <= n / 4; k++) { a[l] = l + 1; a[r] = r - 1; a[l + 1] = r; a[r - 1] = l; l += 2; r -= 2; } if ((n - 1) % 4 == 0) a[n / 2 + 1] = n / 2 + 1; for (i = 1; i <= n; i++) cout << a[i] << " "; } else cout << "-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
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class R176qCLuckyPermutation { static int ans[]; public static void main(String args[]) throws Exception { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = in.nextInt(); ans = new int[n + 1]; if(n % 4 == 0 || n % 4 == 1){ solve(1,n); if(!get(ans)) throw new Exception(); for(int i=1;i<=n;i++) w.print(ans[i] + " "); w.println(); } else w.println(-1); w.close(); } static public void solve(int l,int r){ if(l > r) return; if(l == r) ans[l] = l; else{ ans[l] = l + 1; ans[l + 1] = r; ans[r] = r - 1; ans[r-1] = l; solve(l+2,r-2); } } static public void go(int i,int arr[],boolean state[],int n){ if(i == n+1){ if(get(arr)) System.out.println(Arrays.toString(arr)); } else{ for(int j=1;j<=n;j++){ if(!state[j]){ state[j] = true; arr[i] = j; go(i+1,arr,state,n); state[j] = false; } } } } static public boolean get(int check[]){ boolean hmm = true; for(int i=1;i<check.length;i++) if(check[check[i]] != check.length - i){ hmm = false; //System.out.println(i); } return hmm; } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
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[100006]; int main() { int n; while (scanf("%d", &n) != EOF) { if (n == 1) puts("1"); else if (n == 2) puts("-1"); else { int delta = n; for (int i = 1; i <= n && delta > 0; i++) { if (i == 1) a[1] = 2; else if (i & 1) { a[i] = a[i - 1] - delta; } else a[i] = a[i - 1] + delta; if (delta == 3) delta = 2; else delta -= 2; } delta = n; int flag = 0; for (int i = n; i >= 1 && delta > 0; i--) { if (i == n) a[i] = n - 1; else if (flag == 0) { a[i] = a[i + 1] + delta; } else a[i] = a[i + 1] - delta; if (delta == 3) delta = 2; else delta -= 2; flag ^= 1; } flag = 1; for (int i = 1; i <= n; i++) { if (a[a[i]] != n - i + 1) { flag = 0; break; } } if (flag == 0) { puts("-1"); continue; } for (int i = 1; i <= n; i++) printf(i == 1 ? "%d" : " %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
import java.io.*; import java.util.*; import java.lang.*; public class A286A{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int t,l,r,max,min; int arr[]= new int[n+1]; if(n%2==0){ l=n/2; r=n/2 + 1; max=n; min=1; while(min<max){ arr[l]=max; arr[r]=min; max--; min++; if(r>l){ t=l; l=r+1; r=t-1; } else{ t=l; l=r-1; r=t+1; } } } else{ l=n/2; r=n/2 + 2; max=n; min=1; arr[l+1]=(n+1)/2; while(min<max){ arr[l]=max; arr[r]=min; max--; min++; if(r>l){ t=l; l=r+1; r=t-1; } else{ t=l; l=r-1; r=t+1; } } } int f=1; for(int i=1;i<n;i++){ if(n%2==1 && i==(n/2+1))continue; if(arr[i]==i || arr[i]==(n+1-i)){ f=0; break; } } if(f==0)System.out.println(-1); else{ for(int i=1;i<=n;i++) System.out.print(arr[i]+" "); System.out.print("\n"); } } }
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, p[1000007]; int main() { scanf("%d", &n); if (n == 1) { puts("1"); return 0; } if (n % 4 == 2 || n % 4 == 3) { puts("-1"); return 0; } if (n % 4 == 0) { for (int i = 0; i < n / 2; i += 2) { int a1 = i; int a2 = i + 1; int a3 = n - i - 1; int a4 = n - i - 2; p[a1] = a2; p[a2] = a3; p[a3] = a4; p[a4] = a1; } for (int i = 0; i < n; ++i) { printf("%d ", p[i] + 1); } puts(""); return 0; } int mid = n / 2; p[mid] = mid; vector<int> a(n - 1); for (int i = 0; i < mid; ++i) { a[i] = i; } for (int i = mid + 1; i < n; ++i) { a[i - 1] = i; } for (int i = 0; i < (n - 1) / 2; i += 2) { int a1 = a[i]; int a2 = a[i + 1]; int a3 = a[n - i - 2]; int a4 = a[n - i - 3]; p[a1] = a2; p[a2] = a3; p[a3] = a4; p[a4] = a1; } for (int 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* array; int n; cin >> n; if (n == 1) cout << 1 << endl; else if (n == 2 || n == 3 || ((n % 2 != 0) && ((n / 2) % 2 != 0)) || ((n % 2 == 0) && ((n / 2) % 2 != 0))) cout << -1 << endl; else { array = new int[n]; if (n & 1) array[(n / 2)] = (n / 2) + 1; int segment = n / 4; for (int it = 0; it < segment; it++) { int idx = it * 2 + 1; int segmentConst = (it + 1) * 2; array[idx - 1] = segmentConst; while (true) { int i; i = n + 1 - array[idx - 1]; array[i - 1] = idx; idx = i; if (idx == segmentConst) break; } } for (int i = 0; i < n; i++) { cout << array[i] << " "; } delete[] array; } 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 long long int mx = 1e6 + 10, inf = 1e9 + 10; int n, a[mx], g, h; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; if (n % 4 > 1) { cout << -1; return 0; } g = 2; h = n - 2; a[n / 2] = n / 2 + 1; for (long long int i = 0; i < n / 2; i++) { a[i] = g; a[n - i - 1] = n - g + 1; (i % 2 == 0) ? g += h : g -= h; h -= 2; } for (long long int i = 0; 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[100005]; int main() { int n; int i; cin >> n; if (n % 4 == 3 || n % 4 == 2) { cout << -1; return 0; } for (i = 1; i <= n / 2; i = 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; for (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
import sys def main() : n = [ int( x ) for x in sys.stdin.readline().strip().split() ][ 0 ] if n % 4 == 2 or n % 4 == 3 : print -1 return res = [] k = 1 while k + 1 <= n / 2 : res.append( n - k ) res.append( k ) k += 2 k -= 2 if n % 2 == 1 : res.append( n - int( n / 2 ) ) while k >= 1 : res.append( n - k + 1 ) res.append( k + 1 ) k -= 2 print ' '.join( [ str( x ) for x in res ] ) if __name__ == '__main__' : main()
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> int mas[(int)1e5 + 10]; int main() { int n; scanf("%d", &n); int m = n; int q = 0; int w = 0; while (n >= 4) { mas[q] = 2 + w; mas[q + 1] = n + w; mas[m - 1 - q] = n - 1 + w; mas[m - 2 - q] = 1 + w; q += 2; w += 2; n -= 4; } if (n == 1) { mas[q] = 1 + w; } else if (n == 2) { printf("-1"); return 0; } else if (n == 3) { printf("-1"); return 0; } for (int i = 0; i < m; i++) printf("%d ", mas[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 int maxn = 100111; int p[maxn]; bool vist[maxn]; int n; void dfs(int cur) { vist[cur] = 1; if (p[p[cur]]) { return; } p[p[cur]] = n - cur + 1; dfs(p[cur]); } int main() { scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) puts("-1"); else { if (n & 1) { p[n / 2 + 1] = n / 2 + 1; for (int i = 1, j = 2; i <= n / 2; i += 2, j += 2) p[i] = j; for (int i = 2, j = n; i <= n / 2; i += 2, j -= 2) p[i] = j; for (int i = n, j = n - 1; i > n / 2 + 1; i -= 2, j -= 2) p[i] = j; for (int i = n - 1, j = 1; i > n / 2 + 1; i -= 2, j += 2) p[i] = j; } else { for (int i = 1, j = 2; i <= n / 2; i += 2, j += 2) p[i] = j; for (int i = 2, j = n; i <= n / 2; i += 2, j -= 2) p[i] = j; for (int i = n, j = n - 1; i > n / 2; i -= 2, j -= 2) p[i] = j; for (int i = n - 1, j = 1; i > n / 2; i -= 2, j += 2) p[i] = j; } for (int i = (1); i <= (n); i++) printf("%d ", p[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 mark[100002], p[100002]; int main() { int t, n; while (scanf("%d", &n) != EOF) { int next = 2; mark[0] = 0; for (int i = 1; i <= n; i++) { mark[i] = 0; p[i] = 0; } int f = 0; for (int i = 1; i <= n; i++) { if (p[i] != 0) continue; int temp = n - i + 1; if (mark[temp] != 0) continue; if (temp != i) { if (next == i) { next = next + 1; while (mark[next] != 0) { next++; if (next == n && f == 0) { f = 1; next = 1; } } } } if (temp == i) { p[temp] = temp; mark[temp] = 1; while (mark[next] != 0) { next++; if (next == n && f == 0) { f = 1; next = 1; } } } else { p[i] = next; p[p[i]] = temp; mark[next] = 1; mark[temp] = 1; while (mark[next] != 0) { next++; if (next == n && f == 0) { f = 1; next = 1; } } } if (f == 1) break; } int ans = 0; for (int i = 1; i <= n; i++) { if (p[i] < 1 || p[i] > n || mark[i] == 0) { ans = -1; break; } } if (ans != 0) cout << ans; else { for (int i = 1; i <= n; i++) { cout << p[i] << " "; } } cout << "\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
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 A { static StringTokenizer st; static BufferedReader in; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); int[]a = new int[n+1]; for (int i = 1; i <= n/2; i += 2) { a[i] = i+1; int ind = i; for (int j = 1; j <= 3; j++) { if (a[a[ind]] != 0) break; a[a[ind]] = n-ind+1; ind = a[ind]; } } if (n % 2==1) a[n/2+1] = n/2+1; boolean f = true; for (int i = 1; i <= n; i++) { if (a[a[i]] != n-i+1 || a[i]==0) { f = false; break; } } if (f) { for (int i = 1; i <= n; i++) { pw.print(a[i]+" "); } } else pw.println(-1); pw.close(); } private static int nextInt() throws IOException{ return Integer.parseInt(next()); } private static long nextLong() throws IOException{ return Long.parseLong(next()); } private static double nextDouble() throws IOException{ return Double.parseDouble(next()); } private static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } 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.*; import java.math.*; import static java.lang.Math.*; import java.security.SecureRandom; import static java.util.Arrays.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import sun.misc.Regexp; import java.awt.geom.*; import sun.net.www.content.text.plain; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } StreamTokenizer in; PrintWriter out; //deb//////////////////////////////////////////////// public static void deb(String n, Object n1) { System.out.println(n + " is : " + n1); } public static void deb(int[] A) { for (Object oo : A) { System.out.print(oo + " "); } System.out.println(""); } public static void deb(boolean[] A) { for (Object oo : A) { System.out.print(oo + " "); } System.out.println(""); } public static void deb(double[] A) { for (Object oo : A) { System.out.print(oo + " "); } System.out.println(""); } public static void deb(String[] A) { for (Object oo : A) { System.out.print(oo + " "); } System.out.println(""); } public static void deb(int[][] A) { for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo + " "); } System.out.println(""); } } public static void deb(double[][] A) { for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo + " "); } System.out.println(""); } } public static void deb(long[][] A) { for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo + " "); } System.out.println(""); } } public static void deb(String[][] A) { for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo + " "); } System.out.println(""); } } ///////////////////////////////////////////////////////////// int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } long nextLong() throws IOException { in.nextToken(); return (long) in.nval; } void run() throws IOException { // in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt"))); // out = new PrintWriter(new FileWriter("output.txt")); in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } @SuppressWarnings("unchecked") void solve() throws IOException { // BufferedReader re= new BufferedReader(new FileReader("C:\\Users\\ASELA\\Desktop\\PROBLEMSET\\input\\F\\10.in")); // BufferedReader re= new BufferedReader(new InputStreamReader(System.in)); int n=nextInt(); int[] A=new int[n+1]; for (int i = 1; i <= n/2; i+=2) { A[i]=i+1; } for (int i = 2; i <= n/2; i+=2) { A[i]=n+2-A[i-1]; } for (int i = n-1; i >= (n+2)/2; i-=2) { A[i]=n-i; } for (int i = n; i >= (n+2)/2; i-=2) { A[i]=i-1; } if(n%4==1){ A[(n+1)/2]=(n+1)/2; } if(n%4==3||n%4==2){ out.println("-1"); return; } for (int i = 1; i <=n ; i++) { out.print(A[i] + " "); } 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.*; public class Task286A { public static void main(String... args) throws NumberFormatException, IOException { Solution.main(System.in, System.out); } static class Scanner { private final BufferedReader br; private String[] cache; private int cacheIndex; Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); cache = new String[0]; cacheIndex = 0; } int nextInt() throws IOException { if (cacheIndex >= cache.length) { cache = br.readLine().split(" "); cacheIndex = 0; } return Integer.parseInt(cache[cacheIndex++]); } long nextLong() throws IOException { if (cacheIndex >= cache.length) { cache = br.readLine().split(" "); cacheIndex = 0; } return Long.parseLong(cache[cacheIndex++]); } String next() throws IOException { if (cacheIndex >= cache.length) { cache = br.readLine().split(" "); cacheIndex = 0; } return cache[cacheIndex++]; } void close() throws IOException { br.close(); } } static class Solution { public static void main(InputStream is, OutputStream os) throws NumberFormatException, IOException { PrintWriter pw = new PrintWriter(os); Scanner sc = new Scanner(is); int n = sc.nextInt(); if (n % 4 > 1) { pw.println(-1); pw.flush(); sc.close(); return; } int[] retVal = new int[n + 1]; if (n % 4 == 1) { retVal[n / 2 + 1] = n / 2 + 1; } for (int i = 0; i < n / 4; i++) { retVal[i * 2 + 1] = (i + 1) * 2; retVal[i * 2 + 2] = n + 2 - retVal[i * 2 + 1]; retVal[n - i * 2 - 1] = retVal[i * 2 + 1] - 1; retVal[n - i * 2] = retVal[i * 2 + 2] - 1; } for (int i = 1; i <= n; i++) { pw.print(retVal[i]); pw.print(" "); } pw.flush(); sc.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 perm[100010 + 1]; using namespace std; int main() { int n; cin >> n; int s = 1; int e = n; while (e - s > 0) { perm[s] = s + 1; perm[s + 1] = e; perm[e] = e - 1; perm[e - 1] = s; s += 2; e -= 2; } if (s == e) perm[s] = s; if ((n % 4) > 1) cout << -1; else for (int i = 1; i <= n; i++) cout << perm[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 main() { int n; cin >> n; vector<int> arr(n + 1); for (int cnt = 1; cnt <= n; cnt++) { if (cnt * 2 == 1 + n) arr[cnt] = cnt; else if (cnt <= n / 2) { if (cnt % 2) arr[cnt] = cnt + 1; else arr[cnt] = n - cnt + 2; } else { if ((n - cnt + 1) % 2) arr[cnt] = cnt - 1; else arr[cnt] = n - cnt; } } for (int cnt = 1; cnt <= n; cnt++) { if (arr[arr[cnt]] != n - cnt + 1) { printf("-1\n"); return 0; } } for (int cnt = 1; cnt <= n; cnt++) { if (cnt - 1) printf(" "); printf("%d", arr[cnt]); } 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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package codeforces; import java.util.*; import java.io.*; /** * * @author Arysson */ public class LuckyPermutation { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); if (n%4==2 || n%4==3) System.out.print(-1); else if (n%2==1) for (int i=1; i<=n; i++) { if (i==n/2+1) System.out.printf("%d ", n/2+1); else if (i<=n/2) System.out.printf("%d ", i%2==1?i+1:n-i+2); else System.out.printf("%d ", i%2==1?i-1:n-i); } else for (int i=1; i<=n; i++) { if (i<=n/2) System.out.printf("%d ", i%2==1?i+1:n-i+2); else System.out.printf ("%d ", i%2==1?n-i:i-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
import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Lokesh Khandelwal */ 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(); int i,a[]=new int[n+1]; if(n==1) { out.printLine(1); return; } if(n%4==2||n%4==3) { out.printLine(-1); return; } int times=n/4; int c=1; while (c<=times) { a[2*c-1]=2*c; a[2*c]=n-(2*c-1)+1; a[n-(2*c-1)+1]=n-2*c+1; a[n-2*c+1]=n-(n-2*c+2)+1; c++; } if(n%4==1) a[n/2+1]=n/2+1; for(i=1;i<n;i++) { out.print(a[i]+" "); } out.printLine(a[i]); } } class InputReader { BufferedReader in; StringTokenizer tokenizer=null; public InputReader(InputStream inputStream) { in=new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try{ while (tokenizer==null||!tokenizer.hasMoreTokens()) { tokenizer=new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
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 mas[100000]; int main() { int N; scanf("%d", &N); if (N % 4 > 1) printf("-1\n"); else { int start = 0; int finish = N - 1; while (finish - start > 0) { mas[start] = start + 1; mas[start + 1] = finish; mas[finish] = finish - 1; mas[finish - 1] = start; start += 2; finish -= 2; } if (N % 4 == 1) mas[N / 2] = N / 2; for (int i = 0; i < N; i++) printf("%d ", mas[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 main() { int n; cin >> n; if (n % 4 == 1 || n % 4 == 0) { int *result = new int[n + 1]; if (n % 2 == 1) { result[(n + 1) / 2] = n / 2 + 1; } for (int i = 1; i < (n + 1) / 2; i += 2) { result[i] = i + 1; result[i + 1] = n + 1 - i; result[n + 1 - i] = n - i; result[n - i] = i; } for (int i = 1; i <= n; i++) cout << result[i] << ' '; delete[] result; } else cout << -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> int ans[100002]; int main() { int i, N, l, r; scanf("%d", &N); if (!(N % 4 <= 1)) { printf("-1\n"); return 0; } l = 2; r = N; for (i = 1; i <= N / 2; i += 2) { ans[i] = l; ans[i + 1] = r; ans[N + 1 - i] = r - 1; ans[N - i] = l - 1; l += 2; r -= 2; } if (N % 2 == 1) ans[N / 2 + 1] = N / 2 + 1; for (i = 1; i <= N; i++) (i == N) ? printf("%d\n", ans[i]) : printf("%d ", ans[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 int INF = 1000000000; int t, n, m, i, j, l = 0, k, c = 0, v = 0, x, r = 0, used[1000001] = {0}, a[1000001], b[1000001]; char s[1000001], s2[1000001]; int main() { scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("-1"); return 0; } if (n == 1) { printf("1"); return 0; } c = 1; v = 2; l = 2; r = n; if (n % 4 == 0) { for (i = 1, j = n; i <= n / 2, j >= (n / 2) + 1; i += 2, j -= 2) { a[i] = l; a[i + 1] = r; a[j] = r - 1; a[j - 1] = l - 1; l += 2; r -= 2; } for (i = 1; i <= n; i++) printf("%d ", a[i]); } else { for (i = 1, j = n; i <= n / 2, j >= (n / 2) + 2; i += 2, j -= 2) { a[i] = l; a[i + 1] = r; a[j] = r - 1; a[j - 1] = l - 1; l += 2; r -= 2; } a[(n / 2) + 1] = (n / 2) + 1; for (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
import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class A implements Runnable { public void solve() throws IOException { int n = in.nextInt(); if ( n % 4 == 2 || n % 4 == 3 ) { out.println( -1 ); return; } for ( int i = 1; i <= n / 4; i ++ ) { out.print( ( 2 * i ) + " " + ( n - 2 * i + 2 ) + " " ); } if ( n % 2 == 1 ) { out.print( ( n / 2 + 1 ) + " " ); } for ( int i = n / 4; i >= 1; i -- ) { out.print( ( 2 * i - 1 ) + " " + ( n - 2 * i + 1 ) + " " ); } out.println(); } public Scanner in; public PrintWriter out; A() throws IOException { in = new Scanner( System.in ); // in = new StreamTokenizer( new InputStreamReader( System.in ) ); out = new PrintWriter( System.out ); } // int nextInt() throws IOException { // in.nextToken(); // return ( int ) in.nval; // } void check( boolean f, String msg ) { if ( ! f ) { out.close(); throw new RuntimeException( msg ); } } void close() throws IOException { out.close(); } public void run() { try { solve(); close(); } catch ( Exception e ) { e.printStackTrace( out ); out.flush(); throw new RuntimeException( e ); } } public static void main( String[] args ) throws IOException { new Thread( new A() ).start(); } }
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; vector<int> a; int main() { cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1 << endl; return 0; } a.resize(n + 1); if (n % 4 == 1) a[(n + 1) / 2] = (n + 1) / 2; int l = 1, r = n; while (r - l >= 3) { a[l] = l + 1; a[l + 1] = r; a[r] = r - 1; a[r - 1] = l; l += 2; r -= 2; } for (int 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.util.*; public class C { static int n = 1; public static void main(String [] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out)); if(!(n%4 == 0 || n%4 == 1)) { System.out.println(-1); return; } int [] arr = new int[n]; int lb = 2; int ub = n; for(int i = 0 ; i < n/2 ; i+=2) { arr[i] = lb; arr[i+1] = ub; arr[n-i-1] = ub-1; arr[n-i-2] = lb-1; lb += 2; ub -= 2; } if(n % 2 != 0) arr[n/2] = n/2+1; for(int i = 0 ; i < n-1 ; i++) writer.print(arr[i]+" "); writer.println(arr[n-1]); writer.flush(); writer.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 N, ar[100005], mark[100005]; void sifir() { for (int i = 2, j = N; i <= N / 2; i += 2, j -= 2) printf("%d %d ", i, j); for (int i = N / 2 - 1, j = N / 2 + 1; i > 0; i -= 2, j += 2) printf("%d %d ", i, j); } void bir() { int orta = N / 2 + 1; for (int i = 2, j = N; j > orta; j -= 2, i += 2) printf("%d %d ", i, j); printf("%d ", orta); for (int i = orta + 1, j = orta - 2; j >= 0; j -= 2, i += 2) printf("%d %d ", j, i); } int main() { scanf("%d", &N); if ((N % 4) > 1) return puts("-1"), 0; if (N % 4 == 0) sifir(); else bir(); }
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, c, idx, a[100001]; int main() { scanf("%d", &n); if (n % 4 > 1) return printf("-1\n"), 0; c = 2, idx = 0; for (int i = 0; i < n / 4; ++i) { a[idx] = c; idx += 2, c += 2; } c = n, idx = 1; for (int i = 0; i < n / 4; ++i) { a[idx] = c; idx += 2, c -= 2; } c = n - 1, idx = n - 1; for (int i = 0; i < n / 4; ++i) { a[idx] = c; idx -= 2, c -= 2; } c = 1, idx = n - 2; for (int i = 0; i < n / 4; ++i) { a[idx] = c; idx -= 2, c += 2; } if (n % 2) a[n / 2] = n / 2 + 1; for (int 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
#include <bits/stdc++.h> using namespace std; int que[100010]; int main() { int n, i; scanf("%d", &n); if (!(n % 4 == 1 || n % 4 == 0)) { printf("-1\n"); return 0; } for (i = 1; i <= n / 2; i += 2) que[i] = i + 1; for (i = 2; i <= n / 2; i += 2) que[i] = n + 2 - i; for (i = n / 2 + 1; i <= n; i++) que[i] = n + 1 - que[n + 1 - i]; if (n % 2) que[n / 2 + 1] = (n + 1) / 2; for (i = 1; i <= n; i++) printf("%d ", que[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
n = input() ans = [0 for i in xrange(n + 1)] s = 1 e = n while e - s + 1 >= 4: ans[s] = s + 1 ans[s + 1] = e ans[e] = e - 1 ans[e - 1] = s s += 2 e -= 2 if e - s + 1 <= 1: if(e - s + 1 == 1): ans[e] = e for i in range(1, n + 1): print ans[i], 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; const int N = 1e5 + 7; int n, p[N]; void solve(int n) { if ((n & 1) && (n - 1) % 4) { puts("-1"); return; } if (!(n & 1) && n % 4) { puts("-1"); return; } if (n & 1) p[(n + 1) >> 1] = (n + 1) >> 1; for (int i = (1); i < ((n >> 1) + 1); ++i) if (!p[i]) { p[i] = i + 1; p[i + 1] = n + 1 - i; p[n + 1 - i] = n - i; p[n - i] = i; } for (int i = (1); i < (n + 1); ++i) printf("%d%c", p[i], " \n"[i == n]); } int main() { scanf("%d", &n); solve(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 arr[1000000]; int main() { int n; while (scanf("%d", &n) != -1) { if (n == 1) { printf("1\n"); continue; } if ((n & 1) && (n - 1) % 4) { printf("-1\n"); continue; } if (!(n & 1) && n % 4) { printf("-1\n"); continue; } for (int i = 1; i + i <= n; i += 2) arr[i] = i + 1, arr[i + 1] = n - i + 1, arr[n - i + 1] = n - i, arr[n - i] = i; if (n % 2) arr[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; ++i) printf(i > 1 ? " %d" : "%d", arr[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; const int MAXN = 1e5 + 5; const long long INF = 1000000000000000; const long long m = 1000000007; int a[MAXN]; long long binpow(long long v, long long st) { long long ans = 1, a = v; for (; st; st >>= 1) { if (st & 1) ans *= a; a *= a; } return ans; } int main() { long long n; cin >> n; long long p = log(1. * n) / log(2.0); long long ppow = binpow(2, p); if (n == 2 || n == 3) { printf("-1"); return 0; } if (n % 4 == 0 || n % 4 == 1) { int cur = 2; for (int i = 1; i < n / 2; i += 2) { a[i] = cur; a[n - i + 1] = n + 1 - cur; a[a[i]] = n + 1 - i; a[a[n - i + 1]] = n + 1 - (n - i + 1); cur += 2; } if (n & 1) a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; i++) { printf("%d ", a[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; int a[100005]; int n; int main() { while (cin >> n) { if (n % 4 == 2 || n % 4 == 3) { puts("-1"); continue; } if (n % 4) a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n + 1 - i; a[n - i] = i; a[n + 1 - i] = n - i; } for (int i = 1; i <= n; i++) printf("%d ", a[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
import java.io.*; import java.util.*; public class Main{ public static void main(String[]args){ Scanner cin=new Scanner(System.in); int n=cin.nextInt(),i=0,Q[]; PrintWriter out=new PrintWriter(System.out); if(2>(3&n)){ Q=new int[n--]; for(Q[n>>1]=2+n>>1;i<n;n-=2){ Q[i++]=n;Q[i]=i; Q[n-1]=1+n;Q[n]=++i; } for(int e:Q) out.print(e+" "); }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; const int MAX = 1e5 + 5; int ans[MAX]; int main() { int n; scanf("%d", &n); if (n % 4 >= 2) return 0 * printf("-1\n"); int a = 2, b = n, idx = 1; for (int i = 1; i <= n / 4; ++i) { ans[idx] = a; ans[idx + 1] = b; ans[n - idx + 1] = b - 1; ans[n - idx] = a - 1; a += 2; b -= 2; idx += 2; } if (n % 4) ans[(n + 1) / 2] = (n + 1) / 2; for (int i = 1; i <= n; ++i) printf("%d ", ans[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> #pragma comment(linker, "/stack:64000000") using namespace std; const int MOD = 1000000007; const int INF = 1000 * 1000 * 1000; const double EPS = 1e-9; const long double PI = acos(-1.0); int main() { int n; cin >> n; vector<int> a(n + 1); if (n % 4 == 2 || n % 4 == 3) { cout << -1 << endl; return 0; } if (n == 1) { cout << 1 << endl; return 0; } for (int i = 1; i < n / 2; i += 2) { a[i] = i + 1; int cur = i; for (int j = 0; j < 3; ++j) { a[a[cur]] = n - cur + 1; cur = a[cur]; } } if (n % 2) { a[n / 2 + 1] = n / 2 + 1; } for (int 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
#include <bits/stdc++.h> using namespace std; string tostr(long long x) { stringstream ss; ss << x; return ss.str(); } long long toint(string &s) { stringstream ss; ss << s; long long x; ss >> x; return x; } void print(vector<int> p) { for (int i = 0; i < p.size(); i++) { cout << p[i] << " "; } cout << endl; } int main() { string fname = "<stdin>"; string fname2 = ""; for (int i = 0; i < fname.length() - 4; i++) { fname2 += fname[i]; } fname = fname2; int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1; return 0; } vector<int> a(n); int c = 0; int f = 0, b = n - 1; int k = 2; int e = 0; while (c < n / 2) { a[f++] = k; a[b--] = n + 1 - k; k--; a[f++] = n + 1 - k; a[b--] = k; k += 3; c += 2; } if (n % 2) a[n / 2] = n / 2 + 1; print(a); 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; vector<int> v; void dog(int a, int b, int c, int d) { v[a - 1] = b; v[b - 1] = d; v[d - 1] = c; v[c - 1] = a; } int main() { cin >> N; v.resize(N); if (N % 4 > 1) { cout << -1 << endl; return 0; } if (N & 1) { int spc = N / 2 + 1; v[spc - 1] = spc; } int i = 1, j = N; while (j - i > 1) { dog(i, i + 1, j - 1, j); i += 2; j -= 2; } for (int a : v) cout << a << " "; 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; const int imod = 1e9 + 7; const long long lmod = 1e18 + 7; const int iinf = INT_MAX; const long long linf = LONG_MAX; const double pi = 2 * acos(0.0); const double eps = 1e-7; int n; int a[(int)1e6 + 134]; int main() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n; if (n % 4 >= 2) return cout << -1, 0; int l = 1, r = n; for (int i = 1; i <= n / 4; ++i) { a[l] = l + 1; a[l + 1] = r; a[r] = r - 1; a[r - 1] = l; l++; l++; r--; r--; } for (int i = 1; i <= n; ++i) cout << (i == l ? (n % 4 == 1 ? l : a[l]) : a[i]) << ' '; return 0; }
CPP