您的位置:首页 > 其它

codeforces 361B 思维题

2016-04-29 17:32 204 查看
B. Levko and Permutation

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Levko loves permutations very much. A permutation of length
n is a sequence of distinct positive integers, each is at most
n.

Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers
a and b. Levko assumes that element
pi of permutation
p1, p2, ... , pn
is good if gcd(i, pi) > 1. Levko considers a permutation
beautiful, if it has exactly
k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them.

Input
The single line contains two integers n and
k (1 ≤ n ≤ 105,
0 ≤ k ≤ n).

Output
In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist.

If there are multiple suitable permutations, you are allowed to print any of them.

Examples

Input
4 2


Output
2 4 3 1


Input
1 1


Output
-1


Note
In the first sample elements 4 and
3 are good because gcd(2, 4) = 2 > 1 and
gcd(3, 3) = 3 > 1. Elements
2 and 1 are not good because
gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.

The second sample has no beautiful permutations.

codeforces上很多都是思维题,要敢于开脑洞。

假设b[I]为要输出的数组(从1到n编号)

拿这道题来说。首先我们能观察到几个性质:

1.b[1]无论是几,肯定是一个不好的数。

2题目要求m个好数,必然有n-m个坏数。除去b[1],还有n-m-1个坏数

3对任意b[k],只要k大于1,则若b[k]=k,它一定是一个好数。

4(很重要!)任意两个相邻的正整数,其最大公约数必然为1

5 因此,题目的坏数可以用性质4构造

至此,本题O(n)的解法已经出来了

b[1]=n-m,接下来的b[2]到b[n-m]不用我说了吧

剩下的b[k]=k

下面贴代码,没有用到数组

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<algorithm>
using namespace std;
#define zuida 100000
int n,m;

int main(void)
{

scanf("%d%d",&n,&m);

if(n==m)
printf("-1");
else
{
printf("%d ",n-m);
for(int i=1;i<=n-m-1;i++)
{
printf("%d ",i);
}
for(int i=n-m+1;i<=n;i++)
printf("%d ",i);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: