-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarbles_UVA10090.cpp
72 lines (60 loc) · 1.43 KB
/
Marbles_UVA10090.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("Marbles_UVA10090.in");
ofstream fout("Marbles_UVA10090.out");
long long gcd(long long a, long long b, long long & x, long long & y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
long long res = gcd(b, a % b, y, x);
y -= (a / b) * x;
return res;
}
}
int main()
{
while (true)
{
long long n = 0, c1, n1, c2, n2;
fin >> n;
if (n == 0) break;
fin >> c1 >> n1 >> c2 >> n2;
long long tempX, tempY, x, y;
long long res = gcd(n1, n2, tempX, tempY);
long long lower = ceil(-1.0 * n * tempX / n2);
long long upper = floor(1.0 * n * tempY / n1);
if (lower > upper || n % res > 0)
{
fout << "failed\n";
continue;
}
if (c1 * n2 > c2 * n1)
{
x = tempX * n / res + n2 / res * lower;
y = tempY * n / res - n1 / res * lower;
}
else
{
x = tempX * n / res + n2 / res * upper;
y = tempY * n / res - n1 / res * upper;
}
fout << x << ' ' << y << '\n';
}
return 0;
}