Problem p00256 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)
#define repd(i, a) for (int i = (a); i >= 0; --i)

const int units[4] = {20, 18, 20, 20}, cycle = 13 * 20 * 20 * 20 * 18;

typedef long long ll;

bool isLeap(int y) { return (y % 4 == 0 && y % 100) || y % 400 == 0; }

int dayOfMonth(int y, int m) {
  switch (m) {
  case 2:
    if (isLeap(y))
      return 29;
    else
      return 28;
  case 4:
  case 6:
  case 9:
  case 11:
    return 30;
  default:
    return 31;
  }
}

// y2/m2/d2 - y1/m1/d1
ll countD(int y1, int m1, int d1, int y2, int m2, int d2) {
  ll ret = 0;
  while (true) {
    if (y1 == y2 && m1 == m2) {
      ret += d2 - d1;
      break;
    } else if (y1 == y2) {
      ret += dayOfMonth(y1, m1) - d1 + 1;
      d1 = 1;
      ++m1;
      if (m1 > 12)
        m1 = 1, ++y1;
    } else {
      ret += 28 + 30 * 4 + 31 * 7 + isLeap(y1) - countD(y1, 1, 1, y1, m1, d1);
      ++y1;
      m1 = 1;
      d1 = 1;
    }
  }

  return ret;
}

void advD(int &y, int &m, int &d, ll days) {
  while (true) {
    int lim = dayOfMonth(y, m) - d + 1;
    if (days < lim) {
      d += days;
      break;
    }
    days -= lim;
    d = 1;
    ++m;
    if (m > 12)
      m = 1, ++y;
  }

  return;
}

int main() {
  std::string s;
  while (std::cin >> s, s != "#") {
    // y.m.d
    if (std::count(s.begin(), s.end(), '.') == 2) {
      int y, m, d;
      sscanf(s.c_str(), "%d.%d.%d", &y, &m, &d);
      ll days = countD(2012, 12, 21, y, m, d);

      days %= cycle;

      std::vector<ll> v;
      rep(i, 4) {
        v.push_back(days % units[i]);
        days /= units[i];
      }
      v.push_back(days);
      repd(i, 4) printf("%lld%c", v[i], ".\n"[!i]);
    }
    // b.ka.t.w.ki
    else {
      int a[5];
      sscanf(s.c_str(), "%d.%d.%d.%d.%d", a, a + 1, a + 2, a + 3, a + 4);
      int days = 0;
      repd(i, 5) {
        days += a[4 - i];
        if (i)
          days *= units[i - 1];
      }
      int y = 2012, m = 12, d = 21;
      advD(y, m, d, days);
      printf("%d.%d.%d\n", y, m, d);
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)
#define repd(i, a) for (int i = (a); i >= 0; --i)

const int units[4] = {20, 18, 20, 20}, cycle = 13 * 20 * 20 * 20 * 18;

typedef long long ll;

bool isLeap(int y) { return (y % 4 == 0 && y % 100) || y % 400 == 0; }

int dayOfMonth(int y, int m) {
  switch (m) {
  case 2:
    if (isLeap(y))
      return 29;
    else
      return 28;
  case 4:
  case 6:
  case 9:
  case 11:
    return 30;
  default:
    return 31;
  }
}

// y2/m2/d2 - y1/m1/d1
ll countD(int y1, int m1, int d1, int y2, int m2, int d2) {
  ll ret = 0;
  while (true) {
    if (y1 == y2 && m1 == m2) {
      ret += d2 - d1;
      break;
    } else if (y1 == y2) {
      ret += dayOfMonth(y1, m1) - d1 + 1;
      d1 = 1;
      ++m1;
      if (m1 > 12)
        m1 = 1, ++y1;
    } else if (y2 - y1 > 1000) {
      ret += (28 + 30 * 4 + 31 * 7) * 1000;
      rep(i, 1000) ret += isLeap(y1 + i + 1);
      ret -= countD(y1, 1, 1, y1, m1, d1);
      y1 += 1000;
      m1 = 1;
      d1 = 1;
    } else {
      ret += 28 + 30 * 4 + 31 * 7 + isLeap(y1) - countD(y1, 1, 1, y1, m1, d1);
      ++y1;
      m1 = 1;
      d1 = 1;
    }
  }

  return ret;
}

void advD(int &y, int &m, int &d, ll days) {
  while (true) {
    int lim = dayOfMonth(y, m) - d + 1;
    if (days < lim) {
      d += days;
      break;
    }
    days -= lim;
    d = 1;
    ++m;
    if (m > 12)
      m = 1, ++y;
  }

  return;
}

int main() {
  std::string s;
  while (std::cin >> s, s != "#") {
    // y.m.d
    if (std::count(s.begin(), s.end(), '.') == 2) {
      int y, m, d;
      sscanf(s.c_str(), "%d.%d.%d", &y, &m, &d);
      ll days = countD(2012, 12, 21, y, m, d);

      days %= cycle;

      std::vector<ll> v;
      rep(i, 4) {
        v.push_back(days % units[i]);
        days /= units[i];
      }
      v.push_back(days);
      repd(i, 4) printf("%lld%c", v[i], ".\n"[!i]);
    }
    // b.ka.t.w.ki
    else {
      int a[5];
      sscanf(s.c_str(), "%d.%d.%d.%d.%d", a, a + 1, a + 2, a + 3, a + 4);
      int days = 0;
      repd(i, 5) {
        days += a[4 - i];
        if (i)
          days *= units[i - 1];
      }
      int y = 2012, m = 12, d = 21;
      advD(y, m, d, days);
      printf("%d.%d.%d\n", y, m, d);
    }
  }

  return 0;
}