Lucky and Good Months by Gregorian Calendar
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1619   Accepted: 539

Description

Have you ever wondered why normally an year has 365 days, not 400 days? Why August have 31 days, but February have only 28 days? Why there are 7 days, not 6 days, in a week? Do people in ancient time use the same calendar as we do? There are many interesting
conjectures and theories about those problems. Now we will tell you one story that may help explaining plausible answers to these questions. Using information in the story, you are then ask to solve an interesting problem using computer. Note that there are
many theories about the calendar system discussed. This problem set will tell only one of them in a simplified way.

Throughout history, people keep track of time by observing the relative positions of the earth, the moon and the sun. A day is the amount of time the earth completes a self rotation. An year is defined to be the amount of time the earth
orbits the sun. The earth takes roughly 365.242190 days to orbit the sun with some small variations. For practical purpose, a calendar year needs to have an integral number of days. Hence people need to add leap days to keep the calendar synchronized
with the sun. If you keep a calendar year to have 365 years, you need to add one more day in a leap year roughly about every 4 years. However, this kind of calendar will not be in perfect synchronization with the earth’s position orbiting the sun because it
advanced 365.25 days in average, which is slightly more than the actual period.

Depending on how accurate you can measure the period of the earth orbiting the sun, you need to invent different formulas for leap years. Several famous Western calendar systems have been invented, not to mention the more complex Oriental systems. In order
to save programmers’ efforts, we will not discuss the Oriental, such as Chinese, calendar systems. We will focus on major Western calendar systems. The earliest one may be the Julian calendar created by Julius Caesar in 46 BC. It is not accurate enough and
will have one day off every 128 years. The next one is the Astronomical Julian calendar invented by Joseph Justus Scaliger around the 16th century. Both have simple formulas to determine which year is a leap year.

The next major one is called Gregorian calendar that was invented at the year 1582 because the synchronization of the earth’s orbiting and the calendar is finally noticed by people. In this system, a leap year is dropped every 100 years unless it is every
400 years. By doing this modification, the average number of days in a calendar year is 365.2425. Note that this system is also not perfect. It adds one more day every 3289 years. There are other more modifications suggested, such as the one by Astronomer
John Herschel, the Greek Orthodox, and the SPAWAR group in the US Navy. For simplicity, people use Gregorian calendar system though it may not be perfect.

The following is the formula for the Gregorian calendar to determine whether an year is a leap year or not. An year yy > 1582 and y ≠ 1700, is a leap year if and only if

  • y is divisible by 4, and

  • y is not divisible by 100 unless it is divisible by 400.

An year y, 0 < y < 1582 is a leap year if and only if

  • y is divisible by 4.

Hence year 4 is a leap year, year 100 is a leap year, year 1900 is not a leap year, but year 2000 is a leap year. A leap year has 366 days with the extra day February 29. A non-leap year has 365 days.

During your computation, you may also want to observe the following facts about Gregorian calendar. Many calendar systems were used by people in different areas in the Western world at the same time. The current Western calendar system, primarily follows
Gregorian calendar, and is so called the Gregorian Reformation, was adopted by Britain and the possessions on September 3, 1752. For lots of reasons that we are sure you do not want to read in this problem description, 11 days are eliminated starting September
3, 1752 in order for people not to rewrite history. That is, in the Gregorian calendar, there is no days in between September 3, 1752 and September 13, 1752. Note that Rome adopted the Gregorian calendar at the year 1582, when it was invented. Also for historical
reasons, the year 1700 is declared a leap year in the Gregorian calendar. There are other variations about the Gregorian calendar system, however, we will use the one that is defined above.

