2016 浙江省赛 The 13th Zhejiang Provincial Collegiate Programming Contest D题 循环节



链接:戳这里


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


思路:

这种类型的题肯定是有一个循环的,先暴力打表求出对应的周期

发现每400年一个循环,且400年里满足条件的日期有2058个

具体看代码吧


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int T;
ll Y,M,D,n;
int a1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int a2[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
    scanf("%d",&T);
    while(T--){
        cin>>Y>>M>>D>>n;
        n--;
        Y+=n/2058*400;
        n=n%2058;
        if(n==0) {
            cout<<Y<<" "<<M<<" "<<D<<endl;
            continue;
        }
        ll YY=Y,DD=D,MM=M;
        n++;
        int last=D,f=0,yue,k;
        for(ll i=Y;n;i++){
            if(!f) f=1,yue=M;
            else yue=1;
            if(i%400==0 || (i%4==0 && i%100!=0) ){
                for(int j=yue;j<=12&&n;j++){
                    for(k=last;k<=a1[j]&&n;k+=7){
                        if(k==1 || k==11 || k==21){
                            n--;
                            if(n==0){YY=i;MM=j;DD=k;}
                        }
                    }
                    last=k%a1[j];
                }
            } else {
                for(int j=yue;j<=12&&n;j++){
                    for(k=last;k<=a2[j]&&n;k+=7){
                        if(k==1 || k==11 || k==21){
                            n--;
                            if(n==0){YY=i;MM=j;DD=k;}
                        }
                    }
                    last=k%a2[j];
                }
            }
        }
        cout<<YY<<" "<<MM<<" "<<DD<<endl;
    }
    return 0;
}




你可能感兴趣的:(2016 浙江省赛 The 13th Zhejiang Provincial Collegiate Programming Contest D题 循环节)