每日三题-Day6-B(ZOJ 3939 The Lucky Week 找寻环节打表)

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 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
 
 
题目大意:
定义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

*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值