問題
A club meets on the first day of every month, and those meetings that land on a Sunday get a brunch. Count the months from January of start_year to December of end_year whose first day is a Sunday, using the Gregorian calendar. As a fixed point: 1 January 2023 was a Sunday. Remember the leap year rule: divisible by 4, except century years, which must be divisible by 400.
Implement counting_sundays(start_year, end_year).
Input
start_year: The first year, counted from January.
end_year: The last year, counted through December.
Output
Return the number of months in that range whose first day is a Sunday.
制約
1600 ≤ start_year ≤ end_year ≤ 2400
例
入力:
start_year = 2023
end_year = 2023
期待される出力:
expected = 2
入力:
long long start_year = 2023;
long long end_year = 2023;
期待される出力:
long long expected = 2;
入力:
long start_year = 2023;
long end_year = 2023;
期待される出力:
long expected = 2;
解説
In 2023 only January and October began on a Sunday, so the answer is 2.
入力:
start_year = 2024
end_year = 2025
期待される出力:
expected = 3
入力:
long long start_year = 2024;
long long end_year = 2025;
期待される出力:
long long expected = 3;
入力:
long start_year = 2024;
long end_year = 2025;
期待される出力:
long expected = 3;
解説
2024 had September and December, 2025 had June: 3 months.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
Find the weekday of January 1 of the start year from a known date, or use a weekday formula.
ヒント 2
Moving to the next month shifts the weekday by that month's length mod 7.
ヒント 3
February has 29 days in leap years: divisible by 4, but century years only when divisible by 400.
目安の計算量
O(years) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。