zoukankan      html  css  js  c++  java
  • 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京)K Co-prime Permutation

    题目

    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 kk co-prime permutation. A permutation (p_1,p_2,cdots,p_n)


    of nn is called a kk co-prime permutation of nn if there exists exactly kk integers ii such that (1 le i le n1≤i≤n ,and ext{gcd}(p_i,i)=1,gcd(p i,i)=1, where, ext{gcd}(x,y)gcd(x,y)) indicates the greatest common divisor of xx and yy.

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

    Recall that a permutation of nn is a sequence of length nn containing all integers from 11 to nn.
    输入描述:
    There is only one test case in each test file.

    The first and only line contains two integers nn and kk ((1 le n le 10^6,1≤n≤10^6 , 0 le k le n ,0≤k≤n).)
    输出描述:
    Output one line containing nn integers (p_1, p_2, cdots, p_n)

    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

    题意

    给定一个长度为n的数列和k,让求出一个(gcd(a[i],i)=1) 个数等于k的排列

    思路

    尝试多写几组样例可得
    5 0
    -1
    5 1
    1 2 3 4 5
    1 2 3 4 5

    5 2
    2 1 3 4 5
    1 2 3 4 5

    5 3
    1 2 3 5 4
    1 2 3 4 5

    5 4
    2 1 4 3 5
    1 2 3 4 5

    5 5
    5 1 2 3 4
    1 2 3 4 5

    k=0的时候无解,因为1和1互质
    其他情况都有解
    k是奇数的时候从非1的数里面两两调换,这样就可以多出来偶数对(gcd(a[i],i)=1)
    k是奇数的时候从1开始的数里面两两调换,这样就可以多出来奇数对(gcd(a[i],i)=1)

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 1000000+100;
    int a[maxn];
    int main()
    {
    	int n,k;
    	cin>>n>>k;
    	for(int i=1;i<=n;i++) a[i] = i;
    	if(k==0) return cout<<-1,0;
    	else
    	{
    		if(k==1)
    		{
    			for(int i=1;i<=n;i++)
    			cout<<i<<" ";
    		}
    		else if(k==n)
    		{
    			cout<<n<<" ";
    			for(int i=1;i<=n-1;i++)
    			cout<<i<<" ";
    		}
    		else
    		{
    			if(k%2==0)
    			{
    				int now = k/2;
    				int f = 1;
    				int cnt = 0;
    				for(int i=1;i<=n;i++)
    				{
    					if(now)
    					{
    						cnt++;
    						if(f)
    						cout<<i+1<<" " , f = !f; 
    						else
    						cout<<i-1<<" " , f = !f;
    						if(cnt==2) now-- , cnt = 0;
    					}
    					else
    					cout<<i<<" ";
    				}	
    			}
    			else
    			{
    				a[1] = 1;
    				for(int i=2;i<=k;i+=2)
    				{
    					a[i] = i+1;
    					a[i+1] = i;
    				}
    				for(int i=1;i<=n;i++) cout<<a[i]<<" ";
    			}
    		}
    	} 
    }
    
    /*
    5 0 
    -1
    5 1
    1 2 3 4 5
    1 2 3 4 5
    
    5 2
    2 1 3 4 5
    1 2 3 4 5
    
    5 3
    1 2 3 5 4
    1 2 3 4 5 
    
    5 4
    2 1 4 3 5
    1 2 3 4 5
    
    5 5
    5 1 2 3 4
    1 2 3 4 5
    */
    
  • 相关阅读:
    Python单元测试之unittest基础
    linux--硬链接和软链接
    12-8 istio核心功能实战-----可观察性(程访问遥测插件)
    12-7 istio核心功能实战-----可观察性(网络可视化)
    12-6 istio核心功能实战-----可观察性(分布式追踪)
    12-3 部署面向生产的istio-----核心组件
    12 ServiceMesh代表作istio-----12-1 ServiceMes、Istio架构原理
    11-7 Grafana看板和邮件报警
    11-6 监控落地
    11-4 部署前奏-Helm&Operator
  • 原文地址:https://www.cnblogs.com/liangyj/p/14168847.html
Copyright © 2011-2022 走看看