Problem p02100 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <deque>
#include <iostream>
#include <set>
#include <sstream>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;
typedef long long ll;
const bool debug = false;

deque<string> split(string s) {
  deque<string> deq;
  int n = (int)s.size();
  int sp = 0;
  rep(i, n) {
    if (s[i] == 'x') {
      int ep = i;
      if (i + 1 < n && s[i + 1] == '^') {
        ep = i + 2;
        assert(i + 2 < n);
      }
      deq.push_back(s.substr(sp, ep - sp + 1));
      sp = ep + 1;
    }
  }
  if (sp != n) {
    deq.push_back(s.substr(sp));
  }
  return deq;
}

string getCoef(string s) {

  bool minus = (s[0] == '-');

  rep(i, (int)s.size()) {
    if (s[i] == '+' || s[i] == '-') {
      s[i] = ' ';
    }
  }
  stringstream ss2;
  ss2 << s;
  ss2 >> s;

  bool found = false;
  if (s[0] == 'x') {
    if (minus)
      return "-1";
    return "1";
  }
  rep(i, (int)s.size()) {
    if (s[i] == 'x') {
      s[i] = ' ';
      found = true;
    }
  }
  if (!found) {
    if (minus)
      s = "-" + s;
    return s;
  }
  stringstream ss;
  ss << s;
  string ret;
  ss >> ret;
  if (minus) {
    ret = "-" + ret;
  }
  return ret;
}

int getPcoef(string s) {
  rep(i, (int)s.size()) {
    if (s[i] == 'x') {
      if (i + 1 < (int)s.size() && s[i + 1] == '^') {
        return s[i + 2] - '0';
      } else {
        return 1;
      }
    }
  }
  return 0;
}

bool check(ll x, deque<string> &deq) {
  if (debug) {
    puts("");
    cout << "check [[ " << x << "]]" << endl;
  }
  ll res = 0;
  rep(i, (int)deq.size()) {
    ll cost = 0;
    string s = deq[i];
    string coef = getCoef(s);
    int pcoef = getPcoef(s);
    if (debug) {
      cout << deq[i] << " --> " << coef << " * x^[" << pcoef << "]" << endl;
    }

    ll v = atoll(coef.c_str());
    if (debug)
      cout << "v = " << v << endl;
    ll tmp = 1;
    rep(_, pcoef) tmp *= x;
    cost = v * tmp;
    if (debug) {
      cout << "cost = " << cost << endl;
    }
    res += cost;
  }
  return res == 0;
}

int getDegree(string s) {
  int ret = 0;
  int n = (int)s.size();
  rep(i, (int)s.size()) {
    if (s[i] == 'x') {
      if (i + 1 < n && s[i + 1] == '^') {
        assert(i + 2 < n);
        ret = max(ret, s[i + 2] - '0');
      } else {
        ret = max(ret, 1);
      }
    }
  }
  return ret;
}

void compute(string s) {
  int deg = getDegree(s);
  if (debug)
    cout << "degree = " << deg << endl;
  deque<string> deq = split(s);
  if (debug) {
    cout << "split --" << endl;
    rep(i, (int)deq.size()) { cout << deq[i] << " :: "; }
    puts("");
  }

  set<int> S;
  rep(i, (int)deq.size()) { S.insert(getPcoef(deq[i])); }
  assert((int)S.size() == (int)deq.size());

  /*
  cout << check(-2,deq) << endl;
  return;
  */

  vector<ll> ans;
  for (ll i = 1;; ++i) {
    if (check(i, deq))
      ans.push_back(i);
    if (check(-i, deq))
      ans.push_back(-i);
    if ((int)ans.size() >= deg)
      break;
  }
  sort(ans.begin(), ans.end(), greater<int>());
  rep(i, (int)ans.size()) {
    if (ans[i] == 0) {
      cout << "x";
    } else {
      cout << "(x";
      if (ans[i] < 0LL)
        cout << "+" << -ans[i] << ")";
      else
        cout << "-" << ans[i] << ")";
    }
  }
  puts("");
}

