zoj3939

来源:互联网 发布:旅游cms开源 编辑:程序博客网 时间:2024/05/22 17:49
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给的特别大,如果直接暴力,肯定超时。所以要先对N进行预处理。因为对于年份来说,每400年是一个大循环,而且每400年的天数刚刚好是7的倍数。所以只需要先计算出对当下输入的日期后400年的Lucky week数目num,然后让N对num取模。再计算这个暴力取模后num是几号,再在这个基础上加上去掉的整数个400年就可以了。

#include <iostream>#include <stdio.h>#include <cstring>#include <string>#include <cmath>#include <algorithm>using namespace std;int a, b, c;int n;int T;int dp[2][13][4];int a1[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30,31};int a2[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31,30,31};int main(){  cin >> T;  while(T--) {    scanf("%d%d%d%d", &a, &b, &c, &n);    if(n == 1) {      printf("%d %d %d\n", a, b, c);      continue;    }    int res = 0;    int m = b;//月分    int flag ;//是否闰年    if((a%4 == 0 && a%100 != 0) || a%400 == 0) flag = 0;      else flag = 1;    int y = a;//年分    int d = c;    for (int k = 1; k <= 20871; k++) {      d += 7;      if(flag && d/a1[m] >= 1 && d/a1[m] != 0 ) {  //不是闰年            d = d%a1[m];            m++;            if(m > 12) {m = 1; y++;}            if((y%4 == 0 && y%100 != 0) || y%400 == 0) flag = 0;          }          if(!flag && d/a2[m] >= 1 && d%a2[m] != 0){ //是闰年            d = d%a2[m];            m++;            if(m > 12) {              m = 1; y++;              flag = 1;            }          }        if(d == 1 || d == 11 || d == 21) res++;}    int w = n/res;    n = n%res;    if((a%4 == 0 && a%100 != 0) || a%400 == 0) flag = 0;      else flag = 1;      res = 1;      m = b;      y = a;      d = c;      for (int k = 1; k <= 20871; k++) {        d += 7;        if(flag && (d/a1[m]) >= 1 && d/a1[m] != 0) {          d = d%a1[m]; //不是闰年          m++;          if(m > 12) {m = 1; y++;}          if((y%4 == 0 && y%100 != 0) || y%400 == 0) flag = 0;        }        if(!flag && (d/a2[m]) >= 1&& d/a2[m] != 0){ //是闰年          d = d%a2[m];          m++;          if(m > 12) {            m = 1; y++;            flag = 1;          }        }        if(d == 1 || d == 11 || d == 21) res++;        if(res == n) {          printf("%d %d %d\n", y+w*400, m, d);          break;        }      }  }    return 0;}

0 0