您的位置:首页 > 其它

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?

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