(簡單博弈,奇偶輪換性)Calendar Game

Calendar Game
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4826   Accepted: 2274

Description

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.

Input

The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. 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.

Output

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".

Sample Input

3 
2001 11 3 
2001 11 2 
2001 10 3 

Sample Output

YES
NO
NO

Source

題目大意:給定一個日期, 玩家輪流選擇給它"加月"或"加日",最先到達2001.11.04的勝利,問先手必勝還是必敗?
分析:自己做了常規方法一, 後來在別人的blog發現了巧妙的方法二:
方法一: PN大法.
方法二: 奇偶輪換法.

方法一思路是常規的, 這裏只對方法二作一些個人的解析:
定義:sum爲日期中月和日的和.
一般地,新的sum比舊的sum多1,sum的值滿足奇偶輪換性,而“目標日期”11.4的sum爲奇數,自然地我們規定11.4的sum爲P態;那麼是否所有爲偶數的sum都是N態,所有爲奇數的sum都是P態呢(即所有偶數的sum都至少有一種方式更新爲奇數的sum,所有奇數的sum都只能更新爲偶數的sum)
這裏按加月和加日的選擇分兩種情況:
①如果選擇加月,新sum比舊sum多1,那麼sum的值滿足奇偶輪換性。
②如果選擇加日,如果不需要向月進位,新sum比舊sum多1,那麼sum的值滿足奇偶輪換性;如果需要向月進位,手動枚舉所有情況,發現不滿足奇偶輪換性的有五種情況:閏年的2.28→3.1,4.30→5.1,6.30→7.1,  9.30→10.1,  11.30→12.1。
所以,如果當前sum是偶數且處於這些狀態中的一種,可以選擇加月使遊戲回到奇偶循環。故所有爲偶數的sum都是N態。
但是所有爲奇數的sum都是P態嗎?不是的,從上面五種特殊情況中可見9.30→10.1和11.30→12.1並不符合這個規律。故9.30和11.30也是N態,其它奇數是P態。

綜上,如果先手一開始處於9.30或10.1則必勝,一開始處於偶數的sum也必勝(這時只要不讓對手到達9.30和10.1而使sum在符合奇偶輪換性的環境下進行即可),否則必敗。

方法一不解析, 代碼(但是注意這個代碼在POJ提交是WA, 在杭電提交是AC,估計是POJ上有些數據沒有通過,沒有再仔細找錯......):
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 20020000;
bool a[2002][13][32];
bool is_leap(int y)
{
	if (y % 100 == 0)
		return y % 400 == 0;
	else
		return y % 4 == 0;
	return false;
}
bool is_exist(int y, int m, int d)
{
	bool flag = false;

	if (m == 2)
	{
		if (is_leap(y))
		{
			if (d <= 29)		flag = true;
		}
		else
		{
			if (d <= 28)		flag = true;
		}
	}
	else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 10)
	{
		if (d <= 31)	flag = true;
	}
	else
	{
		if (d <= 30)	flag = true;
	}
	return flag;
}
bool judge(int y, int m, int d, int cho)
{
	bool flag = false;
	if (cho)
	{
		if (m == 2)
		{
			if (is_leap(y))
			{
				if (d < 29)		flag = true;
			}
			else
			{
				if (d < 28)		flag = true;
			}
		}
		else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 10)
		{
			if (d<31)	flag = true;
		}
		else
		{
			if (d<30)	flag = true;
		}

		if (flag)
			return a[y][m][d + 1];
		else
		{
			if (m < 12)
				return a[y][m + 1][1];
			else
				return a[y + 1][1][1];
		}
	}
	else if (!cho)
	{
		if (m == 12)
		{
			m = 1;
			y++;
		}
		else m++;
		if (is_exist(y, m, d))
		{
			if (y * 10000 + m * 100 + d <= 20011104)
				return a[y][m][d];
		}
		else
			return true;
	}
}
void init()
{
	a[2001][11][4] = false;
	int y, m, d, cho;
	for (y = 2001; y >= 1900; y--)
	for (m = 12; m >= 1; m--)
	for (d = 31; d >= 1; d--)
	{
		if (y * 10000 + m * 100 + d >= 20011104)	continue;
		if (is_exist(y, m, d))
		{
			a[y][m][d] = false;
			for (cho = 0; cho <= 1; cho++)
				a[y][m][d] |= (judge(y, m, d, cho)==false);
		}

	}
}
int main()
{
	init();
	int T;
	cin >> T;
	while (T--)
	{
		int y, m, d;
		cin >> y >> m >> d;
		if (a[y][m][d])
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
}

方法二,奇偶輪換性:
#include <iostream>
using namespace std;

bool win(int m,int d)
{
	if((m+d)%2==0)return true;
	if(d==30 && (m==9 ||m==11))return true;
	return false;
}

int main()
{
	freopen("in.txt","r",stdin);
	int i,n;
	int y,m,d;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>y>>m>>d;
		if(win(m,d))cout<<"YES"<<endl;
		else  cout<<"NO"<<endl;
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章