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

#include <iostream>
#include <string>
using namespace std;
int main() {
  long long y, m, d, b, ka, t, w, ki;
  int i, j, k, p;
  long long day, base = 0, ba = 1872000;
  int mo[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  string str;
  cin >> str;
  while (str[0] != '#') {
    y = 0;
    m = 0;
    d = 0;
    i = 0;
    day = 0;
    p = 0;
    while (str[i] != '\\0') {
      if (str[i] == '.')
        p++;
      i++;
    }
    if (p == 2) {
      i = 0;
      while (str[i] != '.')
        i++;
      for (j = 0; j < i; j++) {
        y *= 10;
        y += str[j] - '0';
      }
      i++;
      k = i;
      while (str[i] != '.')
        i++;
      for (j = k; j < i; j++) {
        m *= 10;
        m += str[j] - '0';
      }
      i++;
      k = i;
      while (str[i] != '\\0')
        i++;
      for (j = k; j < i; j++) {
        d *= 10;
        d += str[j] - '0';
      }
      // cout << y << " " << m << " " << d << endl;
      if (y == 2012) {
        day = d - 21;
      } else {
        day = 10;
        for (i = 2013; i < y; i++)
          if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
            day += 366;
          else
            day += 365;
        for (i = 1; i < m; i++)
          day += mo[i];
        if (2 < m && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
          day++;
        day += d;
      }
      while (ba < day)
        day -= ba;
      b = day % (13 * 20 * 20 * 18 * 20) / (20 * 20 * 18 * 20);
      ka = day % (20 * 20 * 18 * 20) / (20 * 18 * 20);
      t = day % (20 * 18 * 20) / (18 * 20);
      w = day % (18 * 20) / 20;
      ki = day % (20);
      cout << b << "." << ka << "." << t << "." << w << "." << ki << endl;
    } else {
      i = 0;
      b = 0;
      ka = 0;
      t = 0;
      w = 0;
      ki = 0;
      while (str[i] != '.')
        i++;
      for (j = 0; j < i; j++) {
        b *= 10;
        b += str[j] - '0';
      }
      i++;
      k = i;
      while (str[i] != '.')
        i++;
      for (j = k; j < i; j++) {
        ka *= 10;
        ka += str[j] - '0';
      }
      i++;
      k = i;
      while (str[i] != '.')
        i++;
      for (j = k; j < i; j++) {
        t *= 10;
        t += str[j] - '0';
      }
      i++;
      k = i;
      while (str[i] != '.')
        i++;
      for (j = k; j < i; j++) {
        w *= 10;
        w += str[j] - '0';
      }
      i++;
      k = i;
      while (str[i] != '\\0')
        i++;
      for (j = k; j < i; j++) {
        ki *= 10;
        ki += str[j] - '0';
      }
      // cout << b << "." << ka << "." << t << "." << w << "." << ki << endl;
      day =
          b * 20 * 20 * 18 * 20 + ka * 20 * 18 * 20 + t * 18 * 20 + w * 20 + ki;

      y = 2012;
      m = 12;
      d = 21;
      if (day <= 10)
        d += day;
      else {
        day -= 10;
        y = 2013;
        int f1 = 0;
        while (day > 365 && f1 == 0) {
          if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
            if (day == 366)
              f1 = 1;
            else
              day -= 366;
          else
            day -= 365;
          if (f1 == 0)
            y++;
        }
        m = 1;
        int flag = 0;
        // cout << day << endl;
        while (day > mo[m] && flag == 0) {
          if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) {
            if (day > 29) {
              day -= 29;
              m++;

            } else {
              flag = 1;
            }
          } else {
            day -= mo[m];
            m++;
          }
        }
        // cout << day << endl;
        d = day;
      }
      cout << y << "." << m << "." << d << endl;
    }
    cin >> str;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long lli;

const static lli n_day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool isleap(lli y) {
  if (y % 4 != 0)
    return false;
  return (y % 100 != 0 || y % 400 == 0);
}

struct Maya {
  lli b, ka, t, w, ki;
};

struct Date {
  lli y, m, d;
  bool operator!=(const Date &date) const {
    return y != date.y || m != date.m || d != date.d;
  }

  bool isleap() {
    if (y % 4 != 0)
      return false;
    return (y % 100 != 0 || y % 400 == 0);
  }
  lli numOfDays() {
    if (m == 2)
      return n_day[2] + isleap();
    else
      return n_day[m];
  }
  void next() {
    if (d == numOfDays()) {
      d = 1;
      if (m == 12) {
        m = 1;
        y += 1;
      } else {
        m += 1;
      }
    } else {
      d += 1;
    }
  }
};

const int YSIZE = 3000000;
// sumDay[i] : sum of days between 2012/12/21 and 2012+i*4/12/20
lli sumDay[YSIZE];
lli rSumDay[YSIZE];

void init() {
  sumDay[0] = 0;
  rSumDay[0] = 2012;
  for (int i = 1; i < YSIZE; ++i) {
    sumDay[i] = sumDay[i - 1] + 365 * 4 + isleap(2012 + i * 4);
    rSumDay[i] = 2012 + i * 4;
  }
}

lli mayaToDay(const Maya &maya) {
  return maya.b * 20 * 20 * 18 * 20 + maya.ka * 20 * 18 * 20 +
         maya.t * 18 * 20 + maya.w * 20 + maya.ki;
}

Maya dayToMaya(lli day) {
  Maya maya;
  day %= 20 * 20 * 18 * 20 * 13;

  maya.ki = day % 20;
  day /= 20;
  maya.w = day % 18;
  day /= 18;
  maya.t = day % 20;
  day /= 20;
  maya.ka = day % 20;
  day /= 20;
  maya.b = day;
  return maya;
}

Date dayToDate(lli day) {
  int p = lower_bound(sumDay, sumDay + YSIZE, day) - sumDay;
  if (sumDay[p] != day)
    --p;
  int remainDay = day - sumDay[p];
  Date date = {2012 + p * 4, 12, 21};
  while (remainDay > 0) {
    --remainDay;
    date.next();
  }
  return date;
}

lli dateToDay(const Date &date) {
  int p = lower_bound(rSumDay, rSumDay + YSIZE, date.y) - rSumDay;
  if (rSumDay[p] != date.y)
    --p;
  lli res = sumDay[p];
  Date n = {2012 + p * 4, 12, 21};
  while (date != n) {
    res += 1;
    n.next();
  }
  return res;
}

int main() {
  init();
  string line;
  while (getline(cin, line) && line != "#") {
    replace(line.begin(), line.end(), '.', ' ');
    stringstream ss(line);
    vector<lli> v;
    string str;
    while (ss >> str) {
      v.push_back(atoll(str.c_str()));
    }
    if (v.size() == 3) {
      Date date = {v[0], v[1], v[2]};
      Maya maya = dayToMaya(dateToDay(date));
      cout << maya.b << "." << maya.ka << "." << maya.t << "." << maya.w << "."
           << maya.ki << endl;
    } else {
      Maya maya = {v[0], v[1], v[2], v[3], v[4]};
      Date date = dayToDate(mayaToDay(maya));
      cout << date.y << "." << date.m << "." << date.d << endl;
    }
  }
  return 0;
}