2013
11-27

Calendar Game

Adam and Eve enter this year’s ACM International Collegiate Programming Contest. Last night, they played the Calendar Game, in celebration of this contest. This game consists of the dates from January 1, 1900 to November 4, 2001, the contest day. The game starts by randomly choosing a date from this interval. Then, the players, Adam and Eve, make moves in their turn with Adam moving first: Adam, Eve, Adam, Eve, etc. There is only one rule for moves and it is simple: from a current date, a player in his/her turn can move either to the next calendar date or the same day of the next month. When the next month does not have the same day, the player moves only to the next calendar date. For example, from December 19, 1924, you can move either to December 20, 1924, the next calendar date, or January 19, 1925, the same day of the next month. From January 31 2001, however, you can move only to February 1, 2001, because February 31, 2001 is invalid.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.

The input consists of T test cases. The number of test cases (T) is given in the first line of the input. Each test case is written in a line and corresponds to an initial date. The three integers in a line, YYYY MM DD, represent the date of the DD-th day of MM-th month in the year of YYYY. Remember that initial dates are randomly chosen from the interval between January 1, 1900 and November 4, 2001.

Print exactly one line for each test case. The line should contain the answer “YES” or “NO” to the question of whether Adam has a winning strategy against Eve. Since we have T test cases, your program should output totally T lines of “YES” or “NO”.

3
2001 11 3
2001 11 2
2001 10 3

YES
NO
NO

2011-12-23 08:57:38

mark：记忆化爆搜。

# include <stdio.h>
# include <string.h>

int dp[2200][15][35] ;
int month[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}} ;

int leap(int y){return (y%4==0 && y%100!= 0) || (y%400==0) ;}

int gao(int y, int m, int d)
{
int yy = y, mm = m, dd = d ;
if (y > 2001) return 1 ;
if (y == 2001 && m > 11) return 1 ;
if (y == 2001 && m == 11 && d > 4) return 1 ;
if (dp[y][m][d] != -1) return dp[y][m][d] ;

//next day
dd = d+1 ;
if (dd > month[leap(y)][m])
dd = 1, mm++ ;
if (mm > 12)
mm = 1, yy++ ;
if (gao(yy, mm, dd) == 0) return dp[y][m][d] = 1 ;

//next month
dd = d, mm = m+1, yy = y ;
if (mm > 12) mm = 1, yy++ ;
if (dd <= month[leap(yy)][mm]) //日期存在
if (gao(yy, mm, dd) == 0) return dp[y][m][d] = 1 ;
return dp[y][m][d] = 0 ;
}

int main ()
{
int T, yy, mm, dd ;
memset (dp, -1, sizeof(dp)) ;
dp[2001][11][4] = 0 ;
scanf ("%d", &T) ;
while (T--)
{
scanf ("%d %d %d", &yy, &mm, &dd) ;
puts (gao(yy, mm, dd) ? "YES" : "NO") ;
}
return 0 ;
}

1. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count

2. 老实说，这种方法就是穷举，复杂度是2^n，之所以能够AC是应为题目的测试数据有问题，要么数据量很小，要么能够得到k == t，否则即使n = 30，也要很久才能得出结果，本人亲测

3. 算法是程序的灵魂，算法分简单和复杂，如果不搞大数据类，程序员了解一下简单点的算法也是可以的，但是会算法的一定要会编程才行，程序员不一定要会算法，利于自己项目需要的可以简单了解。