Problem:
Age Difference
A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the difference of their ages will hit a maximum value even though the months move on forever.
Given the birthdays for the two, make a program to report the maximum difference between their ages. The age increases by one at the moment the birthday begins. If the birthday coincides with the 29
th
of February in a leap year, the age increases at the moment the 1
st
of March arrives in non-leap years.
Input
The input is given in the following format.
y_1
m_1
d_1
y_2
m_2
d_2
The first and second lines provide Hatsumi’s and Taku’s birthdays respectively in year
y_i
(1 ≤
y_i
≤ 3000), month
m_i
(1 ≤
m_i
≤ 12), and day
d_i
(1 ≤
d_i
≤ D
max
) format. Where
D
max
is given as follows:
28 when February in a non-leap year
29 when February in a leap-year
30 in April, June, September, and November
31 otherwise.
It is a leap year if the year represented as a four-digit number is divisible by 4. Note, however, that it is a non-leap year if divisible by 100, and a leap year if divisible by 400.
Output
Output the maximum difference between their ages.
Sample Input 1
1999 9 9
2001 11 3
Sample Output 1
3
In this example, the difference of ages between them in 2002 and subsequent years is 3 on the 1
st
of October, but 2 on the 1
st
of December.
Sample Input 2
2008 2 29
2015 3 1
Sample Output 2
8
In this example, the difference of ages will become 8 on the 29
th
of February in and later years than 2016.

Submitted solutions:

code_snippet: #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

bool isuru(int y){
    if(y % 400 == 0) return true;
    else if(y % 100 == 0) return false;
    else if(y % 4 == 0) return true;
    else return false;
}

