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
题目大意:
定义LuckyWeek为,如果每月的1、11、21号为周一,那么这周为幸运周。
给你第一个幸运周的周一的日期,问你第n个幸运周的周一的日期。
思路:
1<=n<=1e9 直接暴力肯定超时。
考虑到,闰年的话,400年一周期。有没有可能400年是一个循环,即如果今天是周一,400年后的今天仍然是周一?
验证:计算400年有多少天,然后模7如果得0,那么就是循环节。
400年有多少天,就是365*400,加上四年一闰,百年不闰,四百再闰。
那就是365*400+100-4+1=146097,146097%7=0,证毕。
打表出从1753年1月1日开始,400年内的幸运周,一共有2058个。记录每个幸运周的年月日。
然后将输入的第一个幸运周和n去掉循环节,再求解。
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int ddd[2100][5]; int getDays(int yy,int mm,int dd) { int Y=yy-1753; int days=Y*365+Y/4-Y/100+Y/400; if(Y%400>(2000-1753))days++; if(Y%100>(1800-1753))days--; switch(mm) { case 12: days+=30; case 11: days+=31; case 10: days+=30; case 9: days+=31; case 8: days+=31; case 7: days+=30; case 6: days+=31; case 5: days+=30; case 4: days+=31; case 3: days+=28; case 2: days+=31; } days+=dd; if((yy%4==0&&yy%100!=0||yy%400==0)&&mm>2)days++; return days-1; } int init() { int num=0; for(int i=1753; i<2153; i++) { for(int j=1; j<13; j++) { for(int k=1; k<=21; k+=10) { int sum=getDays(i,j,k); if(sum%7==0) { ddd[num][0]=i; ddd[num][1]=j; ddd[num][2]=k; num++; } } } } //printf("num:%d\n",num); } int main() { int t; scanf("%d",&t); init(); while(t--) { int yy,mm,dd,n; scanf("%d%d%d%d",&yy,&mm,&dd,&n); int YY,MM,DD; YY=1753+(yy-1753)%400; int tmp=0; for(int i=0; i<2060; i++) { if(ddd[i][0]==YY&&ddd[i][1]==mm&&ddd[i][2]==dd) tmp=i; } int p=n%2058; int k = (p+tmp)/2058; p = (p+tmp+2057)%2058; k+=(n-1)/2058; k+=(yy-1753)/400; printf("%lld %d %d\n",(long long)ddd[p][0]+400*(long long)k,ddd[p][1],ddd[p][2]); } return 0; } /* 100 1753 1 1 1 1753 1 1 1000000000 1753 1 1 2057 1753 1 1 2058 1753 1 1 2059 1753 1 1 4116 1753 1 1 4117 2153 1 1 2057 2153 1 1 2058 2153 1 1 2059 1753 1 1 194365212 10 1 2152 9 11 2152 12 11 2153 1 1 2552 12 11 2553 1 1 2552 9 11 2552 12 11 2553 1 1 */