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
Question meaning: If the date of a day is 1,11,21, and this day is Monday, then this week is the lucky week, now give a date (the first lucky week Monday) and n, ask you the nth lucky week What time of the week is, print the Monday of the week.
At the beginning, we still thought that there must be a cycle festival, then leap years will break the balance, and we don’t know the next Monday will become the day of the week, then 400*7 must be a cycle festival, and then I want to look at the table of the first 400 years. , and then play another 400 years later to see the law, and the first one is found. At that time, we started to play the watch in 1753. Later, I thought about it from 0 to 399, and then I tried it. Here is a method to calculate the day of the week for a certain day.
Kim Larson's formula: W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7
(taken from Baidu Encyclopedia) and the d in this formula represents the date + 1, and w is directly the day of the week.
Here is the basic board:
#include<stdio.h> intmain() { int y,m,d; while(~scanf("%d%d%d",&y,&m,&d)) { if(m==1||m==2) { m+=12; and--; } int w=(d+1+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; printf("%d\n",w); } return 0; }
Code for this question:
#include<stdio.h> #include<string.h> #include<math.h> #include<queue> #include<algorithm> #include<iostream> #include<map> #define inf 0x3f3f3f3f #define ll long long using namespace std; int v[3000][500]; int r; void aa() { int yy,mm,dd; r=0; for(int i=0;i<400;i++) { for(int j=1;j<=12;j++) { for(int k=1;k<30;k+=10) { yy=i; mm=j; dd=k; if(mm==1||mm==2) mm+=12,yy--; int w=(dd+1+2*mm+3*(mm+1)/5+yy+yy/4-yy/100+yy/400)%7; if(w==1) { v[r][1]=i; v[r][2]=j; v[r][3]=k; r++; } } } } } intmain() { aa(); int t,y,m,d,n; while(~scanf("%d",&t)) { while(t--) { scanf("%d%d%d%d",&y,&m,&d,&n); n--; int res = y / 400; y%=400; int flag; for(int i=0; i<r; i++) { if(v[i][1]==y&&v[i][2]==m&&v[i][3]==d) flag=i; } res+=(flag+n)/2058; flag=(flag+n)%2058; res=res*400+v[flag][1]; printf("%d %d %d\n",res,v[flag][2],v[flag][3]); } } return 0; }