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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int par[10];
bool istop[10];
int m;
int num[10];

int solve() {
  int res = 0;
  int S = (1 << m) - 1;
  while (S < (1 << 10)) {
    int p = 0;
    REP(i, 10)
    if (S >> i & 1)
      num[p++] = i;
    do {
      int sum = 0;
      bool ok = 1;
      REP(i, m) {
        if (num[i] == 0 && istop[i]) {
          ok = 0;
          break;
        }
        sum += num[i] * par[i];
      }
      if (ok && sum == 0)
        res++;
    } while (next_permutation(num, num + m));
    int x = S & -S, y = S + x;
    S = ((S & ~y) / x >> 1) | y;
  }
  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    m = 0;
    map<char, int> id;
    memset(par, 0, sizeof(par));
    memset(istop, 0, sizeof(istop));
    REP(i, n) {
      string s;
      cin >> s;
      int hoge = 1;
      for (int j = s.size() - 1; j >= 0; --j) {
        int index;
        if (id.count(s[j]))
          index = id[s[j]];
        else
          index = id[s[j]] = m++;
        if (i < n - 1)
          par[index] += hoge;
        else
          par[index] -= hoge;
        hoge *= 10;
      }
      if (s.size() > 1)
        istop[id[s[0]]] = 1;
    }
    cout << solve() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int par[10];
bool istop[10];
int m;
bool used[10];
int num[10];

int dfs(int pos) {
  if (pos == m) {
    int sum = 0;
    REP(i, m) {
      if (num[i] == 0 && istop[i])
        return 0;
      sum += num[i] * par[i];
    }
    if (sum)
      return 0;
    return 1;
  }
  int res = 0;
  REP(i, 10) {
    if (!used[i]) {
      used[i] = 1;
      num[pos] = i;
      res += dfs(pos + 1);
      used[i] = 0;
    }
  }
  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    m = 0;
    map<char, int> id;
    memset(par, 0, sizeof(par));
    memset(istop, 0, sizeof(istop));
    REP(i, n) {
      string s;
      cin >> s;
      int hoge = 1;
      for (int j = s.size() - 1; j >= 0; --j) {
        int index;
        if (id.count(s[j]))
          index = id[s[j]];
        else
          index = id[s[j]] = m++;
        if (i < n - 1)
          par[index] += hoge;
        else
          par[index] -= hoge;
        hoge *= 10;
      }
      if (s.size() > 1)
        istop[id[s[0]]] = 1;
    }
    memset(used, 0, sizeof(used));
    cout << dfs(0) << endl;
  }
}