CSP-S2020 Question 1 (Julian Day) Solution

Topic
title Portal
solving ideas
in the title of this calendar, the date is calculated in the 400-year cycle, there are exactly 146,097 days every 400 years.
We can preprocess the situation within 400 years, and then modulate the year to 400 to quickly get the answer.
Acceleration skills
For the "Gregorian calendar", take January 1, 1200 as the starting date, and r minus 2159351 days
r⩽2299160 is the Julian calendar. X year BC is regarded as (1-x) year.
Reference Code

#include <bits/stdc++.h>
using namespace std;
const int N = 146097, A = 2299160, B = 2159351, C = 1461, D = 4712;
int y[N], m[N], d[N];
inline int get(int y, int m) {
    
    
    if (m == 2) {
    
    
		return (y % 4) ? 28 : (y % 100) ? 29 : y % 400 ? 28 : 29;
	}
    return (m == 4 || m == 6 || m == 9 || m == 11) ? 30 : 31;
}
void init() {
    
    
	m[0] = d[0] = 1;
    for(int i = 1; i < N; ++i) {
    
    
        d[i] = d[i - 1] + 1; m[i] = m[i - 1]; y[i] = y[i - 1];
        if (d[i] > get(y[i], m[i])) {
    
    
			++m[i]; d[i] = 1;
		}
        if (m[i] > 12) {
    
    
			++y[i]; m[i] = 1;
		} 
    }
}
int main() {
    
    
	init();
    int T; cin >> T;
    while (T--) {
    
    
        long long n, t; cin >> n;
        if (n > A) {
    
    
            n -= B; t = n / N * 400 + 1200; n %= N;
        } else {
    
    
            t = n / C * 4 - D; n %= C;
        }
        if (t + y[n] > 0) {
    
    
        	cout << d[n] << ' ' << m[n] << ' ' << t + y[n] << '\n';
        } else {
    
    
        	cout << d[n] << ' ' << m[n] << ' ' << 1 - t - y[n] << " BC\n";
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/yueyuedog/article/details/112620225