Calendar Game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2766 Accepted Submission(s): 1594
A player wins the game when he/she exactly reaches the date of November 4, 2001. If a player moves to a date after November 4, 2001, he/she looses the game.
Write a program that decides whether, given an initial date, Adam, the first mover, has a winning strategy.
For this game, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.
2001 11 3
2001 11 2
2001 10 3
NO
NO
思路:sg打表,为了不爆栈从高往低打表,注意日期转换
经验教训:代码风格太凌乱了导致没发现bug
#include <cstdio>
#include <cstring>
using namespace std;
int dayofmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int sg[45000];//记录结果
bool isgapyear(int year){//是否间隔年
if(year%4==0&&(year%100!=0||year%400==0))return true;
return false;
}
int getdofm(int year,int month){//得到这个月天数
int ans=dayofmonth[month];
if(month==1&&isgapyear(year))ans++;
return ans;
}
int getdate(int year,int month,int day){//得到从1970.1.1开始的天数
int date=0;
for(int i=1900;i<year;i++)date+=(isgapyear(i)?366:365);
for(int i=0;i<month;i++){
date+=getdofm(year,i);
}
date+=day;
return date;
}
void getcalendar(int date ,int& year,int &month,int &day){//由从1970开始的天数得到年月日
int accu=0;
for(int i=0;i<102;i++){
accu+=(isgapyear(i+1900)?366:365);
if(date<accu){
year=i;date-=accu-(isgapyear(i+1900)?366:365);break;
}
else if(date==accu){
year=i;day=31;month=11;return ;
}
}
accu=0;
for(int i=0;i<12;i++){
int dm=getdofm(year+1900,i);
accu+=dm;
if(accu>date){
month=i;
date-=accu-dm;
break;
}
else if(accu==date){
month=i;
date=dm;
break;
}
}
day=date;
}
int seg(int date){//sg函数
if(sg[date]!=-1)return sg[date];
int year,month,day;
getcalendar(date,year,month,day);
if(seg(date+1)==0)return sg[date]=1;//日历左移
int ty,tm,td;getcalendar(date+getdofm(year+1900,month),ty,tm,td);
if((day==td&&date+getdofm(year+1900,month)<=37198)&&seg(date+getdofm(year+1900,month))==0)return sg[date]=1;//如果日历下一个月存在这一天,试着移一个月
return sg[date]=0;
}
int main(){
memset(sg,-1,sizeof(sg));
sg[37198]=0;
for(int i=37197 ;i>0;i--){
seg(i);
}
int y,m,d;
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&y,&m,&d);m--;
int date=getdate(y,m,d);
printf("%s\n",seg(date)==0?"NO":"YES");
}
return 0;
}