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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int N, M;
string ss[101];
map<char, int> equ;
char cs[101];
int co[101];
bool isHead[1001];
int cnt;
int nums[101];

// Mツ古つづ個閉カツ篠堋づ可青板篠堋づーツ環づィツ督鳴づづゥ
void check() {
  do {
    // ツ陛サツ津カツ篠ョツつェツ湘ーツ個渉づーツ鳴楪つスツつキツつゥツづつ、ツつゥツチツェツッツク
    int sum = 0;
    bool ok = true;
    for (int i = 0; i < M; i++) {
      if (isHead[cs[i]] && nums[i] == 0) {
        ok = false;
        break;
      }
      sum += co[i] * nums[i];
    }
    if (ok && sum == 0)
      cnt++;
  } while (next_permutation(nums, nums + M));
}

void solve() {
  for (int s = (1 << M) - 1; s < (1 << 10);) {
    int a = 0;
    // vector<int> v;
    for (int i = 0; i < 10; i++) {
      if ((s >> i) & 1) {
        a++;
        nums[a - 1] = i;
      }
    }
    check();
    // table[a].push_back(v);
    int bit = s;
    int x = bit & -bit, y = bit + x;
    bit = ((bit & ~y) / x >> 1) | y;
    s = bit;
  }
  // int a=table[M].size();
  // for(int i=0;i<table[M].size();i++){
  //     for(int j=0;j<M;j++)nums[j]=table[M][i][j];
  //     check();
  // }
}

// void prePro(){
//     for(int s=0;s<(1<<10);s++){
//         int a=0;
//         vector<int> v;
//         for(int i=0;i<10;i++){
//             if((s>>i)&1){
//                 a++;
//                 v.push_back(i);
//             }
//         }
//         table[a].push_back(v);
//     }
// }

int main() {
  std::ios_base::sync_with_stdio(0);
  // prePro();
  while (cin >> N && N) {
    cnt = 0;
    equ.clear();
    memset(isHead, 0, sizeof(isHead));
    for (int i = 0; i < N; i++)
      cin >> ss[i];
    // ツ暗ェツ篠淞陛サツ津カツ篠ョツづーツ催ャツ青ャツつオツづつィツつュ
    for (int i = 0; i < N; i++) {
      int mul = 1;
      for (int j = ss[i].size() - 1; j >= 0; j--) {
        if (i == N - 1)
          equ[ss[i][j]] -= mul;
        else
          equ[ss[i][j]] += mul;
        if (j == 0 && ss[i].size() != 1)
          isHead[ss[i][j]] = true;
        mul *= 10;
      }
    }
    M = equ.size();
    int id = 0;
    // ツ係ツ青板づ閉カツ篠堋づーツ禿シツづェツづつィツつュ
    for (map<char, int>::iterator it = equ.begin(); it != equ.end(); it++) {
      cs[id++] = it->first;
      co[id - 1] = it->second;
    }
    solve();
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int N, M;
string ss[101];
map<char, int> equ;
char cs[101];
ll co[101];
bool isHead[1001];
int cnt;
int nums[101];

// Mツ古つづ個閉カツ篠堋づ可青板篠堋づーツ環づィツ督鳴づづゥ
void check() {
  do {
    // ツ陛サツ津カツ篠ョツつェツ湘ーツ個渉づーツ鳴楪つスツつキツつゥツづつ、ツつゥツチツェツッツク
    ll sum = 0;
    bool ok = true;
    for (int i = 0; i < M; i++) {
      if (isHead[cs[i]] && nums[i] == 0) {
        ok = false;
        break;
      }
      sum += co[i] * nums[i];
    }
    if (ok && sum == 0)
      cnt++;
  } while (next_permutation(nums, nums + M));
}

void solve() {
  for (int s = (1 << M) - 1; s < (1 << 10);) {
    int a = 0;
    // vector<int> v;
    for (int i = 0; i < 10; i++) {
      if ((s >> i) & 1) {
        a++;
        nums[a - 1] = i;
      }
    }
    check();
    // table[a].push_back(v);
    int bit = s;
    int x = bit & -bit, y = bit + x;
    bit = ((bit & ~y) / x >> 1) | y;
    s = bit;
  }
  // int a=table[M].size();
  // for(int i=0;i<table[M].size();i++){
  //     for(int j=0;j<M;j++)nums[j]=table[M][i][j];
  //     check();
  // }
}

// void prePro(){
//     for(int s=0;s<(1<<10);s++){
//         int a=0;
//         vector<int> v;
//         for(int i=0;i<10;i++){
//             if((s>>i)&1){
//                 a++;
//                 v.push_back(i);
//             }
//         }
//         table[a].push_back(v);
//     }
// }

int main() {
  std::ios_base::sync_with_stdio(0);
  // prePro();
  while (cin >> N && N) {
    cnt = 0;
    equ.clear();
    memset(isHead, 0, sizeof(isHead));
    for (int i = 0; i < N; i++)
      cin >> ss[i];
    // ツ暗ェツ篠淞陛サツ津カツ篠ョツづーツ催ャツ青ャツつオツづつィツつュ
    for (int i = 0; i < N; i++) {
      ll mul = 1;
      for (int j = ss[i].size() - 1; j >= 0; j--) {
        if (i == N - 1)
          equ[ss[i][j]] -= mul;
        else
          equ[ss[i][j]] += mul;
        if (j == 0 && ss[i].size() != 1)
          isHead[ss[i][j]] = true;
        mul *= 10;
      }
    }
    M = equ.size();
    int id = 0;
    // ツ係ツ青板づ閉カツ篠堋づーツ禿シツづェツづつィツつュ
    for (map<char, int>::iterator it = equ.begin(); it != equ.end(); it++) {
      cs[id++] = it->first;
      co[id - 1] = it->second;
    }
    solve();
    cout << cnt << endl;
  }
  return 0;
}