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

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

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

int n, k;
int d[11];

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

template <typename T> inline void _tostr(const T t, string &s) {
  ostringstream o;
  o << t;
  s = o.str();
}
template <typename T> inline string _tostr(const T t) {
  ostringstream o;
  o << t;
  return o.str();
}
template <typename T> inline void _toint(const string s, T &n) {
  istringstream i(s);
  i >> n;
}
template <typename T> inline T _toint(const string s) {
  istringstream i(s);
  T n;
  i >> n;
  return n;
}

int main(int argc, char *argv[]) {
  while (cin >> n, n) {
    cin >> k;
    rep(i, n) cin >> d[i];

    vector<string> res;

    for (int comb = (1 << k) - 1; comb < (1 << n);
         comb = nextCombination(comb)) {
      vector<int> vec;
      rep(mask, n) if (comb & 1 << mask) vec.push_back(mask);
      do {
        string s = "";
        rep(i, k) s += _tostr(d[vec[i]]);
        res.push_back(s);
      } while (next_permutation(vec.begin(), vec.begin() + k));
    }

    sort(res.begin(), res.end());
    res.erase(unique(res.begin(), res.end()), res.end());
    cout << res.size() << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

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

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

int n, k;
int d[11];

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

template <typename T> inline void _tostr(const T t, string &s) {
  ostringstream o;
  o << t;
  s = o.str();
}
template <typename T> inline string _tostr(const T t) {
  ostringstream o;
  o << t;
  return o.str();
}
template <typename T> inline void _toint(const string s, T &n) {
  istringstream i(s);
  i >> n;
}
template <typename T> inline T _toint(const string s) {
  istringstream i(s);
  T n;
  i >> n;
  return n;
}

int main(int argc, char *argv[]) {
  while (cin >> n, n) {
    cin >> k;
    rep(i, n) cin >> d[i];

    vector<string> res;

    for (int comb = (1 << k) - 1; comb < (1 << n);
         comb = nextCombination(comb)) {
      do {
        string s = "";
        rep(mask, n) if (comb & 1 << mask) s += _tostr(d[mask]);
        res.push_back(s);
      } while (next_permutation(d, d + n));
    }

    sort(res.begin(), res.end());
    res.erase(unique(res.begin(), res.end()), res.end());
    cout << res.size() << endl;
  }

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

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

int main() {
  int n, k;
  while (cin >> n >> k && (n | k)) {
    string card[10];
    string t;
    for (int i = 0; i < n; i++)
      cin >> card[i];

    sort(card, card + n);

    set<int> res;

    do {
      int a = 0;
      for (int i = 0; i < k; i++) {
        for (int j = 0; j < card[i].size(); j++) {
          a *= 10;
          a += card[i][j] - '0';
        }
      }

      res.insert(a);
    } while (next_permutation(card, card + n));

    cout << res.size() << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  while (cin >> n >> k && (n | k)) {
    string card[10];
    string t;
    for (int i = 0; i < n; i++)
      cin >> card[i];

    sort(card, card + n);

    set<string> res;

    do {
      string s = "";
      for (int i = 0; i < k; i++) {
        s += card[i];
      }
      res.insert(s);
    } while (next_permutation(card, card + n));

    cout << res.size() << endl;
  }
}
Problem p00469 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef long long ll;

#ifdef _MSC_VER
#define __builtin_popcount __popcnt
#endif

int n, m;
int main() {
  while (cin >> n >> m, n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    vector<set<ll>> dp(1 << n);
    dp[0] = set<ll>({0});
    for (int i = 1; i < 1 << n; i++) {
      if (__builtin_popcount(i) > m)
        continue;
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k : dp[i - (1 << j)]) {
            dp[i].insert(stoll(to_string(k) + to_string(a[j])));
          }
        }
      }
    }
    set<ll> ret;
    for (int i = 0; i < 1 << n; i++) {
      if (__builtin_popcount(i) == m) {
        ret.insert(dp[i].begin(), dp[i].end());
      }
    }
    cout << ret.size() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;

#ifdef _MSC_VER
#define __builtin_popcount __popcnt
#endif

int n, m;
int main() {
  while (cin >> n >> m, n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    vector<set<ll>> dp(1 << n);
    dp[0] = set<ll>({0});
    for (int i = 1; i < 1 << n; i++) {
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k : dp[i - (1 << j)]) {
            dp[i].insert(stoll(to_string(k) + to_string(a[j])));
          }
        }
      }
    }
    set<ll> ret;
    for (int i = 0; i < 1 << n; i++) {
      if (__builtin_popcount(i) == m) {
        ret.insert(dp[i].begin(), dp[i].end());
      }
    }
    cout << ret.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

vector<string> card;
set<int> used;
set<int> ans;
int s[4];
int n, k;

void saiki(int t) {
  if (t == k) {
    string ss = "";
    REP(i, k) ss += card[s[i]];
    ans.insert(stoi(ss));
    return;
  }
  REP(i, n) {
    if (used.find(i) == used.end()) {
      used.insert(i);
      s[t] = i;
      saiki(t + 1);
      used.erase(i);
    }
  }
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    card = vector<string>(n);
    REP(i, n) { cin >> card[i]; }
    saiki(0);
    cout << ans.size() << endl;
    ans.clear();
    used.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

vector<string> card;
set<int> used;
set<int> ans;
int n, k;

void saiki(int t, string s) {
  if (t == k) {
    ans.insert(stoi(s));
  }
  REP(i, n) {
    if (used.find(i) == used.end()) {
      used.insert(i);
      saiki(t + 1, s + card[i]);
      used.erase(i);
    }
  }
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    card = vector<string>(n);
    REP(i, n) { cin >> card[i]; }
    saiki(0, "");
    cout << ans.size() << endl;
    ans.clear();
    used.clear();
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring> // require memset
#include <fstream> // require freopen
#include <iostream>
#include <set>
#include <sstream> // require stringstream
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  //	cut here before submit
  //	freopen ("testcase.lining_up_card", "r", stdin );
  int n, k;

  while (cin >> n >> k && n && k) {
    //		vector <string> card (n );
    vector<int> card(n, 0);
    for (int i = 0; i < n; ++i) {
      cin >> card[i];
    } // end for
    sort(card.begin(), card.end());
    set<ll> res;

    do {
      ll sum = 0LL;
      for (int i = 0; i < k; ++i) {
        if (card[i] < 10) {
          sum *= 10LL;
        } else {
          sum *= 100LL;
        } // end if
        sum += (ll)card[i];
        //				cout << card[i] << ' ';
      } // end for

      //			if (!res.count (sum ) ){
      //				cout << "sum: " << sum << endl;
      res.insert(sum);
      //			}else{
      //				cout << endl;
      //			} // end if
    } while (next_permutation(card.begin(), card.end()));

    cout << res.size() << endl;

  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring> // require memset
#include <fstream> // require freopen
#include <iostream>
#include <set>
#include <sstream> // require stringstream
#include <vector>

using namespace std;

string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.lining_up_card", "r", stdin );
  int n, k;

  while (cin >> n >> k && n && k) {
    vector<string> card(n);
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m;
      card[i] = itos(m);
    } // end for
    sort(card.begin(), card.end());
    set<string> res;

    do {
      string sum = "";
      for (int i = 0; i < k; ++i) {
        sum += card[i];
      } // end for
      res.insert(sum);
    } while (next_permutation(card.begin(), card.end()));

    cout << res.size() << endl;

  } // end loop

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int n, k;
int main() {
  while (cin >> n >> k, n || k) {
    vector<string> v;
    for (int i = 0; i < n; ++i) {
      string st;
      cin >> st;
      v.push_back(st);
    }
    sort(v.begin(), v.end());

    set<string> st;
    do {
      string num = "";
      for (int i = 0; i < k; ++i) {
        num += v[i];
      }
      st.insert(num);
    } while (next_permutation(v.begin(), v.end()));
    cout << st.size() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, k;
int main() {
  while (cin >> n >> k, n || k) {
    vector<string> v;
    for (int i = 0; i < n; ++i) {
      string st;
      cin >> st;
      v.push_back(st);
    }
    sort(v.begin(), v.end());
    vector<string> nums;
    do {
      string num = "";
      for (int i = 0; i < k; ++i) {
        num += v[i];
      }
      nums.push_back(num);
    } while (next_permutation(v.begin(), v.end()));
    sort(nums.begin(), nums.end());
    nums.erase(unique(nums.begin(), nums.end()), nums.end());
    cout << nums.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-8
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = (j); i < (k); i++)
typedef long long ll;
typedef pair<int, int> Pii;
class P {
public:
  int num;
  int cnt;
  bool used[10];
  P() {}
  P(int num, int cnt, bool a[10]) : num(num), cnt(cnt) {
    for (int i = 0; i < 10; i++) {
      used[i] = a[i];
    }
  }
};
int bond(int x, int y) {
  stringstream ss1;
  ss1 << x;
  stringstream ss2;
  ss2 << y;
  string ans = ss1.str() + ss2.str();
  stringstream Ans;
  Ans << ans;
  int res;
  Ans >> res;
  return res;
}
int main() {
  int n;
  int m;
  while (scanf("%d%d", &n, &m), (n | m)) {
    int card[10];
    set<int> S;
    queue<P> Q;
    for (short i = 0; i < n; i++) {
      bool x[10] = {0};
      x[i] = 1;
      scanf("%d", &card[i]);
      Q.push(P(card[i], 1, x));
    }
    short ans = 0;
    while (Q.size()) {
      P p = Q.front();
      Q.pop();
      if (p.cnt == m) {
        if (S.find(p.num) == S.end())
          ans++, S.insert(p.num);
      } else {
        for (int i = 0; i < n; i++) {
          if (p.used[i] == 0) {
            p.used[i] = 1;
            Q.push(P(bond(p.num, card[i]), p.cnt + 1, p.used));
            p.used[i] = 0;
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-8
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = (j); i < (k); i++)
typedef long long ll;
typedef pair<int, int> Pii;
vector<string> vs;
map<string, int> memo;
int n, k;
void solve(int dep, string str, int F) {
  if (dep == n) {
    int cnt = 0;
    rep(i, n) if (F >> i & 1) cnt++;
    if (cnt == k)
      memo[str] = 1;
    return;
  }
  int cnt = 0;
  rep(i, n) {
    if (!(F >> i & 1)) {
      string sub = str;
      sub += vs[i];
      F = F | 1 << i;
      solve(dep + 1, sub, F);
      F = F & ~(1 << i);
      solve(dep + 1, str, F);
    }
  }
  return;
}
int main() {
  while (scanf("%d%d", &n, &k), n) {
    memo.clear();
    vs.clear();
    rep(i, n) {
      string tmp;
      cin >> tmp;
      vs.push_back(tmp);
    }
    rep(i, n) {
      int F = 0;
      F = 1 << i;
      solve(1, vs[i], F);
    }
    printf("%d\\n", memo.size());
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 29)

set<string> st;

void solve(int K, int S, vector<string> &s, string str) {
  if (K == 0) {
    st.insert(str);
    return;
  }

  int N = s.size();
  for (int i = 0; i < N; i++) {
    if (S >> i & 1)
      continue;
    solve(K - 1, S | (1 << i), s, str + s[i]);
  }
}

int main() {
  int N, K;
  while (cin >> N, N) {
    vector<string> s(N);
    cin >> K;
    for (int i = 0; i < N; i++) {
      cin >> s[i];
    }
    st.clear();
    solve(K, 0, s, "");
    cout << st.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 29)

set<string> st;

void solve(int K, int S, vector<string> &s, string str) {
  if (K == 0) {
    st.insert(str);
  }

  int N = s.size();
  for (int i = 0; i < N; i++) {
    if (S >> i & 1)
      continue;
    solve(K - 1, S | (1 << i), s, str + s[i]);
  }
}

int main() {
  int N, K;
  while (cin >> N, N) {
    vector<string> s(N);
    cin >> K;
    for (int i = 0; i < N; i++) {
      cin >> s[i];
    }
    st.clear();
    solve(K, 0, s, "");
    cout << st.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> P;
typedef pair<ll, pair<ll, ll>> P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int n, k;
int s[15];
set<int> st;
bool used[15]{};

void dfs(int i, int t) {
  if (i == 0) {
    st.insert(t);
  }
  REP(j, n) {
    if (used[j])
      continue;
    used[j] = true;
    dfs(i - 1, t * (s[j] > 9 ? 100 : 10) + s[j]);
    used[j] = false;
  }
}

int main() {
  while (1) {
    cin >> n >> k;
    if (n == 0)
      break;
    REP(i, n) cin >> s[i];
    st.clear();
    fill(used, used + 15, false);
    dfs(k, 0);
    cout << st.size() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> P;
typedef pair<ll, pair<ll, ll>> P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int n, k;
string s[15];
set<string> st;
bool used[15]{};

void dfs(int i, string t) {
  if (i == 0) {
    st.insert(t);
  }
  REP(j, n) {
    if (used[j])
      continue;
    used[j] = true;
    dfs(i - 1, t + s[j]);
    used[j] = false;
  }
}

int main() {
  while (1) {
    cin >> n >> k;
    if (n == 0)
      break;
    REP(i, n) cin >> s[i];
    st.clear();
    fill(used, used + 15, false);
    dfs(k, "");
    cout << st.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "stdio.h"
#include <queue>
#include <set>
#include <vector>

using namespace std;

int x[10];

int main() {
  while (1) {
    int n, k;
    scanf("%d", &n);
    scanf("%d", &k);
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &x[i]);
    }

    int w = 1;
    for (int i = 0; i < k; i++) {
      w *= n;
    }

    set<int> sw;
    for (int i = 0; i < w; i++) {
      priority_queue<int> q1;
      vector<int> q2;
      int s = i;
      for (int i = 0; i < k; i++) {
        int l = s % n;
        q1.push(l);
        q2.push_back(l);
        s /= n;
      }

      int flgw = 0;
      int mae = -1;
      for (int ii = 0; ii < k; ii++) {
        if (mae == q1.top()) {
          flgw = 1;
          break;
        }
        mae = q1.top();
        q1.pop();
      }
      if (flgw == 1) {
        continue;
      }

      int g = 0;
      for (int ii = 0; ii < k; ii++) {
        if (x[q2[ii]] < 10) {
          g = g * 10 + x[q2[ii]];
        } else {
          g = g * 100 + x[q2[ii]];
        }
      }
      sw.insert(g);
    }

    printf("%d\\n", sw.size());
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "stdio.h"
#include <queue>
#include <vector>

using namespace std;

char flg[100000000];

int x[10];

int main() {
  for (int i = 0; i < 100000000; i++) {
    flg[i] = 0;
  }
  int n, k;
  scanf("%d", &n);
  scanf("%d", &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }

  int w = 1;
  for (int i = 0; i < k; i++) {
    w *= n;
  }

  for (int i = 0; i < w; i++) {
    priority_queue<int> q1;
    vector<int> q2;
    int s = i;
    for (int i = 0; i < k; i++) {
      int l = s % n;
      q1.push(l);
      q2.push_back(l);
      s /= n;
    }

    int flgw = 0;
    int mae = -1;
    for (int ii = 0; ii < k; ii++) {
      if (mae == q1.top()) {
        flgw = 1;
        break;
      }
      mae = q1.top();
      q1.pop();
    }
    if (flgw == 1) {
      continue;
    }

    int g = 0;
    for (int ii = 0; ii < k; ii++) {
      if (x[q2[ii]] < 10) {
        g = g * 10 + x[q2[ii]];
      } else {
        g = g * 100 + x[q2[ii]];
      }
    }
    flg[g]++;
  }

  int all = 0;
  for (int i = 0; i < 100000000; i++) {
    if (flg[i] > 0) {
      all++;
    }
  }
  printf("%d\\n", all);
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <sstream>
#include <string>
using namespace std;
class P {
public:
  int num;
  int cnt;
  bool used[10];
  P() {}
  P(int num, int cnt, bool a[10]) : num(num), cnt(cnt) {
    for (int i = 0; i < 10; i++) {
      used[i] = a[i];
    }
  }
};
int bond(int x, int y) {
  stringstream ss1;
  ss1 << x;
  stringstream ss2;
  ss2 << y;
  string ans = ss1.str() + ss2.str();
  stringstream Ans;
  Ans << ans;
  int res;
  Ans >> res;
  return res;
}
int main() {
  int n;
  int m;
  while (scanf("%d%d", &n, &m), (n | m)) {
    int card[10];
    set<int> S;
    queue<P> Q;
    for (short i = 0; i < n; i++) {
      bool x[10] = {0};
      x[i] = 1;
      scanf("%d", &card[i]);
      Q.push(P(card[i], 1, x));
    }
    short ans = 0;
    while (Q.size()) {
      P p = Q.front();
      Q.pop();
      if (p.cnt == m) {
        if (S.find(p.num) == S.end())
          ans++, S.insert(p.num);
      } else {
        for (int i = 0; i < n; i++) {
          if (p.used[i] == 0) {
            p.used[i] = 1;
            Q.push(P(bond(p.num, card[i]), p.cnt + 1, p.used));
            p.used[i] = 0;
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <sstream>
#include <string>
using namespace std;
bool memo[100000000];
class P {
public:
  int num;
  int cnt;
  bool used[10];
  P() {}
  P(int num, int cnt, bool a[10]) : num(num), cnt(cnt) {
    for (int i = 0; i < 10; i++) {
      used[i] = a[i];
    }
  }
};
int bond(int x, int y) {
  stringstream ss1;
  ss1 << x;
  stringstream ss2;
  ss2 << y;
  string ans = ss1.str() + ss2.str();
  stringstream Ans;
  Ans << ans;
  int res;
  Ans >> res;
  return res;
}
int main() {
  int n;
  int m;
  while (scanf("%d%d", &n, &m), (n | m)) {
    int card[10];
    queue<P> Q;
    memset(memo, 0, sizeof(memo));
    for (short i = 0; i < n; i++) {
      bool x[10] = {0};
      x[i] = 1;
      scanf("%d", &card[i]);
      Q.push(P(card[i], 1, x));
    }
    short ans = 0;
    while (Q.size()) {
      P p = Q.front();
      Q.pop();
      if (p.cnt == m) {
        if (memo[p.num])
          continue;
        ans++;
        memo[p.num] = true;
      } else {
        for (int i = 0; i < n; i++) {
          if (p.used[i] == 0) {
            p.used[i] = 1;
            Q.push(P(bond(p.num, card[i]), p.cnt + 1, p.used));
            p.used[i] = 0;
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}