Data mapping Day4 T1

topic

  X small number of columns to see mountains of work is a headache, you want smart to help him. Consider the number of columns \ (A = \) [ \ (A_1 \) , \ (A_2 \) ...... \ (A_n \) ], define the transform F (A, K) = [ \ (A_2 \) , \ (A_3 \) ...... \ (a_k \) , \ (A_1 \) , \ (A_ {2} + K \) , \ (A_. 3} {K + \) ...... \ (A_ 2K} {\) , \ (A_ { . 1} + k \) ......], that is, the a segment, each of k (k is less than one if the last, all assigned to the new section, the detailed examples), then the first one of each segment moving to the last segment.
  Now, small wondered X f (f (f (f ( [1,2,3, ⋯, n], 2), 3), ⋯), n) results.

Entry

Input row contains an integer n.

Export

Output line contains n integers, it represents the final number of columns.

Sample

Entry Export
4 4 2 3 1

data range

For 60% of the data, 1≤n≤ \ (10. 3 ^ \) .
To 100% of the data, 1≤n≤ \ (. 6 10 ^ \) .

answer

  Careful observation can be found, every time a first number of the previous period moves to the period after the first.
  Each time the first number of the previous period moves to the first position after the period, then each will move to the first number of the previous segment after the first segment, the final total number of columns on the entire movement of the n -1 time. Just open a large array twice, and then record the current head position to the number of columns.
(I use the scroll array, saw a Dalao code can be found just use swapSTL Dafa is good!

#include<cstdio>
#include<algorithm>
using namespace std;
int n,head,inv=0;
int a[2000010]={0},t[2]={0};

void init(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) a[i]=i;
    head=1;
}

void work(int k){
    for(int i=head;i<=n+head-1;i+=k){
        t[inv]=a[i];
        a[i]=t[inv^1];
        inv^=1;
        if(i+k>n+head-1) a[n+head]=t[inv^1];
    }
}

int main(){
    init();
    for(int i=2;i<=n;i++){
        work(i);
        head++;
    }
    for(int i=head;i<n+head-1;i++) printf("%d ",a[i]);
    printf("%d",a[n+head-1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/znk161223/p/11516643.html