ZOJ-3939-The Lucky Week(日期循环节)
2016-04-24 17:19
288 查看
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?
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).
可以观察到1753年1月1日是周一,然后我们知道大概再过2800年就会重复。于是预处理出1753.01.01后面2800年的所有lucky week。之后根据循环节算一下就好了。
实际循环节400就可以- -。。。
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
D. THE LUCKY WEEK
可以观察到1753年1月1日是周一,然后我们知道大概再过2800年就会重复。于是预处理出1753.01.01后面2800年的所有lucky week。之后根据循环节算一下就好了。实际循环节400就可以- -。。。
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <string> #include <cstring> #include <math.h> #include <map> #define INF 0x3f3f3f3f using namespace std; #define MAXN 200005 #define MAXM 10005 map<long long,int> q; int sum; struct node { int year,month,day; node(int a,int b,int c){year=a;month=b;day=c;} }; vector<node> data; bool is_monday(int year,int month,int day) { if(month<3){year-=1;month+=12;} int c=year/100; int y=year-100*c; int w =c/4-2*c+y+y/4+26*(month+1)/10+day-1; w=(w%7+7)%7; if(w==1)return 1; return 0; } void make_form() { for(int i=1500; i<4300; ++i) for(int j=1; j<=12; ++j) for(int k=1; k<30; k+=10) if(is_monday(i,j,k)) { long long f=i*10000+j*100+k; node poi(i,j,k); q[f]=sum++; data.push_back(poi); } } int main() { int T,month,year,day,n; sum=0; make_form(); cin>>T; while(T--) { scanf("%d%d%d%d",&year,&month,&day,&n); int y=(year-1500)%2800+1500; long long poi=y*10000+month*100+day; int f=(q[poi]+n-1)/sum; int k=(q[poi]+n-1)%sum; node a=data[k]; cout<<year+a.year-data[q[poi]].year+f*2800<<" "<<a.month<<" "<<a.day<<endl; } return 0; }
相关文章推荐
- Gradle学习系列之八——构建多个Project
- Gradle学习系列之七——依赖管理
- Gradle学习系列之六——使用Java Plugin
- 我的面经
- mysql表的字符集gbk转换成utf8测试
- 有关软件工程的问题的分析和讨论及课后的作业7
- MAC下搭建ionic环境
- str_replace.php
- JMockit学习笔记
- 布尔运算(Boolean Operations)
- (最小树形图 朱刘算法) poj 3164
- 剑指Offer--027-二叉搜索树与双向链表
- Gradle学习系列之五——自定义Property
- Gradle学习系列之四——增量式构建
- Sublime——Sublime中增加格式化代码的快捷键
- MongoDB 3.0创建用户
- 简洁强悍的杨辉三角
- Git使用教程
- https
- binbinyang---Android自动检测更新功能的实现--杨彬