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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

vector<string> vs;

ll power(ll a, ll b /*,ll mod*/) {
  ll ret = 1;
  if (b > 0) {
    ret = power(a, b / 2 /*,mod*/);
    if (b % 2 == 0)
      ret = (ret * ret) /*%mod*/;
    else
      ret = (((ret * ret) /*%mod*/) * a) /*%mod*/;
  }
  return ret;
}

set<char> cant_zero;

map<char, ll> solve(vector<string> vs) {
  map<char, ll> mp;
  rep(i, vs.size() - 1) {
    rep(j, vs[i].size()) { mp[vs[i][j]] += power(10, vs[i].size() - j - 1); }
  }

  string right = vs[vs.size() - 1];
  rep(i, right.size()) { mp[right[i]] -= power(10, right.size() - i - 1); }

  return mp;
}

vector<pair<ll, ll>> getValue(vector<char> v, map<char, ll> mp) {
  vector<pair<ll, ll>> ret;
  int en = power(10, v.size());
  rep(i, en) {
    int num = i;
    vector<int> t;
    rep(i, v.size()) {
      t.pb(num % 10);
      num /= 10;
    }
    reverse(all(t));
    if (set<int>(all(t)).size() != v.size())
      continue;

    bool f = false;
    rep(i, t.size()) {
      if (t[i] == 0 && cant_zero.count(v[i]))
        f = true;
    }
    if (f)
      continue;

    ll tmp = 0;
    ll bit = 0;
    rep(i, t.size()) {
      tmp += mp[v[i]] * t[i];
      bit += 1 << t[i];
    }
    ret.pb(make_pair(tmp, bit));
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    vs.clear();
    vs.resize(n);
    rep(i, n) cin >> vs[i];

    cant_zero.clear();
    rep(i, vs.size()) {
      if (vs[i].size() == 1)
        continue;
      cant_zero.insert(vs[i][0]);
    }

    map<char, ll> mp = solve(vs);

    int m = mp.size();

    vector<char> vl, vr;
    int c = 0;
    for (auto &e : mp) {
      if (c < m / 2)
        vl.pb(e.first);
      else
        vr.pb(e.first);
      c++;
    }

    vector<pair<ll, ll>> a = getValue(vl, mp);
    vector<pair<ll, ll>> b = getValue(vr, mp);

    int ans = 0;

    sort(all(a));
    sort(all(b));

    rep(i, a.size()) {
      ll val = a[i].first;
      ll stat = a[i].second;

      int pos = lower_bound(all(b), pair<ll, ll>(-1 * val, 0)) - b.begin();

      for (int j = pos; j < b.size(); j++) {
        if (b[j].first != -1 * val)
          break;

        if ((b[j].second & stat) == 0) {
          //                    cout<<bitset<10>(stat)<<"
          //                    "<<bitset<10>(b[j].second)<<endl;
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

bool isValid(string s) {
  if (s.size() == 1)
    return true;
  if (s[0] == '0')
    return false;
  return true;
}
int n;

int dfs(const vector<string> &ex, map<char, int> &mp, int f, string cal,
        bool used[10]) {
  int ret = 0;

  if (!f) {
    int ans;
    string m = "";
    rep(i, ex[ex.size() - 1].size()) { m += cal[mp[ex[ex.size() - 1][i]]]; }
    if (!isValid(m))
      return 0;
    ans = stoi(m);
    int sum = 0;
    rep(i, ex.size() - 1) {
      string tmp = "";
      rep(j, ex[i].size()) { tmp += cal[mp[ex[i][j]]]; }
      if (!isValid(tmp))
        return 0;
      sum += stoi(tmp);
      if (sum > ans)
        return 0;
    }
    if (sum == ans)
      return 1;
    else
      return 0;
  }

  rep(i, 10) {
    if (!used[i]) {
      used[i] = true;
      ret += dfs(ex, mp, f - 1, cal + (char)('0' + i), used);
      used[i] = false;
    }
  }
  return ret;
}

int main() {
  while (cin >> n && n) {
    vector<string> ex;
    rep(i, n) {
      string s;
      cin >> s;
      ex.pb(s);
    }

    set<char> st;
    rep(i, n) rep(j, ex[i].size()) st.insert(ex[i][j]);

    map<char, int> mp;
    int t = 0;
    for (auto &c : st)
      mp[c] = t++;

    bool used[10] = {};
    int sum = dfs(ex, mp, st.size(), "", used);

    cout << sum << endl;
  }
}