A lunar month is defined to be the average time between successive new or full moons which is 29.531 days. People observe in average 12.368 full moons in an year. Unfortunately, this is also not an integral number in terms of days. Hence if we set
an year to have 12 months with each month having 30 days, we need to add several days each year. To save the trouble, an alternative way is to have the number of days in a month to alternative between 30 and 31. However, this introduces one extra day. After
lots of struggle, the Gregorian calendar defined the numbers of days in each month during a non-leap year to be 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, respectively from the first month to the 12th month. One more day is added on February in the leap
year. The names for the months in sequence from the first month — January, February, March, April, May, June, July, August, September, October, November, and December, are also very interesting and have lots of stories associated with them. For example, the
Roman Senate named the month of July after Julius Caesar to honor him for reforming their calendar. However, we do not have time to cover them here.

In ancient time, a week may have different number of days, say from 4 to 10 days. In the Gregorian calendar system, a week corresponds roughly to the moon’s quarter phase whose position can be clearly observed by people. Hence people can easily
measure a week. Also for some other reasons, such as religion, it is defined as 7 days. The names of the 7 days in sequence — Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday, all have interesting stories. However, we also do not have time
to cover them here.

Throughout history, people believe the relative positions of the stars can decide their fate. This is also true for people living in an island T. In island T, people are working from Monday through Friday every week and enjoy holidays on
every Saturday and Sunday without exceptions. There is no other holidays. From ancient tales, a month is called lucky if the last working day in this month is Friday. For examples, the last working day of September, 2006 is September 29, 2006 — which
is Friday. Hence it is lucky. The last working day of July, 2006 is July 31, 2006 — which is Monday. Hence it is not lucky. The last working day of August, 2006 is August 31, 2006 — which is Thursday. Hence it is also not lucky. It is believed that if one
eats only vegetable everyday during a lucky month, he/she will have a good fortune in getting rich.

Also from ancient tales, a month is called good if the first working day in this month is Monday. For examples, the first working day of July, 2006 is July 3, 2006 — which is Monday. Hence it is good. The first working day of October, 2006 is October
2, 2006 — which is Monday. Hence it is also good. The first working day of August, 2006 is August 1, 2006 — which is Tuesday. Hence it is not good. The first working day of September, 2006 is September 1, 2006 — which is Friday. Hence it is also not good.
It is believed that if one goes to bed before 10 PM every day during a good month, he/she will be very healthy. A month can be both good and lucky at the same time.

Given a period of time, your task is to report the number of lucky months and the number good months during this period of time using the described Gregorian calendar system.

Input

The first line contains the number of test cases w, 1 ≤ w ≤ 10. Then the w test cases are listed one by one. Each test case consists of 1 line with four numbers:

Ys Ms Ye Me

where two numbers are separated by a single blank, Ys is an integer, 0 < Ys < 10000, denoting the starting year in western style, Ms is an integer, 1 ≤ Ms ≤ 12, denoting the
starting month, Ye is an integer, 0 <Ye < 10000, denoting the ending year in western style, Me is an integer, 1 ≤ Me ≤ 12, denoting the ending month.

Note that you can be sure the month indicated by MsYs is never after the month indicated by MeYe.

Output

For each test case, output the number of lucky months and the number of good month in between the month Ms of the year Ys (including this month) and the month Me of the year Ye (including
this month) in one line. The two numbers are separated by a single blank.

Sample Input

2
2006 9 2006 9
2006 7 2006 9

Sample Output

1 0
1 1

终于A掉这个题了,简直是阅读理解。。。

总结起来的话:

//当y大于1582年且y不等于1700年的时候,它是闰年的条件是

//它能被4整除

//它不能被100整除除非它能被400整除



//当y小于等于1582年的时候它是闰年的条件是

//它能被4整除



//1752年的9月丢了11天,从3号到13号



//lucky month 这个月的最后一个工作日是星期五

//good month 这个月的第一个工作日是星期一

然后要求输出的是lucky_month与good_month的数量,包括了起始端点与结束端点。

明白了这些,这个题就变成了水题了,我自己写得有些麻烦。

代码:

