题解 P7075 【儒略日(洛谷民间数据)】

· · 题解

中规中矩的模拟题,考察了对时间的理解。

给的大样例也不是很强,所以 OI 赛制下这题可能会丢很多分,甚至爆 0。

有趣的是,比赛那天 2020 年 11 月 7 日正好距离 1582 年 10 月 15 日 16 万天整,由此断定出题人就是罗马教皇格列高利十三世转世。

有两种思路:

  1. Day by Day 模拟然后找规律计算。
  2. 二分年份再剩余日期模拟。

写起来会有一些坑,比如:

时间复杂度 O(Q\log Y),其中 Y 为年份范围,具体实现如下:

int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
LL cal(LL x) {
    int sb = -4712;
    if (x < sb) return 0;
    LL res = (x - sb + 1) * 365;
    if (x < 1582) {
        res += (x - sb) / 4 + 1;
    } else {
        res -= 10;
        res += (1581 - sb) / 4 + 1;
        res += (x - 1580) / 4 - (x - 1500) / 100 + (x - 1200) / 400;
    }
    return res;
}
bool pd(int x) {
    if (x > 1582) {
        return x % 4 == 0 && x % 100 != 0 || x % 400 == 0;
    } else {
        return x % 4 == 0;
    }
}
int main() {
    int _;
    scanf("%d", &_);
    while (_--) {
        LL n;
        scanf("%lld", &n);
        n++;
        int l = -4712, r = 1e9 + 5;
        while (l < r) {
            int mid = l + r >> 1;
            if (cal(mid) >= n) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        n -= cal(l - 1);
        a[2] = 28 + pd(l);
        a[10] = l == 1582 ? 21 : 31;
        int m;
        for (int i = 1; i <= 12; i++) {
            if (n > a[i]) {
                n -= a[i];
            } else {
                m = i;
                break;
            }
        }
        if (l == 1582 && m == 10) {
            if (n >= 5) n += 10;
            printf("%lld %d %d\n", n, m, l);
        } else {
            if (l > 0)
                printf("%lld %d %d\n", n, m, l);
            else
                printf("%lld %d %d BC\n", n, m, -l + 1);
        }
    }
    return 0;
}