-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathPermutationSequence.cpp
More file actions
80 lines (74 loc) · 1.39 KB
/
PermutationSequence.cpp
File metadata and controls
80 lines (74 loc) · 1.39 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <algorithm>
using namespace::std;
int fib(int n)
{
int fib_n=1;
for(int i=2;i<=n;i++)
fib_n*=i;
return fib_n;
}
//Time Limit Exceeded.
string getPermutation(int n,int k)
{
string result;
if(k>fib(n)) return result;
for(int i=0;i<n;i++)
{
char c='1'+i;
result.push_back(c);
}
for(int i=1;i<k;i++)
next_permutation(result.begin(),result.end());
return result;
}
string getPermutation2(int n,int k)
{
string s;
if(k>fib(n)) return s;
for(int i=0;i<n;i++)
{
char c='1'+i;
s.push_back(c);
}
if(k==fib(n))
{
reverse(s.begin(),s.end());
return s;
}
int i=0;
for(i=0;i<n;i++)
if(fib(i+1)>k)
break;
int cur=s.size()-1-i;
while(k)
{
int res=k/fib(i);
if(k%=fib(i))
{
char temp=s[cur+res];
s.erase(cur+res,1);
s.insert(s.begin()+cur,temp);
cur++;
}
else
{
char temp=s[cur+res-1];
s.erase(cur+res-1,1);
s.insert(s.begin()+cur,temp);
cur++;
reverse(s.begin()+cur,s.end());
}
i--;
}
return s;
}
int main()
{
int n,k;
cin>>n>>k;
//cout<<getPermutation(n,k)<<endl;
cout<<getPermutation2(n,k)<<endl;
return 0;
}