//当y大于1582年且y不等于1700年的时候,它是闰年的条件是
//它能被4整除
//它不能被100整除除非它能被400整除 //当y小于1582年的时候它是闰年的条件是
//它能被4整除 //1752年的9月丢了11天,从3号到13号 //lucky month 这个月的最后一个工作日是星期五
//good month 这个月的第一个工作日是星期一
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int start_year, start_month, end_year, end_month;
int not_leap_year[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int leap_year[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; bool check_year(int x)//判断x年是不是闰年
{
if (x == 1700)
return true;
if (x >= 1582)
{
if (x % 4 == 0)
{
if (x % 400 == 0)
{
return true;
}
if (x % 100 == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
else
{
if (x % 4 == 0)
{
return true;
}
else
{
return false;
}
}
} int cal(int year, int month)
{
int i, j;
int res = 0;
for (i = 1; i <= year - 1; i++)
{
if (check_year(i))
{
for (j = 0; j < 12; j++)
{
if (i == 1752 && j == 8)
{
res += 19;
continue;
}
res += leap_year[j];
}
}
else
{
for (j = 0; j < 12; j++)
{
res += not_leap_year[j];
}
}
} if (check_year(year))
{
for (j = 0; j < month; j++)
{
if (year == 1752 && j == 8)
{
res += 19;
continue;
}
res += leap_year[j];
}
}
else
{
for (j = 0; j < month; j++)
{
res += not_leap_year[j];
}
}
return res;
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int test, i, j, ans1, ans2; scanf("%d", &test);
while (test--)
{
scanf("%d%d%d%d", &start_year, &start_month, &end_year, &end_month); ans1 = 0;
ans2 = 0;
if (start_year == end_year)
{
int num = cal(start_year, start_month - 1);//计算这之前有多少天
if (check_year(start_year))
{
for (i = start_month - 1; i <= end_month - 1; i++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
if (start_year == 1752 && i == 8)
{
num += 19;
}
else
num = num + leap_year[i];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
}
else
{
for (i = start_month - 1; i <= end_month - 1; i++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
num = num + not_leap_year[i];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
}
}
else
{
int num = cal(start_year, start_month - 1); if (check_year(start_year))
{
for (i = start_month - 1; i < 12; i++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
if (start_year == 1752 && i == 8)
{
num += 19;
}
else
num = num + leap_year[i];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
}
else
{
for (i = start_month - 1; i < 12; i++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
num = num + not_leap_year[i];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
} for (i = start_year + 1; i <= end_year - 1; i++)
{
if (check_year(i))
{
for (j = 0; j < 12; j++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
if (i == 1752 && j == 8)
{
num += 19;
}
else
num = num + leap_year[j];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
}
else
{
for (j = 0; j < 12; j++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
num = num + not_leap_year[j];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
}
} if (check_year(end_year))
{
for (i = 0; i < end_month; i++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
if (end_year == 1752 && i == 8)
{
num += 19;
}
else
num = num + leap_year[i];
//num = num + leap_year[i];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
}
else
{
for (i = 0; i < end_month; i++)
{
int temp = ((num + 1) + 5) % 7;
if (temp == 1 || temp == 6 || temp == 0)
{
ans1++;
}
num = num + not_leap_year[i];
temp = (num + 5) % 7;
if (temp == 5 || temp == 6 || temp == 0)
{
ans2++;
}
}
} } printf("%d %d\n", ans2, ans1);
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3393:Lucky and Good Months by Gregorian Calendar 年+星期 模拟的更多相关文章

  1. POJ 3393 Lucky and Good Months by Gregorian Calendar 模拟题

    题目:http://poj.org/problem?id=3393 不多说了,简单模拟题,因为粗心写错了两个字母,导致错了N遍,模拟还是一贯的恶心,代码实在不想优化了,写的难看了点.. #includ ...

  2. POJ 3393 Lucky and Good Months by Gregorian Calendar

    http://poj.org/problem?id=3393 题意 : 对于这篇长长的英语阅读,表示无语无语再无语,花了好长时间,终于读完了.题目中规定每周的周六日为假日,其他为工作日,若是一个月的第 ...

  3. poj 3393 Lucky and Good Months by Gregorian Calendar(模拟)

    题目:http://poj.org/problem?id=3393一道题目挺长的模拟题,参考了网上大神的题解. #include <iostream> #include <cstdi ...

  4. Lucky and Good Months by Gregorian Calendar - POJ3393模拟

    Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS Memory Limit: 65536K Description Have ...

  5. Lucky and Good Months by Gregorian Calendar(poj 3393)

    大致题意: 科普文一篇,文章80%都是无用信息,因为都是常识,但是又不得不看,因为有20%是常人不知道的历史常识. 定义: Goog month : 该月第一个工作日为星期一的月份 Luckly mo ...

  6. Lucky and Good Months by Gregorian Calendar(模拟)

    http://poj.org/problem?id=3393 好大的一道模拟题,直接当阅读理解看了.下面是大神写的题意,解释的好详细. 定义: Goog month : 该月第一个工作日为星期一的月份 ...

  7. 三部曲二(基本算法、动态规划、搜索)-1003-Lucky and Good Months by Gregorian Calendar

    模拟加阅读题......虽然很多事常识性的知识,但也有许多不知道的知识,关键是不读不知道那些是已经知道的那些不是,许多重要的信息零散的分布在一大坨英文里,读起来很痛苦......自己读了一遍,读的晕晕 ...

  8. poj 2346 Lucky tickets(区间dp)

    题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...

  9. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

随机推荐

  1. 实用类-<字符串与基本类型的转换>

    字符串与基本类型的转换 字符串->基本类型 int i5=Integer.parseInt("123"); System.out.println(i5); 基本类型-> ...

  2. Android游戏开发学习(5)--实现Button悬浮于与SurfaceView之上

    原文:http://daikainan.iteye.com/blog/1407355 实现Button悬浮于与SurfaceView之上实现 先看效果: 注意:你实现的SurfaceView和andr ...

  3. 发送邮件功能 Service 层

    package com.thinkgem.jeesite.modules.yudengjipush.service; import java.text.ParseException; import j ...

  4. iOS 批量上传图片的 3 种方法

    AFNetworking 在去年年底升级到了 3.0.这个版本更新想必有很多好处,然而让我吃惊的是,它并没有 batch request 接口.之前的 1.x 版本.2.x 版本都实现了这个很常见的需 ...

  5. 个人相关API - 行驶方向判断函数

    calculateDrivingDirection(newCoordinates,oldCoordinates){ let o = { direction: '', deviation:null, i ...

  6. Django manager 命令笔记

    1. 新建app startapp your_app_name 2. 删除app migrate your_app_name zero 此外还需要删除整个App文件夹,并在settings.py的In ...

  7. [Linux] day03——REHL部署

    REHL 部署 Linux 树形目录结构  最顶层 根 / 在Linux中 一切皆文件 文件系统 swap / 安装方式 PXE U盘 光盘 配置安装程序 选择包   5.9    6.4

  8. 第3节 storm高级应用:2、storm与hdfs的整合工程环境准备;3、整合代码开发

    ======================================== 3.  storm与hdfs的整合使用 3.1.功能需求: 实现随机发送订单数据,从计算订单的总金额,然后将订单中的数 ...

  9. Java的clone方法效率问题

    在Java中,经常会需要新建一个对象,很多情况下,需要这个新建的对象和现有的某个对象保持属性一致. 那么,就有两种方式来实现这个对象的构造: ①通过新建一个对象,为这个对象的属性根据原有对象的属性来进 ...

  10. bzoj 4475: [Jsoi2015]子集选取

    233,扒题解的时候偷瞄到这个题的题解了,,GG 暴力发现是2^(nm),然后就是sb题了 #include <bits/stdc++.h> #define LL long long us ...