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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    int size = 0;
    ll res = 0;
    vector<string> s(N);
    map<char, ll> omomil;
    map<char, ll> omomir;
    vector<int> perm(10);
    vector<int> out;
    REP(i, 10) { perm[i] = (int)i; }
    REP(i, N) cin >> s[i];
    REP(i, N) {
      REP(j, s[i].size()) {
        if (omomil.count(s[i][s[i].size() - 1 - j]) == 0) {
          omomil[s[i][s[i].size() - 1 - j]] = 0;
          omomir[s[i][s[i].size() - 1 - j]] = 0;
          ++size;
        }
        if (i != N - 1)
          omomil[s[i][s[i].size() - 1 - j]] += pow(10, j);
        else
          omomir[s[i][s[i].size() - 1 - j]] += pow(10, j);
      }
    }
    map<char, ll>::iterator ite3 = omomil.begin();
    int foo = 0;
    while (ite3 != omomil.end()) {
      REP(i, N) {
        if (s[i].size() > 1 && s[i][0] == (*ite3).first) {
          out.pb(foo);
          // cout<<(*ite3).first<<":"<<foo<<endl;
          break;
        }
      }
      ++ite3;
      ++foo;
    }

    vector<ll> sugoil(size);
    vector<ll> sugoir(size);
    ite3 = omomil.begin();
    REP(i, size) {
      sugoil[i] = (*ite3).second;
      ++ite3;
    }
    ite3 = omomir.begin();
    REP(i, size) {
      sugoir[i] = (*ite3).second;
      ++ite3;
    }
    do {
      ll l = 0;
      ll r = 0;
      bool cont = false;
      int roc = 0;
      REP(i, out.size()) {
        if (perm[out[i]] == 0) {
          cont = true;
          break;
        }
      }
      if (cont)
        continue;
      while (roc < size) {
        l += perm[roc] * sugoil[roc];
        ++roc;
      }
      roc = 0;
      while (roc < size) {
        r += perm[roc] * sugoir[roc];
        ++roc;
      }
      if (l == r)
        ++res;
    } while (next_permutation(ALL(perm)));

    ll hoge = 1;
    REP(i, 10 - size) { hoge *= 10 - size - i; }
    cout << res / hoge << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    int size = 0;
    ll res = 0;
    vector<string> s(N);
    map<char, ll> omomil;
    map<char, ll> omomir;
    vector<int> perm(10);
    vector<int> out;
    REP(i, 10) { perm[i] = (int)i; }
    REP(i, N) cin >> s[i];
    REP(i, N) {
      REP(j, s[i].size()) {
        if (omomil.count(s[i][s[i].size() - 1 - j]) == 0) {
          omomil[s[i][s[i].size() - 1 - j]] = 0;
          omomir[s[i][s[i].size() - 1 - j]] = 0;
          ++size;
        }
        if (i != N - 1)
          omomil[s[i][s[i].size() - 1 - j]] += pow(10, j);
        else
          omomir[s[i][s[i].size() - 1 - j]] += pow(10, j);
      }
    }
    map<char, ll>::iterator ite3 = omomil.begin();
    int foo = 0;
    while (ite3 != omomil.end()) {
      REP(i, N) {
        if (s[i].size() > 1 && s[i][0] == (*ite3).first) {
          out.pb(foo);
          // cout<<(*ite3).first<<":"<<foo<<endl;
          break;
        }
      }
      ++ite3;
      ++foo;
    }
    do {
      ll l = 0;
      ll r = 0;
      bool cont = false;
      REP(i, out.size()) {
        if (perm[out[i]] == 0) {
          cont = true;
          break;
        }
      }
      if (cont)
        continue;
      map<char, ll>::iterator ite = omomil.begin();
      REP(i, size) {
        l += perm[i] * (*ite).second;
        ++ite;
      }
      ite = omomir.begin();
      REP(i, size) {
        r += perm[i] * (*ite).second;
        ++ite;
      }
      if (l == r)
        ++res;
    } while (next_permutation(ALL(perm)));

    ll hoge = 1;
    REP(i, 10 - size) { hoge *= 10 - size - i; }
    cout << res / hoge << endl;
  }
}