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

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

std::vector<std::string> split(const std::string &str, char sep) {
  std::vector<std::string> v;
  std::stringstream ss(str);
  std::string buffer;
  while (std::getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

inline bool reap(int y) {
  if (y % 4 == 0 && y % 100 != 0) {
    return true;
  } else if (y % 400 == 0) {
    return true;
  }
  return false;
}

int main() {

  string s;

  while (1) {

    cin >> s;

    if (s[0] == '#')
      break;

    vector<string> vs = split(s, '.');

    if (vs.size() == 3) {
      long long int b, ka, t, w, ki;
      b = ka = t = w = ki = 0;

      int y, m, d;

      stringstream ss;

      ss << vs[0];
      ss >> y;
      ss.clear();
      ss << vs[1];
      ss >> m;
      ss.clear();
      ss << vs[2];
      ss >> d;
      ss.clear();

      if (y == 2012) {
        ki = d - 21;
      }

      else {

        /*while (y != 2012) {

            do {

                while (d != 0) {
                    d--;
                    ki++;
                }

                m--;

                if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 ||
        m == 0) { d = 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { d
        = 30; } else if (m == 2 && reap(y)) { d = 29; } else { d = 28;
                }

            } while (m != 0);

            if (ki >= 20) {
                w += ki / 20;
                ki %= 20;
            }
            if (w >= 18) {
                t += w / 18;
                w %= 18;
            }
            if (t >= 20) {
                ka += t / 20;
                t %= 20;
            }
            if (ka >= 20) {
                b += ka / 20;
                ka %= 20;
            }
            if (b >= 13) {
                b %= 13;
            }

            m = 12;

            y--;

        }*/

        /*if(d>21){
            ki += d-21;
            d = 21;
        }
        else{
            m--;
            if(m==0){
                ki += d+31-21;
                m = 12;
                y--;
            }
            else if(m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 1){
                ki += d+31-21;
            }
            else if(m == 4 || m == 6 || m == 9 || m == 11){
                ki += d+30-21;
            }
            else if(m == 2 && reap(y)){
                ki += d+29-21;
            }
            else{
                ki += d+28-21;
            }
            ki++;
            d = 21;
        }

        while(y!=2012){

            while(m!=0){
                if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 ||
        m == 12){ ki += 31;
                }
                else if(m == 4 || m == 6 || m == 9 || m == 11){
                    ki += 30;
                }
                else if(m == 2 && reap(y)){
                    ki += 29;
                }
                else{
                    ki += 28;
                }
                m--;
            }


            if(ki>=20){
                w += ki/20;
                ki %= 20;
            }
            if(w>=18){
                t += w/18;
                w %= 18;
            }
            if(t>=20){
                ka += t/20;
                t %= 20;
            }
            if(ka>=20){
                b += ka/20;
                ka %= 20;
            }
            if(b>=13){
                b %= 13;
            }

            m = 12;

            y--;

        }*/

        long long int allday = 0;

        allday = 11;

        int tmpy = 2013;

        while (y != tmpy) {
          if (reap(tmpy)) {
            allday += 366;
          } else {
            allday += 365;
          }
          tmpy++;
          allday %= 1872000;
        }

        for (int i = 1; i < m; i++) {

          if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 ||
              i == 0) {
            allday += 31;
          } else if (i == 4 || i == 6 || i == 9 || i == 11) {
            allday += 30;
          } else if (i == 2 && reap(y)) {
            allday += 29;
          } else if (i == 2) {
            allday += 28;
          } else {
            break;
          }
        }

        allday += d - 1;

        /*do {

            while (d != 0) {
                d--;
                allday++;
            }

            m--;

            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m
        == 0) { d = 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { d =
        30; } else if (m == 2 && reap(y)) { d = 29; } else { d = 28;
            }

        } while (m != 0);*/

        b = allday / (20 * 18 * 20 * 20);
        allday %= 20 * 18 * 20 * 20;
        ka = allday / (20 * 18 * 20);
        allday %= 20 * 18 * 20;
        t = allday / (20 * 18);
        allday %= 20 * 18;
        w = allday / 20;
        allday %= 20;
        ki = allday;

        b %= 13;

        /*if (ki >= 20) {
            w += ki / 20;
            ki %= 20;
        }
        if (w >= 18) {
            t += w / 18;
            w %= 18;
        }
        if (t >= 20) {
            ka += t / 20;
            t %= 20;
        }
        if (ka >= 20) {
            b += ka / 20;
            ka %= 20;
        }
        if (b >= 13) {
            b %= 13;
        }*/

        /*if(ki>=20){
            w += ki/20;
            ki %= 20;
        }
        if(w>=18){
            t += w/18;
            w %= 18;
        }
        if(t>=20){
            ka += t/20;
            t %= 20;
        }
        if(ka>=20){
            b += ka/20;
            ka %= 20;
        }
        if(b>=13){
            b %= 13;
        }

        //ki += 31 - 21;

        if (ki >= 20) {
            w += ki / 20;
            ki %= 20;
        }
        if (w >= 18) {
            t += w / 18;
            w %= 18;
        }
        if (t >= 20) {
            ka += t / 20;
            t %= 20;
        }
        if (ka >= 20) {
            b += ka / 20;
            ka %= 20;
        }
        if (b >= 13) {
            b %= 13;
        }*/

        /*if(ki<0){
            if(w==0){
                if(t==0){
                    if(ka==0){
                        b--;
                        ka = 19;
                        t = 19;
                        w = 17;
                        ki += 19;
                    }
                    else{
                        ka--;
                        t = 19;
                        w = 17;
                        ki += 19;
                    }
                }
                else{
                    t--;
                    w = 17;
                    ki += 19;
                }
            }
            else{
                w--;
                ki += 19;
            }
        }*/
      }

      printf("%lld.%lld.%lld.%lld.%lld\\n", b, ka, t, w, ki);

    } else {

      long long int day = 0;

      long long int b, ka, t, w, ki;
      b = ka = t = w = ki = 0;

      long long int y, m, d;

      stringstream ss;

      ss << vs[0];
      ss >> b;
      ss.clear();
      ss << vs[1];
      ss >> ka;
      ss.clear();
      ss << vs[2];
      ss >> t;
      ss.clear();
      ss << vs[3];
      ss >> w;
      ss.clear();
      ss << vs[4];
      ss >> ki;
      ss.clear();

      day =
          ki + w * 20 + t * 20 * 18 + ka * 20 * 18 * 20 + b * 20 * 18 * 20 * 20;

      y = 2012;
      m = 12;
      d = 21;
      /*
                  if(day > 10){
                      y++; m = 1; d = 1; day -= 11;
                  }
                  else{
                      d += day;
                      day = 0;
                  }

                  while(day!=0){

                      if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m ==
         10){ if(day >= 31){ m++; day -= 31;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 4 || m == 6 || m == 9 || m == 11){
                          if(day >= 30){
                              m++;
                              day -= 30;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 2 && reap(y)){
                          if(day >= 29){
                              m++;
                              day -= 29;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 2){
                          if(day >= 28){
                              m++;
                              day -= 28;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 12) {
                          if(day >= 31){
                              m = 1;
                              y++;
                              day -= 31;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }

                  }
      */

      while (day != 0) {

        if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10) {
          while (day != 0 && d != 32) {
            day--;
            d++;
          }
          if (d == 32) {
            m++;
            d = 1;
          }
          /*if(day >= 31){
              m++;
              day -= 31;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 4 || m == 6 || m == 9 || m == 11) {
          while (day != 0 && d != 31) {
            day--;
            d++;
          }
          if (d == 31) {
            m++;
            d = 1;
          }
          /*if(day >= 30){
              m++;
              day -= 30;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 2 && reap(y)) {
          while (day != 0 && d != 30) {
            day--;
            d++;
          }
          if (d == 30) {
            m++;
            d = 1;
          }
          /*if(day >= 29){
              m++;
              day -= 29;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 2) {
          while (day != 0 && d != 29) {
            day--;
            d++;
          }
          if (d == 29) {
            m++;
            d = 1;
          }
          /*if(day >= 28){
              m++;
              day -= 28;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 12) {
          while (day != 0 && d != 32) {
            day--;
            d++;
          }
          if (d == 32) {
            m = 1;
            d = 1;
            y++;
          }
          /*if(day >= 31){
              m = 1;
              y++;
              day -= 31;
          }
          else{
              d += day;
              day = 0;
          }*/
        }
      }

      printf("%lld.%lld.%lld\\n", y, m, d);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

std::vector<std::string> split(const std::string &str, char sep) {
  std::vector<std::string> v;
  std::stringstream ss(str);
  std::string buffer;
  while (std::getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

inline bool reap(int y) {
  if (y % 4 == 0 && y % 100 != 0) {
    return true;
  } else if (y % 400 == 0) {
    return true;
  }
  return false;
}

int main() {

  string s;

  while (1) {

    cin >> s;

    if (s[0] == '#')
      break;

    vector<string> vs = split(s, '.');

    if (vs.size() == 3) {
      int b, ka, t, w, ki;
      b = ka = t = w = ki = 0;

      int y, m, d;

      stringstream ss;

      ss << vs[0];
      ss >> y;
      ss.clear();
      ss << vs[1];
      ss >> m;
      ss.clear();
      ss << vs[2];
      ss >> d;
      ss.clear();

      if (y == 2012) {
        ki = d - 21;
      }

      else {

        /*while (y != 2012) {

            do {

                while (d != 0) {
                    d--;
                    ki++;
                }

                m--;

                if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 ||
        m == 0) { d = 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { d
        = 30; } else if (m == 2 && reap(y)) { d = 29; } else { d = 28;
                }

            } while (m != 0);

            if (ki >= 20) {
                w += ki / 20;
                ki %= 20;
            }
            if (w >= 18) {
                t += w / 18;
                w %= 18;
            }
            if (t >= 20) {
                ka += t / 20;
                t %= 20;
            }
            if (ka >= 20) {
                b += ka / 20;
                ka %= 20;
            }
            if (b >= 13) {
                b %= 13;
            }

            m = 12;

            y--;

        }*/

        if (d > 21) {
          ki += d - 21;
          d = 21;
        } else {
          m--;
          if (m == 0) {
            ki += d + 31 - 21;
            m = 12;
            y--;
          } else if (m == 3 || m == 5 || m == 7 || m == 8 || m == 10 ||
                     m == 1) {
            ki += d + 31 - 21;
          } else if (m == 4 || m == 6 || m == 9 || m == 11) {
            ki += d + 30 - 21;
          } else if (m == 2 && reap(y)) {
            ki += d + 29 - 21;
          } else {
            ki += d + 28 - 21;
          }
          ki++;
        }

        while (y != 2013) {

          while (m != 0) {
            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 ||
                m == 12) {
              ki += 31;
            } else if (m == 4 || m == 6 || m == 9 || m == 11) {
              ki += 30;
            } else if (m == 2 && reap(y)) {
              ki += 29;
            } else {
              ki += 28;
            }
            m--;
          }

          if (ki >= 20) {
            w += ki / 20;
            ki %= 20;
          }
          if (w >= 18) {
            t += w / 18;
            w %= 18;
          }
          if (t >= 20) {
            ka += t / 20;
            t %= 20;
          }
          if (ka >= 20) {
            b += ka / 20;
            ka %= 20;
          }
          if (b >= 13) {
            b %= 13;
          }

          m = 12;

          y--;
        }

        while (m != 0) {
          if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 ||
              m == 12) {
            ki += 31;
          } else if (m == 4 || m == 6 || m == 9 || m == 11) {
            ki += 30;
          } else if (m == 2 && reap(y)) {
            ki += 29;
          } else {
            ki += 28;
          }
          m--;
        }

        if (ki >= 20) {
          w += ki / 20;
          ki %= 20;
        }
        if (w >= 18) {
          t += w / 18;
          w %= 18;
        }
        if (t >= 20) {
          ka += t / 20;
          t %= 20;
        }
        if (ka >= 20) {
          b += ka / 20;
          ka %= 20;
        }
        if (b >= 13) {
          b %= 13;
        }

        // ki += 31 - 21;

        if (ki >= 20) {
          w += ki / 20;
          ki %= 20;
        }
        if (w >= 18) {
          t += w / 18;
          w %= 18;
        }
        if (t >= 20) {
          ka += t / 20;
          t %= 20;
        }
        if (ka >= 20) {
          b += ka / 20;
          ka %= 20;
        }
        if (b >= 13) {
          b %= 13;
        }

        /*if(ki<0){
            if(w==0){
                if(t==0){
                    if(ka==0){
                        b--;
                        ka = 19;
                        t = 19;
                        w = 17;
                        ki += 19;
                    }
                    else{
                        ka--;
                        t = 19;
                        w = 17;
                        ki += 19;
                    }
                }
                else{
                    t--;
                    w = 17;
                    ki += 19;
                }
            }
            else{
                w--;
                ki += 19;
            }
        }*/
      }

      printf("%d.%d.%d.%d.%d\\n", b, ka, t, w, ki);

    } else {

      long long int day = 0;

      long long int b, ka, t, w, ki;
      b = ka = t = w = ki = 0;

      long long int y, m, d;

      stringstream ss;

      ss << vs[0];
      ss >> b;
      ss.clear();
      ss << vs[1];
      ss >> ka;
      ss.clear();
      ss << vs[2];
      ss >> t;
      ss.clear();
      ss << vs[3];
      ss >> w;
      ss.clear();
      ss << vs[4];
      ss >> ki;
      ss.clear();

      day =
          ki + w * 20 + t * 20 * 18 + ka * 20 * 18 * 20 + b * 20 * 18 * 20 * 20;

      y = 2012;
      m = 12;
      d = 21;
      /*
                  if(day > 10){
                      y++; m = 1; d = 1; day -= 11;
                  }
                  else{
                      d += day;
                      day = 0;
                  }

                  while(day!=0){

                      if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m ==
         10){ if(day >= 31){ m++; day -= 31;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 4 || m == 6 || m == 9 || m == 11){
                          if(day >= 30){
                              m++;
                              day -= 30;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 2 && reap(y)){
                          if(day >= 29){
                              m++;
                              day -= 29;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 2){
                          if(day >= 28){
                              m++;
                              day -= 28;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }
                      else if(m == 12) {
                          if(day >= 31){
                              m = 1;
                              y++;
                              day -= 31;
                          }
                          else{
                              d += day;
                              day = 0;
                          }
                      }

                  }
      */

      while (day != 0) {

        if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10) {
          while (day != 0 && d != 32) {
            day--;
            d++;
          }
          if (d == 32) {
            m++;
            d = 1;
          }
          /*if(day >= 31){
              m++;
              day -= 31;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 4 || m == 6 || m == 9 || m == 11) {
          while (day != 0 && d != 31) {
            day--;
            d++;
          }
          if (d == 31) {
            m++;
            d = 1;
          }
          /*if(day >= 30){
              m++;
              day -= 30;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 2 && reap(y)) {
          while (day != 0 && d != 30) {
            day--;
            d++;
          }
          if (d == 30) {
            m++;
            d = 1;
          }
          /*if(day >= 29){
              m++;
              day -= 29;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 2) {
          while (day != 0 && d != 29) {
            day--;
            d++;
          }
          if (d == 29) {
            m++;
            d = 1;
          }
          /*if(day >= 28){
              m++;
              day -= 28;
          }
          else{
              d += day;
              day = 0;
          }*/
        } else if (m == 12) {
          while (day != 0 && d != 32) {
            day--;
            d++;
          }
          if (d == 32) {
            m = 1;
            d = 1;
            y++;
          }
          /*if(day >= 31){
              m = 1;
              y++;
              day -= 31;
          }
          else{
              d += day;
              day = 0;
          }*/
        }
      }

      printf("%lld.%lld.%lld\\n", y, m, d);
    }
  }

  return 0;
}