-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations_UVA941.cpp
64 lines (53 loc) · 1.25 KB
/
permutations_UVA941.cpp
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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("permutations_UVA941.in");
ofstream fout("permutations_UVA941.out");
int main()
{
vector<long long> f(21); f[0] = 1;
for (int i = 1; i <= 20; ++i)
{
f[i] = f[i - 1] * i;
}
int T; fin >> T;
for (int t = 1; t <= T; ++t)
{
long long n;
string s;
fin >> s >> n;
sort(s.begin(), s.end());
int size = s.size();
for (int i = size - 1; i >= 0; --i)
{
long long pos = n / f[i], count = 0;
for (int j = 0; j <= size - 1; ++j)
{
if (s[j] != 0)
{
if (count == pos)
{
fout << s[j];
s[j] = 0;
break;
}
else
{
++count;
}
}
}
n %= f[i];
}
fout << '\n';
}
return 0;
}