zoj3939 (playing the watch, Kim Larson's calculation formula, the method of calculating the day of the week)

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 YMD 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 

In the formula, d is the number of days in the date, m is the number of months, and y is the number of years.
Note: There is one difference in the formula from other formulas:
Consider January and February as the thirteenth and fourteenth months of the previous year. For example, if it is 2004-1-10, it is converted into: 2003-13-10 to substitute into the formula.

(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;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325943193&siteId=291194637
Kim