Problem p00256 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
long long p1[13][33], p2[13][33], V[401];
tuple<int, int, int> x[13][20][20][18][20];
tuple<int, int, int, int, int> xx[1872002];
void Day_Init() {
  for (int i = 0; i < 429; i++) {
    p1[i / 33][i % 33] = -1;
    p2[i / 33][i % 33] = -1;
  }
  long long CNTS = 0;
  for (int i = 1; i <= 12; i++) {
    long long V = 31;
    if ((i <= 6 && i % 2 == 0) || (i >= 9 && i % 2 == 1))
      V = 30;
    if (i == 2)
      V = 28;
    for (int j = 1; j <= V; j++) {
      p1[i][j] = CNTS;
      CNTS++;
    }
  }
  CNTS = 0;
  for (int i = 1; i <= 12; i++) {
    long long V = 31;
    if ((i <= 6 && i % 2 == 0) || (i >= 9 && i % 2 == 1))
      V = 30;
    if (i == 2)
      V = 29;
    for (int j = 1; j <= V; j++) {
      p2[i][j] = CNTS;
      CNTS++;
    }
  }
  for (int i = 1; i <= 400; i++) {
    long long II = i - 1;
    if (II % 4 == 0 && (II % 100 >= 1 || II % 400 == 0))
      V[i] = V[i - 1] + 366;
    else {
      V[i] = V[i - 1] + 365;
    }
  }
}
void init() {
  Day_Init();
  long long CNTS = 0;
  long long yy = 2012, mm = 12, dd = 21;
  for (int i = 0; i < 13; i++) {
    for (int j = 0; j < 20; j++) {
      for (int k = 0; k < 20; k++) {
        for (int l = 0; l < 18; l++) {
          for (int m = 0; m < 20; m++) {
            x[i][j][k][l][m] = make_tuple(yy, mm, dd);
            xx[CNTS] = make_tuple(i, j, k, l, m);
            CNTS++;
            dd++;
            if (yy % 4 == 0 && (yy % 100 >= 1 || yy % 400 == 0)) {
              if (p2[mm][dd] == -1) {
                mm++;
                dd = 1;
              }
            } else {
              if (p1[mm][dd] == -1) {
                mm++;
                dd = 1;
              }
            }
            if (mm == 13) {
              mm = 1;
              yy++;
            }
          }
        }
      }
    }
  }
}
long long DayCount(long long y, long long m, long long d) {
  long long DD = 0;
  if (y % 4 == 0 && (y % 100 >= 1 || y % 400 == 0)) {
    DD = p2[m][d];
  } else {
    DD = p1[m][d];
  }
  return (V[400] * (y / 400) + V[y % 400]) + DD;
}
tuple<int, int, int> MayaWest(long long I, long long J, long long K,
                              long long L, long long M) {
  return x[I][J][K][L][M];
}
tuple<int, int, int, int, int> WestMaya(long long I, long long J, long long K) {
  long long X =
      (DayCount(I, J, K) + 1872000LL - DayCount(2012, 12, 21)) % 1872000LL;
  return xx[X];
}
int main() {
  init();
  while (true) {
    string S;
    cin >> S;
    if (S == "#")
      break;
    S += '.';
    vector<long long> Vec;
    string T = "";
    for (int i = 0; i < S.size(); i++) {
      if (S[i] == '.') {
        Vec.push_back(stoi(T));
        T = "";
      } else {
        T += S[i];
      }
    }
    if (Vec.size() == 3) {
      tuple<long long, long long, long long, long long, long long> ans =
          WestMaya(Vec[0], Vec[1], Vec[2]);
      cout << get<0>(ans) << '.' << get<1>(ans) << '.' << get<2>(ans) << '.'
           << get<3>(ans) << '.' << get<4>(ans) << endl;
    }
    if (Vec.size() == 5) {
      tuple<long long, long long, long long> ans =
          MayaWest(Vec[0], Vec[1], Vec[2], Vec[3], Vec[4]);
      cout << get<0>(ans) << '.' << get<1>(ans) << '.' << get<2>(ans) << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
long long p1[13][33], p2[13][33], V[401];
tuple<long long, long long, long long> x[13][20][20][18][20];
tuple<long long, long long, long long, long long, long long> xx[1872000];
void init() {
  for (int i = 0; i < 429; i++) {
    p1[i / 33][i % 33] = -1;
    p2[i / 33][i % 33] = -1;
  }
  long long CNTS = 0;
  for (int i = 1; i <= 12; i++) {
    long long V = 31;
    if ((i <= 6 && i % 2 == 0) || (i >= 9 && i % 2 == 1))
      V = 30;
    if (i == 2)
      V = 28;
    for (int j = 1; j <= V; j++) {
      p1[i][j] = CNTS;
      CNTS++;
    }
  }
  CNTS = 0;
  for (int i = 1; i <= 12; i++) {
    long long V = 31;
    if ((i <= 6 && i % 2 == 0) || (i >= 9 && i % 2 == 1))
      V = 30;
    if (i == 2)
      V = 29;
    for (int j = 1; j <= V; j++) {
      p2[i][j] = CNTS;
      CNTS++;
    }
  }
  for (int i = 1; i <= 400; i++) {
    long long II = i - 1;
    if (II % 4 == 0 && (II % 100 >= 1 || II % 400 == 0))
      V[i] = V[i - 1] + 366;
    else {
      V[i] = V[i - 1] + 365;
    }
  }
  CNTS = 0;
  long long yy = 2012, mm = 12, dd = 21;
  for (int i = 0; i < 13; i++) {
    for (int j = 0; j < 20; j++) {
      for (int k = 0; k < 20; k++) {
        for (int l = 0; l < 18; l++) {
          for (int m = 0; m < 20; m++) {
            x[i][j][k][l][m] = make_tuple(yy, mm, dd);
            xx[CNTS] = make_tuple(i, j, k, l, m);
            CNTS++;
            dd++;
            if (yy % 4 == 0 && (yy % 100 >= 1 || yy % 400 == 0)) {
              if (p2[mm][dd] == -1) {
                mm++;
                dd = 1;
              }
            } else {
              if (p1[mm][dd] == -1) {
                mm++;
                dd = 1;
              }
            }
            if (mm == 13) {
              mm = 1;
              yy++;
            }
          }
        }
      }
    }
  }
}
long long DayCount(long long y, long long m, long long d) {
  long long DD = 0;
  if (y % 4 == 0) {
    DD = p2[m][d];
  } else {
    DD = p1[m][d];
  }
  return (V[400] * (y / 400) + V[y % 400]) + DD;
}
tuple<long long, long long, long long>
MayaWest(long long I, long long J, long long K, long long L, long long M) {
  return x[I][J][K][L][M];
}
tuple<long long, long long, long long, long long, long long>
WestMaya(long long I, long long J, long long K) {
  long long X = (DayCount(I, J, K) - DayCount(2012, 12, 21)) % 1872000;
  return xx[X];
}
int main() {
  init();
  while (true) {
    string S;
    cin >> S;
    if (S == "#")
      break;
    S += '.';
    vector<long long> Vec;
    string T = "";
    for (int i = 0; i < S.size(); i++) {
      if (S[i] == '.') {
        Vec.push_back(stoi(T));
        T = "";
      } else {
        T += S[i];
      }
    }
    if (Vec.size() == 3) {
      tuple<long long, long long, long long, long long, long long> ans =
          WestMaya(Vec[0], Vec[1], Vec[2]);
      cout << get<0>(ans) << '.' << get<1>(ans) << '.' << get<2>(ans) << '.'
           << get<3>(ans) << '.' << get<4>(ans) << endl;
    }
    if (Vec.size() == 5) {
      tuple<long long, long long, long long> ans =
          MayaWest(Vec[0], Vec[1], Vec[2], Vec[3], Vec[4]);
      cout << get<0>(ans) << '.' << get<1>(ans) << '.' << get<2>(ans) << endl;
    }
  }
  return 0;
}