您的位置:首页 > 其它

ZOJ-3939-The Lucky Week【13th浙江省赛】【找规律】

2016-04-24 10:09 316 查看

ZOJ-3939-The Lucky Week

Time Limit: 2 Seconds      Memory Limit: 65536 KB


Edward, the headmaster of the Marjar University, is very busy every day and always forgets the date.

There was one day Edward suddenly found that if Monday was the 1st, 11th or 21st day of that month, he could remember the date clearly in that week. Therefore, he called such week “The Lucky Week”.

But now Edward only remembers the date of his first Lucky Week because of the age-related memory loss, and he wants to know the date of the N-th Lucky Week. Can you help him?

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:

The only line contains four integers Y, M, D and N (1 ≤ N ≤ 109) indicating the date (Y: year, M: month, D: day) of the Monday of the first Lucky Week and the Edward’s query N.

The Monday of the first Lucky Week is between 1st Jan, 1753 and 31st Dec, 9999 (inclusive).

Output

For each case, print the date of the Monday of the N-th Lucky Week.

Sample Input

2

2016 4 11 2

2016 1 11 10

Sample Output

2016 7 11

2017 9 11

题目链接:ZOJ-3939

题目大意:幸运的星期指,星期一为每个月的1 or 11 or 21号。给出第一个幸运星期的时间,问第n个的日期

题目思路:比赛的时候,这道题卡了最久时间。因为刚开始题意理解错了,以为第n个最大是9999年的,于是SF了。

然后是循环节找了很久。。 循环节为400 知道了循环节就比较好写了,还有就是求年份比较绕。

暴力的时候,利用蔡勒公式就可以了

以下是代码:

//
//  3939-The Lucky Week.cpp
//  ZOJ
//
//  Created by pro on 16/4/23.
//  Copyright (c) 2016年 pro. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
#define ll long long
struct node
{
int y,m,d;
};
vector <node> ans;
map <ll,ll> mp;
bool isMonday(int year,int month,int day)
{
if(month < 3) { year -= 1;  month += 12; }
int c = int (year / 100), y = year - 100 * c;
int w = int ( c / 4) - 2 * c + y + int ( y / 4 ) +(26 * ( month + 1 ) / 10) + day - 1;
w = (w % 7 + 7) % 7;

if (w == 1) return 1;
else return 0;
}

void solve()
{
for (int i = 1600; i <= 1999; i++)
{
for (int j = 1; j <= 12; j++)
{
node tmp;
ll num = 0;
if (isMonday(i,j,1))
{
tmp.y = i,tmp.m = j,tmp.d = 1;
num = tmp.y * 10000 + tmp.m * 100 + tmp.d;
ans.push_back(tmp);
mp[num] = ans.size() - 1;
}
if (isMonday(i,j,11))
{
tmp.y = i,tmp.m = j,tmp.d = 11;
num = tmp.y * 10000 + tmp.m * 100 + tmp.d;
ans.push_back(tmp);
mp[num] = ans.size() - 1;
}
if (isMonday(i,j,21))
{
tmp.y = i,tmp.m = j,tmp.d = 21;
num = tmp.y * 10000 + tmp.m * 100 + tmp.d;
ans.push_back(tmp);
mp[num] = ans.size() - 1;
}
}
}
}
int main()
{
int t;
cin >> t;
solve();
//cout << ans.size() << endl;
while(t--)
{
int y,m,d,n;
cin >> y >> m >> d >> n;

int a = 400;
int b = ans.size();

int yy = (y - 1600) % a + 1600;  //在[1600,1999]年中属于第几年

ll num = yy * 10000 + m * 100 + d;   //将年份转化为数字
int pos = mp[num];  //找到num这个日期在[1600,1999]年中属于第几次

int e_pos = (pos + n - 1) % b; // 增加了n次之后,是在第几次

int ss = (pos + n - 1) / b; // 增加了n次之后,年份增加了多少个400年。

cout << y + ans[e_pos].y - ans[pos].y + ss * a << " " << ans[e_pos].m << " " << ans[e_pos].d << endl; //年份计算有点绕,原来的年份 + (在[1600,1999]中起始年份与终止年份的差值) + 增加了几个400年
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: