Rahul's Dad is the CEO of one of the leading companies. Every time somebody seeks for an appointment he calls up his secretary and asks her whether the day is a Sunday or not. He has to keep his caller on hold and is unhappy about it. Knowing that his son Mayank knows a bit of programming he asks him to make a program to help him find all the sundays in a given month of a specific year.
Input:
The first Line contains t an integer, the number of test cases. The next t lines contain to integers first the year and then month.
Output:
Output consists of t lines containing the dates of sundays in that particular month
But the description says: "The next t lines contain to integers first the year and then month." Actually sample input: "2\n3 2013\n5 2010". That appears to be month then year? Actually it says "first the year and then month." But sample shows "3 2013" and "5 2010" which would be month=3, year=2013? That seems month first then year? Let's check output: For first test case (month=3, year=2013), output is "3 10 17 24 31". That corresponds to dates (days) of March 2013 that are Sundays? Let's verify: March 2013: March 1, 2013 was a Friday? Actually let's recall: 1 March 2013? Quick check: 2013-03-01 was Friday. Then Sunday would be March 3, 10, 17, 24, 31. Yes, that matches output. So the input order is month then year. But description says "first the year and then month" but sample suggests otherwise. Possibly they swapped. But based on the sample, we should interpret input as month then year. However let's check second test case: month=5, year=2010: May 2010? May 1, 2010 was Saturday. Then Sundays: 2,9,16,23,30. Output: "2 9 16 23 30". Yes matches. So input: month, year.
So the problem: Given t test cases, each with month (1-12) and year (2000 ≤ year ≤ 5000), find all dates (day of month) that are Sundays. Output them in a line, separated by spaces. The dates are just the day numbers. We need to output for each test case.
Constraints: t < 100000, year up to 5000. So we need efficient method.
We can compute the day of week for the first day of the month, then compute the offset to first Sunday, then list all Sundays.
We need to consider leap years. Use known algorithms: Zeller's congruence, or compute using epoch.
We can also use the fact that we have year up to 5000, we could precompute a table of cumulative days, but t up to 100k, we can compute per test case quickly.
Simplest: Use the formula: day of week (0=Sunday..6=Saturday) for a given date. For example, using Zeller's congruence for Gregorian calendar:
h = ( q + floor((13*(m+1))/5) + K + floor(K/4) + floor(J/4) - 2*J ) mod 7
where:
h: day of week (0=Saturday,1=Sunday,...? Actually Zeller's variant gives 0=Saturday, 1=Sunday,...6=Friday). But we can adjust.
But careful: m is month with March=3,...January=13, February=14 of previous year.
So we can define: if month < 3: let m = month+12, y = year-1 else m=month, y=year.
Then compute K = y % 100, J = y // 100.
Then h = (q + floor((13*(m+1))/5) + K + floor(K/4) + floor(J/4) + 5J) % 7? Actually there are variations. Standard Zeller: h = (q + floor((13(m+1))/5) + K + floor(K/4) + floor(J/4) + 5J) mod 7, where result: 0=Saturday,1=Sunday,...6=Friday. Or the alternative: h = (q + floor((13(m+1))/5) + K + floor(K/4) + floor(J/4) - 2*J) mod 7, with 0=Saturday? Need consistency.
Better: Use Sakamoto's algorithm: static int t[] = {0,3,2,5,0,3,5,1,4,6,2,4}; // month offset? Actually Sakamoto's method:
int dayofweek(int y, int m, int d) /* 1 <= m <= 12, y > 1752 (in the U.K.) /
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
Returns 0 for Sunday? Actually typical implementation: returns 0 = Sunday? Let's check: Commonly it returns 0=Sunday,1=Monday,...6=Saturday. For example, for 1 Jan 2000 (Saturday) -> should return 6? Let's test: y=2000, m=1, d=1 -> m<3 so y=1999; then y + y/4 - y/100 + y/400 = 1999+499-19+4 = 2483; t[0]=0; d=1; sum=2484; 2484%7=... 7355=2485, so 2484%7=6? Actually 7*354=2478, remainder 6. So returns 6 which would be Saturday if 0=Sunday? That would be 6 = Saturday? So 0=Sunday? Then 6 = Saturday. That matches: 1 Jan 2000 is Saturday. So yes, 0=Sunday,1=Monday,...,6=Saturday.
But we need to confirm with known date: March 1,2013 (Friday) -> y=2013,m=3,d=1 -> m>=3 so y unchanged 2013. y/4=503, y/100=20, y/400=5; sum = 2013+503-20+5=2501; t[2]=2? Actually t[2] (index 2 for March) = 2 (since t[] = {0,3,2,5,0,3,5,1,4,6,2,4}), so plus d=1 gives 2504; 2504%7 = ? 7*357=2499, remainder 5. So 5 -> if 0=Sunday, then 0 Sun,1 Mon,2 Tue,3 Wed,4 Thu,5 Fri,6 Sat. So 5 is Friday, correct. So Sakamoto works with 0=Sunday.
Thus we can compute day of week for first day of month: d=1. Then first Sunday = if dow0 == 0 then day 1, else day = 1 + (7 - dow0) % 7? Actually if dow0 is day of week of day 1 (0=Sunday). Then first Sunday after day 1: if dow0 == 0: day 1 is Sunday; else next Sunday is day = 1 + (7 - dow0) because if dow0=1 (Monday) then days until Sunday: 6 => 1+6=7; if dow0=2 -> 5 -> day 6? Actually compute: if dow0 = d (0=Sunday), then the offset to next Sunday is (7 - d) % 7. But if d=0, offset 0. So first Sunday = 1 + (7 - d) % 7. But careful: if d=0, (7-0)%7 = 0, so day=1.
Then subsequent Sundays are first Sunday + 7, +14, etc., while <= days_in_month.
So we need days_in_month for given month and year (leap year affects February). Leap year: if year divisible by 400, or (divisible by 4 and not by 100) -> leap.
So algorithm per test case:
Input m, y (but be careful about order: we assume month first then year as sample, but problem statement says year then month. Let's see if they might have a mistake? The sample clearly shows month first. The updated test cases maybe corrected? The instruction says: "The next t lines contain to integers first the year and then month." But sample input shows "3 2013" which is month=3, year=2013. So contradictory. Possibly the description is wrong; but we should follow sample. However sometimes coding problems have input order swapped. We can check typical problem: "Sundays in a month" might have input year and month. But sample shows month then year. Without further info, we assume month then year as per sample. Also check constraints: "month ≤ 12, 2000 ≤ year ≤ 5000". They put month before year in the constraint? Actually they wrote: "month ≤ 12, 2000 ≤ year ≤ 5000" but that doesn't indicate order. Many problems state "first the year and then month." But then they give sample with month first? Actually sample:
2
3 2013
5 2010
If they said first year then month, sample would be "2013 3" but it's "3 2013". So likely they made mistake in description. So we implement as month then year.
But we should consider that the test cases on the judge might follow the description, not the sample. That is ambiguous. In such cases, we can try to read both as integers and then if the first is >12 then it's year, else month? But constraints: year >=2000, month <=12. So first integer will always be <=12 if it's month, and >=2000 if it's year. So we can differentiate. So we can read two integers a,b. If a <= 12 and b >= 2000 then likely a is month, b is year. If a >= 2000 and b <=12 then likely a is year, b is month. But what if some test case where both <=12? Not possible because year >=2000. So it's safe: we can check the first integer: if a > 12 then it's year; else it's month. But what if year is 2000 but that's >12, so a>12 => year, else a<=12 => month. So we can decide:
if a > 12: // then a is year, b is month? But then year could be >12 and month <=12, but b might also be >12 if it's year? Actually b would be month if it's the second integer, but month <=12. So if a>12 and b<=12, then order is year then month. If a<=12 and b>12, order is month then year. In sample: a=3, b=2013 => a<=12, b>12 so month then year. So we can implement:
Read a, b.
If a <= 12 and b >= 2000: month = a, year = b.
Else if a >= 2000 and b <= 12: year = a, month = b.
Else ambiguous? Not possible.
Thus we can handle both possibilities. That would be robust. But typical programming challenge might expect fixed order. But we can implement the robust method.
Now, output: list of dates (day numbers) in ascending order separated by spaces. For each test case, print each number separated by space, and after the line, newline. If no Sunday? Every month has at least 4 Sundays, sometimes 5. So output will always have numbers.
Constraints: t up to 100k, we need efficient I/O. Use fast input (sys.stdin.buffer) and output via list of strings and join.
Now, implement Sakamoto:
def day_of_week(y, m, d):
if m < 3:
y -= 1
m += 12
# Zeller? But we can use simpler: y + y//4 - y//100 + y//400 + t[m-1] + d? Actually the Sakamoto method as above.
# But that method uses month offset t[] defined for m from 1 to 12, and adjusts year if m<3.
t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
if m < 3:
y -= 1
return (y + y//4 - y//100 + y//400 + t[m-1] + d) % 7
But careful: In Sakamoto, the month offset t[] uses original m (1-12) and we adjust year. So we can keep m as original (1-12). So we can implement:
def dow(year, month, day):
if month < 3:
year -= 1
month += 12? Actually Sakamoto does not change month; it only adjusts year. In the standard Sakamoto code:
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
So month stays same. So we need t for month indices 1-12. So we don't change month. So we just do:
if month < 3: year -= 1
Then use t[month-1] (month is original). That's correct.
But I recall there is a variation with month offset that works. For March=2? Actually t[] provided is {0,3,2,5,0,3,5,1,4,6,2,4} which corresponds to Jan=0, Feb=3, Mar=2, Apr=5, May=0, Jun=3, Jul=5, Aug=1, Sep=4, Oct=6, Nov=2, Dec=4. This is standard.
Thus dow = (year + year//4 - year//100 + year//400 + t[month-1] + day) % 7, with year adjusted if month<3.
That gives 0=Sunday.
Now, compute first Sunday date:
dow1 = dow(year, month, 1)
if dow1 == 0:
first_sun = 1
else:
first_sun = 1 + (7 - dow1) # Because dow1 in 1..6 => days to next Sunday = 7 - dow1
Actually if dow1=1 (Monday), then days to Sunday =6 => 1+6=7. 7-dow1 =6. correct.
If dow1=6 (Saturday), then days to Sunday =1 => 1+1=2, 7-6=1 correct.
Thus first_sun = 1 + (7 - dow1) % 7; but (7-dow1) for dow1>0 is between 1 and 6, and mod 7 ensures dow1=0 gives 0. So we can do: first_sun = 1 + ( (7 - dow1) % 7 ) which works for dow1=0 yields (7-0)%7=0 -> 1, ok.
Alternatively: first_sun = (8 - dow1) if dow1>0 else 1? Not needed.
Now, we need the number of days in the month. Write a function days_in_month(year, month). For months: Jan=31, Feb=28 or 29, Mar=31, Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31. Use leap year function: leap = (year%400==0) or (year%4==0 and year%100!=0).
Now, generate list: for d in range(first_sun, days+1, 7): collect d.
Then output as space-separated string.
Edge Cases: Month can be any 1-12, year up to 5000. t up to 100k, all fine.
Now, about performance: Doing Sakamoto and leap check per test case is cheap.
But we need to ensure input reading is fast.
Implementation Plan (Python):
Import sys
Define t = [0,3,2,5,0,3,5,1,4,6,2,4]
Define function is_leap(y): return y%400==0 or (y%4==0 and y%100!=0)
Define dow(y,m,d):
yr = y
if m<3: yr -= 1
return (yr + yr//4 - yr//100 + yr//400 + t[m-1] + d) % 7
Read input: data = sys.stdin.buffer.read().split()
Convert to ints.
First value is t.
Then iterate i from 1 to t*2 step 2: a=int(data[i]), b=int(data[i+1]).
Determine month and year:
if a <= 12 and b >= 2000: month = a; year = b
elif a >= 2000 and b <= 12: year = a; month = b
else: # maybe both? not possible, but fallback: if a<=12 and b<=12? not possible because year>=2000 so b cannot be <=12 if a is month? Actually if a is year and b is month, a>=2000, b<=12. If a is month and b is year, a<=12, b>=2000. So both conditions cover all. So set accordingly.
Compute days = month_days[month] but if month==2 and is_leap(year): days=29.
Potential issues: The dow function using t[m-1] with m unchanged is correct. Verify with March 2013: dow(2013,3,1) => yr=2013 (m>=3) => (2013 + 503 -20 +5 + t[2]=2 +1) = 2504 %7 = 5 (Friday) correct.
Now, test with sample:
Test1: month=3, year=2013. days=31. dow1 =? As computed 5. first_sun = 1 + (7-5)%7 = 1+2=3. Then 3,10,17,24,31.
Test2: month=5, year=2010. days=31. dow1? Compute: 2010,5,1. yr=2010 (m>=3). 2010 + 502 -20 +5? Actually 2010//4=502, //100=20, //400=5, t[4]=0, d=1 => 2010+502-20+5+0+1=2498. 2498%7? 7357=2499, remainder -1? Actually 7356=2492, remainder 6. 2498-2492=6 => 6? So dow1=6 (Saturday). first_sun = 1+(7-6)%7=1+1=2. Then list 2,9,16,23,30. Works.
Now, ensure output format: each line contains dates separated by space. No trailing spaces, but that's okay with join.
Now, constraints: t up to 100k, each output line may have up to 5 numbers, so total output size not huge. Use join.
Now, possible pitfalls: The input order may be ambiguous. I'll implement the detection logic. But note: The problem statement says "first the year and then month". So maybe the intended order is year then month, but sample mistakenly swapped? But if we implement detection, it will work either way. However, if some test case uses order year then month (i.e., first integer >=2000, second <=12), our detection will correctly assign. If it uses month then year, detection also works. So it's safe.
Double-check detection: Suppose input is year then month: first integer a=2013, b=3. Then a>12 and b<=12 => condition a>=2000 and b<=12 true => year=a, month=b. Good.
If input is month then year: a=3, b=2013 => a<=12 and b>=2000 true => month=a, year=b. Good.
If by chance a=12 and b=2000? That would be month=12, year=2000, and both conditions: a<=12 and b>=2000 true, also a>=2000 false. So picks first condition. So fine.
Now, we should also consider that maybe they intend year first but the sample had a mistake? But we are robust.
Now, consider edge case: month 2 in leap year 2000: days=29. Compute Sundays accordingly.
Now, let's test a few manually:
January 2000: 1 Jan 2000 is Saturday (dow1=6). first_sun = 1+(7-6)%7=2 => 2,9,16,23,30. So output should be 2 9 16 23 30.
February 2000 (leap): 1 Feb 2000? dow of 1 Feb 2000? Let's compute quickly: 2000,2,1. Since m<3, yr=1999. 1999+1999//4=1999+499=2498, -1999//100= -19 => 2479, +1999//400= +4 => 2483, + t[1]=3, +1 => 2487. 2487%7? 7*355=2485, remainder 2. So dow1=2? That would be Tuesday? Actually 1 Feb 2000 was Tuesday? Quick check: 1 Jan 2000 Sat, then Jan has 31 days, so 1 Feb is Tuesday? 31 days from Sat: 31%7=3, Sat+3=Tue yes. So dow1=2 (Tuesday). first_sun = 1+(7-2)%7=1+5=6 => 6,13,20,27. So output 6 13 20 27. Also check 29 Feb? 29 is Tuesday, not Sunday. So okay.
Now, check month December 5000? That's far but fine.
Now, performance: 100k test cases, each constant operations, fine in Python.
Now, implement.
But we need to be careful about the modulo operation when dow1=0: (7-0)%7=0, so first_sun=1. Works.
Now, writing solution.
But there is a potential nuance: The sample input says "2\n3 2013\n5 2010". They printed "3 10 17 24 31" and "2 9 16 23 30". So that's correct.
Now, code.
However, I recall that for Zeller's or Sakamoto, we must ensure that the formula yields correct for years up to 5000. It should.
Alternative: Use Python's datetime module? But that might be slower and might not be allowed (though usually allowed) but for safety, we can implement formula.
But note: Python's datetime only supports year up to 9999, so it's fine, but might be slower? Actually datetime might be more overhead, but probably fine for 100k calls. However, many online judges restrict usage of datetime? Not explicitly. But it's safer to use formula.
Now, let's plan code structure.
We'll define:
import sys
def main():
data = sys.stdin.buffer.read().split()
if not data: return
t = int(data[0])
idx = 1
t_offsets = [0,3,2,5,0,3,5,1,4,6,2,4]
month_days_normal = [0,31,28,31,30,31,30,31,31,30,31,30,31]
out_lines = []
for _ in range(t):
a = int(data[idx]); b = int(data[idx+1]); idx+=2
if a <= 12 and b >= 2000:
month, year = a, b
else:
# assume a is year, b is month
year, month = a, b
# compute days
days = month_days_normal[month]
if month == 2:
# leap year check
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
days = 29
# compute dow for 1st
y = year
if month < 3:
y -= 1
dow1 = (y + y//4 - y//100 + y//400 + t_offsets[month-1] + 1) % 7
# find first Sunday
if dow1 == 0:
first = 1
else:
first = 1 + (7 - dow1)
# generate list
sundays = []
d = first
while d <= days:
sundays.append(str(d))
d += 7
out_lines.append(' '.join(sundays))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
main()
But we need to ensure that for dow1 == 0 we set first=1. Actually first = 1 + (7 - dow1) % 7 works for all. Let's compute: (7-0)%7=0 -> 1. So we can just do: first = 1 + (7 - dow1) % 7. That's simpler.
Now, test with sample: dow1=5 => (7-5)%7=2 => first=3; dow1=6 => (7-6)%7=1 => first=2; dow1=0 => first=1. Good.
Now, we can also compute using:
first = (8 - dow1) % 7? Actually if dow1 is 0: (8-0)%7=1? Actually 8%7=1, not 1. But we want 1. Let's see: For dow1=1 (Monday), we want first=7. (8-1)=7 -> 7, that's correct. For dow1=2 -> 6, correct. dow1=3 -> 5, dow1=4 ->4, dow1=5 ->3, dow1=6 ->2, dow1=0 ->8 -> but 8 >? Actually if dow1=0, 8-0=8, but we want 1. So not good. So (8 - dow1) works except when dow1=0 we need to subtract 7? So better to use the (7-dow1)%7.
We'll use first = 1 + (7 - dow1) % 7.
Now, consider that the while loop can be replaced by list comprehension:
sundays = list(range(first, days+1, 7))
But then need to convert to strings. So we can do: sundays = range(first, days+1, 7) and then ' '.join(map(str, sundays))
But careful: range returns an iterator in Python3, but join expects strings. So map(str, sundays) works with range.
Now, code optimization: Use local variables for speed.
Now, think about input detection: Could there be case where a and b both >12? Not possible because one is month, so <=12. Could both be <=12? Not possible because year >=2000. So detection works.
Now, also consider that month could be 0? No, month from 1 to 12.
Now, let's test with a few random cases using Python datetime to verify.
I'll simulate:
But overall it's correct.
Now, one more nuance: The problem statement: "Output consists of t lines containing the dates of sundays in that particular month". It doesn't specify what if there are no Sundays? But always at least 4.
Now, edge case: For month February in non-leap year (28 days), possible Sundays: maybe 4 or 5? Let's check: For February 2015 (non-leap, 28 days). 1 Feb 2015 was Sunday? Actually 1 Feb 2015 was Sunday? Check: 1 Feb 2015 was Sunday? I think 1 Feb 2015 was Sunday? Wait, 2015-02-01: Yes, it was Sunday. So Sundays: 1,8,15,22. That's 4. So we output those. Our code will compute first_sun accordingly.
Now, ensure for February 2015: year=2015, month=2. days=28. dow1? Let's compute: y = year if month>=3 else year-1 => 2014. So dow1 = (2014 + 503 - 20 + 5 + t[1]=3 + 1) %7. 2014+503=2517, -20=2497, +5=2502, +3=2505, +1=2506. 2506%7? 7358=2506 exactly? 7358=2506, remainder 0. So dow1=0 -> first_sun=1. Then Sundays: 1,8,15,22. Good.
Now, month February 2100 (non-leap because 2100 not divisible by 400, but divisible by 4 and 100 so not leap). Let's test: 1 Feb 2100? That's a Monday? Actually we can trust code.
Now, confirm leap year check: if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0). Good.
Now, final code.
Potential issues: The detection of month/year order might fail if the input uses year then month but the year is 2000 and month is 12, that's fine. If they use month then year, also fine. So we are safe.
But maybe some test cases have extra spaces? We'll use split.
Now, performance: 100k test cases, each with few arithmetic operations, should be fine.
Now, we should also consider using Python's built-in functions like calendar.weekday but that might be slower? But it's okay.
But let's measure: 100k loops in Python ~0.1-0.2 seconds for arithmetic; reading input and output also.
Now, we must ensure to flush output.
Now, implement final answer.
We should also handle t = 0? Possibly not, but we can handle.
Now, final code.
Let me write the solution.
But wait: The problem says: "t<100000, month ≤ 12, 2000 ≤ year ≤ 5000". So t up to 100k.
Now, output: "Output consists of t lines containing the dates of sundays in that particular month". So each line should have the dates separated by spaces.
Now, maybe they require leading zeros? No, just day numbers.
Now, we should confirm that the sample output uses single space between numbers and no trailing space.
Now, code.
But before finalize, think about potential off-by-one errors with Sakamoto's formula: It returns 0 for Sunday. That's what we used.
Alternative: Use Zeller's: h = (q + floor((13*(m+1))/5) + K + floor(K/4) + floor(J/4) + 5*J) % 7, with h from 0=Saturday to 6=Friday? Not consistent.
Our method is fine.
Double-check for a date far in future: year 5000, month 12, day 1. Compute using formula: Should be fine.
Now, final answer.
But the problem says: "Test cases updated.. You may submit now". So perhaps they corrected the input order? Not sure. Our detection will handle both.
Now, we produce final solution explanation.
Given the nature of the conversation: "Write a python solution to the problem" we need to output the code.
So final answer: provide the Python code.
But the user asked: "Sundays in a month (ID: 19907) ... You need to solve ..." Probably we need to output the solution code.
Thus, final.
```python
import sys
def main() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
t = int(data[0])
idx = 1
code
# month offset for Sakamoto's algorithm (0 = Sunday)
t_offsets = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
out_lines = []
for _ in range(t):
a = int(data[idx])
b = int(data[idx + 1])
idx += 2
# Determine order: either (month, year) or (year, month)
if a <= 12 and b >= 2000:
month, year = a, b
else: # then (year, month) is the only possibility
year, month = a, b
# Days in the month (February leap‑year handling)
days = month_days[month]
if month == 2:
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
days = 29
# Day‑of‑week for the 1st (Sakamoto, 0 = Sunday)
y = year
if month < 3:
y -= 1
dow1 = (y + y // 4 - y // 100 + y // 400 + t_offsets[month - 1] + 1) % 7
# First Sunday of the month
first_sunday = 1 + (7 - dow1) % 7
# Collect all Sundays
sundays = range(first_sunday, days + 1, 7)
out_lines.append(' '.join(map(str, sundays)))
sys.stdout.write('\n'.join(out_lines))