Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- using namespace std;
- template<typename T>
- class Polynomial {
- private:
- map<size_t, T> data;
- public:
- Polynomial() {}
- Polynomial(const vector <T>& vec) {
- for (size_t i = 0; i < vec.size(); ++i) {
- if (vec[i] != T(0)) {
- data[i] = vec[i];
- }
- }
- }
- Polynomial(const T x) {
- if (x != T(0)) {
- data[0] = x;
- }
- }
- template<typename Iter>
- Polynomial(Iter first, Iter last) {
- size_t k = 0;
- while (first != last) {
- if (*first != T(0)) {
- data[k] = *first;
- }
- ++k;
- ++first;
- }
- }
- bool operator == (const Polynomial& p1) const {
- return p1.data == data;
- }
- bool operator != (const Polynomial& p1) const {
- return p1.data != data;
- }
- Polynomial& operator += (const Polynomial& other) {
- for (const auto& i : other.data) {
- if (i.second + data[i.first] == T(0)) {
- data.erase(i.first);
- } else {
- data[i.first] += i.second;
- }
- }
- return *this;
- }
- friend Polynomial operator + (const Polynomial& p1, const Polynomial& p2) {
- auto tmp(p1);
- tmp += p2;
- return tmp;
- }
- Polynomial& operator -= (const Polynomial& other) {
- for (const auto& i : other.data) {
- if (i.second - data[i.first] == T(0)) {
- data.erase(i.first);
- } else {
- data[i.first] -= i.second;
- }
- }
- return *this;
- }
- friend Polynomial operator - (const Polynomial& p1, const Polynomial& p2) {
- auto tmp(p1);
- tmp -= p2;
- return tmp;
- }
- const typename map<size_t, T>::const_iterator begin() const {
- return data.begin();
- }
- const typename map<size_t, T>::const_iterator end() const {
- return data.end();
- }
- T operator[] (const size_t pos) const {
- if (data.find(pos) == data.end()) {
- return T(0);
- }
- return data.at(pos);
- }
- size_t Degree() const {
- if (data.empty()) {
- return T(-1);
- }
- auto it = data.end();
- --it;
- return it->first;
- }
- Polynomial& operator *= (const Polynomial& other) {
- Polynomial Copy_p;
- for (const auto& i : data) {
- for (const auto& j : other.data) {
- Copy_p.data[i.first + j.first] += i.second * j.second;
- }
- }
- (*this).data.clear();
- for (const auto& i : Copy_p) {
- if (i.second != T(0)) {
- data[i.first] = i.second;
- }
- }
- return *this;
- }
- friend Polynomial operator * (const Polynomial& p1, const Polynomial& p2) {
- auto tmp(p1);
- tmp *= p2;
- return tmp;
- }
- T operator () (const T& x) const {
- T ans(0);
- for (const auto& i : data) {
- T st(1);
- for (size_t j = 0; j < i.first; ++j) {
- st *= x;
- }
- ans += st * i.second;
- }
- return ans;
- }
- friend ostream& operator << (ostream& out, const Polynomial& p) {
- if (p.data.empty()) {
- out << '0';
- return out;
- }
- auto it = p.data.end();
- auto start = --it;
- ++it;
- do {
- --it;
- size_t st = it->first;
- T a = it->second;
- if (st == 0) {
- if (it != start && a > T(0)) {
- out << '+' << a;
- } else {
- out << a;
- }
- continue;
- }
- if (a == T(1) && it == start) {
- } else {
- if (a == T(1) && it != start) {
- out << '+';
- } else {
- if (a == T(-1)) {
- out << '-';
- } else {
- if (a > T(0) && start != it) {
- out << '+' << a;
- } else {
- out << a;
- }
- }
- }
- }
- if (a != T(1) && a != T(-1)) {
- out << '*';
- }
- if (st == 1) {
- out << "x";
- } else {
- if (st != 0) {
- out << "x^" << st;
- }
- }
- } while (it != p.data.begin());
- return out;
- }
- friend Polynomial operator & (const Polynomial& p1, const Polynomial& p2) {
- Polynomial tmp(T(0));
- for (const auto& i : p1.data) {
- Polynomial st(1);
- for (size_t j = 0; j < i.first; ++j) {
- st *= p2;
- }
- tmp += st * i.second;
- }
- return tmp;
- }
- };
- int main() {
- int n, m;
- cin >> n >> m;
- vector <int> vec1(n);
- for (int i = 0; i < n; ++i) {
- cin >> vec1[i];
- }
- vector <int> vec2(m);
- for (int i = 0; i < m; ++i) {
- cin >> vec2[i];
- }
- Polynomial<int> x(vec1);
- Polynomial<int> y(vec2);
- cout << (x & y) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement