[ICPC2020 Nanjing R] K Co-prime Permutation(构造&思维)

题面翻译

题目描述

给定两个整数 n n n k k k,构造一个 1 ∼ n 1 \sim n 1n 的排列 p 1 , p 2 , ⋯   , p n p_1,p_2,\cdots,p_n p1,p2,,pn,使得存在 k k k 个整数 i i i 满足 1 ≤ i ≤ n 1 \le i \le n 1in gcd ( p i , i ) = 1 \text{gcd}(p_i,i)=1 gcd(pi,i)=1

gcd ( x , y ) \text{gcd}(x,y) gcd(x,y) 表示 x x x y y y 的最大公约数。

输入格式

只有一组测试数据。

第一行输入两个整数 n n n k k k ( 1 ≤ n ≤ 1 0 6 , 0 ≤ k ≤ n ) (1 \le n \le 10^6, 0 \le k \le n) (1n106,0kn)

输出格式

输出一行 n n n 个整数 p 1 , p 2 , ⋯   , p n p_1, p_2, \cdots, p_n p1,p2,,pn,用空格分隔,表示一个满足给定的约束的排列。如果没有存在的排列则输出 -1。如果有多个有效的答案,输出任意一个均可。

请不要在行末输出多余的空格,否则你的答案可能会被认为是错误的。

样例 #1

样例输入 #1
5 3
样例输出 #1
1 4 5 2 3

样例 #2

样例输入 #2
1 0
样例输出 #2
-1

题目描述

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 \textit{k co-prime permutation} k co-prime permutation. A permutation p 1 , p 2 , ⋯   , p n p_1,p_2,\cdots,p_n p1,p2,,pn of n n n is called a k k k co-prime permutation of n n n if there exists exactly k k k integers i i i such that 1 ≤ i ≤ n 1 \le i \le n 1in and gcd ( p i , i ) = 1 \text{gcd}(p_i,i)=1 gcd(pi,i)=1, where gcd ( x , y ) \text{gcd}(x,y) gcd(x,y) indicates the greatest common divisor of x x x and y y y.

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

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

输入格式

There is only one test case in each test file.

The first and only line contains two integers n n n and k k k ( 1 ≤ n ≤ 1 0 6 1 \le n \le 10^6 1n106, 0 ≤ k ≤ n 0 \le k \le n 0kn).

输出格式

Output one line containing n n n integers p 1 , p 2 , ⋯   , p n p_1, p_2, \cdots, p_n p1,p2,,pn 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

样例输入 #1

5 3

样例输出 #1

1 4 5 2 3

样例 #2

样例输入 #2

1 0

样例输出 #2

-1

解析

特判 0 , 1 0,1 0,1 的情况,然后对于相邻的两个数字进行颠倒,那么其肯定互质。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define inf 1e18
const int mod=1e9+7;
const int N=1e6+5;
int n,k;
int a[N];
void solve(){
	cin>>n>>k;
	if(k==0){
		cout<<"-1";
		return;
	}
	int ans[N];
	for(int i=1;i<=n;i++) ans[i]=i;
//	if(k<=(n+1)/2){
		for(int i=1;i<=n;i++){
			if(k==1) break;
			if(k!=3){
				ans[i]=i+1;
				ans[i+1]=i;
				k-=2;
				i++;		
			}
			else if(k==3){
				ans[i+1]=i;
				ans[i+2]=i+1;
				ans[i]=i+2;
				k-=3;
				i+=2;
			}
			if(k<=0) break;
		}
//	}
	for(int i=1;i<=n;i++){
		cout<<ans[i];
		if(i!=n) cout<<" ";
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int tt=1;
//	cin>>tt;
	while(tt--) solve();
	return 0;
}
  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值