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 + 2 and 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
  1. 2
Output
  1. 2 1
Input
  1. 3
Output
  1. 1 3 2
Input
  1. 4
Output
  1. 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]

在每次变换的时候,都是取一个长度是t的区间,然后把区间的第一个元素放末尾

那么只要每次把所有这样长度为t的区间的a[kt+1]放到a[kt+t+1]即可。

比如样例的变换:

1 2 3 4 0 0 0

0 2 1 4 3 0 0

0 0 1 4 2 3 0

0 0 0 4 2 3 1

这样每次元素在数组当中的位置都会往后移一位,但是总长度还是n

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<deque>
  9. #include<set>
  10. #include<map>
  11. #include<ctime>
  12. #define LL long long
  13. #define inf 0x7ffffff
  14. #define pa pair<int,int>
  15. #define mkp(a,b) make_pair(a,b)
  16. #define pi 3.1415926535897932384626433832795028841971
  17. using namespace std;
  18. inline LL read()
  19. {
  20. LL x=,f=;char ch=getchar();
  21. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  22. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  23. return x*f;
  24. }
  25. int n;
  26. int a[];
  27. int main()
  28. {
  29. while (~scanf("%d",&n))
  30. {
  31. for (int i=;i<=n;i++)a[i]=i;
  32. for (int k=;k<=n;k++)
  33. {
  34. int ed=n+k-,rp=n/k+(n%k!=);
  35. for (int t=k-+(rp-)*k;t>=k-;t-=k)
  36. {
  37. swap(a[ed],a[t]);
  38. ed=t;
  39. }
  40. }
  41. for (int i=n;i<=*n-;i++)printf("%d ",a[i]);
  42. puts("");
  43. }
  44. }

cf 287D

cf287D Shifting的更多相关文章

  1. Level shifting a +/- 2.5V signal to 0 - 5V

    Google : Op-Amp Level Shifter Level shifting a +/- 2.5V signal to 0 - 5V I have a front end module t ...

  2. [LeetCode] Shifting Letters 漂移字母

    We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...

  3. [Swift]LeetCode848. 字母移位 | Shifting Letters

    We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...

  4. Codeforces 286B Shifting (看题解)

    Shifting 感觉这题被智力打击了.. 刚开始我想的是对于每个位置我们可以暴力找出最后的位置在哪里. 因为对于当前位置p, 在进行第x步操作时, 如果p % x == 1 则 p = p + x ...

  5. XBee Level Shifting

    http://www.faludi.com/bwsn/xbee-level-shifting/ The XBee communication (RX/TX) pins definitely opera ...

  6. LeetCode 848. Shifting Letters

    原题链接在这里:https://leetcode.com/problems/shifting-letters/ 题目: We have a string S of lowercase letters, ...

  7. 848.Shifting Letters——weekly contest 87

    848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...

  8. 《Learning to warm up cold Item Embeddings for Cold-start Recommendation with Meta Scaling and Shifting Networks》论文阅读

    <Learning to warm up cold Item Embeddings for Cold-start Recommendation with Meta Scaling and Shi ...

  9. 【LeetCode】848. Shifting Letters 解题报告(Python)

    [LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

随机推荐

  1. Solr版本安装部署指南

    一.依赖包 1.  JDK 1.6以上 2.  solr-4.3.0.tgz 3.  Tomcat或者jetty(注意,solr包中本身就含有jetty的启动相关内容):apache-tomcat-7 ...

  2. JS中的事件、事件冒泡和事件捕获、事件委托

    https://www.cnblogs.com/diver-blogs/p/5649270.html https://www.cnblogs.com/Chen-XiaoJun/p/6210987.ht ...

  3. 爬虫4_python2

    import urllib2 response = urllib2.urlopen("https://www.baidu.com") print response.read() 构 ...

  4. javaEE(6)_JSP

    一.什么是JSP 1.JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术,只用JSP就可以开发动态web资源. 2.为什么J ...

  5. linux 5.7.20和5.6.38版本 数据库忘记root密码怎么找回?

    1.    5.6.38版本的数据库密码丢失找回方法: 第一步.关数据库 第二步:mysqld_safe --skip-grant-tables --skip-networking & 第三步 ...

  6. Ubuntu中安装配置 JDK与apache

    一,前期准备: 1.下载apach网址:https://tomcat.apache.org/download-90.cgi 3.下载:jdk网址:http://www.oracle.com/techn ...

  7. Linux内核——进程管理之CFS调度器(基于版本4.x)

    <奔跑吧linux内核>3.2笔记,不足之处还望大家批评指正 建议阅读博文https://www.cnblogs.com/openix/p/3262217.html理解linux cfs调 ...

  8. sklearn 快速入门教程

    1. 获取数据 1.1 导入sklearn数据集 sklearn中包含了大量的优质的数据集,在你学习机器学习的过程中,你可以通过使用这些数据集实现出不同的模型,从而提高你的动手实践能力,同时这个过程也 ...

  9. timer event

    /* linux/kernel/time/jiffies.c*/ static cycle_t jiffies_read(struct clocksource *cs) { return (cycle ...

  10. Developing

    To work with the Android code, you will need to use both Git and Repo. In most situations, you can u ...