[ICPC2020南京K] K Co-prime Permutation - 构造

[ICPC2020南京K] K Co-prime Permutation - 构造

Description

给定n,k,要求构造一个排列a,满足存在恰好k对:\(gcd(a[i],i)=1\),如果无解输出-1.

Solution

由于 \(gcd(x-1,x)=1\),将前 \(k\) 个数循环移位即可

#include <bits/stdc++.h>
using namespace std;

#define int long long

int n, k, a[1000005];

signed main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    iota(a + 1, a + n + 1, 1);
    if (k == 0)
    {
        cout << -1 << endl;
    }
    else
    {

        rotate(a + 1, a + 2, a + k + 1);
        for (int i = 1; i <= n; i++)
            cout << a[i] << " ";
        cout << endl;
    }
}
上一篇:PAT 甲级 1023 Have Fun with Numbers (20分)


下一篇:【CF452F】Permutation(线段树维护哈希值)