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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)
#define BIT(x, i) (((x) >> (i)) & 1)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vc> vvc;
typedef vector<vll> vvll;

char c[10];
int k[10];
bool not0[10];
// vi hoge;
int m;

int dfs(int idx, int used, int lr) {
  if (idx >= m) {
    // if (lr == 0) {
    //   REP(i, hoge.size() / 3) {
    //     LOG("%c:%d*%d=%d ", c[i], hoge[i * 3], hoge[i * 3 + 1], hoge[i * 3 +
    //     2]);
    //   }
    //   LOG("%d ", lr);
    //   LOG("\\n");
    // }
    return lr == 0;
  }
  int res = 0;
  FOR(i, not0[idx] ? 1 : 0, 10) {
    if (BIT(used, i))
      continue;
    // hoge.push_back(i);
    // hoge.push_back(k[idx]);
    // hoge.push_back(k[idx] * i);
    res += dfs(idx + 1, (used | 1 << i), lr + k[idx] * i);
    // hoge.pop_back();
    // hoge.pop_back();
    // hoge.pop_back();
  }
  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    fill_n((int *)k, 10, 0);
    fill_n((bool *)not0, 10, false);

    vector<string> str(n);
    map<char, pair<int, bool>> alpha;
    REP(i, n) {
      cin >> str[i];
      REP(j, str[i].length()) { alpha[str[i][j]] = {0, false}; }
    }
    m = (int)alpha.size();

    REP(i, n - 1) {
      for (int j = str[i].length() - 1, n = 1; j >= 0; j--, n *= 10) {
        alpha[str[i][j]].first += n;
      }
    }
    for (int j = str[str.size() - 1].length() - 1, n = 1; j >= 0;
         j--, n *= 10) {
      alpha[str[str.size() - 1][j]].first -= n;
    }
    REP(i, n) {
      if (str[i].length() > 1)
        alpha[str[i][0]].second = true;
    }

    int ii = 0;
    for (auto it = alpha.begin(); it != alpha.end(); it++, ii++) {
      c[ii] = it->first;
      k[ii] = it->second.first;
      not0[ii] = it->second.second;
    }
    // REP(i, m){
    //   LOG("%c %d %s\\n", c[i], k[i], not0[i] ? "true" : "false");
    // }

    // getchar(); getchar();
    cout << dfs(0, 0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int main() {
  int n;
  while (cin >> n, n) {
    map<char, int> cs;
    map<char, bool> nozero;

    REP(i, n) {
      string str;
      cin >> str;
      int k = 1;
      if (i == n - 1)
        k = -1;

      for (int j = (int)str.length() - 1; j >= 0; j--) {
        if (str.length() > 1)
          nozero[str[j]] |= (j == 0);
        cs[str[j]] += k;
        k *= 10;
      }
    }
    int m = (int)cs.size();

    //		for(auto p:cs){
    //			LOG("%c %d %d\\n",p.first, p.second, nozero[p.first]);
    //		}

    const int N = 10;
    int num[N];
    REP(i, N) { num[i] = i; }

    int cnt = 0;
    do {
      int sum = 0, i = 0;
      for (auto p : cs) {
        sum += p.second * num[i++];
      }
      if (sum == 0) {
        i = 0;
        //				for(auto p:cs){
        //					if(num[i++] == 0 &&
        //nozero[p.first]){ 						cnt --; 						LOG("noZero!!!!!\\n");
        //					}
        //				}
        cnt++;
        i = 0;
        //				for(auto p:cs){
        //					LOG("--- %c %d\\n", p.first,
        //num[i++]);
        //				}
        //				LOG("\\n");
      }

      sort(num + m, num + N, greater<int>());
    } while (next_permutation(num, num + N));

    cout << cnt << endl;
  }
}