Aizu Nyan has recently caught a cold. I can't get out of bed because I'm too lazy. The spicy appearance is also cute.
However, Aizu Nyan, who had no choice but to spare time, came up with a way to play with the digital clock under the pillow. The number of glowing bars of a digital clock as shown in the figure below is counted every second, just earnestly.
After playing this game for several hours, Aizu Nyan noticed that the number of glowing sticks may be the same even if the time is different. I was wondering how many times the number of glowing sticks was the same, and Aizunyan asked you to count it while you were nursing.
For Aizu Nyan who can't get out of bed, you decide to write a program and ask for it.
problem
There is a digital clock like the one shown above that displays the year, month, day, and hour, minute, and second. Answer how many years, months, days, minutes, and seconds the number of glowing bars is exactly N. However, since the digital clock is partially broken, there are some sticks that never shine. Note that even if the display looks the same depending on the non-illuminating stick, if the year, month, day, hour, minute, and second you tried to display are different, they are counted separately.
The shining parts of each number are as shown in the following figure.
Digital clocks display the year in 0-sized 4 digits (0000 to 9999) and month, day, hour, minute, and second in 0-sized 2 digits.
The month starts on the 1st, April, 6, 9, November until the 30th, and 1, 3, 5, 7, 8, 10, December until the 31st. February is usually up to 28th, but leap years are up to 29th. Here, a leap year is the following year.
A year in which the Christian era is divisible by 400 is a leap year.
The year is not divisible by 400, but the year divisible by 100 is not a leap year.
The year is not divisible by 100, but the year divisible by 4 is a leap year.
A year in which the Christian era is not divisible by 4 is not a leap year.
The hours are from 0:00 to 23:00, the minutes are from 0 to 59 minutes, and the seconds are from 0 to 59 seconds. You don't have to think about leap seconds.
Input format
The number of glowing bars N (0 ≤ N ≤ 98) is given in the first line.
The number of broken parts K (0 ≤ K ≤ 98) is given in the second line, and then the information of the broken parts is given in the K line. On the i-th line of the K line, the digit p_i (0 ≤ p_i ≤ 13) and the position q_i (0 ≤ q_i ≤ 6) of the i-th broken bar are given, separated by blanks.
The digit is the ID assigned to each number of the digital clock as shown in the following figure, and is the number assigned from 0 to 13 in order from the highest number.
The position of the bar is the ID of the bar in each number as shown in the following figure, and is the number assigned the numbers from 0 to 6 in order from the upper bar.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Output the total number of years, months, hours, minutes, seconds, and seconds when the number of glowing bars is exactly N on one line.
Input example 1
28
0
Output example 1
1
It is one way of 11:11:11 on November 11, 1111.
Input example 2
28
1
2 0
Output example 2
2
There are two ways: 11:11:11 on November 11, 1171, and 11:11:11 on November 11, 1111.
Input example 3
98
1
3 0
Output example 3
0
Input example 4
60
1
3 0
Output example 4
13362470370
Example
Input
28
0
Output
1
We need to understand the problem.
We have a digital clock that displays: year (4 digits), month (2 digits), day (2 digits), hour (2 digits), minute (2 digits), second (2 digits). So total digits = 4+2+2+2+2+2 = 14 digits. But the problem says digits assigned from 0 to 13 inclusive, which is 14 digits. Yes.
Each digit is a 7-segment display. Normally each digit has 7 segments (bars) that can light up to form digits 0-9. However, the clock is partially broken: there are some sticks that never shine. That is, certain segments (bars) in certain digit positions are permanently off. Even if the number to display would normally turn that segment on, it cannot light up because it's broken. The clock still attempts to display the time, but the broken segments remain off. The total number of glowing bars at a given time is the count of segments that are supposed to be on (based on the digit) but are not broken. If a segment is broken, it never lights, so it doesn't count even if the digit pattern would normally light it.
We are given N (0 ≤ N ≤ 98) and K broken segments. For each broken segment, we are given digit p_i (0-13) and position q_i (0-6). So we know which specific segment in which digit is broken.
We need to count how many valid date-time combinations (year, month, day, hour, minute, second) result in exactly N glowing bars total, considering the broken segments.
The date-time ranges:
Year: 0000 to 9999 (4 digits, leading zeros allowed). So 10000 years.
Month: 01-12? Actually "month starts on 1st"? The problem says: "The month starts on the 1st, April, 6, 9, November until the 30th, and 1, 3, 5, 7, 8, 10, December until the 31st. February is usually up to 28th, but leap years are up to 29th." So months are from 1 to 12. Displayed as 2 digits with leading zeros (01 to 12).
Day: depends on month and leap year. Displayed as 2 digits with leading zeros (01 to 31). Actually day can be from 1 up to max days of that month, but displayed as two digits. So days from 01,02,... up to 28/29/30/31.
Hour: 00-23 (2 digits)
Minute: 00-59 (2 digits)
Second: 00-59 (2 digits)
All combinations that satisfy the calendar constraints are valid.
We need to count the number of such timestamps (year, month, day, hour, minute, second) such that total glowing bars = N.
The clock attempts to display each digit with 7 segments, but broken segments remain off. The glowing bars are those segments that are supposed to be on according to the digit's pattern AND the corresponding segment is not broken. So for each digit d (0-13), we know which segments are broken (possibly none). For a given digit value (0-9) to be displayed at that digit position, we have a set of segments that should be on (the usual pattern). However, because of broken segments, only the subset that are not broken actually glow. So the number of glowing bars contributed by that digit is: count of segments in the digit's pattern minus those that are broken (and would have been on). More precisely, if we denote for digit position p, let broken[p] be a bitmask (or set) of segment indices that are broken. For a digit value v (0-9), let pattern[v] be the set of segments that normally light. Then the actual glowing segments = pattern[v] ∩ complement(broken[p]). So number = |pattern[v]| - |pattern[v] ∩ broken[p]|. Alternatively, we could precompute for each digit position p and each possible digit value v, the number of segments that actually light: segCount[p][v] = number of segments in pattern[v] that are not broken at position p.
Then total N = sum_{p=0}^{13} segCount[p][digitValueAtPosition(p)].
We need to count the number of valid date/time combinations that yield that total.
Constraints: The ranges are large. There are 10000 years * (approx) 365.25 days * 24 hours * 3600 seconds ~ 31.5e9 ~ 3.15e10. That's about 31.5 billion combinations. That's too many to brute-force directly, but perhaps we can do DP or combinatorial counting because N ≤ 98, and digits are independent? However, the date constraints introduce dependencies across digits (e.g., year determines leap, month and day constraints, and the digits are not independent across positions). But we can break the problem into parts: we have 14 digit positions with constraints based on date.
We need to count the number of (Y, M, D, h, m, s) where Y in 0..9999, M in 01..12, D in 01..maxDay(Y,M), h in 00..23, m in 00..59, s in 00..59. Each component has 2 or 4 digits. The total N is sum of segCounts across these digits.
Given that N ≤ 98, and each digit maximum segCount is at most 7 (if no broken), but due to broken segments maybe less. But we could treat each digit independently and then combine counts with convolution, but we have constraints linking digits (e.g., month and day ranges, leap year). However, month and day involve 4 digits (2 for month, 2 for day) with a relationship (month-day combination must be valid for the year). Year is independent, and hour/minute/second are independent of date except they have no cross-constraints.
Thus we can compute counts for year (4 digits) separately, counts for hour (2 digits), minute (2 digits), second (2 digits) separately, and then month-day as a combined unit (4 digits) with constraints depending on year (leap). The month and day are not independent because day depends on month and year. So we need to count, for each year, the number of (month, day) pairs that yield a certain total segCount for those 4 digits (month and day digits). Then combine with year count and time counts.
Approach: Precompute segCount[p][d] for each digit position p (0-13) and digit d (0-9). Also note that leading zeros are allowed for all fields? For year, it's 4-digit with leading zeros, yes. For month, it's always two digits from 01 to 12, so first digit of month can be 0 or 1, second digit 0-9 but with constraints. So month is always two-digit with leading zero. Same for day: day is two-digit with leading zero (01-31). Hour: 00-23, minute:00-59, second:00-59. So all digits are always displayed as fixed width, with leading zeros allowed.
So we have 14 digits:
Positions:
Year: digits Y3 (most significant), Y2, Y1, Y0 (least). Typically indices 0,1,2,3? The problem says digits assigned from 0 to 13 in order from the highest number. So 0 is the leftmost digit of year? The figure shows year 4 digits, then month 2, day 2, hour 2, minute 2, second 2. So yes, 0: year thousand, 1: year hundred, 2: year ten, 3: year one; 4: month ten, 5: month one; 6: day ten, 7: day one; 8: hour ten, 9: hour one; 10: minute ten, 11: minute one; 12: second ten, 13: second one.
Now, each digit position p has its own broken set.
Given N up to 98 and max total segments (if all segments work) is 147=98. So N=98 means all segments are on, which only possible if all digits are 8? Actually digit 8 uses all 7 segments. So if all digits are 8, total = 147=98. But also need broken segments none. N max =98. So N matches total possible.
Now, we need to count the number of valid timestamps that sum to N. Since total combinations is ~3.15e10, but counting by DP over 14 digits with constraints might be doable via enumerations over each component? 3.15e10 is not huge for a computer if we just loop over all? Actually 31.5 billion is large; typical loops of billions may be borderline but perhaps too slow in Python. We need efficient counting.
We can treat each field separately and use combinatorics. However, due to constraints, month and day have only up to 366 possibilities per year, and year has 10000 possibilities. So naive enumeration over year (10000) * (month, day) up to 366 ~ 3.66 million combos, and then time (86400 combos) yields 1000036686400 ~ 3.16e11, too large. So we need smarter.
We can precompute for each possible sum contributions of each field (year, month-day, hour, minute, second) and then use convolution (like sum of four independent sums equals N) but note that month-day depends on year (leap). However, we can treat year and month-day combined? Actually full date = year + month + day. Could precompute count of each sum for date (year+month+day) across all valid dates (including leap). That's 10000366 ≈ 3.66e6 combinations, which is manageable. Then time (hour+minute+second) has 2460*60 = 86400 combos. So we can compute a frequency array freqDate[sum] = number of dates (Y,M,D) with total segCount = sum, and freqTime[sum] = number of times (h,m,s) with total segCount = sum. Then answer = sum_{a+b=N} freqDate[a] * freqTime[b]. That is straightforward and efficient.
Complexities:
Dates: up to 10000 * 12 * 31? Actually maximum days per year 366, so total dates = sum over years of number of days in that year. For non-leap 365, leap 366. Over 10000 years, total dates = 10000365 + number of leap years. Number of leap years in 0..9999? According to leap rules: years divisible by 400 are leap; years divisible by 100 but not 400 are not; years divisible by 4 but not 100 are leap. So count leaps = floor(10000/4) - floor(10000/100) + floor(10000/400) = 2500 - 100 + 25 = 2425? Actually 10000/4=2500, 10000/100=100, 10000/400=25. So 2500 - 100 + 25 = 2425. So total dates = 10000365 + 2425 = 3,652,425? Wait 365*10000 = 3,650,000 plus 2425 = 3,652,425. That's about 3.65 million, which is fine.
Times: 246060 = 86,400.
Thus we can compute two frequency arrays of size up to max possible sum (98). Then convolution O(98*98) trivial.
So plan:
Parse input: N, K, then K lines (p, q). Build a list broken[p] (set of q) for each p=0..13.
Predefine the normal segment patterns for digits 0-9: Typically 7-seg:
Standard mapping: segments labeled 0..6: Usually 0=top, 1=upper right, 2=lower right, 3=bottom, 4=lower left, 5=upper left, 6=middle. But we need to confirm from problem description. The problem says "position of the bar is the ID of the bar in each number as shown in the following figure, and is the number assigned the numbers from 0 to 6 in order from the upper bar." The figure likely shows standard 7-segment labeling: top (0), upper right (1), lower right (2), bottom (3), lower left (4), upper left (5), middle (6). That is common. Let's assume that. The digits patterns:
0: segments {0,1,2,3,4,5} (all except middle) -> 6 segments.
1: segments {1,2} -> 2 segments.
2: segments {0,1,3,4,6} -> 5 segments? Actually typical 2: top, upper right, middle, lower left, bottom => segments 0,1,6,4,3 => that's 5.
3: segments {0,1,2,3,6} -> 5 segments (top, upper right, lower right, bottom, middle).
4: segments {1,2,5,6} -> 4 segments (upper right, lower right, upper left, middle).
5: segments {0,2,3,5,6} -> 5 segments (top, lower right, bottom, upper left, middle).
6: segments {0,2,3,4,5,6} -> 6 segments (all except upper right). Actually typical 6: top, upper left, lower left, bottom, lower right, middle => 0,5,4,3,2,6? And maybe also? Wait typical 6 includes lower right, yes. So 6: segments {0,2,3,4,5,6} but also includes? Let's verify: Standard 7-seg for 6: top, upper left, lower left, bottom, lower right, middle. That's segments 0,5,4,3,2,6. So includes 2 (lower right) but not 1 (upper right). So yes {0,2,3,4,5,6} = 6 segments.
7: segments {0,1,2} -> 3 segments (top, upper right, lower right).
8: segments {0,1,2,3,4,5,6} -> 7 segments.
9: segments {0,1,2,3,5,6} -> 6 segments (all except lower left).
Check: 9: top, upper right, lower right, bottom, upper left, middle => {0,1,2,3,5,6}. So correct.
So we can define patterns as sets or bitmask.
For each position p (0..13), compute segCount[p][d] for d=0..9:
count = number of segments in pattern[d] that are not in broken[p].
That is: len(pattern[d]) - len(pattern[d] ∩ broken[p]).
Because broken segments never light.
Then generate all possible dates (Y, M, D) valid, compute sum over positions 0-7? Actually year digits are positions 0-3, month digits positions 4-5, day digits positions 6-7. So total 8 digits for date? Wait date includes year, month, day: total 4+2+2 = 8 digits. However, earlier we said 14 digits total: year (4), month (2), day (2), hour (2), minute (2), second (2). So date part = 8 digits, time part = 6 digits.
But careful: In the date part, month and day depend on each other and year. So we need to compute date sum: sum_{p=0}^{7} segCount[p][digit_p]. For each valid date.
Similarly, for time: positions 8-13 (hour ten, hour one, minute ten, minute one, second ten, second one). For each valid time (h,m,s) with h in 0..23, m in 0..59, s in 0..59, compute sum_{p=8}^{13} segCount[p][digit_p].
Implementation details:
Represent digits of a number with fixed width: For year (0-9999), we need to output 4 digits with leading zeros. So we can get thousands = Y // 1000, hundreds = (Y//100)%10, tens = (Y//10)%10, ones = Y%10. Similarly for month: two digits: tens = M//10, ones = M%10; but month can be 1..12, so tens can be 0 or 1. So we can treat M as integer and compute digits. Day: D from 1..31, compute tens = D//10, ones = D%10. Hour: 0..23; minute: 0..59; second: 0..59.
Precompute segCount for each position p and digit d.
Now we need to generate all dates from year 0 to 9999 inclusive? Check: Year range: "0000 to 9999". So year = 0 is allowed? Typically Gregorian calendar? The problem doesn't specify any special calendar; it just uses the rules for leap years based on Christian era (Gregorian) but includes year 0? Gregorian calendar didn't have year 0; it goes from 1 BC to 1 AD. But problem says "year in which the Christian era is divisible by 400 is a leap year." This is the Gregorian rule but applied to year numbers, including 0? Typically Gregorian leap year rules are defined for years > 0. But the problem says year from 0000 to 9999. We should assume year 0 exists and apply the same rules? In many programming problems, they treat year 0 as a leap year? Let's see: The leap year condition: "1. A year in which the Christian era is divisible by 400 is a leap year. 2. The year is not divisible by 400, but the year divisible by 100 is not a leap year. 3. The year is not divisible by 100, but the year divisible by 4 is a leap year. 4. A year in which the Christian era is not divisible by 4 is not a leap year."
If we apply these rules to year 0: 0 divisible by 400? Yes (0/400=0). So by rule 1, it's a leap year. That would mean February has 29 days. Is that acceptable? Possibly. Many such problems treat year 0 as leap (e.g., in UVA or other). We'll adopt that.
Also month days: "The month starts on the 1st, April, 6, 9, November until the 30th, and 1, 3, 5, 7, 8, 10, December until the 31st. February is usually up to 28th, but leap years are up to 29th." So months:
30 days: April (4), June (6), September (9), November (11).
31 days: January (1), March (3), May (5), July (7), August (8), October (10), December (12).
February (2): 28 or 29.
Check: They mentioned "1, 3, 5, 7, 8, 10, December" likely means months 1,3,5,7,8,10,12. Yes.
So we need to generate all valid (Y,M,D). For each year Y, determine if leap: leap = (Y%400==0) or (Y%100!=0 and Y%4==0). For Y=0, Y%400=0 => leap true.
For each month M=1..12, for each day D from 1 to monthDays[M] (with Feb special), generate date.
Now we need to compute sum for date. The sum is sum over 8 digit positions (0-7). But careful: The digits are arranged in order: year (4 digits), month (2), day (2). So we need to map digits to positions.
We'll compute digits for each component and add contributions using precomputed segCount[p][digit].
We can do loops:
For Y from 0 to 9999:
Compute year digits: y3 = Y // 1000; y2 = (Y // 100) % 10; y1 = (Y // 10) % 10; y0 = Y % 10.
But careful: The input example 2: N=28, broken: one segment: digit 2, position 0. That means the segment 0 (top) of the digit position 2 is broken. Position 2 is the third digit of year? Let's check: digit indices: 0: year's most significant, 1: next, 2: year's ten, 3: year's one. So digit 2 is year's ten digit. For year 1111, the digits are: 1,1,1,1. For year 1171, digits: 1,1,7,1. The broken segment 0 on digit 2: segment 0 is top. For digit 1, pattern[1] has segments {1,2} only, not top. So if digit 2 is 1, segment 0 is not lit anyway, so broken doesn't affect count. But for digit 7, pattern[7] = {0,1,2} includes top (0). So if broken, then when digit 2 is 7, that segment does not light, reducing count. Let's compute example 2: Input: N=28, broken: (2,0). Output 2. They say there are two ways: 11:11:11 on November 11, 1111, and 11:11:11 on November 11, 1171. So in the second, year 1171 has digits: 1,1,7,1. The third digit is 7, which normally would have 3 segments (0,1,2). But segment 0 is broken, so it only lights 2 segments (1 and 2). So the total glowing bars count remains 28 (same as all 1's). The first case all digits are 1 (digit 1 uses 2 segments each), but wait: each digit 1 uses 2 segments, so total segments if all digits are 1: 14 * 2 = 28. But we have a broken segment at digit 2, but digit 1 doesn't use that segment, so total remains 28. So they counted two timestamps.
Thus our method works.
Example 4: N=60, broken: (3,0). Output huge number 13362470370. That's ~13.36e9, which is plausible total number of timestamps? Total number of timestamps is number of valid date-time combos: total dates * 86400. total dates = 3,652,425? Actually total dates from year 0-9999 inclusive: 3,652,425. Times: 86,400 => total combos = 3,652,425 * 86,400 ≈ 315, something? Let's compute: 3,652,425 * 86,400 = 3,652,425 * 864 * 100 = (3,652,425864)100. 3,652,425800=2,921,940,000; 64=233,755,200; sum = 3,155,695,200? Actually compute precisely: 3,652,425 * 86,400 = 3,652,425 * (243600) but let's do: 3,652,425 * 86,400 = 3,652,425 * 864 * 100 = (3,652,425864)100. 3,652,425864 = 3,652,425800=2,921,940,000; 60=219,145,500; 4=14,609,700; sum = 2,921,940,000+219,145,500=3,141,085,500; +14,609,700=3,155,695,200; times 100? Wait we multiplied by 864 and then by 100? Actually 86,400 = 864 * 100? 864100=86,400 yes. So product = (3,652,425864)100 = 3,155,695,200100? Let's recalc: 3,652,425 * 864. Compute with more precision: 3,652,425 * 864 = 3,652,425 * (8100 + 64?) Better: 3,652,425 * 864 = 3,652,425 * (2^5 * 27?) Not needed. Let's use calculator mental: 3,652,425 * 800 = 2,921,940,000; *60 = 219,145,500; *4 = 14,609,700; sum = 2,921,940,000 + 219,145,500 = 3,141,085,500; +14,609,700 = 3,155,695,200. Good. Then *100 = 315,569,520,000? That would be 3.155e11, which is about 315 billion. But example output is 13.36e9, which is much smaller. So total combos is ~315 billion, but answer is about 13.36 billion. That's plausible for N=60 with a broken segment. So our counting must use 64-bit integers. Python int can handle.
Now we must ensure we treat digit indices correctly. According to the digit IDs from 0 to 13: year (0-3), month (4-5), day (6-7), hour (8-9), minute (10-11), second (12-13). The problem statement says "digit p_i (0 ≤ p_i ≤ 13)" and "position q_i (0 ≤ q_i ≤ 6)". So we must map correctly.
Edge Cases: K can be 0. Broken segments may duplicate? Probably not, but we can just add to set.
Now, check the sample 1: N=28, K=0. Then we count number of timestamps with total segments exactly 28. According to example, output 1. That suggests that only 1111-11-11 11:11:11 yields sum 28 when no broken segments? But wait, all digits are 1 yields 142=28. Could there be other combinations giving total 28? Possibly, but sample says output 1. So maybe with no broken segments, only that specific timestamp yields exactly 28? But we can verify: digits patterns: 0=6,1=2,2=5,3=5,4=4,5=5,6=6,7=3,8=7,9=6. So each digit contributes a certain number. To get total 28 across 14 digits, average 2 per digit. So all digits must be 1 (each 2) or maybe some combination mixing digits with 2 and others with more/less but sum still 28. But if any digit deviates from 2, we need to compensate with other digits that are not 2 to keep sum 28. For example, digit 7 gives 3 (+1) would require another digit with 1 to offset, but no digit gives 1. The smallest contribution is digit 1 with 2, digit 7 with 3, digit 4 with 4, etc. Actually digit? Are there digits with 1? Not in standard 7-seg. So each digit gives at least 2? Wait digit 1 gives 2, digit 7 gives 3, digit 4 gives 4, etc. But digit 0 gives 6, 2 gives 5, 3 gives 5, 4 gives 4, 5 gives 5, 6 gives 6, 7 gives 3, 8 gives 7, 9 gives 6. Minimum is 2 (digit 1). So sum cannot be less than 28 if all digits are 1? Actually 142=28 is the minimum total if all digits are 1? But is there any digit with 1? None. So the global minimum total (with all digits 1) is 28. But could there be a digit that contributes 0? No, because at least one segment lights? Actually if a digit is 0, it uses 6 segments, so more. So the minimum total is 28 (all ones). So any timestamp with all digits = 1 would yield 28. Are there other timestamps that also yield 28? If any digit is not 1, its contribution >2, so total >28. However, if some digits are 1 and others are something else, total would exceed 28 unless some digits contribute less than 2 to offset. Since none contribute less than 2, the only way to get exactly 28 is all digits = 1. So only one timestamp: year 1111, month 11, day 11, hour 11, minute 11, second 11. But wait, month 11 is November, day 11 is valid, hour 11 is valid (11 AM or 11 PM? Actually hour 11 is valid (0-23 includes 11). minute 11, second 11. That's one. Could there be year 1111, month 11, day 11, hour 11, minute 11, second 11 only. So output 1. So consistent.
So the method stands.
Now compute segCount with broken segments.
One nuance: Broken segments can affect the minimal possible sum? If some segments are broken, the contribution of a digit may be less than its normal pattern because some segments that would be on are broken. Could a digit contribute 0? Possibly if all segments that would normally light are broken. For example, if for a digit position, all segments of pattern[d] are broken, then that digit contributes 0 glowing bars. That might allow totals less than 28. And also digits might have different contributions.
But overall, our method enumerating all dates and times is fine.
Complexities: Date enumeration: 3.65e6 loops; inside loops over months (12) and days (up to 31) per year? Actually we are going year->month->day triple nested: total number of date combos is sum over years of days in that year = ~3.65e6. So if we iterate year from 0 to 9999, and for each year iterate over months and days, total inner loops exactly the number of dates. That's okay.
But we need to compute year digits and yearSum for each year; we do that inside the outer loop, before month loop. Then for each month we compute monthSum; for each day we compute daySum. That's fine.
Time enumeration: 246060 = 86400. Manageable.
We should be careful about performance in Python: 3.65 million loops is trivial (<0.1 sec). Actually 3.65 million is fine. 86,400 also fine. So total compute <0.5 sec.
But we need to ensure we use appropriate data structures: segCount as list of lists: 14x10 integers. Also dateFreq and timeFreq as list of length maybe up to 98+1, but could be up to maximum possible sum. Max sum per date: sum of max contributions for positions 0-7. Since each position max can be up to 7 (if no broken) but could be less. But we can allocate size up to 98 (maybe 98+1). But to be safe, we can compute maximum possible sum across all 14 digits: for each p, max contribution = max_{d in 0..9} segCount[p][d]. Then sum these maxes for date (p=0..7) to get maxDateSum; similarly for time. But N max is 98, so we only need up to 98. So we can set array size 99 (index 0..98). Initialize to 0. Then when we compute total for date, if total > 98, we could ignore? Actually N ≤ 98, so we only care totals ≤ 98. But some totals may exceed 98. In that case, we can skip incrementing (since they won't be used). But we could still count them but they won't affect answer. But to save memory, we can still increment if total ≤ 98, else ignore. That's fine.
However, note that for convolution, we need timeFreq for all sums ≤ 98. So we only need to store sums ≤ 98. So we can cap: if total > 98: continue (don't increment). This speeds up slightly.
But careful: For date sums, we need to consider only those ≤ 98; but some date sums might be >98, they won't contribute. So we can ignore them.
Now, Implementation details:
Read N integer.
Read K integer.
Initialize broken = [set() for _ in range(14)].
For i in range(K): read p, q; broken[p].add(q).
Define patterns: pattern[d] = set of segments that are on. Use the standard mapping as above. We'll define a list patterns = [set(...), ...].
But we need to ensure the segment IDs correspond to the figure. The problem says "position of the bar is the ID of the bar in each number as shown in the following figure, and is the number assigned the numbers from 0 to 6 in order from the upper bar." That likely means: 0: top, 1: upper right, 2: lower right, 3: bottom, 4: lower left, 5: upper left, 6: middle. This is common. We'll assume that.
Now for each position p (0..13):
segCount[p] = [0]*10
For d in 0..9:
count = len(pattern[d])
for segment in pattern[d]:
if segment in broken[p]:
count -= 1
segCount[p][d] = count
Alternatively, we could compute using set difference: count = len(pattern[d] - broken[p]). But pattern[d] is a set, broken[p] is a set. So count = len(pattern[d] - broken[p]).
Let's do that.
Now date enumeration:
dateFreq = [0]*99
We'll iterate Y from 0 to 9999 inclusive.
For each Y:
y3 = Y // 1000
y2 = (Y // 100) % 10
y1 = (Y // 10) % 10
y0 = Y % 10
yearSum = segCount[0][y3] + segCount[1][y2] + segCount[2][y1] + segCount[3][y0]
compute leap
leap = (Y % 400 == 0) or (Y % 100 != 0 and Y % 4 == 0)
for M in range(1,13):
m1 = M // 10
m0 = M % 10
monthSum = segCount[4][m1] + segCount[5][m0]
if M in (4,6,9,11): maxDay = 30
elif M == 2: maxDay = 29 if leap else 28
else: maxDay = 31
for D in range(1, maxDay+1):
d1 = D // 10
d0 = D % 10
daySum = segCount[6][d1] + segCount[7][d0]
total = yearSum + monthSum + daySum
if total <= 98:
dateFreq[total] += 1
But careful: month M is from 1 to 12, but month displayed as two digits with leading zero. For M=1, digits: 0 and 1? Actually tens digit is 0 for months 1-9. So m1 = M // 10 yields 0 for 1-9, 1 for 10-12. That's correct.
Day: D from 1 to maxDay, digits: for D=1..9, tens digit 0, ones digit D. Good.
Now time enumeration:
timeFreq = [0]*99
for h in range(24):
h1 = h // 10
h0 = h % 10
hourSum = segCount[8][h1] + segCount[9][h0]
for m in range(60):
m1 = m // 10
m0 = m % 10
minuteSum = segCount[10][m1] + segCount[11][m0]
for s in range(60):
s1 = s // 10
s0 = s % 10
secondSum = segCount[12][s1] + segCount[13][s0]
total = hourSum + minuteSum + secondSum
if total <= 98:
timeFreq[total] += 1
Alternatively, we can precompute contributions for each two-digit pair to reduce nested loops? But 86400 is fine.
Now answer = sum_{a=0}^{98} dateFreq[a] * timeFreq[N - a] if (N-a) in 0..98.
But we must be careful about integer overflow? Python fine.
Check example 1: N=28, K=0, no broken segments. Then dateFreq[?] Let's compute total date combos? But we only need ones with total date sum a such that a + time sum = 28. With no broken, min date sum? For date (year+month+day), all digits are 1? The minimum possible date sum is when all digits are 1: 8 digits 2 = 16? Wait 8 digits (date) 2 = 16. But is it possible to have all date digits =1? That would require year=1111, month=11, day=11. That's one date. Similarly time digits all 1: hour=11, minute=11, second=11: that's one time. So date sum for that date is 82=16, time sum for that time is 62=12, total=28. So dateFreq[16] should be at least 1, timeFreq[12] at least 1. Are there other date sums 16? Possibly other dates where the digits sum to 16? But min date sum is 16, because minimum per digit is 2, so date sum min = 82 = 16. Similarly time sum min = 62 = 12, total min = 28. So any total 28 must have date sum = 16 and time sum = 12. So we need to check if there are any other date combinations with sum 16. As argued, to have sum 16 each digit must be 1 because any digit >1 adds at least 3? Actually digit 1 is 2, digit 7 is 3, so if any digit is not 1, sum >16. So only all ones. But can there be a date with all digits =1? Year=1111, month=11, day=11 is valid. No other year has all digits 1? Could be 1111 only. Could month 11 day 11 be other years? Year must be 1111 to have all digits 1. So exactly 1 date. Similarly time: only 11:11:11 yields all ones? hour 11, minute 11, second 11. So exactly 1. So product =1. So output 1.
Thus our enumeration should produce dateFreq[16]=1, timeFreq[12]=1.
Now example 2: broken segment at (2,0). That changes segCount for position 2 (year ten digit) for some digits. Let's compute:
pattern[1] = {1,2} so broken segment 0 doesn't affect, segCount[2][1] still 2.
pattern[7] = {0,1,2} so broken segment 0 removes that, so segCount[2][7] becomes len({1,2}) = 2 (instead of 3). So digit 7 now contributes 2, same as digit 1.
So now for year ten digit (position 2), digits 1 and 7 both contribute 2. Also other digits contributions may be affected if they include segment 0. pattern[0] = {0,1,2,3,4,5} includes 0, so segCount[2][0] = 5? Actually pattern[0] has 6 segments including 0, so if broken, becomes 5. pattern[2] = {0,1,3,4,6} includes 0 -> becomes 4? Actually pattern[2] normally 5 segments, minus 1 => 4. pattern[3] = {0,1,2,3,6} includes 0 -> becomes 4? Actually pattern[3] 5 segments -> 4. pattern[4] = {1,2,5,6} doesn't include 0 -> remains 4. pattern[5] = {0,2,3,5,6} includes 0 -> becomes 4? Actually pattern[5] 5 -> 4. pattern[6] = {0,2,3,4,5,6} includes 0 -> becomes 5 (from 6). pattern[8] = all 7 includes 0 -> becomes 6. pattern[9] = {0,1,2,3,5,6} includes 0 -> becomes 5 (from 6). So many digits become less.
Now minimum per digit: at position 2, minimum is now? digit 1 gives 2, digit 7 gives 2, digit 4 gives 4, etc. So minimum is still 2. But overall date sum min might still be 16? Let's compute: For all date digits to be 1, we have year digits: 1,1,1,1 => positions 0,1,2,3 all digits 1. For position 2, digit 1 gives 2 (since not affected). So yearSum = 2+2+2+2 =8? Actually each digit 1 contributes 2, so year (4 digits) sum = 8. Month 11: digits both 1 => 2+2=4. Day 11: 2+2=4. Total date = 8+4+4=16. That's same as before. But now there is also year 1171: digits: 1,1,7,1. At position 2, digit 7 now gives 2 instead of 3, so yearSum becomes 2+2+2+2=8? Wait, position 0 digit 1 ->2, position 1 digit 1->2, position 2 digit 7->2 (because broken), position 3 digit 1->2 => total year=8. So year still 8. So date sum for 1171-11-11 also 16. So there are two dates: 1111-11-11 and 1171-11-11. Are there any others? For date sum 16, all date digits must be 1 except possibly at position 2 could be 7 because it also yields 2. But also other positions? Could other digits also have alternatives that yield 2? At other positions, only digit 1 gives 2 (since no broken). So positions 0,1,3 must be 1. Position 2 could be 1 or 7. But also maybe digit? At other positions, if they had broken segments reducing other digits to 2? But in this specific input, only position 2 has a broken segment that reduces digit 7 to 2; other positions have no broken, so only digit 1 gives 2. So date digits: pos0=1, pos1=1, pos2∈{1,7}, pos3=1; month digits: pos4 and pos5 both 1; day digits: pos6 and pos7 both 1. So that yields 2 dates. Also need to check if month 11 and day 11 are valid for year 1171? Yes, November 11 is always valid. So 2 dates. Similarly, time sum 12 must be from all ones? At time positions, no broken, so only time 11:11:11 yields sum 12. So total 2 combos. So answer 2.
Thus our method should produce that.
Now we must be careful: The date sum includes year, month, day. We must ensure we include both dates. In our enumeration, year 1111 and 1171 will both be considered, and month=11, day=11 will be considered, so dateFreq[16] will be 2. timeFreq[12] = 1. So answer = 2. Good.
Now for example 3: N=98, broken: (3,0). Output 0. That means no timestamp yields total 98. With broken segment at position 3 (year ones digit), segment 0 broken. Digit 8 normally uses all 7 segments; with broken segment 0, digit 8 would have 6. So maximum total might be less than 98. But we can trust enumeration.
Now example 4: N=60, broken: (3,0). Output 13362470370. We'll trust our method.
Now let's think about potential pitfalls:
Leap year calculation: Use rules correctly. The problem statement:
Year divisible by 400 -> leap.
Not divisible by 400 but divisible by 100 -> not leap.
Not divisible by 100 but divisible by 4 -> leap.
Not divisible by 4 -> not leap.
We'll implement: leap = (Y % 400 == 0) or (Y % 4 == 0 and Y % 100 != 0). That matches.
Year 0 is included; Y%400==0 true, so leap.
Months: The problem says "The month starts on the 1st, April, 6, 9, November until the 30th, and 1, 3, 5, 7, 8, 10, December until the 31st." That is a bit oddly phrased but it's standard: months with 30 days: April (4), June (6), September (9), November (11); months with 31 days: January (1), March (3), May (5), July (7), August (8), October (10), December (12). February special.
Day range: from 1 to maxDay inclusive.
Hour: 0-23 inclusive. Minute: 0-59. Second: 0-59.
The digits: For month, day, hour, minute, second, leading zeros are displayed as 0. So e.g., hour 5 is displayed as "05". That's correct.
Broken segments: The same segment might be listed multiple times? Not likely; but we can use set to avoid duplicates.
Digit indices: ensure correct mapping. Double-check: According to "digit p_i (0 ≤ p_i ≤ 13) and position q_i (0 ≤ q_i ≤ 6)". The figure likely shows 14 digits. We'll assume our mapping is correct.
In the date enumeration, we compute yearSum for each year only once, and then reuse for all months and days of that year. That's efficient.
Potential performance: 3.65e6 loops is fine.
But wait: total dates = ~3.65 million. That's for year 0 to 9999 inclusive? Actually year 0 is included. So number of years = 10000. Number of dates = total days across 10000 years. As computed, 3,652,425. That's 3.65 million. So we will do about 3.65 million iterations of the innermost loop (day). That's fine.
But we also have to consider that the year loop goes 10000, and inside we have month and day loops; total inner operations = 3.65 million. So overall date enumeration is about 3.65 million, which is trivial.
Time enumeration: 86,400 loops.
Thus total operations ~3.64 million + 86,400 = ~3.73 million. That's very small. So Python will be fast.
Now we just need to implement.
But there is a nuance: The sum of segments may exceed 98; we ignore those. But in date enumeration, many totals may be >98. However, we still need to loop through them. That's fine.
Now we need to be careful with the output as potentially large integer. Print as integer.
Now test with examples:
Example 1: Input:
28
0
Our code: no broken; compute freq arrays.
Let's simulate quickly to ensure we get dateFreq[16]=1, timeFreq[12]=1. But is there any other date with sum 16? For date to sum to 16, all 8 digits must be 1. Is it possible to have year=1111, month=11, day=11 only. Could there be year=1111, month=11, day=11? Yes. What about year=1111, month=11, day=11 is only. But month 11 and day 11 are fixed. Also year must be 1111. That's one. Could there be year=1111, month=11, day=11? Yes. So dateFreq[16] should be 1. timeFreq[12] should be 1 (only 11:11:11). So answer=1.
But wait, are there timestamps like hour=11, minute=11, second=11? Yes.
Now dateFreq[16] should be 2: for year 1111 (digits 1,1,1,1) and year 1171 (1,1,7,1). Also year 1117? 1,1,1,7 gives yearSum: pos0=1(2), pos1=1(2), pos2=1(2), pos3=7(3) -> yearSum=2+2+2+3=9. monthSum=4, daySum=4 => total=17. Not 16. So only those two. Also year 1171? Already. Also year 111? No. Also year 1711? digits: 1,7,1,1: pos0=1(2), pos1=7(3) because position 1 not broken, so yearSum=2+3+? Actually need compute: pos0=1:2, pos1=7:3 (since no broken at pos1), pos2=1:2 (broken? pos2 broken only segment 0, digit 1 unaffected =>2), pos3=1:2 => total year=9, so date sum 17. So not. Also year 1711? No. So dateFreq[16] = 2. timeFreq[12] still 1. So answer=2.
Now example 3: N=98, broken at (3,0). date and time frequencies maybe none sum to 98. Output 0.
Now example 4: N=60, broken at (3,0). We'll trust.
Now potential pitfalls: The digit ordering for month and day: Are month digits positions 4 and 5? Yes: month ten and month one. Day digits positions 6 and 7. hour: 8,9; minute: 10,11; second:12,13. We'll double-check with problem statement: "digit p_i (0 ≤ p_i ≤ 13) of the i-th broken bar are given". And they show a figure with 14 digits: year (4 digits), month (2), day (2), hour (2), minute (2), second (2). So mapping is as we assume.
Now confirm segment mapping: "position of the bar is the ID of the bar in each number as shown in the following figure, and is the number assigned the numbers from 0 to 6 in order from the upper bar." So typical labeling: 0: top (a), 1: upper right (b), 2: lower right (c), 3: bottom (d), 4: lower left (e), 5: upper left (f), 6: middle (g). That is common. We'll adopt that.
Now we must ensure that the patterns we define match the typical 7-seg display. It's standard.
Now one nuance: The problem says "The shining parts of each number are as shown in the following figure." So we should rely on figure, but since we don't have figure, we assume typical. However, sometimes there might be different segment assignments (e.g., some use different order). But the problem statement mentions "upper bar" first, so likely 0 is top. And then "position q_i (0 ≤ q_i ≤ 6)" corresponds to segments. So it's standard.
Now code steps:
Read all input lines.
Parse N, K.
Initialize broken_sets = [set() for _ in range(14)].
For i in range(K): read p, q; broken_sets[p].add(q).
Alternatively, we could define as bitmask for speed, but set difference fine.
Compute segCount: 14x10 list of ints.
for p in range(14):
broken = broken_sets[p]
segCount[p] = [len(patterns[d] - broken) for d in range(10)]
This uses set difference. Since pattern sets are small, it's fine.
Compute dateFreq array of size 99 (index 0..98). Use zeros.
For Y in range(0, 10000):
y3 = Y // 1000
y2 = (Y // 100) % 10
y1 = (Y // 10) % 10
y0 = Y % 10
yearSum = segCount[0][y3] + segCount[1][y2] + segCount[2][y1] + segCount[3][y0]
# leap
leap = (Y % 400 == 0) or (Y % 4 == 0 and Y % 100 != 0)
for M in range(1, 13):
m1 = M // 10
m0 = M % 10
monthSum = segCount[4][m1] + segCount[5][m0]
if M in (4,6,9,11):
maxDay = 30
elif M == 2:
maxDay = 29 if leap else 28
else:
maxDay = 31
for D in range(1, maxDay+1):
d1 = D // 10
d0 = D % 10
daySum = segCount[6][d1] + segCount[7][d0]
total = yearSum + monthSum + daySum
if total <= 98:
dateFreq[total] += 1
This loops over all dates. However, note that for each year we compute yearSum and then inside month/day loops, we add monthSum and daySum. That's fine.
But we should consider that monthSum and daySum are computed each time. Could precompute for each month and day the sum for the two digits? Since there are only 12 months and up to 31 days, we can precompute monthSum[M] for M=1..12, and daySum[D] for D=1..31. That would speed up a little. But not necessary.
However, we need to be careful: monthSum depends on position 4 and 5 (fixed). So we can precompute an array month_sum[M] = segCount[4][M//10] + segCount[5][M%10] for M from 1 to 12. Similarly day_sum[D] = segCount[6][D//10] + segCount[7][D%10] for D from 1 to 31. That would avoid repeated digit extraction and addition inside the innermost loop. That's simple and beneficial. Similarly, we could precompute year_sum[Y] for each year? That would be 10000 entries, but we can compute on the fly. But we can also precompute year_sum array of size 10000 to avoid recomputing digits each time? That might be fine but not necessary; computing 10000 * 4 additions is cheap.
But note: yearSum is used for each month/day combination of that year (~ up to 366 times). So computing yearSum once per year is fine; we already do that.
Precompute month_sum and day_sum:
month_sum = [0]*13 (index 1..12) (ignore 0)
for M in range(1,13):
month_sum[M] = segCount[4][M//10] + segCount[5][M%10]
Now the answer.
Now check for correctness with sample inputs.
But there is a potential pitfall: The total number of glowing bars includes only the bars that are supposed to be on and are not broken. But what about segments that are not supposed to be on? They are off. So only those that are on in the digit pattern count, provided they are not broken. Yes.
Now, one more nuance: The problem says: "Note that even if the display looks the same depending on the non-illuminating stick, if the year, month, day, hour, minute, and second you tried to display are different, they are counted separately." So we count each distinct timestamp, not each distinct display pattern. That's exactly what we are doing.
Now, we need to make sure that the month and day constraints are correctly applied. For February 29, it's only valid in leap years. Our code includes that.
Now, what about month 0? Not valid. So month is 1..12. Our loop covers.
Day must be >=1. Good.
Now, what about year 0000? Yes.
Now, the range of years: 0-9999 inclusive. That's 10000 years.
Now, check leap year calculation for year 0: modulo operations: 0%400=0 => leap. 0%4=0, 0%100=0 but condition Y%100 !=0 fails, so it's leap by first rule anyway. So we treat it as leap. That's consistent with rule 1.
Now, does the calendar allow February 29 in year 0? In proleptic Gregorian calendar, year 0 is a leap year. So it's valid.
Now, we might need to consider that months and days are always two digits with leading zeros. For example, month 2 is displayed as "02". Our digit extraction for month: m1 = M//10, m0 = M%10. For M=2, m1=0, m0=2. That's correct. Similarly for day.
Now, ensure that day_sum precomputation for D from 1 to 31 is fine. For day 31, digits 3 and 1. Good.
Now, we must be careful: The day_sum precomputation uses segCount[6] and [7] which are fixed positions. But what about day numbers like 01..09? Those are covered: D=1 => d1=0, d0=1.
Now, potential issue: The date sum may be less than or equal to 98; we only count those. But what if N is high, like 98, and date sums that are high, we need to count them. So we need to ensure we capture all sums up to 98, but not those above. So our if total <= 98 is fine; we ignore totals >98. Since N ≤ 98, sums >98 can't combine with any time sum (which is non-negative) to equal N because time sum ≥ 0, so they'd give total > N if date sum > N? Actually if date sum > N, even with time sum = 0 (minimum possible), total > N, so they can't contribute. So we can ignore them.
But what about date sums >98? Actually maximum date sum could be up to maybe 87=56 if no broken? Actually 8 digits each max 7 => 56. But if broken segments reduce max? Actually broken segments reduce max, so max date sum can't exceed 56. So 56 ≤ 98. So date sum will never exceed 98? Wait, 8 digits each max 7 = 56, yes. So date sum is at most 56. Similarly, time sum at most 67=42. So total max 98. So sums are within 0..98 anyway. But wait, if some segments are broken, the maximum per digit might be less than 7, so maximum total might be less than 98. But still, 56 and 42 are well below 98. So date sum is at most 56, time sum at most 42. So total always ≤ 98. So we don't need to check total <=98; it's always true. But we must consider that broken segments might reduce the count, not increase, so maxima are at most those numbers. So we can skip the condition. But we will keep it for safety, or remove to simplify.
Actually careful: With broken segments, maximum per digit might be less than 7, so overall max is less. But still ≤ 56/42. So total ≤ 98 always. So we can skip the if.
But maybe there is scenario where broken segments increase the number? No, broken only reduces. So safe.
Thus we can just increment without condition.
But we'll keep condition for clarity.
Now, Implementation details: Use Python's integers for counting. Use list of ints for freq.
Now, test with example 4 manually? Not possible, but we can trust.
Now, check if there are any hidden pitfalls: The digit indices for year: 0: most significant, 1: next, 2: next, 3: least. That's correct. For month: positions 4 and 5. For day: 6 and 7. For hour: 8 and 9. For minute: 10 and 11. For second: 12 and 13.
Now, confirm that month and day have constraints only within themselves and year; no other constraints with time. So our separate frequencies and product is correct.
But is there any dependency between date and time? No, all combinations are valid. So the total number of timestamps is product of number of dates and number of times, but that's only if we count all combos; but for specific N, we need convolution. So we can indeed separate.
Now we must ensure that dateFreq and timeFreq cover all sums.
Now, check potential overflow: The counts can be large (up to 3.65 million dates, 86,400 times). The product may be up to ~3.65e6 * 86400 = 315 billion, which fits within Python int.
Now, let's implement.
Pseudo-code:
def main():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
N = int(next(it))
K = int(next(it))
broken = [set() for _ in range(14)]
for _ in range(K):
p = int(next(it)); q = int(next(it))
broken[p].add(q)
code
# patterns
patterns = [
{0,1,2,3,4,5},
{1,2},
{0,1,3,4,6},
{0,1,2,3,6},
{1,2,5,6},
{0,2,3,5,6},
{0,2,3,4,5,6},
{0,1,2},
{0,1,2,3,4,5,6},
{0,1,2,3,5,6}
]
segCount = [[0]*10 for _ in range(14)]
for p in range(14):
br = broken[p]
for d in range(10):
segCount[p][d] = len(patterns[d] - br)
# Precompute month sums and day sums
month_sum = [0]*13 # index 1..12
for m in range(1,13):
month_sum[m] = segCount[4][m//10] + segCount[5][m%10]
day_sum = [0]*32 # index 1..31
for d in range(1,32):
day_sum[d] = segCount[6][d//10] + segCount[7][d%10]
dateFreq = [0]*99 # size 99 for sums 0..98
# Iterate years
for Y in range(0, 10000):
y3 = Y // 1000
y2 = (Y // 100) % 10
y1 = (Y // 10) % 10
y0 = Y % 10
yearSum = segCount[0][y3] + segCount[1][y2] + segCount[2][y1] + segCount[3][y0]
leap = (Y % 400 == 0) or (Y % 4 == 0 and Y % 100 != 0)
for m in range(1,13):
ms = month_sum[m]
if m in (4,6,9,11):
maxDay = 30
elif m == 2:
maxDay = 29 if leap else 28
else:
maxDay = 31
for d in range(1, maxDay+1):
total = yearSum + ms + day_sum[d]
# total <= 98 always, but we check
if total <= 98:
dateFreq[total] += 1
# else ignore, though not needed
# Precompute time sums
hour_sum = [0]*24
for h in range(24):
hour_sum[h] = segCount[8][h//10] + segCount[9][h%10]
minute_sum = [0]*60
for m in range(60):
minute_sum[m] = segCount[10][m//10] + segCount[11][m%10]
second_sum = [0]*60
for s in range(60):
second_sum[s] = segCount[12][s//10] + segCount[13][s%10]
timeFreq = [0]*99
for h in range(24):
hs = hour_sum[h]
for m in range(60):
ms = minute_sum[m]
for s in range(60):
total = hs + ms + second_sum[s]
if total <= 98:
timeFreq[total] += 1
# Compute answer
ans = 0
for d in range(0, 99):
t = N - d
if 0 <= t < 99:
ans += dateFreq[d] * timeFreq[t]
print(ans)
if name == 'main':
main()
Now, test with given examples.
We need to ensure we handle K=0 correctly; broken sets empty.
Now, let's manually test with example 1: N=28, K=0.
We'll run mental simulation.
Compute segCount: For all positions, segCount[p][d] = len(pattern[d]) because no broken. So pattern lengths: [6,2,5,5,4,5,6,3,7,6].
Date enumeration: For year 1111: yearSum = 2+2+2+2 =8. month 11: month_sum[11] = segCount[4][1] + segCount[5][1] = 2+2 =4. day 11: day_sum[11] = segCount[6][1] + segCount[7][1] = 2+2=4. total = 8+4+4=16. So dateFreq[16] gets at least 1. For other dates, totals will be different.
Time: hour 11: hour_sum[11] = segCount[8][1]+segCount[9][1] = 2+2=4? Actually wait hour is two digits: for hour 11, digits: '1','1'. So sum = 2+2=4? But we need sum for hour part? Actually hour part contributes two digits, but we have hour_sum for hour only? hour_sum defined as sum of segCount[8] and segCount[9] for that hour. So hour_sum[11] = 2+2=4. Similarly minute_sum[11]=4, second_sum[11]=4. total time = 4+4+4=12. So timeFreq[12] gets 1. So ans = dateFreq[16]timeFreq[12] = 11 =1. Good.
Now example 2: broken (2,0). Compute segCount[2][1] = 2 (since pattern[1] doesn't include segment0), segCount[2][7] = len({0,1,2} - {0}) = 2. So year 1111: yearSum = 2+2+2+2 =8. year 1171: year digits: 1,1,7,1: pos0=1 ->2, pos1=1 ->2, pos2=7 ->2, pos3=1 ->2 => yearSum=8. So dateFreq[16] gets 2. timeFreq[12] still 1 => ans=2.
Now example 3: N=98, broken (3,0). Max possible total? date max 56, time max 42, sum max 98. But can we achieve 98? Need each digit to be 8 (7 segments) but at position 3, digit 8 would have segment0 broken, so segCount[3][8] = 6. So the maximum total may be less than 98. So answer 0.
Now example 4: large number. We'll trust.
Now, I must double-check that the date enumeration includes all dates from year 0 to 9999 inclusive. Year 0 is considered? Yes, we range(0,10000). That includes 0.
Now, confirm month/day ranges: For month 1..12 inclusive. For day, we loop from 1 to maxDay inclusive. That's correct.
Now, think about any possible missing timestamps: The timestamp includes year, month, day, hour, minute, second. All combos valid. There is no notion of timezone or DST.
Now, consider the possibility of month having 0? No.
Now, confirm leap year rule for year 0: According to rules, year 0 divisible by 400 => leap. So February has 29 days. That is okay.
Now, check if any date like February 29, year 0 is valid. Yes.
Now, everything seems fine.
But wait: The problem says "The month starts on the 1st, April, 6, 9, November until the 30th, and 1, 3, 5, 7, 8, 10, December until the 31st." This phrasing might indicate that months are 1-indexed? Yes.
Now, check that month 4 (April) has 30 days, etc.
Now, also note: The digital clock displays the year in 0-sized 4 digits (0000 to 9999). So leading zeros for years less than 1000: e.g., year 5 displayed as 0005. That is handled by our digit extraction: y3 = Y//1000 etc. For Y=5, y3=0, y2=0, y1=0, y0=5. That's correct.
Now, think about performance: The date loop 3.65 million, each iteration doing a few integer operations and array increment. In Python, 3.65 million is fine (<0.1 sec). But careful: Actually the innermost loop runs about 3.65 million times. That's fine.
But we also have the outer loop over years (10000) and inner loops over months (12) and days (variable). The total inner loop iterations are indeed sum over years of days. That's 3.65 million. So overall date enumeration should be fast.
But we also have to compute yearSum for each year (10000 times). That's fine.
Now, confirm that the broken segments may affect the same digit multiple times? Yes, we use set, so duplicates are ignored.
Now, confirm that K can be 0; then broken sets are empty.
Now, check for possible off-by-one: The positions q_i from 0 to 6. Good.
Now, consider if there is any possibility that some digit values are not displayed because the digit is not used (like hour tens digit can only be 0-2). But we still precompute segCount for all digits 0-9. But the actual digit values that appear in each position are constrained by the range of the field. For example, hour tens digit can be 0,1,2. For hour 0-9, tens digit is 0. For hour 10-19, tens digit is 1. For hour 20-23, tens digit is 2. So it's limited. However, we are enumerating all valid values, so we are only summing for those digits that actually appear. So correct.
But our precomputed month_sum and day_sum use all months/days values. That's fine.
Now, what about year digits? We loop over years 0-9999, so all digits appear.
Now, is there any constraint like month cannot be 00? Not relevant because we only generate valid months.
Now, also ensure that for day, we are using day_sum for D up to 31. But for months with 30 days, we only loop D=1..30, so day_sum[31] not used for those months. That's fine.
Now, test with a small case to ensure correct counting: For example, N=0? Possibly count timestamps where no segments light. That would require that for each digit, the displayed digit's pattern has no segment that is not broken. That could happen if the digit is such that all segments that would normally light are broken. For example, if a digit is 8, and all 7 segments are broken at that position, then segCount[p][8]=0. Could produce 0. But our enumeration would count those if they form valid dates. So fine.
Now, confirm that the problem statement says "the number of glowing bars" is exactly N. So we count those.
Now, think about potential overflow of answer: We'll use Python int, no issue.
Now, consider if the broken segment list includes a segment that is not used by any digit? That doesn't matter.
Now, final check: The input format: first line N, second line K, then K lines each with p_i and q_i. There's a blank line? Not needed. We'll read all tokens.
Now, let's implement.
But before finalizing, let's double-check the segment patterns: I want to be absolutely sure about the mapping of digits to segments. Standard 7-segment representation often uses:
Segment a (top)
Segment b (upper right)
Segment c (lower right)
Segment d (bottom)
Segment e (lower left)
Segment f (upper left)
Segment g (middle)
Usually digits:
0: a,b,c,d,e,f (6)
1: b,c (2)
2: a,b,d,e,g (5) [note: a,b,g,e,d? Actually standard 2: a,b,g,e,d? Wait typical 2: a,b,g,e,d? That's a,b,g,e,d? Let's recall: Digit 2: a, b, g, e, d. That's segments: top, upper right, middle, lower left, bottom. So set {a,b,g,e,d} which corresponds to indices 0,1,6,4,3. Yes.
3: a,b,c,d,g (0,1,2,3,6)
4: f,g,b,c (5,6,1,2) -> {5,6,1,2}
5: a,f,g,c,d (0,5,6,2,3)
6: a,f,g,c,d,e (0,5,6,2,3,4) but sometimes includes g? Actually typical 6: a,f,g,e,d,c? Wait standard 6: a,f,e,d,c,g? That's a, f, e, d, c, g. So segments: 0,5,4,3,2,6. Yes, 6 segments.
7: a,b,c (0,1,2)
8: all 7
9: a,b,c,d,f,g (0,1,2,3,5,6)
So our patterns match. Good.
Now, let's think about the digit index assignment: The problem figure might have a different ordering of segments? But they said "position of the bar is the ID of the bar in each number as shown in the following figure, and is the number assigned the numbers from 0 to 6 in order from the upper bar." That strongly suggests 0 is top, then going around? Usually "upper bar" is top. Then perhaps next is right? But they didn't specify the order after top. Could be: after top, then maybe right vertical? But they say "in order from the upper bar". Usually labeling might be: 0: top, 1: upper right, 2: lower right, 3: bottom, 4: lower left, 5: upper left, 6: middle. That is common. Alternatively, sometimes they label clockwise: top, right, bottom, left? But they have 7 segments, so order: top, upper right, lower right, bottom, lower left, upper left, middle. That is plausible.
Given the sample 2, we saw that breaking segment 0 on digit 2 (year ten) affected digit 7 but not digit 1. That matches our pattern because digit 7 uses segment 0, digit 1 does not. So this confirms that segment 0 is indeed a segment that is used by digit 7. In digit 7 pattern, which segments are on? Typically it's top, upper right, lower right (segments 0,1,2). So yes, segment 0 is top. So our mapping is consistent with the sample. So it's correct.
Now, confirm that digit 2 is year ten. In year 1171, the ten digit is 7. Breaking top segment on that digit reduces its count from 3 to 2. So matches.
Thus our pattern mapping is correct.
Now, I'll proceed to code.
Potential improvements: Use bitmask to compute len quickly. But sets are fine.
Now, test with example 4: We could attempt to compute approximate to ensure no bug. But let's trust.
Now, consider if K lines might have p_i beyond 13? It's given 0 ≤ p_i ≤ 13, so fine.
Now, about input reading: Use sys.stdin.read() to get all.
Now, we need to be careful about newline spaces. We'll split.
Now, let's think about large K: up to 98. That's fine.
Now, one more nuance: The total glowing bars count includes only segments that actually glow. But what about segments that are not broken but the digit pattern does not light them? They are off, so not counted. So our calculation is correct.
Now, what about segments that are broken but the digit pattern would not light them anyway? That doesn't affect count. Our set difference automatically handles.
Now, we might consider that broken segments are permanently off, but could that affect the digit's appearance? The clock still tries to display the digit, but broken segments remain off. That's what we model.
Now, implement.
But before final code, let's think about the month and day loops: We precompute month_sum for each month. However, note that month_sum depends on positions 4 and 5, which are fixed for the month. That's fine.
But what about the possibility that broken segments might be in month or day positions. That is covered because segCount for those positions includes the broken information.
Now, one more nuance: The problem says "Note that even if the display looks the same depending on the non-illuminating stick, if the year, month, day, hour, minute, and second you tried to display are different, they are counted separately." This just reinforces we count timestamps, not display states.
Now, time to code.
Potential memory/performance: dateFreq and timeFreq are arrays of 99 ints, but we might need to consider that sums can be up to 98 inclusive, but date sums max 56, time max 42. So 99 is fine.
Now, check if N can be 0? Yes.
Now, check if K can be 98? That's many broken segments, but still fine.
Now, after code, we'll test with given examples. However, we can't actually run, but we can reason.
Now, also consider that the input example 4 yields 13362470370. We could approximate check if it's plausible. The total number of timestamps is about 315 billion. So if N=60, the number of timestamps with sum 60 is about 13.36 billion, which is about 4.2% of total. That seems plausible.
Now, we need to ensure no double counting: Each timestamp is a combination of date and time, and we multiply frequencies and sum. Since date and time are independent, this yields the correct total count.
Now, one more nuance: Could there be any constraint linking date and time? For example, the clock displays all fields continuously, no constraint. So independent.
Now, we might consider optimizing further by using convolution, but not needed.
Now, implement final code.
But wait: In the date enumeration, we compute yearSum for each year. That involves four digit lookups. For each year, we might want to precompute year_sum[Y] to avoid recomputation? Not needed.
But we could also precompute year_sum array for all years (size 10000). That might be slightly faster, but 10000 is small.
Let's possibly precompute year_sum to avoid recalculating digits each time? Not necessary.
Actually, we compute yearSum inside the year loop, and then reuse for all months/days of that year. That's fine.
Now, we also compute month_sum and day_sum precomputed.
Now, let's check day_sum: For day values from 1 to 31, we compute sum of segCount[6][d//10] and segCount[7][d%10]. For days where day is 1-9, d//10=0, d%10=d. That's correct. For day 08? Not, day numbers are 1-31, so no leading zero in the integer representation, but we need to treat it as two digits: tens digit may be 0 for 1-9. That's fine.
Now, similarly for month: month 1-9, tens digit 0.
Now, time: hour_sum for hour 0-9: h//10=0, h%10=h. So good.
Now, ensure we treat hour values 0-23 correctly. Hour 0 is midnight, displayed as 00. That's fine.
Now, final answer.
Now, before finalizing, let's do a quick test with a minimal scenario to ensure the loops count correctly. Suppose we have only year 0-0, month 01, day 01, hour 00, minute 00, second 00. But that's one timestamp. We can compute manually.
Now, we need to check if the code includes year 0, month 1, day 1, etc. Yes.
Now, I'll code.
But there is a potential pitfall: The input might have trailing spaces. We'll handle with split.
Now, note: In the date enumeration, we used month_sum[m] and day_sum[d]. We must ensure that day_sum is defined for d up to 31, and we only access up to maxDay. That's fine.
Now, we need to ensure that month_sum array size 13 (indices 0..12). We'll ignore index 0.
Now, one more nuance: The problem states "Digital clocks display the year in 0-sized 4 digits (0000 to 9999) and month, day, hour, minute, and second in 0-sized 2 digits." So it's leading zeros. Our digit extraction for month and day: For month, we compute m//10 and m%10. For month=1, we get 0 and 1. That's correct. For day=1, 0 and 1. For hour=0, 0 and 0. So good.
Now, about year: For year=0, digits: 0,0,0,0.
Now, all good.
Now, let's implement the code.
But I recall that sometimes in such problems, the year range is 0..9999 inclusive, but the Gregorian calendar might not be defined for year 0. However, the problem likely expects the given leap year rules to be applied to year 0 as well, because they said "Christian era" and the rules include divisibility. Many programming contest problems treat year 0 as a leap year when applying these rules. For example, in UVA 893, they treat year 0 as leap. So it's fine.
Now, final code.
One more optimization: For date enumeration, we can precompute for each year the leap flag and yearSum, then inner loops. Already.
Now, also note that for each year, we could precompute the list of (month, day) pairs? But we already loop.
Now, implement.
Now, after implementing, I'll run through mental test with example 1 and 2 to ensure correct counts.
But also need to ensure we handle case where broken segments cause segCount for some digit to be zero for all digits? That could happen if all segments for a digit position are broken. Then segCount[p][d]=0 for all d. That would make the digit always contribute 0. That's fine.
Now, answer may be large, but Python prints fine.
Now, let's code.
But wait: The input example 4 has output 13362470370. If we compute our answer, we must match exactly.
Now, double-check that the date enumeration includes all years 0-9999. In some calendar systems, year 0 is not used, but the problem explicitly says 0000 to 9999. So include year 0.
Now, potential off-by-one: February in leap year has 29 days. So we set maxDay=29 for leap. That's correct.
Now, check that month 2 for leap year includes day 29. Our loop for D in range(1, maxDay+1) will include 29.
Now, check that month 4,6,9,11 have 30 days; our tuple includes these numbers.
Now, check that month 1,3,5,7,8,10,12 have 31 days.
Now, note: Month numbers: 4,6,9,11 are correct.
Now, implement.
Now, one more thing: The digit index mapping: year digits: 0: thousands, 1: hundreds, 2: tens, 3: ones. But is that order from leftmost to rightmost? The problem says "the digit is the ID assigned to each number of the digital clock as shown in the following figure, and is the number assigned from 0 to 13 in order from the highest number." Highest number likely means leftmost digit. So yes, 0 is leftmost of year (most significant). So our mapping is correct.
Now, write code.
Now, after writing code, we need to test on sample inputs.
We'll do a mental check for sample 1 and 2.
But also verify that for sample 2, dateFreq[16] gets exactly 2. Let's think if there could be date 1111-11-11 and 1171-11-11. Are there any other years where the year digits sum to 8 given the broken? Let's compute possible year combinations with yearSum=8 given broken at pos2. YearSum = segCount[0][y3] + segCount[1][y2] + segCount[2][y1] + segCount[3][y0] = we need 8. Without broken, minimum is 8 when all digits 1. With broken, pos2 can be 1 or 7, each gives 2. For other positions, only digit 1 gives 2 because no broken there. So y3 must be 1 (2), y1? Actually careful: positions: 0: thousands, 1: hundreds, 2: tens, 3: ones. Only position 2 has alternative digit 7 that also gives 2. Others: must be 1 to get 2 each. So possible year values: y3=1, y1? Wait y2 is position 1? Let's assign: pos0: y3, pos1: y2, pos2: y1, pos3: y0? Actually earlier we set y3 = Y//1000 (thousands), y2 = (Y//100)%10 (hundreds), y1 = (Y//10)%10 (tens), y0 = Y%10 (ones). So pos0: y3, pos1: y2, pos2: y1, pos3: y0. So pos2 is y1 (tens). In example, broken at digit 2 which is the tens digit of year (position 2). So that digit can be 1 or 7 to give 2. So y1 (tens) can be 1 or 7. The other digits: y3 (thousands), y2 (hundreds), y0 (ones) must be 1 to give 2. So possible year numbers: y3=1, y2=1, y1=1 or 7, y0=1. That gives 1111 and 1171. Also y1 could be something else if it also gives 2? Is there any other digit that gives 2 at position 2? Only digit 1 gives 2 normally, but digit 7 gives 2 only because broken segment removed one; also maybe digit ? Could digit? If broken removes some segments, could digit 4 give 2? No, pattern[4] = {1,2,5,6} length 4, broken at 0 doesn't affect, so still 4. digit 0 pattern length 6, broken reduces to 5. So only 1 and 7 give 2. So exactly two years. Good.
Now, check that month 11 and day 11 are valid for both years: yes.
Now, for time, hour 11, minute 11, second 11 yields sum 12. Could there be any other time with sum 12? Without broken, each digit 1 gives 2, so six digits => 12. So only all ones give 12? Could there be combination where some digits are 7? Digit 7 gives 3, so would increase sum. Could there be combination where some digits are 1 and some are something else that sum to 12? Since minimum per digit is 2, total min 12. To keep sum 12, all digits must be at minimum 2. Only digit 1 gives exactly 2. So only all ones. So timeFreq[12]=1. So answer 2.
Thus our enumeration will correctly find those.
Now, we must ensure that the date enumeration includes year 1171, month 11, day 11. Our loops: for Y=1171, we compute yearSum, then month loop, for m=11, ms = month_sum[11], then day loop for d=11, day_sum[11]. So yes.
Now, what about year 1171 and month 11, day 11 is valid? Month 11 is November, has 30 days, so day 11 is within. So yes.
Now, sample 4: We'll trust.
Now, potential issues: Use of set for broken; computing len(pattern[d] - broken) for each digit. Could there be broken segments that are not in pattern[d]? That's fine; set difference removes only those present.
Now, think about performance of computing segCount: for each p (14), for each d (10), we compute len(pattern[d] - broken[p]). That's 140 set differences, trivial.
Now, everything seems fine.
Now, let's write final code.
But before finalizing, consider if the problem expects answer for N=0? Possibly test.
Now, note that the maximum total segments with broken can be less than 98. So if N > max possible, answer 0. Our freq arrays would have zero for those sums, so answer 0.
Now, possible integer overflow in Python? No.
Now, let's also consider using arrays from array module? Not needed.
Now, I'll write code.
One more nuance: The input might have multiple spaces or lines; using split handles.
Now, final answer.
But wait: The problem says "Answer how many years, months, days, minutes, and seconds the number of glowing bars is exactly N." Actually it says "Answer how many years, months, days, minutes, and seconds the number of glowing bars is exactly N." That's a bit odd, but we understand we count timestamps.
Now, I'll produce final code.
Now, after code, I'll consider testing with a small custom case:
Case: N=0, K=0. With no broken, minimal total is 28, so no timestamp with 0. So answer 0. Our dateFreq[16] and timeFreq[12] produce 28 at minimum, so dateFreq[0] and timeFreq[0] are 0, answer 0.
Case: If we break all segments (K huge) such that all segments are broken, then segCount for all digits all 0. Then total always 0. Then number of timestamps = total dates * total times = 3,652,425 * 86,400 = 315,569,520,000? Actually compute: 3,652,425 * 86,400 = as earlier, about 315.5 billion. So answer for N=0 would be that huge number. Our code would compute segCount all zeros, dateFreq[0] = number of dates, timeFreq[0] = number of times, answer = product. So correct.
Now, confirm dateFreq[0] counting: yearSum = 0+0+0+0 =0. month_sum[M] = 0, day_sum[D] = 0. So each date total =0. So dateFreq[0] = total dates. Similarly timeFreq[0] = 86400. So product huge. That's okay.
Now, confirm we use correct multiplication for answer: dateFreq[d] * timeFreq[N-d] sum.
Now, we might need to use 64-bit but Python auto.
Now, I'll code.
Potential micro-optimizations: Use list comprehension for segCount.
Now, final code:
code
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
N = int(next(it))
K = int(next(it))
broken = [set() for _ in range(14)]
for _ in range(K):
p = int(next(it))
q = int(next(it))
broken[p].add(q)
# digit patterns: 0-9
patterns = [
{0,1,2,3,4,5}, # 0
{1,2}, # 1
{0,1,3,4,6}, # 2
{0,1,2,3,6}, # 3
{1,2,5,6}, # 4
{0,2,3,5,6}, # 5
{0,2,3,4,5,6}, # 6
{0,1,2}, # 7
{0,1,2,3,4,5,6}, # 8
{0,1,2,3,5,6} # 9
]
segCount = [[0]*10 for _ in range(14)]
for p in range(14):
br = broken[p]
for d in range(10):
segCount[p][d] = len(patterns[d] - br)
# Precompute month and day sums
month_sum = [0]*13 # 1..12
for m in range(1,13):
month_sum[m] = segCount[4][m//10] + segCount[5][m%10]
day_sum = [0]*32 # 1..31
for d in range(1,32):
day_sum[d] = segCount[6][d//10] + segCount[7][d%10]
dateFreq = [0]*99 # sum up to 98
# Iterate over years
for Y in range(0, 10000):
# year digits
y3 = Y // 1000
y2 = (Y // 100) % 10
y1 = (Y // 10) % 10
y0 = Y % 10
yearSum = segCount[0][y3] + segCount[1][y2] + segCount[2][y1] + segCount[3][y0]
leap = (Y % 400 == 0) or (Y % 4 == 0 and Y % 100 != 0)
for m in range(1,13):
ms = month_sum[m]
if m in (4,6,9,11):
maxDay = 30
elif m == 2:
maxDay = 29 if leap else 28
else:
maxDay = 31
for d in range(1, maxDay+1):
total = yearSum + ms + day_sum[d]
# total is at most 56, so within 0..98
dateFreq[total] += 1
# Precompute time sums
hour_sum = [0]*24
for h in range(24):
hour_sum[h] = segCount[8][h//10] + segCount[9][h%10]
minute_sum = [0]*60
for m in range(60):
minute_sum[m] = segCount[10][m//10] + segCount[11][m%10]
second_sum = [0]*60
for s in range(60):
second_sum[s] = segCount[12][s//10] + segCount[13][s%10]
timeFreq = [0]*99
for h in range(24):
hs = hour_sum[h]
for m in range(60):
ms = minute_sum[m]
for s in range(60):
total = hs + ms + second_sum[s]
timeFreq[total] += 1
ans = 0
for d in range(0, 99):
t = N - d
if 0 <= t < 99:
ans += dateFreq[d] * timeFreq[t]
print(ans)
if __name__ == "__main__":
main()
Now, check if any variable name conflict: minute_sum and month_sum? We used ms for both but separate scopes, fine.
Now, we need to confirm that dateFreq and timeFreq arrays are correctly sized to 99. Since sum can be up to 98 inclusive. But we might also get sum 0? Yes.
Now, test with sample 1:
Compute segCount: all normal. dateFreq[16]? Let's compute manually: For year=1111, month=11, day=11, total=16. Any other date with total 16? Probably none. So dateFreq[16]=1. timeFreq[12]=1. ans=1.
Now, sample 3: with broken (3,0), we might have max total less than 98, but maybe some combinations could still sum to 98? For total 98, each digit must be 8 at all positions? Actually if any digit is not 8, the sum is less. But with broken at pos3, digit 8 at that position gives 6 not 7, so max total is 97? Actually check: date max: year: digits all 8? But at pos3, digit 8 gives 6, so year max = 7+7+7+6=27? Actually pos0,1,2 each can be 7 (if 8) gives 7, pos3 gives 6 => total 27. Month: two digits 8 => 7+7=14, but month digits can be 8? Month can be 08,18? Actually month range 01-12, so max month digits: month can be 08? Month 08 (August) is 31 days, so valid. Also month 12 has digits 1 and 2, not both 8. For maximizing, month could be 08? But month 08 is August, valid. So month digits: 0 and 8. But digit 0 gives 6, 8 gives 7 => total 13. Or month 88 invalid. So actual month sum maximum depends on valid months. Similarly day: maximum day sum depends on valid days (up to 31). Day could be 38 invalid. So need to find max possible total; but likely not 98. So answer 0. Our code would compute dateFreq[98] = 0, timeFreq[?] etc. So ans=0.
Now, sample 4: We'll trust.
Now, we should verify that we correctly handle leading zeros for month and day: For month 8, we treat as "08": tens digit 0, ones digit 8. That's correct. So month_sum[8] = segCount[4][0] + segCount[5][8]. Good.
Now, also for day 8, "08": tens digit 0, ones digit 8. day_sum[8] = segCount[6][0] + segCount[7][8].
Now, we should ensure that month 08 (August) is valid (month 8). Yes, month 8 is August, which has 31 days, so valid.
Now, everything fine.
Now, we might want to combine the loops into a more compact form, but not needed.
Now, potential bug: The order of digits for month and day: The tens digit is at position 4 and 6 respectively, ones at 5 and 7. That's correct.
Now, double-check: In the problem, digit IDs 0-13: leftmost year digit is 0, then 1,2,3; then month tens? Actually which is month tens? Usually month displayed as two digits, e.g., "11". The left digit of month is tens, right is ones. Since digits are in order from left to right across the entire display, after the 4 year digits, the next digit is the leftmost of month, i.e., month tens. So position 4 = month tens, position 5 = month ones. That matches.
Then day: position 6 = day tens, position 7 = day ones.
Then hour: position 8 = hour tens, position 9 = hour ones.
Then minute: position 10 = minute tens, position 11 = minute ones.
Then second: position 12 = second tens, position 13 = second ones.
Yes.
Now, we should confirm that for hour, the tens digit can be 0,1,2. For hour 0-9, tens digit 0; hour 10-19 tens digit 1; hour 20-23 tens digit 2. So our hour_sum uses the integer hour to compute digits, correct.
Now, all good.
Now, final code as above.
But one more thought: The date enumeration loop for Y from 0 to 9999 inclusive includes year 0. However, some programming languages may treat year 0 as not leap? But we follow the rules. I think it's correct.
Now, I'll produce final answer.
Now, I should also consider that the number of years is 10000, but sometimes the range may be from 1 to 9999? The problem says "year in 0-sized 4 digits (0000 to 9999)". So inclusive of 0000. So we include.
Now, let's run a quick sanity: total dates count. With no broken, we can compute total number of dates: sum over years of days. We can compute programmatically later, but for mental check: For year 0-9999, total days = 365 * 10000 + number of leap years. Number of leap years = floor(10000/4)-floor(10000/100)+floor(10000/400) = 2500 - 100 + 25 = 2425. So total days = 3,650,000 + 2,425 = 3,652,425. Good.
Times: 246060 = 86,400.
Total timestamps = 3,652,425 * 86,400 = 315,569,520,000. This matches the product we computed earlier? 3,652,42586,400 = 3,652,425 * 864 * 100 = (3,652,425864)100. 3,652,425864 = 3,155,695,200? Actually earlier we got 3,155,695,200? Let's compute more carefully:
3,652,425 * 864.
3,652,425 * 800 = 2,921,940,000
3,652,425 * 60 = 219,145,500
3,652,425 * 4 = 14,609,700
Sum = 2,921,940,000 + 219,145,500 = 3,141,085,500; +14,609,700 = 3,155,695,200. That's correct. Then *100 = 315,569,520,000. So total = 3.1556952e11. That's about 315.6 billion.
Now, example 4 output is 13,362,470,370 which is about 4.2% of total. Reasonable.
Now, I'm confident.
Now, final code. ```python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
N = int(next(it))
K = int(next(it))
code
# broken[digit_position] = set of broken segment ids
broken = [set() for _ in range(14)]
for _ in range(K):
p = int(next(it))
q = int(next(it))
broken[p].add(q)
# normal patterns for digits 0..9 (segment ids 0..6)
patterns = [
{0,1,2,3,4,5}, # 0
{1,2}, # 1
{0,1,3,4,6}, # 2
{0,1,2,3,6}, # 3
{1,2,5,6}, # 4
{0,2,3,5,6}, # 5
{0,2,3,4,5,6}, # 6
{0,1,2}, # 7
{0,1,2,3,4,5,6}, # 8
{0,1,2,3,5,6} # 9
]
# segCount[position][digit] = number of actually glowing segments
segCount = [[0]*10 for _ in range(14)]
for p in range(14):
br = broken[p]
for d in range(10):
segCount[p][d] = len(patterns[d] - br)
# pre‑compute sums for month and day (they are fixed length)
month_sum = [0]*13 # index 1..12
for m in range(1,13):
month_sum[m] = segCount[4][m//10] + segCount[5][m%10]
day_sum = [0]*32 # index 1..31
for d in range(1,32):
day_sum[d] = segCount[6][d//10] + segCount[7][d%10]
# frequency of date sums (0..98)
dateFreq = [0]*99
for Y in range(0, 10000):
# year digits (leading zeros)
y3 = Y // 1000
y2 = (Y // 100) % 10
y1 = (Y // 10) % 10
y0 = Y % 10
yearSum = (segCount[0][y3] + segCount[1][y2] +
segCount[2][y1] + segCount[3][y0])
leap = (Y % 400 == 0) or (Y % 4 == 0 and Y % 100 != 0)
for m in range(1,13):
ms = month_sum[m]
if m in (4,6,9,11):
maxDay = 30
elif m == 2:
maxDay = 29 if leap else 28
else:
maxDay = 31
for d in range(1, maxDay+1):
total = yearSum + ms + day_sum[d]
# total is at most 56, well inside 0..98
dateFreq[total] += 1
# pre‑compute sums for hour, minute, second
hour_sum = [0]*24
for h in range(24):
hour_sum[h] = segCount[8][h//10] + segCount[9][h%10]
minute_sum = [0]*60
for m in range(60):
minute_sum[m] = segCount[10][m//10] + segCount[11][m%10]
second_sum = [0]*60
for s in range(60):
second_sum[s] = segCount[12][s//10] + segCount[13][s%10]
# frequency of time sums (0..98)
timeFreq = [0]*99
for h in range(24):
hs = hour_sum[h]
for m in range(60):
ms = minute_sum[m]
for s in range(60):
total = hs + ms + second_sum[s]
timeFreq[total] += 1
# combine
ans = 0
for d in range(99):
t = N - d
if 0 <= t < 99:
ans += dateFreq[d] * timeFreq[t]
print(ans)
day_sum = [0]*32 (index 1..31)
for D in range(1,32):
day_sum[D] = segCount[6][D//10] + segCount[7][D%10]
Then inner loop:
for M in range(1,13):
ms = month_sum[M]
if M in ...:
maxDay = ...
for D in range(1, maxDay+1):
total = yearSum + ms + day_sum[D]
if total <= 98: dateFreq[total] += 1
That's efficient.
Similarly for time: precompute hour_sum[h] for h in 0..23: segCount[8][h//10] + segCount[9][h%10]
minute_sum[m] for m in 0..59: segCount[10][m//10] + segCount[11][m%10]
second_sum[s] for s in 0..59: segCount[12][s//10] + segCount[13][s%10]
Then triple nested loops:
for h in range(24):
hs = hour_sum[h]
for m in range(60):
ms = minute_sum[m]
total_time = hs + ms + second_sum[s] ??? Actually need to include s inside innermost:
But we can also precompute sum for each second, but easier: nested loops with precomputed arrays for hour, minute, second.
For each s, we can get second_sum[s] and add.
This is fine.
But maybe we can even precompute a frequency for two-digit pairs? But fine.
Since 86400 iterations, we can just compute inside loops:
for h in range(24):
hs = segCount[8][h//10] + segCount[9][h%10]
for m in range(60):
ms = segCount[10][m//10] + segCount[11][m%10]
for s in range(60):
ss = segCount[12][s//10] + segCount[13][s%10]
total = hs + ms + ss
if total <= 98: timeFreq[total] += 1
That's 86,400 inner operations, each with a few adds and if. It's fine.
But we can also precompute sums for hour, minute, second to avoid recomputing digit extraction each time. However, digit extraction is cheap. But we can still precompute arrays of size 24, 60, 60.
Let's do that for clarity and speed.
hour_sum = [segCount[8][h//10] + segCount[9][h%10] for h in range(24)]
minute_sum = [segCount[10][m//10] + segCount[11][m%10] for m in range(60)]
second_sum = [segCount[12][s//10] + segCount[13][s%10] for s in range(60)]
Then timeFreq:
for h in range(24):
hs = hour_sum[h]
for m in range(60):
ms = minute_sum[m]
for s in range(60):
total = hs + ms + second_sum[s]
if total <= 98: timeFreq[total] += 1