Language: C++

#include <bits/stdc++.h>
#define reo(i,n) for(int i=0;i<n;i++)
using namespace std;
struct Poly {
  vector<int> c;
  Poly() : c(1, 1) {;}
  Poly(int s, int i) : c(s, i) {;}
  Poly(vector<int> v) {c = v;}
  int size() const { return c.size(); }
  void print(){
    int flag = 0;
    for(int i=size()-1;i>=1;i--){
      if( c[i] ){
        flag = 1;
        if( c[i] > 0 && i != size() - 1 ) cout<<'+';
        if( c[i] == -1 ) cout<<'-';
        else if( c[i] != 1 ) cout<<c[i];
        cout<<'x';
        if( i != 1 ) cout<<"^"<<i;
      }
    }
    if( c[0] ){
      if( flag && c[0] > 0 ) cout<<"+";
      cout<<c[0];
    }
    cout<<endl;
  }
  void normalize() {
    int s = 1;
    for (int i = size() - 1; i >= 0; i--)
      if (c[i] != 0) { s = i + 1; break; }
    c.resize(s);
  }
  int operator[](int idx) const {
    assert(idx >= 0 && idx < size());
    return c[idx];
  }
  int &operator[](int idx) {
    assert(idx >= 0 && idx < size());
    return c[idx];
  }
  Poly operator+(const Poly &c7) const {
    Poly ret = *this;
    ret.c.resize(max(size(), c7.size()));
    REP(i,c7.size()) ret[i] = ret[i] + c7[i];
    ret.normalize();
    return ret;
  }
  Poly operator-() const {
    Poly ret = *this;
    REP(i,ret.size()) ret[i] = -ret[i];
    return ret;
  }
  Poly operator-(const Poly &c7) const { return *this + (-c7); }
  Poly operator*(const Poly &c7) const {
    Poly ret(size() + c7.size(), 0);
    REP(i,size()) REP(j,c7.size()) ret[i+j] += c[i] * c7[j];
    ret.normalize();
    return ret;
  }
  Poly operator/(const Poly &c7) const { return divmod(c7).first; }
  Poly operator%(const Poly &c7) const { return divmod(c7).second; }
  Poly operator+=(const Poly &c7) { return *this = *this + c7; }
  Poly operator-=(const Poly &c7) { return *this = *this - c7; }
  Poly operator*=(const Poly &c7) { return *this = *this * c7; }
  Poly operator/=(const Poly &c7) { return *this = *this / c7; }
  Poly operator%=(const Poly &c7) { return *this = *this % c7; }
  void minus() {
    int g = abs(c.back());
    if (g > 0) {
      for (int j: c)
        if (j != 0) g = __gcd(g, abs(j));
      if (c.back() < 0) g = -g;
      for (int &j: c) j /= g;
    }
  }
  pair<Poly, Poly> divmod(const Poly &c7) const {
    int ls = size(), rs = c7.size(), s = ls - rs + 1;
    if (s < 0) { return make_pair(Poly(1, 0), *this); }
    Poly div(s, 0), rest = *this;
    assert(c7[rs-1] != 0);
    REP(i,s) {
      if (rest[ls-i-1] % c7[rs-1] != 0)      //
        for (auto &j: rest.c) j *= c7[rs-1]; //
      int d = rest[ls-i-1] / c7[rs-1];
      div[s - i - 1] = d;
      REP(j,rs) rest[ls-i-j-1] -= c7[rs-j-1] * d;
    }
    div.normalize(); rest.normalize();
    rest.minus(); //
    return make_pair(div, rest);
  }
  Poly pow(int power) const {
    Poly base = *this;
    Poly ret;
    while (power > 0) {
      if (power & 1) ret *= base;
      base *= base; power >>= 1;
    }
    return ret;
  }
  Poly differential() const {
    if (c.size() == 1) return Poly(1, 0);
    Poly ret(c.size() - 1, 0);
    for (int i = 1; i < (int)c.size(); ++i) ret[i-1] = c[i] * i;
    return ret;
  }
  int calc(int x) const {
    int ans = 0;
    for (int i = c.size() - 1; i >= 0; i--) ans *= x, ans += c[i];
    return ans;
  }
  Poly PolyGCD(Poly a, Poly b) {
    if (b.size() == 1 && b[0] == 0) { a.minus(); return a; }
    return PolyGCD(b, a % b);
  }
  Poly Poly_GCD(Poly a){
    Poly b=*this;
    return PolyGCD(a,b);
  }
};
struct Poly_parse{
  string s;
  Poly g_A(){
    if(s[p]=='('){
      p++;
      Poly res = bnf1();
      p++;
      int num2=0;
      if(s[p]=='^'){
        p++;
        while('0'<=s[p]&&s[p]<='9') num2 = num2 * 10 + s[p++] - '0';
      }
      if(num2)res = res.pow(num2);
      return res;
    }
    else{
      int num = 0;
      while('0'<=s[p]&&s[p]<='9') num = num * 10 + s[p++] - '0';
      if( p <s.size() && s[p] == 'x' ){
        if(num==0) num = 1;
        p++;
        int num2 = 0;
        vector<int>v;
        if(p<s.size()&&s[p]=='^'){
          p++;
          while('0'<=s[p]&&s[p]<='9') num2 = num2 * 10 + s[p++] - '0';
        }
        if(num2==0){
          v.resize(2,0);
          v[1]=num;
          Poly res(v);
          return res;
        }
        else{
          v.resize(num2+1,0);
          v[num2] = num;
          Poly res(v);
          return res;
        }
      }
      else{
        Poly res(1,num);
        return res;
      }
    }
  }
  Poly bnf2(){
    Poly res=g_A();
    while(p<s.size()&&s[p]=='*'){
      int t=p++;
      if(s[t]=='*')res = res*g_A();
    }
    return res;
  }
  Poly bnf1(){
    Poly res=bnf2();
    while(p<s.size()&&s[p]=='+'||s[p]=='-'){
      int t=p++;
      if(s[t]=='+')res = res+bnf2();
      if(s[t]=='-')res = res-bnf2();
    }
    return res;
  }
  string convert(string ss){
    for(int i=0;i<(int)ss.size()-1;i++){
      if(ss[i+1]=='('&&!(ss[i]=='+'||ss[i]=='-'||ss[i]=='*')){
        ss= ss.substr(0,i+1)+'*'+ss.substr(i+1);
      }
    }
    if(ss[0]=='-'){
      ss="0"+ss;
    }
    return ss;
  }
  Poly parse(string x){
    s=convert(x);
    p=0;
    return bnf1();
  }
}

