alexjercan/bugnet huggingface dataset:
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, m;

  while (scanf("%d%d", &n, &m), n != 0 && m != 0) {
    bool fr[501][501] = {false};
    bool mem[501] = {false};

    for (int i = 0; i < m; i++) {
      int m_a, m_b;
      scanf("%d%d", &m_a, &m_b);

      // friend check
      fr[m_a][m_b] = true;
      fr[m_b][m_a] = true;
    }

    for (int i = 2; i <= n; i++) {
      if (fr[1][i] == false) {
        continue;
      }
      mem[i] = true;
      for (int j = 2; j <= n; j++) {
        if (fr[i][j] == true) {
          mem[j] = true;
        }
      }
    }

    int cnt = 0;

    for (int i = 2; i <= n; i++) {
      if (mem[i] == true) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }

  return (0);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, m;

  while (scanf("%d%d", &n, &m), n != 0 && m != 0) {
    bool fr[502][502] = {false};
    bool mem[502] = {false};

    for (int i = 0; i < m; i++) {
      int m_a, m_b;
      scanf("%d%d", &m_a, &m_b);

      // friend check
      fr[m_a][m_b] = true;
      fr[m_b][m_a] = true;
    }

    for (int i = 2; i <= m; i++) {
      if (fr[1][i] == false) {
        continue;
      }
      mem[i] = true;
      for (int j = 2; j <= m; j++) {
        if (fr[i][j] == true) {
          mem[j] = true;
        }
      }
    }

    int cnt = 0;

    for (int i = 2; i <= m; i++) {
      if (mem[i] == true) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }

  return (0);
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>

int fr[501];
int c[10001], d[10001];
using namespace std;
int main(void) {
  int n, m;
  int a, b;
  while (scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < 501; i++)
      fr[i] = 0, c[i] = 0, d[i] = 0;

    int j = 0;
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &a, &b);

      if (a == 1)
        fr[b] = 1;
      else if (b == 1)
        fr[a] = 1;
      else {
        c[j] = a, d[j] = b;
        j++;
      }
    }

    for (int i = 0; i < j; i++) {
      if (fr[c[i]] == 1 && fr[d[i]] != 1)
        fr[d[i]] = 2;
      else if (fr[d[i]] == 1 && fr[c[i]] != 1)
        fr[c[i]] = 2;
    }

    int cnt = 0;
    for (int i = 0; i < 501; i++)
      if (fr[i] != 0)
        cnt++;

    printf("%d\\n", cnt);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

int fr[510];
int c[510], d[510];
using namespace std;
int main(void) {
  int n, m;
  while (scanf("%d%d", &n, &m), n + m) {
    for (int i = 0; i < 501; i++) {
      fr[i] = 0;
      c[i] = 0;
      d[i] = 0;
    }

    int j = 0;
    int a, b;
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &a, &b);
      if (a == 1)
        fr[b] = 1;
      else if (b == 1)
        fr[a] = 1;
      else {
        c[j] = a, d[j] = b;
        j++;
      }
    }

    for (int i = 0; i < j; i++) {
      if (fr[c[i]] == 1 && fr[d[i]] != 1)
        fr[d[i]] = 2;
      else if (fr[d[i]] == 1 && fr[c[i]] != 1)
        fr[c[i]] = 2;
    }

    int cnt = 0;
    for (int i = 0; i < n; i++)
      if (fr[i] != 0)
        cnt++;

    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P; // cost,node

struct node {
  vector<int> et;
  int size;
} *f;
int cost[500];

void BFS(int start) {
  queue<P> que;
  P now;
  que.push(P(0, start));

  while (!que.empty()) {
    now = que.front();
    que.pop();
    if (cost[now.second] != 10000)
      continue;
    cost[now.second] = now.first;

    node *no = &f[now.second];
    for (int i = 0; i < no->size; i++) {
      que.push(P(now.first + 1, no->et[i]));
    }
  }
}

int main() {
  int N, M, a, b, i;
  int ans;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    f = new node[N];
    ans = 0;
    for (i = 0; i < N; i++) {
      cost[i] = 10000;
      f[i].size = 0;
    }

    for (i = 0; i < M; i++) {
      cin >> a >> b;
      a--;
      b--;
      f[a].et.push_back(b);
      f[b].et.push_back(a);
      f[a].size++;
      f[b].size++;
    }
    BFS(0);

    for (i = 1; i < N; i++) {
      if (cost[i] <= 2)
        ans++;
    }
    cout << ans << endl;

    delete[] f;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P; // cost,node

struct node {
  vector<int> et;
  int size;
} *f;
int cost[500];

void BFS(int start) {
  queue<P> que;
  P now;
  que.push(P(0, 0));

  while (!que.empty()) {
    now = que.front();
    que.pop();
    if (cost[now.second] != 10000)
      continue;
    cost[now.second] = now.first;

    node *no = &f[now.second];
    for (int i = 0; i < no->size; i++) {
      que.push(P(now.first + 1, no->et[i]));
    }
  }
}

int main() {
  int N, M, a, b, i;
  int ans;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    f = new node[502];
    ans = 0;
    for (i = 0; i < N; i++) {
      cost[i] = 10000;
    }

    for (i = 0; i < M; i++) {
      cin >> a >> b;
      a--;
      b--;
      f[a].et.push_back(b);
      f[b].et.push_back(a);
      f[a].size++;
      f[b].size++;
    }
    BFS(0);

    for (i = 1; i < N; i++) {
      if (cost[i] <= 2)
        ans++;
    }
    cout << ans << endl;

    delete[] f;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

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

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

int main() {
  int n, m;
  pair<int, int> p[10010];
  while (cin >> n >> m, n | m) {
    set<int> st1, st2;
    st1.insert(1);
    rep(i, m) {
      cin >> p[i].first >> p[i].second;
      if (p[i].first == 1 || p[i].second == 1) {
        st1.insert(p[i].first);
        st1.insert(p[i].second);
      }
    }
    rep(i, m) {
      if (st1.find(p[i].first) != st1.end() &&
          st1.find(p[i].second) == st1.end()) {
        st2.insert(p[i].second);
      }
      if (st1.find(p[i].second) != st1.end() &&
          st1.find(p[i].first) == st1.end()) {
        st2.insert(p[i].first);
      }
    }

    cout << (st1.size() - 1) + (st2.size()) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

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

int main() {
  int n, m;
  pair<int, int> p[512];
  while (scanf("%d%d", &n, &m), n | m) {
    set<int> st1, st2;
    st1.insert(1);
    rep(i, m) {
      cin >> p[i].first >> p[i].second;
      if (p[i].first == 1 || p[i].second == 1) {
        st1.insert(p[i].first);
        st1.insert(p[i].second);
      }
    }
    rep(i, m) {
      if (st1.find(p[i].first) != st1.end() &&
          st1.find(p[i].second) == st1.end()) {
        st2.insert(p[i].second);
      }
      if (st1.find(p[i].second) != st1.end() &&
          st1.find(p[i].first) == st1.end()) {
        st2.insert(p[i].first);
      }
    }

    cout << (st1.size() - 1) + (st2.size()) << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
using namespace std;

int n, k;
int array[10];

int c[4];
bool use[10];
char str[10000][100];
int m;

int number[10000];
int p;

void Search(int i, int j) {
  if (j == k) {
    for (int l = 0; l < k; l++) {
      char nstr[3];
      sprintf(nstr, "%d", c[l]);
      strcat(str[m], nstr);
    }

    int num;
    sscanf(str[m], "%d", &num);
    //		printf("%d\\n", num);

    bool f = false;
    for (int l = 0; l < p; l++) {
      if (number[l] == num) {
        f = true;
        break;
      }
    }

    if (!f) {
      number[p] = num;
      p++;
    }

    m++;
    return;
  }

  if (i == n) {
    return;
  }

  Search(i + 1, j);

  if (use[i]) {
    return;
  }

  c[j] = array[i];

  use[i] = true;

  Search(0, j + 1);

  use[i] = false;
}

int main() {
  while (scanf("%d %d", &n, &k), n + k) {
    memset(array, 0, sizeof(array));
    memset(c, 0, sizeof(c));
    memset(use, 0, sizeof(use));
    memset(str, 0, sizeof(str));
    memset(number, 0, sizeof(number));
    m = 0;
    p = 0;

    for (int i = 0; i < n; i++) {
      scanf("%d", &array[i]);
    }

    Search(0, 0);

    printf("%d\\n", p);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;

int n, k;
int array[10];

int c[4];
bool use[10];
char str[10000][100];
int m;

bool number[100000000];

void Search(int i, int j) {
  if (j == k) {
    for (int l = 0; l < k; l++) {
      char nstr[3];
      sprintf(nstr, "%d", c[l]);
      strcat(str[m], nstr);
    }

    int num;
    sscanf(str[m], "%d", &num);
    //		printf("%d\\n", num);
    number[num] = true;

    m++;
    return;
  }

  if (i == n) {
    return;
  }

  Search(i + 1, j);

  if (use[i]) {
    return;
  }

  c[j] = array[i];

  use[i] = true;

  Search(0, j + 1);

  use[i] = false;
}

int main() {
  while (scanf("%d %d", &n, &k), n + k) {
    memset(array, 0, sizeof(array));
    memset(c, 0, sizeof(c));
    memset(use, 0, sizeof(use));
    memset(str, 0, sizeof(str));
    memset(number, 0, sizeof(number));
    m = 0;

    for (int i = 0; i < n; i++) {
      scanf("%d", &array[i]);
    }

    Search(0, 0);

    int cnt = 0;
    for (int i = 0; i < 100000000; i++) {
      cnt += number[i];
    }

    printf("%d\\n", cnt);
  }

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

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

int n = {}, k = {};
string integer[11] = {};
vector<string> num = {};
int chousen[11] = {};
string complete_integer[10000] = {};
string answer = {};
int integer_count = 0;

void operation() {
  for (int i = 1; i <= n; i++) {
    if (chousen[i] == 0) {

      chousen[i] = 1;
      num.push_back(integer[i]);

      if (num.size() == k) {
        for (int i = 1; i <= k; i++) {
          answer += num[i - 1];
        }
        for (int i = 1; i <= integer_count; i++) {
          if (answer == complete_integer[i]) {
            goto none;
          }
        }
        integer_count += 1;
        complete_integer[integer_count] = answer;

      none:;
        answer = {};
      } else {
        operation();
      }
      num.pop_back();
      chousen[i] = 0;
    }
  }
}

int main() {

  while (true) {

    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      cin >> integer[i];
    }

    operation();

    cout << integer_count << endl;

    n = {};
    k = {};
    for (int i = 0; i <= 10; i++) {
      integer[i] = {};
      chousen[i] = {};
    }
    num = {};
    for (int i = 0; i <= 9999; i++) {
      complete_integer[i] = {};
    }
    answer = {};
    integer_count = 0;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int n = {}, k = {};
string integer[11] = {};
vector<string> num = {};
int chousen[10] = {};
string complete_integer[10000] = {};
string answer = {};
int integer_count = 1;
bool check[100000000] = {};
int counter = 0;

void operation() {
  for (int i = 1; i <= n; i++) {
    if (chousen[i] == 0) {

      chousen[i] = 1;
      num.push_back(integer[i]);

      if (num.size() == k) {
        for (int i = 1; i <= k; i++) {
          answer += num[i - 1];
        }
        complete_integer[integer_count] = answer;
        integer_count += 1;
        answer = {};
      } else {
        operation();
      }
      num.pop_back();
      chousen[i] = 0;
    }
  }
}

void compare() {
  for (int i = 1; i <= integer_count - 1; i++) {
    // cout << complete_integer[i] << endl;
    if (check[stoi(complete_integer[i])]) {
    } else {
      check[stoi(complete_integer[i])] = 1;
      counter += 1;
    }
  }
}

int main() {

  while (true) {

    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      cin >> integer[i];
    }

    operation();
    compare();
    cout << counter << endl;
    n = {};
    k = {};
    for (int i = 0; i <= 9; i++) {

      integer[i] = {};
      chousen[i] = {};
    }
    num = {};
    for (int i = 0; i <= 9999; i++) {
      complete_integer[i] = {};
    }
    answer = {};
    integer_count = 1;
    for (int i = 0; i <= 99999999; i++) {
      check[i] = {};
    }
    counter = 0;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Runtime Error

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

int n, k, ans;
string save, cards[11];
vector<string> v;

void solve(int a, int b, int c) {
  for (int i = 1; i <= n; i++) {
    if (a != b && a != c && a != i && b != c && b != i && c != i) {
      if (k == 3) {
        v.push_back(save + cards[i]);
      } else {
        if (c == -1) {
          solve(a, b, i);
        } else {
          v.push_back(save + cards[c] + cards[i]);
        }
      }
    }
  }
}

int main() {
  while (1) {
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }

    for (int i = 1; i <= n; i++) {
      cin >> cards[i];
    }

    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        if (a != b) {
          save = cards[a] + cards[b];
          if (k >= 3) {
            solve(a, b, -1);
          } else {
            v.push_back(save);
          }
        }
      }
    }

    ans = 1;
    sort(v.begin(), v.end());
    for (int i = 1; i < v.size(); i++) {
      if (v[i] != v[i - 1]) {
        ans++;
      }
    }

    cout << ans << endl;

    int x = v.size();
    for (int i = 0; i < x; i++) {
      v.pop_back();
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  while (true) {
    int n, k, ans = 0;
    string cards[11];
    vector<string> v;

    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }

    for (int i = 1; i <= n; i++) {
      cin >> cards[i];
    }

    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        if (a != b) {
          v.push_back(cards[a] + cards[b]);

          for (int c = 1; c <= n; c++) {
            if (k >= 3) {
              if (a != c && b != c) {
                v.back() += cards[c];
              }
            } else {
              break;
            }
            for (int d = 1; d <= n; d++) {
              if (k >= 4) {
                if (a != d && b != d && c != d) {
                  v.back() += cards[d];
                }
              } else {
                break;
              }
            }
          }
        }
      }
    }

    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      if (v[i] != v[i + 1] || i == v.size() - 1) {
        ans++;
      }
    }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFll = 1e18;
#define put(n) cout << (n) << endl
#define Cout(n) cout << (n)
#define rep(i, num, N) for (int(i) = (num); (i) < (N); ++(i))
#define rrep(i, num, N) for (int(i) = (num); (i) > (N); --(i))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define MP make_pair
#define pb(q) push_back(q)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;

  while (cin >> N >> K, N != 0 && K != 0) {
    set<string> num;
    vector<string> card(N);
    rep(i, 0, N) cin >> card[i];
    sort(all(card));

    do {
      string s = "";
      rep(i, 0, K) s += card[i];
      num.insert(s);
    } while (next_permutation(all(card)));
    put(num.size());
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFll = 1e18;
#define put(n) cout << (n) << endl
#define Cout(n) cout << (n)
#define rep(i, num, N) for (int(i) = (num); (i) < (N); ++(i))
#define rrep(i, num, N) for (int(i) = (num); (i) > (N); --(i))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define MP make_pair
#define pb(q) push_back(q)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;

  while (cin >> N >> K, N != 0 && K != 0) {
    set<string> num;
    vector<string> card(N);
    rep(i, 0, N) cin >> card[i];
    sort(all(card));

    do {
      rep(i, 0, N - K) {
        string s = "";
        rep(j, i, K + i) { s += card[j]; }
        num.insert(s);
      }

    } while (next_permutation(all(card)));

    put(num.size());
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <set>
#include <string>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define rept(X, Y, Z) for (int(X) = (Y); (X) < (Z); ++(X))

int N, K;
std::string num[11];
std::set<std::string> data;

void f(int used, int cnt, std::string d) {
  if (cnt == 0) {
    data.insert(d);
    return;
  }

  rep(i, N) {
    if (used & (1 << i))
      continue;

    f(used | 1 << i, cnt - 1, d + num[i]);
  }
}

int main() {
  while (scanf("%d%d", &N, &K), N * K) {
    data.clear();

    rep(i, N) std::cin >> num[i];

    f(0, K, "");

    std::cout << data.size() << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <set>
#include <string>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define rept(X, Y, Z) for (int(X) = (Y); (X) < (Z); ++(X))

int N, K;
std::string num[11];
std::set<std::string> data;

void f(int used, int cnt, std::string d) {
  if (cnt == 0)
    data.insert(d);

  rep(i, N) {
    if (used & 1 << i)
      continue;

    f(used | 1 << i, cnt - 1, d + num[i]);
  }
}

int main() {
  while (scanf("%d%d", &N, &K), N * K) {
    data.clear();

    rep(i, N) std::cin >> num[i];

    f(0, K, "");

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

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#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 mp make_pair
#define fi first
#define sc second
int n, k, c[11];
bool al[11];
set<int> se;
void search(int d, int s) {
  if (d >= k) {
    se.insert(s);
    return;
  }
  REP(i, n) {
    if (!al[i]) {
      al[i] = true;
      int t = s * 10;
      if (c[i] > 9)
        t *= 10;
      search(d + 1, t + c[i]);
      al[i] = false;
    }
  }
}
bool solve() {
  cin >> n >> k;
  if (n == 0 && k == 0)
    return false;
  se.clear();
  REP(i, n)
  cin >> c[i];
  REP(i, n)
  al[i] = false;

  search(0, 0);
  printf("%d\\n", se.size());
  return true;
}
int main() {
  while (solve())
    ;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#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 mp make_pair
#define fi first
#define sc second
int n, k, c[11];
bool a[100000000];
bool al[11];
void search(int d, int s) {
  if (d >= k) {
    a[s] = true;
    return;
  }
  REP(i, n) {
    if (!al[i]) {
      al[i] = true;
      int t = s * 10;
      if (c[i] > 9)
        t *= 10;
      search(d + 1, t + c[i]);
      al[i] = false;
    }
  }
}
bool solve() {
  cin >> n >> k;
  if (n == 0 && k == 0)
    return false;

  REP(i, n)
  cin >> c[i];
  REP(i, 100000000)
  a[i] = false;
  REP(i, n)
  al[i] = false;

  search(0, 0);
  int sum = 0;
  REP(i, 100000000)
  sum += a[i];
  printf("%d\\n", sum);
  return true;
}
int main() {
  while (solve())
    ;
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (!n & !k)
      break;
    int a[10], use[10] = {};
    for (int i = 0; i < n; ++i)
      cin >> a[i];
    sort(a, a + n);
    for (int i = 0; i < k; ++i)
      use[i] = 1;
    vector<string> v;
    do {
      int choise[k];
      for (int i = 0, j = 0; i < n; ++i) {
        if (use[i])
          choise[j++] = a[i];
      }
      do {
        string s;
        for (int i = 0; i < k; ++i)
          s += to_string(choise[i]);
        bool can = true;
        for (int i = 0; i < v.size(); ++i) {
          if (s == v[i]) {
            can = false;
            break;
          }
        }
        if (can)
          v.push_back(s);
      } while (next_permutation(choise, choise + k));
    } while (prev_permutation(use, use + n));
    cout << v.size() << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (!n & !k)
      break;
    int a[n];
    for (int i = 0; i < n; ++i)
      cin >> a[i];
    sort(a, a + n);
    vector<string> v;
    do {
      string s;
      for (int i = 0; i < k; ++i)
        s += to_string(a[i]);
      bool can = true;
      for (int i = 0; i < v.size(); ++i) {
        if (s == v[i]) {
          can = false;
          break;
        }
      }
      if (can)
        v.push_back(s);
    } while (next_permutation(a, a + n));
    cout << v.size() << endl;
  }

  return 0;
}