int main() {
  string s;
  cin >> s;
  compute(s);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <deque>
#include <iostream>
#include <set>
#include <sstream>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;
typedef long long ll;
const bool debug = false;

deque<string> split(string s) {
  deque<string> deq;
  int n = (int)s.size();
  int sp = 0;
  rep(i, n) {
    if (s[i] == 'x') {
      int ep = i;
      if (i + 1 < n && s[i + 1] == '^') {
        ep = i + 2;
        assert(i + 2 < n);
      }
      deq.push_back(s.substr(sp, ep - sp + 1));
      sp = ep + 1;
    }
  }
  if (sp != n) {
    deq.push_back(s.substr(sp));
  }
  return deq;
}

string getCoef(string s) {

  rep(i, (int)s.size()) {
    if (s[i] == '+') {
      s[i] = ' ';
    }
  }
  stringstream ss2;
  ss2 << s;
  ss2 >> s;

  bool found = false;
  if (s[0] == 'x')
    return "1";
  rep(i, (int)s.size()) {
    if (s[i] == 'x') {
      s[i] = ' ';
      found = true;
    }
  }
  if (!found) {
    return s;
  }
  stringstream ss;
  ss << s;
  string ret;
  ss >> ret;
  return ret;
}

int getPcoef(string s) {
  rep(i, (int)s.size()) {
    if (s[i] == 'x') {
      if (i + 1 < (int)s.size() && s[i + 1] == '^') {
        return s[i + 2] - '0';
      } else {
        return 1;
      }
    }
  }
  return 0;
}

bool check(ll x, deque<string> &deq) {
  if (debug) {
    puts("");
    cout << "check [[ " << x << "]]" << endl;
  }
  ll res = 0;
  rep(i, (int)deq.size()) {
    ll cost = 0;
    string s = deq[i];
    string coef = getCoef(s);
    int pcoef = getPcoef(s);
    if (debug) {
      cout << deq[i] << " --> " << coef << " * x^[" << pcoef << "]" << endl;
    }

    ll v = atoll(coef.c_str());
    if (debug)
      cout << "v = " << v << endl;
    ll tmp = 1;
    rep(_, pcoef) tmp *= x;
    cost = v * tmp;
    if (debug) {
      cout << "cost = " << cost << endl;
    }
    res += cost;
  }
  return res == 0;
}

int getDegree(string s) {
  int ret = 0;
  int n = (int)s.size();
  rep(i, (int)s.size()) {
    if (s[i] == 'x') {
      if (i + 1 < n && s[i + 1] == '^') {
        assert(i + 2 < n);
        ret = max(ret, s[i + 2] - '0');
      } else {
        ret = max(ret, 1);
      }
    }
  }
  return ret;
}

void compute(string s) {
  int deg = getDegree(s);
  if (debug)
    cout << "degree = " << deg << endl;
  deque<string> deq = split(s);
  if (debug) {
    cout << "split --" << endl;
    rep(i, (int)deq.size()) { cout << deq[i] << " :: "; }
    puts("");
  }

  set<int> S;
  rep(i, (int)deq.size()) { S.insert(getPcoef(deq[i])); }
  assert((int)S.size() == (int)deq.size());

  /*
  cout << check(-2,deq) << endl;
  return;
  */

  vector<ll> ans;
  for (ll i = 1;; ++i) {
    if (check(i, deq))
      ans.push_back(i);
    if (check(-i, deq))
      ans.push_back(-i);
    if ((int)ans.size() >= deg)
      break;
  }
  sort(ans.begin(), ans.end(), greater<int>());
  rep(i, (int)ans.size()) {
    if (ans[i] == 0) {
      cout << "x";
    } else {
      cout << "(x";
      if (ans[i] < 0LL)
        cout << "+" << -ans[i] << ")";
      else
        cout << "-" << ans[i] << ")";
    }
  }
  puts("");
}

int main() {
  string s;
  cin >> s;
  compute(s);
  return 0;
}