LONGCOOK :EDITORIAL Easy Code

So firstly its a Gregorian Calendar.
Now for overlapping if you take a close look over several months ,you will observe following things
->For any other month other than FEB you wont have a overlap ::
Reason:: lets take best condition 1st friday is 7th ,then long challenge will extend till 7+10 =17 , now for overlap the second last sunday should fall on or before 17th , that means last sunday should fall on 24 or before : whoa if you look at this there will be one more sunday for 31 and i did not even took into account sequence of days.
If you take that into account lets say friday first is 7th then sunday would come at 30th second last at 23 ,huge diffrence .
Similarly you can check for other 6 days .

Now for February if its a leap year and first friday is 7Th than we have a answer ,for non leap both 6th and 7th dates will work

Now calendar repeats every 400 year .So you dont need to check for all.just calculate for first 400.

Now how to check which day is the first friday for each Feb Year, its simple just subtract 1 from previous years day if its non leap and 2 if its a leap

Example 1st friday in year 1 was on 2nd ,it means for year 2 it will be on 1st and so on

Happy coding :innocent:

#include <bits/stdc++.h>
    using namespace std;
    int main() {
    	// your code goes here
    	int t;
    	cin>>t;
    	int day=2;
    	vector<int> arr;
    	bool leap=false;
    	for(int i=1;i<=400;i++){
    	    leap=false;
    	    if(i%4==0&&i%100!=0 || i%400==0){
    	        leap=true;
    	    }
    	    if(day==0){
    	        arr.push_back(i);
    	    }
    	    else if(day==6 && leap==false)
    	    arr.push_back(i);
    	    if(leap){
    	        day=((day-2)+7)%7;
    	    }
    	    else{
    	        day=(day-1+7)%7;
    	    }
    	}
    	int num=arr.size();
    while(t--){
    	    int x,y;
    	    cin>>x>>y;
    	     int u,v;
    	    cin>>u>>v;
    	    int times=(y/400)*num;
    	    y=y%400;
    	    if(x<=2)
    	    y=y-1;
    	    for(int j=0;j<arr.size();j++){
    	        if(arr[j]<=y)
    	        times++;
    	        else
    	        break;
    	    }
    	    int times2=(v/400)*num;
    	    v=v%400;
    	    if(u<2)
    	    v=v-1;
    	    for(int j=0;j<arr.size();j++){
    	        if(arr[j]<=v)
    	        times2++;
    	        else
    	        break;
    	    }
    	    cout<<times2-times<<endl;	      
    	}
    	return 0;
    }

Editorial should also have explanation

Thanks for reminding ,i have tried explaning my approach.You can check it now UPDATED

1 Like

You guys can also check out my simple solution here
I just used the below stated facts :-

  • The overlap can happen in feb only.
  • For a non-leap year, 1st feb should be sat or sun only.
  • For a leap year, 1st feb should be sat only.
  • Gregorian Calendar repeats every 400 years.

Then i looped over the given year to next 400 years and then looped over left years.