题目:
Time Limit: 2000MS |
| Memory Limit: 65536KB |
| 64bit IO Format: %lld & %llu |
Description
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
Source
The 13th Zhejiang Provincial Collegiate Programming Contest
题目大意:
给一日期,如果这个月的1,11,21号是星期一没那么那一天是是幸运的,求所给的日期后第n个幸运天的日期
题目思路:
1、因为时间只有2ns,而且数据为10^9,所以不能一天一天的判断
2、所以从月开始算起,求出当月的第一天是星期几,当月加有
3、通过月份来求出年份
4、.通过年份求出最小的循环(400年为一次)
程序:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[13]={0,3,0,3,2,3,2,3,3,2,3,2,3};
bool is_run(int );
bool yue[7]={0,1,1,0,0,1,0};
int nian[7]={4,4,5,4,6,6,6};
int run_nian[7]={3,4,7,4,4,7,5};
int main()
{
int ci;
scanf("%d",&ci);
while(ci--)
{
int year,month,day,n;
scanf("%d%d%d%d",&year,&month,&day,&n);
bool f=is_run(year);
int da=(35-day+2)%7;
while(n>2058)
{
n-=2058;
year+=400;
}
while(n)
{ //cout<<year<<month<<da<<endl;
if(month>12)
{
year++;
month=1;
f=is_run(year);
}
if(n>yue[da])
{
n-=yue[da];
da=(da+a[month])%7;
if(month==2&&is_run(year))
da=(da+1)%7;
month++;
}
else
{
if(da==1)
day=1;
else if((da+10)%7==1)
day=11;
else if((da+20)%7==1)
day=21;
break;
}
}
printf("%d %d %d\n",year,month,day);
}
return 0;
}
bool is_run(int nian)
{
return nian%400==0||nian%4==0&&nian%100?1:0;
}