ZOJ 3326 An Awful Problem(模拟)

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值