题解:P14127 [SCCPC 2021] K-skip Permutation

· · 题解

思路

这道题要分类讨论,我们来试试,第一个数为 1,那么第二个数为 1+k,第三个数是 1+2k……我们只需讨论开头为 1,2,3...k 的情况,把上一个数字计作 j,特殊的如果这是一个新序列,上一个数字为 0 ,如果 j+k\le n 就把他加入答案,否则开启一个新序列。

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+5;
int n,k;
int ans[N],cnt;
signed main() 
{
    cin>>n>>k;
    for(int i=1;i<=k;i++)
    {
        int j=i;
        while(j<=n)
        {
            ans[++cnt]=j;
            j+=k;
        }
    }
    for(int i=1;i<n;i++)
        cout<<ans[i]<<" ";
    cout<<ans[n];
    return 0;
}