zoukankan      html  css  js  c++  java
  • Calendar Game

    Calendar Game

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    Problem 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. 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
     
    分析:判断当前状态的胜负,只需看后继是否存在必败态;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <cassert>
    #include <ctime>
    #define rep(i,m,n) for(i=m;i<=(int)n;i++)
    #define inf 0x3f3f3f3f
    #define mod 1000000007
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    #define ls (rt<<1)
    #define rs (rt<<1|1)
    #define all(x) x.begin(),x.end()
    const int maxn=5e5+10;
    const int N=2e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qmul(ll p,ll q,ll mo){ll f=0;while(q){if(q&1)f=(f+p)%mo;p=(p+p)%mo;q>>=1;}return f;}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t;
    bool sg[maxn];
    bool leap(int yy)
    {
        return (yy%4==0&&yy%100!=0)||yy%400==0;
    }
    int day(int yy,int mm)
    {
        if(mm<=7)
        {
            if(mm&1)return 31;
            else if(mm==2)return leap(yy)?29:28;
            else return 30;
        }
        else return mm&1?30:31;
    }
    int id(int yy,int mm,int dd)
    {
        int ret=0;
        int i;
        rep(i,1900,yy-1)ret+=leap(i)?366:365;
        rep(i,1,mm-1)ret+=day(yy,i);
        ret+=dd;
        return ret;
    }
    void locate(int x,int &yy,int &mm,int &dd)
    {
        yy=1900;
        int d;
        while(x>(d=leap(yy)?366:365))x-=d,yy++;
        mm=1;
        while(x>(d=day(yy,mm)))x-=d,mm++;
        dd=x;
    }
    bool vaild(int yy,int mm,int dd)
    {
        return dd<=day(yy,mm);
    }
    void init()
    {
        int st=id(1900,1,1),ed=id(2001,11,4);
        sg[ed]=false;
        int i;
        for(i=ed-1;i>=st;i--)
        {
            if(!sg[i+1])
            {
                sg[i]=true;
                continue;
            }
            else
            {
                int ny,nm,nd;
                locate(i,ny,nm,nd);
                if(nm==12)ny++,nm=1;
                else nm++;
                if(vaild(ny,nm,nd)&&id(ny,nm,nd)<=ed&&!sg[id(ny,nm,nd)])
                {
                    sg[i]=true;
                }
                else sg[i]=false;
            }
        }
    }
    int main(){
        int i,j;
        init();
        scanf("%d",&t);
        while(t--)
        {
            int yy,mm,dd;
            scanf("%d%d%d",&yy,&mm,&dd);
            puts(sg[id(yy,mm,dd)]?"YES":"NO");
        }
        return 0;
    }
  • 相关阅读:
    DOM基础(二)
    DOM基础(一)
    JS入门(五)
    linux的用法
    一道关于运行顺序题
    vue框架
    HTML的知识点
    从队友那偷来的主席树模板(静态区间第k小)
    网络流基础模型——任务分配模型(HDU 3572)
    HDU 5521 Meeting(建图思维)
  • 原文地址:https://www.cnblogs.com/dyzll/p/7597522.html
Copyright © 2011-2022 走看看