int main(){
  string s;
  Poly_parse A;
  while(cin>>s,s!="."){
    Poly a=A.parse(s);
    cin>>s;
    Poly b=A.parse(s);
    Poly c=a.Poly_GCD(b);
    c.print();
  }
}


Compiler output:
a.cc: In member function 'Poly Poly::operator+(const Poly&) const':
a.cc:45:9: error: 'i' was not declared in this scope
   45 |     REP(i,c7.size()) ret[i] = ret[i] + c7[i];
      |         ^
a.cc:45:5: error: 'REP' was not declared in this scope
   45 |     REP(i,c7.size()) ret[i] = ret[i] + c7[i];
      |     ^~~
a.cc: In member function 'Poly Poly::operator-() const':
a.cc:51:9: error: 'i' was not declared in this scope
   51 |     REP(i,ret.size()) ret[i] = -ret[i];
      |         ^
a.cc:51:5: error: 'REP' was not declared in this scope
   51 |     REP(i,ret.size()) ret[i] = -ret[i];
      |     ^~~
a.cc: In member function 'Poly Poly::operator*(const Poly&) const':
a.cc:57:9: error: 'i' was not declared in this scope
   57 |     REP(i,size()) REP(j,c7.size()) ret[i+j] += c[i] * c7[j];
      |         ^
a.cc:57:5: error: 'REP' was not declared in this scope
   57 |     REP(i,size()) REP(j,c7.size()) ret[i+j] += c[i] * c7[j];
      |     ^~~
a.cc: In member function 'std::pair<Poly, Poly> Poly::divmod(const Poly&) const':
a.cc:82:9: error: 'i' was not declared in this scope
   82 |     REP(i,s) {
      |         ^
a.cc:82:5: error: 'REP' was not declared in this scope
   82 |     REP(i,s) {
      |     ^~~
a.cc: At global scope:
a.cc:201:2: error: expected ';' after struct definition
  201 | }
      |  ^
      |  ;
a.cc: In member function 'Poly Poly_parse::g_A()':
a.cc:125:10: error: 'p' was not declared in this scope
  125 |     if(s[p]=='('){
      |          ^
a.cc: In member function 'Poly Poly_parse::bnf2()':
a.cc:170:11: error: 'p' was not declared in this scope
  170 |     while(p<s.size()&&s[p]=='*'){
      |           ^
a.cc: In member function 'Poly Poly_parse::bnf1()':
a.cc:178:11: error: 'p' was not declared in this scope
  178 |     while(p<s.size()&&s[p]=='+'||s[p]=='-'){
      |           ^
a.cc: In member function 'Poly Poly_parse::parse(std::string)':
a.cc:198:5: error: 'p' was not declared in this scope
  198 |     p=0;
      |     ^
a.cc: In member function 'Poly Poly_parse::g_A()':
a.cc:124:8: warning: control reaches end of non-void function [-Wreturn-type]
  124 |   Poly g_A(){
      |        ^~~