ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Game of Primes (博弈)

2022-03-06 12:03:28  阅读:151  来源: 互联网

标签:Case le 博弈 int Alice tot Game Primes Bob


思路:

  • 找一个人作为带入,我就是他。(选择情况数少的) 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

 

标签:Case,le,博弈,int,Alice,tot,Game,Primes,Bob
来源: https://www.cnblogs.com/Lamboofhome/p/15971259.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有