H.Shifting

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  CodeForces 286B

Description

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.

Sample Input

Input
2
Output
2 1 
Input
3
Output
1 3 2 
Input
4
Output
4 2 3 1 

Hint

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]
/***************************************************************************************/

模拟水过,STL一直都很好用,这次正好deque派上用场,具体上代码

#pragma comment (linker,"/stack:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<functional>
#include<iostream>
#include<string>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;
const double PI=acos(-1.0);
#define eps 1e-8
#define inf 1000000000
#define fib 112233
#define ll __int64
#define able puts("0000000000000000000000")
template<class T_T> T_T f_max(T_T a,T_T b) { return a>b?a:b; }
template<class T_T> T_T f_min(T_T a,T_T b) { return a<b?a:b; }
template<class T_T> T_T f_abs(T_T a) { return a>0?a:-a; }
template<class T_T> T_T gcd(T_T a,T_T b){ return b?gcd(b,a%b):a; }
template<class T_T> void swap(T_T *a,T_T *b){T_T c;c=a;a=b;b=c;}
int main()
{
    int i,n,m;
    deque<int> d;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        d.push_back(i);
    for(i=2;i<=n;i++)
    {
        m=(n-1)/i*i;
        d.push_back(d[m]);
        for(;m-i>=0;m-=i)
            d[m]=d[m-i];
        d.pop_front();
    }
    for(i=0;i<n;i++)
        printf("%d ",d[i]);
    printf("\n");
    return 0;
}



你可能感兴趣的:(算法,模拟,ACM,双端队列)