int a[1005], b[1005], c[2005], d[1005];
int main() {
//输入第一个多项式
int n1; cin >> n1;
for(int i = 0; i < n1; i ++) {
int x, y;
cin >> x >> y;
a[y] = x;
}
//输入第二个多项式
int n2; cin >> n2;
for(int i = 0; i < n2; i ++) {
int x, y;
cin >> x >> y;
b[y] = x;
}
//多项式相乘
for(int i = 0; i <= 1000; i ++) {
if(a[i]) {
for(int j = 0; j <= 1000; j ++) {
if(b[j]) {
c[i + j] += a[i] * b[j]; //+=
}
}
}
}
bool f = 0;
for(int i = 2000; i >= 0; i --) {
if(c[i]) {
if(f) cout << " ";
cout << c[i] << " " << i;
f = 1;
}
}
if(f == 0) cout << "0 0";
cout << endl;
//多项式相加
for(int i = 0; i <= 1000; i ++) {
d[i] = a[i] + b[i];
}
f = 0;
for(int i = 1000; i >= 0; i --) {
if(d[i]) {
if(f) cout << " ";
cout << d[i] << " " << i;
f = 1;
}
}
if(f == 0) cout << "0 0";
return 0;
}
多项式相乘时要用+=赋值,合并同类项