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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

char ban[128];
int sum[128];

string table;
int n;

int main() {
  while (cin >> n && n) {
    rep(i, 128) ban[i] = 0;
    rep(i, 128) sum[i] = 0;
    table = "";
    rep(i, n) {
      string s;
      cin >> s;
      if (s.size() != 1)
        ban[s[0]] = true;
      int w = 1, islst = 1 - 2 * (i == n - 1);
      rep(j, s.size()) {
        sum[s[s.size() - j - 1]] += w * islst;
        w *= 10;
      }
      table += s;
    }
    sort(table.begin(), table.end());
    table.erase(unique(table.begin(), table.end()), table.end());
    int s = table.size();
    int ans = 0, x[10] = {0}, bit = (1 << s) - 1, clc = 0;
    while (bit < (1 << 10)) {
      int p = 0;
      rep(j, 10) if (bit >> j & 1) x[p++] = j;
      do {
        rep(k, p) if (x[k] == 0 && ban[table[k]]) goto exit;
        clc = 0;
        rep(k, p) clc += sum[table[k]] * x[k];
        if (clc == 0)
          ans++;
      exit:;
      } while (next_permutation(x, x + p));
      int x = bit & -bit, y = bit + x;
      bit = ((bit & ~y) / x >> 1) | y;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

char ban[128];
int sum[128];

string table;
int n;

int main() {
  while (cin >> n && n) {
    rep(i, 128) ban[i] = 0;
    rep(i, 128) sum[i] = 0;
    table = "";
    rep(i, n) {
      string s;
      cin >> s;
      if (s.size() != 1)
        ban[s[0]] = true;
      int w = 1, islst = 1 - 2 * (i == n - 1);
      rep(j, s.size()) {
        sum[s[s.size() - j - 1]] += w * islst;
        w *= 10;
      }
      table += s;
    }
    sort(table.begin(), table.end());
    table.erase(unique(table.begin(), table.end()), table.end());
    int s = table.size();
    int ans = 0, x[10] = {0}, bit = (1 << s) - 1;
    while (bit < (1 << 10)) {
      int p = 0;
      rep(j, 10) if (bit >> j & 1) x[p++] = j;
      do {
        int f = 1;
        rep(k, p) if (x[k] == 0 && ban[table[k]]) f = 0;
        if (!f)
          continue;
        int clc = 0;
        rep(k, p) clc += sum[table[k]] * x[k];
        if (clc == 0)
          ans++;
      } while (next_permutation(x, x + p));
      int x = bit & -bit, y = bit + x;
      bit = ((bit & ~y) / x >> 1) | y;
    }
    cout << ans << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

char ban[128];
int sum[128];

string table;
int n;

int main() {
  while (cin >> n && n) {
    rep(i, 128) ban[i] = 0;
    rep(i, 128) sum[i] = 0;
    table = "";
    rep(i, n) {
      string s;
      cin >> s;
      if (s.size() != 1)
        ban[s[0]] = true;
      int w = 1, islst = 1 - 2 * (i == n - 1);
      rep(j, s.size()) {
        sum[s[s.size() - j - 1]] += w * islst;
        w *= 10;
      }
      table += s;
    }
    sort(table.begin(), table.end());
    table.erase(unique(table.begin(), table.end()), table.end());
    int s = table.size();
    int ans = 0, x[10] = {0}, bit = (1 << s) - 1, clc = 0;
    while (bit < (1 << 10)) {
      int p = 0;
      rep(j, 10) if (bit >> j & 1) x[p++] = j;
      do {
        rep(k, p) if (x[k] == 0 && ban[table[k]]) goto exit;
        clc = 0;
        rep(k, p) clc += sum[table[k]] * x[k];
        if (clc == 0)
          ans++;
      exit:;
      } while (next_permutation(x, x + p));
      int x = bit & -bit, y = bit + x;
      bit = ((bit & ~y) / x >> 1) | y;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

char ban[128];
int sum[128];

string table;
int n;

int main() {
  while (cin >> n && n) {
    rep(i, 128) ban[i] = 0;
    rep(i, 128) sum[i] = 0;
    table = "";
    rep(i, n) {
      string s;
      cin >> s;
      if (s.size() != 1)
        ban[s[0]] = true;
      int w = 1, islst = 1 - 2 * (i == n - 1);
      rep(j, s.size()) {
        sum[s[s.size() - j - 1]] += w * islst;
        w *= 10;
      }
      table += s;
    }
    sort(table.begin(), table.end());
    table.erase(unique(table.begin(), table.end()), table.end());
    int s = table.size();
    int ans = 0, x[10] = {0}, clc = 0;
    for (int bit = 0; bit < (1 << 10); bit++) {
      if (__builtin_popcount(bit) != s)
        continue;
      int p = 0;
      rep(j, 10) if (bit >> j & 1) x[p++] = j;
      do {
        rep(k, p) if (x[k] == 0 && ban[table[k]]) goto exit;
        clc = 0;
        rep(k, p) clc += sum[table[k]] * x[k];
        if (clc == 0)
          ans++;
      exit:;
      } while (next_permutation(x, x + p));
    }
    cout << ans << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
using namespace std;
int a[11], b[10], d[26], e[26];
int fn(int p, int q) {
  int i;
  int ct = 0;
  if (a[p + 1] == -1) {
    if (0) {
    } else if (q == 0 && d[a[p]] == 0) {
      for (i = e[a[p]]; i < 10; i++) {
        if (b[i])
          ct++;
      }
    } else if (q == 0) {
      if (b[0] && e[a[p]] == 0)
        ct = 1;
    } else if (d[a[p]] == 0) {
    } else {
      for (i = e[a[p]]; i < 10; i++) {
        if (b[i] && q + i * d[a[p]] == 0)
          ct++;
      }
    }
  } else {
    for (i = e[a[p]]; i < 10; i++) {
      if (b[i]) {
        b[i] = 0;
        ct += fn(p + 1, q + i * d[a[p]]);
        b[i] = -1;
      }
    }
  }
  return ct;
}
int main() {
  int i, j;
  int n;
  memset(b, -1, sizeof(b));
  while (cin >> n && n) {
    int mx = 0;
    set<int> f;
    memset(d, 0, sizeof(d));
    memset(e, 0, sizeof(e));
    for (i = 0; i < n; i++) {
      string s;
      cin >> s;
      int ln = s.length();
      if (i == n - 1)
        mx = (mx <= ln);
      else
        mx = max(mx, ln);
      for (j = 0; j < ln; j++) {
        f.insert(s[j] - 'A');
        if (j == 0 && ln > 1)
          e[s[j] - 'A'] = 1;
        if (i < n - 1)
          d[s[j] - 'A'] += (int)pow(10, ln - j - 1);
        else
          d[s[j] - 'A'] -= (int)pow(10, ln - j - 1);
      }
    }
    if (mx) {
      set<int>::iterator it;
      i = 0;
      for (it = f.begin(); it != f.end(); it++) {
        a[i] = *it;
        i++;
      }
      a[i] = -1;
      cout << fn(0, 0) << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
using namespace std;
int a[11], b[10], d[26], e[26];
int fn(int p, int q) {
  int i;
  int ct = 0;
  if (a[p] == -1) {
    if (q == 0)
      ct = 1;
  } else {
    for (i = e[a[p]]; i < 10; i++) {
      if (b[i]) {
        b[i] = 0;
        ct += fn(p + 1, q + i * d[a[p]]);
        b[i] = -1;
      }
    }
  }
  return ct;
}

int main() {
  int i, j;
  int n;
  memset(b, -1, sizeof(b));
  while (cin >> n && n) {
    set<int> f;
    memset(d, 0, sizeof(d));
    memset(e, 0, sizeof(e));
    for (i = 0; i < n; i++) {
      string s;
      cin >> s;
      int ln = s.length();
      for (j = 0; j < ln; j++) {
        f.insert(s[j] - 'A');
        if (j == 0 && ln > 1)
          e[s[j] - 'A'] = 1;
        if (i < n - 1)
          d[s[j] - 'A'] += pow(10, ln - j - 1);
        else
          d[s[j] - 'A'] -= pow(10, ln - j - 1);
      }
    }
    set<int>::iterator it;
    i = 0;
    for (it = f.begin(); it != f.end(); it++) {
      a[i] = *it;
      i++;
    }
    a[i] = -1;
    cout << fn(0, 0) << endl;
  }
  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  ios::sync_with_stdio(false);
  while (cin >> N, N) {
    vector<string> S(N);
    map<char, int> C;
    map<char, bool> not_zero;
    long long int ans = 0;
    for (int i = 0; i < N; ++i) {
      cin >> S[i];
      long long int t = 1;
      if (S[i].length() > 1)
        not_zero[S[i][0]] = true;
      for (int j = S[i].length() - 1; j >= 0; --j) {
        if (i < N - 1)
          C[S[i][j]] += t;
        else
          C[S[i][j]] -= t;
        t *= 10;
      }
    }
    int n = C.size();
    vector<int> A;
    for (int i = 0; i < 10; ++i)
      A.push_back(i);
    do {
      long long int sum = 0;
      int t = 0;
      for (map<char, int>::iterator itr = C.begin(); itr != C.end(); ++itr) {
        if (A[t] == 0 && not_zero[itr->first]) {
          sum = -1;
          break;
        }
        sum += A[t] * (itr->second);
        ++t;
      }
      if (sum == 0) {
        ++ans;
      }
      sort(A.begin() + n, A.end());
      reverse(A.begin() + n, A.end());
    } while (next_permutation(A.begin(), A.end()));
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  while (cin >> N, N) {
    vector<string> S(N);
    map<char, int> C;
    map<char, bool> not_zero;
    long long int ans = 0;
    for (int i = 0; i < N; ++i) {
      cin >> S[i];
      long long int t = 1;
      if (S[i].length() > 1)
        not_zero[S[i][0]] = true;
      for (int j = S[i].length() - 1; j >= 0; --j) {
        if (i < N - 1)
          C[S[i][j]] += t;
        else
          C[S[i][j]] -= t;
        t *= 10;
      }
    }
    vector<char> V;
    for (map<char, int>::iterator itr = C.begin(); itr != C.end(); ++itr) {
      V.push_back(itr->first);
    }
    int n = V.size();
    vector<int> A;
    for (int i = 0; i < 10; ++i)
      A.push_back(i);
    do {
      long long int sum = 0;
      for (int i = 0; i < n; ++i) {
        if (A[i] == 0 && not_zero[V[i]]) {
          sum = -1;
          break;
        }
        sum += A[i] * C[V[i]];
      }
      if (sum == 0) {
        ++ans;
      }
      sort(A.begin() + n, A.end());
      reverse(A.begin() + n, A.end());
    } while (next_permutation(A.begin(), A.end()));
    cout << ans << endl;
  }
  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <cstring>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

string id;
ll sum[22], ans;
bool head[22], use[22];

void rec(int depth, ll val) {
  if (depth + 1 == (int)id.size()) {
    ll rem = sum[depth];
    if (rem == 0LL) {
      if (val == 0LL) {
        ans += 10 - depth;
        if (!use[0] && head[depth])
          ans--;
      }
    } else {
      if (val == 0LL) {
        if (!use[0] && !head[depth])
          ans++;
      } else if (val * rem < 0) {
        val = abs(val);
        rem = abs(rem);
        if (val % rem == 0LL && val / rem < 10LL && !use[val / rem]) {
          ans++;
        }
      }
    }
    return;
  }

  for (ll i = 0; i < 10; i++) {
    if (i == 0LL && head[depth])
      continue;
    if (use[i])
      continue;
    use[i] = true;
    rec(depth + 1, val + sum[depth] * i);
    use[i] = false;
  }
}

int main() {
  int n;
  string s;

  while (cin >> n, n) {
    memset(sum, 0, sizeof(sum));
    memset(head, 0, sizeof(head));
    id = "";

    rep(i, n) {
      cin >> s;
      ll tmp = 1;
      for (int j = (int)s.size() - 1; j >= 0; j--) {
        int k = 0;
        for (; k < (int)id.size(); k++) {
          if (s[j] == id[k])
            break;
        }
        sum[k] += (i + 1 == n) ? -tmp : tmp;
        if (k == id.size())
          id += s[j];
        if (j == 0 && s.size() != 1)
          head[k] = true;
        tmp *= 10LL;
      }
    }

    ans = 0;
    memset(use, 0, sizeof(use));
    rec(0, 0);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

string id;
long long sum[22], ans;
bool head[22], use[22];

void rec(int depth, int val) {
  if (depth + 1 == (int)id.size()) {
    if (val == 0) {
      if (sum[depth] == 0)
        ans++;
    } else {
      int rem = sum[depth];
      if (val * rem < 0 && -val % rem == 0 && -val / rem <= 9 &&
          !use[-val / rem])
        ans++;
    }
    return;
  }

  for (int i = 0; i < 10; i++) {
    if (i == 0 && head[depth])
      continue;
    if (use[i])
      continue;
    use[i] = true;
    rec(depth + 1, val + sum[depth] * i);
    use[i] = false;
  }
}

int main() {
  int n;
  string s;

  while (cin >> n, n) {
    memset(sum, 0, sizeof(sum));
    memset(head, 0, sizeof(head));
    id = "";

    rep(i, n) {
      cin >> s;
      int tmp = 1;
      for (int j = (int)s.size() - 1; j >= 0; j--) {
        int k = 0;
        for (; k < (int)id.size(); k++) {
          if (s[j] == id[k])
            break;
        }
        sum[k] += (i + 1 == n) ? -tmp : tmp;
        if (k == id.size())
          id += s[j];
        if (j == 0 && s.size() != 1)
          head[k] = true;
        tmp *= 10;
      }
    }

    ans = 0;
    memset(use, 0, sizeof(use));
    rec(0, 0);
    cout << ans << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
string str[13];
int used[10], num[101], n, flg[101];

int nxp(int idx, int i, int sum, int a) {
  int res = 0;
  if (sum < 0 || idx == n)
    return sum == 0;
  if (i < 0)
    return nxp(idx + 1, str[idx + 1].size() - 1, sum, 1);

  int f = idx ? -1 : 1, ch = str[idx][i];
  if (num[ch] != -1)
    res += nxp(idx, i - 1, sum + f * (a * num[ch]), a * 10);
  else
    for (int j = 0; j < 10; j++) {
      if (used[j] || (!i && !j && str[idx].size() != 1) || (!j && flg[ch]))
        continue;
      used[j] = 1, num[ch] = j;
      res += nxp(idx, i - 1, sum + f * (a * j), a * 10);
      used[j] = 0, num[ch] = -1;
    }

  return res;
}

int main() {
  memset(num, -1, sizeof(num));
  while (cin >> n, n) {
    memset(flg, 0, sizeof(flg));
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      if (str[i].size() != 1)
        flg[str[i][0]] = 1;
    }
    swap(str[0], str[n - 1]);
    cout << nxp(0, str[0].size() - 1, 0, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string str[13];
int used[10], num[101], n;

int nxp(int idx, int i, int sum, int a) {
  int res = 0;
  if (sum < 0 || idx == n)
    return sum == 0;
  if (i < 0)
    return nxp(idx + 1, str[idx + 1].size() - 1, sum, 1);

  int f = idx ? -1 : 1, ch = str[idx][i];
  if (!i && !num[ch] && str[idx].size() == 1)
    return 0;
  if (num[ch] != -1)
    res += nxp(idx, i - 1, sum + f * (a * num[ch]), a * 10);
  else
    for (int j = 0; j < 10; j++) {
      if (used[j] || (!i && !j && str[idx].size() != 1))
        continue;
      used[j] = 1, num[ch] = j;
      res += nxp(idx, i - 1, sum + f * (a * j), a * 10);
      used[j] = 0, num[ch] = -1;
    }

  return res;
}

int main() {
  memset(num, -1, sizeof(num));
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> str[i];
    swap(str[0], str[n - 1]);
    cout << nxp(0, str[0].size() - 1, 0, 1) << 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 < n; i++)

int N;
string s[22];
int hm[33];
int d[33];

inline int next(int c) {
  int x = c & -c, y = c + x;
  return (((c & ~y) / x) >> 1) | y;
}

int main() {
  while (cin >> N, N) {
    rep(i, 33) hm[i] = 0;
    rep(i, 33) d[i] = 0;
    rep(i, N) cin >> s[i];
    rep(i, N) {
      int pw = 1;
      rep(j, s[i].size()) pw *= 10;
      pw /= 10;
      rep(j, s[i].size()) {
        if (i == N - 1)
          d[s[i][j] - 'A'] -= pw;
        else
          d[s[i][j] - 'A'] += pw;
        pw /= 10;
      }
    }

    vector<char> alpha;
    rep(i, N) rep(j, s[i].size()) alpha.push_back(s[i][j]);
    sort(alpha.begin(), alpha.end());
    alpha.erase(unique(alpha.begin(), alpha.end()), alpha.end());
    int n = alpha.size();
    int res = 0;
    for (int comb = (1 << n) - 1; comb < (1 << 10); comb = next(comb)) {
      vector<int> can;
      rep(i, 10) if (comb & 1 << i) can.push_back(i);
      do {
        rep(i, n) hm[alpha[i] - 'A'] = can[i];
        bool ok = true;
        rep(i, N) if (hm[s[i][0] - 'A'] == 0 && s[i].size() != 1) {
          ok = false;
          break;
        }
        if (!ok)
          continue;

        int sum = 0;
        rep(i, n) sum += hm[alpha[i] - 'A'] * d[alpha[i] - 'A'];
        if (sum == 0)
          res += 1;
      } while (next_permutation(can.begin(), can.end()));
    }

    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 < n; i++)

int N;
string s[22];
int hm[33];
int d[33];

inline int next(int c) {
  int x = c & -c, y = c + x;
  return (((c & ~y) / x) >> 1) | y;
}

inline int dec(int p) {
  int tmp = 0;
  rep(i, s[p].size()) {
    tmp *= 10;
    tmp += hm[s[p][i] - 'A'];
  }
  return tmp;
}

int main() {
  while (cin >> N, N) {
    rep(i, 33) hm[i] = 0;
    rep(i, 33) d[i] = 0;
    rep(i, N) cin >> s[i];
    rep(i, N) {
      int pw = 1;
      rep(j, s[i].size()) pw *= 10;
      rep(j, s[i].size()) {
        if (i == N - 1)
          d[s[i][j] - 'A'] -= pw / 10;
        else
          d[s[i][j] - 'A'] += pw / 10;
        pw /= 10;
      }
    }

    vector<char> alpha;
    rep(i, N) rep(j, s[i].size()) alpha.push_back(s[i][j]);
    sort(alpha.begin(), alpha.end());
    alpha.erase(unique(alpha.begin(), alpha.end()), alpha.end());
    int n = alpha.size();
    int res = 0;
    for (int comb = (1 << n) - 1; comb < (1 << 10); comb = next(comb)) {
      vector<int> can;
      rep(i, 10) if (comb & 1 << i) can.push_back(i);
      do {
        rep(i, n) hm[alpha[i] - 'A'] = can[i];
        bool ok = true;
        rep(i, N) if (hm[s[i][0] - 'A'] == 0 && s[i].size() != 1) ok = false;
        if (!ok)
          continue;

        int sum = 0;
        rep(i, n) sum += hm[alpha[i] - 'A'] * d[alpha[i] - 'A'];
        if (sum == 0)
          res += 1;
      } while (next_permutation(can.begin(), can.end()));
    }

    cout << res << endl;
  }

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

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s[12], t;
int r1[100], r2[100], n, x[10], ans, tt;
bool ng[100], used[12];
int ch() {
  int res1 = 0, res2 = 0;
  r(i, t.size()) res1 += r1[t[i]] * x[i];
  r(i, t.size()) res2 += r2[t[i]] * x[i];
  return res1 == res2 ? 1 : 0;
}
int dfs(int d) {
  int res = 0;
  if (d == tt)
    return ch();
  r(i, 10) if (!used[i]) {
    if (!i && ng[t[d]])
      continue;
    x[d] = i;
    used[i] = 1;
    res += dfs(d + 1);
    used[i] = 0;
  }
  return res;
}
int main() {
  while (cin >> n, n) {
    memset(used, 0, sizeof(used));
    t = "";
    ans = 0;
    set<char> se;
    memset(ng, 0, sizeof(ng));
    memset(r1, 0, sizeof(r1));
    memset(r2, 0, sizeof(r2));
    r(i, n) cin >> s[i];
    r(i, 10) x[i] = i;
    r(i, n) r(j, s[i].size()) if (!se.count(s[i][j])) se.insert(s[i][j]),
        t += s[i][j];
    r(i, n) if (s[i].size() > 1) ng[s[i][0]] = 1;
    r(i, n - 1) for (int j = s[i].size() - 1, k = 0; j >= 0; j--, k++)
        r1[s[i][j]] += pow(10, k);
    for (int j = s[n - 1].size() - 1, k = 0; j >= 0; j--, k++)
      r2[s[n - 1][j]] += pow(10, k);
    tt = se.size();
    cout << dfs(0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s[12], t;
int r1[100], r2[100], n, x[10], ans;
bool ng[100];
int ch() {
  int res1 = 0, res2 = 0;
  r(i, t.size()) res1 += r1[t[i]] * x[i];
  r(i, t.size()) res2 += r2[t[i]] * x[i];
  return res1 == res2 ? 1 : 0;
}
int main() {
  while (cin >> n, n) {
    t = "";
    ans = 0;
    set<char> se;
    memset(ng, 0, sizeof(ng));
    memset(r1, 0, sizeof(r1));
    memset(r2, 0, sizeof(r2));
    r(i, n) cin >> s[i];
    r(i, 10) x[i] = i;
    r(i, n) r(j, n) if (!se.count(s[i][j])) se.insert(s[i][j]), t += s[i][j];
    r(i, n) ng[s[i][0]] = 1;
    r(i, n - 1) for (int j = s[i].size() - 1, k = 0; j >= 0; j--, k++)
        r1[s[i][j]] += pow(10, k);
    for (int j = s[n - 1].size() - 1, k = 0; j >= 0; j--, k++)
      r2[s[n - 1][j]] += pow(10, k);
    do {
      int po;
      r(i, 10) if (!x[i]) po = i;
      if (po < t.size() && !ng[po])
        ans += ch();
    } while (next_permutation(x, x + 10));
    cout << ans << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    vector<string> vs(n);
    REP(i, n) cin >> vs[i];
    set<char> mask;
    for (string s : vs)
      for (char c : s)
        mask.insert(c);
    vector<char> ma(mask.begin(), mask.end());
    while (ma.size() < 10)
      ma.push_back('a');
    int csum[256][10];
    REP(i, 256) REP(j, 10) csum[i][j] = 0;
    int tmp = 1;
    REP(i, vs.size() - 1) {
      tmp = 1;
      for (int j = vs[i].size(); j >= 0; --j) {
        csum[vs[i][j]][1] += tmp;
        tmp *= 10;
      }
    }
    tmp = 1;
    for (int j = vs[n - 1].size(); j >= 0; --j) {
      csum[vs[n - 1][j]][1] -= tmp;
      tmp *= 10;
    }
    REP(i, 256) REP(j, 10) csum[i][j] = csum[i][1] * j;
    int answer = 0;
    map<char, bool> l0;
    for (string s : vs)
      for (char c : s)
        l0[c] = false;
    REP(i, vs.size()) if (vs[i].size() > 1) l0[vs[i][0]] = true;
    do {
      if (l0[ma[0]])
        continue;
      int sum = 0;
      REP(i, ma.size()) sum += csum[ma[i]][i];
      if (sum == 0)
        ++answer;
    } while (next_permutation(ma.begin(), ma.end()));
    cout << answer << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    vector<string> vs(n);
    REP(i, n) cin >> vs[i];
    set<char> mask;
    for (string s : vs)
      for (char c : s)
        mask.insert(c);
    vector<char> ma(mask.begin(), mask.end());
    while (ma.size() < 10)
      ma.push_back('a');
    map<char, int> csum;
    csum['a'] = 0;
    int tmp = 1;
    REP(i, vs.size() - 1) {
      tmp = 1;
      for (int j = vs[i].size(); j >= 0; --j) {
        csum[vs[i][j]] += tmp;
        tmp *= 10;
      }
    }
    tmp = 1;
    for (int j = vs[n - 1].size(); j >= 0; --j) {
      csum[vs[n - 1][j]] -= tmp;
      tmp *= 10;
    }
    int answer = 0;
    map<char, bool> l0;
    for (string s : vs)
      for (char c : s)
        l0[c] = false;
    REP(i, vs.size()) if (vs[i].size() > 1) l0[vs[i][0]] = true;
    do {
      if (l0[ma[0]])
        continue;
      int sum = 0;
      REP(i, ma.size()) sum += i * csum[ma[i]];
      if (sum == 0)
        ++answer;
    } while (next_permutation(ma.begin(), ma.end()));
    cout << answer << endl;
  }
}