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

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<string> s(n);
    REP(i, n) cin >> s[i];

    // 座圧しておく
    vector<char> v;
    REP(i, n) REP(j, s[i].size()) v.PB(s[i][j]);
    sort(ALL(v));
    v.erase(unique(ALL(v)), v.end());
    REP(i, n)
    REP(j, s[i].size()) s[i][j] =
        lower_bound(ALL(v), s[i][j]) - v.begin() + 'A';

    // leading-zero対策
    vector<bool> fro(n, false);
    REP(i, n) if (s[i].size() >= 2) fro[s[i][0] - 'A'] = true;

    // 係数を求める
    VI a(15, 0);
    REP(i, n) {
      int tmp = 1;
      for (int j = s[i].size() - 1; j >= 0; --j) {
        if (i == n - 1)
          a[s[i][j] - 'A'] -= tmp;
        else
          a[s[i][j] - 'A'] += tmp;
        tmp *= 10;
      }
    }

    map<PII, int> mp1, mp2;
    int m = v.size(), m2 = m / 2;

    function<void(int, int, int)> dfs1 = [&](int x, int sum, int used) {
      if (x == m2) {
        mp1[{sum, used}]++;
        return;
      }
      REP(i, 10) {
        if (fro[x] && i == 0)
          continue;
        if (used & 1 << i)
          continue;
        dfs1(x + 1, sum + i * a[x], used | 1 << i);
      }
    };

    function<void(int, int, int)> dfs2 = [&](int x, int sum, int used) {
      if (x == m) {
        mp2[{sum, used}]++;
        return;
      }
      REP(i, 10) {
        if (fro[x] && i == 0)
          continue;
        if (used & 1 << i)
          continue;
        dfs2(x + 1, sum + i * a[x], used | 1 << i);
      }
    };

    dfs1(0, 0, 0);
    dfs2(m2, 0, 0);

    vector<pair<PII, int>> vec1(ALL(mp1)), vec2(ALL(mp2));

    for (auto &i : vec2)
      i.first.first *= -1;
    // cout << vec1 << endl << vec2 << endl;

    int ans = 0;
    int l = 0, r = vec2.size() - 1;
    for (; l < vec1.size();) {
      while (r > 0 && vec1[l].first.first > vec2[r].first.first)
        r--;
      int val1 = vec1[l].first.first, val2 = vec2[r].first.first;
      // cout << l << " " << r << " " << val1 << " " << val2 << endl;
      if (val1 != val2) {
        while (l < vec1.size() && vec1[l].first.first == val1)
          l++;
        continue;
      }
      for (int i = l; i < vec1.size() && vec1[i].first.first == val1; ++i) {
        for (int j = r; j >= 0 && vec2[j].first.first == val2; --j) {
          if ((vec1[i].first.second & vec2[j].first.second) == 0) {
            ans += vec1[i].second * vec2[j].second;
          }
        }
      }
      while (l < vec1.size() && vec1[l].first.first == val1)
        l++;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<string> s(n);
    REP(i, n) cin >> s[i];

    // 座圧しておく
    vector<char> v;
    REP(i, n) REP(j, s[i].size()) v.PB(s[i][j]);
    sort(ALL(v));
    v.erase(unique(ALL(v)), v.end());
    cout << v << endl;
    REP(i, n)
    REP(j, s[i].size()) s[i][j] =
        lower_bound(ALL(v), s[i][j]) - v.begin() + 'A';

    // leading-zero対策
    vector<bool> fro(n, false);
    REP(i, n) if (s[i].size() >= 2) fro[s[i][0] - 'A'] = true;

    // 係数を求める
    VI a(15, 0);
    REP(i, n) {
      int tmp = 1;
      for (int j = s[i].size() - 1; j >= 0; --j) {
        if (i == n - 1)
          a[s[i][j] - 'A'] -= tmp;
        else
          a[s[i][j] - 'A'] += tmp;
        tmp *= 10;
      }
    }

    map<int, int> mp1[1LL << 10], mp2[1LL << 10];
    int m = v.size(), m2 = m / 2;

    function<void(int, int, int)> dfs1 = [&](int x, int sum, int used) {
      if (x == m2) {
        mp1[used][sum]++;
        return;
      }
      REP(i, 10) {
        if (fro[x] && i == 0)
          continue;
        if (used & 1 << i)
          continue;
        dfs1(x + 1, sum + i * a[x], used | 1 << i);
      }
    };

    function<void(int, int, int)> dfs2 = [&](int x, int sum, int used) {
      if (x == m) {
        mp2[used][sum]++;
        return;
      }
      REP(i, 10) {
        if (fro[x] && i == 0)
          continue;
        if (used & 1 << i)
          continue;
        dfs2(x + 1, sum + i * a[x], used | 1 << i);
      }
    };

    dfs1(0, 0, 0);
    dfs2(m2, 0, 0);

    int ans = 0;
    REP(bit, 1 << 10) {
      for (auto i : mp1[bit]) {
        REP(j, 1 << 10) {
          if (!(bit & j)) {
            ans += i.second * mp2[j][-i.first];
          }
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}