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

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

struct M {
  long long b;
  long long ka;
  long long t;
  long long w;
  long long ki;
};
M mkM(long long a, long long b, long long c, long long d, long long e) {
  M m;
  m.b = a;
  m.ka = b;
  m.t = c;
  m.w = d;
  m.ki = e;
  return m;
}

struct C {
  long long y;
  long long m;
  long long d;
};
C mkC(long long a, long long b, long long c) {
  C cc;
  cc.y = a;
  cc.m = b;
  cc.d = c;
  return cc;
}

long long MtoN(M m) {
  long long ret = 0;
  ret += m.b;
  ret *= 20;
  ret += m.ka;
  ret *= 20;
  ret += m.t;
  ret *= 18;
  ret += m.w;
  ret *= 20;
  ret += m.ki;
  return ret;
}

M NtoM(long long n) {
  M m;
  m.ki = n;
  m.w = m.ki / 20;
  m.ki %= 20;
  m.t = m.w / 18;
  m.w %= 18;
  m.ka = m.t / 20;
  m.t %= 20;
  m.b = m.ka / 20;
  m.ka %= 20;
  m.b %= 13;
  return m;
}

long long D[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

long long CtoN(C c) {
  if (c.y == 2012)
    return c.d - 21;

  long long ret = 0;
  while (c.y >= 2013 + 400) {
    c.y -= 400;
    ret += 365 * 400 + 97;
  }
  // ret+=(365*400+97)*((c.y-2013)%400);
  // c.y-=400*((c.y-2013)%400);
  ret += c.d - 1;
  c.d = 1;
  while (c.m > 1) {
    c.m--;
    ret += D[c.m];
    if (c.m == 2 && c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0))
      ret++;
  }
  while (c.y > 2013) {
    c.y--;
    ret += 365;
    if (c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0))
      ret++;
  }

  return ret + 11;
}

C NtoC(long long n) {
  C c;
  c.y = 2012;
  c.m = 12;
  c.d = 21;
  if (n < 11) {
    c.d += n;
    return c;
  }
  c.y = 2013;
  c.m = 1;
  c.d = 1;
  n -= 11;

  while (n >= 365 * 400 + 97) {
    n -= 365 * 400 + 97;
    c.y += 400;
  }
  while (n >= 365) {
    if (c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0)) {
      if (n >= 366) {
        c.y++;
        n -= 366;
      } else
        break;
    } else {
      c.y++;
      n -= 365;
    }
  }
  if (c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0)) {
    D[2]++;
    while (n >= D[c.m]) {
      n -= D[c.m];
      c.m++;
    }
    c.d += n;
    D[2]--;
  } else {
    while (n >= D[c.m]) {
      n -= D[c.m];
      c.m++;
    }
    c.d += n;
  }

  return c;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;

    long long t = 0;
    long long a[6] = {0};
    for (long long i = 0; i < s.size(); i++) {
      if (s[i] == '.')
        t++;
      else {
        a[t] *= 10;
        a[t] += s[i] - 48;
      }
    }

    M m;
    C c;
    if (t == 4) {
      m.b = a[0];
      m.ka = a[1];
      m.t = a[2];
      m.w = a[3];
      m.ki = a[4];
      long long n = MtoN(m);
      c = NtoC(n);

      printf("%d.%d.%d\\n", c.y, c.m, c.d);
    } else {
      c.y = a[0];
      c.m = a[1];
      c.d = a[2];
      long long n = CtoN(c);
      m = NtoM(n);

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

struct M {
  long long b;
  long long ka;
  long long t;
  long long w;
  long long ki;
};
M mkM(long long a, long long b, long long c, long long d, long long e) {
  M m;
  m.b = a;
  m.ka = b;
  m.t = c;
  m.w = d;
  m.ki = e;
  return m;
}

struct C {
  long long y;
  long long m;
  long long d;
};
C mkC(long long a, long long b, long long c) {
  C cc;
  cc.y = a;
  cc.m = b;
  cc.d = c;
  return cc;
}

long long MtoN(M m) {
  long long ret = 0;
  ret += m.b;
  ret *= 20;
  ret += m.ka;
  ret *= 20;
  ret += m.t;
  ret *= 18;
  ret += m.w;
  ret *= 20;
  ret += m.ki;
  return ret;
}

M NtoM(long long n) {
  M m;
  m.ki = n;
  m.w = m.ki / 20;
  m.ki %= 20;
  m.t = m.w / 18;
  m.w %= 18;
  m.ka = m.t / 20;
  m.t %= 20;
  m.b = m.ka / 20;
  m.ka %= 20;
  m.b %= 13;
  return m;
}

long long D[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

long long CtoN(C c) {
  if (c.y == 2012)
    return c.d - 21;

  long long ret = 0;
  while (c.y >= 2013 + 400) {
    c.y -= 400;
    ret += 365 * 400 + 97;
  }
  ret += c.d - 1;
  c.d = 1;
  while (c.m > 1) {
    c.m--;
    ret += D[c.m];
    if (c.m == 2 && c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0))
      ret++;
  }
  while (c.y > 2013) {
    c.y--;
    ret += 365;
    if (c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0))
      ret++;
  }

  return ret + 11;
}

C NtoC(long long n) {
  C c;
  c.y = 2012;
  c.m = 12;
  c.d = 21;
  if (n < 11) {
    c.d += n;
    return c;
  }
  c.y = 2013;
  c.m = 1;
  c.d = 1;
  n -= 11;

  while (n >= 365 * 400 + 97) {
    n -= 365 * 400 + 97;
    c.y += 400;
  }
  while (n >= 365) {
    if (c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0)) {
      if (n >= 366) {
        c.y++;
        n -= 366;
      }
    } else {
      c.y++;
      n -= 365;
    }
  }
  if (c.y % 4 == 0 && (c.y % 100 != 0 || c.y % 400 == 0)) {
    D[2]++;
    while (n >= D[c.m]) {
      n -= D[c.m];
      c.m++;
    }
    c.d += n;
    D[2]--;
  } else {
    while (n >= D[c.m]) {
      n -= D[c.m];
      c.m++;
    }
    c.d += n;
  }

  return c;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;

    long long t = 0;
    long long a[6] = {0};
    for (long long i = 0; i < s.size(); i++) {
      if (s[i] == '.')
        t++;
      else {
        a[t] *= 10;
        a[t] += s[i] - 48;
      }
    }

    M m;
    C c;
    if (t == 4) {
      m.b = a[0];
      m.ka = a[1];
      m.t = a[2];
      m.w = a[3];
      m.ki = a[4];
      long long n = MtoN(m);
      c = NtoC(n);

      printf("%d.%d.%d\\n", c.y, c.m, c.d);
    } else {
      c.y = a[0];
      c.m = a[1];
      c.d = a[2];
      long long n = CtoN(c);
      m = NtoM(n);

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