http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3326
给出一个时间区间,输出里面月份和日期都是质数的时间总数。
#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000000+7
using namespace std;
typedef long long ll;
#define MaxVal 20000+20
ll can[4000][13][32];
int isr[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
bool cheakyear(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0)
return true;
return false;
}
bool isPrime[40];
int main()
{
int T;
cin >> T;
memset(isPrime, false, sizeof isPrime);
memset(can, 0, sizeof can);
for (int i = 2; i <= 32; i++)
{
int flag = 1;
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0){ flag = 0; break; }
}
isPrime[i] = (flag == 1) ? true : false;
}
int cot = 0;
for (int year = 1000; year <= 3000; year++)
{
if (cheakyear(year))isr[2] = 29;
else isr[2] = 28;
for (int month = 1; month <= 12; month++)
{
for (int day = 1; day <= isr[month]; day++)
{
if (isPrime[day] && isPrime[month])cot++;
can[year][month][day] = cot;
}
}
}
while (T--)
{
int y1, y2, m1, m2, d1, d2;
scanf("%d %d %d %d %d %d", &y1, &m1, &d1, &y2, &m2, &d2);
int ans = can[y2][m2][d2] - can[y1][m1][d1];
if (isPrime[m1] && isPrime[d1])ans++;
printf("%d\n", ans);
}
return 0;
}