来个水题。。难得的1Y。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define LL __int64 6 int flag[31],que[31]; 7 LL fact[31]; 8 int n; 9 LL m; 10 void dfs(LL x,int step) 11 { 12 LL temp = 0; 13 int i; 14 for(i = 1;i <= n;i ++) 15 { 16 if(!flag[i]) 17 { 18 if(temp + fact[n-step] >= x) 19 { 20 flag[i] = 1; 21 que[step] = i; 22 dfs(x-temp,step+1); 23 return ; 24 } 25 temp += fact[n-step]; 26 } 27 } 28 } 29 int main() 30 { 31 int i; 32 fact[0] = 1; 33 for(i = 1;i <= 20;i ++) 34 fact[i] = fact[i-1]*i; 35 scanf("%d%I64d",&n,&m); 36 dfs(m,1); 37 for(i = 1;i <= n;i ++) 38 { 39 if(i == 1) 40 printf("%d",que[i]); 41 else 42 printf(" %d",que[i]); 43 } 44 printf(" "); 45 return 0; 46 }