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
81
82
83
84
85
86
87
88
89
90
91
92
|
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll k, now = 0;
int n;
int seq[21];
bool visited[21];
vector<int> ans, res;
ll fact[21];
ll makeFact(ll m)
{
if (m == 0 || m == 1)
return fact[m] = 1;
return fact[m] = m * makeFact(m - 1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
//n개의 순열이 있음
int qes;
cin >> qes;
for (int i = 0; i < n; i++)
seq[i] = i + 1;
makeFact(n - 1);
if (qes == 1)
{
//k번째 순열을 찾는다.
cin >> k;
for (int i = 0; i < n; i++)
{
int pos = n - i - 1;
int cnt = 0;
int now = 0;
for (int j = 1; j <= n; j++)
{
if (visited[j]) continue;
if (k <= fact[pos] * cnt)
{
break;
}
now = j;
++cnt;
}
k -= (cnt-1) * fact[pos];
visited[now] = true;
ans.push_back(now);
}
for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
cout << '\n';
}
else if (qes == 2)
{
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
res.push_back(tmp);
}
//res가 몇번째 순열인지 찾기
for (int i = 0; i < n; i++) {
int pos = n-i-1;
int cnt = 0;
for (int j = 1; j <= res[i]; j++) {
if (!visited[j]) ++cnt;
}
k += (cnt-1) * fact[pos];
visited[res[i]] = true;
}
cout << k+1 << '\n';
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
BOJ/C++