alexjercan/bugnet huggingface dataset:
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define int long long
using namespace std;

int n, m, ans;
string vs[13];
int c[10], ca[10];
bool notZero[10];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> n, n) {
    map<char, int> mp;
    m = 0;
    ans = 0;
    memset(c, 0, sizeof(c));
    memset(ca, 0, sizeof(ca));
    memset(notZero, 0, sizeof(notZero));
    rep(i, 0, n) {
      cin >> vs[i];
      rep(j, 0, vs[i].size()) {
        if (!mp.count(vs[i][j])) {
          mp[vs[i][j]] = m++;
        }
      }
      if (vs[i].size() != 1) {
        notZero[mp[vs[i][0]]] = true;
      }
    }
    rep(i, 0, n) {
      int tmp = 1;
      for (int j = vs[i].size() - 1; j >= 0; j--) {
        if (i != n - 1)
          c[mp[vs[i][j]]] += tmp;
        else
          ca[mp[vs[i][j]]] += tmp;
        tmp *= 10;
      }
    }
    vector<int> p(10);
    rep(i, 0, 10) p[i] = i;
    do {
      bool f = true;
      int sum = 0, suma = 0;
      rep(i, 0, m) {
        if (p[i] == 0 && notZero[i]) {
          f = false;
          break;
        }
        sum += p[i] * c[i];
        suma += p[i] * ca[i];
      }
      if (sum != suma)
        f = false;
      if (f)
        ans++;
    } while (next_permutation(p.begin(), p.end()));
    int l = 10 - m;
    int div = 1;
    rep(i, 1, l + 1) div *= i;
    cout << ans / div << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define int long long
using namespace std;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  string vs[13];
  while (cin >> n, n) {
    map<char, int> mp;
    int m = 0;
    rep(i, 0, n) {
      cin >> vs[i];
      rep(j, 0, vs[i].size()) {
        if (!mp.count(vs[i][j])) {
          mp[vs[i][j]] = m++;
        }
      }
    }
    vector<int> c(m), ca(m);
    rep(i, 0, n) {
      int tmp = 1;
      for (int j = vs[i].size() - 1; j >= 0; j--) {
        if (i != n - 1)
          c[mp[vs[i][j]]] += tmp;
        else
          ca[mp[vs[i][j]]] += tmp;
        tmp *= 10;
      }
    }
    int l = 10 - m;
    int skip = 1;
    rep(i, 1, l + 1) skip *= i;
    vector<int> p(10);
    rep(i, 0, 10) p[i] = i;
    set<string> st;
    int count = -1;
    do {
      count++;
      if (count % skip)
        continue;
      bool f = true;
      string t = "";
      rep(i, 0, m) { t += '0' + p[i]; }
      if (st.count(t))
        continue;
      int sum = 0, suma = 0;
      rep(i, 0, n) {
        int now = p[mp[vs[i][0]]];
        if (vs[i].size() != 1 && now == 0) {
          f = false;
          break;
        }
      }
      rep(i, 0, m) {
        sum += p[i] * c[i];
        suma += p[i] * ca[i];
      }
      if (sum != suma)
        f = false;
      if (f) {
        st.insert(t);
      }
    } while (next_permutation(p.begin(), p.end()));
    cout << st.size() << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Runtime Error

//=================================
// Created on: 2018/07/04 15:12:12
//=================================
#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int N;;) {
    cin >> N;
    if (N == 0) {
      break;
    }
    vector<int> coeff1(10, 0);
    vector<int> coeff2(10, 0);
    set<int> nz;
    map<char, int> ind;
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      reverse(s.begin(), s.end());
      for (int j = 0, d = 1; j < s.size(); j++, d *= 10) {
        const char c = s[j];
        if (ind.find(c) == ind.end()) {
          const int s = ind.size();
          ind[c] = s;
        }
        const int in = ind[c];
        (i < N - 1 ? coeff1 : coeff2).at(in) += d;
        if (j == s.size() - 1 and s.size() > 1) {
          nz.insert(in);
        }
      }
    }
    assert(ind.size() <= 10);
    vector<int> num{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int div = 1;
    for (int i = 0; i < 10 - (int)ind.size(); i++) {
      div *= (i + 1);
    }
    int ans = 0;
    do {
      bool ok = true;
      for (const int nzz : nz) {
        if (num[nzz] == 0) {
          ok = false;
          break;
        }
      }
      if (not ok) {
        continue;
      }
      int left = 0, right = 0;
      for (int i = 0; i < ind.size(); i++) {
        left += coeff1[i] * num[i], right += coeff2[i] * num[i];
      }
      if (left == right) {
        ans++;
      }
    } while (next_permutation(num.begin(), num.end()));
    cout << ans / div << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

//=================================
// Created on: 2018/07/04 15:12:12
//=================================
#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int N;;) {
    cin >> N;
    if (N == 0) {
      break;
    }
    vector<int> coeff1(10, 0);
    vector<int> coeff2(10, 0);
    set<int> nz;
    map<char, int> ind;
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      reverse(s.begin(), s.end());
      for (int j = 0, d = 1; j < s.size(); j++, d *= 10) {
        const char c = s[j];
        if (ind.find(c) == ind.end()) {
          ind[c] = ind.size();
        }
        const int in = ind[c];
        (i < N - 1 ? coeff1 : coeff2).at(in) += d;
        if (j == s.size() - 1 and s.size() > 1) {
          nz.insert(in);
        }
      }
    }
    assert(ind.size() <= 10);
    vector<int> num{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int div = 1;
    for (int i = 0; i < 10 - (int)ind.size(); i++) {
      div *= (i + 1);
    }
    int ans = 0;
    do {
      bool ok = true;
      for (const int nzz : nz) {
        if (num[nzz] == 0) {
          ok = false;
          break;
        }
      }
      if (not ok) {
        continue;
      }
      int left = 0, right = 0;
      for (int i = 0; i < ind.size(); i++) {
        left += coeff1[i] * num[i], right += coeff2[i] * num[i];
      }
      if (left == right) {
        ans++;
      }
    } while (next_permutation(num.begin(), num.end()));
    cout << ans / div << endl;
  }
  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, h[100];
vector<char> s;
set<char> first;
vector<string> vs(20);
bool used[10];

bool calc() {
  int left, item, ans;
  left = 0;
  ans = 0;
  for (int i = 0; i < vs[n - 1].size(); i++) {
    ans = 10 * ans + h[vs[n - 1][i]];
  }
  for (int i = 0; i < n - 1; ++i) {
    item = 0;
    for (int j = 0; j < vs[i].size(); ++j) {
      item = 10 * item + h[vs[i][j]];
    }
    left = left + item;
    if (left > ans)
      return false;
  }
  return left == ans;
}

int dfs(int size = 0) {
  if (size == s.size()) {
    return calc();
  }
  int res;
  res = 0;
  for (int i = 0; i < 10; ++i) {
    if (used[i])
      continue;
    if (i == 0 and first.find(s[size]) != first.end())
      continue;
    h[s[size]] = i;
    used[i] = true;
    res += dfs(size + 1);
    used[i] = false;
  }
  return res;
}

int main() {
  while (std::cin >> n, n) {
    s.clear();
    first.clear();
    for (int i = 0; i < n; ++i) {
      std::cin >> vs[i];
      if (vs[i].size() != 1)
        first.insert(vs[i][0]);
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < vs[i].size(); ++j) {
        s.push_back(vs[i][j]);
      }
    }
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    std::cout << dfs() << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, h[100];
vector<char> s;
set<char> first;
vector<string> vs(20);
bool used[10];

bool calc() {
  int left, item;
  left = 0;
  for (int i = 0; i < n; ++i) {
    item = 0;
    for (int j = 0; j < vs[i].size(); ++j) {
      item = 10 * item + h[vs[i][j]];
    }
    left = left + item;
  }
  return left == 2 * item;
}

int dfs(int size = 0) {
  if (size == s.size()) {
    return calc();
  }
  int res;
  res = 0;
  for (int i = 0; i < 10; ++i) {
    if (used[i])
      continue;
    if (i == 0 and first.find(s[size]) != first.end())
      continue;
    h[s[size]] = i;
    used[i] = true;
    res += dfs(size + 1);
    used[i] = false;
  }
  return res;
}

int main() {
  while (std::cin >> n, n) {
    s.clear();
    first.clear();
    for (int i = 0; i < n; ++i) {
      std::cin >> vs[i];
      if (vs[i].size() != 1)
        first.insert(vs[i][0]);
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < vs[i].size(); ++j) {
        s.push_back(vs[i][j]);
      }
    }
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    std::cout << dfs() << std::endl;
  }
  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(s) s.begin(), s.end()

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    // map<char,int> a;
    int a[256] = {};
    // map<char,bool> not_zero;
    bool not_zero[256] = {};
    rep(i, n) {
      cin >> s[i];
      int k = s[i].size();
      if (k > 1)
        not_zero[s[i][0]] = true;
      reverse(all(s[i]));
      rep(j, k) { a[s[i][j]] += pow(10, j) * (i == n - 1 ? -1 : 1); }
    }

    set<char> st;
    rep(i, n) {
      for (char c : s[i]) {
        st.insert(c);
      }
    }
    string char_list;
    for (char c : st) {
      char_list += c;
      // cerr<<a[c]<<" ";
    }
    // cerr<<endl;

    char_list += string(10 - (int)st.size(), '-');
    sort(all(char_list));
    int res = 0;

    int perm = 0;
    do {
      perm++;
      if (not_zero[char_list[0]])
        continue;
      int sum = 0;
      rep(i, 10) sum += i * a[char_list[i]];
      if (sum == 0)
        res++;
    } while (next_permutation(all(char_list)));
    cout << res << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(s) s.begin(), s.end()

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<string> s(n);
    map<char, int> a;
    map<char, bool> not_zero;
    rep(i, n) {
      cin >> s[i];
      int k = s[i].size();
      if (k > 1)
        not_zero[s[i][0]] = true;
      reverse(all(s[i]));
      rep(j, k) { a[s[i][j]] += pow(10, j) * (i == n - 1 ? -1 : 1); }
    }

    set<char> st;
    rep(i, n) {
      for (char c : s[i]) {
        st.insert(c);
      }
    }
    string char_list;
    for (char c : st) {
      char_list += c;
      // cerr<<a[c]<<" ";
    }
    // cerr<<endl;

    char_list += string(10 - (int)st.size(), '-');
    sort(all(char_list));
    int res = 0;

    int perm = 0;
    do {
      perm++;
      if (not_zero[char_list[0]])
        continue;
      int sum = 0;
      rep(i, 10) sum += i * a[char_list[i]];
      if (sum == 0)
        res++;
    } while (next_permutation(all(char_list)));
    cout << res << endl;
  }

  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

long long conv(int *num, vector<int> &perm) {
  long long res = 0;
  for (int i = 0; i < perm.size(); i++) {
    res += num[i] * perm[i];
  }
  return res;
}

int fact(int n) {
  if (n == 0)
    return 1;
  return n * fact(n - 1);
}

void factor(string str, int *arr, int *m) {
  int add = 1;
  reverse(str.begin(), str.end());
  for (int i = 0; i < str.size(); i++) {
    arr[m[str[i]]] += add;
    add *= 10;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> ss(n);
    int mcnt = 0;
    int m[256];
    for (int i = 0; i < 256; i++)
      m[i] = -1;

    for (int i = 0; i < n; i++)
      cin >> ss[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < ss[i].size(); j++) {
        if (m[ss[i][j]] < 0)
          m[ss[i][j]] = mcnt++;
      }
    }
    vector<int> v(10);
    for (int i = 0; i < 10; i++)
      v[i] = i;
    int res = 0;

    const int div = fact(10 - mcnt);
    int left[10] = {};
    int right[10] = {};
    for (int i = 0; i < n - 1; i++)
      factor(ss[i], left, m);
    factor(ss[n - 1], right, m);
    do {
      bool f = false;
      for (int i = 0; i < n; i++) {
        if (v[m[ss[i][0]]] == 0 && ss[i].size() > 1)
          f = true;
      }
      if (f)
        continue;
      if (conv(right, v) == conv(left, v)) {
        res++;
      }
    } while (next_permutation(v.begin(), v.end()));
    cout << res / div << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int conv(string str, int *m, vector<int> &perm) {
  int res = 0;
  for (int i = 0; i < str.size(); i++) {
    res *= 10;
    res += perm[m[str[i]]];
  }
  return res;
}

int fact(int n) {
  if (n == 0)
    return 1;
  return n * fact(n - 1);
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> ss(n);
    int mcnt = 0;
    int m[256];
    for (int i = 0; i < 256; i++)
      m[i] = -1;

    for (int i = 0; i < n; i++)
      cin >> ss[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < ss[i].size(); j++) {
        if (m[ss[i][j]] < 0)
          m[ss[i][j]] = mcnt++;
      }
    }
    vector<int> v(10);
    for (int i = 0; i < 10; i++)
      v[i] = i;
    int res = 0;

    const int cnt_ = fact(10 - mcnt);
    int cnt = cnt_;
    do {
      if (--cnt) {
        continue;
      }
      cnt = cnt_;
      bool f = false;
      for (int i = 0; i < n; i++) {
        if (v[m[ss[i][0]]] == 0 && ss[i].size() > 1)
          f = true;
      }
      if (f)
        continue;
      long long s = 0;
      for (int i = 0; i < n - 1; i++) {
        s += conv(ss[i], m, v);
      }
      if (s == conv(ss[n - 1], m, v)) {
        res++;
      }
    } while (next_permutation(v.begin(), v.end()));
    cout << res << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>

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

using namespace std;

int m, coef[10];
bool zero[10];

int dfs(int idx, int sum, int used) {
  if (idx == m)
    return sum == 0;

  int pr_max = 0, pr_min = 0;
  for (int i = idx; i < m; i++) {
    if (coef[i] > 0)
      pr_max += coef[i] * 9;
    if (coef[i] < 0)
      pr_min += coef[i] * 9;
  }
  if (sum > 0 && sum + pr_min > 0)
    return 0;
  if (sum < 0 && sum + pr_max < 0)
    return 0;

  int cnt = 0;
  rep(i, 10) {
    if (used & (1 << i) || (i == 0 && !zero[idx]))
      continue;
    cnt += dfs(idx + 1, sum + coef[idx] * i, used | (1 << i));
  }
  return cnt;
}

int main() {
  for (int n; cin >> n, n;) {
    int f[128], idx = 0;
    fill(f, f + 128, -1);
    fill(coef, coef + 10, 0);
    fill(zero, zero + 10, true);
    rep(i, n) {
      string num;
      cin >> num;
      int sign = (i < n - 1 ? 1 : -1);
      for (int j = num.length() - 1, d = 1; j >= 0; j--, d *= 10) {
        if (f[num[j]] == -1)
          f[num[j]] = idx++;
        coef[f[num[j]]] += sign * d;
      }
      if (num.length() > 1)
        zero[f[num[0]]] = false;
    }
    m = idx;
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

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

using namespace std;

int m, coef[10];
bool zero[10];

int dfs(int idx, int sum, int used) {
  if (idx == m)
    return sum == 0;

  int cnt = 0;
  rep(i, 10) {
    if (used & (1 << i) || (i == 0 && !zero[idx]))
      continue;
    cnt += dfs(idx + 1, sum + coef[idx] * i, used | (1 << i));
  }
  return cnt;
}

int main() {
  for (int n; cin >> n, n;) {
    int f[128], idx = 0;
    fill(f, f + 128, -1);
    fill(coef, coef + 10, 0);
    fill(zero, zero + 10, true);
    rep(i, n) {
      string num;
      cin >> num;
      int sign = (i < n - 1 ? 1 : -1);
      for (int j = num.length() - 1, d = 1; j >= 0; j--, d *= 10) {
        if (f[num[j]] == -1)
          f[num[j]] = idx++;
        coef[f[num[j]]] += sign * d;
      }
      if (num.length() > 1)
        zero[f[num[0]]] = false;
    }
    m = idx;
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int num[256];
int sum[256];
string str[108];
bool leading[256];
int n;
vector<char> chars;
int to_num(string str) {
  int l = str.size();
  if (l > 1 && num[str[0]] == 0)
    return -3141;
  int b = 1, res = 0;
  for (int i = l - 1; i >= 0; i--) {
    res += num[str[i]] * b;
    b *= 10;
  }
  return res;
}

bool solve() {
  cin >> n;
  int cnt = 0;
  if (n == 0)
    return false;
  chars.clear();
  for (int i = 0; i < 256; i++) {
    sum[i] = 0;
    leading[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> str[i];
    int l = str[i].size();
    int b = 1;
    for (int j = l - 1; j >= 0; j--) {
      sum[str[i][j]] += b * ((i == n - 1) ? -1 : 1);
      chars.push_back(str[i][j]);
      b *= 10;
    }
    if (l > 1)
      leading[str[i][0]] = true;
  }
  sort(chars.begin(), chars.end());
  chars.erase(unique(chars.begin(), chars.end()), chars.end());
  while (chars.size() < 10)
    chars.push_back('[');
  do {
    int res = 0;
    if (leading[chars[0]])
      continue;
    for (int i = 0; i < 10; i++) {
      res += sum[chars[i]] * i;
    }
    cnt += res == 0;
  } while (next_permutation(chars.begin(), chars.end()));
  cout << cnt << endl;
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int num[256];
string str[108];
int n;
vector<char> chars;
int to_num(string str) {
  int l = str.size();
  if (l > 1 && num[str[0]] == 0)
    return -3141;
  int b = 1, res = 0;
  for (int i = l - 1; i >= 0; i--) {
    res += num[str[i]] * b;
    b *= 10;
  }
  return res;
}

bool solve() {
  cin >> n;
  int cnt = 0;
  if (n == 0)
    return false;
  chars.clear();
  for (int i = 0; i < n; i++) {
    cin >> str[i];
    for (int j = 0; j < str[i].size(); j++) {
      chars.push_back(str[i][j]);
    }
  }
  sort(chars.begin(), chars.end());
  chars.erase(unique(chars.begin(), chars.end()), chars.end());
  while (chars.size() < 10)
    chars.push_back('[');
  do {
    for (int i = 0; i < 10; i++) {
      num[chars[i]] = i;
    }
    int res = 0;
    for (int i = 0; i < n - 1; i++) {
      res += to_num(str[i]);
    }
    res -= to_num(str[n - 1]);
    cnt += res == 0;
  } while (next_permutation(chars.begin(), chars.end()));
  cout << cnt << endl;
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int N;
bool ng[15];
int v[15];
bool f[15];
char S[15][15];
int len[15];
inline int getnum(int x) {
  int ret = 0;
  for (int i = 0; i < len[x]; i++, ret *= 10)
    ret += v[S[x][i]];
  return ret;
}

inline bool check() {
  int ret = getnum(N - 1);
  for (int i = 0; i < N - 1; i++)
    if ((ret -= getnum(i)) < 0)
      return false;
  return ret == 0;
}

inline int solve(int c, int m) {
  if (c == m)
    return check();
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (f[i] || (i == 0 && ng[c]))
      continue;
    f[i] = 1;
    v[c] = i;
    ret += solve(c + 1, m);
    f[i] = 0;
  }
  return ret;
}

int main() {
  while (cin >> N && N) {
    map<char, int> F;
    memset(ng, 0, sizeof(ng));
    for (int i = 0; i < N; i++) {
      cin >> S[i];
      len[i] = strlen(S[i]);
      for (int j = 0; j < len[i]; j++) {
        if (F.count(S[i][j]) == 0)
          F[S[i][j]] = F.size() - 1;
        S[i][j] = F[S[i][j]];
        if (j == 0 && len[i] > 1)
          ng[S[i][j]] = true;
      }
    }

    int m = F.size();
    memset(f, 0, sizeof(f));
    int res = solve(0, m);
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int N;
string S[15];
bool ng[15];
int v[15];
bool f[15];

inline bool isvalid() {
  for (int i = 0; i < N; i++)
    if (S[i].size() > 1 && v[S[i][0]] == 0)
      return false;
  return true;
}

inline int getnum(string &s) {
  int ret = 0;
  for (int i = 0; i < s.size(); i++, ret *= 10)
    ret += v[s[i]];
  return ret;
}

inline bool check() {
  int ret = getnum(S[N - 1]);
  for (int i = 0; i < N; i++)
    if (i < N - 1)
      if ((ret -= getnum(S[i])) < 0)
        return false;
  return ret == 0;
}

inline int solve(int c, int m) {
  if (c == m) {
    if (isvalid())
      return check();
    return 0;
  }
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (f[i] || (i == 0 && ng[c]))
      continue;
    f[i] = 1;
    v[c] = i;
    ret += solve(c + 1, m);
    f[i] = 0;
  }
  return ret;
}

int main() {
  while (cin >> N && N) {
    map<char, int> F;
    memset(ng, 0, sizeof(ng));
    for (int i = 0; i < N; i++) {
      cin >> S[i];
      for (int j = 0; j < (int)S[i].size(); j++) {
        if (F.count(S[i][j]) == 0)
          F[S[i][j]] = F.size() - 1;
        S[i][j] = F[S[i][j]];
        if (j == 0 && S[i].size() > 1)
          ng[S[i][j]] = true;
      }
    }

    int m = F.size();
    memset(f, 0, sizeof(f));
    int res = solve(0, m);
    cout << res << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

// 53
#include <cctype>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

map<char, int> f;
set<char> ap;
int c;
vector<int> vc;
vector<bool> va;
int cn[10];
int n;

int pow10(int x) {
  int r = 1;
  while (x--) {
    r *= 10;
  }
  return r;
}

int dfs(int s, int u) {
  if (s == vc.size()) {
    int r = 0;
    for (int i = 0; i < vc.size(); i++) {
      r += cn[i] * vc[i];
    }
    return r + c == 0;
  } else {
    int r = 0;
    for (int i = 0; i <= 9; i++) {
      if (!(u & 1 << i) && (i || !va[s])) {
        cn[s] = i;
        r += dfs(s + 1, u | 1 << i);
      }
    }
    return r;
  }
}

int main() {
  while (cin >> n, n) {
    c = 0;
    vc.clear();
    va.clear();
    f.clear();
    ap.clear();
    for (int j = 0; j < n; j++) {
      string s;
      cin >> s;
      if (isalpha(s[0]) && s.size() != 1) {
        ap.insert(s[0]);
      }
      for (int i = 0; i < s.size(); i++) {
        if (isdigit(s[i])) {
          c += (s[i] - '0') * pow10(s.size() - i - 1) * ((j == n - 1) ? -1 : 1);
        } else {
          f[s[i]] += pow10(s.size() - i - 1) * ((j == n - 1) ? -1 : 1);
        }
      }
    }
    for (map<char, int>::iterator it = f.begin(); it != f.end(); it++) {
      vc.push_back(it->second);
      va.push_back(ap.count(it->first) == 1);
    }
    cout << dfs(0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 51
#include <cctype>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;

int n;
string s[12];
vector<char> ch;
int ci[128];

int dfs(int ss, int u) {
  if (ss < ch.size()) {
    int r = 0;
    for (int i = 0; i <= 9; i++) {
      if (!(u & 1 << i)) {
        ci[ch[ss]] = i;
        r += dfs(ss + 1, u | 1 << i);
      }
    }
    return r;
  } else {
    int a[12] = {};
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < s[i].size(); j++) {
        if (isdigit(s[i][j])) {
          a[i] = (a[i] * 10) + s[i][j] - '0';
        } else {
          if (j == 0 && s[i].size() != 1 && ci[s[i][j]] == 0)
            return 0;
          a[i] = (a[i] * 10) + ci[s[i][j]];
        }
      }
    }
    return accumulate(a, a + n - 1, 0) == a[n - 1];
  }
}

int main() {
  while (cin >> n, n) {
    bool ap[128] = {};
    ch.clear();
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      for (int j = 0; j < s[i].size(); j++) {
        if (isalpha(s[i][j]) && !ap[s[i][j]]) {
          ap[s[i][j]] = true;
          ch.push_back(s[i][j]);
        }
      }
    }
    cout << dfs(0, 0) << endl;
  }
  return 0;
}