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

#include <iostream>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;
int main() {

  vector<long> p;
  set<long> w;
  int n, k;
  string m[1000];
  string a;
  int e;
  int count = 0;
  long y;
  int f = 0;
  while (1) {
    w.clear();
    count = 0;
    cin >> n >> k;
    if (n == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      cin >> m[i];
    }
    if (k == 4) {
      for (int i = 0; i < n; i++) {
        for (int i2 = 0; i2 < n; i2++) {
          for (int i3 = 0; i3 < n; i3++) {
            for (int i4 = 0; i4 < n; i4++) {
              if (i != i2 && i3 != i4 && i != i3 && i != i4 && i2 != i3 &&
                  i2 != i4) {
                a = m[i] + m[i2] + m[i3] + m[i4];
                y = atol(a.c_str());
                w.insert(y);
                a.clear();
              }
            }
          }
        }
      }
    }
    long u = 0;
    if (k == 3) {
      for (int i = 0; i < n; i++) {
        for (int i2 = 0; i2 < n; i2++) {
          for (int i3 = 0; i3 < n; i3++) {
            if (i != i2 && i != i3 && i2 != i3) {
              a = m[i];
              a += m[i2];
              a += m[i3];

              y = atol(a.c_str());
              w.insert(y);
              a.clear();
            }
          }
        }
      }
    }
    int o;
    if (k == 2) {
      for (int i = 0; i < n; i++) {
        for (int i2 = 0; i2 < n; i2++) {

          if (i != i2) {
            a = m[i] + m[i2];
            y = atol(a.c_str());
            w.insert(y);
            a.clear();
          }
        }
      }
    }
    cout << w.size() << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;
int main() {

  bool p[1000001];
  int n, k;
  string m[1000];
  string a;
  int count = 0;
  while (1) {
    count = 0;
    cin >> n >> k;
    if (n == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      cin >> m[i];
    }
    if (k == 4) {
      for (int i = 0; i < n; i++) {
        for (int i2 = 0; i2 < n; i2++) {
          for (int i3 = 0; i3 < n; i3++) {
            for (int i4 = 0; i4 < n; i4++) {
              if (i != i2 && i3 != i4 && i != i3 && i != i4 && i2 != i3 &&
                  i2 != i4) {
                a = m[i] + m[i2] + m[i3] + m[i4];
                p[atol(a.c_str())] = false;
                a.clear();
              }
            }
          }
        }
      }
    }
    long u = 0;
    if (k == 3) {
      for (int i = 0; i < n; i++) {
        for (int i2 = 0; i2 < n; i2++) {
          for (int i3 = 0; i3 < n; i3++) {
            if (i != i2 && i != i3 && i2 != i3) {
              a = m[i];
              a += m[i2];
              a += m[i3];

              u = atol(a.c_str());
              p[u] = false;
              a.clear();
            }
          }
        }
      }
    }
    int o;
    if (k == 2) {
      for (int i = 0; i < n; i++) {
        for (int i2 = 0; i2 < n; i2++) {

          if (i != i2) {
            a = m[i] + m[i2];
            o = atoi(a.c_str());

            p[o] = false;
            a.clear();
          }
        }
      }
    }

    for (int i = 0; i < 100000; i++) {
      if (!p[i])
        count++;
    }
    cout << count << endl;
  }
}