Problem p00469 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);

int main() {
  int N, K;
  while (cin >> N >> K && N && K) {
    vector<int> R(N);
    int ans = 0;
    map<int, bool> appeared;
    for (int i = 0; i < N; i++)
      cin >> R[i];

    for (int i = 0; i < (1 << N); i++) {
      vector<int> cards;
      for (int k = 0; k < N; k++) {
        if ((i >> k) & 1)
          cards.push_back(R[k]);
      }
      if (cards.size() != K)
        continue;
      sort(cards.begin(), cards.end());

      do {
        string joined = "";
        for (auto val : cards) {
          joined += to_string(val);
        }
        if (!appeared[stoi(joined)]) {
          appeared[stoi(joined)] = true;
          ans++;
        }
      } while (next_permutation(cards.begin(), cards.end()));
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);

bool ans[10000001];
int a[11];
int n, k;

void calc(int i, int j, int qqq, int l) {
  int num = a[i];
  if (a[j] >= 10)
    num *= 100;
  else
    num *= 10;
  num += a[j];
  if (3 <= k) {
    if (a[qqq] >= 10)
      num *= 100;
    else
      num *= 10;
    num += a[qqq];
    if (4 <= k) {
      if (a[l] >= 10)
        num *= 100;
      else
        num *= 10;
      num += a[l];
    }
  }
  // cout << num << endl;
  // system("pause");
  ans[num] = true;
}

int main() {
  while (cin >> n >> k && n && k) {
    for (int i = 0; i < 10000001; i++)
      ans[i] = false;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int qqq = 0; qqq < n; qqq++) {
          for (int l = 0; l < n; l++) {
            if (i == j)
              continue;
            if (i == qqq)
              continue;
            if (i == l)
              continue;
            if (j == qqq)
              continue;
            if (j == l)
              continue;
            if (qqq == l)
              continue;
            calc(i, j, qqq, l);
          }
        }
      }
    }
    int s = 0;
    for (int i = 0; i < 10000000; i++) {
      if (ans[i])
        s++;
    }
    cout << s << endl;
  }
}