ZOJ 3939 The Lucky Week (400年一个周期)

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 theN-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 andN (1 ≤ N ≤ 109) indicating the date (Y: year,M: month, D: day) of the Monday of the first Lucky Week and the Edward's queryN.

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

题意:给出开始的年份,这个年份是一个lucky week ,意思是某月1号或11获21号恰好为星期一,现在问你这个
年份之后第n个luck week是几年几月
分析:首先可以发现每400年为一个周期(恰包含了400年的一个周期闰年),那么先预处理出0~400年中间所有的
lucky week ,再将n对此数量取模,就能得到答案,在处理年份答案的时候需要特殊处理一下

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f

struct node
{
    int y,m,d;
    friend bool operator < (node A,node B)
    {
        if(A.y==B.y)
        {
            if(A.m==B.m) return A.d<B.d;
            return A.m<B.m;
        }
        return A.y<B.y;
    }
} data[400005];

int p1[]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
int p2[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};

bool isp(int y)
{
    if(y%400==0||(y%4==0&&y%100!=0)) return true;
    return false;
}

void del(int &y,int &m,int &d,int &week)
{
    d--;
    if(d==0)
    {
        m--;
        if(m==0) y--,m=12;
        if(isp(y)) d=p1[m];
        else d=p2[m];
    }
    week--;
    if(week==0) week=7;
}

int main()
{
    int pos=0;
    int y=400,m=12,d=11,week=1;
    while(y>=0)
    {
        if(y>=0&&y<400)
        {
            if(week==1&&(d==1||d==11||d==21))
            {
                data[pos].y=y;
                data[pos].m=m;
                data[pos].d=d;
                pos++;
            }
        }
        del(y,m,d,week);
    }
    sort(data,data+pos);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int y,m,d,n;
        scanf("%d%d%d%d",&y,&m,&d,&n);
        n--;
        int year=y/400;
        y%=400;
        int p;
        for(int i=0; i<pos; i++)
        {
            if(data[i].y==y&&data[i].m==m&&data[i].d==d)
            {
                p=i;
                break;
            }
        }
        year+=((p+n)/pos);
        p=((n%pos)+p)%pos;
        year=year*400+data[p].y;
        printf("%d %d %d\n",year,data[p].m,data[p].d);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值