2020 is the current year and is a leap year starting on Wednesday of the Gregorian calendar, the 2020th year of the Common Era (CE) and Anno Domini (AD) designations, the 20th year of the 3rd millennium, the 20th year of the 21st century, and the 1st year of the 2020s decade.
2020 has been designated as Year of the Nurse and Midwife by the World Health Organization. The United Nations has declared 2020 as the International Year of Plant Health. 2020 has been designated as International Year of Sound by the International Commission for Acoustics.
Because there are so many unforgettable things happening in 2020, somebody has now proposed that all dates with a substring containing "202" to be designated as the Day for Disaster Reduction. We represent a date in the format YYYYMMDD (for example, 21110202), then if 202 is a substring of this date, this day is the Day for Disaster Reduction.
Please write a program to compute how many Days for Disaster Reduction are there in all the dates between Y_1Y1M_1M1D_1D1 and Y_2Y2M_2M2D_2D2 (both inclusive)? Note that you should take leap years into consideration. A leap year is a year that can be divided by 400 or can be divided by 4 but can't be divided by 100. A leap year has 2929 days in February, instead of the normal 2828 days.
Input
The input contains multiple cases. The first line of the input contains a single positive integer TT (1 \le T \le 10^51≤T≤105), the number of cases.
The first and only line of each case contains six integers Y_1,M_1,D_1,Y_2,M_2,D_2Y1,M1,D1,Y2,M2,D2, which are described in the problem statement above.
It's guaranteed that Y_1Y1M_1M1D_1D1 is not larger than Y_2Y2M_2M2D_2D2. Both Y_1Y1M_1M1D_1D1 and Y_2Y2M_2M2D_2D2 are between 20000101 and 99991231, and both dates are valid.
We kindly remind you that the size of the input and output of this problem can be large, so it's recommended to use a faster I/O method. For example, in C++, you can use scanf/printf instead of cin/cout.
Output
For each case, print a single line containing a single integer, the answer.
#include <bits/stdc++.h>
using namespace std;
int pre[10000][12][40];
int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_run(int year)
{
if (year % 400 == 0) return true;
if (year % 100 != 0 && year % 4 == 0) return true;
return false;
}
inline bool is_202(int num)
{
string str = "";
bool flag1 = false;
bool flag2 = false;
while (num)
{
int temp = num % 10;
if (temp == 2) flag1 = true;
if (temp == 0) flag2 = true;
num /= 10;
str += (temp + '0');
}
if (!flag1 || !flag2) return false;
for (int i = 0; i < str.size(); i++)
{
if (i + 3 - 1 <= str.size() - 1)
{
if (str.substr(i, 3) == "202") return true;
}
}
return false;
}
signed main()
{
int T;
int yea = 2000, mon = 1, da = 1;
int ans = 0;
while (true)
{
int num = yea * 10000 + mon * 100 + da;
if (is_202(num)) ans++;
pre[yea][mon][da] = ans;
if (yea == 9999 && mon == 12 && da == 31)
break;
if (is_run(yea))
month[2] = 29;
else
month[2] = 28;
if (da + 1 <= month[mon])
{
da += 1;
}
else if (mon + 1 <= 12)
{
da = 1;
mon += 1;
}
else
{
da = 1;
mon = 1;
yea++;
}
}
scanf("%lld", &T);
while (T--)
{
int year1, month1, day1, year2, month2, day2;
scanf("%lld%lld%lld%lld%lld%lld", &year1, &month1, &day1, &year2,&month2, &day2);
int ans = pre[year2][month2][day2] - pre[year1][month1][day1];
if (is_202(year1 * 10000 + month1 * 100 + day1)) ans++;
printf("%lld\n", ans);
}
return 0;
}