E - An Awful Problem 求两段时间内满足条件的天数//lxm
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
这个一开始想复杂了后来想想只要开一个三维数组把到这天的可以拿糖的个数放上去,至于两者之间直接剪一下就可,但是要注意如果起始点是素数月、天要总数++,忘了这个点不小心wa了一发
#include <iostream>
#include <string.h>
#include <cmath>
using namespace std;
struct node
{
int year;
int month;
int day;
}st,ed;
int check(int year)
{
if((year%==&&year%!=)||year%==)
return ;
else
return ;
}
int isprime(int n)
{
int flag=;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
flag=;
break;
}
}
if((flag&&n!=)||n==)return ;
else
return ;
}
int month[]={,,,,,,,,,,,,};
int ans[][][];
void solveit()
{
int sum=;
for(int y=;y<=;y++)
{
if(check(y))
month[]=;
else
month[]=;
for(int m=;m<=;m++)
{
for(int d=;d<=month[m];d++)
{
if(isprime(m)&&isprime(d))
sum++;
ans[y][m][d]=sum;
}
}
}
}
int main()
{
int t;
cin>>t;
memset(ans, , sizeof(ans));
solveit();
while (t--) {
cin>>st.year>>st.month>>st.day;
cin>>ed.year>>ed.month>>ed.day;
int days=ans[ed.year][ed.month][ed.day]-ans[st.year][st.month][st.day];
if(isprime(st.month)&&isprime(st.day))days++;
cout<<days<<endl;
}
return ;
}
E - An Awful Problem 求两段时间内满足条件的天数//lxm的更多相关文章
- PHP 求两个日期之间相差的天数、月数
<?php /** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day ...
- MySql 求一段时间范围内的每一天,每一小时,每一分钟
平常经常会求一段时间内的每一天统计数据,或者每一时点的统计数据.但是mysql本身是没有直接获取时点列表的函数或表.下面是自己用到的一些方法,利用临时变量和一个已存在的比较多数据(这个需要根据实际情况 ...
- 【u124】环状最大两段子段和
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. ...
- js 求两个日期之间相差天数
//求两个日期之间的相差天数 function daysBetween(DateOne, DateTwo) { var OneMonth = DateOne.substring(5, DateOne. ...
- java 求取某一段时间内的每一天、每一月、每一年
1.求取某一段时间内的每一天 Date date0 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-01" ...
- POJ 2479 两段连续最大和
题目大意: 在一组数中,找到连续的两段 , 是这两段相加和达到最大 这里利用dp[2][N]的数组保存所有的状态 dp[0][i]表示取到第i个数时只取了一段的最大和,第i个数是一定要被取到的 dp[ ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 求两条线段交点zz
"求线段交点"是一种非常基础的几何计算, 在很多游戏中都会被使用到. 下面我就现学现卖的把最近才学会的一些"求线段交点"的算法说一说, 希望对大家有所帮助. 本 ...
- storm入门(二):关于storm中某一段时间内topN的计算入门
刚刚接触storm 对于滑动窗口的topN复杂模型有一些不理解,通过阅读其他的博客发现有两篇关于topN的非滑动窗口的介绍.然后转载过来. 下面是第一种: Storm的另一种常见模式是对流式数据进行所 ...
随机推荐
- python学习站点
1.python 外部扩展网址 http://www.lfd.uci.edu/~gohlke/pythonlibs Python Extension Packages 2.web2py学习 http: ...
- Spring boot 中Hibernate 使用
spring.jpa.properties.hibernate.hbm2ddl.auto=有四种配置方式,分别如下: 是hibernate的配置属性,其主要作用是:自动创建.更新.验证数据库表结构.该 ...
- 简单了解SQL(结构化查询语言)
简单了解SQL(结构化查询语言) 年10月,美国国家标准学会对SQL进行规范后,以此作为关系式数据库管理系统的标准语言(ANSI X3. 135-1986),1987年得到国际标准组织的支持下成为国际 ...
- const 学习笔记
#include<stdlib.h> #include<iostream> using namespace std; int main(){ // const 仅仅起到是否为常 ...
- NGUI实现简单的倒计时组件
using System; using UnityEngine; public enum ETimerType { CommonFormat, // 78 77 76 75 ... TimeForma ...
- 小行星碰撞 Asteroid Collision
2018-08-07 11:12:01 问题描述: 问题求解: 使用一个链表模拟栈,最后的状态一定是左侧全部是负值,表示的是向左飞行,右侧的全部是正值,表示的是向右飞行. 遍历整个数组,对于每个读到的 ...
- Hibernate实例
Hibernate实例 一.Hibernate简介 Hibernate是简化项目中连接数据库的一个框架工具 Hibernate是Java领域类技术成熟稳定的ORM框架 * ORM是对象关系映射 * 使 ...
- Solaris 11, gcc 的安装
注意点在于, 头文件在另外一个包system/header里,需要另外安装 pkg pkg install system/header
- 关于C和C++
最开始学的就是C和C++,但只是学过,根本就不知道怎么使用. 后来接触了Python和Perl才知道怎么将编程应用于实际需求当中,读取文件,存放到数据结构,处理,输出. 但脚本语言有其固有的缺点,不能 ...
- English trip -- VC(情景课)8 D Reading
Listen and read. Shop Smart [smɑːt] Employee of the Month: Sara['særə] (萨拉) Lopez(洛佩斯) Congratulati ...