hdu 1079 Calendar Game sg函数 难度:0
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;
}
hdu 1079 Calendar Game sg函数 难度:0的更多相关文章
- hdu 1536&&1944 S-Nim sg函数 难度:0
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 3032 Nim or not Nim? sg函数 难度:0
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3032(博弈sg函数)
题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...
- HDU 5724 Chess(SG函数+状态压缩)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...
- HDU 1079 Calendar Game (博弈或暴搜)
题意:给定一个日期,然后 A 和 B 双方进行操作,谁先把日期变成2001年11月04日,将获胜,如果超过该日期,则输了,就两种操作. 第一种:变成下一天,比如现在是2001.11.3 变成 2001 ...
- HDU 5724 Chess(SG函数)
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 1535 S-Nim(SG函数)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU 1079 Calendar Game (博弈论-sg)
版权声明:欢迎关注我的博客,本文为博主[炒饭君]原创文章.未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/32336485 C ...
- hdu 1517 A Multiplication Game 段sg 博弈 难度:0
A Multiplication Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
随机推荐
- vue-cli中的.babelrc文件介绍
转载自:http://www.cnblogs.com/ye-hcj/p/7071850.html { // 此项指明,转码的规则 "presets": [ //个人认为多此一举 [ ...
- The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551
地址:http://acm.uestc.edu.cn/#/problem/show/1551 题目: Hesty Str1ng Time Limit: 3000/1000MS (Java/Others ...
- 粗略介绍Java AQS的实现原理
本文转自 http://www.importnew.com/24006.html 感谢作者 对我很有帮助 ①引言 AQS是JDK1.5提供的一个基于FIFO等待队列一个同步器的基础框架,java中的同 ...
- SDUT oj 2610
/*题目大意:输入一序列n个数字,然后输入m个询问,每个询问包含左边区间和右边区间,还有a和b,问你这个区间内有几个数大于等于a且小于等于b 做法:树状数组,先求出这个区间内有几个数小于a,然后求这个 ...
- Centos下给PHP7添加Xhprof性能分析
什么是 Xhprof?XHProf是facebook 开发的一个测试php性能的扩展,本文记录了在PHP应用中使用XHProf对PHP进行性能优化,查找性能瓶颈的方法. 它报告函数级别的请求次数和各种 ...
- RESTful风格与RESTful Api
REST(representational state transfer)(表述性状态转移),词汇解析: 1.representational 表述性:指资源以用各种形式来表述,包括 XML.JSON ...
- Jquery11 动画效果
学习要点: 1.显示.隐藏 2.滑动.卷动 3.淡入.淡出 4.自定义动画 5.列队动画方法 6.动画相关方法 7.动画全局属性 在以前很长一段时间里,网页上的各种特效还需要采用 flash 在进行. ...
- 关于JavaScript对象中的一切(二) -- 继承
先上一张我制作的思维导图.
- JS时间和字符串的相互转换 Date+String
1.js字符串转换成时间 1.1方法一:输入的时间格式为yyyy-MM-dd function convertDateFromString(dateString) { if (dateString) ...
- 访问ashx一般应用程序
浏览器中的地址栏键入要访问页面的地址:回车(是和服务器软件打交道)----向服务器发送请求(以http协议为基础,服务器按照此协议解释理解接收到的数据),服务器接收到发送的请求,根据请求信息知道当前所 ...