列表

详情


NC236062. K-skip Permutation

描述

For a permutation of n, let f(P, k) be the number of i satisfying and .
Given two integers n and k, your task is to find a permutation P of n such that f(P, k) is maximized.
Recall that in a permutation of n, each integer from 1 to n (both inclusive) appears exactly once.

输入描述

There is only one test case in each test file.
The first and only line contains two integers n and k ().

输出描述

Output one line containing n integers indicating a permutation P of n that maximizes f(P, k). If there are multiple valid answers you can output any of them.
Please, DO NOT output extra spaces at the end of the line, or your answer may be considered incorrect!

示例1

输入:

3 1

输出:

1 2 3

示例2

输入:

7 3

输出:

2 5 1 4 7 3 6

示例3

输入:

3 7

输出:

1 3 2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 110ms, 内存消耗: 7140K, 提交时间: 2022-04-03 09:40:03

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=k;i++){
		for(int j=i;j<=n;j+=k){
			cout<<j<<' ';
		}
	}
}

Python3 解法, 执行用时: 1310ms, 内存消耗: 96372K, 提交时间: 2023-05-11 21:39:09

n,k=map(int,input().split())
x=[]
for i in range(1,k+1):
    for j in range(i,n+1,k):
        x.append(str(j))
print(" ".join(x))

上一题