forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF.cpp
More file actions
86 lines (78 loc) · 2.04 KB
/
F.cpp
File metadata and controls
86 lines (78 loc) · 2.04 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
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
int n, target, f[33333];
pair <int,int> a[22];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> target;
for (int i = 1; i <= n; i++)
{
cin >> a[i].first;
a[i].second = i;
if (a[i].first == target) {
cout << 1 << endl;
cout << 1 << ' ' << i << endl;
return 0;
}
}
sort(a + 1, a + n + 1);
queue <int> q;
f[0] = 1;
q.push(0);
while (!q.empty() && !f[target])
{
int x = q.front();
q.pop();
for (int i = 1; i < n; i++)
{
int y = (x + a[i].first) % a[n].first;
if (!f[y])
{
f[y] = i;
q.push(y);
}
}
}
if (!f[target]) cout << -1 << endl;
else
{
vector <int> trace;
int x = target;
while (x)
{
trace.push_back(f[x]);
x = (x - a[f[x]].first + a[n].first) % a[n].first;
}
reverse(trace.begin(), trace.end());
vector <int> type;
vector < pair<int,int> > ves;
for (int id : trace)
{
x += a[id].first;
type.push_back(1);
ves.push_back({a[id].second, 0});
type.push_back(3);
ves.push_back({a[id].second, a[n].second});
if (x >= a[n].first)
{
type.push_back(2);
ves.push_back({a[n].second, 0});
if (x > a[n].first)
{
type.push_back(3);
ves.push_back({a[id].second, a[n].second});
}
x %= a[n].first;
}
}
cout << type.size() << endl;
for (int i = 0; i < type.size(); i++)
{
cout << type[i] << ' ' << ves[i].first;
if (type[i] == 3) cout << ' ' << ves[i].second;
cout << '\n';
}
}
}