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

#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;

//  0   1  2   3    4  5   6    7  8   9  10 11 12
ll days[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; // leap year

ll Leap = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;
ll nLeap = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;

bool isLeap(ll year) {
  return ((year % 4LL) == 0LL && (year % 100LL) != 0LL ||
          (year % 400LL) == 0LL);
}

void compute(vector<int> vec) {
  if (vec.size() == 3) {
    ll y = vec[0], m = vec[1], d = vec[2];
    ll counter = 0;
    if (y == 2012) {
      assert(m == 12);
      counter = d - 21;
      assert(counter >= 0);
    } else {
      counter = d;
      REP(month, 1, m) counter += days[isLeap(y)][month];

      REP(year, 2013, y) {
        if (isLeap(year))
          counter += Leap;
        else
          counter += nLeap;
      }
      counter += 10LL;
    }
    ll limit = 13LL * (20LL * 20LL * 18LL * 20LL);
    while (counter > limit)
      counter -= limit;
    ll b, ka, t, w, ki, temp;
    b = ka = t = w = ki = 0;
    ki = counter % 20LL;
    temp = counter - ki;
    assert(temp % 20LL == 0);
    int total_winaru = temp / 20LL;
    w = total_winaru % 18LL;
    temp -= w * 20LL;
    assert(temp % (18LL * 20LL) == 0);
    int total_toun = temp / (18LL * 20LL);
    t = total_toun % 20LL;
    temp -= t * 20LL * 18LL;
    assert(temp % (20LL * 18LL * 20LL) == 0);
    int total_katoun = temp / (20LL * 18LL * 20LL);
    ka = total_katoun % 20LL;
    temp -= ka * 20LL * 18LL * 20LL;
    assert(temp % (20LL * 18LL * 20LL * 20LL) == 0);
    int total_bakutoun = temp / (20LL * 18LL * 20LL * 20LL);
    b = total_bakutoun;
    temp -= b * 20LL * 18LL * 20LL * 20LL;
    assert(temp == 0);
    cout << b << "." << ka << "." << t << "." << w << "." << ki << endl;
  } else {
    ll counter = 0;
    counter += ll(vec[0]) * 20LL * 20LL * 18LL * 20LL;
    counter += ll(vec[1]) * 20LL * 18LL * 20LL;
    counter += ll(vec[2]) * 18LL * 20LL;
    counter += ll(vec[3]) * 20LL;
    counter += ll(vec[4]);
    ll Y = 2012, M = 12, O = 21LL + counter;

    while (!(1LL <= O && O <= days[isLeap(Y)][M])) {
      O -= days[isLeap(Y)][M];
      ++M;
      if (M > 12LL) {
        M = 1;
        ++Y;
      }
    }
    cout << Y << "." << M << "." << O << endl;
  }
}

int main() {
  string s;
  while (cin >> s, s[0] != '#') {
    rep(i, s.size()) if (s[i] == '.') s[i] = ' ';
    stringstream ss;
    ss << s;
    vector<int> vec;
    int temp;
    while (!(ss >> temp).fail())
      vec.push_back(temp);
    compute(vec);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;

//  0   1  2   3    4  5   6    7  8   9  10 11 12
ll days[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; // leap year

bool isLeap(ll year) {
  return ((year % 4LL) == 0LL && (year % 100LL) != 0LL ||
          (year % 400LL) == 0LL);
}

void compute(vector<int> vec) {
  if (vec.size() == 3) {
    ll y = vec[0], m = vec[1], d = vec[2];
    ll counter = 0;
    if (y == 2012) {
      assert(m == 12);
      counter = d - 21;
      assert(counter >= 0);
    } else {
      counter = d;
      REP(month, 1, m) counter += days[isLeap(y)][month];
      REP(year, 2013, y)
      REP(month, 1, 12 + 1) counter += days[isLeap(year)][month];
      counter += 10LL;
    }
    ll limit = 13LL * (20LL * 20LL * 18LL * 20LL);
    while (counter > limit)
      counter -= limit;
    ll b, ka, t, w, ki, temp;
    b = ka = t = w = ki = 0;
    ki = counter % 20LL;
    temp = counter - ki;
    assert(temp % 20LL == 0);
    int total_winaru = temp / 20LL;
    w = total_winaru % 18LL;
    temp -= w * 20LL;
    assert(temp % (18LL * 20LL) == 0);
    int total_toun = temp / (18LL * 20LL);
    t = total_toun % 20LL;
    temp -= t * 20LL * 18LL;
    assert(temp % (20LL * 18LL * 20LL) == 0);
    int total_katoun = temp / (20LL * 18LL * 20LL);
    ka = total_katoun % 20LL;
    temp -= ka * 20LL * 18LL * 20LL;
    assert(temp % (20LL * 18LL * 20LL * 20LL) == 0);
    int total_bakutoun = temp / (20LL * 18LL * 20LL * 20LL);
    b = total_bakutoun;
    temp -= b * 20LL * 18LL * 20LL * 20LL;
    assert(temp == 0);
    cout << b << "." << ka << "." << t << "." << w << "." << ki << endl;
  } else {
    ll counter = 0;
    counter += ll(vec[0]) * 20LL * 20LL * 18LL * 20LL;
    counter += ll(vec[1]) * 20LL * 18LL * 20LL;
    counter += ll(vec[2]) * 18LL * 20LL;
    counter += ll(vec[3]) * 20LL;
    counter += ll(vec[4]);
    ll Y = 2012, M = 12, O = 21LL + counter;

    while (!(1LL <= O && O <= days[isLeap(Y)][M])) {
      O -= days[isLeap(Y)][M];
      ++M;
      if (M > 12LL) {
        M = 1;
        ++Y;
      }
    }
    cout << Y << "." << M << "." << O << endl;
  }
}

int main() {
  string s;
  while (cin >> s, s[0] != '#') {
    rep(i, s.size()) if (s[i] == '.') s[i] = ' ';
    stringstream ss;
    ss << s;
    vector<int> vec;
    int temp;
    while (!(ss >> temp).fail())
      vec.push_back(temp);
    compute(vec);
  }
  return 0;
}