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

#include <bits/stdc++.h>
using namespace std;

typedef long long lli;

const lli MAXN = 12;
const lli ten[9] = {1,      10,      100,      1000,     10000,
                    100000, 1000000, 10000000, 100000000};
lli N, ans;
string S[MAXN];
lli num[128], mini[MAXN], maxi[MAXN];

void rec(lli i, lli j, lli sum, lli used) {
  if (j == S[i].size())
    ++i, j = 0;
  if (i == N) {
    ans += !sum;
    return;
  }
  if (sum < 0)
    return;
  if (i && !j) {
    lli low = 0, high = 0;
    for (lli k = i; k < N; ++k) {
      low += mini[k];
      high += maxi[k];
    }
    if (sum - low < 0)
      return;
    if (sum - high > 0)
      return;
  }
  if (num[S[i][j]] == -1) {
    for (lli d = S[i].size() - 1 && !j; d < 10; ++d) {
      if (used >> d & 1)
        continue;
      num[S[i][j]] = d;
      lli nsum = sum;
      if (i)
        nsum -= ten[S[i].size() - j - 1] * d;
      else
        nsum += ten[S[i].size() - j - 1] * d;
      rec(i, j + 1, nsum, used | (1 << d));
    }
    num[S[i][j]] = -1;
  } else {
    lli d = num[S[i][j]];
    lli nsum = sum;
    if (S[i].size() - 1 && !j && !d)
      return;
    if (i)
      nsum -= ten[S[i].size() - j - 1] * d;
    else
      nsum += ten[S[i].size() - j - 1] * d;
    rec(i, j + 1, nsum, used);
  }
}

struct LT {
  bool operator()(const string &a, const string &b) const {
    return (a.size() != b.size() ? a.size() > b.size() : a > b);
  }
};

int main() {
  while (cin >> N && N) {
    for (lli i = 0; i < N; ++i)
      cin >> S[i];
    swap(S[0], S[N - 1]);
    // sort(S+1, S+N, LT());
    for (lli i = 0; i < N; ++i) {
      map<char, lli> d;
      maxi[i] = 0;
      for (lli j = 0, used = 0; j < S[i].size(); ++j) {
        if (!d.count(S[i][j])) {
          for (lli k = 9; k >= (S[i].size() - 1 && !j); --k) {
            if (used >> k & 1)
              continue;
            used |= 1 << k;
            d[S[i][j]] = k;
            break;
          }
        }
        maxi[i] = maxi[i] * 10 + d[S[i][j]];
      }
    }

    for (lli i = 0; i < N; ++i) {
      map<char, lli> d;
      mini[i] = 0;
      for (lli j = 0, used = 0; j < S[i].size(); ++j) {
        if (!d.count(S[i][j])) {
          for (lli k = S[i].size() - 1 && !j; k < 10; ++k) {
            if (used >> k & 1)
              continue;
            used |= 1 << k;
            d[S[i][j]] = k;
            break;
          }
        }
        mini[i] = mini[i] * 10 + d[S[i][j]];
      }
    }

    ans = 0;
    memset(num, -1, sizeof(num));
    rec(0, 0, 0, 0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long lli;

const lli MAXN = 12;
const lli ten[9] = {1,      10,      100,      1000,     10000,
                    100000, 1000000, 10000000, 100000000};
lli N, ans;
string S[MAXN];
lli num[128], mini[MAXN], maxi[MAXN];

void rec(lli i, lli j, lli sum, lli used) {
  if (j == S[i].size())
    ++i, j = 0;
  if (i == N) {
    ans += !sum;
    return;
  }
  if (sum < 0)
    return;
  if (i && !j) {
    lli low = 0, high = 0;
    for (lli k = i; k < N; ++k) {
      low += mini[k];
      high += maxi[k];
    }
    if (sum - low < 0)
      return;
    if (sum - high > 0)
      return;
  }
  if (num[S[i][j]] == -1) {
    for (lli d = S[i].size() - 1 && !j; d < 10; ++d) {
      if (used >> d & 1)
        continue;
      num[S[i][j]] = d;
      lli nsum = sum;
      if (i)
        nsum -= ten[S[i].size() - j - 1] * d;
      else
        nsum += ten[S[i].size() - j - 1] * d;
      rec(i, j + 1, nsum, used | (1 << d));
    }
    num[S[i][j]] = -1;
  } else {
    lli d = num[S[i][j]];
    lli nsum = sum;
    if (i)
      nsum -= ten[S[i].size() - j - 1] * d;
    else
      nsum += ten[S[i].size() - j - 1] * d;
    rec(i, j + 1, nsum, used);
  }
}

struct LT {
  bool operator()(const string &a, const string &b) const {
    return a.size() < b.size();
  }
};

int main() {
  while (cin >> N && N) {
    for (lli i = 0; i < N; ++i)
      cin >> S[i];
    swap(S[0], S[N - 1]);
    sort(S + 1, S + N, LT());
    for (lli i = 0; i < N; ++i) {
      map<char, lli> d;
      maxi[i] = 0;
      for (lli j = 0, used = 0; j < S[i].size(); ++j) {
        if (!d.count(S[i][j])) {
          for (lli k = 9; k >= (S[i].size() - 1 && !j); --k) {
            if (used >> k & 1)
              continue;
            used |= 1 << k;
            d[S[i][j]] = k;
            break;
          }
        }
        maxi[i] = maxi[i] * 10 + d[S[i][j]];
      }
    }

    for (lli i = 0; i < N; ++i) {
      map<char, lli> d;
      mini[i] = 0;
      for (lli j = 0, used = 0; j < S[i].size(); ++j) {
        if (!d.count(S[i][j])) {
          for (lli k = S[i].size() - 1 && !j; k < 10; ++k) {
            if (used >> k & 1)
              continue;
            used |= 1 << k;
            d[S[i][j]] = k;
            break;
          }
        }
        mini[i] = mini[i] * 10 + d[S[i][j]];
      }
    }

    ans = 0;
    memset(num, -1, sizeof(num));
    rec(0, 0, 0, 0);
    cout << ans << endl;
  }
  return 0;
}