forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10090.cpp
234 lines (224 loc) · 4.84 KB
/
10090.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <bits/stdc++.h>
using namespace std;
// @ http://www.ddj.com/cpp/184403801
template <class T>
struct StringTok
{
StringTok(const T &seq, typename T::size_type pos = 0) : seq_(seq), pos_(pos) {}
T operator()(const T &delim);
const T &seq_;
typename T::size_type pos_;
};
template <class T>
T StringTok<T>::operator()(const T &delim)
{
T token;
if (pos_ != T::npos)
{
// start of found token
typename T::size_type first(seq_.find_first_not_of(delim.c_str(), pos_));
if (first != T::npos)
{
// length of found token
typename T::size_type num(seq_.find_first_of(delim.c_str(), first) - first);
// do all the work off to the side
token = seq_.substr(first, num);
// done; now commit using
// nonthrowing operations only
pos_ = first + num;
if (pos_ != T::npos)
{
++pos_;
}
if (pos_ >= seq_.size())
{
pos_ = T::npos;
}
}
}
return token;
}
// @ http://www.gotw.ca/gotw/029.htm for case-insensitive lexicographic compares
struct ci_char_traits : public std::char_traits<char>
{
static bool eq(char c1, char c2)
{
return toupper(c1) == toupper(c2);
}
static bool ne(char c1, char c2)
{
return toupper(c1) != toupper(c2);
}
static bool lt(char c1, char c2)
{
return toupper(c1) < toupper(c2);
}
static int compare(const char *s1, const char *s2, size_t n)
{
return strncasecmp(s1, s2, n);
}
static const char *find(const char *s, int n, char a)
{
while (n-- > 0 && toupper(*s) != toupper(a))
{
++s;
}
return s;
}
};
// no boost makes c++ angry; lexical cast comes in handy for string <=> int conversion without expliucitly writing out the streams all the time.
template <typename T, typename S>
T lexical_cast(const S &arg)
{
stringstream interpreter;
T result;
if (!(interpreter << arg) || !(interpreter >> result) || !(interpreter >> std::ws).eof())
{
throw logic_error("bad lexical_cast");
}
return result;
}
typedef unsigned long ul;
typedef long long ll;
typedef vector<string> vs;
typedef vector<int> vi;
typedef std::basic_string<char, ci_char_traits> ci_string;
pair<ll, ll> extended_gcd(ll a, ll b)
{
if (a % b == 0)
{
return make_pair(0, 1);
}
else
{
pair<ll, ll> temp(extended_gcd(b, a % b));
return make_pair(temp.second, temp.first - temp.second * (a / b));
}
}
pair<ll, ll> extended_gcd_2(ll a, ll b)
{
ll x(0), y(1), lastx(1), lasty(0);
while (b != 0)
{
ll temp(b);
ll quotient(a / b);
b = a % b;
a = temp;
temp = x;
x = lastx - quotient * x;
lastx = temp;
temp = y;
y = lasty - quotient * y;
lasty = temp;
}
return make_pair(lastx, lasty);
}
int main()
{
ll n;
while (scanf("%lld", &n) == 1, n)
{
ll c1, n1, c2, n2;
cin >> c1 >> n1 >> c2 >> n2;
pair<ll, ll> res(extended_gcd(n1, n2));
ll gcd(res.first * n1 + res.second * n2);
ll lcm((n1 * n2) / gcd);
if (0 == n % gcd)
{
n /= gcd;
lcm /= gcd;
n1 /= gcd;
n2 /= gcd;
}
else
{
cout << "failed" << endl;
continue;
}
ll n1_delta(lcm / n1);
ll n2_delta(lcm / n2);
ll t_n1(res.first), t_n2(res.second);
// overflow the UL to chk `extended_gcd`
t_n1 *= n;
t_n2 *= n;
if (t_n1 < 0)
{
ll steps((0 - t_n1) / n1_delta);
t_n1 += steps * n1_delta;
t_n2 -= steps * n2_delta;
if (t_n1 < 0)
{
t_n1 += n1_delta;
t_n2 -= n2_delta;
}
ll cand_cost(t_n1 * c1 + t_n2 * c2);
ll t_t_n1(t_n1 + n1_delta);
ll t_t_n2(t_n2 - n2_delta);
ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2);
if (t_cand_cost < cand_cost && t_t_n2 >= 0)
{
ll more_steps(t_n2 / n2_delta);
t_n2 -= more_steps * n2_delta;
t_n1 += more_steps * n1_delta;
}
}
else if (t_n2 < 0)
{
ll steps((0 - t_n2) / n2_delta);
t_n1 -= steps * n1_delta;
t_n2 += steps * n2_delta;
if (t_n2 < 0)
{
t_n1 -= n1_delta;
t_n2 += n2_delta;
}
ll cand_cost(t_n1 * c1 + t_n2 * c2);
ll t_t_n1(t_n1 - n1_delta);
ll t_t_n2(t_n2 + n2_delta);
ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2);
if (t_cand_cost < cand_cost && t_t_n1 >= 0)
{
ll more_steps(t_n1 / n1_delta);
t_n1 -= more_steps * n1_delta;
t_n2 += more_steps * n2_delta;
}
}
else
{
{
ll cand_cost(t_n1 * c1 + t_n2 * c2);
ll t_t_n1(t_n1 + n1_delta);
ll t_t_n2(t_n2 - n2_delta);
ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2);
if (t_cand_cost < cand_cost && t_t_n2 >= 0)
{
ll more_steps(t_n2 / n2_delta);
t_n2 -= more_steps * n2_delta;
t_n1 += more_steps * n1_delta;
}
}
{
ll cand_cost(t_n1 * c1 + t_n2 * c2);
ll t_t_n1(t_n1 - n1_delta);
ll t_t_n2(t_n2 + n2_delta);
ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2);
if (t_cand_cost < cand_cost && t_t_n1 >= 0)
{
ll more_steps(t_n1 / n1_delta);
t_n1 -= more_steps * n1_delta;
t_n2 += more_steps * n2_delta;
}
}
}
if (t_n1 < 0 || t_n2 < 0)
{
printf("failed\n");
continue;
}
else
{
cout << t_n1 << " " << t_n2 << endl;
}
}
return 0;
}