列表

详情


NC216011. KCo-primePermutation

描述

Kotori is very good at math (really?) and she loves playing with permutations and primes.

One day, she thinks of a special kind of permutation named k co-prime permutation. A permutation of n is called a k co-prime permutation of n if there exists exactly k integers i such that and , where indicates the greatest common divisor of x and y.

Given n and k, please help Kotori construct a k co-prime permutation of n or just report that there is no such permutation.

Recall that a permutation of n is a sequence of length n containing all integers from 1 to n.

输入描述

There is only one test case in each test file.

The first and only line contains two integers n and k (, ).

输出描述

Output one line containing n integers  separated by one space, indicating the permutation satisfying the given constraints. If no such permutation exists output "-1" (without quotes) instead. If there are multiple valid answers you can print any of them.

Please, DO NOT output extra spaces at the end of each line, otherwise your answer may be considered incorrect!

示例1

输入:

5 3

输出:

1 4 5 2 3

示例2

输入:

1 0

输出:

-1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3(3.9) 解法, 执行用时: 485ms, 内存消耗: 9728K, 提交时间: 2020-12-25 13:47:00

n, k = map(int, input().split())
if k == 0:
    print(-1)
elif n == 1:
    print(1)
else:
    print(k, end = ' ')
    for i in range(1, k):
        print(i, end = ' ')
    for i in range(k + 1, n + 1):
        print(i, end = ' ')

pypy3 解法, 执行用时: 1008ms, 内存消耗: 35972K, 提交时间: 2022-10-06 13:22:11

n, k = map(int,input().split())

if k == 0:
    print(-1)
    exit(0)

print(k, end = '')
for i in range(1, k):
    print('',i, end = '')
for i in range(k+1, n+1):
    print('',i, end = '')
print()

C++(clang++11) 解法, 执行用时: 79ms, 内存消耗: 7128K, 提交时间: 2021-01-22 17:01:58

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin>>n>>k;
	if(k==0)
	return puts("-1"),0;
	for(int i=1;i<=n;i++)
	printf("%d%c",i<=k?i%k+1:i,i==n?'\n':' ');
}

上一题