1. 程式人生 > 其它 >Game of Primes (博弈)

Game of Primes (博弈)

思路:

  • 找一個人作為帶入,我就是他。(選擇情況數少的) Alice
  • 想想一些讓別人浴霸不能的步驟和做法,看看這個做法能不能讓自己贏,不行的話自己就不能贏。(自己取勝的條件本來就處於劣勢)
  • 就是 x-1,y-1, 他選x,我就選y,他選y,我就選x。

attention:

1 初始情況可能要特判,更具自己的程式碼

Alice and Bob always like playing games with each other and today they found a new game about primes.

There are two positive integers xx and yy 
in the game, and Alice and Bob move in turn. At each turn, the current player can choose one integer and subtract it by 11 (making (x, y)(x,y) to (x - 1, y)(x−1,y) or to ( x, y - 1)(x,y−1)). The game ends when one of following conditions is met and the winner is specified at the same time: When xx or yy equals to KK: Bob wins. When xx and yy are both primes: Alice wins. When both of the previous conditions are satisfied at the same time: Bob wins. Now xx, yy, KK and who moves first are given, can you determine who will
finally win the game if they both play optimally? Input The first line of input contains an integer TT, representing the number of test cases. Then following TT lines and each line contains one test case. For each test case, there are four integers xx, yy, KK and ww separated by exactly one space. xx,yy,KK are mentioned above. w=0w=0
when Alice moves first and w = 1w=1 when Bob moves first. Output For each test case, you should output Case xx: name in one line, where xx indicates the case number starting from 1, and name is the player who will win the game. Sample 1 Inputcopy Outputcopy 4 4 9 2 0 7 10 2 0 6 39 2 0 5 28 2 0 Case 1: Alice Case 2: Alice Case 3: Alice Case 4: Bob Note 1 \le T \le 1001≤T≤100 2 \le x, y \le 10^62≤x,y≤10 6 2 \le K \le \min(x, y)2≤K≤min(x,y) 0 \le w \le 10≤w≤1 For 90\%90% test cases: \max(x, y) \le 1000max(x,y)≤1000
View problem
#include <bits/stdc++.h>
using namespace std;
#define ri register int
#define M 1000005

template <class G> void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
 } 
 

int flag[M],q[M];
void init()
{
    int r=0;
    for(ri i=2;i<=1e6;i++)
    {
        if(!flag[i])
        {
            q[++r]=i;
        }
        for(ri j=1;j<=r;j++)
        {
            if(i*q[j]>1e6) break;//
            flag[i*q[j]]=1;
            if(i%q[j]==0) break;
        }
    }
}
int k;
bool ck(int x,int y)
{
    while(x>k&&y>k)
    {
        if(!flag[x]&&!flag[y])
        {
            return 1;
        }
        x--;y--;
    }
    return 0;
}
bool pd(int x,int y)
{
            if(ck(x,y))
            {
                return 1;
               
            }
            if(ck(x-2,y)&&ck(x,y-2)) // attention
            {
                return 1;
               
            }
            return 0;
            
}
int T,x,y,w;
int main(){
    
    read(T);
    init();
    int tot=0;
    while(T--)
    {
        tot++;
        read(x);read(y);read(k);read(w);
        if((x==k||y==k&&!flag[x]&&!flag[y]))
        {
            printf("Case %d: Bob\n",tot);
            continue;
        }
        if(!flag[x]&&!flag[y])
        {
            printf("Case %d: Alice\n",tot);
            continue;
        }
        if(x==k||y==k)
        {
            printf("Case %d: Bob\n",tot);
            continue;
        }
        if(w==1)
        {
            if(pd(x,y))
            {
                printf("Case %d: Alice\n",tot);
            }
            else printf("Case %d: Bob\n",tot);
            continue;
        }
        if(w==0)
        {
            if(pd(x-1,y)||(pd(x,y-1)))
            {
                printf("Case %d: Alice\n",tot);
            }
            else printf("Case %d: Bob\n",tot);
            continue ;
        }
    }
    return 0;
    
    
    
}
View Code