forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1189-sum_of_factorials.cpp
77 lines (60 loc) · 1.03 KB
/
1189-sum_of_factorials.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
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[22];
ll facto[22];
vector <pair <ll, ll> > v;
void fact()
{
facto[0] = 1;
for (int i = 1; i < 21; i++)
facto[i] = i * facto[i - 1];
}
ll yes ;
ll indx;
void recursive(ll num, ll pos)
{
// cout << num << endl;
if (num == 0 )
{
yes = 1;
return;
}
if (pos == -1)
return;
if (num >= facto[pos]) {
arr[indx] = pos;
indx++;
recursive(num - facto[pos], pos - 1 );
return;
}
recursive(num, pos - 1);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/Documents/sameer/input.sam", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
ll i, j, k, n, m, t, cont, a, b;
cin >> t;
ll cases = t;
fact();
while (t--) {
cin >> n ;
yes = 0;
indx = 0;
memset(arr, 0, sizeof arr);
recursive(n, 20);
cout << "Case " << cases - t << ": " ;
if (yes == 1) {
for (i = indx - 1; i > 0; i--)
cout << arr[i] << "!+";
cout << arr[0] << "!\n";
} else {
cout << "impossible\n";
}
}
return 0;
}