原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=139
分析:cantor展开,康托展开用于计算一个排列的序数。公式为: X=a[n]*n!+a[n-1]*(n-1)!+...+a[2]*2!+a[1]*1!+a[0]*0!;0<=a[i]<=i;康托展开计算的是其前面有多少的排列,所以结果需要加1.对于1,2,3组成的排列,321为第2*2!+1*1+1个。
我排第几个
#include<cstdio> #include<cstring> using namespace std; char s[15]; long long fac[15]; void F() { fac[0]=1;fac[1]=1; for(int i=2;i<=12;i++) fac[i]=fac[i-1]*i; } int main() { int T; scanf("%d",&T); getchar(); F(); while(T--) { memset(s,0,sizeof(s)); scanf("%s",s); getchar(); long long res=0; for(int i=0;i<11;i++) { long long count=0; for(int j=i+1;j<12;j++) if(s[j]<s[i])count++; res+=count*fac[11-i]; } printf("%lld ",res+1); } return 0; }