forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
138 lines (113 loc) · 3.1 KB
/
A.cpp
File metadata and controls
138 lines (113 loc) · 3.1 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
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
using namespace std;
struct Fraction {
ll x, y;
Fraction(ll k) : x(k), y(1) {}
Fraction(ll _x, ll _y) : x(_x), y(_y) {
ll g = __gcd(x, y);
x /= g;
y /= g;
if (y < 0) {
x = -x;
y = -y;
}
}
Fraction operator + (const Fraction& a) const {
return Fraction(x*a.y + y*a.x, y*a.y);
}
Fraction operator - (const Fraction& a) const {
return Fraction(x*a.y - y*a.x, y*a.y);
}
Fraction operator * (const Fraction& a) const {
return Fraction(x*a.x, y*a.y);
}
Fraction operator / (const Fraction& a) const {
return Fraction(x*a.y, y*a.x);
}
Fraction inverse() const { return Fraction(y, x); }
string toString() {
stringstream ss; ss << x << '/' << y;
return ss.str();
}
};
struct Cont {
vector<ll> a;
void read(int n) {
a.resize(n);
REP(i,n) cin >> a[i];
}
void print() {
for(ll x : a) cout << x << ' '; cout << '\n';
}
Fraction toFrac() {
Fraction res = a[a.size()-1];
FORD(i,a.size()-2,0) {
res = res.inverse() + Fraction(a[i]);
}
return res;
}
};
Cont toCont(Fraction f) {
Cont res;
res.a.clear();
if (f.x == 0) {
res.a.push_back(0);
return res;
}
if (f.x % f.y == 0) {
res.a.push_back(f.x / f.y);
return res;
}
while (f.x) {
if (f.x < 0) {
ll t = f.x / f.y - 1;
res.a.push_back(t);
f.x -= t * f.y;
}
else {
res.a.push_back(f.x / f.y);
f.x %= f.y;
}
if (!f.x) break;
f = f.inverse();
}
return res;
}
void test() {
Cont x; x.a = vector<ll> {5, 1, 1, 2};
Fraction f = x.toFrac();
assert(f.x == 28 && f.y == 5);
x.a = vector<ll> {5, 2, 2};
f = x.toFrac();
assert(f.x == 27 && f.y == 5);
f = Fraction(275, 25);
assert(f.x == 11 && f.y == 1);
}
int main() {
ios :: sync_with_stdio(false);
test();
int n1, n2;
int test = 0;
while (cin >> n1 >> n2 && n1 && n2) {
Cont a, b;
a.read(n1);
b.read(n2);
Fraction fa = a.toFrac();
Fraction fb = b.toFrac();
cout << "Case " << ++test << ":\n";
toCont(fa + fb).print();
toCont(fa - fb).print();
toCont(fa * fb).print();
toCont(fa / fb).print();
}
return 0;
}