520的今天表白CUIT6301所有成员。此事无关风与月。那一份执着与坚持,愈久弥新。
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
我是一只酸菜鱼,又酸又菜又多余。题意:找出第N个满足从所给日期开始算的周一是1、11、21号的日期。场上wa了一两个小时的我,让我独自在风中飘零。暴力算,超时+wa会紧跟你。AC思路:400年为一个周期,求出400年有多少天周一是1、11、21号(cnt计数),然后用N/cnt,求出有多少个是满足周期的(Count),在Y的基础上加上400Count,剩下的年月日,他肯定不会超过一个周期。Then就直接枚举过。*
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,month,day,m,cnt;
long long int year;
int a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
void init(){
int cas = 0;
for(int i = 1753 ; i <= 1753 + 399 ; i++){//四00年一个周期
((i % 100 != 0 && i % 4 == 0) || i % 400 == 0) ?a[2] = 29:a[2] = 28;
for(int j = 1 ; j <= 12 ; j++)
for(int k = 1 ; k <= a[j] ; k++){
++cas;
if((k == 1 || k == 11 || k == 21) && cas % 7 == 1) ++cnt;
}
}
}
int main(){
init();
scanf("%d",&t);
for(int pp = 0 ; pp < t ; pp++){
scanf("%lld%d%d%d",&year,&month,&day,&m);
int Count = m/cnt;
int remain = m - Count*cnt;
year += 400*Count;
int cas = 0,k,j;
// cout<<year<<endl;
// cout<<"aaaaaaaa"<<endl;
for(long long int i = year ; i <= year + 400 ; i++){
((i % 100 != 0 && i % 4 == 0) || i % 400 == 0)? a[2] = 29:a[2] = 28;
for(j = month ; j <= 12 ; j++){
for(k = day; k <= a[j] ; k++){
++cas;
if((k == 1 || k == 11 || k == 21) && cas % 7 == 1){
remain--;
// printf("%lld %d %d\n",year,month,day);
}
if(remain == 0){
year = i;
month = j;
day = k;
break;
}
}
if(!remain) break;
day = 1;
}
if(!remain) break;
month = 1;
}
printf("%lld %d %d\n",year,month,day);
}
return 0;
}