Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = int(input())
mod = n % 4
if mod == 2 or mod == 3:
print(-1)
else:
ans = [-1 for i in range(n)]
i = 0
while i < n // 2:
ans[i] = n - i - 1
ans[i+1] = i + 1
ans[n-1-i] = i + 2
ans[n-2-i] = n - i
i += 2
if mod == 1:
ans[n//2] = n//2 + 1
print(' '.join(map(str, ans))) | PYTHON3 |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long maxx = 1ll << 32;
const int maxn = 100005;
int n, k = 0, m, l, r, x, y, t;
int a[maxn];
long long c[maxn];
int dp[maxn];
int vis[maxn];
int gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
struct lee {
int num;
int x, y;
} lo[maxn];
struct le {
int v, sum;
} lm[maxn];
char ss;
string s1, s2;
stack<long long> stackk;
int main() {
while (~scanf("%d", &n)) {
if (n % 4 != 0 && n % 4 != 1)
printf("-1\n");
else {
int k = 1;
for (int i = 1; i <= n; i++) vis[i] = 0;
if (n & 1) a[n / 2 + 1] = n / 2 + 1;
for (int i = n / 2; vis[i] == 0 && i; i--) {
a[i] = k;
a[n + 1 - k] = i;
a[n + 1 - i] = n + 1 - k;
a[k] = n + 1 - i;
vis[i] = vis[n + 1 - k] = vis[n + 1 - i] = vis[k] = 1;
k++;
}
for (int i = 1; i <= n; i++)
i == n ? printf("%d\n", a[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.util.*;
public class C {
Scanner sc = new Scanner(System.in);
void doIt()
{
int n = sc.nextInt();
if(n % 4 >= 2) System.out.println(-1);
else {
int [] ans = new int[n];
if(n % 4 == 1) ans[n / 2] = (n + 1) / 2;
for(int i = 0; i < n / 2; i += 2 ){
ans[i] = i+2;
ans[i+1] = n-i;
ans[n-i-2] = i+1;
ans[n-i-1] = n-i-1;
//System.out.println(Arrays.toString(ans));
}
StringBuilder sb = new StringBuilder("");
for(int i = 0; i < n; i++) sb.append(ans[i] + " ");
System.out.println(sb);
}
}
/**
* @param args
*/
public static void main(String[] args) {
new C().doIt();
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int v[100010];
int main() {
int n;
while (cin >> n) {
memset(v, 0, sizeof(v));
if (n % 4 == 2 || n % 4 == 3) {
cout << -1 << endl;
continue;
}
if (n == 1) {
cout << 1 << endl;
continue;
}
int m = n / 4;
m <<= 1;
int i;
for (i = 1; i <= m; i += 2) {
v[i] = i + 1;
v[i + 1] = n - i + 1;
v[n - i + 1] = n - i;
v[n - i] = i;
}
if (n % 4 == 1) {
v[i] = i;
}
for (int i = 1; i <= n; i++) {
printf("%d ", v[i]);
}
cout << endl;
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n=int(input())
if(n%4>1):
print(-1)
else:
ans=[0]*(n+1)
i,j,a,b=1,n,1,n
while(i<j and a<=n and b>=1):
ans[i],ans[j]=a+1,b-1
ans[i+1],ans[j-1]=b,a
i+=2
j-=2
a+=2
b-=2
if(i==j):
ans[i]=a
for i in range(1,n+1):
print(ans[i],end=' ')
| 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;
void _ad(int &p) {
static int i = 0;
p = i++;
}
int n;
int perm[100001] = {0};
bool checkit(vector<int> &a) {
for (int i = 0; i < a.size(); i++) {
if (a[a[i] - 1] != a.size() - i) return false;
}
return true;
}
void past(int l, int r, int mn, int mx) {
perm[l] = mn + 1;
perm[l + 1] = mx;
perm[r - 1] = mx - 1;
perm[r - 2] = mn;
}
int main() {
cin >> n;
if (n == 1) {
cout << 1;
return 0;
}
if (n < 4) {
cout << -1;
return 0;
}
if (n % 4 == 0 || n % 4 == 1) {
int mn = 1;
int mx = n;
int l = 0, r = n;
while (l < r - 1) {
past(l, r, mn, mx);
l += 2;
r -= 2;
mn += 2;
mx -= 2;
}
if (n % 2 == 1) perm[n / 2] = mn;
for (int i = 0; i < n; i++) {
printf("%d ", perm[i]);
}
return 0;
}
cout << -1;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Locale;
import java.util.StringTokenizer;
public class A {
private void solve() throws IOException {
int n = nextInt();
if (n % 4 == 2 || n % 4 == 3) {
println("-1");
return;
}
int l = 0;
int r = n - 1;
int L = 1;
int R = n;
int[] a = new int[n];
int m = n;
while (m >= 4) {
a[l] = L + 1;
a[l + 1] = R;
a[r] = R - 1;
a[r - 1] = L;
l += 2;
r -= 2;
L += 2;
R -= 2;
m -= 4;
}
if (m == 1) {
a[l] = l + 1;
}
for (int i: a) {
print(i + " ");
}
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
private int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
private double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private void print(Object o) {
writer.print(o);
}
private void println(Object o) {
writer.println(o);
}
private void printf(String format, Object... o) {
writer.printf(format, o);
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
Locale.setDefault(Locale.US);
new A().run();
System.err.printf("%.3f\n", 1e-3 * (System.currentTimeMillis() - time));
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
private void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(13);
}
}
} | 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 = 101000;
int a[Maxn];
int n;
int main() {
scanf("%d", &n);
if ((n / 2) % 2 == 1) {
printf("-1\n");
return 0;
}
for (int i1 = 1, i2 = 2, i3 = n - 1, i4 = n; i2 < i3;) {
a[i1] = i2;
a[i2] = i4;
a[i4] = i3;
a[i3] = i1;
i1 += 2;
i2 += 2;
i3 -= 2;
i4 -= 2;
}
if (n % 2 == 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 n, a[100005], i;
void Solve(int left, int right) {
if (left > right) return;
if (right - left < 3) {
if (left == right) a[left] = right;
if (left + 1 == right) a[left] = left, a[right] = right;
if (left + 2 == right)
a[left] = right, a[left + 1] = left + 1, a[right] = left;
return;
}
a[left] = left + 1;
a[left + 1] = right;
a[right] = right - 1;
a[right - 1] = left;
Solve(left + 2, right - 2);
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
Solve(1, n);
for (i = 1; i <= n; ++i)
if (a[a[i]] != n - i + 1) return cout << "-1\n", 0;
for (i = 1; i <= n; ++i) cout << a[i] << " \n"[i == 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 max_n = 1e5 + 15;
int n, p[max_n];
int main() {
cin >> n;
if ((n & 3) > 1) {
cout << -1;
return 0;
}
if (n == 1) {
cout << 1;
return 0;
}
for (int i = 0; i + i + 1 < n; i += 2) {
p[i] = i + 2;
p[n - i - 1] = n + 1 - p[i];
p[i + 1] = p[n - i - 1] + 1;
p[n - i - 2] = n + 1 - p[i + 1];
}
if (n & 1) p[n / 2] = n / 2 + 1;
for (int i = 0; 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;
long p[100001];
bool visit[100001];
long n;
bool DFS(int i);
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
if (n > 1) {
memset(p, -1, sizeof(p));
memset(visit, false, sizeof(visit));
bool key = true;
for (int i = 1; i <= n; i++) {
if (!visit[i]) {
if (i + 1 <= n && !visit[i + 1])
p[i] = i + 1;
else
p[i] = i;
key = key && DFS(i);
}
}
if (key) {
for (int i = 1; i <= n; i++) cout << p[i] << " ";
} else
cout << "-1";
} else if (n == 1)
cout << "1";
return 0;
}
bool DFS(int i) {
long temp = n - i + 1;
visit[i] = true;
if (p[p[i]] == -1) {
p[p[i]] = temp;
return DFS(p[i]);
} else if (p[p[i]] == temp)
return true;
else
return false;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author BSRK Aditya ([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 n = in.readInt();
if(n%4 <= 1) {
int [] ans = new int[n];
for(int i = 1, j = 2; 2*i < n; i += 2, j += 2) {
ans[j-1] = i;
ans[i-1] = n+1-j;
ans[n-j] = n+1-i;
ans[n-i] = j;
}
if(n%4 == 1) ans[(n-1)/2] = (n-1)/2+1;
for(int v : ans) out.print(v + " ");
out.printLine();
} else {
out.printLine(-1);
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void 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;
const int N = 100005;
int a[N];
int main() {
int n, i;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3)
printf("-1\n");
else {
for (i = 1; i <= n / 2; i += 2) {
a[i] = i + 1;
a[i + 1] = n - i + 1;
a[n - i] = i;
a[n - i + 1] = n - i;
}
if (n % 4 == 1) a[n / 2 + 1] = n / 2 + 1;
for (i = 1; i < n; i++) printf("%d ", a[i]);
printf("%d\n", a[n]);
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int ans[100000];
int main() {
int a;
scanf("%d", &a);
if (a % 4 == 2 || a % 4 == 3) {
printf("-1\n");
return 0;
}
if (a % 4 == 0) {
for (int i = 0; i < a / 4; i++) {
ans[i * 2] = i * 2 + 2;
ans[i * 2 + 1] = a - i * 2;
ans[a - 1 - i * 2] = a - 1 - i * 2;
ans[a - 2 - i * 2] = i * 2 + 1;
}
} else {
for (int i = 0; i < a / 4; i++) {
ans[i * 2] = i * 2 + 2;
ans[i * 2 + 1] = a - i * 2;
ans[a - 1 - i * 2] = a - 1 - i * 2;
ans[a - 2 - i * 2] = i * 2 + 1;
}
ans[a / 2] = a / 2 + 1;
}
for (int i = 0; i < a; i++) printf("%d ", ans[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 a, b, c, d, n, m, i, j, ans, l, r, lcnt = 1, rcnt;
int ar[100005];
int main() {
scanf("%d\n", &n);
if (n % 4 > 1) {
printf("-1\n");
return 0;
}
lcnt = 1, rcnt = n;
l = 1, r = n;
for (i = 0; i <= n - 4; i += 4) {
ar[l++] = lcnt + 1;
ar[l++] = rcnt;
ar[r--] = rcnt - 1;
ar[r--] = lcnt;
lcnt += 2;
rcnt -= 2;
}
if (n % 4 == 1) {
ar[n / 2 + 1] = n / 2 + 1;
}
for (i = 1; i <= n; i++) {
printf("%d ", ar[i]);
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int n;
int f[100002];
void DP() {
int i, x, y;
for (i = 1; 2 * i <= n; i += 2) {
x = i;
y = i + 1;
f[x] = y;
f[y] = n - x + 1;
f[n - x + 1] = n - y + 1;
f[n - y + 1] = x;
}
if (n % 4) f[(n + 1) / 2] = (n + 1) / 2;
for (i = 1; i < n; i++) printf("%d ", f[i]);
printf("%d\n", f[n]);
}
int main() {
while (~scanf("%d", &n)) {
if (n == 1 || (n % 4) <= 1)
DP();
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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int N = r.nextInt();
if(N % 4 == 0 || (N % 4 == 1)){
int[] a = new int[N];
int rem = N;
if(N % 2 == 1)a[N / 2] = N / 2 + 1;
for(int i = 0; i < N; i += 2){
if(rem < 4)break;
a[i] = (i + 1) + 1;
a[i + 1] = N - i - 1 + 1;
a[m(i, N) - 1] = i + 1;
a[m(i, N)] = m(i, N) - 1 + 1;
rem -= 4;
}
for(int i = 0; i < N; i++)
out.println(a[i]);
}else{
out.println(-1);
}
out.close();
}
private static int m(int i, int N) {
return N - i - 1;
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String nextLine(){
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| 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() {
long long int n, i;
cin >> n;
if (n % 4 != 1 && n % 4 != 0) {
cout << "-1";
return 0;
}
long long int a[n];
for (i = 0; i < n / 4; i++) {
a[2 * i] = 2 * i + 2;
a[2 * i + 1] = n - 2 * i;
a[n - 2 * i - 1] = n - 2 * i - 1;
a[n - 2 * i - 2] = 2 * i + 1;
}
if (n % 4 == 1) {
a[n / 2] = n / 2 + 1;
}
for (i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.*;
import java.io.*;
public class a {
static long mod = 1000000007;
public static void main(String[] args) throws IOException
{
input.init(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = input.nextInt();
int[] res = new int[n+1];
if(n%4 == 2 || n%4 == 3)
{
out.println(-1);
}
else if(n%4 == 1)
{
res[(n+1)/2] = (n+1)/2;
int at = (n+1)/2;
for(int i = 0; i<n/4; i++)
{
res[at+1+2*i] = res[at]-2-2*i;
res[at+2+2*i] = res[at]+1+2*i;
res[at-1-2*i] = res[at]+2+2*i;
res[at-2-2*i] = res[at]-1-2*i;
}
for(int i = 1; i<=n; i++) out.print(res[i]+" ");
}
else if(n%4 == 0)
{
int at = n/2;
res[at] = n/2+2;
for(int i = 0; i<n/4; i++)
{
res[at+1+2*i] = res[at]-3-2*i;
res[at-2*i] = res[at]+2*i;
res[at-1-2*i] = res[at]-2-2*i;
res[at+2+2*i] = res[at]-1+2*i;
}
for(int i = 1; i<=n; i++) out.print(res[i]+" ");
}
out.close();
}
//static long gcd(long a, long b)
//{
// if(b==0) return a;
// return gcd(b,a%b);
//}
//static long choose(int n, int k)
//{
// long num = 1, denom = 1;
// for(int i = 0; i<k; i++)
// {
// num = (num*(n-i))%mod;
// denom = (denom*(k-i))%mod;
// }
// return (num * modinv(denom, mod))%mod;
//}
//static long[] gcd(long p, long q) {
// if (q == 0)
// return new long[] { p, 1, 0 };
// long[] vals = gcd(q, p % q);
// long d = vals[0];
// long a = vals[2];
// long b = vals[1] - (p / q) * vals[2];
// return new long[] { d, a, b };
//}
//
//static long modinv(long k, long n) {
// long[] vals = gcd(k, n);
// long d = vals[0];
// long a = vals[1];
// long b = vals[2];
// if (a > 0) return a;
// return n + a;
//}
static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static String nextLine() throws IOException {
return reader.readLine();
}
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int DEBUG = 0;
int main(int argc, char **argv) {
DEBUG = (argc >= 2) ? atoi(argv[1]) : 0;
int n;
scanf("%d", &n);
if (n == 1) {
cout << 1 << endl;
return 0;
}
if ((n % 4) == 0 || (n % 4) == 1) {
int k = n / 4;
for (int i = 1; i <= k; i++) {
cout << (2 * i) << " " << (n - 2 * (i - 1)) << " ";
}
if (n == (4 * k + 1)) {
cout << (2 * k + 1) << " ";
}
for (int i = k; i >= 1; i--) {
cout << (2 * i - 1) << " " << (n - (2 * i - 1)) << " ";
}
cout << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int P[100010];
int main(int argc, const char *argv[]) {
int n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << -1;
return 0;
}
for (int i = 1; i <= (n - 1) / 2; i += 2) {
P[i] = i + 1;
for (int j = i; P[P[j]] == 0; j = P[j]) {
P[P[j]] = n - j + 1;
}
}
if (n % 4 == 1) P[(n + 1) / 2] = (n + 1) / 2;
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 | #import itertools
#def isok(l, n):
# for i in range(1, n+1):
# if l[l[i-1]-1] != n - i + 1:
# return False
# return True
n = int(raw_input())
#ok = 0
#for l in itertools.permutations([i for i in range(1, n+1)]):
# if isok(l, n):
# ok = 1
# print ' '.join(str(i) for i in l)
# break
#if ok == 0:
# print -1
if n % 4 == 2 or n % 4 == 3:
print -1
exit()
a = [0]*(n+1)
if n%2 == 1:
a[n/2+1] = n/2+1
for i in range(1, n/2, 2):
a[i] = i+1
a[i+1] = n-i+1
a[n-i+1] = n-i
a[n-i] = i
print ' '.join(map(str, a[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 outpu[100001];
set<int> se;
set<int>::iterator it;
int n;
int main() {
cin >> n;
bool k = 0;
if ((n - 1) % 4 == 0) {
k = 1;
outpu[n / 2 + 1] = n / 2 + 1;
}
if (n % 4 == 0 || n % 4 == 1) {
for (int i = 1; i <= n; i++)
if (k == 0 || (k == 1 && i != n / 2 + 1)) se.insert(i);
for (int i = 1; i <= n; i++)
if (outpu[i] == 0) {
for (it = se.begin(); it != se.end(); it++)
if (((*it) != i) && (*it) != (n - i + 1)) break;
int j = i;
outpu[j] = (*it);
se.erase(*it);
while (1) {
outpu[outpu[j]] = (n - j + 1);
j = outpu[j];
se.erase(j);
if (j == i) break;
}
}
for (int i = 1; i <= n; i++) {
cout << outpu[i] << ' ';
}
cout << endl;
} else {
cout << "-1" << endl;
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.IOException;
import java.util.InputMismatchException;
public class LuckyPermutation {
public static void main(String[] args) {
FasterScanner sc = new FasterScanner();
int N = sc.nextInt();
if (N % 4 == 2 || N % 4 == 3) {
System.out.println(-1);
return;
}
int[] arr = new int[N + 1];
int L = 1;
int R = N;
for (int i = 0; i < N / 4; i++) {
arr[L] = L + 1;
arr[L + 1] = R;
arr[R] = R - 1;
arr[R - 1] = L;
L += 2;
R -= 2;
}
if (N % 2 == 1) {
arr[N / 2 + 1] = N / 2 + 1;
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= N; i++) {
sb.append(arr[i] + " ");
}
System.out.println(sb.toString());
}
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.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 nextString() {
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 nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | from collections import Counter
from itertools import cycle, product as prod, permutations as perm, combinations as comb, combinations_with_replacement as combr
from sys import stdin, stdout
read_ints = lambda: map(int, raw_input().split())
read_floats = lambda: map(float, raw_input().split())
def main():
n = input()
b = [None] * (n + 1)
cur = 1
if n % 4 == 0:
for i in xrange(1, n/2+1, 2):
o = [i, i+1, n-i+1, n-i]
for j in xrange(4):
b[o[j]] = o[j+1 & 3]
print ' '.join(map(str, b[1:]))
elif n % 4 == 1:
for i in xrange(1, n/2+1, 2):
o = [i, i+1, n-i+1, n-i]
for j in xrange(4):
b[o[j]] = o[j+1 & 3]
b[n/2+1] = n/2+1
print ' '.join(map(str, b[1:]))
else:
print -1
if __name__ == '__main__':
main()
| PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long INV2 = 500000004;
long long INV6 = 166666668;
long long power(long long a, long long b, long long c) {
long long x = 1, y = a;
while (b > 0) {
if (b & 1) x = (x * y) % c;
y = (y * y) % c;
b /= 2;
}
return x % c;
}
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
int ans[100005];
bool fl;
int has[100005];
int main() {
int n;
scanf("%d", &n);
if (n == 2 || n == 3 || n == 6) {
printf("-1\n");
} else {
int mid = n / 2 + 1;
if (n & 1) {
ans[mid] = mid;
for (int i = 1; i < mid; i += 2) {
ans[i] = i + 1;
has[i + 1]++;
if (has[i + 1] > 1) fl = 1;
}
int val = n;
for (int i = 2; i < mid; i += 2) {
ans[i] = val;
val -= 2;
has[val]++;
if (has[val] > 1) fl = 1;
}
for (int i = n; i > mid; i -= 2) {
ans[i] = i - 1;
has[i - 1]++;
if (has[i - 1] > 1) fl = 1;
}
val = 1;
for (int i = n - 1; i > mid; i -= 2) {
ans[i] = val;
has[val]++;
if (has[val] > 1) fl = 1;
val += 2;
}
} else {
mid = n / 2;
for (int i = 1; i <= mid; i += 2) {
ans[i] = i + 1;
has[i + 1]++;
if (has[i + 1] > 1) fl = 1;
}
int val = n;
for (int i = 2; i <= mid; i += 2) {
ans[i] = val;
has[val]++;
if (has[val] > 1) fl = 1;
val -= 2;
}
for (int i = n; i > mid; i -= 2) {
ans[i] = i - 1;
has[i - 1]++;
if (has[i - 1] > 1) fl = 1;
}
val = 1;
for (int i = n - 1; i > mid; i -= 2) {
ans[i] = val;
has[val]++;
if (has[val] > 1) fl = 1;
val += 2;
}
}
for (int i = 1; i <= n; ++i) {
if (ans[ans[i]] != (n - i + 1)) {
fl = 1;
break;
}
}
if (fl) {
printf("-1\n");
return 0;
}
for (int i = 1; i <= n; ++i) {
printf("%d ", ans[i]);
}
printf("\n");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline bool iseq(double x, double y) {
if (fabs(x - y) < 1e-8) return true;
return false;
}
template <typename T>
inline T hpt(T x1, T y1, T x2, T y2) {
return hypot(x1 - x2, y1 - y2);
}
template <typename T>
inline T gcd(T a, T b) {
if (!b)
return a;
else
return gcd(b, a % b);
}
template <typename T>
inline void extended_euclid(T a, T b, T &x, T &y) {
if (a % b == 0)
x = 0, y = 1;
else {
extended_euclid(b, a % b, x, y);
T temp = x;
x = y;
y = -y * (a / b) + temp;
}
}
template <typename T>
inline T bigmod(T b, T p, T m) {
if (!p)
return 1;
else if (!(p % 2)) {
T x = bigmod(b, p / 2, m);
return x * x;
} else
return ((b % m) * bigmod(b, p - 1, m)) % m;
}
int prime[5 / 32 + 1];
void setbit(int i) {
int p = i >> 5, q = i & 31;
prime[p] |= (1 << q);
}
bool checkbit(int i) {
int p = i >> 5, q = i & 31;
return prime[p] & (1 << q) ? true : false;
}
void buildprime(int n) {
int i, j, k = sqrt(double(n));
prime[0] = 3;
for (i = 4; i < n; i += 2) setbit(i);
for (i = 3; i <= k; i += 2) {
if (!checkbit(i)) {
int ii = i + i;
for (j = i * i; j < n; j += ii) setbit(j);
}
}
}
int sum, r, n, N, R, C;
int val[150005];
int main() {
int i, j, k;
while (scanf("%d", &n) == 1) {
if (n % 4 > 1) {
printf("-1\n");
continue;
}
list<int> L;
for (int i = 1; i <= n; ++i) L.push_back(i);
int beg = 0;
int end_ = n - 1;
while (!L.empty()) {
if (beg == end_) {
val[beg] = L.front();
L.pop_front();
break;
}
int f1 = L.front();
L.pop_front();
int f2 = L.front();
L.pop_front();
int e1 = L.back();
L.pop_back();
int e2 = L.back();
L.pop_back();
val[beg] = f2;
val[beg + 1] = e1;
val[end_] = e2;
val[end_ - 1] = f1;
beg += 2;
end_ -= 2;
}
for (int i = 0; i < n; ++i) cout << val[i] << " ";
cout << endl;
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
int a[100001];
int main() {
int i, n;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3)
puts("-1");
else {
for (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 % 2) 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;
int n, num[100005];
int main() {
cin >> n;
if (!(n % 4 == 0 || n % 4 == 1)) {
cout << -1 << endl;
return 0;
}
int st = 1, en = n, cnt = 0;
while (1) {
if (cnt * 4 == n) break;
if (cnt * 4 + 1 == n) {
num[st] = st;
break;
}
num[st] = st + 1;
num[st + 1] = en;
num[en] = en - 1;
num[en - 1] = st;
st = st + 2;
en = en - 2;
cnt++;
}
for (int i = 0; i < n; i++) cout << num[i + 1] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | n = int(input())
if n%4 > 1:
print(-1)
else:
a = [n+1>>1]*n
for i in range(n//4):
j = i*2
a[j], a[j+1], a[-2-j], a[-1-j] = j+2, n-j, j+1, n-1-j
print(' '.join(map(str, a))) | 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.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
public class B {
static int N;
static PrintWriter out;
public static void main(String[] args) {
MScanner sc = new MScanner();
out = new PrintWriter(System.out);
int N = sc.nextInt();
if(N%4>1){
out.println(-1);
out.close();
return;
}
LinkedList<Integer> start = new LinkedList<Integer>();
if(N%4==1)start.add(1);
else{
start.add(2);
start.add(4);
start.add(1);
start.add(3);
}
while(start.size()!=N){
int s = start.size();
start.addFirst(s+4);
start.addFirst(2);
start.addLast(1);
start.addLast(s+3);
}
LinkedList<Integer> outputL = new LinkedList<Integer>();
LinkedList<Integer> outputR = new LinkedList<Integer>();
int val = 0;
while(!start.isEmpty()){
if(start.size()<4){
while(!start.isEmpty()){
outputL.add(start.poll()+val);
}
}
else{
outputL.addLast(start.poll()+val);
outputL.addLast(start.poll()+val);
outputR.addFirst(start.pollLast()+val);
outputR.addFirst(start.pollLast()+val);
val+=2;
}
}
for(Integer x : outputL)out.print(x+" ");
for(Integer x : outputR)out.print(x+" ");
out.close();
}
static class MScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public MScanner() {
stream = System.in;
// stream = new FileInputStream(new File("dec.in"));
}
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++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextInt(int N) {
int[] ret = new int[N];
for (int a = 0; a < N; a++)
ret[a] = nextInt();
return ret;
}
int[][] nextInt(int N, int M) {
int[][] ret = new int[N][M];
for (int a = 0; a < N; a++)
ret[a] = nextInt(M);
return ret;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLong(int N) {
long[] ret = new long[N];
for (int a = 0; a < N; a++)
ret[a] = nextLong();
return ret;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDouble(int N) {
double[] ret = new double[N];
for (int a = 0; a < N; a++)
ret[a] = nextDouble();
return ret;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] next(int N) {
String[] ret = new String[N];
for (int a = 0; a < N; a++)
ret[a] = next();
return ret;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
String[] nextLine(int N) {
String[] ret = new String[N];
for (int a = 0; a < N; a++)
ret[a] = nextLine();
return ret;
}
}
}
| 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;
int main() {
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3) {
printf("-1\n");
return 0;
}
if (n % 4) {
for (int i = 0; i < n / 2; ++i) {
if (i) printf(" ");
if (i & 1)
printf("%d", n - i + 1);
else
printf("%d", i + 2);
}
if (n / 2) printf(" ");
printf("%d", n / 2 + 1);
for (int i = n / 2 + 1; i < n; ++i) {
printf(" ");
if (i & 1)
printf("%d", n - i - 1);
else
printf("%d", i);
}
} else {
for (int i = 0; i < n / 2; ++i) {
if (i) printf(" ");
if (i & 1)
printf("%d", n - i + 1);
else
printf("%d", i + 2);
}
for (int i = n / 2; i < n; ++i) {
printf(" ");
if (i & 1)
printf("%d", i);
else
printf("%d", n - i - 1);
}
}
printf("\n");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;
import static java.lang.Math.*;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.lang.Character.*;
@SuppressWarnings("unused")
public class round176C {
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
static StringTokenizer st = new StringTokenizer("");
static int nextInt() throws Exception {
return Integer.parseInt(next());
}
static String next() throws Exception {
while (true) {
if (st.hasMoreTokens()) {
return st.nextToken();
}
String s = br.readLine();
if (s == null) {
return null;
}
st = new StringTokenizer(s);
}
}
public static void main(String[] args) throws Exception {
PrintWriter out = new PrintWriter(System.out);
int n = nextInt();
if(n % 4 == 2 || n % 4 == 3){
out.println(-1);
out.flush();
return;
}
int [] ans = new int [n + 1];
int mx = n;
int mn = 1;
for(int i = 0 ; i < n / 4 ; ++i){
ans[mn] = mn + 1;
ans[mn + 1] = mx;
ans[mx] = mx - 1;
ans[mx - 1] = mn;
mx -= 2;
mn += 2;
}
if(n % 4 == 1){
ans[n / 2 + 1] = n / 2 + 1;
}
for(int i = 1 ; i < n + 1 ; ++i){
out.print(ans[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 | import static java.util.Arrays.deepToString;
import java.io.*;
import java.math.*;
import java.util.*;
public class A {
static void solve() {
int n = nextInt();
int pairs = n / 2;
if (pairs % 2 == 1) {
writer.println(-1);
return;
}
int[] p = new int[n + 1];
if (n % 2 == 1) {
p[n / 2 + 1] = n / 2 + 1;
}
for (int it = 0; it < pairs / 2; it++) {
int i = 2 * it + 1;
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++) {
writer.print(p[i] + " ");
}
}
public static void main(String[] args) throws Exception {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
Locale.setDefault(Locale.US);
setTime();
solve();
printTime();
printMemory();
writer.close();
}
static BufferedReader reader;
static PrintWriter writer;
static StringTokenizer tok = new StringTokenizer("");
static long systemTime;
static void debug(Object... o) {
System.err.println(deepToString(o));
}
static void setTime() {
systemTime = System.currentTimeMillis();
}
static void printTime() {
System.err.println("Time consumed: " + (System.currentTimeMillis() - systemTime));
}
static void printMemory() {
System.err.println("Memory consumed: "
+ (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000 + "kb");
}
static String next() {
while (!tok.hasMoreTokens()) {
String w = null;
try {
w = reader.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (w == null)
return null;
tok = new StringTokenizer(w);
}
return tok.nextToken();
}
static int nextInt() {
return Integer.parseInt(next());
}
static long nextLong() {
return Long.parseLong(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
static BigInteger nextBigInteger() {
return new BigInteger(next());
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, -1, -1, 1, 1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int res[100010];
bool vis[100010];
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
if (n == 1)
cout << 1;
else {
int co = 1;
if (n & 1) vis[n / 2 + 1] = res[n / 2 + 1] = n / 2 + 1;
if ((n / 2) % 4 != 2 && (n / 2) % 4 != 0) {
cout << -1 << endl;
return 0;
}
for (long long i = 1; i < n + 1; i++) {
if (vis[i]) continue;
int x = n - i + 1;
res[i] = i + 1;
while (!vis[i]) {
vis[i] = 1;
int x = n - i + 1;
res[res[i]] = x;
i = res[i];
}
}
for (long long i = 1; i < n + 1; i++) cout << res[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 static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
final int maxn = 100100;
int n;
int a[] = new int[maxn];
private void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));//new FileReader("input.txt"));
out = new PrintWriter(System.out);//"output.txt");
n = nextInt();
if ((n - n % 2) % 4 != 0)
out.println("-1\n");
else {
if (n % 2 == 1)
a[n / 2 + 1] = n / 2 + 1;
int l = 1;
int r = n;
while (r - l + 1 >= 4) {
a[l] = l + 1;
a[l + 1] = r;
a[r - 1] = l;
a[r] = r - 1;
l += 2;
r -= 2;
}
for (int i = 1; i <= n; i++)
out.print(a[i] + " ");
out.println();
}
in.close();
out.close();
}
void chk(boolean b) {
if (b)
return;
System.out.println(new Error().getStackTrace()[1]);
exit(999);
}
void deb(String fmt, Object... args) {
System.out.printf(Locale.US, fmt + "%n", args);
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, p[N];
void Read_in() {
scanf("%d", &n);
return;
}
void Put_out() {
if (n % 4 == 2 || n % 4 == 3) {
printf("-1\n");
return;
}
for (int i = 1; i <= n; i++) {
printf("%d", p[i]);
if (i != n)
printf(" ");
else
printf("\n");
}
return;
}
void QAQ() {
if (n % 4 == 2 || n % 4 == 3) return;
for (int i = 1; i <= n / 2; i++) {
if (i % 2)
p[i] = i + 1, p[n - i + 1] = n - i;
else
p[i] = n - i + 2, p[n - i + 1] = i - 1;
}
if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1;
return;
}
int main() {
Read_in();
QAQ();
Put_out();
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 + 2;
int a[maxn];
int n;
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
int t = n % 4;
for (int i = 1; i <= n; i++) a[i] = i;
if (t == 1 or !t) {
int pt1 = 1;
int pt2 = n;
while (pt1 < pt2) {
swap(a[pt1], a[pt1 + 1]);
swap(a[pt2], a[pt2 - 1]);
swap(a[pt2 - 1], a[pt1 + 1]);
pt2 -= 2;
pt1 += 2;
}
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
} else
cout << -1;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
int a[1000020];
int main() {
cin >> n;
if (n == 1) {
puts("1");
} else if (n % 4 == 2 || n % 4 == 3) {
puts("-1");
} else {
for (int i = 0; i < n / 2; i++)
if (i % 2 == 0) {
a[i] = i + 1;
a[i + 1] = n - 1 - i;
a[n - 2 - i] = i;
a[n - 1 - i] = n - 2 - i;
}
if (n & 1) a[n / 2] = n / 2;
for (int i = 0; i < n; i++) printf("%d ", a[i] + 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.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class CodeA
{
static class Scanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String nextLine()
{
try
{
return br.readLine();
}
catch(Exception e)
{
throw(new RuntimeException());
}
}
public String next()
{
while(!st.hasMoreTokens())
{
String l = nextLine();
if(l == null)
return null;
st = new StringTokenizer(l);
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public int[] nextIntArray(int n)
{
int[] res = new int[n];
for(int i = 0; i < res.length; i++)
res[i] = nextInt();
return res;
}
public long[] nextLongArray(int n)
{
long[] res = new long[n];
for(int i = 0; i < res.length; i++)
res[i] = nextLong();
return res;
}
public double[] nextDoubleArray(int n)
{
double[] res = new double[n];
for(int i = 0; i < res.length; i++)
res[i] = nextLong();
return res;
}
public void sortIntArray(int[] array)
{
Integer[] vals = new Integer[array.length];
for(int i = 0; i < array.length; i++)
vals[i] = array[i];
Arrays.sort(vals);
for(int i = 0; i < array.length; i++)
array[i] = vals[i];
}
public void sortLongArray(long[] array)
{
Long[] vals = new Long[array.length];
for(int i = 0; i < array.length; i++)
vals[i] = array[i];
Arrays.sort(vals);
for(int i = 0; i < array.length; i++)
array[i] = vals[i];
}
public void sortDoubleArray(double[] array)
{
Double[] vals = new Double[array.length];
for(int i = 0; i < array.length; i++)
vals[i] = array[i];
Arrays.sort(vals);
for(int i = 0; i < array.length; i++)
array[i] = vals[i];
}
}
static int[] permutation;
public static boolean poner(int n, int val)
{
if(permutation[n] != 0)
{
if(permutation[n] != val)
return false;
return true;
}
else
{
permutation[n] = val;
return poner(val, (permutation.length - 1) - n + 1);
}
}
public static boolean intentar(int n)
{
permutation = new int[n + 1];
if(n == 1)
{
permutation[1] = 1;
return true;
}
for(int i = 1; permutation[i] == 0; i += 2)
{
if(!poner(i, i + 1))
{
permutation[i] = 0;
if(!poner(i, i))
return false;
return true;
}
}
return true;
}
public static void main(String[] args)
{
Scanner sc = new Scanner();
int n = sc.nextInt();
if(intentar(n))
{
for(int i = 1; i < permutation.length; i++)
System.out.print((i == 1 ? "" : " ") + permutation[i]);
System.out.println();
}
else
System.out.println(-1);
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author George Marcus
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
int[] A;
int N;
public void solve(int testNumber, InputReader in, PrintWriter out) {
N = in.nextInt();
if(N % 4 > 1) {
out.println(-1);
return;
}
LinkedList<Integer> left = new LinkedList<Integer>();
for(int i = 1; i <= N; i++)
left.addLast(i);
A = new int[N + 1];
if(N % 2 == 1)
A[N / 2 + 1] = N / 2 + 1;
boolean odd = N % 2 == 1;
for(int i = 1; i <= N; i++) {
if(odd && i == N / 2 + 1)
continue;
if(A[i] == 0) {
int other = N - i + 1;
int k;
for(int j = 0; ; j++) {
k = left.get(j);
if(A[k] != 0) {
left.remove(j);
j--;
continue;
}
if(k != i && k != other) {
left.remove(j);
break;
}
}
A[i] = k;
A[k] = other;
go(k);
}
}
for(int i = 1; i <= N; i++)
out.print(A[i] + " ");
}
private void go(int k) {
if(A[A[k]] == 0) {
A[A[k]] = N - k + 1;
go(A[k]);
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
if (n == 1) return cout << "1", 0;
if (n % 4 > 1) return cout << "-1", 0;
vector<int> ats, le, ri, add(n + 5);
int mx, l, r;
if (n % 4 == 0) {
ats = {2, 4, 1, 3};
n -= 4;
mx = 4;
l = n / 2 + 1;
r = n / 2 + 4;
} else {
ats = {1};
n -= 1;
mx = 1;
l = n / 2 + 1;
r = n / 2 + 1;
}
while (n > 0) {
mx += 4;
n -= 4;
le.push_back(mx);
le.push_back(2);
ri.push_back(1);
ri.push_back(mx - 1);
add[l] += 2;
add[r + 1] -= 2;
l -= 2;
r += 2;
}
reverse(le.begin(), le.end());
reverse(ats.begin(), ats.end());
reverse(ri.begin(), ri.end());
while (!ats.empty()) le.push_back(ats.back()), ats.pop_back();
while (!ri.empty()) le.push_back(ri.back()), ri.pop_back();
for (int i = 0; i < le.size(); i++) {
add[i + 1] += add[i];
cout << le[i] + add[i + 1] << " ";
}
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T t) {
return t < 0 ? -t : t;
}
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
int main() {
int n, i, p[100005];
scanf("%d", &n);
if (n == 1) {
puts("1");
return 0;
}
if (n % 4 == 2 || n % 4 == 3) {
puts("-1");
return 0;
}
for (i = 1; i <= n / 2; i++)
if (i % 2)
p[i] = i + 1, p[n - i + 1] = n - i;
else
p[i] = n - i + 2, p[n - i + 1] = i - 1;
if (n % 4) p[n / 2 + 1] = n / 2 + 1;
for (i = 1; i <= n; i++) printf("%d ", p[i]);
putchar('\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 f[100500];
int main() {
int n;
while (~scanf("%d", &n)) {
if (n == 1) {
printf("1\n");
continue;
} else if (n <= 3) {
printf("-1\n");
continue;
}
f[1] = 2;
if (n % 2 == 0) {
int x = n - 2;
for (int i = 2; i <= n; ++i) {
if (i % 2 == 0)
f[i] = f[i - 1] + x;
else
f[i] = f[i - 1] - x;
if (i == n / 2)
x = 3;
else if (i == n / 2 + 1)
x = 2;
else {
if (i < n / 2)
x -= 2;
else
x += 2;
}
}
} else {
int x = n - 2;
for (int i = 2; i <= n; ++i) {
if (i == n / 2 + 1 || i == n / 2 + 2) {
f[i] = f[i - 1] - 2;
if (i == n / 2 + 1) continue;
} else {
if (i <= n / 2) {
if (i % 2 == 0)
f[i] = f[i - 1] + x;
else
f[i] = f[i - 1] - x;
} else {
if (i % 2 != 0)
f[i] = f[i - 1] + x;
else
f[i] = f[i - 1] - x;
}
}
if (i <= n / 2) {
x -= 2;
} else {
x += 2;
}
}
}
bool flag = 0;
for (int i = 1; i <= n; ++i) {
if (f[f[i]] != n - i + 1) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("%d", f[1]);
for (int i = 2; i <= n; ++i) printf(" %d", f[i]);
} else
printf("-1");
puts("");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | 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;
}
for(int i = 0; i < N/2; i += 2){
ans[i] = i+1;
ans[i+1] = N-1 - i;
ans[N - 1 - i] = N - 1 - (i + 1);
ans[N - 2 - i] = i;
}
for(int i = 0; i < ans.length; i++) ans[i]++;
print1Int(ans);
}
//-----------------------------------------------------------
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;
int flag[111111];
int main() {
int n;
cin >> n;
if (n % 4 == 3 || n % 4 == 2) {
cout << -1 << endl;
return 0;
}
int m = n / 4;
for (int i = 1, j = 1; i <= m; i++, j = j + 2) {
flag[j] = j + 1;
flag[j + 1] = n - j + 1;
flag[n - j + 1] = n + 1 - flag[j];
flag[n - j] = n + 1 - flag[j + 1];
}
if (n % 4 == 1) {
flag[n / 2 + 1] = n / 2 + 1;
}
for (int i = 1; i <= n; i++) {
if (i == n) {
cout << flag[i] << endl;
} else {
cout << flag[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 res[110000];
int main() {
int n;
while (cin >> n) {
if (n / 2 % 2) {
cout << -1 << endl;
continue;
}
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++) {
cout << res[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 p[1111111];
int main() {
int n;
int cnt = 1;
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3) {
printf("-1\n");
return 0;
}
if (n == 1) {
printf("1\n");
return 0;
}
int tmp = n / 2;
for (int i = 1; i <= tmp; i += 2) {
p[i] = i + 1;
p[n - i + 1] = n - i;
p[i + 1] = n + 1 - i;
p[n - i] = i;
}
if (n % 2 == 1) p[(n + 1) / 2] = (n + 1) / 2;
for (int i = 1, _b = n; i <= _b; 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 | N = int(raw_input())
if N%4 >= 2:
print -1
else:
perm = [0 for i in xrange(N)]
for i in xrange(0,N/2,2):
perm[i] = i+1
perm[i+1] = N-i-1
perm[-i-1] = N-perm[i]-1
perm[-i-2] = i
if N%2 == 1:
perm[N/2] = N/2
for p in perm:
print p+1,
print
| PYTHON |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Locale;
import java.util.Scanner;
public class HappyPermutationSolver {
private int n;
public static void main(String[] args) {
HappyPermutationSolver solver = new HappyPermutationSolver();
solver.readData();
int[] solution = solver.solve();
solver.print(solution);
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private int lcm(int a, int b) {
return a * b / gcd(a, b);
}
private void print(int[] values) {
if (values.length > 0) {
StringBuilder builder = new StringBuilder();
for (int value : values) {
builder.append(value);
builder.append(" ");
}
print(builder);
} else {
print(-1);
}
}
private void print(Object value) {
System.out.println(value);
}
private void print(boolean value) {
System.out.println(value ? "YES" : "NO");
}
private void print(int value) {
System.out.println(value);
}
private void print(long value) {
System.out.println(value);
}
private void print(double value) {
System.out.printf(Locale.ENGLISH, "%.10f", value);
}
private int[] getDigits(int number) {
int[] digits = new int[10];
int index = digits.length - 1;
int digitsCount = 0;
while (number > 0) {
digits[index] = number % 10;
number /= 10;
index--;
digitsCount++;
}
int[] result = new int[digitsCount];
System.arraycopy(digits, digits.length - digitsCount, result, 0, digitsCount);
return result;
}
private int[] readArray(Scanner scanner, int size) {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = scanner.nextInt();
}
return result;
}
private void readData() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
}
private int[] solve() {
int[] result;
int x = n & 3;
if (x == 0 || x == 1) {
result = new int[n];
if ((n & 1) == 1) {
result[n >> 1] = (n >> 1) + 1;
}
for (int i = 1; i < (n >> 1); i += 2) {
result[i - 1] = i + 1;
result[i] = n - i + 1;
result[n - i] = n - i;
result[n - i - 1] = i;
}
}
else {
result = new int[0];
}
return result;
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << "-1";
return 0;
}
int p[n + 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++) {
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() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
if (n % 4 >= 2) {
puts("-1");
return 0;
}
vector<long long> p(n);
long long k = 1;
for (long long i = 0; i < n / 2; i += 2) {
p[n - i - 2] = k;
p[i] = k + 1;
p[n - i - 1] = n - k;
p[i + 1] = n - k + 1;
k += 2;
}
if (n % 4 == 1) p[n / 2] = (n + 1) / 2;
for (long long i = (0); i < (long long)(p.size()); i++) cout << p[i] << " ";
cout << "\n";
;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Scanner;
public class Prob286A {
public static void main(String[] Args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
if (x % 4 < 2) {
int[] arr = new int[x];
int min = 1;
int max = x;
if (x % 2 == 1)
arr[x / 2] = x / 2 + 1;
int count = x / 4;
while (count-- > 0) {
arr[min - 1] = min + 1;
arr[min] = max;
arr[max - 2] = min;
arr[max - 1] = max - 1;
min += 2;
max -= 2;
}
for (int i = 0; i < x; i++)
System.out.print(arr[i] + " " );
} else
System.out.println(-1);
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.PrintWriter;
import java.util.Scanner;
public class A {
int N;
int ans[];
private A() {
}
private void read() {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
}
private void init() {
}
private void solve() {
ans = new int[N];
for (int i=0; i<N/2; i+=2) {
ans[i] = i+2;
ans[i+1] = N-i;
ans[N-i-1] = N-i-1;
ans[N-i-2] = i+1;
}
if (1 == N % 4) {
ans[N/2] = N/2 + 1;
}
}
private void save() {
PrintWriter pw = new PrintWriter(System.out);
if (2 == N % 4 || 3 == N % 4) {
pw.print(-1);
} else {
for (int i=0; i<N; i++) {
if (i > 0) {
pw.print(" ");
}
pw.print(ans[i]);
}
}
pw.close();
}
private void run() {
read();
init();
solve();
save();
}
public static void main(String args[]) {
new A().run();
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// get n
int n = in.nextInt();
// check parity of (n / 2)
if ((n / 2) % 2 == 1)
{
System.out.println(-1);
return;
}
int[] ary = new int[n + 5];
// check parity of n
if (n % 2 == 1)
{
int m = n + 1;
m /= 2;
ary[m] = m;
}
// for
for (int i = 1; i <= n / 2; i += 2)
{
int a = i;
int b = i + 1;
int A = n + 1 - a;
int B = n + 1 - b;
ary[a] = B;
ary[B] = A;
ary[A] = b;
ary[b] = a;
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++)
{
sb.append(ary[i]);
if (i + 1 <= n)
sb.append(" ");
}
System.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 | import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
new Main().run();
}
void run() {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
if (n % 4 > 1) {
out.println(-1);
}
else {
for (int i = 0; i < n / 2; i += 2)
out.print((i + 2) + " " + (n - i) + " ");
if (n % 4 == 1)
out.print((n / 2 + 1) + " ");
for (int i = (n + 1) / 2; i < n; i += 2)
out.print((n - i - 1) + " " + (i + 1) + " ");
out.println();
}
out.close();
}
}
class InputReader {
BufferedReader buff;
StringTokenizer tokenizer;
InputReader(InputStream stream) {
buff = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
try {
tokenizer = new StringTokenizer(buff.readLine());
}
catch (Exception e) {
return false;
}
return true;
}
String next() {
if (!hasNext())
throw new RuntimeException();
return tokenizer.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int ax[100010];
int main(void) {
int n;
scanf("%d", &n);
if (!(n % 4 == 0 || n % 4 == 1)) {
printf("-1\n");
return 0;
}
for (int i = 1; i <= n / 2; i += 2) {
ax[i] = i + 1;
ax[i + 1] = n - i + 1;
ax[n - i] = i;
ax[n - i + 1] = n - i;
}
if (n % 2 == 1) {
ax[n / 2 + 1] = n / 2 + 1;
}
for (int i = 1; i <= n; i++) {
printf("%d ", ax[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;
vector<long long> v[301], vv, v1;
long long a, b, c[1234567], c1[1234][1234], e, i, j, n, x, y, l, r, k;
string s, s1;
long long used[301];
long long ans;
bool ok[123];
int main() {
cin >> n;
if (n / 2 % 2 == 1) {
cout << -1;
exit(0);
}
a = 2;
for (int i = 1; i <= n / 2; i += 2) {
c[i] = i + 1;
c[n - i + 1] = (n + 1) - (i + 1);
c[n - i] = i;
c[i + 1] = n + 1 - i;
}
if (n % 2 == 1) c[n / 2 + 1] = n / 2 + 1;
for (long long i = 1; i <= n; i++) cout << c[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 = 1e5 + 5;
int p[maxn];
int n;
int main() {
while (~scanf("%d", &n)) {
if (n == 1) {
printf("1\n");
continue;
}
if (n <= 3) {
printf("-1\n");
continue;
}
memset(p, 0, sizeof(p));
;
if (n & 1) {
if ((n - 1) % 4 != 0) {
printf("-1\n");
continue;
}
p[(n + 1) / 2] = (n + 1) / 2;
} else {
if (n % 4 != 0) {
printf("-1\n");
continue;
}
}
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++) printf("%d ", p[i]);
printf("\n");
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 4 && (n - 1) % 4) {
cout << -1 << endl;
return 0;
}
int ara[n + 1], i;
for (i = 1; i <= n / 2; i += 2) {
ara[i] = i + 1;
}
for (i = 2; i <= n / 2; i += 2) {
ara[i] = n - i + 2;
}
for (i = n; i > (n + 1) / 2; i -= 2) {
ara[i] = i - 1;
}
for (i = n - 1; i > (n + 1) / 2; i -= 2) {
ara[i] = n - i;
}
if (n % 2) ara[(n + 1) / 2] = (n + 1) / 2;
for (i = 1; i <= n; i++) cout << ara[i] << " ";
cout << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.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;
}
if (count == 1) {
out.printLine(1);
return;
}
if (count % 4 == 0) {
int left = 2;
int right = count;
while (left <= right) {
out.print(left + " " + right + " ");
left += 2;
right -= 2;
}
left = (count - 2) / 2;
right = (count + 2) / 2;
while (left > 0) {
out.print(left + " " + right + " ");
left -= 2;
right += 2;
}
} else {
int left = 2;
int right = count;
while (left <= right) {
out.print(left + " " + right + " ");
left += 2;
right -= 2;
}
out.print((count + 1) / 2 + " ");
left = (count - 3) / 2;
right = (count + 3) / 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>
const bool debug = false;
using namespace std;
long long powmod(long long a, long long b, long long MOD) {
long long res = 1;
a %= MOD;
for (; b; b >>= 1) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
}
return res;
}
void buginfo(const char* f, ...) {
if (!debug) return;
va_list al;
va_start(al, f);
vprintf(f, al);
va_end(al);
}
const int maxn = 1e5 + 10;
int n, a[maxn];
int main() {
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3) {
printf("-1\n");
return 0;
}
int p = 1, q = n;
while (p < q) {
a[p] = p + 1;
a[p + 1] = q;
a[q] = q - 1;
a[q - 1] = p;
p += 2, q -= 2;
}
if (p == q) a[p] = p;
for (int i = 1; i < n + 1; ++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;
template <class T>
inline void checkmin(T& a, const T& b) {
if (a > b) a = b;
};
template <class T>
inline void checkmax(T& a, const T& b) {
if (a < b) a = b;
};
int main() {
for (int n; cin >> n;) {
vector<int> ans(n + 1);
if (n % 4 == 2 || n % 4 == 3) {
puts("-1");
continue;
}
int left = 1, right = n;
while (right - left + 1 >= 4) {
ans[left] = right - 1;
ans[left + 1] = left;
ans[right - 1] = right;
ans[right] = left + 1;
left += 2;
right -= 2;
}
if (left == right) {
ans[left] = right;
}
for (int i = 1; i <= n; ++i) cout << ans[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.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
int n;
int p [];
boolean used[];
int count = 0;
void doIt (int k) {
if (count > 0) return;
if (k == n + 1) {
boolean good = true;
for (int i = 1; i <= n; i++) {
if (p[p[i]] != n - i + 1) {
good = false;
}
}
if (good) {
count++;
for (int i = 1; i <= n; i++) {
out.print(p[i]+" ");
}
out.println();
}
} else {
for (int i = 1; i <= n; i++) {
if (!used[i]) {
used[i] = true;
p[k] = i;
doIt (k + 1);
used[i] = false;
}
}
}
}
public void solve() throws Throwable {
n = sc.nextInt();
if (n == 1) {
out.println(1);
return;
} else if (n <= 3) {
out.println(-1);
return;
}
p = new int [n + 1];
used = new boolean [n + 1];
if (n % 4 == 0 || (n - 1) % 4 == 0) {
int l = 1;
int r = n;
while (l < r) {
p[l] = l + 1;
p[l + 1] = n - l + 1;
p[r] = n - l;
p[r - 1] = l;
l += 2;
r -= 2;
}
if (l == r) {
p[l] = n / 2 + 1;
}
} else {
out.println(-1);
return;
}
for (int i = 1; i <= n; i++) {
out.print((p[i])+" ");
}
}
BufferedReader in;
PrintWriter out;
FastScanner sc;
static Throwable uncaught;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable t) {
Solution.uncaught = t;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread t = new Thread(null, new Solution(), "", (1 << 26));
t.start();
t.join();
if (uncaught != null) {
throw uncaught;
}
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer strTok;
public FastScanner(BufferedReader reader) {
this.reader = reader;
}
public String nextToken() throws IOException {
while (strTok == null || !strTok.hasMoreTokens()) {
strTok = new StringTokenizer(reader.readLine().trim());
}
return strTok.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());
}
} | 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;
namespace Flandre_Scarlet {
int n;
void Input() { cin >> n; }
int a[155555];
void Soviet() {
memset(a, 0, sizeof(a));
if (n % 4 == 0) {
for (int i = 1; i <= n; ++i) {
if (!a[i]) {
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]);
}
putchar('\n');
} else if (n % 4 == 1) {
a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n / 2; ++i) {
if (!a[i]) {
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]);
}
putchar('\n');
} else {
puts("-1");
}
}
void IsMyWife() {
Input();
Soviet();
}
} // namespace Flandre_Scarlet
int main() {
Flandre_Scarlet::IsMyWife();
getchar();
getchar();
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 f[100010];
int N;
bool dfs(int l, int r, int k) {
if (r - l + 1 >= 4) {
f[l] = 2 + k * 2;
f[l + 1] = N - k * 2;
f[r] = N - 1 - k * 2;
f[r - 1] = 1 + k * 2;
return dfs(l + 2, r - 2, k + 1);
} else if (r - l + 1 == 1) {
f[r] = r;
return true;
} else if (r < l)
return true;
return false;
}
int main() {
scanf("%d", &N);
if (!dfs(1, N, 0)) {
printf("-1\n");
return 0;
}
for (int(i) = 1; (i) < (N + 1); (i)++) {
if (i != 1) putchar(' ');
printf("%d", f[i]);
}
putchar('\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;
cin >> n;
if (n == 1) {
cout << 1;
return 0;
}
if (n % 4 == 3 || n % 4 == 2) {
cout << -1 << endl;
return 0;
}
int a[n + 1];
int i = 1, j = n, l = 1, r = n;
while (i < j) {
a[i] = l + 1;
a[j] = r - 1;
a[i + 1] = r;
a[j - 1] = l;
l += 2;
r -= 2;
i += 2;
j -= 2;
}
if (i == j) a[i] = l;
for (int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100002];
int main() {
int n;
cin >> n;
if (n % 4 > 1) return cout << -1, 0;
if (n % 4 == 1) a[(n - 1) / 2 + 1] = (n - 1) / 2 + 1;
int e = n / 2;
for (int i = 1; i < e; i += 2) {
a[i] = i + 1;
a[i + 1] = n - i + 1;
a[n - i + 1] = n - i;
a[n - i] = i;
}
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100005], n, p, x, k;
bool test() {
for (int i = 1; i <= n; i++)
if (a[a[i]] != n - i + 1) return 0;
return 1;
}
int main() {
scanf("%d", &n);
k = ceil(n / 2.0);
int x = 1, p = 2;
while (x <= k) {
a[x] = p;
p += 2;
x += 2;
}
x = n;
p = n - 1;
while (x > k) {
a[x] = p;
p -= 2;
x -= 2;
}
if (n % 2) a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n; i++)
if (a[i]) a[a[i]] = n - i + 1;
if (test()) {
printf("%d", a[1]);
for (int i = 2; i <= n; i++) printf(" %d", a[i]);
printf("\n");
} else
printf("-1\n");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
if (n % 4 > 1) {
cout << "-1\n";
return 0;
}
for (int i = 1; i <= n / 2; i++) {
if (i % 2)
cout << i + 1 << ' ';
else
cout << (n - i + 2) << ' ';
}
int m = n / 2;
if (n % 4 == 1) {
cout << m + 1;
if (m + 1 < n) cout << ' ';
m++;
}
for (int i = m + 1; i < n; i++) {
if ((i - m) % 2)
cout << n - i << ' ';
else
cout << i - 1 << ' ';
}
if (m + 1 < n) cout << (n - 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 |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class A {
private static final int mod = (int)1e9+7;
boolean ok (final int[] xs) {
for(int i = 0; i < xs.length; i++) {
if(xs[xs[i]] != xs.length - 1 - i) {
return false;
}
}
return true;
}
int[] xs;
void check(int a, int b) {
xs[a] = b;
for(int i = 0; i < xs.length; i++) {
final int c = xs.length - 1 - xs[a];
// System.err.println("2: " + Arrays.toString(xs) + " " + a + " " + b + " " + c);
if(xs[c] == a) break;
xs[c] = a;
a = c;
}
// System.err.println(Arrays.toString(xs));
}
public void run() throws IOException {
final int n = IOFast.nextInt();
xs = new int[n];
Arrays.fill(xs, -1);
if(n == 1) { IOFast.out.println(1); return; }
for(int i = n - 2, j = 0; i >= 0; i -= 2, j += 2) if(xs[i] == -1) {
check(i, j);
}
if(n % 2 == 1) xs[n/2] = n/2;
if(ok(xs)) {
for(int j = 0; j < xs.length; j++) {
IOFast.out.print(++xs[j] + (j == xs.length-1?"\n":" "));
}
return;
}
IOFast.out.println(-1);
/*
LOOP: for(int i = 1; i <= 10; i++) {
IOFast.out.println("check: " + i);
final int[] xs = new int[i];
for(int j = 0; j < xs.length; j++) {
xs[j] = j;
}
do {
if(ok(xs)){
for(int j = 0; j < xs.length; j++) {
IOFast.out.print(xs[j] + " ");
}
IOFast.out.println();
continue LOOP;
}
} while(Permutation.nextPermutation(xs));
IOFast.out.println("NOT FOUND");
}
*/
}
static
public class Permutation {
private static final void swap(int[] xs, int i, int j) {
final int t = xs[j];
xs[j] = xs[i];
xs[i] = t;
}
private static final boolean nextPermutation(final int[] xs) {
for(int i = xs.length - 1; i > 0; i--) {
if(xs[i - 1] > xs[i]) {
continue;
}
for(int j = i, k = xs.length - 1; j < k; j++, k--) {
swap(xs, j, k);
}
for(int j = i; j < xs.length; j++) if(xs[j] > xs[i - 1]) {
swap(xs, j, i - 1);
break;
}
return true;
}
return false;
}
}
public static void main(String[] args) throws IOException {
// IOFast.setFileIO("input.txt", "output.txt");
new A().run();
IOFast.out.flush();
}
static public class IOFast {
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter out = new PrintWriter(System.out);
static void setFileIO(String ins, String outs) throws IOException {
try {
in = new BufferedReader(new FileReader(ins));
out = new PrintWriter(new FileWriter(outs));
}
catch(IOException e) {
System.err.println(e);
throw e;
}
}
// private static final int BUFFER_SIZE = 50 * 200000;
private static int pos, readLen;
private static final char[] buffer = new char[1024 * 8];
private static final StringBuilder buf = new StringBuilder();
private static boolean[] isDigit = new boolean[256];
private static boolean[] isSpace = new boolean[256];
static {
for(int i = 0; i < 10; i++) {
isDigit['0' + i] = true;
}
isDigit['-'] = true;
isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
}
static boolean endInput;
private static int read() throws IOException {
if(readLen == -1) {
return -1;
}
if(pos >= readLen) {
readLen = in.read(buffer);
pos = 0;
if(readLen <= 0) {
return -1;
}
}
return buffer[pos++];
}
private static int nextInt() throws IOException {
boolean plus = false;
int ret = 0;
while(true) {
final int c = read();
if(c == -1) {
endInput = true;
return Integer.MIN_VALUE;
}
if(isDigit[c]) {
if(c != '-') {
plus = true;
ret = c - '0';
}
break;
}
}
while(true) {
final int c = read();
if(c == -1 || !isDigit[c]) {
break;
}
ret = ret * 10 + c - '0';
}
return plus ? ret : -ret;
}
private static long nextLong() throws IOException {
boolean plus = false;
long ret = 0;
while(true) {
final int c = read();
if(c == -1) {
endInput = true;
return Integer.MIN_VALUE;
}
if(isDigit[c]) {
if(c != '-') {
plus = true;
ret = c - '0';
}
break;
}
}
while(true) {
final int c = read();
if(c == -1 || !isDigit[c]) {
break;
}
ret = ret * 10 + c - '0';
}
return plus ? ret : -ret;
}
private static char nextChar() throws IOException {
while(true) {
final int c = read();
if(c == -1) {
endInput = true;
return '\0';
}
if(!isSpace[c]) {
return (char)c;
}
}
}
private static String next() throws IOException {
buf.setLength(0);
while(true) {
final int c = read();
if(c == -1) {
endInput = true;
return "-1";
}
if(!isSpace[c]) {
buf.append((char)c);
break;
}
}
while(true) {
final int c = read();
if(c == -1 || isSpace[c]) {
break;
}
buf.append((char)c);
}
return buf.toString();
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:666000000")
using namespace std;
const int inf = (1 << 30) - 1;
const long double eps = 1e-9;
const long double pi = fabs(atan2(0.0, -1.0));
void ML() {
int *ass;
for (;;) {
ass = new int[2500000];
for (int i = 0; i < 2500000; i++) ass[i] = rand();
}
}
int n;
int a[100500];
void LoAd() { cin >> n; }
void SoLvE() {
if (1 == n) {
puts("1");
return;
}
if (2 == n || 3 == n) {
puts("-1");
return;
}
int lt = 0;
int rt = n - 1;
while (lt <= rt) {
if (0 == rt - lt) {
a[lt] = lt;
break;
} else if (1 == rt - lt || 2 == rt - lt) {
puts("-1");
return;
} else {
a[rt - 1] = lt;
a[rt] = rt - 1;
a[lt] = lt + 1;
a[lt + 1] = rt;
lt += 2;
rt -= 2;
}
}
for (int i = 0; i < n; i++) printf("%d ", a[i] + 1);
puts("");
}
int main() {
srand((int)time(NULL));
{
LoAd();
SoLvE();
}
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.math.*;
import java.util.*;
public class Solution {
public void run() {
try {
int n = reader.nextInt();
if (n % 4 >= 2) {
writer.println(-1);
} else {
int[] p = new int[n];
for (int i = 0; i < n; ++ i) {
p[i] = i;
}
for (int i = 0; i + 1 < n - i - 2; i += 2) {
p[i] = n - i - 2;
p[n - i - 2] = p[n - i - 1];
p[n - i - 1] = p[i + 1];
p[i + 1] = i;
}
for (int i = 0; i < n; ++ i) {
writer.print(String.format("%d%c", p[i] + 1, i == n - 1 ? '\n' : ' '));
}
}
} catch (IOException ex) {
}
writer.close();
}
InputReader reader;
PrintWriter writer;
Solution() {
reader = new InputReader();
writer = new PrintWriter(System.out);
}
public static void main(String[] args) {
new Solution().run();
}
}
class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
}
String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
Integer nextInt() throws IOException {
return Integer.parseInt(next());
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int seq[111111];
int main(void) {
int n = 0;
scanf("%d", &n);
if (n % 4 > 1)
puts("-1");
else {
for (int i = 0; i < n / 4; i++) {
int t = i * 2 + 1;
int l = n + 1 - t;
seq[t] = t + 1;
seq[t + 1] = l;
seq[l] = l - 1;
seq[l - 1] = t;
}
for (int i = 1; i <= n; i++)
if (seq[i] == 0) seq[i] = i;
for (int i = 1; i <= n; i++) printf("%d ", seq[i]);
puts("");
bool okay = true;
for (int i = 1; i <= n; i++)
if (seq[seq[i]] != n - i + 1) okay = false;
}
while (getchar() != EOF)
;
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 a[100010];
int main() {
cin >> n;
if (n % 4 > 1) {
cout << -1;
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 % 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>
using namespace std;
int main() {
int arr[100009] = {0};
bool vis[100009] = {0};
set<int> st;
bool f = 1;
int n = 0;
cin >> n;
if (n == 1) {
cout << n;
return 0;
}
if (n <= 3) {
cout << -1;
return 0;
}
for (int i = 1; i <= n; i++) st.insert(i);
while (st.size()) {
int i = *st.begin();
st.erase(i);
int v = vis[i + 1] ? i : i + 1;
while (!vis[v]) {
vis[v] = 1;
int v2 = n - i + 1;
arr[i] = v;
i = v;
v = v2;
st.erase(i);
}
}
for (int i = 1; i <= n; i++) f &= vis[i];
if (!f)
cout << -1;
else
for (int i = 1; i <= n; i++) cout << arr[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
FILE *f, *g;
int v[100100];
int i, j, q;
int n;
int main() {
scanf("%d", &n);
if (n % 4 == 2 || n % 4 == 3) {
printf("-1");
return 0;
}
for (i = 1, j = n; i <= (n / 4) * 2; i += 2, j -= 2) {
v[i] = i + 1;
v[i + 1] = j;
v[j - 1] = i;
v[j] = j - 1;
}
if (n % 4 == 1) {
v[i] = i;
}
for (i = 1; i <= n; i++) printf("%d ", v[i]);
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | //package codeforces;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class A implements Closeable {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
A() throws IOException {
// reader = new BufferedReader(new FileReader("input.txt"));
// writer = new PrintWriter(new FileWriter("output.txt"));
}
StringTokenizer stringTokenizer;
String next() throws IOException {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
private int MOD = 1000 * 1000 * 1000 + 7;
int sum(int a, int b) {
a += b;
return a >= MOD ? a - MOD : a;
}
int product(int a, int b) {
return (int) (1l * a * b % MOD);
}
int pow(int x, int k) {
int result = 1;
while (k > 0) {
if (k % 2 == 1) {
result = product(result, x);
}
x = product(x, x);
k /= 2;
}
return result;
}
int inv(int x) {
return pow(x, MOD - 2);
}
void solve() throws IOException {
int n = nextInt();
if(n % 4 != 0 && n % 4 != 1) {
writer.println(-1);
return;
}
int[] p = new int[n];
for(int i = 1; i < n / 2; i += 2) {
int x = i, y = n - x + 1, u = i + 1, v = n - u + 1;
//x -> u -> y -> v
p[x - 1] = u;
p[u - 1] = y;
p[y - 1] = v;
p[v - 1] = x;
}
if(n % 2 == 1) {
p[n / 2] = n / 2 + 1;
}
for (int x : p) {
writer.print(x + " ");
}
}
public static void main(String[] args) throws IOException {
try (A a = new A()) {
a.solve();
}
}
@Override
public void close() throws IOException {
reader.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 n;
int a[100005];
int used[10005];
bool check(int i) {
if (a[i] == -1) return true;
if (a[a[i] - 1] == -1) {
a[a[i] - 1] = n - i;
used[n - i] = true;
} else
return a[a[i] - 1] == n - i;
return check(a[i] - 1);
}
bool checkans() {
for (int i = 0; i < n; ++i) {
if (a[i] != -1) {
if (!check(i)) return false;
} else {
for (int j = 1; j <= n; ++j) {
if (used[j]) continue;
used[j] = true;
a[i] = j;
if (check(i)) break;
a[i] = -1;
used[j] = false;
}
}
}
return true;
}
int main() {
cin >> n;
if (n == 1 || ((n - (n % 2)) % 4 == 0)) {
for (int i = 2; i <= n / 2; i += 2) {
printf("%d %d ", i, n - i + 2);
}
if (n % 2) printf("%d ", n / 2 + 1);
for (int i = n / 2 - 1; i >= 1; i -= 2) {
printf("%d %d ", i, n - i);
}
} else
printf("-1");
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
set<int> st;
int ans[100007];
int main() {
int n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << "-1" << endl;
return 0;
}
for (int i = 1; i <= n; i++) st.insert(i);
if (n % 4 == 1) {
int idx = (n + 1) / 2;
st.erase(idx);
ans[idx] = idx;
}
while (!st.empty()) {
int num1 = *(st.begin());
set<int>::iterator it;
it = ++st.begin();
int num2 = *it;
int num3 = n - num1 + 1;
int num4 = n - num2 + 1;
ans[num1] = num2;
ans[num2] = num3;
ans[num3] = num4;
ans[num4] = num1;
st.erase(num1);
st.erase(num2);
st.erase(num3);
st.erase(num4);
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const double PI = 3.1415926535;
int n;
int main() {
cin >> n;
if (n % 4 > 1)
cout << -1;
else {
int x = n / 4;
for (int i = 0; i < x; ++i) printf("%d %d ", 2 + i * 2, n - i * 2);
if (n % 4 == 1) printf("%d ", n / 2 + 1);
for (int i = x - 1; i >= 0; --i) printf("%d %d ", 1 + i * 2, n - 1 - i * 2);
}
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;
int p[100005], parent[100005];
scanf("%d", &n);
if (n % 4 > 1) {
printf("-1\n");
return 0;
}
memset(parent, 0, sizeof(parent));
for (int i = 1; i <= n / 2; i += 2) {
p[i] = 1 + i;
p[i + 1] = n - i + 1;
p[n - i + 1] = n - i;
p[n - i] = i;
}
if (n % 4 == 1) p[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i < n; ++i) {
printf("%d ", p[i]);
}
printf("%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() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
if (n % 4 > 1) {
cout << "-1\n";
return 0;
}
int p[n];
if (n % 4 == 1) {
p[n / 2] = (n + 1) / 2;
}
for (int i = 0, j = n - 2; i < n / 2; i += 2, j -= 2) {
if (i == 0) {
p[0] = 2;
p[1] = n;
} else {
p[i] = p[i - 2] + 2;
p[i + 1] = p[i - 1] - 2;
}
p[j] = p[i] - 1;
p[j + 1] = p[i + 1] - 1;
}
for (int i : p) cout << i << " ";
cout << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.*;
import java.util.*;
public class Contest176 {
public static void main(String[] args) throws IOException {
new Contest176().run();
}
public void solve() throws IOException {
int n = nextInt();
int rem = n % 4;
if(rem == 2 || rem == 3) {
out.println(-1);
return;
}
int res[] = new int[n + 1];
int map1[] = new int[5];
final int p[] = {0, 3, 1, 4, 2};
for(int i = 1, stop = n / 2 + 1; i < stop; i += 2) {
map1[1] = i;
map1[2] = i + 1;
map1[3] = n - i;
map1[4] = n - i + 1;
for(int j = 1; j <= 4; ++j) {
int num = map1[j];
int prepos = p[j];
int finpos = map1[prepos];
res[finpos] = num;
}
}
if(n % 2 == 1)
res[n / 2 + 1] = n / 2 + 1;
for(int i = 1; i <= n; ++i) {
out.print(res[i]);
out.print(' ');
}
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
String nextToken() throws IOException {
while( in == null || !in.hasMoreTokens() ) {
in = new StringTokenizer(br.readLine());
}
return in.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());
}
void run() throws IOException {
boolean oj = System.getProperty( "ONLINE_JUDGE" ) != null;
Reader reader = new InputStreamReader( oj ? System.in : new FileInputStream("input.txt"));
Writer writer = new OutputStreamWriter( oj ? System.out : new FileOutputStream("output.txt"));
br = new BufferedReader(reader);
out = new PrintWriter(writer);
solve();
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 | #!/usr/bin/python
import sys
def read_obj(cls):
return cls(sys.stdin.readline().strip())
def read_obj_list(cls):
return map(cls, sys.stdin.readline().strip().split())
def solve():
n = read_obj(int)
if n % 4 in [2, 3]:
print -1
return
res = [0 for i in xrange(n + 1)]
for i in xrange(1, n / 2 + 1, 2):
res[i] = i + 1
res[i + 1] = n - i + 1
res[n - i + 1] = n - i
res[n - i] = i
if n % 4:
res[(n + 1) / 2] = n - (n + 1) / 2 + 1
for i in res[1:]:
print i,
print
if __name__ == "__main__":
solve()
| 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 INF = 1 << 30;
const double EPS = 1e-7;
const int MAX = 100005;
int p[MAX];
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
if (n % 4 > 1)
cout << -1 << endl;
else {
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++) 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 main() {
int n;
scanf("%d", &n);
if (n % 4 == 0 || n % 4 == 1) {
if (n % 4 == 0) {
for (int i = 1; i <= n / 2; i += 2) {
printf("%d ", i + 1);
printf("%d ", (n - i + 1));
}
for (int i = n / 2 + 1; i <= n; i += 2) {
printf("%d ", (n - (i + 1) + 1));
printf("%d ", i);
}
} else {
for (int i = 1; i <= n / 2; i += 2) {
printf("%d ", i + 1);
printf("%d ", (n - i + 1));
}
printf("%d ", n / 2 + 1);
for (int i = n / 2 + 2; i <= n; i += 2) {
printf("%d ", (n - (i + 1) + 1));
printf("%d ", i);
}
}
} else
puts("-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.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;
import java.lang.management.*;
import static java.lang.Math.*;
@SuppressWarnings("unchecked")
public class P0286A {
public void run() throws Exception {
int n = nextInt(), n_2 = n / 2;
if (n == 1) {
println(1);
return;
} else if (((n & 0b11) != 0) && (((n - 1) & 0b11) != 0)) {
println(-1);
return;
}
StringBuilder ans = new StringBuilder();
for (int i = 2, j = n; i <= n_2; ans.append(i).append(' ').append(j).append(' '), i += 2, j -= 2);
if ((n & 1) == 0) {
for (int i = n / 2 - 1, j = i + 2; j < n; ans.append(i).append(' ').append(j).append(' '), i -= 2, j += 2);
} else {
ans.append(n / 2 + 1).append(' ');
for (int i = n / 2 - 1, j = (n + 1) / 2 + 1; j < n; ans.append(i).append(' ').append(j).append(' '), i -= 2, j += 2);
}
println(ans);
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new P0286A().run();
br.close();
pw.close();
System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
long gct = 0, gcc = 0;
for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
gct += garbageCollectorMXBean.getCollectionTime();
gcc += garbageCollectorMXBean.getCollectionCount();
}
System.err.println("[GC time : " + gct + " ms, count = " + gcc + "]");
System.err.println("[JIT time : " + ManagementFactory.getCompilationMXBean().getTotalCompilationTime() + " ms]");
}
static long startTime = System.currentTimeMillis();
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) { return null; }
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) { print("" + b); }
void print(int i) { print("" + i); }
void print(long l) { print("" + l); }
void print(double d) { print("" + d); }
void print(char c) { print("" + c); }
void print(Object o) {
if (o instanceof int[]) { print(Arrays.toString((int [])o));
} else if (o instanceof long[]) { print(Arrays.toString((long [])o));
} else if (o instanceof char[]) { print(Arrays.toString((char [])o));
} else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
} else if (o instanceof short[]) { print(Arrays.toString((short [])o));
} else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
} else if (o instanceof float[]) { print(Arrays.toString((float [])o));
} else if (o instanceof double[]) { print(Arrays.toString((double [])o));
} else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
} else { print("" + o); }
}
void printsp(int [] a) { for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++); }
void printsp(long [] a) { for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++); }
void print(String s) { pw.print(s); }
void println() { println(""); }
void println(byte b) { println("" + b); }
void println(int i) { println("" + i); }
void println(long l) { println("" + l); }
void println(double d) { println("" + d); }
void println(char c) { println("" + c); }
void println(Object o) { print(o); println(); }
void println(String s) { pw.println(s); }
int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
long nextLong() throws IOException { return Long.parseLong(nextToken()); }
double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
char nextChar() throws IOException { return (char) (br.read()); }
String next() throws IOException { return nextToken(); }
String nextLine() throws IOException { return br.readLine(); }
int [] readInt(int size) throws IOException {
int [] array = new int [size];
for (int i = 0; i < size; i++) { array[i] = nextInt(); }
return array;
}
long [] readLong(int size) throws IOException {
long [] array = new long [size];
for (int i = 0; i < size; i++) { array[i] = nextLong(); }
return array;
}
double [] readDouble(int size) throws IOException {
double [] array = new double [size];
for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
return array;
}
String [] readLines(int size) throws IOException {
String [] array = new String [size];
for (int i = 0; i < size; i++) { array[i] = nextLine(); }
return array;
}
int gcd(int a, int b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
long gcd(long a, long b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Long.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
boolean isPrime(int v) {
if (v <= 1) { return false; }
if (v <= 3) { return true; }
if (((v & 1) == 0) || ((v % 3) == 0)) { return false; }
for (int m = 5, n = (int)sqrt(v); m <= n; m += 6) { if (((v % m) == 0) || ((v % (m + 2)) == 0)) { return false; } }
return true;
}
int [] rshuffle(int [] a) { // RANDOM shuffle
Random r = new Random();
for (int i = a.length - 1, j, t; i >= 0; j = r.nextInt(a.length), t = a[i], a[i] = a[j], a[j] = t, i--);
return a;
}
int [] qshuffle(int [] a) { // QUICK shuffle
int m = new Random().nextInt(10) + 2;
for (int i = 0, n = a.length, j = m % n, t; i < n; t = a[i], a[i] = a[j], a[j] = t, i++, j = (i * m) % n);
return a;
}
long [] shuffle(long [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
long t = a[i]; a[i] = a[j]; a[j] = t;
}
return a;
}
void shuffle(Object [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
Object t = a[i]; a[i] = a[j]; a[j] = t;
}
}
int [] sort(int [] a) {
final int SHIFT = 16, MASK = (1 << SHIFT) - 1, SIZE = (1 << SHIFT) + 1;
int n = a.length, ta [] = new int [n], ai [] = new int [SIZE];
for (int i = 0; i < n; ai[(a[i] & MASK) + 1]++, i++);
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++);
for (int i = 0; i < n; ta[ai[a[i] & MASK]++] = a[i], i++);
int [] t = a; a = ta; ta = t;
ai = new int [SIZE];
for (int i = 0; i < n; ai[(a[i] >> SHIFT) + 1]++, i++);
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++);
for (int i = 0; i < n; ta[ai[a[i] >> SHIFT]++] = a[i], i++);
return ta;
}
void flush() {
pw.flush();
}
void pause() {
flush(); System.console().readLine();
}
} | JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
void run() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
deque<int> solve(int n, int add) {
if (n == 0) return {};
if (n == 1) return {1 + add};
deque<int> q = solve(n - 4, add + 2);
q.push_front(n + add);
q.push_front(2 + add);
q.push_back(1 + add);
q.push_back(n - 1 + add);
return q;
}
int main() {
run();
int n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) return cout << "-1", 0;
deque<int> q = solve(n, 0);
for (auto &it : q) cout << it << ' ';
}
| 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 a[100005];
int main() {
long n;
cin >> n;
if (n == 1)
cout << "1" << endl;
else if (n % 4 == 0) {
for (int i = 1; i <= n / 2; i += 2) a[n - i] = i;
for (int i = 2; i <= n / 2; i += 2) a[i - 1] = i;
for (int i = n; i > n / 2; i -= 2) a[n + 2 - i] = i;
for (int i = n - 1; i > n / 2; i -= 2) a[i + 1] = i;
for (int i = 1; i <= n - 1; i++) cout << a[i] << " ";
cout << a[n] << endl;
} else if ((n - 1) % 4 == 0) {
a[n / 2 + 1] = n / 2 + 1;
for (int i = 1; i <= n / 2; i += 2) a[n - i] = i;
for (int i = 2; i <= n / 2; i += 2) a[i - 1] = i;
for (int i = n; i > n / 2 + 1; i -= 2) a[n + 2 - i] = i;
for (int i = n - 1; i > n / 2 + 1; i -= 2) a[i + 1] = i;
for (int i = 1; i <= n - 1; i++) cout << a[i] << " ";
cout << a[n] << endl;
} else
cout << "-1" << endl;
return 0;
}
| CPP |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class A {
public static void main(String[] args) {
FS r = new FS();
int N = r.nextInt();
int[] ans = new int[N];
if(N%4==2||N%4==3) {
System.out.println(-1);
return;
}
for(int i=1; i<=N/2; i++) {
if(i%2==0) ans[i-2] = i;
else ans[N-i-1] = i;
}
if(N%4==1) ans[N/2] = (N+1)/2;
for(int i=N; i>(N+1)/2; i--) {
if((N-i)%2==0) ans[N-i+1] = i;
else ans[i] = i;
}
String[] print = new String[N];
for(int i=0; i<N; i++) print[i] = String.valueOf(ans[i]);
System.out.println(String.join(" ", print));
}
// Read Class
static class FS {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { return hasNextByte() ? buffer[ptr++] : -1;}
private boolean isPrintableChar(int c) {return 33 <= c && c <= 126;}
private void skipUnprintable() {while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
public boolean hasNext() { skipUnprintable(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if(b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
}
}
| 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 | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package codeforces;
import java.util.*;
import java.io.*;
/**
*
* @author Arysson
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if (n%4==2 || n%4==3) System.out.print(-1);
else if (n%2==1) for (int i=1; i<=n; i++) {
if (i==n/2+1) System.out.printf("%d ", n/2+1);
else if (i<=n/2) System.out.printf("%d ", i%2==1?i+1:n-i+2);
else System.out.printf("%d ", i%2==1?i-1:n-i);
}
else for (int i=1; i<=n; i++) {
if (i<=n/2) System.out.printf("%d ", i%2==1?i+1:n-i+2);
else System.out.printf ("%d ", i%2==1?n-i:i-1);
}
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: horikawa
* Date: 3/23/13
* Time: 2:05 AM
* To change this template use File | Settings | File Templates.
*/
public class C {
public static void main (String[] argv) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if ((n%4 != 0) && (n%4 != 1)) {
System.out.println(-1);
return;
}
int ans[] = new int[n+1];
for (int i=1; i<n/2; i+=2) {
ans[i] = i+1;
ans[i+1] = n-i+1;
ans[n-i+1] = n-i;
ans[n-i] = i;
}
if (n%4 == 1){
ans[(n+1)/2] = (n+1)/2;
}
for (int i=1; i<=n; i++) {
System.out.print(ans[i]);
if (i != n) {
System.out.print(" ");
}
}
System.out.println();
}
}
| JAVA |
287_C. Lucky Permutation | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).
A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.
You have integer n. Find some lucky permutation p of size n.
Input
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
Output
Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist.
Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.
If there are multiple answers, you can print any of them.
Examples
Input
1
Output
1
Input
2
Output
-1
Input
4
Output
2 4 1 3
Input
5
Output
2 5 3 1 4 | 2 | 9 | import java.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+1)>>1;i+=2)
{
if((i&1)==1) {
p[i]=i+1; p[i+1]=n-i+1;
p[n-i+1]=n+1-p[i];p[n-i]=i;
} else {
p[i]=i+2;p[n-i+1]=n+1-p[i];
p[i+1]=n-i+1;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 | #include <bits/stdc++.h>
const double eps = 1e-6;
const double PI = acos(-1.0);
const int inf = ~0u >> 2;
using namespace std;
const int N = 100010;
int p[N];
int main() {
int n, i;
while (~scanf("%d", &n)) {
if (n == 1) {
puts("1");
continue;
}
if (n < 4) {
puts("-1");
continue;
}
if (n % 4 != 0 && (n - 1) % 4 != 0) {
puts("-1");
continue;
}
for (i = 1; i <= n / 4; ++i) {
p[i] = i + n / 4;
p[p[i]] = n - i + 1;
p[p[p[i]]] = n - p[i] + 1;
p[p[p[p[i]]]] = n - p[p[i]] + 1;
}
if ((n - 1) % 4 == 0) {
p[n / 2 + 1] = n / 2 + 1;
}
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;
const int N = 1e5 + 20;
int n, p[N], ans1, ans2, ans3, ans4, k1, k2, k3, k4, m;
int main() {
cin >> n;
if (n % 4 != 0 && (n - 1) % 4 != 0) {
cout << -1;
return 0;
}
if (n % 4 == 0) {
m = n / 4;
k1 = 1;
k2 = 2;
k3 = n - 1;
k4 = n;
ans1 = 2;
ans2 = n;
ans3 = 1;
ans4 = n - 1;
while (m--) {
p[k1] = ans1;
p[k2] = ans2;
p[k3] = ans3;
p[k4] = ans4;
ans1 += 2;
ans2 -= 2;
ans3 += 2;
ans4 -= 2;
k1 += 2;
k2 += 2;
k3 -= 2;
k4 -= 2;
}
} else {
m = n / 4;
k1 = 1;
k2 = 2;
k3 = n - 1;
k4 = n;
ans1 = 2;
ans2 = n;
ans3 = 1;
ans4 = n - 1;
while (m--) {
p[k1] = ans1;
p[k2] = ans2;
p[k3] = ans3;
p[k4] = ans4;
ans1 += 2;
ans2 -= 2;
ans3 += 2;
ans4 -= 2;
k1 += 2;
k2 += 2;
k3 -= 2;
k4 -= 2;
}
p[(n + 1) / 2] = (n + 1) / 2;
}
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 | n = input ()
if (n==2 or n == 3 or 0 == (n-2)%4 or (n - 3)%4 == 0):
print -1
else:
if (n%2==0):
for i in range(1, n/2+1):
if (i%2==0):
print i-1
else:
print n - i
for i in range(n/2 + (2 if (n%2==1) else 1), n+1):
if (i%2==1):
print i + 1
else:
print n - i + 2
else:
for i in range(1, n/2+1):
if (i%2==0):
print i-1
else:
print n - i
print n/2+1
for i in range(n/2 + 2, n+1):
if (i%2==0):
print i + 1
else:
print n - i + 2 | 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 main() {
ifstream fin("input.txt", ios::in);
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cout << setprecision(10);
cout << fixed;
int n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) return cout << -1, 0;
if (n == 1) return cout << 1, 0;
int d[n];
if (n % 2 == 0) {
for (int i = 0; i < n / 4; i++) {
d[i * 2] = (i + 1) * 2;
d[i * 2 + 1] = n - i * 2;
d[n - i * 2 - 1] = n + 1 - 2 * (i + 1);
d[n - i * 2 - 2] = i * 2 + 1;
}
} else {
d[n / 2] = n / 2 + 1;
for (int i = 0; i < n / 4; i++) {
d[i * 2] = (i + 1) * 2;
d[i * 2 + 1] = n - i * 2;
d[n - i * 2 - 1] = n + 1 - 2 * (i + 1);
d[n - i * 2 - 2] = i * 2 + 1;
}
}
for (int i = 0; i < n; i++) cout << d[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;
vector<int> vec;
void cal(int l, int r) {
if (r < l) return;
if (l == r) {
vec.push_back(l);
return;
}
vec.push_back(l + 1);
vec.push_back(r);
cal(l + 2, r - 2);
vec.push_back(l);
vec.push_back(r - 1);
}
int main() {
int n;
cin >> n;
if (n % 4 != 0 && n % 4 != 1)
cout << -1 << endl;
else {
cal(1, n);
for (int u : vec) cout << u << " ";
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 a[100001];
int main() {
int i, n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) {
cout << -1;
return 0;
}
if (n % 2 == 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] = i;
a[n - i + 1] = n - i;
}
cout << a[1];
for (int i = 2; i <= n; i++) cout << " " << a[i];
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.