A wise king declared a new calendar. "Tomorrow shall be the first day of the calendar, that is, the day 1 of the month 1 of the year 1. Each year consists of 10 months, from month 1 through month 10, and starts from a big month. A common year shall start with a big month, followed by small months and big months one after another. Therefore the first month is a big month, the second month is a small month, the third a big month, ..., and the 10th and last month a small one. A big month consists of 20 days and a small month consists of 19 days. However years which are multiples of three, that are year 3, year 6, year 9, and so on, shall consist of 10 big months and no small month."
Many years have passed since the calendar started to be used. For celebration of the millennium day (the year 1000, month 1, day 1), a royal lottery is going to be organized to send gifts to those who have lived as many days as the number chosen by the lottery. Write a program that helps people calculate the number of days since their birthdate to the millennium day.
Input
The input is formatted as follows.
n
Y1 M1 D1
Y2 M2 D2
...
Yn Mn Dn
Here, the first line gives the number of datasets as a positive integer n, which is less than or equal to 100. It is followed by n datasets. Each dataset is formatted in a line and gives three positive integers, Yi (< 1000), Mi (≤ 10), and Di (≤ 20), that correspond to the year, month and day, respectively, of a person's birthdate in the king's calendar. These three numbers are separated by a space.
Output
For the birthdate specified in each dataset, print in a line the number of days from the birthdate, inclusive, to the millennium day, exclusive. Output lines should not contain any character other than this number.
We need to write a program that, given a birthdate (Y, M, D) in the described calendar, compute the number of days from the birthdate (inclusive) to the millennium day: year 1000, month 1, day 1 (exclusive). So we need the count of days from birthdate to 1000-1-1, i.e., if we count days starting from birthdate as day 1, then the number of days until but not including the millennium day. That is equivalent to the difference in days between the two dates: days from epoch (day 1 = start of calendar) to millennium day minus days from epoch to birthdate. Since the millennium day is year 1000, month 1, day 1, its day count (from epoch) is some number, call it T. The birthdate's day count from epoch is B. Then the number of days from birthdate inclusive to millennium day exclusive is T - B. Because inclusive means birthdate is day 1, then day B+1 is one day after, etc. For example, if birthdate = millennium day itself, then T - B = 0 (since birthdate is day T, but we want days from birthdate inclusive to millennium day exclusive, which is 0). That matches: if you are born on millennium day, you have lived 0 days until that day exclusive, i.e., you haven't lived any full day before it. So formula: answer = days from epoch (year 1 month 1 day 1) to year 1000 month 1 day 1 - days from epoch to (Y,M,D). Let's define days from epoch to a date as the number of days before that date? Actually careful: If epoch is day 1, then date (1,1,1) is day 1. Then the number of days from date A inclusive to date B exclusive = (day count of B) - (day count of A). So we need to compute day counts.
Thus we need to compute for a given date (Y,M,D), how many days have elapsed from the start of calendar (1-1-1) to that date (inclusive? or exclusive?). Let's define: The count of days from epoch to a date inclusive = number of days from day 1 up to and including that date. Or we can define ordinal: day number where day 1 corresponds to (1,1,1). Then (1,1,2) is day 2, etc. That is easier. So we need to compute day number for each date. Then answer = day_number(1000,1,1) - day_number(Y,M,D). That yields the sample output? Let's test with sample.
Sample first: birthdate (1,1,1). Then answer should be days from (1,1,1) inclusive to (1000,1,1) exclusive. That is total days from (1,1,1) to (1000,1,1) minus 1? Let's check: If we compute day_number(1,1,1)=1. Then day_number(1000,1,1) =? The number of days from start up to day before 1000-1-1 would be day_number(1000,1,1)-1. Actually day_number(1000,1,1) is the count that includes that day. Then the difference day_number(1000,1,1) - day_number(1,1,1) = days from 1-1-1 inclusive to 1000-1-1 inclusive? Let's see: If A=1, B=5, then B-A = 4, but if A is day 1 and B is day 5, inclusive difference from A to B inclusive would be 5 days? Actually from day 1 to day 5 inclusive is 5 days. But exclusive of B? The problem says "from the birthdate, inclusive, to the millennium day, exclusive." So we want days from birthdate (including that day) up to but not including millennium day. So if birthdate = millennium day, answer=0. So if birthdate = (1,1,1) and millennium day = (1000,1,1), then answer = number of days from day 1 inclusive to day before day 1000-1-1. That's day_number(1000,1,1) - day_number(1,1,1). Because day_number(1000,1,1) is the number of days from epoch up to and including millennium day. So if we subtract day_number(1,1,1), we get days from day 1 to millennium day inclusive? That would be day_number(1000,1,1)-1+1? Let's compute with simple numbers: Suppose epoch day 1 is Jan 1, target is Jan 5. Then day_number(Jan1)=1, day_number(Jan5)=5. The number of days from Jan1 inclusive to Jan5 exclusive is days: Jan1,2,3,4 = 4 days. That's 5-1=4. Good. So indeed answer = day_number(1000,1,1) - day_number(Y,M,D). Because millennium day is exclusive, we subtract the day number of birthdate inclusive, and we do not count the millennium day.
Check if birthdate = (1000,1,1): day_number(1000,1,1)-day_number(1000,1,1)=0. Works.
So we need to compute day number for any date up to year 999 (since year < 1000 input). But we also need day number for year 1000 month 1 day 1. So we compute total days from epoch to (1000,1,1). We can precompute days per year for years 1 through 999, and also compute for year 1000 (but only need day 1 of month 1). Actually year 1000 is also a multiple of 3? 1000 mod 3 = 1000 % 3 = 1000 - 999 = 1, not multiple of 3. So year 1000 is a common year (alternating big/small). But we only need the start of year 1000. So we can compute total days for years 1 to 999, then add 1 for day 1 of month 1 of year 1000? Actually day number for (1000,1,1) = total days in years 1..999 + 1? Because at the beginning of year 1000, the day number of the first day of year 1000 is after all days of years 1..999. But careful: Epoch: (1,1,1) is day 1. So after completing year 1, the next day is start of year 2 month 1 day 1. So if we sum total days in years 1..999, that gives the number of days from the start of year 1 to the end of year 999. But we need day number for (1000,1,1). That day is the first day after the end of year 999. So day_number(1000,1,1) = (total days in years 1..999) + 1? Actually day 1 is (1,1,1). After completing year 1 (which has some total days), the next day is (2,1,1). The day number for (2,1,1) = total_days_year1 + 1. Because after day 1, we've lived total_days_year1 days? Let's illustrate: Suppose year 1 has N days. Then day numbers:
(1,1,1) = 1
(1,1,2) = 2
...
(1,1,N_day1) =? Actually month structure.
But easier: The count of days before a given date: number of days from epoch (exclusive of that date) to the start. But using ordinal: day number = 1 + number of days that have passed before that date. The number of days before date (Y,M,D) is: total days in years 1..Y-1 + total days in months before M in year Y + (D-1). Then day number = 1 + that sum. So day_number = 1 + days_before.
Thus, for millennium date: Y=1000, M=1, D=1. days_before = total days in years 1..999 + 0 months before + 0 = total_days_1_to_999. So day_number = 1 + total_days_1_to_999.
Similarly, for birthdate: day_number = 1 + days_before_birth.
But check with birthdate (1,1,1): days_before_birth = 0, so answer = total_days_1_to_999. That would be number of days from (1,1,1) inclusive to (1000,1,1) exclusive? Actually earlier we derived answer = day_number(1000) - day_number(birth). That equals total_days_1_to_999 + 1 - (0+1) = total_days_1_to_999. So indeed answer = total days in years 1 through 999 (i.e., days before year 1000). That makes sense: the number of days from birthdate (1,1,1) inclusive to (1000,1,1) exclusive is exactly all days of years 1 through 999, because after the last day of year 999, the next day is millennium day. But wait, is the last day of year 999 included? If we start at (1,1,1) inclusive, and we want to count days up to but not including (1000,1,1), then we count all days from (1,1,1) through (999, last day). That's exactly total days in years 1..999. So answer for (1,1,1) should be total days in years 1..999. Sample output for first entry is 196470. So total days 1..999 should be 196470. We can verify later.
Now we need to compute days in each year based on the rules. Years that are multiples of 3 (year 3,6,9,...) consist of 10 big months (each 20 days) = 200 days. For other years (non-multiples of 3), the months alternate: month1 big (20), month2 small (19), month3 big, month4 small, ... month10 small. Since there are 10 months, and starting with big, the pattern is: big, small, big, small, big, small, big, small, big, small. That yields 5 big months and 5 small months? Actually there are 10 months: positions 1,3,5,7,9 are big (5 of them), positions 2,4,6,8,10 are small (5 of them). So total days = 520 + 519 = 100 + 95 = 195 days. Wait: 520=100, 519=95, sum=195. That's for common years. For leap (multiple of 3) years: 10 big months, each 20 days = 200 days.
But need to confirm: "Each year consists of 10 months, from month 1 through month 10, and starts from a big month. A common year shall start with a big month, followed by small months and big months one after another. Therefore the first month is a big month, the second month is a small month, the third a big month, ..., and the 10th and last month a small one. A big month consists of 20 days and a small month consists of 19 days. However years which are multiples of three, that are year 3, year 6, year 9, and so on, shall consist of 10 big months and no small month." So indeed common years: pattern big, small, big, small,... ending with small (10th small). That yields 5 big, 5 small => 195 days. Multiples of 3: all 10 big => 200 days.
Check sample: For birthdate (344,2,19). The output is 128977. We'll verify after building logic.
But there is an important nuance: The first year is year 1. Is year 1 a multiple of 3? 1 mod 3 = 1, not multiple. So year 1 is a common year (195 days). Good.
Also need to compute days before a given date within a year: for a given year Y (non-multiple of 3 or multiple of 3) we need to compute days completed in earlier months plus days in current month up to day D-1? Actually days_before = sum of days in previous years + sum of days in previous months of current year + (D-1). Then answer = total_days_1_to_999 - days_before.
But careful: For the birthdate, we are counting days before birthdate (i.e., days that have passed before that day). Then the answer is days from that birthdate inclusive to before millennium = total_days_1_to_999 - days_before_birth. So if we compute days_before_birth as days before the date (i.e., number of days that elapsed from (1,1,1) to (Y,M,D) exclusive of that date), then answer = total_days_1_to_999 - days_before_birth. Alternatively, compute day number as 1+days_before, then subtract as earlier.
We'll compute days_before_birth as described.
Implementation steps:
Precompute total days for years 1..999. We can compute an array cumulative_days[year] = total days from year 1 to year inclusive? Actually easier: compute total_days_up_to_year[y] = sum of days for years 1..y. So for y=0, total=0.
Then for a date (Y,M,D), days_before = total_days_up_to_year[Y-1] + days_in_months_before_M in year Y + (D-1). But careful: For year Y itself, we need to know if it's a common or special year. Then we need a function to compute days in months before month M (i.e., sum of days of months 1..M-1) for that year type.
Then answer = total_days_up_to_year[999] - days_before.
Check: For birthdate (1000,1,1) would give days_before = total_days_up_to_year[999] + 0 + 0 = total_days_up_to_year[999]. Answer would be 0, which is correct.
But input Yi < 1000, so Y <= 999. So we don't have to handle Y=1000. But our answer formula uses total_days_up_to_year[999] which is the days before year 1000.
Now let's verify with sample data.
First compute total days for years 1..999. Let's compute number of common years and leap years among 1..999.
Let N = 999. Among numbers 1..999, multiples of 3: floor(999/3) = 333. These are years 3,6,...,999. Year 999 is multiple of 3 (since 999/3=333). So total years that are multiples of 3 = 333. The rest = 999 - 333 = 666 are common years.
But careful: Year 999 is less than 1000, so included in total days 1..999. Year 999 is multiple of 3, so it has 200 days.
Total days = 666 * 195 + 333 * 200. Let's compute: 666195 = 666200 - 6665 = 133200 - 3330 = 129870? Let's compute precisely: 195666 = 195600 + 19566 = 117000 + 12870 = 129870. Yes. 200*333 = 66600. Sum = 129870 + 66600 = 196470. That matches sample output for first entry (1,1,1) which is 196470. So good.
Now test other sample entries manually to confirm formula.
We'll compute days_before for each given birthdate, subtract from total_days (196470) to get answer. Let's compute a couple to verify.
Sample 2: 344 3 1. Year=344, month=3, day=1. Need days_before: total days in years 1..343 plus days before month 3 day 1 in year 344.
First compute years 1..343. Among 1..343, number of multiples of 3: floor(343/3)=114 (since 3114=342). So common years = 343-114=229. Check: 343 total, multiples of 3 = 114 (years 3,6,...,342). So total days for years 1..343 = 229195 + 114200. Compute: 229195 = 229200 - 2295 = 45800 - 1145 = 44655. 114*200 = 22800. Sum = 44655+22800 = 67455? Wait 44655+22800=67455. But let's double-check: 44655+22800=67455. That seems plausible.
But earlier we computed total days up to year 343? Actually total days up to year 343 means sum of days from year 1 to year 343 inclusive. So that's 67455. Then we need days before year 344's month 3 day 1.
Now year 344: 344 mod 3 = 344-342=2, so not multiple of 3 => common year (195 days, pattern alternating). For month 3 day 1, we need days completed in months 1 and 2. In common year: month1 big (20 days), month2 small (19 days). So days before month 3 = days in month1 + month2 = 20+19=39. Then days before day 1 of month 3 = 39 (since D-1=0). So days_before = total_days_1_to_343 + 39 = 67455 + 39 = 67494.
Then answer = total_days_1_to_999 - days_before = 196470 - 67494 = 128976. That matches sample output for second entry. Good.
Sample 3: 696 5 1. Compute.
Years 1..695 total days. Multiples of 3 up to 695: floor(695/3)=231 (since 3231=693). So common = 695-231=464. Days = 464195 + 231200. Compute 464195 = 464200 - 4645 = 92800 - 2320 = 90480. 231*200 = 46200. Sum = 90480+46200=136680. That's total days up to year 695 inclusive? Wait careful: We want years 1..695 inclusive? For days before year 696, we need total days in years 1..695. Yes. So 136680.
Now year 696: 696 mod 3 = 0? 696/3=232, so it's a multiple of 3, thus a leap year with all big months (20 days each). For month 5 day 1: months 1-4 are all big months, each 20 days, so days before month5 = 4*20 = 80. Then days_before = 136680 + 80 = 136760.
Answer = 196470 - 136760 = 59710. That matches sample third output: 59710. Good.
Sample 4: 182 9 5. Compute.
Years 1..181. Multiples of 3: floor(181/3)=60 (360=180). Common=181-60=121. Days = 121195 + 60200. 121195 = 121200 - 1215 = 24200 - 605 = 23595. 60*200 = 12000. Sum = 35595. That's total days up to year 181 inclusive.
Year 182: 182 mod 3 = 182-180=2 => common. For month 9 day 5, need days before month 9 (months 1-8) plus (5-1)=4 days in month 9.
In common year: pattern: month sizes: 1:20,2:19,3:20,4:19,5:20,6:19,7:20,8:19,9:20,10:19. So months 1-8: let's sum. Group: 4 pairs of (20+19)=39 each? Actually months 1-8: 1:20,2:19 (39), 3:20,4:19 (39), 5:20,6:19 (39), 7:20,8:19 (39). That's 4*39 = 156. So days before month 9 = 156. Then add (5-1)=4 => 160. So days_before = 35595 + 160 = 35755. Answer = 196470 - 35755 = 160715. Matches sample 4 output: 160715.
Sample 5: 998 8 7. Compute.
Years 1..997. Multiples of 3: floor(997/3) = 332 (since 3332=996). Common=997-332=665. Days = 665195 + 332200. Compute 665195 = 665200 - 6655 = 133000 - 3325 = 129675. 332*200 = 66400. Sum = 129675+66400=196075. That's total days up to year 997 inclusive.
Year 998: 998 mod 3 = 998-996=2 => common. For month 8 day 7: months 1-7 sum + (7-1). In common year: months: 1:20,2:19,3:20,4:19,5:20,6:19,7:20,8:19,9:20,10:19. Months 1-7: let's sum. There are 7 months. Pattern: 1:20,2:19,3:20,4:19,5:20,6:19,7:20. We can sum: For months 1-6 it's three pairs (20+19)3 = 393 = 117. Then month7 is 20 => total 137. Alternatively compute directly: 20+19=39, +20=59, +19=78, +20=98, +19=117, +20=137. Yes 137. So days before month 8 = 137. Then add days before day 7: D-1 = 6. So total days into year = 137+6 = 143. So days_before = 196075 + 143 = 196218. Answer = 196470 - 196218 = 252. Matches sample 5: 252.
Sample 6: 344 2 19. This is same year as sample 2 but month 2 day 19. We computed for sample 2: days_before for (344,3,1) was 67494. For (344,2,19), let's compute. Year 344 common. Month 2 is small (19 days). For day 19, note that days in month 2 is 19, so day 19 is the last day of the month. Then days_before = total_days_1_to_343 + (days in month1) + (19-1). Month1 = 20 days. So days into year = 20 + 18 = 38? Actually (D-1) = 18, so 20+18=38. But careful: For month 2 day 19, days before this date = month1 full (20 days) + (19-1) days of month2 = 20+18=38. So days_before = 67455 (total years 1..343) + 38 = 67493. Then answer = 196470 - 67493 = 128977. Matches sample: 128977. Good.
Sample 7: 696 4 19. Year 696 leap (all big). Month 4 day 19. Days before: months 1-3 each 20 => 60. Then D-1 = 18 => total = 78. days_before = total years 1..695 (136680) + 78 = 136758. Answer = 196470 - 136758 = 59712. Matches sample: 59712.
Sample 8: 999 10 20. Year 999 mod 3 = 0 => leap (all big). Month 10 day 20: month 10 is big (20 days). Days before month10 = sum months 1-9 = 920 = 180. D-1 = 19 => total days into year = 199. But careful: In leap year, all months are 20 days. So maximum days in year = 200. So day 20 of month 10 is the last day? Actually month 10 is 20 days, so day 20 is the last day. So days_before = total days years 1..998 + 199. We need total years 1..998. Year 998 is common? Actually we need years up to 998, inclusive. Let's compute total days up to 998: includes years 1..998. Since we earlier computed total days up to 997 = 196075. Add year 998 days: year 998 common = 195 days. So total up to 998 inclusive = 196075 + 195 = 196270? Wait 196075 + 195 = 196270. Alternatively we can compute directly. But let's be careful: total days 1..999 = 196470. That includes year 999 as well. So total days 1..998 = total days 1..999 - days in year 999. Year 999 is leap (200 days). So 196470 - 200 = 196270. Yes. So days_before = total_days_1_to_998 + days into year 999 before (999,10,20). But careful: days_before for date (999,10,20) includes full days of years 1..998 plus days in year 999 before that date. So days_before = 196270 + (days before (10,20)). In leap year: days before month10 = months 1-9 = 920 = 180. D-1 = 19 => 199. So days_before = 196270 + 199 = 196469. Then answer = total_days_1_to_999 - days_before = 196470 - 196469 = 1. Matches sample output: 1.
Need to compute days_before(Y,M,D) efficiently. Since Y ≤ 999, we could precompute an array total_days_up_to_year[y] for y from 0 to 999, where total_days_up_to_year[0] = 0, and for each year i from 1 to 999, compute days in year i, and set total_days_up_to_year[i] = total_days_up_to_year[i-1] + days_in_year(i). Then for a date (Y,M,D), days_before = total_days_up_to_year[Y-1] + days_in_months_before_M_in_year(Y, M) + (D-1). Then answer = total_days_up_to_year[999] - days_before.
But careful: If Y=1, then Y-1=0, total_days_up_to_year[0]=0, works.
Alternatively, we can compute on the fly without precomputing array by using formulas for number of leap years, but precomputation is simpler.
Given n up to 100 and Y up to 999, precomputation array of size 1000 is trivial.
Now we need a function to compute days in a given year Y: if Y % 3 == 0, then 200, else 195.
Also need a function to compute days in months before month M in year Y (i.e., sum of days of months 1..M-1). This depends on whether Y is multiple of 3.
If Y % 3 == 0 (leap year), all months have 20 days. So sum = 20*(M-1).
Else (common year): pattern: for month i, days = 20 if i is odd, 19 if i is even. Because month 1 odd -> big, month 2 even -> small, etc. So sum for first M-1 months. This sum can be computed: For k = M-1 months, we can compute number of odd months among first k, and number of even months. Since months start at 1 (odd). For first k months, the count of odd months = ceil(k/2) because positions 1,3,5,...; count of even months = floor(k/2). So sum = 20 * ceil(k/2) + 19 * floor(k/2). Alternatively, we can compute via formula: if k is even, there are k/2 odds and k/2 evens, sum = (20+19)(k/2) = 39(k/2). If k is odd, there are (k+1)/2 odds and (k-1)/2 evens, sum = 20*((k+1)/2) + 19*((k-1)/2) = can compute as 39*((k-1)/2) + 20. Since we have small n, we can just loop from 1 to M-1 and sum, that's fine as M ≤ 10, so max loop 9 per date, negligible.
Simplify: Write function days_before_month(Y, M) that loops i=1 to M-1 and adds days_in_month(Y,i). Where days_in_month(Y,i) returns: if Y%3==0: 20; else: if i%2==1: 20 else 19.
Then days_before = total_days_up_to_year[Y-1] + days_before_month(Y,M) + (D-1).
Edge Cases: Validate that D is within month's days. According to input, Di ≤ 20, but we trust it's valid. But might check: For common year, even months have 19 days, so D should be ≤19; for leap year, all have 20. Input probably respects that.
Now, compute total_days_up_to_year[999] using precomputation.
Implementation steps in code:
Define MAX_YEAR = 1000 (or 1000+ something). Use array of size 1000 (0-indexed for years). Actually we can make array of length 1000 (indices 0..999). Let cum[0] = 0. For y from 1 to 999: cum[y] = cum[y-1] + (200 if y%3==0 else 195).
Then total_days_1_to_999 = cum[999] (since cum includes up to year 999). Actually cum[999] = sum of days for years 1..999. Yes.
For each dataset (Y,M,D):
days_before_years = cum[Y-1] (if Y-1>=0; if Y==1, cum[0]=0)
days_before_months = 0; for m = 1 to M-1: add days_in_month(Y,m)
Now we need to ensure we treat the year numbering correctly: Year 1 starts from the day after "tomorrow" from declaration? The problem says: "Tomorrow shall be the first day of the calendar, that is, the day 1 of the month 1 of the year 1." So the epoch is day 1 = year1 month1 day1. So our counting matches.
Now we output each answer as integer.
Edge Cases: If birthdate is exactly the day before millennium day? That would be (999,10,20) if year 999 is common? But sample shows (999,10,20) yields answer 1 because it's the last day before millennium? Actually millennium day is (1000,1,1). The day before that is the last day of year 999. For leap year (year 999 all big), last day is (999,10,20). So answer = 1, meaning from that birthdate inclusive (which is the last day) to millennium exclusive is just that day? Actually from (999,10,20) inclusive to (1000,1,1) exclusive: you count day (999,10,20) only? That's 1 day. So output 1. That matches sample.
What if birthdate is after millennium day? Not possible as Y<1000.
Thus solution is straightforward.
But before finalizing, we should double-check the handling of the inclusive/exclusive nuance. In the formula answer = total_days_1_to_999 - days_before, we derived from day_number difference. Let's re-derive: Let E = day number of epoch (1,1,1) = 1. For date X, day_num(X) = 1 + days_before(X). Then number of days from X inclusive to Y exclusive = day_num(Y) - day_num(X). If Y is millennium day (1000,1,1), then day_num(Y) = 1 + total_days_1_to_999 (since days before Y = total_days_1_to_999). So answer = (1 + total_days_1_to_999) - (1 + days_before(X)) = total_days_1_to_999 - days_before(X). So consistent.
Now note: days_before(X) = number of days before X (i.e., days from epoch to the day before X). So for X = (1,1,1), days_before=0, answer = total_days_1_to_999, which is 196470. Good.
Thus implementation is correct.
One could also compute directly via formulas using counts of leap years, but precomputation is simpler and less error prone.
Now let's consider if the input can have Y=0? No, Yi is positive (<1000). So Y>=1.
Now what about month and day constraints? Mi ≤ 10, Di ≤ 20. Valid.
Potential pitfall: For a common year, month 10 is small (19 days). So D should be ≤ 19. Input likely ensures that.
Now produce final code.
We'll write in C++ (since many solutions are in C++, but we can produce any language). The problem statement expects a program; typical approach: C, C++, Java, Python. We'll produce a C++ solution.
Implementation details:
Read integer n.
Precompute cumulative array up to 1000 (index 0..1000). Actually we need up to 999, but we can go to 1000.
Potential overflow: cum[999] = 196470, fits within int (32-bit). Actually max total days could be 1000*200 = 200,000, well within int. So int is fine.
Now, some might prefer to compute cum using long long but not needed.
Edge Cases: Y=999, M=10, D=20 gave answer 1. With our method: cum[998] = total days 1..998 = 196270; daysInMonthsBefore for M=10: loop m=1..9: if year multiple of 3: each 20 => 180; plus D-1=19 => daysBefore = 196270+180+19=196469; answer=196470-196469=1. Works.
Y=999, M=10, D=19: then daysBefore = 196270+180+18=196468; answer=2. Not in sample but plausible.
Now we should confirm for the date (999,10,20) that it's the last day. In leap year, last day is indeed (10,20). In common year, last day is (10,19) because month10 is small (19). But for year 999 it's leap, so correct.
Now check the pattern: For common year, months: 1:20, 2:19, 3:20, 4:19, 5:20, 6:19, 7:20, 8:19, 9:20, 10:19. So total = 520+519=195. For leap year: all 20 => 200.
All good.
Now, we should be careful about the definition of "years which are multiples of three" includes year 3,6,9,... So year 0 doesn't exist. So we start at year 1.
Thus code is simple.
Now produce final answer.
But before final, we can also compute cum as:
int cum[1001] = {0};
for (int y=1; y<=1000; ++y) {
int days = (y % 3 == 0) ? 200 : 195;
cum[y] = cum[y-1] + days;
}
But we only need up to 999. But having up to 1000 might be okay.
But careful: For year 1000, we might compute, but it's not needed.
Alternatively, we can compute total_days_1_to_999 directly as 196470 (as constant) since it's fixed? But that's cheating; better compute generically.
Now consider if Y=0? Not allowed.
Now produce solution.
We'll also include a function days_in_month(int y, int m) for clarity.
Implementation:
#include
using namespace std;
int days_in_month(int y, int m) {
if (y % 3 == 0) return 20;
else return (m % 2 == 1) ? 20 : 19;
}
int main() {
int n;
cin >> n;
const int MAX_YEAR = 999;
int cum[1000] = {0}; // cum[y] = total days in years 1..y
for (int y = 1; y <= MAX_YEAR; ++y) {
int days_in_year = (y % 3 == 0) ? 200 : 195;
cum[y] = cum[y-1] + days_in_year;
}
int total_days = cum[MAX_YEAR]; // total days in years 1..999
code
while (n--) {
int Y, M, D;
cin >> Y >> M >> D;
int days_before = cum[Y-1]; // years before Y
for (int m = 1; m < M; ++m) {
days_before += days_in_month(Y, m);
}
days_before += (D - 1);
int ans = total_days - days_before;
cout << ans << endl;
}
return 0;
}
Check with sample.
Now, ensure we handle Y=1 correctly: cum[0] should be 0, fine.
Now let's test sample quickly by mental simulation.
Already did.
Thus answer.
But we need to consider if the input includes lines like "8" then lines. That's fine.
Now output exactly the number.
Thus final code.
But there is a subtlety: In some programming languages, reading input might need to handle extra spaces; but C++ handles.
Now, we can also compute cum array once outside loop.
Thus final solution is good.
However, let me think about alternative potential interpretation: The problem says "the number of days from the birthdate, inclusive, to the millennium day, exclusive." So we count from birthdate day up to but not including millennium day. So if birthdate is millennium day itself, answer = 0. That's consistent.
Now ensure that we treat inclusive/exclusive correctly: For birthdate (1000,1,1) not present.
Now maybe the year 1000 is also a special year? Since year 1000 is not a multiple of 3, it's common. But the millennium day is the start of that year, so we don't need its details.
Thus all set.
Now we can produce final answer in code.
But we should note that the problem statement shows sample input and output exactly as given. So it's fine.
Now I'll produce the final answer with explanation. ```cpp
#include
using namespace std;
// Returns the number of days in a given month of a given year
int days_in_month(int year, int month) {
if (year % 3 == 0) { // year is a multiple of 3 → all months have 20 days
return 20;
} else { // common year: odd months big (20), even months small (19)
return (month % 2 == 1) ? 20 : 19;
}
}
int main() {
int n;
cin >> n;
code
// pre‑compute cumulative days up to each year (years 1 .. 999)
const int MAX_YEAR = 999;
int cum[1000] = {0}; // cum[y] = total days in years 1 .. y (cum[0] = 0)
for (int y = 1; y <= MAX_YEAR; ++y) {
int days_in_year = (y % 3 == 0) ? 200 : 195;
cum[y] = cum[y-1] + days_in_year;
}
int total_days_1_to_999 = cum[MAX_YEAR]; // = 196470
while (n--) {
int Y, M, D;
cin >> Y >> M >> D;
// days before the given date
int days_before = cum[Y-1]; // full years before year Y
for (int m = 1; m < M; ++m) { // full months before month M in year Y
days_before += days_in_month(Y, m);
}
days_before += (D - 1); // days before day D in month M
// answer = days from birthdate (inclusive) to millennium (exclusive)
int answer = total_days_1_to_999 - days_before;
cout << answer << endl;
}
return 0;