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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

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

int main() {
  int n;
  string str[20];
  int alpha[30];
  int al[30];
  int top[30];
  for (;;) {
    memset(alpha, 0, sizeof(alpha));
    memset(al, 0, sizeof(alpha));
    memset(top, 0, sizeof(alpha));
    n = in();
    if (n == 0)
      break;
    rep(i, n) cin >> str[i];
    rep(i, n) {
      int ten = 1;
      for (int j = str[i].size() - 1; j >= 0; j--, ten *= 10) {
        al[str[i][j] - 'A'] = 1;
        if (i < n - 1)
          alpha[str[i][j] - 'A'] += ten;
        else
          alpha[str[i][j] - 'A'] -= ten;
      }
      if (str[i].size() > 1)
        top[str[i][0] - 'A'] = 1;
    }
    int used[30];
    int m = 0;
    rep(i, 30) {
      if (al[i] > 0)
        used[m++] = i;
    }
    int res = 0;
    do {
      int bit = (1 << m) - 1;
      int q;
      for (; bit < 1 << 10;
           q = bit + (bit & -bit), bit = q | ((bit & ~q) / (bit & -bit)) >> 1) {
        if ((bit & 1) > 0 && top[used[0]] > 0)
          continue;
        int c = 0, sum = 0;
        rep(i, 10) {
          if ((bit & 1 << i) != 0) {
            sum += alpha[used[c++]] * i;
          }
        }
        if (sum == 0)
          res++;
      }
    } while (next_permutation(used, used + m));
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

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

int main() {
  int n;
  string str[20];
  int alpha[30];
  int al[30];
  int top[30];
  for (;;) {
    memset(alpha, 0, sizeof(alpha));
    memset(al, 0, sizeof(alpha));
    memset(top, 0, sizeof(alpha));
    n = in();
    if (n == 0)
      break;
    rep(i, n) cin >> str[i];
    rep(i, n) {
      int ten = 1;
      for (int j = str[i].size() - 1; j >= 0; j--, ten *= 10) {
        al[str[i][j] - 'A'] = 1;
        if (i < n - 1)
          alpha[str[i][j] - 'A'] += ten;
        else
          alpha[str[i][j] - 'A'] -= ten;
      }
      if (str[i].size() > 1)
        top[str[i][0] - 'A'] = 1;
    }
    string used = "";
    rep(i, 30) {
      if (al[i] > 0)
        used += 'A' + i;
    }
    int res = 0;
    do {
      int bit = (1 << used.size()) - 1;
      int q;
      for (; bit < 1 << 10;
           q = bit + (bit & -bit), bit = q | ((bit & ~q) / (bit & -bit)) >> 1) {
        if ((bit & 1) > 0 && top[used[0] - 'A'] > 0)
          continue;
        int c = 0, sum = 0;
        rep(i, 10) {
          if ((bit & 1 << i) != 0) {
            sum += alpha[used[c++] - 'A'] * i;
          }
        }
        if (sum == 0)
          res++;
      }
    } while (next_permutation(used.begin(), used.end()));
    cout << res << endl;
  }
  return 0;
}