50 lines
1.1 KiB
C++
50 lines
1.1 KiB
C++
#include <cmath>
|
|
#include <iostream>
|
|
#include <map>
|
|
|
|
using namespace std;
|
|
|
|
class Polynomial : public map<unsigned int, long long, greater<>> {
|
|
public:
|
|
Polynomial operator+=(const Polynomial& other) {
|
|
for (const auto& [exp, coef] : other) {
|
|
(*this)[exp] += coef;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
void print() const {
|
|
for (const auto& [exp, coef] : *this) {
|
|
if (coef == 0) { continue; }
|
|
cout << "[ " << coef << " " << exp << " ] ";
|
|
}
|
|
cout << endl;
|
|
}
|
|
};
|
|
|
|
Polynomial input_polynomial() {
|
|
long long coefficient;
|
|
int exponent;
|
|
Polynomial polynomial;
|
|
while (cin >> coefficient >> exponent && exponent >= 0) {
|
|
polynomial[exponent] += coefficient;
|
|
}
|
|
return polynomial;
|
|
}
|
|
|
|
int main() {
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
int n;
|
|
cin >> n;
|
|
for (int _ = 0; _ < n; _++) {
|
|
Polynomial polynomial1 = input_polynomial();
|
|
Polynomial polynomial2 = input_polynomial();
|
|
polynomial1 += polynomial2;
|
|
polynomial1.print();
|
|
}
|
|
|
|
return 0;
|
|
}
|