链接:戳这里
思路:
这种类型的题肯定是有一个循环的,先暴力打表求出对应的周期
发现每400年一个循环,且400年里满足条件的日期有2058个
具体看代码吧
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include <ctime> #include<queue> #include<set> #include<map> #include<stack> #include<iomanip> #include<cmath> #define mst(ss,b) memset((ss),(b),sizeof(ss)) #define maxn 0x3f3f3f3f #define MAX 1000100 ///#pragma comment(linker, "/STACK:102400000,102400000") typedef long long ll; typedef unsigned long long ull; #define INF (1ll<<60)-1 using namespace std; int T; ll Y,M,D,n; int a1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int a2[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int main(){ scanf("%d",&T); while(T--){ cin>>Y>>M>>D>>n; n--; Y+=n/2058*400; n=n%2058; if(n==0) { cout<<Y<<" "<<M<<" "<<D<<endl; continue; } ll YY=Y,DD=D,MM=M; n++; int last=D,f=0,yue,k; for(ll i=Y;n;i++){ if(!f) f=1,yue=M; else yue=1; if(i%400==0 || (i%4==0 && i%100!=0) ){ for(int j=yue;j<=12&&n;j++){ for(k=last;k<=a1[j]&&n;k+=7){ if(k==1 || k==11 || k==21){ n--; if(n==0){YY=i;MM=j;DD=k;} } } last=k%a1[j]; } } else { for(int j=yue;j<=12&&n;j++){ for(k=last;k<=a2[j]&&n;k+=7){ if(k==1 || k==11 || k==21){ n--; if(n==0){YY=i;MM=j;DD=k;} } } last=k%a2[j]; } } } cout<<YY<<" "<<MM<<" "<<DD<<endl; } return 0; }