signed main(){
    
    int y1, m1, d1; cin >> y1 >> m1 >> d1;
    int y2, m2, d2; cin >> y2 >> m2 >> d2;

    int cury = 1;
    int curm = 1;
    int curd = 1;
    int age1 = 0;
    int age2 = 0;

    bool isuru1 = false;   
    bool isuru2 = false;
    if(m1 == 2 and d1 == 29 and isuru(y1)) isuru1 = true;
    if(m2 == 2 and d2 == 29 and isuru(y2)) isuru2 = true;


    int ans = 0;
    while(cury < 10000){
        if(cury >= y1 and curm == m1 and curd == d1) age1++;
        if(isuru1 and !isuru(cury) and cury >= y1 and curm == 3 and curd == 1) age1++;

        if(cury >= y2 and curm == m2 and curd == d2) age2++;
        if(isuru2 and !isuru(cury) and cury >= y2 and curm == 3 and curd == 1) age2++;
        
        //cerr << cury << " " << curm << " " << curd << " " << age1 << " " << age2 << endl;       

        ans = max(ans, (int)abs(age1 - age2));
        
        curd++;
        if(!isuru(cury) and curm == 2 and curd == 29){
            curd = 1;
            curm++;
        }else if(isuru(cury) and curm == 2 and curd == 30){
            curd = 1;
            curm++;
        }else if((curm == 4 or curm == 6 or curm == 9 or curm == 11) and curd == 31){
            curd = 1;
            curm++;
        }else if(curd == 32){
            curd = 1;
            curm++;
        }

        if(curm == 13){
            curm = 1;
            cury++;
        }
    }

    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3104
final_rank: 1

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	tuple<int,int,int> a, b, now;
	int x, y, z;
	cin >> x >> y >> z; a = tie(x, y, z);
	cin >> x >> y >> z; b = tie(x, y, z);
	if(a > b) swap(a, b);
	if(a == b){
		cout << 0 << endl;
		return 0;
	}
	auto inc = [](tuple<int,int,int> &x){
		auto numd = [](int y, int m){
			const int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
			if(m != 2) return a[m - 1];
			if(y % 400 == 0 || y % 4 == 0 && y % 100) return 29;
			return 28;
		};
		
		int y, m, d; tie(y, m, d) = x;
		if(++d > numd(y, m)){
			d = 1;
			if(++m > 12) m = 1, y++;
		}
		x = tie(y, m, d);
	};
	auto ok = [](tuple<int,int,int> b, tuple<int,int,int> a){
		if(get<1>(a) == get<1>(b) && get<2>(a) == get<2>(b)) return 1;
		if(get<1>(a) == 2 && get<2>(a) == 29 && 
			get<1>(b) == 3 && get<2>(b) == 1 &&
			!(get<0>(b) % 400 == 0 || get<0>(b) % 4 == 0 && get<0>(b) % 100))
		return 1;
		return 0;
	};
	
	now = a;
	int ya = 0, yb = 0, ans = 0;
	while(now < b){
		inc(now);
		if(ok(now, a)) ya++;
		ans = max(ans, ya - yb);
	}
	//dbg(get<0>(now),get<1>(now),get<2>(now));
	rep(it, 365 * 4400){
		inc(now);
		if(ok(now, a)) ya++;
		if(ok(now, b)) yb++;
		ans = max(ans, ya - yb);
	}
	cout << ans << endl;
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3160
final_rank: 2

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	tuple<int,int,int> a, b, now;
	int x, y, z;
	cin >> x >> y >> z; a = tie(x, y, z);
	cin >> x >> y >> z; b = tie(x, y, z);
	if(a > b) swap(a, b);
	if(a == b){
		cout << 0 << endl;
		return 0;
	}
	auto inc = [](tuple<int,int,int> &x){
		auto numd = [](int y, int m){
			const int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
			if(m != 2) return a[m - 1];
			if(y % 400 == 0 || y % 4 == 0 && y % 100) return 29;
			return 28;
		};
		
		int y, m, d; tie(y, m, d) = x;
		if(++d > numd(y, m)){
			d = 1;
			if(++m >= 12) m = 1, y++;
		}
		x = tie(y, m, d);
	};
	auto ok = [](tuple<int,int,int> b, tuple<int,int,int> a){
		if(get<1>(a) == get<1>(b) && get<2>(a) == get<2>(b)) return 1;
		if(get<1>(a) == 2 && get<2>(a) == 29 && 
			get<1>(b) == 3 && get<2>(b) == 1 &&
			!(get<0>(b) % 400 == 0 || get<0>(b) % 4 == 0 && get<0>(b) % 100))
		return 1;
		return 0;
	};
	
	now = a;
	int ya = 0, yb = 0, ans = 0;
	while(now < b){
		inc(now);
		if(ok(now, a)) ya++;
		ans = max(ans, ya - yb);
	}
	//dbg(get<0>(now),get<1>(now),get<2>(now));
	rep(it, 365 * 4400){
		inc(now);
		if(ok(now, a)) ya++;
		if(ok(now, b)) yb++;
		ans = max(ans, ya - yb);
	}
	cout << ans << endl;
	
	return 0;
}
accuracy: 0.125
time_ms: 10
memory_kb: 3160
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

int days(int y,int m,int d){
  if(m<3)y--,m+=12;
  return 365*y+y/4-y/100+y/400+(153*m+8)/5+d;
}

void ymd(int day,int &y,int &m,int &d){
  int lo=-1,hi=100000,mid;
  while(lo+1<hi){
    mid=(lo+hi)/2;
    if(days(mid,12,31)<day)lo=mid;
    else hi=mid;
  }
  y=hi;
  for(m=1;m<12;m++)if(days(y,m+1,1)>day)break;
  d=day-days(y,m,0);
}

int main() {
  int y1,m1,d1,y2,m2,d2,a1=-1,a2=-1,ans=-1;
  cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
  int s=min(days(y1,m1,d1),days(y2,m2,d2));
  for(int i=s; i<=s+3500*366; i++) {
    int y,m,d;
    ymd(i,y,m,d);
    if(i>=days(y1,m1,d1)) {
      if(m==m1&&d==d1) a1++;
      else if(m1==2&&d1==29) {
        int yy,mm,dd;
        ymd(i-1,yy,mm,dd);
        if(mm==2&&dd==28) a1++;
      }
    }
    if(i>=days(y2,m2,d2)) {
      if(m==m2&&d==d2) a2++;
      else if(m2==2&&d2==29) {
        int yy,mm,dd;
        ymd(i-1,yy,mm,dd);
        if(mm==2&&dd==28) a2++;
      }
    }
    if(a1>=0&&a2>=0) ans=max(ans,abs(a1-a2));
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3096
final_rank: 2