1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Tasowanie [TAS]
#include <stdio.h>
const int MX=1100100;
int n,m,c,i,j,prev,cur,a[MX],b[MX],p[MX],q[MX];
bool u[MX];
int main() {
  scanf("%d%d",&n,&m);
  for (i=1; i<=(1<<n); i++) scanf("%d",&a[i]);
  p[1]=prev=1;
  for (j=1; j<=n; j++) {
    cur=(1<<j);
    for (i=cur; i>prev; i--) p[i]=p[i-prev];
    for (; i>0; i--) p[i]+=prev;
    prev=cur;
  }
  for (i=1; i<=prev; i++) if (!u[i]) {
    for (j=i, c=0; !u[j]; j=p[j], c++) {
      b[c]=j;
      u[j]=true;
    }
    cur=m%c;
    for (j=0; j<c; j++, cur++) {
      if (cur==c) cur=0;
      q[b[j]]=b[cur];
    }
  }
  for (i=1; i<=prev; i++) printf("%d%c",a[q[i]],(i==prev)?'\n':' ');
  return 0;
}