Why TLE?
查看原帖
Why TLE?
87696
Lily_White楼主2020/11/7 22:36
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeap(LL x) {
  if (x > 1582) {  // use gregorian calendar
    if (x % 4 == 0 && x % 100 != 0) return 1;
    if (x % 400 == 0) return 1;
    return 0;
  } else if (0 < x && x <= 1582) {
    if (x % 4 == 0) return 1;
    return 0;
  } else {
    x = -x;
    if (x % 4 == 1) return 1;
    return 0;
  }
}
LL n;
void print(LL y, int m, int d, bool bc) {
  if (bc) y = -y;
  cout << d << ' ' << m << ' ' << y;
  if (bc) {
    cout << " BC" << endl;
  } else {
    cout << endl;
  }
}
const LL magic = 2299161LL; // day count from the beginning to 1582/10/4
// the code used to calculate this is appended in the end
int getDay(LL y, int m) {
  if (m != 2) return days[m];
  else {
    if (isLeap(y)) return 29;
    else return 28;
  }
}
void bf(LL y = -4713, int m = 1) {
  int cnt = 0;
  while (true) {
    if (n - cnt < getDay(y, m)) {
      print(y, m, n - cnt + 1, (y < 0));
      return;
    } else {
      cnt += days[m];
      if (m == 2 && isLeap(y)) {
        cnt++;
      }
      m++;
      if (m == 13) {
        y++;
        if (y == 0) y = 1;
        m = 1;
      }
    }
  }
}
void solve() {
  if (n < magic) {
    bf();
    return;
  } else if (n == magic) {
    print(1582, 10, 15, 0);
  } else {
    n -= magic;
    if (n <= 77) {  // we are still in year 1582
      if (n <= 16) {
        print(1582, 10, n + 15, false);
      } else if (n > 16 && n <= 16 + 30) {
        n -= 16;
        print(1582, 11, n, false);
      } else if (n > 16 + 30) {
        n -= (16 + 30);
        print(1582, 12, n, false);
      }
      return;
    } else if (n <= 6287) {
      n -= 78;
      bf(1583, 1);
    } else {
      n -= 6287;
      LL y = n / 146097, r = n % 146097;
      n = r;
      bf(y * 400 + 1600, 1);
    }
  }
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    cin >> n;
    solve();
  }
//  cerr << clock() / CLOCKS_PER_SEC << endl;
  return 0;
}


2020/11/7 22:36
加载中...