题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3301
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXN 21
long long N[MAXN];
int t[MAXN];
int n,m;
int ans[MAXN];
void Solve0(int x,long long y) {
if (y==1) {
for (int i=x;i<=n;i++) {
ans[i]=i-x+1;
}
return ;
}
ans[x]=y/N[n-x]+1;
if (!(y%N[n-x])) ans[x]--,Solve0(x+1,N[n-x]);
else Solve0(x+1,y%N[n-x]);
for (int i=x;i++<n;) {
if (ans[i]>=ans[x]) {
ans[i]++;
}
}
}
int main() {
scanf("%d%d",&n,&m);
N[0]=1;
for (int i=0;i++<n;) {
N[i]=N[i-1]*i;
}
while (m--) {
memset(t,0,sizeof(t));
char c[2];
scanf("%s",&c);
if (c[0]=='P') {
long long x;
scanf("%lld",&x);
Solve0(1,x);
for (int i=0;i++<n-1;) printf("%d ",ans[i]);
printf("%d\n",ans[n]);
} else {
int x[MAXN];
for (int i=0;i++<n;) {
scanf("%d",&x[i]);
}
long long ans=0;
for (int i=0;i++<n;) {
ans+=(long long)(N[n-i]*(x[i]-1));
for (int j=i;j++<n;) {
if (x[j]>x[i]) {
x[j]--;
}
}
}
printf("%lld\n",ans+1);
}
}
return 0;
}