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
Output
Input
Output
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
下面贴代码,没有用到数组
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; }
相关文章推荐
- UCOSIII存储管理(待更)
- 操作系统之实验三 进程调度模拟程序
- linux与windows互传
- UVA 624 CD (记录背包)
- untiy 3d ShaderLab_第4章_基本的光照模型
- UCOSIII同时等待多个内核对象(待更)
- Single Number I+II+III
- 形象一点,view和layer的坐标
- XML中<beans>中属性概述
- log4net 错误日志的使用
- python日志模块----logging
- atoi()函数自实现
- NYOJ 991 Registration system
- UCOSIII消息传递(消息队列)(待更)
- UCOSIII事件标志组(待更)
- 49. Group Anagrams 【M】【47】
- 机器学习常见算法分类汇总
- 给QT不规则窗口添加阴影
- maven(二)创建一个maven的web项目中解决Cannot change version of project facet Dynamic web module to 2.5
- The summary of Java