#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int fac[2505] = {1, 1 ,2 ,6 ,24 ,120 ,720, 5040, 40320 ,362880},siz;
int cantor(vector<int>a){
int tmp=0,ret=0;
for(int i=0;i<siz;i++){
tmp=0;
for(int j=i+1;j<siz;j++)
if(a[j]<a[i])
tmp++;
ret+=(fac[siz-i-1])*tmp;
}
return ret;
}
vector<int> Recantor(int a){
vector<int>seq;
seq.clear();
bool ban[2505]={};
for(int i=0;i<siz;i++){
int tmp=a/fac[siz-1-i];a%=fac[siz-1-i];
for(int i=0;i<=tmp;i++)
if(ban[i]) tmp++;
ban[tmp]=1;
seq.push_back(tmp+1);
}
return seq;
}
int main(){
int tmp;
vector<int>a;
scanf("%d\n",&siz);
for(int i=10;i<=siz;i++)
fac[i]=fac[i-1]*i;
for(int i=1;i<=siz;i++)
scanf("%c",&tmp),
a.push_back(tmp-'0');
printf("%d\n",cantor(a)+1);
a=Recantor(cantor(a));
for(int i=0;i<siz;i++) printf("%d",a[i]);
}
这两个都可以用树状数组优化成nlogn