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

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string P[20];
int N, c[26], F[20], r[20];
char d[26];
int cnt1 = 0;
int X10[3628800];
int fact(int n) {
  int sum1 = 1;
  for (int i = 1; i <= n; i++)
    sum1 *= i;
  return sum1;
}
int main() {
  int a2[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, cnt4 = 0;
  do {
    for (int i = 0; i < 10; i++) {
      if (!a2[i])
        X10[cnt4] = i;
    }
    cnt4++;
  } while (next_permutation(a2, a2 + 10));
  while (true) {
    for (int i = 0; i <= 10; i++) {
      F[i] = fact(i);
      r[i] = 0;
    }
    for (int i = 0; i < 20; i++) {
      P[i] = "";
    }
    for (int i = 0; i < 26; i++) {
      c[i] = 0;
      d[i] = ' ';
    }
    cnt1 = 0;
    cin >> N;
    if (N == 0) {
      break;
    }
    for (int i = 0; i < N; i++) {
      cin >> P[i];
      for (int j = 0; j < P[i].size(); j++) {
        c[P[i][j] - 'A']++;
      }
    }
    for (int i = 0; i < 26; i++) {
      if (c[i] >= 1) {
        d[i] = cnt1 + 'A';
        cnt1++;
      } else {
        d[i] = 'Z';
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < P[i].size(); j++) {
        P[i][j] = d[P[i][j] - 'A'];
      }
      if (P[i].size() >= 2) {
        r[P[i][0] - 'A'] = 1;
      }
    }
    int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int cnt2 = 0, cnt3 = 0, V = 0, sum = 0;
    int R[100];
    for (int i = 0; i < N; i++)
      R[i] = P[i].size();
    do {
      if (cnt2 % F[10 - cnt1]) {
        cnt2++;
        continue;
      }
      if (r[X10[cnt2]] == 1) {
        cnt2++;
        continue;
      }
      sum = 0;
      for (int i = 0; i < N; i++) {
        int Q = 0;
        for (int j = 0; j < R[i]; j++)
          Q = Q * 10 + a[P[i][j] - 'A'];
        if (i != N - 1)
          sum += Q;
        else if (sum == Q)
          cnt3++;
      }
    E:;
      cnt2++;
    } while (next_permutation(a, a + 10));
    cout << cnt3 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string P[20];
int N, c[26], F[20], r[20];
char d[26];
int cnt1 = 0;
int X10[3628800];
int fact(int n) {
  int sum1 = 1;
  for (int i = 1; i <= n; i++)
    sum1 *= i;
  return sum1;
}
int main() {
  int a2[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, cnt4 = 0;
  do {
    for (int i = 0; i < 10; i++) {
      if (!a2[i])
        X10[cnt4] = i;
    }
    cnt4++;
  } while (next_permutation(a2, a2 + 10));
  while (true) {
    for (int i = 0; i <= 10; i++) {
      F[i] = fact(i);
      r[i] = 0;
    }
    for (int i = 0; i < 20; i++) {
      P[i] = "";
    }
    for (int i = 0; i < 26; i++) {
      c[i] = 0;
      d[i] = ' ';
    }
    cnt1 = 0;
    cin >> N;
    if (N == 0) {
      break;
    }
    for (int i = 0; i < N; i++) {
      cin >> P[i];
      for (int j = 0; j < P[i].size(); j++) {
        c[P[i][j] - 'A']++;
      }
    }
    for (int i = 0; i < 26; i++) {
      if (c[i] >= 1) {
        d[i] = cnt1 + 'A';
        cnt1++;
      } else {
        d[i] = 'Z';
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < P[i].size(); j++) {
        P[i][j] = d[P[i][j] - 'A'];
      }
      if (P[i].size() >= 2) {
        r[P[i][0] - 'A'] = 1;
      }
    }
    int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int cnt2 = 0, cnt3 = 0, V = 0, sum = 0;
    do {
      if (cnt2 % F[10 - cnt1]) {
        cnt2++;
        continue;
      }
      if (r[X10[cnt2]] == 1) {
        cnt2++;
        continue;
      }
      sum = 0;
      for (int i = 0; i < N; i++) {
        int Q = 0;
        for (int j = 0; j < P[i].size(); j++)
          Q = Q * 10 + a[P[i][j] - 'A'];
        if (i != N - 1)
          sum += Q;
        else if (sum == Q)
          cnt3++;
      }
    E:;
      cnt2++;
    } while (next_permutation(a, a + 10));
    cout << cnt3 << endl;
  }
  return 0;
}