Codeforces Round #176 (Div. 2) D. Shifting(模拟,STLdeque应用)
发布日期:2021-11-12 00:25:54 浏览次数:11 分类:技术文章

本文共 2404 字,大约阅读时间需要 8 分钟。

D. Shifting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

John Doe has found the beautiful permutation formula.

Let's take permutation p = p1, p2, ..., pn. Let's define transformation f of this permutation: 

where k (k > 1) is an integer, the transformation parameter, r is such maximum integer that rk ≤ n. If rk = n, then elements prk + 1, prk + 2and so on are omitted. In other words, the described transformation of permutation p cyclically shifts to the left each consecutive block of length k and the last block with the length equal to the remainder after dividing n by k

John Doe thinks that permutation f(f( ... f(p = [1, 2, ..., n], 2) ... , n - 1), n) is beautiful. Unfortunately, he cannot quickly find the beautiful permutation he's interested in. That's why he asked you to help him.

Your task is to find a beautiful permutation for the given n. For clarifications, see the notes to the third sample.

Input

A single line contains integer n (2 ≤ n ≤ 106).

Output

Print n distinct space-separated integers from 1 to n — a beautiful permutation of size n.

Examples
input
2
output
2 1
input
3
output
1 3 2
input
4
output
4 2 3 1
Note

A note to the third test sample: 

  • f([1, 2, 3, 4], 2) = [2, 1, 4, 3]
  • f([2, 1, 4, 3], 3) = [1, 4, 2, 3]
  • f([1, 4, 2, 3], 4) = [4, 2, 3, 1]

题意:

定义排列p = p1, p2, ..., pn。

定义函数f(p, k)用来转换p排列,k是转换参数, k > 1。

假设排列p的长度为n,首先把p按长度k进行分块,若最后一块长度不足k,则其长度为n%k,然后分别对每一块循环左移一位,得到一个新的排列。

给定n(2 <= n <= 1e6)。请输出 f(f(...f(p = [1,2,...,n],2)...,n-1,),n)的结果。

对于样例3:

1.f([1,2,3,4],2) = [2,1,4,3]

2.f([2,1,4,3],3) = [1,4,2,3]

3.f([1,4,2,3],4) = [4,2,3,1]

题解:

对f(p,k)循环右移一位之后可以发现得到的序列相当于对原序列中下标mod k=1位置构成的子序列循环右移一位,

因此只需要对这些位置循环右移一位再将整个序列循环左移一位(也就是把第一个数字挪到最后)即可得到f(p,k),
可以用一个双端队列维护,第k次操作需要操作n/k+O(1)的元素,总复杂度是O(nlogn)。

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define rep(i,a,n) for (int i=a;i
=a;i--)#define pb push_back#define fi first#define se secondtypedef vector
VI;typedef long long ll;typedef pair
PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=20+10;deque
dq;int main(){ int n; scanf("%d",&n); rep(i,1,n+1) dq.push_back(i); rep(i,2,n+1) { for(int j=(n-1)/i;j>0;j--) swap(dq[(j-1)*i],dq[j*i]); dq.push_back(dq.front()); dq.pop_front(); } rep(i,0,n) printf("%d ",dq[i]); return 0;}

转载地址:https://blog.csdn.net/fouzhe/article/details/56486259 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:hdu 4348 To the moon(主席树,区间更新节省内存,经典)
下一篇:Codeforces Alpha Round #21 D. Traveling Graph(欧拉路,floyed,dp,好题)

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月24日 13时40分20秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章