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的另一种常见模式是对流式数据进行所 ...
随机推荐
- mysql中index与Multiple-Column Indexes区别与联系
索引对提升SELECT/UPDATE语句查询速度有着立竿见影的效果,有索引和无索引,查询速度往往差几个数量级. 本次讨论一下index(每列作为一个索引,单列索引)和Multiple-Column I ...
- React Native控件之Picker
1. import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, Picker, } fro ...
- 51nod 1042 数字0-9的数量 数位dp
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-1 ...
- Linux基础※※※※连接XShell到Ubuntu 14.04 LTS
参考链接: 博客链接1:http://blog.csdn.net/lichangzai/article/details/39379153 百度百科:http://baike.baidu.com/lin ...
- go 修改字符串
在Go中字符串是不可变的,例如下面的代码编译时会报错:cannot assign to s[] var s string = "hello" s[] = 'c' 但如果真的想要修改 ...
- ubuntu16.04 kinetic 安装 robot-pose-publisher
sudo apt-get install ros-kinetic-robot-pose-publisher
- C++ 多态性和虚函数
2017-06-27 19:17:52 C++面向对象编程的一个重要的特性就是多态性,而多态性的实现需要依赖虚函数的帮助. 一.多态的作用: 隐藏实现细节,使得代码能够模块化: 接口重用,实现“一个接 ...
- YII第三步,日志开启
YII第三步,日志开启 index.php入口文件配置: defined('YII_DEBUG') or define('YII_DEBUG',true); cofig/main.php 'prelo ...
- 通过修改manifest文件来解决Vista/Win7/Win8/win10下应用程序兼容性问题
https://www.cnblogs.com/snowbook/p/5363990.html
- Sqlite3,维基百科中的练习:
https://en.wikibooks.org/wiki/SQL_Exercises/The_computer_store 两个相连的表格 Manufactures: code, name prod ...