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; }
相关文章推荐
- 第四次C++作业
- android仿win8
- Labview之ActiceX学习
- 把本地文件上传到github
- poj 1695(三维dp)
- android文件存储
- Android 5.X 新特性
- 线程
- 推荐一个linux下的web压力测试工具神器webbench
- Citrix Receive问题解决2
- 庖丁解牛之仿《闪传》实现文件传输(下)
- hdu4722 Good Numbers(数位dp)
- Oracle之深入浅出(二)--SQL原理、解释计划与执行计划
- 微软面试题:五个囚犯抓绿豆
- Vim
- 期中总结
- Linux下四款Web服务器压力测试工具(http_load、webbench、ab、siege)介绍
- 使用C#调用windows API(从其它地方总结来的,以备查询)
- 当当的面经
- Leetcode——047