forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
141 lines (126 loc) · 3.43 KB
/
C.cpp
File metadata and controls
141 lines (126 loc) · 3.43 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
139
140
141
#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 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;
using namespace std;
//Buffer reading
int INP,AM,REACHEOF;
const int BUFSIZE = (1<<12) + 17;
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
if(!*inp && !REACHEOF) { \
memset(BUF,0,sizeof BUF);\
int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
if (inpzzz != BUFSIZE) REACHEOF = true;\
inp=BUF; \
} \
INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
AM=0;\
GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
if (INP=='-') {AM=1;GETCHAR(INP);} \
j=INP-'0'; GETCHAR(INP); \
while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
if (AM) j=-j;\
}
//End of buffer reading
struct Num {
long long x, y;
Num(long long _x = 0, long long _y = 1) {
long long g = __gcd(llabs(_x), llabs(_y));
x = _x / g;
y = _y / g;
if (y < 0) {
x = -x;
y = -y;
}
}
};
Num operator + (Num a, Num b) {
return Num(a.x*b.y + a.y*b.x, a.y*b.y);
}
Num operator - (Num a, Num b) {
return Num(a.x*b.y - a.y*b.x, a.y*b.y);
}
Num operator * (Num a, Num b) {
return Num(a.x*b.x, a.y*b.y);
}
Num operator / (Num a, Num b) {
return Num(a.x*b.y, a.y*b.x);
}
ostream& operator << (ostream &out, Num a) {
if (a.x == 0) out << '0';
else if (a.y == 1) out << a.x;
else out << a.x << '/' << a.y;
return out;
}
const int MN = 22;
Num p[MN], q[MN], f[MN], a[MN][MN];
int m, n;
void solve() {
REP(j,n) { // Khu cot j
int save = j;
while (a[save][j].x == 0) ++save;
REP(k,n+1) swap(a[j][k], a[save][k]);
FOR(i,j+1,n-1) if (a[i][j].x) {
Num mult = a[i][j] / a[j][j];
REP(k,n+1)
a[i][k] = a[i][k] - a[j][k] * mult;
}
}
FORD(i,n-1,0) {
FOR(j,i+1,n-1)
a[i][n] = a[i][n] - a[i][j] * q[j];
q[i] = a[i][n] / a[i][i];
}
}
void print(Num p[], int n) {
bool good = false;
REP(i,n) {
if (p[i].x) {
good = true;
cout << '(' << p[i] << ',' << i << ')' << ' ';
}
}
if (!good) {
cout << "(0,0)";
}
cout << endl;
}
int main() {
while (cin >> m >> n && m) {
memset(f, 0, sizeof f);
memset(p, 0, sizeof p);
memset(q, 0, sizeof q);
memset(a, 0, sizeof a);
REP(i,m+n) {
cin >> f[i].x; f[i].y = 1;
}
// solve q
REP(i,n) {
REP(j,n) {
if (m+j-i >= 0) a[i][n-j-1] = f[m+j-i];
else a[i][n-j-1] = Num(0, 1);
}
a[i][n] = (i == 0) ? Num(1,1) : Num(0, 1);
}
solve();
// solve p
FORD(i,m-1,0) {
p[i] = q[0]*f[i];
for(int j=1; j<n; j++) {
if (i-j >= 0)
p[i] = p[i] + q[j]*f[i-j];
}
}
print(p, m);
print(q, n);
cout << endl;
}
return 0;
}