"Ritsumeikan University Competitive Programming Camp" will be held this year as well. I am very much looking forward to this annual training camp. However, I couldn't stand my desires and splurged before the training camp, so I couldn't afford it. So I decided to use the cheapest Seishun 18 Ticket to get to Minami Kusatsu, the nearest station to Ritsumeikan University. This ticket is cheap, but there are many transfers, and I have to spend the whole day to move, which makes me very tired. It was after I left Aizu-Wakamatsu Station that I realized that the day of such a move was Friday the 13th. I was swayed by the train for 12 hours, feeling anxious.
Today is the second day of the training camp, Sunday, March 15, 2015. I arrived in Minami-Kusatsu on the 13th without any problems, but I didn't want to feel this kind of anxiety anymore. So, as a judge on the second day, I asked him to ask for the number of Friday the 13th that existed within the specified period, and asked him to create a program.
The definition of the year of the stagnation is as follows.
A year in which the year is divisible by 4 is a leap year.
However, a year divisible by 100 is not a leap year.
However, a year divisible by 400 is a leap year.
Constraints
The
input satisfies the following constraints.
1 ≤ Y1 ≤ Y2 ≤ 1018
1 ≤ Mi ≤ 12
1 ≤ Di ≤ 31 (Mi = 1, 3, 5, 7, 8, 10, 12)
1 ≤ Di ≤ 30 (Mi = 4, 6, 9, 11)
1 ≤ Di ≤ 28 (Mi = 2 and Yi year is not a leap year)
1 ≤ Di ≤ 29 (Mi = 2 and Yi year is a leap year)
Y1 year M January D1 is a date more than 0 days before Y2 year M February D2.
Input
Six integers Y1, M1, D1, Y2, M2, D2 separated by blanks are given in one line.
Output
Output the number of Friday the 13th that exists between M1 January D1 of Y1 and D2 M2 of Y2 on one line.
Examples
Input
2015 3 13 2015 3 13
Output
1
Input
2015 2 14 2015 3 15
Output
1
Input
1234 5 6 789012345678901234 5 6
Output
1357101234567708000
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are asked to count the number of Friday the 13ths between two dates inclusive (I assume inclusive? The problem says "between M1 January D1 of Y1 and D2 M2 of Y2". Usually "between" might include endpoints. The examples: first example: 2015 3 13 to 2015 3 13 yields 1. That indicates that if the start date is a Friday 13th, it's counted. So inclusive both ends. So we need to count number of dates that are 13th of some month and also Friday, within the given inclusive interval.
Constraints: Y up to 1e18. So we cannot iterate over years. Need a formula. But the Gregorian calendar repeats every 400 years? Actually the Gregorian calendar cycle is 400 years: the number of days in 400 years is 400365 + number of leap years = 400365 + 97 = 146097 days. This is a multiple of 7? 146097 mod 7 =? 720871 = 146097? 208717=146097? Let's calculate: 200007=140000, 8717=6097, total 146097. Yes exactly divisible. So the weekday pattern repeats every 400 years. Also the distribution of Friday 13ths repeats every 400 years? The cycle of weekdays relative to dates is 400 years. So number of Friday 13th in any 400-year period is constant? Yes because the calendar repeats exactly. So we can count them in a 400-year cycle.
But the range spans up to 1e18, which is huge. So we need to compute the count over huge intervals efficiently. Since the pattern repeats every 400 years, we can break the interval into three parts: the prefix from Y1 to first year in a 400-year block, full cycles, and suffix.
However, note that the interval is defined by specific dates, not whole years. So we need to handle exact dates. But since the cycle is 400 years, we can consider the starting date and ending date within their respective 400-year cycles.
But careful: The Gregorian leap year rules are as given. The 400-year cycle applies to the pattern of weekdays relative to dates, because the total days mod 7 repeats. However, note that the cycle might not be exactly 400 years for the date offset? Actually it is exactly 146097 days per 400 years, which is exactly 20871 weeks. So the day of week for any given date (month, day) repeats after 400 years. For example, March 13, 2015 was a Friday? Actually 2015-03-13 is a Friday. After 400 years, 2415-03-13 will also be a Friday. So indeed the pattern repeats.
Thus we can compute:
For a given year Y, we can compute the number of Friday 13th from a reference date (e.g., 0001-01-01? But year 0? But the problem uses years AD probably, starting from 1. However, the Gregorian calendar as defined (leap year rules) is applied to all years? Historically, Gregorian calendar starts at 1582, but here it's extended arbitrarily. So we just treat the calendar proleptic? The problem defines the leap year rules only as above, no mention of 1582 cut-off. So it's a pure mathematical calendar: All years AD (year >=1) follow the given leap year rules. So we can use the 400-year cycle starting from year 1. But careful: The cycle length is 400 years, but we need to know the starting point of the cycle relative to year 1. Does the pattern from year 1 to year 400 repeat exactly for year 401 to 800? Yes if we apply the leap year rules continuously. However, there is a subtlety: Year 0 doesn't exist, but the pattern of leap years is determined by year number. For the cycle to be exactly 400 years, we need that for any integer k, the calendar for year Y and year Y+400 are identical. This holds because leap year rule depends on Y modulo 400? Actually: If Y is divisible by 4, it's leap, except if divisible by 100, but if divisible by 400, it's leap. That is exactly the condition: Y mod 400 == 0 => leap; Y mod 100 == 0 but not 400 => not leap; Y mod 4 == 0 and not 100 => leap. So leap(Y) = (Y%400==0) or (Y%4==0 and Y%100!=0). This is periodic with period 400. So yes, the set of leap years repeats every 400 years. Therefore the number of days in a year (365 or 366) also repeats. Thus the offset of weekdays advances by (sum of days mod 7) each year, but over 400 years it's constant because 146097 mod 7 = 0. So indeed the entire calendar (date to weekday) repeats every 400 years. However, we must also consider that the weekday of Jan 1 of year 1 is something. But it's not needed if we use known patterns within the cycle.
We need to compute the number of Friday 13ths between two inclusive dates, possibly huge years. Approach:
Precompute for each month of a 400-year cycle, the number of times that the 13th falls on a Friday. More efficiently, we can precompute for each year in a 400-year cycle a list of months where 13th is Friday (or count). Then total count in a full 400-year cycle = sum over years of such counts.
But careful: The cycle repeats exactly: The sequence of years in the cycle from some base year. Typically we define year 1 as the start. But we need to know which day of week Jan 13, year 1 is. But we can compute using known formulas: There is Zeller's congruence or other methods to compute day of week for any date given the Gregorian calendar (proleptic). However, we can also compute it programmatically by building the calendar for a 400-year cycle, but because we are to implement a solution, we need to compute counts in O(1) essentially, not by iterating over 400 years? But 400 is small, we can precompute via code (or even offline by hand). For the solution, we can precompute within the program by simulating 400 years? But years up to 1e18, but precomputing 400 years is trivial. However, we need to be careful about overflow when iterating 400 years? No.
But the program must handle up to 1e18, but we can precompute the 400-year cycle using 64-bit arithmetic? For precomputation, we can simulate days using small numbers. That is fine.
Thus algorithm:
Determine the day of week for some reference date (like 0001-01-01). We can use Zeller's congruence to compute day of week for any date. We'll need to compute for each month 13th in each year Y, whether it's Friday.
But we might need to consider the specific start and end dates: They are arbitrary within the year (not necessarily Jan 1). So we cannot simply count full cycles; we need to count only those Friday 13th that fall on or after (Y1,M1,D1) and on or before (Y2,M2,D2). This requires handling partial years.
Given the huge range, we can use the cycle for full 400-year blocks, but handle the prefix and suffix by iterating over at most 400 years? Actually, the prefix from Y1 to the first year that is a multiple of 400? But since the range may be huge, the prefix (the years from Y1 up to the end of its "block") can be up to 399 years. Similarly suffix up to 399 years. That is acceptable because 400 is small. However, the years are up to 1e18, but we can still iterate over a few hundred years without issue, as long as we don't iterate over huge ranges. But we need to compute the day of week for each month in those years. That is doable.
But careful: Y can be as large as 1e18. Simulating the calendar for 400 years where the year numbers are large (e.g., near 1e18) we need to handle the leap year rule based on Y modulo 400. That's easy: we can compute Y mod 400 to classify the year. But we also need to know the weekday of the 13th of a month. That depends on the offset from a reference. Since the pattern repeats every 400 years, the distribution of Friday 13ths within a 400-year block is independent of the actual year number; it only depends on the year's position within the 400-year cycle (i.e., year mod 400). And the counts for a given year (the months where 13th is Friday) also depend only on that modulo, because the calendar repeats exactly every 400 years. So for the prefix and suffix, we can treat the year as belonging to a particular residue class mod 400.
Thus, we can precompute for each possible year type (0..399) the months (1..12) in which the 13th is a Friday. But careful: There's no year 0 in AD; but we can map year mod 400 to a specific pattern. However, we need to define a base. Since the pattern repeats, we can pick a reference start: For any year Y, we can compute its "canonical year" as (Y-1) mod 400? Or better, because the cycle of 400 years is aligned such that the calendar for year Y is same as for year Y' if Y ≡ Y' (mod 400) provided we also treat the year numbering consistently. Since leap year condition uses Y mod 400, it's exactly that. So we can define that the pattern for year Y is identical to the pattern for year (Y mod 400) if we define that year 0 corresponds to some year? But year 0 doesn't exist. But we can shift: Let r = Y % 400. Since Y>=1, r is between 1 and 0? Actually, Y mod 400 yields 0..399. For Y=400, mod 400=0. But year 400 is a valid year and it should correspond to the same pattern as year 0 if there were such. So we can treat a residue class: 0 represents year 400, 800, etc; 1 represents year 1,401, etc; ... 399 represents year 399, 799, etc. But careful: Year 1 mod 400 = 1, year 401 mod 400 = 1. So the pattern for year 1 and 401 are same. So yes, we can map residue r (0..399) where r=0 stands for years that are multiples of 400 (like 400, 800,...), and r=1..399 stand for other years. For year 1, r=1. So we need to precompute for each r from 1 to 400? Actually r from 1 to 400? But mod 400 gives 0..399 inclusive. So we need to handle r=0 case. So we can precompute for a representative set of 400 years, say years 1 through 400. Then for any year Y, we can map to an index: if Y % 400 == 0, then it corresponds to year 400; else it corresponds to year (Y % 400). So we can create an array f[0..399] where f[0] corresponds to year 400 (since mod 0), and for i=1..399 corresponds to year i. But simpler: We'll just compute for an entire cycle of 400 consecutive years, e.g., from year 1 to 400 inclusive. Then for any year Y, we find its equivalent year in that cycle: let r = (Y-1) % 400 + 1. Then year r in [1,400] has the same calendar. This mapping works: Y=1 -> r=1; Y=400 -> r=400; Y=401 -> (401-1)%400+1 = 400%400? Actually (400)%400+1=0+1=1? Wait: (401-1)=400; 400%400=0; +1 =1. That would map 401 to 1, but 401 should be same as 1? Actually 401 mod 400 = 1, but year 401 should be same as year 1? Because leap year: year 401 is not divisible by 400, divisible by 4? 401%4=1, so not leap; year 1%4=1, not leap; so yes same. But also the weekday offset? The pattern repeats every 400 years, so year 401 should be same as year 1. So mapping Y to (Y-1)%400+1 yields correct equivalence? Let's test: For Y=400, (400-1)=399%400=399+1=400 => year 400. Good. For Y=800, (800-1)=799%400=399? Actually 799 mod 400 = 399? Because 400*1=400, remainder 399; +1 =400 => year 400? But year 800 should be same as year 400? Since 800 is multiple of 400, it should be same as year 400. Yes. So mapping works: (Y-1)%400+1 yields a number in 1..400 inclusive. So we can use that.
Thus we can precompute for each year in 1..400 a bitmask or list of months where 13th is Friday. Then we can sum over years.
Now, the challenge is to compute the total number of Friday 13th in an interval [start_date, end_date] inclusive.
Given the huge span, we can compute the number from year 1 up to any date, and then subtract.
Define function count_up_to(Y, M, D) that returns number of Friday 13th from some epoch (e.g., year 1 month 1 day 1) up to and including date (Y,M,D). Then answer = count_up_to(Y2,M2,D2) - count_up_to(Y1,M1,D1) + (if start date itself is a Friday 13th? Actually careful: count_up_to includes the date. So if we subtract count_up_to(prev day) we get inclusive count. So we can define count_up_to(date) as number of Friday 13th with date <= given date. Then answer = count_up_to(Y2,M2,D2) - count_up_to(Y1-? Actually we need to subtract count of dates strictly before the start date. So we can compute count_up_to(Y2,M2,D2) - count_up_to(Y1,M1,D1 - 1 day). But easier: define function that counts up to a date inclusive, and another function that counts up to date exclusive. We can compute count_up_to(Y2,M2,D2) - count_up_to(Y1,M1,D1) + (if (Y1,M1,D1) is Friday 13th then 1 else 0). But careful: If we subtract count_up_to(Y1,M1,D1) (which includes that date) we would exclude it. So we need to subtract count_up_to(date before start). So we can compute count_up_to(Y1,M1,D1-1). But handling day before may be messy with month boundaries. Simpler: compute count_up_to(Y2,M2,D2) - count_up_to(Y1,M1,D1) + extra, but we need to include start if it is a Friday 13th. So if we subtract count_up_to(Y1,M1,D1) we remove it. So we can add back if it's a Friday 13th. Alternatively compute count_up_to(Y2,M2,D2) - count_up_to(Y1,M1,D1-1). Let's implement a function prev_day(Y,M,D) to get the previous day's date. That might be messy but doable. However we can also compute count_up_to(Y1,M1,D1-1) by checking if D1>1 then just (Y1,M1,D1-1) else need previous month/year. Could handle with conditional.
Given the huge years, we want to avoid complexity. However, prefix/suffix approach might be simpler: break the interval into three parts: the partial year of start, full years between, and partial year of end. That may be easier to compute with cycle.
Alternate plan: Since we have 400-year cycle, we can compute the number of Friday 13th in any range [L,R] of days? But days also huge. But we can compute using modular arithmetic.
Let's think about using count_up_to approach.
We need to be able to compute count_up_to(Y,M,D) efficiently.
This count includes all Friday 13ths with year < Y, or year == Y and month < M, or year == Y, month == M, day <= D. Since the 13th is the day we care about, we only consider days where day==13. So for a given year and month, the date is a Friday 13th if the 13th falls on Friday. So for count_up_to, we need to count:
All Friday 13th in years < Y.
For year Y, months < M where 13th is Friday.
For month M if D >= 13, and if 13th is Friday, then include it.
Thus we need to compute:
Total(Y) = total number of Friday 13th from year 1 up to year Y-1 (inclusive maybe) plus partial year Y.
So we need to compute total number of Friday 13th in all years from 1 to Y-1 inclusive, and then add for months 1..M-1 in year Y, and possibly month M if D>=13.
Now, Y can be up to 1e18. So we need a formula for sum over years.
Because the pattern repeats every 400 years, we can compute per cycle.
Define:
Let C = total number of Friday 13th in a full 400-year cycle (e.g., years 1..400).
For a given year Y, we need to know which years from 1 to Y-1.
Let Y-1 = q400 + r, where r = (Y-1) % 400, and q = floor((Y-1)/400). Then total number from year 1 to year Y-1 = qC + sum_{i=1}^{r} f(i), where f(i) is number of Friday 13th in year i (i from 1 to 400). Note: careful: Our cycle years are 1..400. If we take Y-1 = q400 + r, with 0 <= r < 400, then the years we cover are: years 1..(q400 + r). That's q full cycles of 400 years? Actually if we start from year 1, the first cycle is years 1..400, second cycle years 401..800, etc. So after q full cycles, we have covered years 1..400q. Then remaining r years are years 400q+1 .. 400q+r. Those correspond to years in the cycle: 1..r? Because 400q+1 corresponds to year 1 in the cycle; 400q+2 to year 2; ... 400q+r to year r. So indeed sum_{i=1}^{r} f(i). So that works.
Thus total for years < Y = q*C + prefix_sum[r] where prefix_sum[r] = sum_{i=1}^{r} f(i). (with prefix_sum[0]=0).
Now we need to add for year Y: months 1..M-1 where 13th is Friday, plus possibly month M if D>=13 and 13th is Friday.
Thus we need fY = number of Friday 13th in year Y (full year) and also need list of months to check for partial year. But we can compute fY from the cycle as f(cycle_index(Y)), where cycle_index(Y) = ((Y-1)%400)+1. Also we can precompute for each year index (1..400) a bitmask of months where Friday 13th occurs. So we can compute partial year count by iterating over months 1..M-1 and checking if month is in that set. Since M <= 12, that's trivial.
So count_up_to(Y,M,D) can be computed efficiently using O(1) arithmetic plus O(12) loop for the partial year.
Now we need to compute C and prefix sums for years 1..400. But we need to compute f(i) for i=1..400. We can compute that by implementing a function that given a year number (1..400) returns the months where the 13th is Friday. We need to know the weekday of the 13th for each month of a given year. That can be derived from the weekday of January 13 of that year, and the month lengths. Alternatively, we can use known formulas: The weekday of a date can be computed via Zeller's congruence.
Zeller's congruence for Gregorian calendar:
For a given date (year, month, day):
if month <= 2, month += 12, year -= 1.
Then:
h = (day + floor(13*(month+1)/5) + year + floor(year/4) - floor(year/100) + floor(year/400)) mod 7
where h = 0 -> Saturday, 1 -> Sunday, 2 -> Monday, ..., 6 -> Friday? Wait common variants. Let's confirm:
One common variant: h = (q + floor((13*(m+1))/5) + K + floor(K/4) + floor(J/4) + 5J) mod 7, where J = century, K = year of century. But easier: use the formula:
h = (d + (13*(m+1))//5 + y + y//4 - y//100 + y//400) % 7
with the adjustments: month: 3=March, 4=April, ..., 14=February; year: if month is Jan/Feb, year = year-1.
Then result: 0=Saturday,1=Sunday,2=Monday,3=Tuesday,4=Wednesday,5=Thursday,6=Friday.
This is consistent.
So we can compute for a given year Y and month M (where we treat March as month 3, April as 4, ... December=12, January=13, February=14) the day-of-week of the 13th (day=13). Then check if it's Friday (h=6).
We can compute this for each year in 1..400 to precompute f.
Now, we must be careful with year 0? Since for January and February we reduce year by 1. So for year=1 and month=1 (January), we would treat as month=13, year=0. But year=0 is not a real year but mathematically it's fine if we treat Gregorian proleptic? The leap year for year 0? According to Gregorian rules, year 0 is divisible by 400? Actually year 0 does not exist historically, but in proleptic Gregorian calendar, year 0 is considered a leap year? Because the rule: divisible by 400 => leap; 0 mod 400 => 0 is divisible by 400, so year 0 would be leap if it existed. But since we adjust only for computation, the formula should work correctly if we treat year 0 as leap (since we use floor division). So it's fine.
Thus we can compute f(i) for i=1..400 using Zeller's congruence.
Now we need to ensure that we have correct mapping: For year Y, the actual calendar (including day-of-week) repeats every 400 years, and we precomputed for years 1..400. So for Y large, we map to (Y-1)%400+1, and use the precomputed f and month list.
Thus count_up_to(Y,M,D) works.
But we must also handle dates before the first Friday 13th? Since year 1 may have some. It's fine.
Simplify: Write a function count_upto_inclusive(Y,M,D) that counts all Friday 13th with date <= (Y,M,D). Then answer = count_upto_inclusive(Y2,M2,D2) - count_upto_inclusive(Y1,M1,D1) + (if date1 is Friday 13th then 1 else 0). Because count_upto_inclusive includes date1. So subtracting count_upto_inclusive(Y1,M1,D1) would remove it. So we add back if needed.
But careful: If we use count_upto_inclusive, we need to compute count_upto_inclusive for start date, but that count includes all dates <= start. So the difference count_upto_inclusive(end) - count_upto_inclusive(start) gives number of dates > start and <= end, i.e., excludes start. So we need to add 1 if start is a Friday 13th. That's fine.
Thus we can implement count_upto_inclusive as described.
Now, we need to compute count_upto_inclusive for huge years. Let's detail:
Function count_upto_inclusive(Y, M, D):
if Y < 1: return 0.
// compute number of full years before Y:
// years from 1 to Y-1 inclusive.
let prev_years = Y - 1
if prev_years == 0: q = 0, r = 0
else:
q = (prev_years) // 400
r = (prev_years) % 400
total = q * C
total += prefix_sum[r] // prefix_sum[r] = sum_{i=1}^{r} f(i)
// Now add months in year Y:
// For months 1..M-1 where the 13th is Friday and also ensure that the 13th date is within year Y and month, which it is.
// For month M: if D >= 13 and the 13th is Friday, then include it.
let idx = ((Y-1) % 400) + 1 // mapping to 1..400
// Use precomputed bitmask for year idx: an array months_friday13[idx] a list of months (1..12) sorted or a boolean per month.
for month = 1 to M-1:
if is_friday13_month[idx][month]: total++
if D >= 13 and is_friday13_month[idx][M]: total++
return total
But careful: For months before M, we always include if the 13th is Friday regardless of D? Yes, because the 13th of those months is before the date (since month < M, day 13 is definitely <= end of that month, and since D is at least 1, but we are counting dates <= (Y,M,D). Since month < M, the entire month's 13th (on day 13) occurs before date if D is at least something? Actually if month < M, then any date in that month is earlier than any date in month M (since year same). So yes, the 13th of earlier months is <= (Y,M,D). So we include all.
But there is a subtlety: For month M, we only include if D >= 13. That's correct.
Now, we need to compute C and prefix_sum for years 1..400.
Now, the mapping: prefix_sum[r] where r is number of years from the cycle after q cycles. But note: In our computation, we used prev_years = Y-1, and r = (Y-1) % 400. Then the remaining years are years: (q400+1) to (q400+r). These correspond to cycle years 1..r. So prefix_sum[r] = sum_{i=1}^{r} f(i). For r=0, sum=0.
Thus we need f(i) for i=1..400.
Now, we need to confirm that the cycle indeed repeats every 400 years starting from year 1. Let's test quickly: Is year 1's calendar identical to year 401? We need to check the weekday of Jan 1? But the pattern might shift if total days in 400 years is multiple of 7. Since we start at year 1, the offset from year 1 to year 401 is exactly 400 years. The total days in those 400 years (from year 1 to 400 inclusive) is 146097? Wait, careful: The days from year 1 to year 400 inclusive is 400 years. But when moving from year 1 to year 401, you've passed through years 1-400. That's 400 years, total days = 146097. 146097 mod 7 = 0. So Jan 1 of year 401 is the same weekday as Jan 1 of year 1. Also leap years repeat. So indeed the calendar for year 401 matches year 1. So the cycle works with mapping as above.
Thus the method is valid.
Now, we need to compute is_friday13_month for year indices 1..400. We'll compute using Zeller's congruence for each (year, month) with day=13. But careful: For January and February we treat month+12, year-1. In that case, for year=1, January: month=13, year=0. We need to compute correctly. Our Zeller implementation must handle year=0 correctly. The formula uses integer division. For year=0, year//4 = 0, year//100 = 0, year//400 = 0. That's fine. So we can compute.
Alternatively, we could compute the weekday of Jan 13 of each year and then propagate through months using month lengths. But Zeller is simpler.
Let's implement Zeller for Gregorian:
def day_of_week(y, m, d):
if m <= 2:
m += 12
y -= 1
h = (d + (13*(m+1))//5 + y + y//4 - y//100 + y//400) % 7
return h # 0=Saturday,1=Sunday,2=Monday,3=Tuesday,4=Wednesday,5=Thursday,6=Friday
Check: For known date: 2015-03-13 should be Friday => h=6. Let's test quickly: y=2015, m=3, d=13 => m>2, so unchanged. compute:
(13*(3+1)=134=52)//5 = 10 (since integer division: 52/5=10.4 floor 10)
y + y//4 - y//100 + y//400 = 2015 + 503 - 20 + 5 = 2503? Actually compute:
y//4 = 2015//4=503
y//100=20
y//400=5
So sum = 2015+503-20+5 = 2503
Add d=13 => 13+10+2503 = 2526
2526 mod 7 = 2526 - 7361 = 2526-2527= -1? 7*361=2527, so 2526 mod 7 = -1 mod 7 = 6. Yes 6 => Friday. Good.
Thus formula works.
Now we need to precompute f(i) for i=1..400 using this formula. Note that for i=1, we may have to handle year 0 for Jan/Feb. That's fine.
We'll generate an array f[401] (1-indexed) storing count of Friday 13th in that year. Also an array month_mask[401][13] maybe boolean.
Now, we need to compute prefix sums: prefix[0]=0; for i=1..400: prefix[i] = prefix[i-1] + f[i].
C = prefix[400].
Now, we need to be careful about the range: The problem says years can be as large as 1e18, but we are using 64-bit integers (unsigned long long) which can handle up to 2^63-1 (~9.22e18) so 1e18 fits. However, the final count may be huge. Example output: 1357101234567708000 which is about 1.357e18, which fits within 64-bit? 2^63-1 ~ 9.22e18, so yes. But we must use 128-bit? Actually 1.357e18 < 9.22e18, so 64-bit signed is sufficient. But we need to be careful with intermediate multiplication qC, where q can be up to ~ (1e18/400) ~ 2.5e15, times C (which is maybe a few hundred? Actually C is number of Friday 13th in 400-year cycle. Let's compute roughly: There are 12 months per year * 400 = 4800 months; on average, the 13th occurs on each weekday equally? Actually probability of 13th being Friday is 1/7? But due to calendar structure, it's slightly more for some? The known average is that any given month's 13th is equally likely to be any day of week over a 400-year cycle? Actually because 400-year cycle contains an integer number of weeks, each day of week occurs the same number of times? For days of month, the distribution may not be uniform. But number of Friday 13th in 400-year cycle is known: 688? Wait, I recall that in a 400-year Gregorian cycle, there are 688 Friday the 13ths. Actually I think it's 688. Let's verify: There are 4800 months; probability of Friday is about 1/7, so expected 4800/7 ≈ 685.7. Known number: I think it's 688. Could be 688. Let's quickly compute later. But anyway, C is about 688. So qC ~ 2.5e15 * 688 ≈ 1.72e18, still within 64-bit signed. But we need to be careful about overflow when adding further terms. 64-bit signed max ~9.22e18, so it's okay. But we might need to use unsigned 64-bit to be safe. However, the example output is 1357101234567708000, which is about 1.357e18, so fits. So we can use 64-bit (signed long long) but ensure we use unsigned for large values? In C++ we can use unsigned long long or __int128 if necessary. But given constraints, 64-bit is enough. Let's confirm max possible count: Range from year 1 to year 1e18, each year has at most 3 Friday 13th? Actually at most 3? Could be up to 3? In some years there can be 1,2, or 3 Friday 13th. Actually maximum per year is 3 (if it's a leap year? Actually it's possible to have 3, but can also have 1? There's also possibility of 0? I think each year has at least 1? Actually every year must have at least one Friday 13th? I think each year has at least one, maybe some years have two, some have three. So average is about 1.72? Actually over 400-year cycle, 688/400 = 1.72. So total up to year 1e18 is about 1.72e18, which is within 2^63-1 ~9.22e18. So safe.
Thus 64-bit signed is enough. But to avoid overflow during multiplication, we might cast to 128-bit or ensure using unsigned 64-bit. In C++ we can use unsigned long long.
Now, we need to compute Zeller for years up to 1e18? Only for precomputing, but for mapping we only need year index 1..400. So fine.
Now, implement:
Precomputation: For y = 1 to 400:
For m = 1 to 12:
compute day_of_week(y, m, 13) using Zeller.
if result == 6 => friday13.
Store f[y] = count.
We also need month_mask[y][m] bool.
We must ensure that Zeller works for years like 0? For y=1 and m=1, we convert to m=13, y=0. That's fine.
Now, we need to be careful about potential bug: Some sources say Zeller's formula may give different result for years before 1582? But we are using proleptic Gregorian. So it's fine.
Now, count_upto_inclusive:
Define:
ull count_upto_inclusive(ull Y, int M, int D) {
if (Y == 0) return 0; // year 0 not exist, but handle if Y=0? Y>=1 per constraints.
// Full years before Y:
ull prev_years = Y - 1;
ull q = prev_years / 400;
ull r = prev_years % 400;
ull total = q * C;
total += prefix[r]; // prefix[0] = 0, prefix[i] sum first i years (i from 0 to 400).
// Year Y partial:
ull idx = ((Y-1) % 400) + 1;
for (int m = 1; m < M; ++m) {
if (month_mask[idx][m]) total++;
}
if (D >= 13 && month_mask[idx][M]) total++;
return total;
}
Now, we need to be careful: The prefix sum array should be of size 401, where prefix[i] = sum of f[1..i] for i from 0 to 400. So prefix[0]=0, prefix[1]=f[1], ... prefix[400]=C. So for r years, we need prefix[r] where r is count of years (0..399). Since r can be 0 to 399? Actually prev_years % 400 yields a number between 0 and 399 inclusive. That r is the number of years after q full cycles. Those years correspond to the first r years of the cycle (years 1..r). So we need prefix[r]. Good.
Now, we must also handle case when Y=1? Then prev_years=0 => q=0, r=0 => total starts at 0. Then we add partial of year 1. Good.
Now, we need to ensure that month_mask for year idx is correct for the month of February in leap years? Zeller will account for leap year because it uses y//4 etc. So if year is leap, the day of week for Feb 13 is computed correctly.
Let's compute count_upto_inclusive(2015,3,13). We'll test manually to ensure it counts that date.
We need to know f for year 2015. Let's compute using our mapping: idx = ((2015-1)%400)+1 = (2014%400)+1. 2014 mod 400 = 2014-5*400=2014-2000=14 => idx=15? Actually 14+1=15. So year index 15. We'll precompute f[15] and month_mask[15].
We need to know for year 2015, which months have Friday 13th? I know that in 2015, Friday 13th occurred in February? Actually recall: 2015 had Friday the 13th in February, March, and November? Let's check: I recall 2015: Feb 13 was Friday? 2015-02-13 was Friday? Let's compute: 2015-02-13: Zeller: m=2 => adjust: m=14, y=2014. Compute? But we can look up: I think 2015 had Friday 13th in February, March, and November. Indeed many calendars: 2015: Feb 13 was Friday, March 13 was Friday, and November 13 was Friday. So 3 occurrences. So f[15] should be 3, with months 2,3,11.
Our partial: M=3, D=13. So we add months 1..2: months 1? January 13 2015? Jan 13 2015 was Tuesday? So not counted. February 13 2015 was Friday => count 1. Then month 3 with D>=13 => March 13 is Friday => count 1. So total from partial =2. Plus full years before 2015.
We need to compute total count up to 2014-12-31. That would include all Friday 13th from year 1 to 2014. Hard to compute manually, but the difference should cancel except the start date.
Anyway the example says output 1, so our method would compute 1.
Now, Example 2: Input 2015 2 14 2015 3 15 => Should output 1. Let's check: The only Friday 13th in that interval is March 13? February 13 is on the 13th, but the start date is Feb 14, so Feb 13 is not included. March 13 is included (since March 15 includes it). So exactly one. So our count: count_upto_inclusive(2015,3,15) includes March 13. count_upto_inclusive(2015,2,14) includes all Friday 13th <= Feb 14. That would include Feb 13? Since Feb 13 < Feb 14, and day 13 is <= 14, so count_upto_inclusive(2015,2,14) includes Feb 13 if it's Friday. It is Friday, so count includes it. Then difference count_upto(end)-count_upto(start) = count of Friday13 > start and <= end = March 13 only, because Feb 13 is <= start and included in start count. Then we need to add if start is Friday13? Start is 2015-02-14, not 13th, so no addition. So difference = 1. Good.
Now, Example 3: Huge numbers. We'll trust.
Now, confirm that our count_upto_inclusive for start date includes Feb 13 when D=14? Yes, because month=2, D=14 >=13, and month_mask[2] true, so it includes. That's correct.
Now, we need to ensure that for months where D <13, we don't include that month's 13th. In start date case, if D=12, then count_upto includes months before February but not February, as it should.
Now, potential pitfalls: The problem's constraints guarantee that the input dates are valid per the calendar rules (given month-day constraints). Also Y1 <= Y2, and Y1 M1 D1 is before or equal to Y2 M2 D2? Actually they say "Y1 year M January D1 is a date more than 0 days before Y2 year M February D2." I think it means Y1 M1 D1 is strictly before Y2 M2 D2? "more than 0 days before" means earlier. So start < end. But they could be equal? The first example has equal dates, and output 1, so equal allowed? Actually equal and output 1 indicates that they consider that date as within "between", so inclusive. So we can handle inclusive.
Now, what about year 0? Not used.
Now, need to be careful with integer types. In C++, we can use unsigned long long for Y and counts. But for operations like q * C, where q and C are both unsigned long long, product may exceed 2^64? C is at most 12*400 = 4800, but more precisely C=688, q up to 2.5e15, product up to 1.7e18, which is < 2^64 (~1.84e19). So safe.
But we should compute prefix sums also as unsigned long long.
Now, let's compute C and prefix sums programmatically. We'll write a function to compute f for a given year y (using Zeller) and fill arrays.
Let's verify Zeller for all years 1..400 to ensure consistency. We'll do in code.
Potential issues: Zeller's formula uses year value after adjustment (y-1 for Jan/Feb). For year=1 and month=1, we set m=13, y=0. Then we compute h = (13 + (13*(13+1)//5) + 0 + 0//4 - 0//100 + 0//400) mod 7. (13*(14)=182//5=36; sum=13+36+0=49; 49%7=0 -> Saturday). So Jan 13, year 1 is Saturday. So not Friday.
Check known facts: Jan 13, 1 AD? Not known. But it's fine.
Now, we also need to confirm that the cycle indeed starts at year 1. But does the pattern repeat from year 1 to 401? As argued, total days from Jan 1 year 1 to Jan 1 year 401 is sum of days in years 1..400. That sum is 146097, divisible by 7, so weekdays align. However, we also need to ensure that the day-of-week of Jan 1 year 1 is what? It doesn't matter; the fact that the total days is multiple of 7 ensures that Jan 1 of year 401 is same as Jan 1 year 1, given that we apply same leap year pattern. So indeed the whole calendar repeats exactly. So our mapping is correct.
But there is nuance: The Gregorian calendar's 400-year cycle is usually considered from year 1 to 400, but sometimes they consider year 0? It doesn't matter. Since our precomputation for years 1..400 includes leap years correctly, we can treat year 401 as mapping to year 1. However, we used mapping idx = ((Y-1)%400)+1. For Y=401, idx = ((401-1)%400)+1 = (400%400)+1 = 0+1 =1. Good.
Now, for Y that is a multiple of 400, say Y=400, idx = ((400-1)%400)+1 = (399%400)+1 = 399+1=400. So year 400 maps to index 400. Good.
Thus the precomputation covers years 1..400.
Now, what about year 0? Not used.
Now, we should also consider whether the range includes dates earlier than year 1? No, Y1 >=1.
Now, the method seems straightforward.
Now, we should test with small ranges to ensure correctness.
Let's try to compute total count from year 1 to year 400 inclusive: should be C. We'll compute later.
Now, we need to be careful about the partial year handling: For year Y, if M=1 and D<13, then we should not include any month because months before M (none) and month M not included. So that's correct.
Now, also note: The date might be, for example, Y1 M1 D1 = 2015 2 13. That's exactly a Friday 13th. Then count_upto_inclusive(2015,2,13) includes it. So for answer: count_upto(end) - count_upto(start) + (is_friday13(start)? 1:0) would subtract it, then add it back, net include. So correct.
Now, confirm with an example: start=2015 2 13, end=2015 3 13. count_upto(end) includes Feb13 and Mar13. count_upto(start) includes Feb13. difference = Mar13. plus add if start is Friday13 => add Feb13 => total 2. Are both within range? From Feb13 to Mar13 inclusive includes both. So answer should be 2. That seems plausible. But not given.
Now, code steps:
Read Y1, M1, D1, Y2, M2, D2 as unsigned long long for years? Years up to 1e18 fit in unsigned long long. But M, D small, can be int.
Precompute arrays f[401], prefix[401], month_mask[401][13]. (Indices: year 1..400, month 1..12).
Implementation of Zeller:
int day_of_week_13(int y, int m) {
int d = 13;
if (m <= 2) {
m += 12;
y -= 1;
}
int h = (d + (13*(m+1))/5 + y + y/4 - y/100 + y/400) % 7;
// division is integer floor.
return h; // 0 Sat,1 Sun,...,6 Fri
}
But careful: y can be 0 after adjustment; that's fine.
But in our precomputation, y is small (1..400). So using int is fine.
Now, for each year Y from 1 to 400:
for month 1..12:
h = day_of_week_13(Y, month);
if (h == 6) then month_mask[Y][month] = true; f[Y]++;
After building, compute prefix[0]=0; for i=1..400: prefix[i] = prefix[i-1] + f[i]; C = prefix[400].
Now, implement count_upto_inclusive(Y, M, D). Y is unsigned long long, M,D int.
Take care when Y=0? Not needed.
Let's define:
typedef unsigned long long ull;
ull count_upto(ull Y, int M, int D) {
if (Y == 0) return 0;
ull total = 0;
ull prev = Y - 1;
ull q = prev / 400;
ull r = prev % 400;
total = q * C + prefix[r];
ull idx = ((Y-1) % 400) + 1; // 1..400
for (int m = 1; m < M; ++m) {
if (month_mask[idx][m]) total++;
}
if (D >= 13 && month_mask[idx][M]) total++;
return total;
}
Now, is_friday13(Y,M,D) can be implemented as: D == 13 && month_mask[((Y-1)%400)+1][M] is true? But also need to ensure that the date is valid (i.e., month M and day 13 exists). Since D==13, we just check month_mask. So is_start_friday13 = (D1 == 13 && month_mask[idx1][M1]).
But careful: For a given year, month_mask indicates if the 13th of that month is a Friday. That's exactly the condition.
Alternatively, we could compute count_upto(Y2,M2,D2) - count_upto(Y1,M1,D1-1) but that would require handling D1=1 -> previous month. So simpler to use addition method.
Now, check with example 1:
Y1=2015 M1=3 D1=13, Y2=2015 M2=3 D2=13.
We need count_upto for both.
Compute idx = ((2015-1)%400)+1 = (2014%400)+1 = 14+1? Wait 2014/400 =5 remainder 14, so idx=15. month_mask[15][3]? Should be true.
count_upto(2015,3,13) includes all before and March 13. count_upto(2015,3,13) same. So difference zero. is_friday13(Y1,M1,D1) true -> add 1 => answer 1. Good.
Example2: (2015,2,14) to (2015,3,15)
count_upto(2015,3,15) includes Feb13? Wait careful: For year 2015 partial: M=3, D=15. So months 1..2 are added if month_mask true. February (month 2) is true, so includes Feb13. Also month 3: D=15>=13 and month_mask true => includes Mar13. So count_upto(2015,3,15) includes both Feb13 and Mar13.
count_upto(2015,2,14): months 1..1 (Jan) maybe false? Jan13 not Friday; month 2: D=14>=13 and month_mask true => includes Feb13. So count_upto(start) includes Feb13.
Difference = Mar13 only. is_friday13(start) false because D1=14 not 13. So answer=1. Good.
Now test a scenario where start date is exactly Feb13: (2015,2,13) to (2015,3,13). count_upto(end) includes Feb13 and Mar13. count_upto(start) includes Feb13. Difference = Mar13. is_friday13(start)=true -> add 1 => total 2. That matches expectation.
Now, we should also test that count_upto works for dates where D < 13. For example, count_upto(2015,1,1) should not include Jan13 if Jan13 is not Friday? Actually Jan13 2015 was Tuesday? So not Friday. But suppose we want to count up to 2015,1,12. Then months 1..0 (none), month 1 not included because D<13. So count_upto returns total from previous years only. Good.
Now, we also need to consider that the date range may start before any Friday 13th? That's fine.
Now, we should double-check precomputation for years 1..400. But we must ensure that we treat leap years correctly. The leap year function: divisible by 4, except by 100, but including by 400. That's exactly what Zeller uses? Actually Zeller's formula uses y//4 - y//100 + y//400. That is exactly the Gregorian adjustment. So it's consistent.
Now, the only remaining challenge is to confirm that our precomputed C and prefix sums are correct. We can compute them in code, so they will be correct as per the Gregorian proleptic calendar. However, we need to be absolutely sure that the pattern repeats exactly every 400 years with our mapping. Let's double-check the total days in 400 years from year 1 to 400 inclusive: number of leap years in that period. Leap years: years divisible by 4: 4,8,...,400 -> 100 numbers. But those divisible by 100: 100,200,300,400 -> 4 numbers, except those divisible by 400: 400 is also divisible by 400, so it remains leap? Actually rule: divisible by 400 is leap. So among the century years, 400 is leap, the others are not. So total leap years = 100 - 4 + 1 = 97? Wait: Count of years divisible by 4: floor(400/4)=100. Among these, those divisible by 100: floor(400/100)=4, which are 100,200,300,400. Among those, we need to add back those divisible by 400: floor(400/400)=1 (400). So leap years = 100 - 4 + 1 = 97. Yes. So days = 400*365 + 97 = 146097. 146097 mod 7 = 146097 / 7 = 20871 exactly. So it's a multiple of 7. So the cycle is exactly 400 years.
Now, we might wonder about year 0. But we don't need it.
Now, we need to ensure that our precomputed f for years 1..400 is correct for any year Y that maps to index idx, especially for years that are multiples of 400 (like 400,800,...). For Y=400, idx=400. So we need f[400] correctly computed. For Y=800, idx = ((800-1)%400)+1 = (799%400)+1 = (399)+1 =400. So both 400 and 800 map to index 400. That is fine if year 400 and year 800 have same calendar. Year 800 is a leap year? 800 mod 400 = 0? Actually 800%400 =0, so 800 is divisible by 400, so it is a leap year. Year 400 is also divisible by 400, so leap year. Both should be same. So our mapping is correct.
Now, we need to test for a known case: For year 2000 (leap year), which is a multiple of 400, idx = ((2000-1)%400)+1 = (1999%400)+1 = (1999-4*400=1999-1600=399)+1=400. So year 2000 maps to index 400. So our precomputed f[400] should match year 2000's Friday 13ths. I know that in 2000, there were Friday 13ths in October? Actually I recall: October 13, 2000 was Friday? Let's check: 2000-10-13 was Friday? I think it was Friday. Also perhaps other months? Let's quickly compute known: In 2000, Friday the 13ths occurred in October only? Actually I think 2000 had only one Friday 13th: October. Let's verify: 2000 Jan 13 was Thursday? Feb 13: Sunday? March 13: Monday? etc. I think indeed only October. So f[400] should be 1 (or maybe more?). Let's compute: 2000 is a leap year, but that shouldn't affect many. I think known distribution: In a leap year starting on Sunday? Actually not needed. We'll trust our precomputation.
Now, before finalizing, we should test with small range by brute force for small Y to validate count_upto function.
We can implement a brute-force for Y up to, say, 1000 to check against our method. Since we cannot run here, but mentally we can test some known values.
For example, known number of Friday 13ths in 20th century (1901-2000) is 172? Actually I think it's 172? Let's check: The total in 400-year cycle is 688. So in 100 years it's about 172? Since 688/4=172 exactly? 400/4=100, but 688 is not necessarily divisible by 4? 688/4=172 exactly. So indeed 688 is divisible by 4? 4*172=688. So the number per century on average is 172. So 20th century (1901-2000) should have 172? But note that the 400-year cycle might have irregular distribution across centuries, but sum of 4 consecutive centuries in the cycle is 688. So each 100-year block (like 1-100, 101-200, etc) may have varying counts. But it's plausible that 1901-2000 (which corresponds to years 1901-2000) might have 172? Actually the 400-year cycle is not aligned to centuries starting at 1? But we can compute. However, we don't need to verify.
But to be safe, we should test with a known online calculator? Not possible offline. But we can reason: The Zeller formula is standard, and our precomputation for years 1..400 should be correct.
Now, one nuance: The input years can be huge, up to 1e18, but our precomputation for f and month_mask only needs to be done for years 1..400. However, the mapping for year Y uses the index computed as ((Y-1)%400)+1. But for year Y=0? Not possible.
Now, we need to consider that the precomputation of f and month_mask for years 1..400 uses the Gregorian leap year rule. That's fine.
Now, edge cases: If Y1 and Y2 are huge, but M1, D1 might be such that the start date is after the 13th of its month, etc. Fine.
Now, we must also ensure that for year Y, the month_mask we use for months that don't have 13? All months have a 13th, so okay.
Now, consider the possibility of overflow when adding partial months: total is ull, increments of at most 12, safe.
Now, we also need to verify that the prefix array for r (0..399) is defined correctly. We set prefix[0]=0. For r=1, prefix[1] = f[1]. For r=400? Actually r max is 399 because prev_years%400 < 400. So we need prefix up to 399. But we can compute prefix[400] for completeness.
Now, we need to be careful: Our prefix array is based on years 1..400. But when r=399, prefix[399] = sum_{i=1}^{399} f(i). Good.
Now, compute C = prefix[400] = sum_{i=1}^{400} f(i). That's the total for a full cycle.
Now, test for Y=1: count_upto(1, M, D). prev_years =0 => total =0. idx = ((1-1)%400)+1 = (0%400)+1 =0+1=1. Then add partial. That should count correctly.
Now, what about Y=0? Not allowed.
Now, we also need to handle the possibility that Y1 > Y2? Constraint says Y1 ≤ Y2. So fine.
Now, one more nuance: The date range includes the end date inclusive. Our count_upto includes end date if it's a Friday 13th. So the answer computed as difference plus start inclusion is correct.
Now, ensure that for start date that is a Friday 13th, we correctly identify is_friday13. That condition is simply D1==13 and month_mask for that month is true.
But what about if D1==13 but month_mask false? That would be a 13th that's not Friday, then it's not counted anyway. So condition is correct.
Now, we need to compute month_mask for idx1 (start year). idx1 = ((Y1-1)%400)+1.
Now, potential pitfalls: The year number Y can be huge, but the modulo operation is fine. However, we need to use 64-bit modulo.
Now, we should also consider the case when Y1 is huge and M1 maybe >? All fine.
Now, let's test with the huge example: Input: 1234 5 6 789012345678901234 5 6. Output: 1357101234567708000. We need to ensure our algorithm yields that. We'll trust.
Now, let's think about possible off-by-one errors: For count_upto, we considered years before Y as years 1..Y-1. But if Y=1, that's empty. Good.
Now, for the partial year, we add months 1..M-1. But what if M=1? Then no months before. Good.
Now, for month M, we add if D>=13 and month_mask true. But is it possible that the 13th of month M is after the date if D <13? Then we don't add. That's correct.
Now, one more nuance: Suppose the date is (Y, M, D) and D is exactly 13 but it's not Friday? Then we don't add, but count_upto should not include it because it's not a Friday 13th. That's correct.
Now, we must also ensure that count_upto for a date that is exactly a Friday 13th includes it exactly once. Our code: It includes months before M (which doesn't include month M), and then includes month M because D>=13 and mask true. So yes.
Now, we must check that for month M where D>=13, but the 13th of month M is a Friday, we include it. But if D is, say, 20, that still includes the 13th. Good.
Now, test a boundary: Suppose Y=2015, M=2, D=13. Then months before: month 1, not Friday. Then month 2: D>=13 and mask true -> include. So Feb13 counted. Good.
Now, test if M=2, D=12. Then month 2 not included. So Feb13 not counted even if it is Friday. That's correct because date is Feb12, which is before 13th.
Now, consider the start date exactly at 2015-02-13, count_upto(start) includes Feb13. So subtract in difference removes it, then we add back if is_friday13(start) true. So net include. Good.
Now, consider start date = 2015-02-12. count_upto(start) does NOT include Feb13. Then subtract in difference removes none, but we need to include Feb13 if it falls within [start,end]? Actually interval includes dates from 2015-02-12 to end. If end >= 2015-02-13, then Feb13 should be counted because it's after start. Since count_upto(start) excludes Feb13, and count_upto(end) includes it, difference includes it. And is_friday13(start) false, so no addition. So correct.
Now, consider start date = 2015-02-13, end = 2015-02-13. count_upto(end)-count_upto(start) = 0, plus is_friday13(start)=1 => answer 1. Good.
Now, we should be careful about the fact that our count_upto uses the precomputed month_mask for the year index. But what about year Y=0? Not used.
Now, we must ensure that our precomputation of f for years 1..400 yields correct results. Let's try to compute f for some known years to verify:
Year 2015 (idx 15). Should have Friday 13ths in Feb, Mar, Nov => 3.
Compute day_of_week_13 for idx 15? But idx 15 corresponds to year 15, not 2015. Wait careful: idx is computed as ((Y-1)%400)+1. For Y=2015, idx=15. So we need to check if year 15 (AD 15) has the same pattern as 2015. So we need to compute for year 15 (AD 15) and see if it has Friday 13ths in February, March, November? But we don't know historically. However, we trust the cycle: because 2015 = 4005 + 15? Actually 5400=2000, remainder 15. So year 2015 corresponds to year 15 in the cycle (since year 1 corresponds to 1, 401, 801,...). So we must verify that our precomputed f[15] yields 3 and months 2,3,11. Let's try to compute for year 15 using Zeller manually? We'll trust our code will compute correctly.
But we can manually compute for year 15 to confirm: For year 15, leap year? 15 mod 4 = 3, not leap. So common year. Using Zeller for month m:
We need to compute weekday of 13th for each month. Let's compute quickly the day of week of Jan 13, year 15, then add offsets.
But easier: The known fact that 2015 had Friday 13th in Feb, Mar, Nov implies that year 15 in the cycle should have the same. So if our precomputed f[15] gives that, then consistent.
We can test with Zeller for year 15, month 2: m=2 => adjust to m=14, y=14. Compute h = (13 + (13*15)//5 + 14 + 14//4 - 14//100 + 14//400) mod 7. Compute step by step:
(13*(14+1)=1315=195)//5 = 39.
y=14
y//4=3
y//100=0
y//400=0
Sum = 13 + 39 + 14 + 3 + 0 + 0? Actually formula: d + (13(m+1))/5 + y + y/4 - y/100 + y/400. So: 13 + 39 + 14 + 3 - 0 + 0 = 69.
69 mod 7 = 69-63=6 => Friday. So Feb 13 year 15 is Friday.
March: m=3, no adjust, y=15. Compute: (13*(3+1)=134=52)//5=10. y=15, y//4=3, y//100=0, y//400=0. Sum=13+10+15+3=41. 41 mod 7 = 6? 42 is 67, so 41 mod 7 = 6? Actually 42 mod7=0, so 41 mod7 = 6 (since 42-1). So Friday. Good.
April: m=4 => 13*(5)//5=135=65//5=13; y=15; sum=13+13+15+3=44; 44 mod7 = 2? 42 is 67, remainder 2 => Tuesday. Not Friday.
Continue to November: m=11 => 13*(12)//5=156//5=31; sum=13+31+15+3=62; 62 mod7= 62-56=6 => Friday. So indeed f[15]=3 months: Feb, Mar, Nov. Good.
Thus our precomputation will match.
Now, ensure that for year 400 (idx 400) we compute correctly. Let's test quickly: Year 400 is leap. Use Zeller.
But there is one nuance: When we compute for year 400, month adjustments for Jan/Feb: we subtract 1 from year, getting year=399. That's fine. So the precomputation is consistent.
Now, we must be careful about the Zeller formula: In some implementations, the result mapping might be different: 0=Saturday,1=Sunday,...6=Friday. That's what we used. Some use 0=Monday, etc. But we tested with 2015-03-13 giving 6, and we know it's Friday, so our mapping is correct.
Now, about integer division: In C++ for negative numbers, division truncates toward zero, but we never have negative y because when we adjust for Jan/Feb we subtract 1 from y, but y may become 0 (non-negative). So fine.
Now, implement in C++.
Potential issues: Y1 and Y2 up to 1e18, so we must read them as unsigned long long. Use scanf("%llu") or cin.
Now, for prefix and f arrays, we can use unsigned long long.
Now, check for Y=0? Not needed.
Now, edge case: If Y1 is very large and M1/D1 may be such that the start date is after the end date? Not possible as per constraints.
Now, let's think about the possibility of overflow in multiplication q * C. q can be up to floor((1e18 -1)/400) ≈ 2.5e15, C maybe 688. product ≈ 1.72e18 < 2^64 (1.84e19). So safe.
Now, we also need to handle the addition of prefix[r] and partial months. The total may approach ~1.8e18, still < 2^64, so safe.
Now, the output may be as large as ~1.8e18, which fits in 64-bit. So we can print with printf("%llu") or cout.
Now, we need to be careful: If we use unsigned long long, the multiplication may overflow if we accidentally use signed int? But C++ unsigned long long multiplication is modulo 2^64 if overflow, but we have to ensure it doesn't exceed. It doesn't. However, some compilers might not detect but result is defined modulo, but we need exact product. Since it's within range, it's fine.
Now, confirm with example 3: The output is 1357101234567708000. That's within 2^63? Actually it's about 1.357e18, which is less than 9e18, so okay.
Now, let's attempt to compute manually for example 3 to ensure our method yields that. We'll trust.
Now, possible pitfalls: The start and end dates may be far apart, but our count_upto for both uses the same precomputed cycle and should be accurate.
Now, consider the case when Y1=1 and M1=1 D1=1, Y2=400 M2=12 D2=31. Then count_upto(Y2) should be C, count_upto(Y1-? Actually count_upto(Y1,M1,D1) would count maybe 0 because no Friday 13th before? But there might be Friday 13th in year 1 if it exists. We need to compute exactly. But not needed.
Now, one more nuance: The date range might include the very first date 0001-01-01? But year 1 is the smallest.
Now, also consider that the mapping for year Y for the partial year uses month_mask based on idx. But what about the case when the month M is February and the year is a leap year? That is captured by month_mask because the Zeller takes leap year into account. So February 13 exists in both leap and common years? Yes, February always has a 13th day, even in non-leap (day 13 exists). So fine.
Now, we might ask: Could there be any scenario where a date is invalid? Input guarantees validity.
Now, we should also consider the possibility that the start date is after the 13th of its month, and the end date is before the 13th of its month, etc. Our method handles correctly.
Now, I think the solution is correct.
Now, let's write the code.
Implementation details:
Use typedef unsigned long long ULL.
Precompute arrays: f[401] (ULL), prefix[401] (ULL), month_ok[401][13] (bool, or int). Since 401*13 ~ 5213, it's fine.
Zeller function: static int day_of_week_13(int y, int m) // returns 0-6.
In main:
Read input.
Precompute if not already? Since small, we can compute each time in main.
Now, need to ensure that month_ok is accessible for idx based on Y1 and Y2. Since we precomputed for years 1..400, and idx ranges 1..400, it's okay.
Now, double-check: For Y=400, idx=400, month_ok[400] exists.
Now, note: In precomputation, we computed for year numbers 1..400. But when Y=0? Not used.
Now, let's test with a few additional cases manually.
Case: Y1=1 M1=1 D1=1, Y2=1 M2=1 D2=13. Suppose Jan 13 year 1 is Saturday (from earlier calculation). So not Friday. So answer should be 0. Our count_upto(Y2,1,13) will include? prev_years=0 => total=0. idx for Y=1 is 1. For M=1, D=13: months before: none. Month M: D>=13 and month_mask[1][1]? We computed earlier: Jan13 year1 is Saturday, so mask false. So count_upto(Y2)=0. count_upto(Y1,1,1): D=1<13, so no addition. So count1=0. is_start false. answer=0. Good.
If Jan13 year1 were Friday, then count_upto(Y2) would be 1, count_upto(Y1)=0, is_start false, answer=1. Good.
Now, test with Y1=1 M1=1 D1=14, Y2=1 M2=1 D2=14, and Jan13 is Friday. Then count_upto(Y2) includes Jan13? M=1 D=14 >=13 and mask true => includes. count_upto(Y1) same, difference 0, is_start false (D1=14 not 13) => answer 0. But interval includes only day 14, which does not include the 13th. So correct.
Now, test with Y1=1 M1=1 D1=13, Y2=1 M2=1 D2=13, and Jan13 Friday. count_upto(Y2)=1, count_upto(Y1)=1, diff=0, is_start true => answer 1. Good.
Now, test with Y1=1 M1=1 D1=12, Y2=1 M2=1 D2=13, Jan13 Friday. count_upto(Y2)=1, count_upto(Y1)=0, diff=1, is_start false => answer 1. Good.
Now, these tests confirm logic.
Now, we must be careful with large Y and the modulo operation. Since Y can be up to 1e18, (Y-1) may overflow if Y is 0? Not. Use ULL, subtraction is fine.
Now, note: For Y=1, Y-1=0, modulo and division work.
Now, we should confirm that our prefix array uses r as integer from 0 to 399. In code, we'll compute r = prev_years % 400, which yields ULL, but we need to cast to int for indexing. r will be between 0 and 399, fits in int.
Now, we need to ensure that prefix array is of type ULL and has length at least 401 (index up to 400). We'll compute prefix[0]=0. For i=1..400, prefix[i] = prefix[i-1] + f[i]. Then when we need prefix[r] where r=0..399, we use prefix[r]. For r=0, prefix[0] defined.
Now, we also need to consider if the start date is before any Friday 13th? That's fine.
Now, potential off-by-one: For count_upto, we included years 1..Y-1. But what about year 0? There's no year 0. So it's correct.
Now, what about year negative? Not.
Now, is there any issue with the fact that the Gregorian calendar's 400-year cycle might not align with year 1? In the proleptic Gregorian calendar, the cycle repeats every 400 years. However, some sources say that the Gregorian calendar repeats every 400 years only for dates after year 0? But since we start at year 1, we must confirm that the cycle holds from year 1 to year 401. We already reasoned it does because total days from Jan1 year1 to Jan1 year401 = days in years 1..400 = 146097, which is multiple of 7. That is correct.
But is there any subtlety about century leap year rule? The rule says year divisible by 100 is not leap unless divisible by 400. This holds for all years. For year 1..400, the leap years are as computed. For year 401..800, the pattern is the same because if Y = 400k + r, the leap condition depends on r mod 400 (since 400k is divisible by 400, adding 400k doesn't change divisibility by 4? Actually (400k + r) mod 4 = r mod 4, because 400k mod 4 =0. Similarly mod 100 = r mod 100 because 400k mod 100 =0? But 400k mod 100 = 0? 400k = 4100k, so yes divisible by 100. So leap(400*k+r) = leap(r) if r!=0? Also when r=0, it's year 400,800,... which are leap because divisible by 400. So indeed the pattern repeats.
Thus the mapping is valid.
Now, what about the fact that there is no year 0, and our cycle includes year 400 which is leap. The next cycle starts at year 401 which corresponds to year 1. So consistent.
Now, we need to check if the number of days from Jan 1 year 1 to Jan 1 year 401 is exactly 146097 days. That is the sum of days in years 1..400. We computed it's 146097. But is there any off-by-one due to the fact that year 1 might be counted incorrectly? Let's compute sum of days from year 1 to 400 inclusive. Actually if we want the difference between Jan 1 year 1 and Jan 1 year 401, it's the total days of years 1 through 400. Yes. So that's correct.
Now, we must also confirm that Zeller's formula for dates in years 1..400 yields correct weekday for proleptic Gregorian. It should.
Now, let's test with known anchor: 2000-01-01 was Saturday? Actually I recall 2000-01-01 was Saturday? Wait, I think 2000-01-01 was Saturday? Let's check: 2000-01-01 was Saturday? Actually I recall 2000-01-01 was Saturday. Yes, many calendars show Jan 1, 2000 was Saturday. Let's test with our Zeller: For 2000-01-01, compute: m=1 => adjust m=13, y=1999. d=1. Compute h = (1 + (13*(13+1))/5 + 1999 + 1999/4 - 1999/100 + 1999/400) mod 7. Compute (1314=182)/5=36; 1999/4=499; 1999/100=19; 1999/400=4. Sum = 1+36+1999+499-19+4 = 1+36=37; 37+1999=2036; +499=2535; -19=2516; +4=2520. 2520 mod 7 = 0 because 7360=2520. So h=0 => Saturday. So correct.
Now, 2000-01-13? 2000-01-13: d=13, same month, so we can compute h = (13 +36 +1999+499-19+4) = 13+36=49; 49+1999=2048; +499=2547; -19=2528; +4=2532. 2532 mod7 = 2532-7*361=2532-2527=5 => Thursday? Actually 5 => Thursday. So Jan 13, 2000 was Thursday. That matches known? I think Jan 13, 2000 was Thursday. Yes, I recall 2000-01-13 was Thursday. So our Zeller gives 5 for Thursday? Wait mapping: 0=Sat,1=Sun,2=Mon,3=Tue,4=Wed,5=Thu,6=Fri. So 5 is Thursday. Good.
Now, for year 2000, October 13: Should be Friday. Check: 2000-10-13: m=10, no adjust, y=2000. Compute: d=13; (13*(10+1)=1311=143)//5=28; y=2000; y//4=500; y//100=20; y//400=5; sum =13+28+2000+500-20+5 =13+28=41; +2000=2041; +500=2541; -20=2521; +5=2526; 2526 mod7 = 2526-7361=2526-2527=-1 => 6 (since mod 7, -1 mod7 =6). So 6=Friday. Good.
Thus Zeller works.
Now, compute f for year 2000 (which maps to idx=400). Our precomputed f[400] should be 1, with month_mask[400][10]=true. Let's test with our code mentally: For year 400? But we don't need to compute manually, trust code.
Now, let's think about possible integer overflow in Zeller: (13*(m+1))/5 with m up to 14, so maximum 13*15=195, /5=39, safe.
Now, we should implement Zeller function to use ints.
Now, note: The precomputation will be done at runtime, which is fine (negligible).
Now, one more nuance: The problem statement mentions "The definition of the year of the stagnation is as follows." It's likely a translation quirk. They define leap year rules exactly as Gregorian.
Now, the output for third example is 1357101234567708000. That's a huge number. Let's attempt to approximate to see if it's plausible. The range is from year 1234 to year 789012345678901234. That's roughly 7.89e17 - 1234 ≈ 7.89e17 years. Average Friday 13th per year ~ 1.72, so total count ~ 1.72 * 7.89e17 ≈ 1.357e18, which matches the given output. So plausible.
Thus algorithm should produce that.
Now, we should double-check the correctness of handling start date that is not January 1. For the huge range, the contribution of the partial start and end years is negligible compared to the full cycles, but must be correct.
Now, need to ensure that month_mask and f are accessible from count_upto, which is called with Y up to 1e18. Inside count_upto, we compute idx = ((Y-1) % 400) + 1, which yields 1..400. So we need to have month_mask and f arrays for indices 1..400.
Now, one potential bug: For Y extremely large, (Y-1) % 400 is computed correctly using ULL modulo.
Now, we need to also consider that the prefix array is based on f for years 1..400. But what about the order? When we compute total for years 1..(Y-1) using q full cycles and then first r years, we rely on the fact that the first r years of the cycle correspond to years (q400+1) to (q400+r). This is true if we align cycles such that the first cycle is years 1..400, second 401..800, etc. That's exactly how we set the mapping: year Y corresponds to cycle index = ((Y-1)%400)+1. So for a given block of years starting from a multiple of 400+1 (e.g., 1,401,801,...), the pattern of f for those years is the same as for indices 1..? So when we have q full cycles, we have exactly q repetitions of the entire 400-year pattern. Then after that, we take the first r years of the pattern. Yes.
But careful: The first cycle (years 1..400) corresponds to indices 1..400. The second cycle (years 401..800) corresponds to indices 1..400 again because mapping gives same indices. So indeed the pattern repeats identically. So summing q*C works.
Now, but what about the ordering of years? For the full cycles, we consider years 1..400q. That includes q complete cycles, each with pattern 1..400. So total = qC.
Now, the remaining years are the next r years, which are years 400q+1 .. 400q+r. Their indices are 1..r. So we add sum_{i=1}^{r} f(i). Good.
Thus implementation correct.
Now, we need to also consider the case where Y is exactly a multiple of 400, say Y=400. Then prev_years=399, q=0, r=399. total = 0 + prefix[399] (sum years 1..399). Then add partial for year 400. So count_upto(400,12,31) should equal C (since includes year 400 fully). Let's check: total = prefix[399] + (full year 400? we need to add months 1..12 because M=12, D=31, so we will add all months 1..11? Actually for M=12, D=31: loop m=1..11 adds f for those months if mask true; and then month 12 added because D>=13 and mask true if applicable. So total becomes prefix[399] + f[400] (since sum over months is exactly f[400]). So total = prefix[399] + f[400] = prefix[400] = C. Good.
Now, if Y=400, M=12, D=12 (D<13), then count_upto would include months 1..11 only, not month 12, so total = prefix[399] + f[400] except maybe month12 not included, so would be less than C, which is correct.
Thus logic holds.
Now, we must ensure that for year Y, the month_mask for months beyond M? Not needed.
Now, we also need to think about the possibility that the start date is such that Y1 is huge, but the month and day are such that we need to include months earlier. That's handled.
Now, let's test a simple case with small numbers to sanity-check our implementation.
We could mentally compute for years 1..3 perhaps using Zeller.
But more important: We need to make sure that the precomputed prefix[0..400] is correct. Since we compute programmatically, it's fine.
Now, potential overflow in prefix sum: prefix[i] can be up to C about 688, and i up to 400, so prefix[400] < 1000, small.
Now, compute C: We can compute it to see if it's 688. Let's quickly estimate using known fact: Over a 400-year cycle, there are 688 Friday the 13ths. I recall reading that any 400-year Gregorian cycle has exactly 688 Friday the 13ths. Yes, I'm pretty sure. So our precomputation should yield 688. Let's test manually with known data: In a 400-year cycle, the 13th falls on each weekday with specific frequencies: the 13th is more likely to be Friday? Actually the distribution: The 13th is Friday 688 times, Thursday 684, etc. So 688 is plausible. So if our C is not 688, then something is off. Let's compute using Zeller quickly for a full 400-year cycle? Could do mental, but trust code.
But we should verify with a small script or reasoning. Since we can't run code now, but we can attempt to compute number of Friday 13ths in the 400-year cycle using known formula: The number of times the 13th occurs on each day of the week in the Gregorian 400-year cycle is known: Monday: 685, Tuesday: 685, Wednesday: 687, Thursday: 684, Friday: 688, Saturday: 684, Sunday: 687? Actually I've seen different numbers. Let me recall: In the Gregorian calendar, the 13th falls most often on Friday. I think the distribution is: Friday 688, Monday 685, Tuesday 685, Wednesday 687, Thursday 684, Saturday 684, Sunday 687? Not sure. But I'm pretty confident Friday is 688. So our precomputed C should be 688. Let's cross-check with another source: There are 4800 months in 400 years. The 13th's day-of-week distribution must sum to 4800. If Friday is 688, the sum of others would be 4800-688=4112. If we assume others around 684-687, plausible. So likely correct.
But we should not rely on that; we just compute.
Now, we must be careful about Zeller formula: Some implementations use a slightly different expression: h = (d + (13*(m+1))/5 + y + y/4 + y/400 - y/100) mod 7. That's same as ours.
Now, also ensure that integer division truncates toward zero (as in C++ for positive numbers). Good.
Now, I think the solution is complete.
Now, let's consider possible alternative pitfalls: The range might be so large that q*C could exceed 2^64? We already reasoned it's safe. But what if Y2 is 10^18, then q ~ 2.5e15, C could be 688, product ~1.72e18. That's okay. But also adding prefix[r] and partial months might bring up to maybe +400, still okay. So maximum count less than 2e18, still within 2^63-1? 2^63-1 ~ 9.22e18, so safe. Even if we consider the maximum possible count from year 1 to 10^18, average maybe 1.72 per year, so about 1.72e18, still within 9e18. So signed 64-bit also safe. However, to be safe, use unsigned long long.
Now, output spec: Output the number on one line. So print using printf("%llu\n", ans) or cout.
Now, double-check the example 3: Output 1357101234567708000. That number is even, which is plausible.
Now, we should also consider that the range includes the start and end dates inclusive. Our method accounts for that.
Now, we should also consider if the start date is after the end date? Not allowed.
Now, we need to think about time zones? No.
Now, all good.
Now, let's proceed to code.
But before finalizing, let's test a few small ranges manually to ensure our count_upto function gives correct results for small Y.
We can compute for the first few years using brute force.
Year 1: Let's compute f[1] using Zeller.
We'll compute day_of_week for each month in year 1.
We'll compute using Zeller with y=1.
We'll need to adjust for Jan and Feb: For Jan: m=13, y=0; Feb: m=14, y=0; For Mar-Dec: y=1.
We'll compute each:
We already computed Jan13: h=0 => Saturday.
Feb13: m=14,y=0: (13*(15)//5=195//5=39; y=0; sum=13+39+0+0-0+0=52; 52 mod7=3? 49+3 => 3? Actually 52 mod7 = 3 because 7*7=49, remainder 3. So h=3 => Tuesday? Wait mapping: 0=Sat,1=Sun,2=Mon,3=Tue,4=Wed,5=Thu,6=Fri. So 3=Tuesday. So Feb13 year1 is Tuesday.
Apr13: m=4,y=1: (13*5=65)//5=13; sum=13+13+1+0=27; 27 mod7=6? 21+6=27 => 6 => Friday! So April 13 year 1 is Friday? Check: 27 mod7 = 27-21=6 => Friday. So f[1] includes April.
Jul13: m=7,y=1: (13*8=104)//5=20; sum=13+20+1=34; 34 mod7=6? 28+6=34 => Friday? Actually 34 mod7 = 34-28=6 => Friday. So July 13 year 1 is Friday? That would be second Friday 13th in year 1? Let's compute: 34 mod7 = 6 => Friday. So July also Friday? But that would mean year 1 has at least April and July as Friday 13ths. Check: 34 mod7 = 6 yes. So two Fridays.
Thus year 1 has Friday 13ths in April and July. That's 2. So f[1]=2.
Now, Year 2: y=2. Compute quickly: But we can trust our precomputation.
Now, what is count_upto(1,4,13)? Should be 1? Let's see: prev_years=0 total=0; idx=1; months 1..3: Jan,Feb,Mar none are Friday (we have Jan not, Feb not, Mar not). Month 4: D>=13 and mask true => include. So total=1. Good.
count_upto(1,4,14) same as above =>1.
count_upto(1,4,12) =>0.
count_upto(1,7,13) => months 1..6: includes April and July? April is month 4, July is month 7? Wait month 7 is not yet processed because we only add months < M? For M=7, D=13, loop m=1..6 includes April (month4) and July not because it's month7. Then month 7 added because D>=13. So total = April + July =2. Good.
Now, count_upto(1,7,12) => months 1..6 includes April; month7 not added because D<13; so total=1.
Thus our function works.
Now, test difference: start=1,4,13 end=1,7,13. count_upto(end)=2, count_upto(start)=1, is_start true? D1=13 and month_mask[1][4]=true => true. So answer =2-1+1=2, which includes both. Good.
Now, test start=1,4,14 end=1,7,13: count_upto(end)=2, count_upto(start)=1 (since start includes April because D=14>=13? Actually start=1,4,14: months 1..3 none, month4 D>=13 => include April, so count_upto(start)=1. So difference=1, is_start false (D1=14 not 13) => answer=1, which is only July. But start date is after April 13? Actually start date is April 14, which is after April 13, so April 13 not included, July 13 included. So correct.
Thus logic stands.
Now, we should also consider that for some years, there might be no Friday 13th. That's fine.
Now, implement.
Potential performance: The precomputation loops over 400 years * 12 months = 4800 iterations, trivial. count_upto called twice, each does O(12) loop. So overall fast.
Now, coding details:
We'll store month_ok as vector<vector> or just array bool[401][13]. Since we need to index by month 1..12, we can use int or bool.
Define:
const int MAX = 401;
int f[MAX]; // or ULL
ULL prefix[MAX];
bool ok[MAX][13]; // 1-indexed months
Initialize f and ok to 0.
Precomputation:
for (int y = 1; y <= 400; ++y) {
for (int m = 1; m <= 12; ++m) {
int dow = day_of_week_13(y, m);
if (dow == 6) {
ok[y][m] = true;
f[y]++;
}
}
}
prefix[0] = 0;
for (int i = 1; i <= 400; ++i) prefix[i] = prefix[i-1] + f[i];
ULL C = prefix[400];
Now, implement day_of_week_13:
int day_of_week_13(int y, int m) {
int d = 13;
if (m <= 2) {
m += 12;
y -= 1;
}
int h = (d + (13*(m+1))/5 + y + y/4 - y/100 + y/400) % 7;
return h;
}
Now, count_upto:
ULL count_upto(ULL Y, int M, int D) {
if (Y == 0) return 0; // not needed but safe
ULL prev = Y - 1;
ULL q = prev / 400;
ULL r = prev % 400;
ULL total = q * C + prefix[r];
ULL idx = ((Y - 1) % 400) + 1;
for (int m = 1; m < M; ++m) {
if (ok[idx][m]) total++;
}
if (D >= 13 && ok[idx][M]) total++;
return total;
}
Now, ensure all variables types are consistent: Y1, Y2 are ULL; M1, etc int.
Now, note: Y1, Y2 could be up to 1e18, which fits in ULL. In C++ we can use unsigned long long. Input reading: scanf("%llu %d %d %llu %d %d", &Y1, &M1, &D1, &Y2, &M2, &D2) but careful: D1, D2 are int, but they are small. Actually D1 and D2 are up to 31, so int fine. But Y1 and Y2 are ULL, so use %llu. Or use cin.
Better use cin to avoid format issues.
We'll do:
cin >> Y1 >> M1 >> D1 >> Y2 >> M2 >> D2;
Now, need to include iostream and cstdint? Use using namespace std; etc.
Now, consider the possibility of negative values? No.
Now, we should also test for Y1=Y2 and M1=M2 and D1> D2? But constraints say start <= end? "more than 0 days before" might mean strictly earlier. The example with equal dates gives output, so equal allowed? Actually "more than 0 days before" would imply strictly before, but they gave equal example which yields 1, so perhaps the phrase means "Y1 M1 D1 is a date more than 0 days before Y2 M2 D2" but the example is equal? Let's read: "Y1 year M January D1 is a date more than 0 days before Y2 year M February D2." Wait the problem statement says: "Y1 year M January D1 is a date more than 0 days before Y2 year M February D2." That might be a translation error. Actually they might have intended to say "Y1 year M1 month D1 is a date not after Y2 year M2 month D2" but they say "more than 0 days before"? The given constraints: "Y1 year M January D1 is a date more than 0 days before Y2 year M February D2." But the first example has equal dates, which is not "more than 0 days before". So maybe the constraint is that the first date is not after the second? Actually the input constraints mention "Y1 ≤ Y2" and then also "Y1 year M January D1 is a date more than 0 days before Y2 year M February D2." That seems contradictory. Possibly the translation means "Y1 year M1 D1 is a date not later than Y2 M2 D2"? But they use phrase "more than 0 days before" might be misinterpreted. But the examples include equal, so likely inclusive and start ≤ end. So we treat inclusive.
Now, final answer.
Now, let's check with a few more test cases:
Case: Y1=2015 M1=1 D1=1, Y2=2015 M2=12 D2=31. Count all Friday 13th in 2015: known 3. Our count_upto(2015,12,31) - count_upto(2015,1,1) + is_start? start is not Friday13. count_upto(2015,12,31) includes all 3 (since full year). count_upto(2015,1,1) includes none. So answer = 3. Good.
Case: Y1=2015 M1=1 D1=1, Y2=2016 M2=12 D2=31. 2015 has 3, 2016 has? 2016 is leap year. Friday 13ths in 2016: Jan? I think 2016 had May and something? Actually 2016: Jan13? Jan 13 2016 was Wednesday? Not sure. Let's quickly recall: 2016 had Friday 13ths in May and July? I think May 13 2016 was Friday, July 13 2016 was Wednesday? Actually need to check. But our method will compute correctly.
Now, one more check: For large Y, we need to be careful that idx = ((Y-1)%400)+1 yields a number in 1..400, but we used idx as ULL and then cast to int for indexing. Since Y is up to 1e18, (Y-1)%400 is computed correctly as ULL and then +1 fits in int after cast because value is between 1 and 400. So we can store as int idx_int = ((Y-1) % 400) + 1;
Now, for prefix array indexing r: r is ULL between 0 and 399, we can cast to int: int r_int = r; but be careful r may be ULL, but it's small. Use prefix[r] with r as size_t.
Now, ensure that month_ok array dimension for idx up to 400, and month up to 12.
Now, we need to consider the case where the start date is exactly a Friday 13th and we add 1. But what if the start date is also counted in count_upto(start)? Actually count_upto(start) includes it. So subtraction removes it, then we add it back. So net include. Good.
Now, potential double counting if we incorrectly add 1 when start is not Friday13? No.
Now, one more nuance: If start date is exactly a Friday 13th, and we compute ans = count_upto(end) - count_upto(start) + 1. But what if end date is the same as start? Then ans = count_upto(start) - count_upto(start) + 1 = 1. Good.
Now, consider if start date is a Friday 13th and end date also the same? Works.
Now, we might consider using count_upto(end) - count_upto(day before start) but handling day before is messy.
Now, finalize.
Now, we should confirm that our precomputed values are correct for years 1..400. I'd like to quickly compute C manually using known result. I can attempt to verify by reasoning: In a 400-year Gregorian cycle, the number of Friday the 13ths is 688. Many sources confirm that. So if our precomputed C is 688, we are confident. If not, maybe our Zeller mapping to Friday = 6 is correct? Or maybe the known 688 uses a different start (e.g., Monday=0)? But known: In the Gregorian calendar, the 13th falls on Friday 688 times in a 400-year cycle. I'm fairly sure. Let's quickly test with a small snippet of mental code? Could compute a few to see if our f[1]=2, f[2]? But to reach 688, average 1.72, f[1]=2 seems plausible. Let's compute f for some years to see if they sum to 688.
We can try to compute for a couple of years manually but not 400. I'll trust our Zeller.
But we should be cautious: Some implementations of Zeller's congruence give a different mapping: 0=Monday, etc. But we tested with known dates and got correct. So it's fine.
Now, I'll produce final code.
But before final, let's double-check the day-of-week for a known Friday 13th in a leap year like 2012 (April 13 2012 was Friday). Test: 2012-04-13: y=2012,m=4,d=13. m>2 so unchanged. Compute: (13*(4+1)=13*5=65)//5=13; y=2012; y//4=503; y//100=20; y//400=5; sum=13+13+2012+503-20+5=13+13=26; +2012=2038; +503=2541; -20=2521; +5=2526; 2526 mod7=6 => Friday. Good.
Now, for 2000-10-13 we got 6. Good.
Now, for 1900? 1900 is not a leap year (since divisible by 100 but not 400). 1900-03-13? Not needed.
So everything fine.
Now, we should also consider that the problem might use the Gregorian calendar but starting from year 1? Yes.
Now, produce final answer.
Now, one more nuance: The constraints say "1 ≤ Y1 ≤ Y2 ≤ 10^18". But also they have conditions like "1 ≤ Di ≤ 31 (Mi = 1,3,5,7,8,10,12)" etc. So D1 and D2 are within valid ranges.
Now, we should also note that the count_upto function assumes that the date is valid. So if D1=31 but month has only 30 days, that input wouldn't happen.
Now, code.
Now, final check: In count_upto, we use prefix[r] where r = prev_years % 400. Since prev_years is ULL, r is ULL. But prefix is an array indexed by int, so we need to convert. Use prefix[(int)r].
Now, ensure that the month_ok array is accessible with idx as int.
Now, compile.
Now, produce final answer.
But before finishing, let's run through the huge example manually with our method to ensure we get the provided output. But we can't compute exactly, but we can trust.
However, I'd like to check the third example programmatically in my mind? Not possible.
We could approximate: The number of full cycles between 1234 and 789012345678901234. Let's compute number of years from start to end inclusive? But we need exact.
But we can test by reasoning that our method yields that output because problem setter likely used similar method.
Now, I'll produce final answer.
But I recall a potential pitfall: The mapping for year 1..400 may not be exactly the same as for years 401..800 if the total offset from year 1 to year 401 is 0 mod 7, but the day-of-week for a given date in year 401 is the same as in year 1 only if we also consider that the Gregorian calendar repeats exactly. But there is a nuance: The Gregorian calendar's cycle of 400 years is based on the leap year pattern, and the day-of-week pattern repeats exactly. So it's fine.
Now, but what about the fact that year 1 is year 1, not year 0? In the 400-year cycle, year 1 is the first year. The next cycle starts at year 401. The mapping we use is consistent.
Now, we could also compute directly using formulas without precomputation, but precomputation is simpler.
Now, one more nuance: The problem might consider that the weekdays are based on some known epoch (like 1 Jan 1 was Monday? Actually historically, 1 Jan 1 AD was Saturday? According to proleptic Gregorian, 1 Jan 1 AD was Monday? I'm not sure. Let's compute using Zeller: For 1 Jan 1 AD: y=1,m=1,d=1. Adjust: m=13,y=0. Compute h = (1 + (13*14)/5 + 0 + 0/4 - 0/100 + 0/400) %7 = 1 + 36 = 37 %7 = 2? 37 mod7 = 2 (since 35+2). So h=2 => Monday? Wait mapping: 0=Sat,1=Sun,2=Mon. So Monday. So Jan 1, 1 AD was Monday according to Zeller. But some sources say it was Saturday? I'm not sure. However, our Zeller is standard for proleptic Gregorian. The problem doesn't specify what day of week corresponds to which days; they just ask for Friday the 13th based on the Gregorian calendar. So as long as we consistently use the same calendar system, it's fine. The leap year rules are given; we must apply them. Zeller's congruence is derived from those rules, so it's appropriate.
Now, there is a known fact: The Gregorian calendar repeats every 400 years, and the distribution of Friday the 13th is constant. So our precomputed f will be correct for any cycle.
Now, I'll code.
But we must consider that some programming languages may not have 64-bit? In C++ we have.
Now, produce final solution.
Now, write the final answer with explanation.
Let's now write the final response. ```cpp
#include
#include
#include
using namespace std;
using ull = unsigned long long;
// Zeller's congruence for Gregorian calendar. Returns day-of-week:
// 0 = Saturday, 1 = Sunday, 2 = Monday, 3 = Tuesday,
// 4 = Wednesday, 5 = Thursday, 6 = Friday.
int day_of_week_13(int y, int m) {
int d = 13;
if (m <= 2) {
m += 12;
y -= 1;
}
int h = (d + (13 * (m + 1)) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
return h;
}
// Precomputed data for one 400‑year cycle (years 1 … 400)
const int CYCLE = 400;
ull f[CYCLE + 1]; // f[i] = number of Friday‑13ths in year i (1‑based)
ull prefix[CYCLE + 1]; // prefix[i] = sum_{j=1}^{i} f[j]
bool ok[CYCLE + 1][13]; // ok[i][m] = true iff month m of year i is a Fri‑13
void precompute() {
for (int y = 1; y <= CYCLE; ++y) {
for (int m = 1; m <= 12; ++m) {
if (day_of_week_13(y, m) == 6) {
ok[y][m] = true;
f[y]++;
}
}
}
prefix[0] = 0;
for (int i = 1; i <= CYCLE; ++i)
prefix[i] = prefix[i - 1] + f[i];
}
// Number of Friday‑13ths with date ≤ (Y, M, D) (inclusive)
ull count_upto(ull Y, int M, int D) {
if (Y == 0) return 0;
ull prev = Y - 1; // full years before Y
ull q = prev / CYCLE;
ull r = prev % CYCLE; // 0 … 399
ull total = q * prefix[CYCLE] + prefix[r];
code
// add contributions of year Y itself
ull idx = ((Y - 1) % CYCLE) + 1; // 1 … 400
for (int m = 1; m < M; ++m)
if (ok[idx][m])
total++;
if (D >= 13 && ok[idx][M])
total++;
return total;