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
import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); if (n==1) { System.out.println("1"); } else if(n%4==2 || n%4==3) { System.out.println("-1"); } else { for(int i=0;i<n/4;i++) { System.out.print((i*2+2)+" "); System.out.print((n-i*2)+" "); } if(n%4==1) System.out.print((n/2+1)+" "); for(int i=n/4-1;i>0;i--) { System.out.print((i*2+1)+" "); System.out.print((n-i*2-1)+" "); } System.out.println("1 "+(n-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.PrintWriter; import java.util.Arrays; 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 / 2) * 2) % 4 != 0) { System.out.println(-1); } else { int[] perm = new int[n]; for (int i = 0; i < n; i++) { perm[i] = n - 1 - i; } int[] res = new int[n]; Arrays.fill(res, -1); for (int i = 0; i < n/2;) { res[i] = i + 1; int tmp=i; while (res[res[i]] == -1) { res[res[i]] = perm[i]; i = res[i]; } i =tmp+2; } if (n % 2 == 1) { res[n / 2] = n / 2; } PrintWriter out = new PrintWriter(System.out); for (int i = 0; i < n; i++) { out.print((res[i] + 1) + " "); } out.println(); out.flush(); } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> const int MAXN = 100010; int n, a[MAXN]; int main() { scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("-1"); return 0; } if (n % 4 == 0) { for (int i = 1; i <= n / 2 - 1; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } for (int i = 1; i <= n; i++) printf("%d ", a[i]); } else { for (int i = 1; i <= n / 2 - 1; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; i++) printf("%d ", a[i]); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); if (n % 4 > 1) System.out.println(-1); else { int[] P = new int[n]; int start = 0; int end = n - 1; int min = 1; int max = n; while (n > 1) { P[end - 1] = min++; P[start] = min++; P[start + 1] = max--; P[end] = max--; start += 2; end -= 2; n -= 4; } if (n == 1) P[start] = min; for (int i = 0; i < P.length; i++) System.out.print(P[i] + " "); System.out.println(); } } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
from itertools import permutations from sys import stdin def checkit(vector, upto=-1): if upto == -1: upto = len(vector) for i in range(0, upto): if vector[vector[i] - 1] != len(vector) - (i + 1) + 1: return False return True def calculate(n): numbers = list(range(1, n + 1)) result = [0] * n for i in range(0, n): if result[i] != 0: continue if i > 0 and checkit(result, i): continue expected = n - i for v in numbers: if v - 1 == i and expected != v: continue if v == expected: result[v-1] = v numbers.remove(v) break elif result[v - 1] == expected: numbers.remove(v) result[i] = v break elif result[v - 1] == 0: assert expected in numbers result[i] = v result[v - 1] = expected numbers.remove(v) numbers.remove(expected) break return result def calculate_v2(n): result = [0] * n first_sum = n + 2 second_sum = n nf = n i = 0 while nf > first_sum // 2: result[i] = first_sum - nf result[i + 1] = nf nf -= 2 i += 2 if n % 2 == 1: result[i] = i + 1 i = n - 1 while i > n // 2: result[i] = i result[i-1] = second_sum - i i -= 2 return result def main(): number = int(stdin.readline()) result = calculate_v2(number) if not checkit(result): print(-1) else: print(" ".join([str(v) for v in result])) if __name__ == "__main__": main()
PYTHON3
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100005] = {0}, i, j; cin >> n; if (n % 4 > 1) { cout << -1; return 0; } a[n / 2 + 1] = n / 2 + 1; for (j = 1; j <= n / 4; j++) { a[n - j * 2 + 1] = j * 2 - 1; i = n - j * 2 + 1; do { a[a[i]] = n - i + 1; i = a[i]; } while (i != n - j * 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; long long n, a[100005]; int main() { cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << "-1\n"; return 0; } long long f = 1; long long b = n; while (f < b) { if (f == b) { a[f] = f; break; } a[f] = f + 1; a[f + 1] = b; a[b - 1] = f; a[b] = b - 1; f += 2; b -= 2; } if (n & 1) a[(n + 1) / 2] = (n + 1) / 2; for (long long 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 d[100100]; int main() { int n; while (scanf(" %d", &n) == 1) { if (n % 4 == 0) { int r = n; for (int i = 1; i < n;) { d[i] = i + 1; d[i + 1] = n; d[n] = n - 1; d[n - 1] = i; i += 2; n -= 2; } for (int i = 1; i <= r; i++) printf("%d ", d[i]); puts(""); } else if ((n - 1) % 4 == 0) { int r = n; for (int i = 1; i < n;) { d[i] = i + 1; d[i + 1] = n; d[n] = n - 1; d[n - 1] = i; i += 2; n -= 2; } d[n] = n; for (int i = 1; i <= r; i++) printf("%d ", d[i]); puts(""); } else { puts("-1"); } } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[200009]; int main() { int i, j, k, n; scanf("%d", &n); if (n == 1) { printf("1\n"); } else if (n % 4 > 1) { printf("-1\n"); } else { for (i = 1; i < n / 2; i += 2) { a[i] = i + 1; j = i; while (!a[a[j]]) { a[a[j]] = n - j + 1; j = a[j]; } } if (!a[n / 2 + 1]) 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.BufferedWriter; import java.util.InputMismatchException; import java.io.InputStream; import java.util.NoSuchElementException; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nguyen Trung Hieu - [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 % 4 >= 2) { out.printLine(-1); return; } int left = 2; int right = count; while (left <= right) { out.print(left + " " + right + " "); left += 2; right -= 2; } if (count % 2 == 1) { out.print((count + 1) / 2 + " "); } left = (count - 2 - count % 2) / 2; right = (count + 2 + count % 2) / 2; while (left > 0) { out.print(left + " " + right + " "); left -= 2; right += 2; } } } 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 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 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 p[100500]; set<int> nused; int n; void sol() { if (n == 1) cout << 1; else if (((n - 1) % 4 == 0) || (n % 4 == 0)) { for (int i = (1); i <= (n); i++) nused.insert(i); memset(p, 0, sizeof(p)); int i = 1; for (int i = (1); i <= (n / 2); i++) { if (p[i] != 0) continue; int t = -1; for (set<int>::iterator it = nused.begin(); it != nused.end(); it++) { t = *it; if (t != i && t != n - i + 1) { nused.erase(it); break; } } if (t == -1) { cout << -100; break; } int at = i; p[at] = t; while (p[p[at]] == 0) { p[p[at]] = n - at + 1; at = p[at]; nused.erase(nused.find(p[at])); } } if ((n - 1) % 4 == 0) p[n / 2 + 1] = n / 2 + 1; for (int i = (1); i <= (n); i++) printf("%d ", p[i]); printf("\n"); } else cout << -1; } int main() { cin >> n; sol(); 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:336777216") #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using ui = unsigned int; template <typename T> using less_priority_queue = priority_queue<T, vector<T>, greater<T>>; ll const mod = 1e9 + 7; auto mt = mt19937(chrono::steady_clock::now().time_since_epoch().count()); inline namespace segment_tree { template <typename T> class Segment_Tree { public: vector<T> &v; vector<T> lazy, tree; int n; Segment_Tree(vector<T> &v) : v(v), n(v.size()) { tree.resize(4 * n); lazy.resize(4 * n, 0); build(0, n - 1, 1); } void build(int s, int e, int i) { if (s == e) return void(tree[i] = v[s]); int m = (s + e) / 2; build(s, m, 2 * i); build(m + 1, e, 2 * i + 1); tree[i] = tree[2 * i] + tree[2 * i + 1]; } void push(int s, int e, int i) { if (!lazy[i]) return; tree[i] += lazy[i] * (e - s + 1); if (s == e) return void(lazy[i] = 0); lazy[2 * i] += lazy[i]; lazy[2 * i + 1] += lazy[i]; lazy[i] = 0; } void update(int l, int r, T val) { update(0, n - 1, 1, l, r, val); } void update(int s, int e, int i, int l, int r, T val) { push(s, e, i); if (s > r || e < l) return; if (l <= s && e <= r) { tree[i] += val * (e - s + 1); if (s == e) return; lazy[2 * i] += val; lazy[2 * i + 1] += val; return; } int m = (s + e) / 2; update(s, m, 2 * i, l, r, val); update(m + 1, e, 2 * i + 1, l, r, val); tree[i] = tree[2 * i] + tree[2 * i + 1]; } T query(int l, int r) { return query(0, n - 1, 1, l, r); } T query(int s, int e, int i, int l, int r) { push(s, e, i); if (s > r || e < l) return T(0); if (l <= s && e <= r) return tree[i]; int m = (s + e) / 2; return query(s, m, 2 * i, l, r) + query(m + 1, e, 2 * i + 1, l, r); } void fill(vector<T> &v) { v.resize(n); fill(0, n - 1, 1, v); } void fill(int s, int e, int i, vector<T> &v) { push(s, e, i); if (s == e) return void(v[s] = tree[i]); int m = (s + e) / 2; fill(s, m, 2 * i, v); fill(m + 1, e, 2 * i + 1, v); } T get(int idx) { T res; get(0, n - 1, 1, idx, res); return res; } void get(int s, int e, int i, int idx, T &val) { push(s, e, i); if (s == e && s == idx) return void(val = tree[i]); int m = (s + e) / 2; if (idx <= m) get(s, m, 2 * i, idx, val); else get(m + 1, e, 2 * i + 1, idx, val); } void update(int idx, int val) { update(0, n - 1, 1, idx, val); } void update(int s, int e, int i, int idx, int val) { push(s, e, i); if (s == e && s == idx) return void(tree[i] += val); tree[i] += val; int m = (s + e) / 2; if (idx <= m) update(s, m, 2 * i, idx, val); else update(m + 1, e, 2 * i + 1, idx, val); } }; } // namespace segment_tree int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int n; cin >> n; int x = n % 4; if (x == 2 || x == 3) return cout << -1 << '\n', 0; if (x == 1) { vector<int> v(n, 0); v[n / 2] = 1; auto segt = Segment_Tree<int>(v); int s = n / 2; int e = n / 2; int len = 1; while (s) { len += 4; segt.update(s, e, 2); segt.update(s - 1, len); segt.update(s - 2, 2); segt.update(e + 1, 1); segt.update(e + 2, len - 1); s -= 2; e += 2; } vector<int> ans; segt.fill(ans); for (auto &x : ans) cout << x << " "; cout << '\n'; } else { vector<int> v(n, 0); v[(n - 4) / 2] = 2; v[(n - 4) / 2 + 1] = 4; v[(n - 4) / 2 + 2] = 1; v[(n - 4) / 2 + 3] = 3; auto segt = Segment_Tree<int>(v); int len = 4; int s = (n - 4) / 2; int e = s + 3; while (s) { len += 4; segt.update(s, e, 2); segt.update(s - 1, len); segt.update(s - 2, 2); segt.update(e + 1, 1); segt.update(e + 2, len - 1); s -= 2; e += 2; } vector<int> ans; segt.fill(ans); for (auto &x : ans) cout << x << " "; 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 main() { int n; scanf("%d", &n); int p[n + 1]; if (n % 4 <= 1) { if (n % 2 == 1) p[(n + 1) / 2] = (n + 1) / 2; for (int s = 1, e = n; s < e; s += 2, e -= 2) { p[s] = s + 1; p[s + 1] = e; p[e] = e - 1; p[e - 1] = s; } for (int i = 1; i <= n; ++i) printf("%d ", p[i]); printf("\n"); } else puts("-1"); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { private static void debug(Object... args) { System.out.println(Arrays.deepToString(args)); } public static void main(String[] rags) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); if(N%4 != 0 && N%4 != 1) { System.out.println(-1); return; } int[]v=new int[N]; for(int i=0;i<N/2;i+=2) { v[N - i - 1] = N - i - 1; v[N - i - 2] = i+1; v[i] = i+2; v[i+1] = N - i; } if(N%4==1) { v[N/2] = (N+1)/2; } for(int i=0;i<N;i++) { System.out.print(v[i] + " "); } System.out.println(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> int main() { int n, m, i, x, y; scanf("%d", &n); m = n / 2; if (m % 2 == 0) { x = m / 2; for (i = 0; i < x; i++) printf("%d ", m - i); for (i = x - 1; i >= 0; i--) printf("%d ", n - i); if (n % 2 == 1) printf("%d ", m + 1); for (i = 1; i <= x; i++) printf("%d ", i); for (i = 0; i < x; i++) printf("%d ", n - x - i); } else printf("-1"); printf("\n"); }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> const int N = 100010; int p[N]; int L[N]; int cnt[N]; int partner[N]; int list[N]; void combine(int i, int j) { assert(L[i] == L[j]); assert(L[i] > 0 && !(L[i] & 1)); int flag = 0; list[0] = i; list[1] = j; int n = 2; int ci = p[i], cj = p[j]; while (ci != i && cj != j) { list[n++] = ci; list[n++] = cj; ci = p[ci]; cj = p[cj]; } assert(ci == i && cj == j); assert(n == 2 * L[i]); list[n] = list[0]; for (int r = 0; r < n; r++) p[list[r]] = list[r + 1]; } int main() { int n; scanf("%d", &n); memset(cnt, 0, (n + 2) * sizeof(int)); memset(L, 0, (n + 2) * sizeof(int)); int i, j; for (i = 1; i <= n; i++) { p[i] = n - i + 1; } for (i = 1; i <= n; i++) { if (!L[i]) { L[i] = 1; int j = p[i]; while (!L[j]) { L[j] = -i; L[i]++; j = p[i]; } cnt[L[i]]++; } } bool flag = true; for (i = 2; i <= n; i += 2) { if (cnt[i] & 1) { flag = false; break; } } if (!flag) { printf("-1\n"); return 0; } memset(partner, 0, (n + 2) * sizeof(int)); for (i = 1; i <= n; i++) { if (L[i] > 0 && !(L[i] & 1)) { int my_partner = partner[L[i]]; if (my_partner == 0) { partner[L[i]] = i; } else { partner[L[i]] = 0; combine(i, my_partner); } } } for (i = 1; i < n; i++) printf("%d ", p[i]); printf("%d\n", p[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 main(int argc, char *argv[]) { int n; cin >> n; if (n % 4 == 0 || n % 4 == 1) { int a[n]; int low = 1, high = n; int i = 0; while (low < high) { a[i] = low + 1; a[i + 1] = high; a[n - i - 1] = high - 1; a[n - i - 2] = low; low += 2; high -= 2; i += 2; } if (low == high) a[i] = low; for (int i = 0; i < n; i++) { std::cout << a[i] << " "; } std::cout << std::endl; } else { std::cout << -1 << std::endl; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n=input() if n%4 not in {0,1}: print -1;exit() p=[(n+1)/2]*n for i in range(n/4): a=i*2 p[a],p[a+1],p[-a-2],p[-a-1]=a+2,n-a,a+1,n-a-1 print ' '.join(map(str, p))
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.*; import java.util.*; public class LuckyPermutation { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(f.readLine()); if (n % 4 == 2 || n % 4 == 3) { System.out.println(-1); return; } int[] a = new int[n+1]; for (int i = 1; i <= n/2-1; i += 2) { a[i] = i+1; a[i+1] = n-i+1; a[n-i+1] = n-i; a[n-i] = i; } if (n % 4 == 1) a[(n+1)/2] = (n+1)/2; for (int i = 1; i <= n; i++) System.out.print(a[i]+" "); System.out.println(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[100000 + 10]; int main() { int n; while (scanf("%d", &n) != EOF) { if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); continue; } for (int i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } if (n % 2 != 0) { a[n / 2 + 1] = n / 2 + 1; } for (int i = 1; i <= n; i++) printf("%d ", 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 N, P[100010]; int main(int argc, char *argv[]) { scanf("%d", &N); if (N % 4 == 2 || N % 4 == 3) { printf("-1\n"); return 0; } int K = N >> 1; for (int i = 1; i <= K; i++) if (P[i] == 0) { P[i] = i + 1; P[i + 1] = N - i + 1; P[N - i + 1] = N - (i + 1) + 1; P[N - (i + 1) + 1] = N - (N - i + 1) + 1; } if (N & 1) { P[K + 1] = K + 1; } for (int i = 1; i <= N; i++) { printf("%d", P[i]); printf(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; 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 (next == n && f == 1) break; } } } if (temp == i) { p[temp] = temp; mark[temp] = 1; while (mark[next] != 0) { next++; if (next == n && f == 0) { f = 1; next = 1; } if (next == n && f == 1) break; } } 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 (next == n && f == 1) break; } } 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.util.Scanner; public class A { public static final boolean DEBUG = false; Scanner sc; public void debug(Object o) { if (DEBUG) { int ln = Thread.currentThread().getStackTrace()[2].getLineNumber(); String fn = Thread.currentThread().getStackTrace()[2].getFileName(); System.out.println("(" + fn + ":" + ln+ "): " + o); } } public void pln(Object o) { System.out.println(o); } public void run() { sc = new Scanner(System.in); int n = sc.nextInt(); if (n==1) { pln(1); return; } if (n%4==2 || n%4==3) { pln(-1); return; } int[] p = new int[n+1]; if (n%4==0 || n%4 == 1) { for (int i=1; i<=n/2; i+=2) { p[i] = i+1; p[i+1] = n-i+1; p[n-i+1] = n-i; p[n-i] = i; } if (n%4==1) { p[n/2+1] = n/2+1; } } for (int i=1; i<=n; i++) { System.out.print(p[i]); System.out.print(" "); } System.out.println(); } public static void main(String[] args) { A t = new A(); t.run(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import static java.lang.Math.*; import static java.math.BigInteger.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class A { final static boolean autoflush = false; public A () { int N = sc.nextInt(); 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 :-) */ final static MyScanner sc = new MyScanner(); static class MyScanner { public String next() { newLine(); return line[index++]; } public char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { line = null; return readLine(); } public String [] nextStrings() { line = null; return readLine().split(" "); } public char [] nextChars() { return next ().toCharArray (); } public Integer [] nextInts() { String [] L = nextStrings(); Integer [] res = new Integer [L.length]; for (int i : rep(L.length)) res[i] = Integer.parseInt(L[i]); return res; } public Long [] nextLongs() { String [] L = nextStrings(); Long [] res = new Long [L.length]; for (int i : rep(L.length)) res[i] = Long.parseLong(L[i]); return res; } public Double [] nextDoubles() { String [] L = nextStrings(); Double [] res = new Double [L.length]; for (int i : rep(L.length)) res[i] = Double.parseDouble(L[i]); return res; } public String [] next (int N) { String [] res = new String [N]; for (int i : rep(N)) res[i] = sc.next(); return res; } public Integer [] nextInt (int N) { Integer [] res = new Integer [N]; for (int i : rep(N)) res[i] = sc.nextInt(); return res; } public Long [] nextLong (int N) { Long [] res = new Long [N]; for (int i : rep(N)) res[i] = sc.nextLong(); return res; } public Double [] nextDouble (int N) { Double [] res = new Double [N]; for (int i : rep(N)) res[i] = sc.nextDouble(); return res; } public String [][] nextStrings (int N) { String [][] res = new String [N][]; for (int i : rep(N)) res[i] = sc.nextStrings(); return res; } public Integer [][] nextInts (int N) { Integer [][] res = new Integer [N][]; for (int i : rep(N)) res[i] = sc.nextInts(); return res; } public Long [][] nextLongs (int N) { Long [][] res = new Long [N][]; for (int i : rep(N)) res[i] = sc.nextLongs(); return res; } public Double [][] nextDoubles (int N) { Double [][] res = new Double [N][]; for (int i : rep(N)) res[i] = sc.nextDoubles(); return res; } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = readLine().split(" "); index = 0; } } } static void print (Object o, Object... a) { printDelim(" ", o, a); } static void cprint (Object o, Object... a) { printDelim("", o, a); } static void printDelim (String delim, Object o, Object... a) { pw.println(build(delim, o, a)); } static void exit (Object o, Object... a) { print(o, a); exit(); } static void exit() { pw.close(); System.out.flush(); System.err.println("------------------"); System.err.println("Time: " + ((millis() - t) / 1000.0)); System.exit(0); } static void NO() { throw new Error("NO!"); } //////////////////////////////////////////////////////////////////////////////////// static String build (String delim, Object o, Object... a) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : a) append(b, p, delim); return b.toString().trim(); } static void append(StringBuilder b, Object o, String delim) { if (o.getClass().isArray()) { int L = java.lang.reflect.Array.getLength(o); for (int i : rep(L)) append(b, java.lang.reflect.Array.get(o, i), delim); } else if (o instanceof Iterable<?>) for (Object p : (Iterable<?>)o) append(b, p, delim); else b.append(delim).append(o); } //////////////////////////////////////////////////////////////////////////////////// static void statics() { abs(0); valueOf(0); asList(new Object [0]); reverseOrder(); } public static void main (String[] args) { new A(); exit(); } static void start() { t = millis(); } static java.io.PrintWriter pw = new java.io.PrintWriter(System.out, autoflush); static long t; static long millis() { return System.currentTimeMillis(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const double pi = acos(-1); const double EPS = 1e-9; long long binpowmod(long long a, long long b) { a %= MOD; long long ret = 1; while (b) { if (b & 1) ret = ret * a % MOD; a = a * a % MOD; b >>= 1; } return ret % MOD; } long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } const int N = 1 << 18; const int E = N * 2; class UnionFind { public: vector<int> p, rank, setSize; int numSets; UnionFind(int N) { setSize.assign(N, 1); numSets = N; rank.assign(N, 0); p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; } int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); } bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } void unionSet(int i, int j) { if (!isSameSet(i, j)) { numSets--; int x = findSet(i), y = findSet(j); if (rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; } else { p[x] = y; setSize[y] += setSize[x]; if (rank[x] == rank[y]) rank[y]++; } } } int numDisjointSets() { return numSets; } int sizeOfSet(int i) { return setSize[findSet(i)]; } }; void solve() { int n; cin >> n; vector<int> prefix, suffix; vector<bool> taken(n + 1); for (int i = 1; i <= n / 2; ++i) { if (i * 2 >= n - (i - 1) * 2) break; taken[i * 2] = true; taken[n - (i - 1) * 2] = true; prefix.push_back(i * 2); prefix.push_back(n - (i - 1) * 2); } for (int i = 1; i <= n / 2; ++i) { if (i * 2 - 1 >= n - i * 2 + 1) break; taken[i * 2 - 1] = true; taken[n - i * 2 + 1] = true; suffix.push_back(n - i * 2 + 1); suffix.push_back(i * 2 - 1); } for (int i = 1; i <= n; ++i) if (!taken[i]) prefix.push_back(i); reverse(suffix.begin(), suffix.end()); vector<int> ans; for (int i = 0; i < (int)prefix.size(); ++i) ans.push_back(prefix[i]); for (int i = 0; i < (int)suffix.size(); ++i) ans.push_back(suffix[i]); bool good = true; for (int i = 0; i < n; ++i) { int ind = ans[i] - 1; int val = n - (i + 1) + 1; if (ans[ind] != val) good = false; } if (good) for (int x : ans) cout << x << ' '; else cout << -1; cout << '\n'; } int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tc = 1; while (tc--) { solve(); } }
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:250777216") using namespace std; const int MOD = int(1e9) + 7; const int HMOD = (1 << 22) - 1; int p1[110000] = {}; int n; int main() { scanf("%d", &n); int num = n; int i = 1; for (i = 1; num > 3; i += 2) { p1[i + 1] = i; p1[i] = n - i; p1[n - i] = n - i + 1; p1[n - i + 1] = i + 1; num -= 4; } if (num == 1) p1[i] = i; if (num == 3 || num == 2) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) printf("%d ", p1[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.*; import java.lang.*; import java.io.*; import java.text.*; /** * @author soumitri12 */ /* Name of the class has to be "Main" only if the class is public*/ public class CF287C { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Node { long pp; long a, b; Node(long x, long y) { a = x; b = y; pp = a * b; } } static class Comp implements Comparator<Node> { public int compare(Node o1, Node o2) { if (o1.pp > o2.pp) { return 1; } else { return -1; } } } static int gcd(int x, int y) { if(y==0) return x; else return gcd(y,x%y); } static long mod_pow(long a, long b, long mod) { a%=mod; if(b==0) return 1%mod; if((b&1)==1) return a*(mod_pow(a,b-1,mod))%mod; else { long u=mod_pow(a,b>>1,mod); return (u*u)%mod; } } static long _pow(long a, long b) { if(b==0) return 1; if((b&1)==1) return a*_pow(a,b-1); else { long u=_pow(a,b>>1); return u*u; } } static int modInv(int a, int m) { if(gcd(a,m)!=1) return -999; else return (a%m+m)%m; } static boolean isPowerOfTwo(int x) { return x!=0 && ((x&(x-1)) == 0); } static boolean isprime(int x) { for(int i=2;i<=Math.sqrt(x);i++) { if(x%i==0) return false; } return true; } static boolean prime[]; static final int INT_MAX=1000007; static void sieve() { prime=new boolean[INT_MAX]; Arrays.fill(prime,true); prime[0]=prime[1]=false; for(int i=2;i<=Math.sqrt(INT_MAX);i++) { if(prime[i]) { for(int j=i*2;j<INT_MAX;j+=i) prime[j]=false; } } } static class Pair { int ff,ss; public Pair(int ff,int ss) { this.ff=ff; this.ss=ss; } } public static void main(String[] args) { FastReader sc=new FastReader(); PrintWriter out=new PrintWriter(System.out); StringBuffer sb=new StringBuffer(); //your code starts here int n=sc.nextInt(); int p[]=new int[n+1]; if((n&1)==1) p[(n+1)>>1]=(n+1)>>1; if((n&2)>=2) { System.out.println(-1); return; } for(int i=1;i<=n/2;i+=2) { p[i]=i+1; p[i+1]=n-i+1; p[n-i+1]=n-i; p[n-i]=i; } for(int i=1;i<=n;i++) out.print(" "+p[i]); 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
# -*- coding:utf-8 -*- def PrintPermutation(n): if n % 4 != 0 and (n-1) %4 != 0: print '-1' return data = [0] * (n + 1) if n%2 == 1: data[(n+1)/2] = (n+1)/2 i0 = 1 i1 = n while True: j0 = i0+1 j1 = i1-1 if j0 >= j1 : break; data[i0] = j0 data[i1] = j1 data[j0] = i1 data[j1] = i0 i0 = i0 + 2 i1 = n + 1 - i0 print '%s' % ' '.join(map(str, data[1:])) if __name__ == "__main__": n = raw_input() PrintPermutation(int(n))
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
n = int(raw_input()) p = [0] * (n+1) lo, hi = 1, n possible = True while hi - lo > 2: if not p[hi-1]: p[hi-1] = lo if not p[lo]: p[lo] = lo+1 if not p[hi]: p[hi] = hi-1 if not p[lo+1]: p[lo+1] = hi lo += 2 hi -= 2 n -= 4 # lo is the first zero if n == 1: p[lo] = lo elif n != 0: possible = False if possible: print ' '.join(map(str, p[1:])) else: print -1
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); } const long double pi = 3.1415926535897932384626433832795; const long double eps = 1e-9; const int INF = (int)1e9; const int N = (int)1e5 + 10; long long n, k; int p[20]; void gen(int n) { cout << "N = " << n << endl; for (int i = 0; i < n; i++) p[i] = i; do { bool first = true; for (int i = 0; i < n && first; i++) if (p[p[i]] != n - i - 1) first = false; if (first) { for (int i = 0; i < n; i++) cout << p[i] + 1 << " "; cout << "\n"; } } while (next_permutation(p, p + n)); } int a[N]; set<int> second; vector<int> ost; int main() { cout.flags(ios::fixed); cout.precision(2); ios_base::sync_with_stdio(0); int n; cin >> n; if (n == 1) { cout << 1; return 0; } if (n % 4 >= 2) { cout << -1; return 0; } for (int i = 1; i <= n; ++i) second.insert(i); for (int i = 1; i <= n / 2; i += 2) a[i] = i + 1; for (int i = n; i > (n + 1) / 2; i -= 2) a[i] = i - 1; for (int i = 1; i <= n; ++i) if (a[i]) second.erase(a[i]); for (set<int>::iterator it = second.begin(); it != second.end(); it++) ost.push_back(*it); reverse(ost.begin(), ost.end()); for (int i = 1, j = 0; i <= n; ++i) if (!a[i]) a[i] = ost[j++]; for (int i = 1; i <= n; ++i) printf("%d ", a[i]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.util.*; import java.io.*; public class Main implements Runnable { public void solve() throws IOException { int N = nextInt(); int[] ans = new int[N]; if((N%4) > 1){ System.out.println(-1); return; } if(N % 4 == 1){ ans[N/2] = N/2 + 1; } int A = 2, B = N, C = N - 1, D = 1; for(int i = 0; i < N/2; i += 2){ ans[i] = A; ans[i+1] = B; ans[N - 1 - i] = C; ans[N - 2 - i] = D; A += 2; B -= 2; C -= 2; D += 2; } StringBuilder sb = new StringBuilder(); for(int i = 0; i < N; i++) sb.append(" " + ans[i]); System.out.println(sb.toString().substring(1)); } //----------------------------------------------------------- public static void main(String[] args) { new Main().run(); } public void print1Int(int[] a){ for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } public void print2Int(int[][] a){ for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[0].length; j++){ System.out.print(a[i][j] + " "); } System.out.println(); } } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tok = null; solve(); in.close(); } catch (IOException e) { System.exit(0); } } public String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } 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()); } BufferedReader in; StringTokenizer tok; }
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 + 10; int a[maxn]; int main() { int n; scanf("%d", &n); if (n & 1) { if ((n - 1) % 4) { puts("-1"); return 0; } else { for (int i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n + 1 - i; a[n + 1 - i] = n - i; a[n - i] = i; } a[n / 2 + 1] = n / 2 + 1; } } else { if (n % 4) { puts("-1"); return 0; } else { for (int i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n + 1 - i; a[n + 1 - i] = n - i; a[n - i] = i; } } } for (int i = 1; i <= n; ++i) { printf("%d%c", a[i], " \n"[i == 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.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 * @author dy */ 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 == 1){ out.println(1);return; } if(N % 2 == 1 && (N - 1) % 4 != 0 || N % 2 == 0 && N % 4 != 0 || N == 2 || N == 3){ out.println(-1);return; } int[] ans = new int[N + 1]; // int even = N / 2 * 2; for(int i = 1; i <= N / 2; i += 2){ int[] P = new int[]{i, i + 1, N - i + 1, N - i}; for(int j = 0; j < P.length; ++j) ans[P[j]] = P[(j + 1) % 4]; } if(N % 2 == 1) ans[N / 2 + 1] = N / 2 + 1; out.print(ans[1]); for(int i = 2; i < ans.length; ++i) out.print(" " + ans[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
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, res[N]; int main() { scanf("%d", &n); if ((n / 2) % 2 == 1) { puts("-1"); } else { res[n / 2] = n / 2; for (int i = 0; i < n / 2; i += 2) { res[i] = i + 1; res[i + 1] = n - i - 1; res[n - i - 1] = n - i - 2; res[n - i - 2] = i; } for (int i = 0; i < n; i++) { printf("%d%c", res[i] + 1, (i + 1 == 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; int n, i, a[100005]; int main() { scanf("%d", &n); if (n % 4 != 0 && n % 4 != 1) { printf("-1\n"); return 0; } for (i = 1; i <= n / 4; i++) { a[(i - 1) * 2 + 1] = i * 2; a[i * 2] = n - (i - 1) * 2; a[n - (i - 1) * 2] = n - (i - 1) * 2 - 1; a[n - (i - 1) * 2 - 1] = i * 2 - 1; } if (n & 1) a[n / 2 + 1] = n / 2 + 1; for (i = 1; i <= n; i++) printf("%d ", 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; const int MaxN = 100000; int main() { int n; static int p[MaxN + 1]; cin >> n; if ((n / 2) % 2 != 0) cout << "-1" << endl; else { for (int i = 1; i <= n - i + 1; i += 2) { if (i == n - i + 1) p[i] = i; else { p[i] = i + 1; p[i + 1] = n - i + 1; p[n - i + 1] = n - i; p[n - i] = i; } } for (int i = 1; i <= n; i++) printf("%d ", 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
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.*; import java.io.*; import java.math.*; import java.lang.*; /** * * @author calcsaransh */ public class Main { /** * @param args the command line arguments */ static int arr[]; public static void main(String[] args) { // TODO code application logic here try { Parserdoubt pd=new Parserdoubt(System.in); PrintWriter pw=new PrintWriter(System.out); int n=pd.nextInt(); arr=new int[n+1]; if(n==1) { System.out.println(1); return; } if(n%4==2||n%4==3) { System.out.println(-1); return; } int rightbound=n-1; int leftbound=0; int forward=1; int backward=n; for(int i=0;i<n/4;i++) { arr[rightbound-1]=forward++; arr[leftbound+1]=backward--; arr[leftbound]=forward++; arr[rightbound]=backward--; rightbound-=2; leftbound+=2; } for(int i=0;i<n;i++) { if(arr[i]==0)arr[i]=n/2+1; pw.print(arr[i]+" "); } pw.flush(); } catch(Exception e) { e.printStackTrace(); } } } class Parserdoubt { final private int BUFFER_SIZE = 1 << 17; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parserdoubt(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString() throws Exception { StringBuffer sb = new StringBuffer(""); byte c = read(); while (c <= ' ') { c = read(); } do { sb.append((char) c); c = read(); } while (c > ' '); return sb.toString(); } public char nextChar() throws Exception { byte c = read(); while (c <= ' ') { c = read(); } return (char) c; } public int nextInt() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) { return -ret; } return ret; } public long nextLong() throws Exception { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) { return -ret; } return ret; } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws Exception { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } }
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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; public class Solver { public static void main(String[] Args) throws NumberFormatException, IOException { new Solver().Run(); } PrintWriter pw; StringTokenizer Stok; BufferedReader br; public String nextToken() throws IOException { while (Stok == null || !Stok.hasMoreTokens()) { Stok = new StringTokenizer(br.readLine()); } return Stok.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } public int zero (int[] arg1,int length){ int i = 0; while (arg1[i]!=0 && i<length/2){ i++; } if (i==length/2){ i = -1; } return i; } public void Run() throws NumberFormatException, IOException { //br = new BufferedReader(new FileReader("input.txt")); //pw = new PrintWriter("output.txt"); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); int n = nextInt(); if (n==1) pw.print(1); else if (n%4 > 1) pw.print(-1); else { int[] per = new int[n]; if (n%2==1) per[n/2] = 1+n/2; int i; for (i=0;i<n/2;i=i+2){ per[i]=i+2; } for (i=1;i<n/2;i=i+2){ per[i]=n-i+1; } for (i=n-1;i>n/2;i=i-2){ per[i]=i; } for (i=n-2;i>=n/2;i=i-2){ per[i]=n-i-1; } for (int j : per){ pw.print(j); pw.print(' '); } } pw.flush(); pw.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.BufferedWriter; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Scanner; public class cf287c { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = in.nextInt(); if(n%4 >= 2) { out.println("-1"); out.close(); return; } int[] v = new int[n]; if(n%2 == 1) v[n/2] = n/2; for(int i=0; i<n/4; i++) { v[i*2] = i*2 + 1; v[i*2+1] = n-(i*2+1); v[n-(i*2+1)] = n - (i*2+1) - 1; v[n-(i*2+1)-1] = i*2; } for(int x : v) out.print((x+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
n = int(raw_input()) if n % 4 == 2 or n % 4 == 3: print "-1" else: perm = [0 for i in range(n)] d = n/4 for i in range(d): perm[2*i] = 2*i + 2 perm[2*i + 1] = n - 2*i perm[n - 2*i - 1] = n - 2*i - 1 perm[n - 2*i - 2] = 2*i + 1 if n % 2 == 1: perm[n / 2] = n / 2 + 1 print " ".join([str(x) for x in perm])
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n, a[100009]; int main() { cin >> n; if (n == 1) cout << 1; else if ((n / 2) % 2 == 1) cout << -1; else { for (int i = 1; i <= (n / 2 / 2); i++) { a[i * 2 - 1] = i * 2; a[i * 2] = n - (i - 1) * 2; a[n - i * 2 + 1] = i * 2 - 1; a[n - (i - 1) * 2] = n - (i - 1) * 2 - 1; if (n % 2 == 1) a[n / 2 + 1] = n / 2 + 1; } for (int i = 1; i <= n; i++) cout << a[i] << " "; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> const int N = 1e5 + 10; int n, m, i, t, x, a[N]; bool e[N]; int check() { int i, x; for (i = 1; i <= n; i++) { x = a[a[i]]; if (x + i == n + 1) ; else return 0; } return 1; } void print() { int i; for (i = 1; i < n; i++) printf("%d ", a[i]); printf("%d\n", a[n]); } void dfs(int p) { if (p == n + 1) { if (check()) print(); } int i; for (i = 1; i <= n; i++) if (e[i] == 0) { e[i] = 1; a[p] = i; dfs(p + 1); e[i] = 0; } } void test() { for (n = 1; n <= 12; n++) { dfs(1); } } int main() { scanf("%d", &n); t = n % 4; if (t == 2 || t == 3) printf("-1\n"); else if (t == 0) { m = n / 2; for (i = 2; i <= m; i += 2) a[i] = i - 1; for (i = 1; i <= m; i += 2) a[i] = n - i; for (i = m + 1; i <= n; i += 2) a[i] = i + 1; for (i = m + 2; i <= n; i += 2) a[i] = n + 2 - i; print(); } else if (t == 1) { m = n / 2 + 1; for (i = 2, x = 1; i < m; i += 2, x += 2) a[i] = x; { a[m] = x; x += 2; } for (i = m + 1; i <= n; i += 2, x += 2) a[i] = x; for (i = 1, x = n - 1; i < m; i += 2, x -= 2) a[i] = x; for (i = m + 2; i <= n; i += 2, x -= 2) a[i] = x; print(); } 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; // http://codeforces.com/contest/287/problem/C public class LuckyPermutation { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); s.close(); if (n % 4 == 0 || (n - 1) % 4 == 0) { int p = n / 4; int[] a = new int[n + 1]; if ((n - 1) % 4 == 0) { int mid = (n + 1) / 2; a[mid] = mid; } for (int i = 1; i <= p; i++) { a[i] = p + p - i + 1; } for (int i = p + 1; i <= p + p; i++) { a[i] = n + i - p - p; } for (int i = n - p - p + 1; i <= n - p; i++) { a[i] = i - n + p + p; } for (int i = n - p + 1; i <= n; i++) { a[i] = n + n - i - p - p + 1; } StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) { sb.append(a[i]); sb.append(' '); } System.out.println(sb); } 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
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; if (n == 4) { cout << "2 4 1 3" << endl; return; } int a[n]; if (n % 4 == 0 || (n - 1) % 4 == 0) { a[n - 3] = n + 1 - 4; a[0] = 2; a[n - 2] = 1; a[2] = 4; a[n - 1] = n - 1; a[1] = n; if (n % 2 == 0) { for (int i = 3; i <= n / 2; i++) { if (i % 2 == 0) { a[i] = min((a[i - 2] + 2), (n - a[i - 2] - 1)); a[n - i - 1] = n + 1 - a[i]; } else { a[i] = max((a[i - 2] - 2), (n - a[i - 2] + 3)); a[n - i - 1] = n + 1 - a[i]; } } } else { for (int i = 3; i <= n / 2; i++) { if (i % 2 == 0) { a[i] = min((a[i - 2] + 2), (n - a[i - 2] - 1)); a[n - i - 1] = n + 1 - a[i]; } else { a[i] = max((a[i - 2] - 2), (n - a[i - 2] + 3)); a[n - i - 1] = n + 1 - a[i]; } } a[n / 2] = (n + 1) / 2; } for (int i = 0; i < n; i++) { cout << a[i] << " "; } } else cout << "-1" << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n; int mas[100005]; int main() { cin >> n; if (n % 4 >= 2) { cout << "-1\n"; return 0; } for (int i = 0; i < n; ++i) mas[i] = i; for (int l = 0, r = n - 1; l < r; l += 2, r -= 2) { mas[l] = l + 1; mas[l + 1] = r; mas[r] = r - 1; mas[r - 1] = l; } for (int i = 0; i < n; ++i) printf("%d ", mas[i] + 1); cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n, p[1010000]; int main() { scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { puts("-1"); return 0; } if (n % 4 == 0) { for (int i = 1; i <= n / 2; i++) { if (i % 2 == 1) p[i] = i + 1, p[n + 1 - i] = n - i; else p[i] = n + 2 - i, p[n + 1 - i] = i - 1; } for (int i = 1; i <= n; i++) printf("%d ", p[i]); } else { for (int i = 1; i <= n / 2; i++) { if (i % 2 == 1) p[i] = i + 1, p[n + 1 - i] = n - i; else p[i] = n + 2 - i, p[n + 1 - i] = i - 1; } p[n / 2 + 1] = n / 2 + 1; 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
// package Practice3.CF286; import java.util.Iterator; import java.util.Scanner; import java.util.TreeSet; public class CF286A { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); if (n % 4 > 1) { System.out.println(-1); } else { int[] p = new int[n + 1]; int[] ans = new int[n + 1]; TreeSet<Integer> available = new TreeSet<>(); for (int i = 1; i <= n; i++) { p[i] = n - i + 1; available.add(p[i]); } int count = 0; int newI = 0; int val = 0; boolean bool = false; if(n % 2 != 0){ ans[n/2 + 1] = n/2 + 1; available.remove(n/2 + 1); bool = true; n--; } while(count != n) { if (count % 4 == 0) { newI = available.first(); available.remove(newI); Iterator<Integer> iter = available.iterator(); int m = 0; while(iter.hasNext()){ int k = iter.next(); if(k == newI || k == p[newI] || k == (n/2 + 1)){ continue; } val = k; break; } ans[newI] = val; val = p[newI]; newI = ans[newI]; } else { available.remove(newI); ans[newI] = val; val = p[newI]; newI = ans[newI]; } count++; } // System.out.println(Arrays.toString(ans)); if(bool){ n++; } for (int i = 1; i <= n; i++) { System.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
import java.util.*; import java.util.regex.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.lang.Integer.*; import static java.lang.Double.*; import static java.util.Collections.*; import java.io.*; public class _287_C_Lucky_Permutation { public void solve() { int n = ni(); int ar[]=new int [n]; if(n%4>1){ out.print(-1); return; } pr(ar); if((ar.length&1)!=0) ar[(n)/2]=(n+2)/2; pr(ar); for (int i = 0; i<n/2; i+=2) { int e=n-1-i; ar[i+1]=n-(i+1)+1; ar[e]=n-(i+2)+1; ar[e-1]=n-(e+1)+1; ar[i]=n-(e+1-1)+1; } pr(ar); for (int j = 0; j < n; j++) { out.print(ar[j]+" "); } } boolean check(int ar[]){ for (int i = 0; i < ar.length; i++) { if(ar[ar[i]-1]!=ar.length-(i+1)+1){ pr("false",i,ar); return false; } } return true; } void run() throws Exception { long s = System.currentTimeMillis(); solve(); out.flush(); pr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception {new _287_C_Lucky_Permutation().run();} InputStream in=System.in; PrintWriter out=new PrintWriter(System.out); private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = in.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) {return !(c >= 33 && c <= 126);} private int skip() {int b;while ((b = readByte()) != -1 && isSpaceChar(b));return b;} public String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != // ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } public int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nl(){ long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } void pr(Object... ob) {if (!oj)System.out.println(Arrays.deepToString(ob).replace("],", "],\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
import java.util.*; import java.io.*; public class LuckyPermutation { public static InputReader in; public static PrintWriter out; public static final int MOD = (int) (1e9 + 7); public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); if(n <= 3 || n%4 >= 2) { out.println(n == 1 ? 1 : -1); out.close(); return; } int[] p = new int[n + 1]; if((n&1) == 1) p[n/2 + 1] = n/2 + 1; int no = 1; for(int i = n/2; i >= 1; i--, no++) { if((i&1) == 0) { p[i] = no; p[n - i + 1] = n - no + 1; } else { p[i] = n - no + 1; p[n - i + 1] = no; } } for(int i = 1; i <= n/2; i++) { if(p[i] == i || p[i] == n - i + 1) { p[i]--; p[n - i + 1]++; p[i + 1]++; p[n - i]--; break; } } boolean pos = true; for(int i = 1; i <= n/2; i++) { if(p[i] == i || p[i] == n - i + 1 || p[i] <= 0 || p[i] > n) { pos = false; } } if(pos) for (int i = 1; i <= n; i++) out.print(p[i] + " "); else out.println(-1); out.close(); } static class Node implements Comparable<Node> { int next; long dist; public Node(int u, int v) { this.next = u; this.dist = v; } public void print() { out.println(next + " " + dist + " "); } public int compareTo(Node n) { return Integer.compare(-this.next, -n.next); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
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 A { public static void main(String args[]) { Scanner s = new Scanner(System.in); int n = s.nextInt(); if (n % 4 <= 1) { int arr[] = new int[n + 1]; int c = n / 4, i = 1; while (i <= c) { arr[2 * i - 1] = 2 * i; arr[2 * i] = n - (i * 2 - 2); arr[n - (i * 2 - 2)] = n - (i * 2 - 1); arr[n - (i * 2 - 1)] = i * 2 - 1; i++; } if (n % 4 == 1) arr[n / 2 + 1] = n / 2 + 1; i = 1; while (i != n) System.out.print(arr[i++] + " "); System.out.println(arr[i]); } else System.out.println(-1); s.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; int p[100010]; bool used[100010]; int main() { scanf("%d", &n); memset(p, 0, sizeof(p)); memset(used, false, sizeof(used)); if (n == 1) { printf("1\n"); return 0; } if (n % 2 == 0) { if (n % 4 != 0) { printf("-1\n"); return 0; } } else { if ((n - 1) % 4 != 0) { printf("-1\n"); return 0; } } if (n % 2 != 0) { p[n / 2 + 1] = n / 2 + 1; used[n / 2 + 1] = true; } for (int i = 1; i <= n / 2; i++) { if (!p[i]) { int num = 1; while (used[num] || num == i) num++; p[i] = num; used[num] = true; p[num] = n + 1 - i; used[n + 1 - i] = true; p[n + 1 - i] = n + 1 - num; used[n + 1 - num] = true; p[n + 1 - num] = i; used[i] = true; } } for (int i = 1; i <= n; i++) printf("%d ", p[i]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; /** * * * @author pttrung */ public class C { // public static long x, y, gcd; // public static int Mod = 1000000007; public static PrintWriter out; public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(); out = new PrintWriter(System.out); // System.out.println(Integer.MAX_VALUE); // PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt"))); int n = in.nextInt(); int[] result = new int[n]; result[0] = 1; int last = 0; boolean[] check = new boolean[n]; check[0] = true; TreeSet<Integer> set = new TreeSet(); for (int i = 1; i < n; i++) { set.add(i); } if (n % 2 != 0) { result[n / 2] = n / 2; check[n / 2] = true; set.remove(n / 2); } boolean found = true; a: while (true) { result[result[last]] = n - last - 1; check[result[last]] = true; set.remove(result[last]); last = result[last]; //System.out.println(last); if (check[result[last]]) { if (result[result[last]] != n - last - 1) { found = false; break; } if (set.size() > 2) { int i = set.pollFirst(); int j = set.first(); result[i] = j; last = i; check[i] = true; continue a; } break; } } for (int i = 0; i < n; i++) { if (result[result[i]] != n - i - 1) { found = false; } } if (found) { for (int i = 0; i < n; i++) { out.print((result[i] + 1) + " "); } } else { out.println(-1); } out.close(); } public static int cross(Point a, Point b) { int val = a.x * b.y - a.y * b.x; return val; } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val; } else { return val * val * a; } } public static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } public static class Point implements Comparable<Point> { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Point o) { if (x != o.x) { return x - o.x; } else { return y - o.y; } } } // public static void extendEuclid(long a, long b) { // if (b == 0) { // x = 1; // y = 0; // gcd = a; // return; // } // extendEuclid(b, a % b); // long x1 = y; // long y1 = x - (a / b) * y; // x = x1; // y = y1; // // } public static class FT { int[] data; FT(int n) { data = new int[n]; } public void update(int index, int value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public int get(int index) { int result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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> #pragma comment(linker, "/STACK:16777216") using namespace std; const int MAXN = 100005; int n, a[MAXN]; void Inp() { scanf("%d", &n); } void Outp() {} void Run() { if (n % 4 == 2 || n % 4 == 3) { printf("-1"); exit(0); } for (int i = 0; i < n / 4; ++i) { a[i * 2] = i * 2 + 1; a[i * 2 + 1] = n - i * 2 - 1; a[n - i * 2 - 1] = n - i * 2 - 2; a[n - i * 2 - 2] = i * 2; } if (n % 4 == 1) a[n / 2] = n / 2; if (n % 4 == 3) { a[n / 2] = n / 2; a[n / 2 - 1] = n / 2 + 1; a[n / 2 + 1] = n / 2 - 1; } for (int i = 0; i < n; ++i) printf("%d ", a[i] + 1); } int main() { Inp(); Run(); Outp(); 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 LuckyPermutation { public static void main(String[] args) { Scanner s = new Scanner(System.in); int num = s.nextInt(); int[]array = new int[num]; if(num ==1){ System.out.println(1); }else{ if (num% 4== 2 || num%4 == 3 ){ System.out.println(-1); }else { for (int i = 0; i < num/2; i++) if (i % 2 == 0) { array[i] = i + 2; array[num - (i + 2)] = (i + 1); } else { array[i] = num - (i - 1); array[num - (i)] = num - i; } if(num%4 == 1 || num % 4 == 3){ array[num/2]=num/2+1; } for (int i = 0; i < num; i++) { System.out.print(array[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 main() { int n; while (scanf("%d", &n) == 1) { if ((n / 2) & 1) printf("-1\n"); else { if (n & 1) { int t = n; for (int i = 1; i <= n; i++) { if (i == n / 2 + 1) { printf("%d", i); t -= 2; } else { if (i & 1 && i < n / 2) printf("%d", i + 1); else if (i & 1 && i > n / 2) printf("%d", i - 1); else { printf("%d", t); t -= 2; } } if (i != n) printf(" "); } } else { int t = n; for (int i = 1; i <= n / 2; i++) { if (i & 1) printf("%d", i + 1); else { printf("%d", t); t -= 2; } printf(" "); } t--; for (int i = n / 2 + 1; i <= n; i++) { if (i & 1) { printf("%d", t); t -= 2; } else printf("%d", i - 1); if (i != n) printf(" "); } } printf("\n"); } } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; while (scanf("%d", &n) != EOF) { if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); continue; } for (int i = 0; i < n; ++i) { if (i) printf(" "); if (n % 2) { if (i == (n - 1) / 2) printf("%d", (n + 1) / 2); else if (i < (n - 1) / 2) printf("%d", i % 2 ? n + 1 - i : i + 2); else { int j = n - 1 - i; printf("%d", n + 1 - (j % 2 ? n + 1 - j : j + 2)); } } else { if (i < n / 2) printf("%d", i % 2 ? n + 1 - i : i + 2); else { int j = n - 1 - i; printf("%d", n + 1 - (j % 2 ? n + 1 - j : j + 2)); } } } 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; long long n, p[5000000], i; int main() { cin >> n; if (n % 4 >= 2) { cout << -1 << endl; return 0; } for (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 % 2) p[n / 2 + n % 2] = n / 2 + n % 2; for (i = 1; i <= n; i++) cout << 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
import java.io.*; import java.util.*; public class Template implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } class GraphBuilder { int n, m; int[] x, y; int index; int[] size; GraphBuilder(int n, int m) { this.n = n; this.m = m; x = new int[m]; y = new int[m]; size = new int[n]; } void add(int u, int v) { x[index] = u; y[index] = v; size[u]++; size[v]++; index++; } int[][] build() { int[][] graph = new int[n][]; for (int i = 0; i < n; i++) { graph[i] = new int[size[i]]; } for (int i = index - 1; i >= 0; i--) { int u = x[i]; int v = y[i]; graph[u][--size[u]] = v; graph[v][--size[v]] = u; } return graph; } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long[] readLongArray(int size) throws IOException { long[] res = new long[size]; for (int i = 0; i < size; i++) { res[i] = readLong(); } return res; } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } <T> List<T>[] createGraphList(int size) { List<T>[] list = new List[size]; for (int i = 0; i < size; i++) { list[i] = new ArrayList<>(); } return list; } public static void main(String[] args) { new Template().run(); // new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void brut(int[] a, int pos) { if (a.length == pos) { for (int i = 0; i < a.length; i++) { if (a[a[i]] != a.length - 1 - i) return; } System.err.println(Arrays.toString(a)); return; } for (int i = pos; i < a.length; i++) { int t = a[i]; a[i] = a[pos]; a[pos] = t; brut(a, pos + 1); t = a[i]; a[i] = a[pos]; a[pos] = t; } } void solve() throws IOException { // int n = 9; // int[] a = new int[n]; // for (int i = 0; i < n; i++) a[i] = i; // brut(a, 0); int n = readInt(); if (n == 1) { out.println(1); return; } if (n % 4 <= 1) { int[] res = new int[n]; if (n % 4 == 1) { res[n / 2] = (n + 1) / 2; } int l = 0; int r = n - 2; boolean right = true; int val = 1; for (int i = 0; i < n / 2; i++) { if (right) { res[r] = val; r -= 2; } else { res[l] = val; l += 2; } right = !right; val++; } l = 1; r = n - 1; val = n; right = false; for (int i = 0; i < n / 2; i++) { if (right) { res[r] = val; r -= 2; } else { res[l] = val; l += 2; } right = !right; val--; } for (int x : res) { out.print(x + " "); } return; } 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
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; int a[N]; int main() { scanf("%d", &n); if (n % 4 >= 2) { printf("-1\n"); return 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; } if (n % 4) a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; i++) printf("%d ", a[i]); printf("\n"); }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int res[100010]; int main() { int n; cin >> n; if (n % 4 == 1) { for (int i = 0; i < n / 2 - 1; i += 2) { res[i] = i + 2; res[i + 1] = n - i; } res[n / 2] = n / 2 + 1; for (int i = n / 2 + 1; i < n - 1; i += 2) { res[i] = n - i - 1; res[i + 1] = i + 1; } for (int i = 0; i < n; i++) cout << res[i] << " "; return 0; } if (n % 4 == 0) { for (int i = 0; i < n / 2 - 1; i += 2) { res[i] = i + 2; res[i + 1] = n - i; } for (int i = n / 2; i < n - 1; i += 2) { res[i] = n - i - 1; res[i + 1] = i + 1; } for (int i = 0; i < n; i++) cout << res[i] << " "; return 0; } else cout << -1; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class A implements Runnable { private void solve() throws Exception { int n = nextInt(); int[] res = solve(n); // if (n < 20) { // solvePerebor(n); // } if (res == null) { System.out.println(-1); } else { for (int x : res) { System.out.print(x + 1); System.out.print(' '); } } // [0] // [1, 3, 0, 2] // [1, 4, 2, 0, 3] // [1, 7, 3, 5, 2, 4, 0, 6] // [1, 8, 3, 6, 4, 2, 5, 0, 7] // [1, 11, 3, 9, 5, 7, 4, 6, 2, 8, 0, 10] // [1, 12, 3, 10, 5, 8, 6, 4, 7, 2, 9, 0, 11] // for (int i = 1; i < 15; i++) { // solvePerebor(i); // } } private int[] solve(int n) { int[] res = new int[n]; if (n <= 1) { return res; } Arrays.fill(res, -1); res[0] = 1; res[1] = n - 1; for (int i = 2; i < n / 2; i += 2) { res[i] = res[i - 2] + 2; res[i + 1] = res[i - 1] - 2; } res[n - 1] = n - 2; res[n - 2] = 0; for (int i = n - 3; i > n / 2; i -= 2) { res[i] = res[i + 2] - 2; res[i - 1] = res[i + 1] + 2; } SortedSet<Integer> avail = new TreeSet<>(); for (int i = 0; i < n; i++) { avail.add(i); } for (int x : res) { if (x >= 0 && !avail.contains(x)) { return null; } avail.remove(x); } for (int i = 0; i < n; i++) { if (res[i] < 0) { res[i] = avail.first(); avail.remove(res[i]); } } if (!good(res)) { return null; } return res; } private void solvePerebor(int n) { int[] p = new int[n]; int countP = 1; for (int i = 0; i < n; i++) { p[i] = i; countP *= (i + 1); } for (int perm = 1; perm <= countP; perm++) { if (good(p)) { System.err.println(Arrays.toString(p)); } if (perm < countP) { nextPerm(p); } } } private void nextPerm(int[] p) { int n = p.length; int m = -1; for (int i = n - 2; i >= 0; i--) { if (p[i] < p[i + 1]) { m = i; break; } } if (m < 0) { System.err.println("Last one, n=" + n); return; } int k = m + 1; for (int i = m + 1; i < n; i++) { if (p[i] < p[k] && p[i] > p[m]) { k = i; } } int tmp = p[k]; p[k] = p[m]; p[m] = tmp; // swap m+1..n-1 for (int i = m + 1; i < n; i++) { int j = n - i + m; if (j <= i) { break; } tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } private boolean good(int[] p) { int n = p.length; for (int i = 0; i < n; i++) { if (p[p[i]] != n - i - 1) { return false; } } return true; } private BufferedReader in; private StringTokenizer tokenizer = new StringTokenizer(""); private PrintWriter out; private String nextToken() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } @Override public void run() { try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out)) { this.in = in; this.out = out; solve(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { 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
import java.io.*; import java.util.*; public class CF_286A { public static void main(String[] args) throws IOException { new CF_286A().solve(); } void solve() throws IOException{ InputStream in = System.in; PrintStream out = System.out; // in = new FileInputStream("in.txt"); // out = new PrintStream("out.txt"); Scanner sc=new Scanner(in); int n=sc.nextInt(); if (n%4==2 || n%4==3) { out.println(-1+""); return; } int[] p=new int[n]; for (int i=0;i<n;i++) p[i]=i+1; for (int v=0;v+1<n/2;v+=2){ int a=p[v], b=p[v+1], c=p[n-1-v-1], d=p[n-1-v]; p[v]=b; p[v+1]=d; p[n-1-v-1]=a; p[n-1-v]=c; // System.out.println(Arrays.toString(p)); } for (int val:p) out.print(val+" "); } }
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 long maxn = 60000; const long maxm = 15000000; const long oo = 100000000; const long mod = 1000000007; const double le = 1e-10; long i, j, k, n, m; long a[1000000]; int main() { scanf("%ld", &n); if (n % 4 == 3 || n % 4 == 2) { puts("-1"); return 0; } m = n >> 1; if (n & 1) a[m + 1] = m + 1; for (i = 1; i <= m; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } for (i = 1; i <= n; ++i) { printf("%ld ", a[i]); } puts(""); ; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, tam; cin >> n; vector<int> v; if (((n % 4) == 2) || ((n % 4) == 3)) { cout << -1 << endl; return 0; } else if (n % 4 == 0) { tam = (int)v.size(); v.push_back(2); v.push_back(4); v.push_back(1); v.push_back(3); int cnt = n / 4, c = 4; cnt--; for (int i = 0; i < cnt; i++) { v.insert(v.begin(), c + 4); v.insert(v.begin(), 2); v.insert(v.end(), 1); v.insert(v.end(), c + 3); c += 4; } for (int i = 0; i < cnt * 2 + 2; i++) { v[i] = v[i] + 2 * (i / 2); } for (int i = cnt * 2 + 2; i < (int)v.size(); i++) { v[i] = v[i] + 2 * (((int)v.size() - 1 - i) / 2); } } else { v.push_back(1); int cnt = (n - 1) / 4, c = 1; for (int i = 0; i < cnt; i++) { v.insert(v.begin(), c + 4); v.insert(v.begin(), 2); v.insert(v.end(), 1); v.insert(v.end(), c + 3); c += 4; } for (int i = 0; i < cnt * 2 + 1; i++) { v[i] = v[i] + 2 * (i / 2); } for (int i = cnt * 2 + 1; i < (int)v.size(); i++) { v[i] = v[i] + 2 * (((int)v.size() - 1 - i) / 2); } } for (int i = 0; i < (int)v.size(); i++) cout << v[i] << char(i + 1 == (int)v.size() ? 10 : 32); }
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 mx = 1e5 + 10; int p[mx]; int main() { int n; cin >> n; if (n % 4 > 1) { puts("-1"); return 0; } int l = 1, r = n; while (l < r) { p[l] = l + 1; p[l + 1] = r; p[r] = r - 1; p[r - 1] = l; l += 2; r -= 2; } if (l == r) p[l] = l; 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
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class LuckyPermutation implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int n = in.ni(); if (n == 1) { out.println(1); return; } if (n % 4 >= 2) { out.println(-1); return; } int[] result = new int[n + 1]; if (n % 2 == 1) result[(n + 1) / 2] = (n + 1) / 2; for (int idx = 1; idx < n / 2; idx += 2) { int value = idx + 1; int i = idx; for (int j = 0; j < 4; j++) { result[i] = value; int next = n - i + 1; i = value; value = next; } } for (int i = 1; i <= n; i++) { out.print(result[i]); out.print(' '); } } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (LuckyPermutation instance = new LuckyPermutation()) { instance.solve(); } } }
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; import java.io.PrintWriter; //PrintWriter public class R176_Div2_C //Name: Lucky Permutation { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); solve(in, out); out.close(); in.close(); } public static void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); if (n % 4 == 2 || n % 4 == 3) out.println(-1); else if (n == 1) out.println(1); else { int[] p = new int[n + 1]; p[1] = 2; p[2] = n; p[n - 1] = 1; p[n] = n - 1; int it = n / 4 - 1; for (int i = 1; i <= it; i++) { p[1 + 2*i] = p[1 + 2*(i-1)] + 2; p[2 + 2*i] = p[2 + 2*(i-1)] - 2; p[n-1 - 2*i] = p[n-1 - 2*(i-1)] + 2; p[n - 2*i] = p[n - 2*(i-1)] - 2; } if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) sb.append(p[i] + " "); out.println(sb); } } }
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 ans[200000], was[200000]; int link[200000]; int main() { int n; scanf("%d", &n); if (n % 2 == 1) { ans[n / 2 + 1] = n / 2 + 1; was[n / 2 + 1] = 1; } for (int i = 1; i <= n; i++) link[i] = n - i + 1; int x = 1; for (int i = 1; i <= n; i++) { if (ans[i]) continue; while (was[x] || x == i) x = x % n + 1; int lin = x, pos = i; while (ans[pos] == 0) { ans[pos] = lin; was[lin] = 1; lin = pos; pos = n - ans[pos] + 1; } } bool f = true; for (int i = 1; i <= n; i++) f = f && (link[i] == ans[ans[i]]); if (f == false) { printf("-1"); return 0; } for (int i = 1; i <= n; 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; bool good(vector<int> p) { for (int i = 0; i < p.size(); ++i) if (p[p[i]] != p.size() - i - 1) return false; return true; } void print(vector<int> p) { for (int i = 0; i < p.size(); ++i) cout << p[i] + 1 << " "; cout << endl; } void solve() { int n; cin >> n; if (n % 4 >= 2) { cout << -1; return; } vector<int> ans(n, n / 2); int hi = n - 1; int lo = 0; for (int l = 0, r = 1, x = n - 2, y = n - 1; r < x; x -= 2, y -= 2, l += 2, r += 2) { ans[l] = lo + 1; ans[r] = hi; ans[x] = lo; ans[y] = hi - 1; hi -= 2; lo += 2; } print(ans); } int main() { ios_base::sync_with_stdio(false); solve(); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int n, i, j, p[100011]; set<int> a; set<int>::iterator b; int main() { scanf("%d", &n); if (n == 1) { printf("1\n"); return 0; } if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); return 0; } for (i = 1; i <= n; ++i) a.insert(i); for (j = 1; j <= n; ++j) if (p[j] == 0) { for (b = a.begin();; ++b) if (*b != j || *b == j && n - j + 1 == j) break; p[j] = *b; a.erase(p[j]); for (i = j; p[p[i]] == 0; i = p[i]) p[p[i]] = n - i + 1, a.erase(n - i + 1); } for (i = 1; i < n; ++i) printf("%d ", p[i]); printf("%d\n", p[i]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import sys from collections import deque n = int(input()) if n % 4 == 2 or n % 4 == 3: print('-1') sys.exit() arr = [None] * (n + 1) qt = deque([i for i in range(1, n + 1)]) mark = set() while qt: while qt and qt[0] in mark: qt.popleft() if not qt: break a = qt.popleft() while qt and qt[0] in mark: qt.popleft() if not qt: break b = qt.popleft() for i in range(4): mark.add(a) mark.add(b) arr[a] = b arr[b] = n - a + 1 a = b b = arr[b] for i in range(1, n + 1): if not arr[i]: arr[i] = a break print(' '.join(map(str, arr[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
import java.util.*; import java.io.*; import java.math.*; public class Main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){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 double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = i();}return ret;} } // |----| /\ | | ----- | // | / / \ | | | | // |--/ /----\ |----| | | // | \ / \ | | | | // | \ / \ | | ----- ------- static class PriorityComparator implements Comparator<ArrayList<Integer>> { public int compare(ArrayList<Integer> al1,ArrayList<Integer> al2) { for(int i=0;i<al1.size();i++) if(al1.get(i)>al2.get(i))return 1; else if((al1.get(i)<al2.get(i)))return -1; return 0; } } public static void func(int n) { Integer arr[]=new Integer[n]; for(int i=0;i<n;i++) arr[i]=i+1; int counter=0; TreeSet<ArrayList<Integer>> hs=new TreeSet<>(new PriorityComparator()); while(counter++!=10000000) { Collections.shuffle(Arrays.asList(arr)); int flag=0; for(int i=0;i<n;i++) { int a=arr[i]-1; if(arr[a]!=n-i) { flag=1; break; } } if(flag==0) { ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(arr[i]); hs.add(al); } } System.out.println(); for(ArrayList<Integer> al:hs) { for(int i=0;i<al.size();i++) System.out.print(al.get(i)+" "); System.out.println(); } } public static void main(String[] args)throws IOException { Reader sc=new Reader(); PrintWriter out=new PrintWriter(System.out); int n=sc.i(); //func(n); if(n==1) out.println(1); else if(n%4>1) System.out.println(-1); else if(n%4==0) { int arr[]=new int[n]; int start=2; int end=n; int pointer=0; while(start<end) { arr[pointer]=start; arr[pointer+1]=end; pointer+=2; start+=2; end-=2; } start =1; end =n-1; pointer=n-2; while(start<end) { arr[pointer]=start; arr[pointer+1]=end; pointer-=2; start+=2; end-=2; } for(int i=0;i<n;i++) out.print(arr[i]+" "); } else if(n%4==1) { int arr[]=new int[n]; int start=2; int end=n-1; int pointer=0; while(start<end) { arr[pointer]=start; arr[n-pointer-1]=end; pointer+=2; start+=2; end-=2; } start =n; end =1; pointer=1; while(start>end) { arr[pointer]=start; arr[n-pointer-1]=end; pointer+=2; start-=2; end+=2; } arr[n/2]=n/2+1; for(int i=0;i<n;i++) out.print(arr[i]+" "); } out.flush(); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> int n, i, a[100005], t1, t2; int main() { scanf("%d", &n); if (n % 4 > 1) { printf("-1\n"); return 0; }; t1 = n - 1; t2 = -1; while (t1 > (n + 1) / 2) { a[t1] = (t2 += 2); t1 -= 2; }; if (n % 4 == 1) { a[(n + 1) / 2] = (n + 1) / 2; t2++; }; for (t1 = (n + 1) / 2 + 2; t1 <= n; t1 += 2) a[t1] = (t2 += 2); for (i = 1; i <= n / 2; i++) a[i] = n + 1 - a[n - i + 1]; for (i = 1; i <= n; i++) printf("%d ", a[i]); printf("\n"); }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; template <class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> T sqr(T x) { return x * x; } template <class T> T cube(T x) { return x * x * x; } template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const long double PI = acos(-1.0); const long double eps = 1e-9; const int dr[] = {-1, 0, +1, 0}; const int dc[] = {0, +1, 0, -1}; const int inf = (int)1e9 + 5; const long long linf = (long long)1e16 + 5; const long long mod = (long long)1e9 + 7; const int maxn = 10005; int a[100005]; int main() { std::ios::sync_with_stdio(false); int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1 << "\n"; return 0; } if (n % 4 == 1) a[(n + 1) / 2] = (n + 1) / 2; for (int i = 1; i < n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } for (int i = (1); i <= (n); ++i) cout << a[i] << " "; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#!/usr/bin/env python # coding=utf-8 n = input() if n % 4 == 2 or n % 4 == 3: print -1 exit(0) ans = [0 for i in xrange(n)] cnt = 0 if n % 2 == 1: ans[(n - 1) / 2] = (n + 1) / 2 cnt += 1 p = 1 q = n while cnt < n: tmpp = p + 2 tmpq = q - 2 ans[p - 1] = p + 1 ans[p] = q ans[q - 2] = p ans[q - 1] = q - 1 cnt += 4 p = tmpp q = tmpq for i in xrange(n): print ans[i]
PYTHON
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int N = 100500; int A[N]; int main() { int n; scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); return 0; } for (int i = 0; i < n; i++) A[i] = i; for (int i = 0; i < n / 2; i += 2) { A[i] = i + 1; A[i + 1] = n - i - 1; A[n - i - 1] = n - i - 2; A[n - i - 2] = i; } for (int i = 0; i < n; i++) printf("%d%c", A[i] + 1, " \n"[i == n - 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 == 1) { cout << "1" << endl; return 0; } if (((n / 2) * 2) % 4 == 0) { int p[n]; int temp; if (n % 2 != 0) p[n / 2] = (n + 1) / 2; for (int i = 0; i < n / 4; i++) { temp = i + n / 2 + (n % 2 != 0 ? 2 : 1); p[i] = temp; temp = n - i; p[p[i] - 1] = temp; temp -= (n / 2 + (n % 2 != 0 ? 1 : 0)); p[p[p[i] - 1] - 1] = temp; p[temp - 1] = (i + 1); } for (int i = 0; i < n; i++) cout << p[i] << " "; } else { cout << "-1" << endl; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> const int maxn = 110000; int a[maxn]; int n; int main() { scanf("%d", &n); if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); return 0; } for (int i = 1; i <= n / 4; i++) { a[2 * i - 1] = 2 * i; a[2 * i] = n - 2 * i + 2; a[n - 2 * i + 2] = n - 2 * i + 1; a[n - 2 * i + 1] = 2 * i - 1; } if (n & 1) a[(n + 1) / 2] = (n + 1) / 2; for (int i = 1; i < n; i++) printf("%d ", a[i]); printf("%d\n", a[n]); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> const long long INF = 2009000999; const float cp = 2 * acos(0.0); const float eps = 1e-18; using namespace std; int main() { long long n, p[1010000]; cin >> n; if (n % 4 == 2 || n % 4 == 3) { puts("-1"); return 0; } if (n % 4 == 0) { for (int i = 1; i <= n / 2; i++) { if (i % 2 == 1) p[i] = i + 1, p[n + 1 - i] = n - i; else p[i] = n + 2 - i, p[n + 1 - i] = i - 1; } for (int(i) = (1); (i) <= (n); i++) cout << p[i] << ' '; } else { for (int(i) = (1); (i) <= (n / 2); i++) { if (i % 2 == 1) p[i] = i + 1, p[n + 1 - i] = n - i; else p[i] = n + 2 - i, p[n + 1 - i] = i - 1; } p[n / 2 + 1] = n / 2 + 1; for (int(i) = (1); (i) <= (n); i++) cout << 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
n=input() a=[0]*(n+1) b=[0]*(n+1) if n==1: print 1 elif n>3 and (n%4==0 or (n-1)%4==0): if n%2==1: a[(n+1)/2]=(n+1)/2 b[(n+1)/2]=(n+1)/2 j=1 while j<n/2: a[j]=j+1 k=j+1 pre=j while a[k]!=j: a[k]=n+1-pre pre=k k=a[k] j+=2 for i in range(1,n+1): print a[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; int ans[100000]; int main() { int n; scanf("%d", &n); if (n / 2 % 2) puts("-1"); else { bool flag = 0; if (n & 1) { --n; flag = 1; } for (int i = 0; i < n / 2; i += 2) ans[i] = i + 2; for (int i = n / 2; i < n; i += 2) ans[i] = n - i - 1; for (int i = 1; i < n / 2; i += 2) ans[i] = n + 2 - ans[i - 1]; for (int i = n / 2 + 1; i < n; i += 2) ans[i] = n - ans[i - 1]; for (int i = 0; i < n / 2; ++i) printf("%d ", ans[i] + (ans[i] > n / 2 && flag)); if (flag) printf("%d ", n / 2 + 1); for (int i = n / 2; i < n; ++i) printf("%d ", ans[i] + (ans[i] > n / 2 && flag)); puts(""); if (flag) ++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 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 = (int)5e7; 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.Comparator; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Collection; import java.util.List; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.ArrayList; import java.util.StringTokenizer; import java.math.BigInteger; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author AlexFetisov */ 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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); if (n == 1) { out.println(1); return; } if (n % 4 == 3 || n % 4 == 2) { out.println(-1); return; } int[] p = new int[n]; rec(p, 0, p.length-1, 1, p.length); out.println(ArrayUtils.toString(p)); } private void rec(int[] p, int le, int ri, int low, int high) { if (le == ri) { p[le] = low; return; } if (le > ri) { return; } p[le] = low + 1; p[le + 1] = high; p[ri] = high-1; p[ri-1] = low; rec(p, le + 2, ri - 2, low + 2, high - 2); } } class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } } class ArrayUtils { public static String toString(int[] a) { StringBuilder result = new StringBuilder(""); for (int x : a) result.append(x).append(' '); return result.substring(0, result.length()-1); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; bool used[123456]; int main() { int n; cin >> n; vector<pair<int, int> > v1, v2; vector<int> ans; int a = 2, b = n; while (a < b) { used[a] = used[b] = 1; v1.push_back(make_pair(a, b)); a += 2; b -= 2; } a = 1, b = n - 1; while (a < b) { used[a] = used[b] = 1; v2.push_back(make_pair(a, b)); a += 2; b -= 2; } reverse(v2.begin(), v2.end()); for (int i = 0; i < (int)v1.size(); ++i) { ans.push_back(v1[i].first); ans.push_back(v1[i].second); } if (2 * (int)v1.size() + 2 * (int)v2.size() + 1 == n) { for (int i = 0; i < n; ++i) if (!used[i + 1]) { ans.push_back(i + 1); break; } } for (int i = 0; i < (int)v2.size(); ++i) { ans.push_back(v2[i].first); ans.push_back(v2[i].second); } if ((int)ans.size() == n) { for (int i = 0; i < (int)ans.size(); ++i) { printf("%d%c", ans[i], " \n"[i == (int)ans.size() - 1]); } } else { puts("-1"); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[100007]; int n; int main() { int i, j; while (~scanf("%d", &n)) { if (n % 4 == 2 || n % 4 == 3) { printf("-1\n"); continue; } for (i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; } if (n & 1) { a[n / 2 + 1] = n / 2 + 1; } for (i = 1; i < n; ++i) printf("%d ", a[i]); printf("%d\n", 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> const double PI = acos(-1.0); using namespace std; int p[100010]; int main() { int n, i; int a, b; while (cin >> n) { if (n % 4 == 0 || n % 4 == 1) { for (i = 1; i <= n; i++) p[i] = i; a = 1; b = n; for (i = 1; i < n / 2; i += 2) { p[i] = a + 1; p[i + 1] = b; p[n - i] = a; p[n - i + 1] = b - 1; a += 2; b -= 2; } for (i = 1; i <= n; i++) printf("%d ", p[i]); puts(""); } else puts("-1"); } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Throwable { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n==1) { System.out.println(1); return; } if((n%2==0 && (n/2)%2==1) || (n%2==1 && ((n-1)/2)%2==1)) { System.out.println(-1); return; } int[] a = new int[n+1]; for(int i = 1; i <= n;i++) { if(a[i]==0) { if(n%2==1 && i==n/2+1) a[i] = i; else a[i] = i+1; int idx = i; while(a[a[idx]]==0) { a[a[idx]] = n-idx+1; idx = a[idx]; } } } StringBuilder sb = new StringBuilder(); for(int i = 1; i < a.length;i++) sb.append(a[i]+" "); System.out.println(sb); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) {br = new BufferedReader(new InputStreamReader(s));} public Scanner(String file) throws FileNotFoundException {br = new BufferedReader(new FileReader(file));} public String next() throws IOException {while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken();} public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public int[] nexIntArray() throws Throwable { st = new StringTokenizer(br.readLine()); int[] a = new int[st.countTokens()]; for (int i = 0; i < a.length; i++)a[i] = nextInt(); return a; } public boolean ready() throws IOException {return br.ready();} } }
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[100000 + 11]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; if (n % 4 >= 2) { cout << -1; return 0; } if (n == 1) { cout << 1; return 0; } p[n - 1] = 1; int fill = 1; int prev = n - 1; while (fill < n) { if (p[p[prev]] and p[p[prev]] != n - prev + 1) { cout << -1; return 0; } if (p[p[prev]]) { prev -= 3; int num = 1; while (p[num]) num++; if (p[prev]) prev++; p[prev] = num; fill++; continue; } p[p[prev]] = n - prev + 1; prev = p[prev]; fill++; } for (int i = 1; i <= n; i++) { cout << p[i] << ' '; } return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int a[100010]; int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << "-1" << endl; return 0; } if (n % 4 == 1) a[(n + 1) / 2] = (n + 1) / 2; int i = 1; int end = n; while (i < end) { a[i] = end - 1; a[end - 1] = (n - i + 1); a[(n - i + 1)] = n - (end - 1) + 1; a[n - (end - 1) + 1] = n - (n - i + 1) + 1; end -= 2; i += 2; } for (int p = 1; p <= n; p++) cout << a[p] << " "; cout << endl; return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d", &n); m = n; int A[n]; if ((n - 2) % 4 == 0 || (n - 3) % 4 == 0) { printf("-1\n"); return 0; } int i = 2; int p = 0; while (n > 0) { if (n == 1) { A[p] = p + 1; n -= 1; } else { A[p] = i; A[i - 1] = m - p; A[m - p - 1] = m - i + 1; A[m - i] = p + 1; p += 2; i += 2; n -= 4; } } for (int i = 0; i < m; 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
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int M = 1e5 + 5; int main() { ios_base::sync_with_stdio(false); int n, i, p[N]; cin >> n; if (n % 2 == 0) { if (n % 4 != 0) { cout << -1 << endl; return 0; } else { int foo = n / 4; for (i = 1; i <= 1 + 2 * (foo - 1); i += 2) { p[i] = i + 1; p[i + 1] = (n + 1) - i; p[(n + 1) - i] = (n + 1) - (i + 1); p[(n + 1) - i - 1] = (n + 1) - ((n + 1) - i); } for (i = 1; i <= n; i++) cout << p[i] << " "; cout << endl; } } else { if ((n - 1) % 4 != 0) { cout << -1 << endl; return 0; } else { n--; int foo = n / 4; for (i = 1; i <= 1 + 2 * (foo - 1); i += 2) { p[i] = i + 1; p[i + 1] = (n + 2) - i; p[(n + 2) - i] = (n + 2) - (i + 1); p[(n + 2) - i - 1] = (n + 2) - ((n + 2) - i); } p[(n / 2) + 1] = 1 + (n / 2); for (i = 1; i <= n + 1; 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
#include <bits/stdc++.h> using namespace std; int n; int nums[100001]; int main() { cin >> n; if (n % 4 == 0 || n % 4 == 1) { for (int i = 1; i <= n / 2; i += 2) { nums[i] = i + 1; nums[i + 1] = n - i + 1; nums[n - i + 1] = n - i; nums[n - i] = i; } if (n % 4 == 1) nums[n / 2 + 1] = n / 2 + 1; for (int i = 1; i < n; ++i) cout << nums[i] << ' '; cout << nums[n] << endl; } 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> using namespace std; int main() { int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) { cout << -1; return 0; } int ary[n + 1]; for (int i = 1; i <= n / 2; i += 2) { ary[i] = i + 1; ary[i + 1] = n - i + 1; ary[n - i + 1] = n - i; ary[n - i] = i; } if (n % 4 == 1) ary[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n; i++) cout << ary[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 N = 200011; const int MOD = 1e9 + 7; long long int powmod(long long int a, long long int b) { if (b == 0) return 1; long long int x = powmod(a, b / 2); long long int y = (x * x) % MOD; if (b % 2) return (a * y) % MOD; return y % MOD; } int main() { int n; scanf("%d", &n); if (n == 1) { printf("1\n"); return 0; } int a[n + 2]; if (n % 4 <= 1) { if (n % 4) { int mid = (n + 1) / 2, add = n - 2, sign = 1; a[mid] = mid; mid++; a[mid] = 1; for (int i = mid + 1; i <= n; i++) { a[i] = a[i - 1] + add * sign; add -= 2; sign *= -1; } a[1] = n / 2, add = 3, sign = 1; for (int i = 2; i <= n / 2; i++) { a[i] = a[i - 1] + add * sign; add += 2; sign *= -1; } } else { int mid = n / 2 + 1, add = n - 2, sign = 1; a[mid] = 1; for (int i = mid + 1; i <= n; i++) { a[i] = a[i - 1] + add * sign; add -= 2; sign *= -1; } a[1] = n / 2, add = 2, sign = 1; for (int i = 2; i <= n / 2; i++) { a[i] = a[i - 1] + add * sign; add += 2; sign *= -1; } } for (int i = 1; i <= n; i++) printf("%d ", a[i]); printf("\n"); return 0; } printf("-1\n"); return 0; }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int a[maxn]; int book[maxn]; set<int> num; int main() { int n, i; cin >> n; if (n == 1) return 0 * printf("1\n"); if (n % 4 > 1) return 0 * printf("-1\n"); for (i = 1; i <= n; i++) num.insert(i); int m = n; if (n & 1) { a[n / 2 + 1] = n / 2 + 1; book[n / 2 + 1] = 1; num.erase(n / 2 + 1); } a[1] = 2; a[2] = n; a[n] = n - 1; a[n - 1] = 1; book[2] = 1; book[1] = 1; book[n] = 1; book[n - 1] = 1; num.erase(2); num.erase(n); num.erase(1); num.erase(n - 1); for (i = 1; i <= n; i++) { if (book[i] == 0) { int x; x = i + 1; a[i] = x; a[x] = n - i + 1; a[n - i + 1] = n - x + 1; a[n - x + 1] = i; num.erase(x); num.erase(n - i + 1); num.erase(n - x + 1); num.erase(i); book[i] = 1; book[x] = 1; book[n - x + 1] = 1; book[n - i + 1] = 1; } } printf("%d", a[1]); for (i = 2; i <= n; i++) { printf(" %d", 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.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solver { BufferedReader br; PrintWriter pw; StringTokenizer stok; public static void main(String[] args) throws NumberFormatException, IOException{ new Solver().Run(); } public String nextToken() throws IOException{ while (stok==null || !stok.hasMoreTokens()){ stok=new StringTokenizer(br.readLine()); } return stok.nextToken(); } public int nextInt() throws NumberFormatException, IOException{ return Integer.parseInt(nextToken()); } public long nextLong() throws NumberFormatException, IOException{ return Long.parseLong(nextToken()); } public double nextDouble() throws NumberFormatException, IOException{ return Double.parseDouble(nextToken()); } int n; public void Run() throws NumberFormatException, IOException{ //br=new BufferedReader(new FileReader("input.txt")); pw=new PrintWriter("output.txt"); br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out)); n=nextInt(); if (n %4 ==0 || (n-1)%4==0){ int[] mas =new int[n]; int i=0; int j=n-1; while (i+1<j-1){ mas[i]=i+2; mas[i+1]=j+1; mas[j]=j; mas[j-1]=i+1; i+=2; j-=2; } if (n%2!=0){ mas[n/2]=n/2+1; } for (i=0; i<n; i++){ pw.print(mas[i]); pw.print(' '); } pw.println(); } else { pw.println(-1); } pw.flush(); pw.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.StringTokenizer; /** * 286A * O(n) time * O(n) space * * @author artyom */ public class _286A implements Runnable { private BufferedReader in; private StringTokenizer tok; private Object solve() throws IOException { int n = nextInt(), r = n % 4; if (r > 1) { return -1; } StringBuilder sb = new StringBuilder(); int k = 2, m = n, l = n / 4; for (int i = 0; i < l; i++, k += 2, m -= 2) { sb.append(k).append(' ').append(m).append(' '); } k -= 3; m += 1; if (r == 1) { sb.append((n + 1) / 2).append(' '); } for (int i = 0; i < l; i++, k -= 2, m += 2) { sb.append(k).append(' ').append(m).append(' '); } return sb; } //-------------------------------------------------------------- public static void main(String[] args) { new _286A().run(); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tok = null; System.out.print(solve()); in.close(); } catch (IOException e) { System.exit(0); } } private String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } }
JAVA
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; const int N = 101000; int P[N]; int n; int main() { scanf("%d", &n); if ((n % 4) > 1) { printf("-1\n"); } else { int dl = (n - (n & 1)); bool dol = true; int naDol = 2, gora = dl; for (int i = 0; i < (dl / 2); ++i) { if (dol) { P[i] = naDol; naDol += 2; } else { P[i] = gora; gora -= 2; } dol = !dol; } naDol = n / 2 - 1; gora = naDol + 2; for (int i = 0; i < (dl / 2); ++i) { if (dol) { P[dl / 2 + i] = naDol; naDol -= 2; } else { P[dl / 2 + i] = gora; gora += 2; } dol = !dol; } if (n % 2 == 0) { for (int i = 0; i < (n); ++i) printf("%d ", P[i]); } else { int pom = (n + 1) / 2; for (int i = 0; i < (dl / 2); ++i) { printf("%d ", P[i] + (P[i] >= pom)); } printf("%d ", pom); for (int i = 0; i < (dl / 2); ++i) { printf("%d ", P[dl / 2 + i] + (P[dl / 2 + i] >= pom)); } } printf("\n"); } }
CPP
287_C. Lucky Permutation
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≀ pi ≀ n). A lucky permutation is such permutation p, that any integer i (1 ≀ i ≀ n) meets this condition ppi = n - i + 1. You have integer n. Find some lucky permutation p of size n. Input The first line contains integer n (1 ≀ n ≀ 105) β€” the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n) after a space β€” the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
2
9
#include <bits/stdc++.h> using namespace std; int a[100010]; int main() { int n; scanf("%d", &n); if (n == 1) puts("1"); else if (n % 4 > 1) puts("-1"); else { int i = 1, j = n; for (; j - i > 0; i += 2, j -= 2) { a[i] = i + 1; a[i + 1] = j; a[j] = j - 1; a[j - 1] = i; } if (n & 1) a[n / 2 + 1] = n / 2 + 1; 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
#include <bits/stdc++.h> using namespace std; int M[100001]; int main() { int n; cin >> n; if (n == 1) { cout << 1 << endl; return 0; } if (n % 4 == 2) { cout << -1 << endl; return 0; } if (n % 2 == 0) { int j = n; for (int i = n / 2; i > 0; i -= 2) { M[i] = j; j -= 2; } for (int i = 1; i < n / 2; i += 2) { M[i] = j; j -= 2; } j = 1; for (int i = n / 2 + 1; i < n; i += 2) { M[i] = j; j += 2; } for (int i = n; i > n / 2; i -= 2) { M[i] = j; j += 2; } for (int i = 1; i <= n; i++) { cout << M[i] << " "; } return 0; } else { if ((n + 1) % 4 == 0) { cout << -1 << endl; return 0; } else { int j = 2; for (int i = 1; i < n / 2; i += 2) { M[i] = j; j += 2; } for (int i = n / 2 + 3; i <= n; i += 2) { M[i] = j; j += 2; } j = 1; for (int i = n - 1; i >= 0; i--) { if (M[i] == 0) { M[i] = j; j += 2; } } for (int i = 1; i <= n; i++) { cout << M[i] << " "; } return 0; } } return 0; }
CPP