In order to encourage Hiqivenfin to study math, his mother gave him a sweet candy when the day of the month was a prime number. Hiqivenfin was happy with that. But several days later, his mother modified the rule so that he could get a candy only when the day of the month was a prime number and the month was also a prime number. He felt a bit upset because he could get fewer candies. What's worse, his mother changed the rule again and he had to answer a question before he could get a candy in those days. The question was that how many candies he could get in the given time interval. Hiqivenfin wanted to cry and asked you for help. He promised to give you half of a candy if you could help him to solve this problem.
Input
There are multiple test cases. The first line of input is an integer T (0 < T <= 50), indicating the number of test cases. Then T test cases follow. The i-th line of the next T lines contains two dates, the day interval of the question. The format of the date is "yyyy mm dd". You can assume both dates are valid. Hiqivenfin was born at 1000-01-01 and would not die after 2999-12-31, so the queries are all in this interval.
Hiqivenfin didn't seem to be an earthman, but the calendar was the same as that we usually use. That is to say, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.
Output
Output the number of candies Hiqivenfin could get in the time interval. Both sides of the interval are inclusive.
Sample Input
2 1000 01 01 1000 01 31 2000 02 01 2000 03 01
Sample Output
0 10
Author: GUAN, Yao
Source: The 7th Zhejiang Provincial Collegiate Programming Contest
题意:给出两个日期,问在这两个日期之间有多少是月和日都是质数的
解题思路:分年份相同和不同两种处理。相同则先分别处理两个零头月,然后模拟中间月份。不同则分别先处理两个零头年,然后模拟中间完整年份。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int MAX=100009;
int yy1,yy2,d1,d2,r1,r2;
bool prime[32]= {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1};
int m[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int x)
{
if((x%4==0&&x%100!=0)||x%400==0) return true;
else return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d %d %d %d",&yy1,&d1,&r1,&yy2,&d2,&r2);
int sum=0;
if(yy1==yy2)
{
if(d1==d2)
{
if(prime[d1])
{
for(int i=r1; i<=r2; i++)
if(prime[i]) sum++;
}
}
else
{
if(check(yy1)) m[2]=29;
if(prime[d1])
{
for(int i=r1; i<=m[d1]; i++)
if(prime[i]) sum++;
}
if(prime[d2])
{
for(int i=1; i<=r2; i++)
if(prime[i]) sum++;
}
for(int i=d1+1; i<=d2-1; i++)
{
if(prime[i])
{
for(int j=1; j<=m[i]; j++)
if(prime[j]) sum++;
}
}
m[2]=28;
}
}
else
{
if(check(yy1)) m[2]=29;
if(prime[d1])
{
for(int i=r1; i<=m[d1]; i++)
if(prime[i]) sum++;
}
for(int i=d1+1; i<=12; i++)
{
if(prime[i])
{
for(int j=1; j<=m[i]; j++)
if(prime[j]) sum++;
}
}
m[2]=28;
if(check(yy2)) m[2]=29;
if(prime[d2])
{
for(int i=1; i<=r2; i++)
if(prime[i]) sum++;
}
for(int i=1; i<=d2-1; i++)
{
if(prime[i])
{
for(int j=1; j<=m[i]; j++)
if(prime[j]) sum++;
}
}
m[2]=28;
for(int i=yy1+1; i<=yy2-1; i++)
{
if(check(i)) sum+=53;
else sum+=52;
}
}
printf("%d\n",sum);
}
return 0;
}